diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-05-21 17:17:42 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-05-21 17:17:42 +0000 |
commit | 2bb67b254657c5707557082da0b23449b8309c72 (patch) | |
tree | 12e921379a6979b29804391ac650ed42c4c12fd1 /nuttx/examples | |
parent | d7d1d7f99d0cd4549f676cf9e450124c9c4170cb (diff) | |
download | px4-nuttx-2bb67b254657c5707557082da0b23449b8309c72.tar.gz px4-nuttx-2bb67b254657c5707557082da0b23449b8309c72.tar.bz2 px4-nuttx-2bb67b254657c5707557082da0b23449b8309c72.zip |
Add FAT rmdir and unlink
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@247 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples')
-rw-r--r-- | nuttx/examples/mount/mount_main.c | 145 |
1 files changed, 138 insertions, 7 deletions
diff --git a/nuttx/examples/mount/mount_main.c b/nuttx/examples/mount/mount_main.c index 09dc8e189..75cc3c2ce 100644 --- a/nuttx/examples/mount/mount_main.c +++ b/nuttx/examples/mount/mount_main.c @@ -63,6 +63,7 @@ static const char g_source[] = "/dev/blkdev"; 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_testfile1[] = "/mnt/fs/TestDir/TestFile.txt"; static const char g_testfile2[] = "/mnt/fs/TestDir/WritTest.txt"; static const char g_testmsg[] = "This is a write test"; @@ -93,6 +94,8 @@ int user_start(int argc, char *argv[]) int nbytes; int ret; + /* Mount the test file system (see arch/sim/src/up_deviceimage.c */ + printf("main: mounting %s filesystem at target=%s with source=%s\n", g_filesystemtype, g_target, g_source); @@ -101,12 +104,15 @@ int user_start(int argc, char *argv[]) if (ret == 0) { + /* Read a test file that is already on the test file system image */ + printf("main: opening %s for reading\n", g_testfile1); int fd = open(g_testfile1, O_RDONLY); if (fd < 0) { - printf("main: failed to open %s, errno=%d\n", g_testfile1, *get_errno_ptr()); + printf("main: ERROR failed to open %s, errno=%d\n", + g_testfile1, *get_errno_ptr()); } else { @@ -114,7 +120,8 @@ int user_start(int argc, char *argv[]) nbytes = read(fd, buffer, 128); if (nbytes < 0) { - printf("main: failed to read from %s, errno=%d\n", g_testfile1, *get_errno_ptr()); + printf("main: ERROR failed to read from %s, errno=%d\n", + g_testfile1, *get_errno_ptr()); } else { @@ -124,19 +131,23 @@ int user_start(int argc, char *argv[]) close(fd); } + /* Write a test file into a pre-existing file on the test file system */ + printf("main: opening %s for writing\n", g_testfile2); fd = open(g_testfile2, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd < 0) { - printf("main: failed to open %s for writing, errno=%d\n", g_testfile2, *get_errno_ptr()); + printf("main: ERROR failed to open %s for writing, errno=%d\n", + g_testfile2, *get_errno_ptr()); } else { int nbytes = write(fd, g_testmsg, strlen(g_testmsg)); if (nbytes < 0) { - printf("main: failed to write to %s, errno=%d\n", g_testfile2, *get_errno_ptr()); + printf("main: ERROR failed to write to %s, errno=%d\n", + g_testfile2, *get_errno_ptr()); } else { @@ -145,12 +156,15 @@ int user_start(int argc, char *argv[]) close(fd); } + /* Read the file that we just wrote */ + printf("main: opening %s for reading\n", g_testfile2); fd = open(g_testfile2, O_RDONLY); if (fd < 0) { - printf("main: failed to open %s for reading, errno=%d\n", g_testfile2, *get_errno_ptr()); + printf("main: ERRORfailed to open %s for reading, errno=%d\n", + g_testfile2, *get_errno_ptr()); } else { @@ -158,7 +172,8 @@ int user_start(int argc, char *argv[]) nbytes = read(fd, buffer, 128); if (nbytes < 0) { - printf("main: failed to read from %s, errno=%d\n", g_testfile2, *get_errno_ptr()); + printf("main: ERROR failed to read from %s, errno=%d\n", + g_testfile2, *get_errno_ptr()); } else { @@ -168,8 +183,124 @@ int user_start(int argc, char *argv[]) close(fd); } + /* Try rmdir() against a file on the directory. It should fail with ENOTDIR */ + + printf("main: Try rmdir(%s)\n", g_testfile1); + + ret = rmdir(g_testfile1); + if (ret == 0) + { + printf("main: ERROR rmdir(%s) succeeded\n", g_testfile1); + } + else if (*get_errno_ptr() != ENOTDIR) + { + printf("main: ERROR rmdir(%s) failed with errno=%d\n", + g_testfile1, *get_errno_ptr()); + } + + /* Try rmdir() against the test directory. It should fail with ENOTEMPTY */ + + printf("main: Try rmdir(%s)\n", g_testdir1); + + ret = rmdir(g_testdir1); + if (ret == 0) + { + printf("main: ERROR rmdir(%s) succeeded\n", g_testdir1); + } + else if (*get_errno_ptr() != ENOTEMPTY) + { + printf("main: ERROR rmdir(%s) failed with errno=%d\n", + g_testdir1, *get_errno_ptr()); + } + + /* Try unlink() against the test directory. It should fail with EISDIR */ + + printf("main: Try unlink(%s)\n", g_testdir1); + + ret = unlink(g_testdir1); + if (ret == 0) + { + printf("main: ERROR unlink(%s) succeeded\n", g_testdir1); + } + else if (*get_errno_ptr() != EISDIR) + { + printf("main: ERROR unlink(%s) failed with errno=%d\n", + g_testdir1, *get_errno_ptr()); + } + + /* Try unlink() against the test file1. It should succeed. */ + + printf("main: Try unlink(%s)\n", g_testfile1); + + ret = unlink(g_testfile1); + if (ret != 0) + { + printf("main: ERROR unlink(%s) failed with errno=%d\n", + g_testfile1, *get_errno_ptr()); + } + + /* Attempt to open testfile1 should fail with ENOENT */ + + printf("main: Try open(%s) for reading\n", g_testfile1); + + fd = open(g_testfile1, O_RDONLY); + if (fd >= 0) + { + printf("main: ERROR open(%s) succeeded\n", g_testfile1); + close(fd); + } + else if (*get_errno_ptr() != ENOENT) + { + printf("main: ERROR open(%s) failed with errno=%d\n", + g_testfile1, *get_errno_ptr()); + } + + /* Try rmdir() against the test directory. It should still fail with ENOTEMPTY */ + + printf("main: Try rmdir(%s)\n", g_testdir1); + + ret = rmdir(g_testdir1); + if (ret == 0) + { + printf("main: ERROR rmdir(%s) succeeded\n", g_testdir1); + } + else if (*get_errno_ptr() != ENOTEMPTY) + { + printf("main: ERROR rmdir(%s) failed with errno=%d\n", + g_testdir1, *get_errno_ptr()); + } + + /* Try unlink() against the test file2. It should succeed. */ + + printf("main: Try unlink(%s)\n", g_testfile2); + + ret = unlink(g_testfile2); + if (ret != 0) + { + printf("main: ERROR unlink(%s) failed with errno=%d\n", + g_testfile2, *get_errno_ptr()); + } + + /* Try rmdir() against the test directory. It should now succeed. */ + + printf("main: Try rmdir(%s)\n", g_testdir1); + + ret = rmdir(g_testdir1); + if (ret != 0) + { + printf("main: ERROR rmdir(%s) failed with errno=%d\n", + g_testdir1, *get_errno_ptr()); + } + + /* Unmount the file system */ + + printf("main: Try unmount(%s)\n", g_target); + ret = umount(g_target); - printf("main: umount() returned %d\n", ret); + if (ret != 0) + { + printf("main: ERROR umount() failed, errno %d\n", *get_errno_ptr()); + } } fflush(stdout); |