summaryrefslogtreecommitdiff
path: root/nuttx/examples/mount/mount_main.c
blob: 978b51dcea77bb6be3ae5fb56d59b915c3373aa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/****************************************************************************
 * mount_main.c
 *
 *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name Gregory Nutt nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statfs.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>

/****************************************************************************
 * Definitions
 ****************************************************************************/

#define TEST_USE_STAT         1
#define TEST_SHOW_DIRECTORIES 1
#define TEST_USE_STATFS       1

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

static const char g_source[]         = "/dev/blkdev";
static const char g_mntdir[]         = "/mnt";
static const char g_target[]         = "/mnt/fs";
static const char g_filesystemtype[] = "vfat";

static const char g_testdir1[]       = "/mnt/fs/TestDir";
static const char g_testdir2[]       = "/mnt/fs/NewDir1";
static const char g_testdir3[]       = "/mnt/fs/NewDir2";
static const char g_testdir4[]       = "/mnt/fs/NewDir3";
static const char g_testfile1[]      = "/mnt/fs/TestDir/TestFile.txt";
static const char g_testfile2[]      = "/mnt/fs/TestDir/WrTest1.txt";
static const char g_testfile3[]      = "/mnt/fs/NewDir1/WrTest2.txt";
static const char g_testfile4[]      = "/mnt/fs/NewDir3/Renamed.txt";
static const char g_testmsg[]        = "This is a write test";

static int        g_nerrors          = 0;

static char       g_namebuffer[256];

/****************************************************************************
 * Private Functions
 ****************************************************************************/

#ifdef TEST_USE_STAT
static void show_stat(const char *path, struct stat *ps)
{
  printf("%s stat:\n", path);
  printf("\tmode        : %08x\n", ps->st_mode);
  if (S_ISREG(ps->st_mode))
    {
      printf("\ttype        : File\n");
    }
  else if (S_ISDIR(ps->st_mode))
    {
      printf("\ttype        : Directory\n");
    }
  else if (S_ISCHR(ps->st_mode))
    {
      printf("\ttype        : Character driver\n");
    }
  else if (S_ISBLK(ps->st_mode))
    {
      printf("\ttype        : Block driver\n");
    }
  else
    {
      printf("\ttype        : Unknown\n");
    }

  printf("\tsize        : %d (bytes)\n",  ps->st_size);
  printf("\tblock size  : %d (bytes)\n",  ps->st_blksize);
  printf("\tsize        : %d (blocks)\n", ps->st_blocks);
  printf("\taccess time : %d\n", ps->st_atime);
  printf("\tmodify time : %d\n", ps->st_mtime);
  printf("\tchange time : %d\n", ps->st_ctime);
}
#endif

/****************************************************************************
 * Name: show_statfs
 ****************************************************************************/

