summaryrefslogtreecommitdiff
path: root/nuttx/examples/mount/mount_main.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-05-21 19:24:30 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-05-21 19:24:30 +0000
commit3306f330105dafeb0423943fe21fc4a4a30eedc2 (patch)
tree82550739be4e98f5e6ccd3a850204602e0467242 /nuttx/examples/mount/mount_main.c
parent2bb67b254657c5707557082da0b23449b8309c72 (diff)
downloadpx4-nuttx-3306f330105dafeb0423943fe21fc4a4a30eedc2.tar.gz
px4-nuttx-3306f330105dafeb0423943fe21fc4a4a30eedc2.tar.bz2
px4-nuttx-3306f330105dafeb0423943fe21fc4a4a30eedc2.zip
Add FAT mkdir()
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@248 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/mount/mount_main.c')
-rw-r--r--nuttx/examples/mount/mount_main.c442
1 files changed, 283 insertions, 159 deletions
diff --git a/nuttx/examples/mount/mount_main.c b/nuttx/examples/mount/mount_main.c
index 75cc3c2ce..ef12c4bb5 100644
--- a/nuttx/examples/mount/mount_main.c
+++ b/nuttx/examples/mount/mount_main.c
@@ -39,6 +39,7 @@
#include <sys/mount.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
@@ -64,15 +65,257 @@ 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/NewDir";
static const char g_testfile1[] = "/mnt/fs/TestDir/TestFile.txt";
static const char g_testfile2[] = "/mnt/fs/TestDir/WritTest.txt";
+static const char g_testfile3[] = "/mnt/fs/NewDir/WritTest.txt";
static const char g_testmsg[] = "This is a write test";
+static int g_nerrors = 0;
+
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
+ * 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++;
+ }
+}
+
+/****************************************************************************
* Public Functions
****************************************************************************/
@@ -90,217 +333,98 @@ void user_initialize(void)
int user_start(int argc, char *argv[])
{
- char buffer[128];
- 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",
+ 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("main: mount() returned %d\n", ret);
+ printf("user_start: mount() returned %d\n", ret);
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);
+ read_test_file(g_testfile1);
- int fd = open(g_testfile1, O_RDONLY);
- if (fd < 0)
- {
- printf("main: ERROR failed to open %s, errno=%d\n",
- g_testfile1, *get_errno_ptr());
- }
- else
- {
- memset(buffer, 0, 128);
- nbytes = read(fd, buffer, 128);
- if (nbytes < 0)
- {
- printf("main: ERROR failed to read from %s, errno=%d\n",
- g_testfile1, *get_errno_ptr());
- }
- else
- {
- buffer[127]='\0';
- printf("main: Read \"%s\" from %s\n", buffer, g_testfile1);
- }
- close(fd);
- }
-
- /* Write a test file into a pre-existing file on the test file system */
+ /* Write a test file into a pre-existing directory 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: 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: ERROR failed to write to %s, errno=%d\n",
- g_testfile2, *get_errno_ptr());
- }
- else
- {
- printf("main: wrote %d bytes to %s\n", nbytes, g_testfile2);
- }
- close(fd);
- }
+ write_test_file(g_testfile2);
/* 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: ERRORfailed to open %s for reading, errno=%d\n",
- g_testfile2, *get_errno_ptr());
- }
- else
- {
- memset(buffer, 0, 128);
- nbytes = read(fd, buffer, 128);
- if (nbytes < 0)
- {
- printf("main: ERROR failed to read from %s, errno=%d\n",
- g_testfile2, *get_errno_ptr());
- }
- else
- {
- buffer[127]='\0';
- printf("main: Read \"%s\" from %s\n", buffer, g_testfile2);
- }
- close(fd);
- }
+ read_test_file(g_testfile2);
/* 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());
- }
+ fail_rmdir(g_testfile1, ENOTDIR);
/* 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());
- }
+ fail_rmdir(g_testdir1, ENOTEMPTY);
/* 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());
- }
+ fail_unlink(g_testdir1, EISDIR);
/* 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());
- }
+ succeed_unlink(g_testfile1);
/* 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());
- }
+ fail_read_open(g_testfile1, ENOENT);
/* Try rmdir() against the test directory. It should still fail with ENOTEMPTY */
- printf("main: Try rmdir(%s)\n", g_testdir1);
+ fail_rmdir(g_testdir1, ENOTEMPTY);
- 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 mkdir() against the test file2. It should fail with EEXIST. */
+
+ fail_mkdir(g_testfile2, EEXIST);
/* Try unlink() against the test file2. It should succeed. */
- printf("main: Try unlink(%s)\n", g_testfile2);
+ succeed_unlink(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 mkdir() against the test dir1. It should fail with EEXIST. */
+
+ fail_mkdir(g_testdir1, EEXIST);
/* Try rmdir() against the test directory. It should now succeed. */
- printf("main: Try rmdir(%s)\n", g_testdir1);
+ succeed_rmdir(g_testdir1);
- ret = rmdir(g_testdir1);
- if (ret != 0)
- {
- printf("main: ERROR rmdir(%s) failed with errno=%d\n",
- g_testdir1, *get_errno_ptr());
- }
+ /* Try mkdir() against the test dir2. It should succeed */
+
+ succeed_mkdir(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 */
+
+ write_test_file(g_testfile3);
+
+ /* Read the file that we just wrote */
+
+ read_test_file(g_testfile3);
/* Unmount the file system */
- printf("main: Try unmount(%s)\n", g_target);
+ printf("user_start: Try unmount(%s)\n", g_target);
ret = umount(g_target);
if (ret != 0)
{
- printf("main: ERROR umount() failed, errno %d\n", *get_errno_ptr());
+ 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);