00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <str.h>
00033 #include <vfs/vfs.h>
00034 #include <unistd.h>
00035 #include <fcntl.h>
00036 #include <dirent.h>
00037 #include <devmap.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include "../tester.h"
00041
00042 #define TEST_DIRECTORY "/tmp/testdir"
00043 #define TEST_FILE TEST_DIRECTORY "/testfile"
00044 #define TEST_FILE2 TEST_DIRECTORY "/nextfile"
00045
00046 #define MAX_DEVICE_NAME 32
00047 #define BUF_SIZE 16
00048
00049 static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
00050
00051 static const char *read_root(void)
00052 {
00053 TPRINTF("Opening the root directory...");
00054
00055 DIR *dirp = opendir("/");
00056 if (!dirp) {
00057 TPRINTF("\n");
00058 return "opendir() failed";
00059 } else
00060 TPRINTF("OK\n");
00061
00062 struct dirent *dp;
00063 while ((dp = readdir(dirp)))
00064 TPRINTF(" node \"%s\"\n", dp->d_name);
00065 closedir(dirp);
00066
00067 return NULL;
00068 }
00069
00070 const char *test_vfs1(void)
00071 {
00072 int rc;
00073 if ((rc = mkdir(TEST_DIRECTORY, 0)) != 0) {
00074 TPRINTF("rc=%d\n", rc);
00075 return "mkdir() failed";
00076 }
00077 TPRINTF("Created directory %s\n", TEST_DIRECTORY);
00078
00079 int fd0 = open(TEST_FILE, O_CREAT);
00080 if (fd0 < 0)
00081 return "open() failed";
00082 TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0);
00083
00084 size_t size = sizeof(text);
00085 ssize_t cnt = write(fd0, text, size);
00086 if (cnt < 0)
00087 return "write() failed";
00088 TPRINTF("Written %zd bytes\n", cnt);
00089
00090 if (lseek(fd0, 0, SEEK_SET) != 0)
00091 return "lseek() failed";
00092 TPRINTF("Sought to position 0\n");
00093
00094 char buf[BUF_SIZE];
00095 while ((cnt = read(fd0, buf, BUF_SIZE))) {
00096 if (cnt < 0)
00097 return "read() failed";
00098
00099 int _cnt = (int) cnt;
00100 if (_cnt != cnt) {
00101
00102 TPRINTF("Read %zd bytes\n", cnt);
00103 } else {
00104 TPRINTF("Read %zd bytes: \"%.*s\"\n", cnt, _cnt, buf);
00105 }
00106 }
00107
00108 close(fd0);
00109
00110 const char *rv = read_root();
00111 if (rv != NULL)
00112 return rv;
00113
00114 if (rename(TEST_FILE, TEST_FILE2))
00115 return "rename() failed";
00116 TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2);
00117
00118 if (unlink(TEST_FILE2))
00119 return "unlink() failed";
00120 TPRINTF("Unlinked %s\n", TEST_FILE2);
00121
00122 if (rmdir(TEST_DIRECTORY))
00123 return "rmdir() failed";
00124 TPRINTF("Removed directory %s\n", TEST_DIRECTORY);
00125
00126 rv = read_root();
00127 if (rv != NULL)
00128 return rv;
00129
00130 return NULL;
00131 }