#ifdef TEST_USE_STATFS
static void show_statfs(const char *path)
{
  struct statfs buf;
  int ret;

  /* Try stat() against a file or directory.  It should fail with expectederror */

  printf("show_statfs: Try statfs(%s)\n", path);
  ret = statfs(path, &buf);
  if (ret == 0)
    {
      printf("show_statfs: statfs(%s) succeeded\n", path);
      printf("\tFS Type           : %0x\n", buf.f_type);
      printf("\tBlock size        : %d\n", buf.f_bsize);
      printf("\tNumber of blocks  : %d\n", buf.f_blocks);
      printf("\tFree blocks       : %d\n", buf.f_bfree);
      printf("\tFree user blocks  : %d\n", buf.f_bavail);
      printf("\tNumber file nodes : %d\n", buf.f_files);
      printf("\tFree file nodes   : %d\n", buf.f_ffree);
      printf("\tFile name length  : %d\n", buf.f_namelen);
    }
  else
    {
      printf("show_statfs: ERROR statfs(%s) failed with errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
}
#else
# define show_statfs(p)
#endif

/****************************************************************************
 * Name: show_directories
 ****************************************************************************/

#ifdef TEST_SHOW_DIRECTORIES
static void show_directories(const char *path, int indent)
{
  DIR *dirp;
  struct dirent *direntry;
  int i;

  dirp = opendir(path);
  if ( !dirp )
    {
      printf("show_directories: ERROR opendir(\"%s\") failed with errno=%d\n", path, *get_errno_ptr());
      g_nerrors++;
      return;
    }

  for (direntry = readdir(dirp); direntry; direntry = readdir(dirp))
    {
      for (i = 0; i < 2*indent; i++)
        {
          putchar(' ');
        }
      if (DIRENT_ISDIRECTORY(direntry->d_type))
        {
          char *subdir;
          printf("%s/\n", direntry->d_name);
          sprintf(g_namebuffer, "%s/%s", path, direntry->d_name);
          subdir = strdup(g_namebuffer);
          show_directories( subdir, indent + 1);
          free(subdir);
        }
      else
        {
          printf("%s\n", direntry->d_name);
        }
    }

  closedir(dirp);
}
#else
# define show_directories(p,i)
#endif

/****************************************************************************
 * Name: fail_read_open
 ****************************************************************************/

static void fail_read_open(const char *path, int expectederror)
{
  int fd;

  printf("fail_read_open: Try open(%s) for reading\n", path);

  fd = open(path, O_RDONLY);
  if (fd >= 0)
    {
      printf("fail_read_open: ERROR open(%s) succeeded\n", path);
      g_nerrors++;
      close(fd);
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_read_open: ERROR open(%s) failed with errno=%d (expected %d)\n",
             path, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: read_test_file
 ****************************************************************************/

static void read_test_file(const char *path)
{
  char buffer[128];
  int  nbytes;
  int  fd;

  /* Read a test file that is already on the test file system image */

  printf("read_test_file: opening %s for reading\n", path);

  fd = open(path, O_RDONLY);
  if (fd < 0)
    {
      printf("read_test_file: ERROR failed to open %s, errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
  else
    {
      memset(buffer, 0, 128);
      nbytes = read(fd, buffer, 128);
      if (nbytes < 0)
        {
          printf("read_test_file: ERROR failed to read from %s, errno=%d\n",
                 path, *get_errno_ptr());
          g_nerrors++;
        }
      else
        {
          buffer[127]='\0';
          printf("read_test_file: Read \"%s\" from %s\n", buffer, path);
        }
      close(fd);
    }
}

/****************************************************************************
 * Name: write_test_file
 ****************************************************************************/

static void write_test_file(const char *path)
{
  int fd;

  /* Write a test file into a pre-existing file on the test file system */

  printf("write_test_file: opening %s for writing\n", path);

  fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
  if (fd < 0)
    {
      printf("write_test_file: ERROR failed to open %s for writing, errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
  else
    {
      int nbytes = write(fd, g_testmsg, strlen(g_testmsg));
      if (nbytes < 0)
        {
          printf("write_test_file: ERROR failed to write to %s, errno=%d\n",
                 path, *get_errno_ptr());
          g_nerrors++;
        }
      else
        {
          printf("write_test_file: wrote %d bytes to %s\n", nbytes, path);
        }
      close(fd);
    }
}

/****************************************************************************
 * Name: fail_mkdir
 ****************************************************************************/

static void fail_mkdir(const char *path, int expectederror)
{
  int ret;

  /* Try mkdir() against a file or directory.  It should fail with expectederror */

  printf("fail_mkdir: Try mkdir(%s)\n", path);

  ret = mkdir(path, 0666);
  if (ret == 0)
    {
      printf("fail_mkdir: ERROR mkdir(%s) succeeded\n", path);
      g_nerrors++;
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_mkdir: ERROR mkdir(%s) failed with errno=%d (expected %d)\n",
             path, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: succeed_mkdir
 ****************************************************************************/

static void succeed_mkdir(const char *path)
{
  int ret;

  printf("succeed_mkdir: Try mkdir(%s)\n", path);

  ret = mkdir(path, 0666);
  if (ret != 0)
    {
      printf("succeed_mkdir: ERROR mkdir(%s) failed with errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: fail_rmdir
 ****************************************************************************/

static void fail_rmdir(const char *path, int expectederror)
{
  int ret;

  /* Try rmdir() against a file or directory.  It should fail with expectederror */

  printf("fail_rmdir: Try rmdir(%s)\n", path);

  ret = rmdir(path);
  if (ret == 0)
    {
      printf("fail_rmdir: ERROR rmdir(%s) succeeded\n", path);
      g_nerrors++;
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_rmdir: ERROR rmdir(%s) failed with errno=%d (expected %d)\n",
             path, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: succeed_rmdir
 ****************************************************************************/

static void succeed_rmdir(const char *path)
{
  int ret;

  printf("succeed_rmdir: Try rmdir(%s)\n", path);

  ret = rmdir(path);
  if (ret != 0)
    {
      printf("succeed_rmdir: ERROR rmdir(%s) failed with errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: fail_unlink
 ****************************************************************************/

static void fail_unlink(const char *path, int expectederror)
{
  int ret;

  /* Try unlink() against a file or directory.  It should fail with expectederror */

  printf("fail_unlink: Try unlink(%s)\n", path);

  ret = unlink(path);
  if (ret == 0)
    {
      printf("fail_unlink: ERROR unlink(%s) succeeded\n", path);
      g_nerrors++;
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_unlink: ERROR unlink(%s) failed with errno=%d (expected %d)\n",
             path, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: succeed_unlink
 ****************************************************************************/

static void succeed_unlink(const char *path)
{
  int ret;

  /* Try unlink() against the test file.  It should succeed. */

  printf("succeed_unlink: Try unlink(%s)\n", path);

  ret = unlink(path);
  if (ret != 0)
    {
      printf("succeed_unlink: ERROR unlink(%s) failed with errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: fail_rename
 ****************************************************************************/

static void fail_rename(const char *oldpath, const char *newpath, int expectederror)
{
  int ret;

  /* Try rename() against a file or directory.  It should fail with expectederror */

  printf("fail_rename: Try rename(%s->%s)\n", oldpath, newpath);

  ret = rename(oldpath, newpath);
  if (ret == 0)
    {
      printf("fail_rename: ERROR rename(%s->%s) succeeded\n",
             oldpath, newpath);
      g_nerrors++;
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_rename: ERROR rename(%s->%s) failed with errno=%d (expected %d)\n",
             oldpath, newpath, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: succeed_rename
 ****************************************************************************/

static void succeed_rename(const char *oldpath, const char *newpath)
{
  int ret;

  printf("succeed_rename: Try rename(%s->%s)\n", oldpath, newpath);

  ret = rename(oldpath, newpath);
  if (ret != 0)
    {
      printf("succeed_rename: ERROR rename(%s->%s) failed with errno=%d\n",
             oldpath, newpath, *get_errno_ptr());
      g_nerrors++;
    }
}

/****************************************************************************
 * Name: fail_stat
 ****************************************************************************/

#ifdef TEST_USE_STAT
static void fail_stat(const char *path, int expectederror)
{
  struct stat buf;
  int ret;

  /* Try stat() against a file or directory.  It should fail with expectederror */

  printf("fail_stat: Try stat(%s)\n", path);

  ret = stat(path, &buf);
  if (ret == 0)
    {
      printf("fail_stat: ERROR stat(%s) succeeded\n", path);
      show_stat(path, &buf);
      g_nerrors++;
    }
  else if (*get_errno_ptr() != expectederror)
    {
      printf("fail_stat: ERROR stat(%s) failed with errno=%d (expected %d)\n",
             path, *get_errno_ptr(), expectederror);
      g_nerrors++;
    }
}
#else
# define fail_stat(p,e);
#endif

/****************************************************************************
 * Name: succeed_stat
 ****************************************************************************/

#ifdef TEST_USE_STAT
static void succeed_stat(const char *path)
{
  struct stat buf;
  int ret;

  printf("succeed_stat: Try stat(%s)\n", path);

  ret = stat(path, &buf);
  if (ret != 0)
    {
      printf("succeed_stat: ERROR stat(%s) failed with errno=%d\n",
             path, *get_errno_ptr());
      g_nerrors++;
    }
  else
    {
      printf("succeed_stat: stat(%s) succeeded\n", path);
      show_stat(path, &buf);
    }
}
#else
#define succeed_stat(p)
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: user_initialize
 ****************************************************************************/

void user_initialize(void)
{
}

/****************************************************************************
 * Name: user_start
 ****************************************************************************/

int user_start(int argc, char *argv[])
{
  int  ret;

  /* Mount the test file system (see arch/sim/src/up_deviceimage.c */

  printf("user_start: mounting %s filesystem at target=%s with source=%s\n",
         g_filesystemtype, g_target, g_source);

  ret = mount(g_source, g_target, g_filesystemtype, 0, NULL);
  printf("user_start: mount() returned %d\n", ret);

  if (ret == 0)
    {
      show_statfs(g_mntdir);
      show_statfs(g_target);

      /* Read a test file that is already on the test file system image */

      show_directories("", 0);
      succeed_stat(g_testfile1);
      show_statfs(g_testfile1);
      read_test_file(g_testfile1);

      /* Write a test file into a pre-existing directory on the test file system */

      fail_stat(g_testfile2, ENOENT);
      write_test_file(g_testfile2);
      show_directories("", 0);
      succeed_stat(g_testfile2);
      show_statfs(g_testfile2);

      /* Read the file that we just wrote */

      read_test_file(g_testfile2);

      /* Try rmdir() against a file on the directory.  It should fail with ENOTDIR */

      fail_rmdir(g_testfile1, ENOTDIR);

      /* Try rmdir() against the test directory.  It should fail with ENOTEMPTY */

      fail_rmdir(g_testdir1, ENOTEMPTY);

      /* Try unlink() against the test directory.  It should fail with EISDIR */

      fail_unlink(g_testdir1, EISDIR);

      /* Try unlink() against the test file1.  It should succeed. */

      succeed_unlink(g_testfile1);
      fail_stat(g_testfile1, ENOENT);
      show_directories("", 0);

      /* Attempt to open testfile1 should fail with ENOENT */

      fail_read_open(g_testfile1, ENOENT);

      /* Try rmdir() against the test directory.  It should still fail with ENOTEMPTY */

      fail_rmdir(g_testdir1, ENOTEMPTY);

      /* Try mkdir() against the test file2.  It should fail with EEXIST. */

      fail_mkdir(g_testfile2, EEXIST);

      /* Try unlink() against the test file2.  It should succeed. */

      succeed_unlink(g_testfile2);
      show_directories("", 0);
      fail_stat(g_testfile2, ENOENT);

      /* Try mkdir() against the test dir1.  It should fail with EEXIST. */

      fail_mkdir(g_testdir1, EEXIST);

      /* Try rmdir() against the test directory.  mkdir should now succeed. */

      succeed_rmdir(g_testdir1);
      show_directories("", 0);
      fail_stat(g_testdir1, ENOENT);

      /* Try mkdir() against the test dir2.  It should succeed */

      succeed_mkdir(g_testdir2);
      show_directories("", 0);
      succeed_stat(g_testdir2);
      show_statfs(g_testdir2);

      /* Try mkdir() against the test dir2.  It should fail with EXIST */

      fail_mkdir(g_testdir2, EEXIST);

      /* Write a test file into a new directory on the test file system */

      fail_stat(g_testfile3, ENOENT);
      write_test_file(g_testfile3);
      show_directories("", 0);
      succeed_stat(g_testfile3);
      show_statfs(g_testfile3);

      /* Read the file that we just wrote */

      read_test_file(g_testfile3);

      /* Use mkdir() to create test dir3.  It should succeed */

      fail_stat(g_testdir3, ENOENT);
      succeed_mkdir(g_testdir3);
      show_directories("", 0);
      succeed_stat(g_testdir3);
      show_statfs(g_testdir3);

      /* Try rename() on the root directory. Should fail with EXDEV*/

      fail_rename(g_target, g_testdir4, EXDEV);

      /* Try rename() to an existing directory.  Should fail with EEXIST */

      fail_rename(g_testdir2, g_testdir3, EEXIST);

      /* Try rename() to a non-existing directory.  Should succeed */

      fail_stat(g_testdir4, ENOENT);
      succeed_rename(g_testdir3, g_testdir4);
      show_directories("", 0);
      fail_stat(g_testdir3, ENOENT);
      succeed_stat(g_testdir4);
      show_statfs(g_testdir4);

      /* Try rename() of file.  Should work. */

      fail_stat(g_testfile4, ENOENT);
      succeed_rename(g_testfile3, g_testfile4);
      show_directories("", 0);
      fail_stat(g_testfile3, ENOENT);
      succeed_stat(g_testfile4);
      show_statfs(g_testfile4);

      /* Make sure that we can still read the renamed file */

      read_test_file(g_testfile4);

      /* Unmount the file system */

      printf("user_start: Try unmount(%s)\n", g_target);

      ret = umount(g_target);
      if (ret != 0)
        {
          printf("user_start: ERROR umount() failed, errno %d\n", *get_errno_ptr());
          g_nerrors++;
        }

      printf("user_start: %d errors reported\n", g_nerrors);
    }

  fflush(stdout);
  return 0;
}