Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4327 | Rev 4718 | ||
---|---|---|---|
Line 32... | Line 32... | ||
32 | #include <string.h> |
32 | #include <string.h> |
33 | #include <vfs/vfs.h> |
33 | #include <vfs/vfs.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | #include <fcntl.h> |
35 | #include <fcntl.h> |
36 | #include <dirent.h> |
36 | #include <dirent.h> |
- | 37 | #include <devmap.h> |
|
37 | #include <sys/types.h> |
38 | #include <sys/types.h> |
38 | #include <sys/stat.h> |
39 | #include <sys/stat.h> |
39 | #include "../tester.h" |
40 | #include "../tester.h" |
40 | 41 | ||
41 | char text[] = "O xein', angellein Lakedaimoniois hoti teide " |
42 | #define FS_TYPE "tmpfs" |
42 | "keimetha tois keinon rhemasi peithomenoi."; |
43 | #define MOUNT_POINT "/tmp" |
- | 44 | #define OPTIONS "" |
|
- | 45 | #define FLAGS 0 |
|
43 | 46 | ||
- | 47 | #define TEST_DIRECTORY MOUNT_POINT "/testdir" |
|
- | 48 | #define TEST_FILE TEST_DIRECTORY "/testfile" |
|
- | 49 | #define TEST_FILE2 TEST_DIRECTORY "/nextfile" |
|
- | 50 | ||
- | 51 | #define MAX_DEVICE_NAME 32 |
|
- | 52 | #define BUF_SIZE 16 |
|
- | 53 | ||
- | 54 | static char text[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"; |
|
- | 55 | ||
44 | char *test_vfs1(bool quiet) |
56 | static char *read_root(void) |
45 | { |
57 | { |
- | 58 | TPRINTF("Opening the root directory..."); |
|
- | 59 | ||
- | 60 | DIR *dirp = opendir("/"); |
|
- | 61 | if (!dirp) { |
|
- | 62 | TPRINTF("\n"); |
|
- | 63 | return "opendir() failed"; |
|
- | 64 | } else |
|
- | 65 | TPRINTF("OK\n"); |
|
- | 66 | ||
- | 67 | struct dirent *dp; |
|
- | 68 | while ((dp = readdir(dirp))) |
|
- | 69 | TPRINTF(" node \"%s\"\n", dp->d_name); |
|
- | 70 | closedir(dirp); |
|
- | 71 | ||
46 | int rc; |
72 | return NULL; |
- | 73 | } |
|
47 | 74 | ||
- | 75 | char *test_vfs1(void) |
|
- | 76 | { |
|
- | 77 | if (mkdir(MOUNT_POINT, 0) != 0) |
|
- | 78 | return "mkdir() failed"; |
|
- | 79 | TPRINTF("Created directory %s\n", MOUNT_POINT); |
|
- | 80 | ||
- | 81 | char null[MAX_DEVICE_NAME]; |
|
- | 82 | int null_id = devmap_null_create(); |
|
- | 83 | ||
- | 84 | if (null_id == -1) |
|
- | 85 | return "Unable to create null device"; |
|
- | 86 | ||
- | 87 | snprintf(null, MAX_DEVICE_NAME, "null%d", null_id); |
|
48 | rc = mount("tmpfs", "/", "nulldev0", "", 0); |
88 | int rc = mount(FS_TYPE, MOUNT_POINT, null, OPTIONS, FLAGS); |
49 | switch (rc) { |
89 | switch (rc) { |
50 | case EOK: |
90 | case EOK: |
51 | if (!quiet) |
- | |
52 | printf("mounted tmpfs on /\n"); |
91 | TPRINTF("Mounted /dev/%s as %s on %s\n", null, FS_TYPE, MOUNT_POINT); |
53 | break; |
92 | break; |
54 | case EBUSY: |
93 | case EBUSY: |
55 | if (!quiet) |
- | |
56 | printf("(INFO) something is already mounted on /\n"); |
94 | TPRINTF("(INFO) Filesystem already mounted on %s\n", MOUNT_POINT); |
57 | break; |
95 | break; |
58 | default: |
96 | default: |
59 | if (!quiet) |
- | |
60 | printf("(INFO) IPC returned errno %d\n", rc); |
97 | TPRINTF("(ERR) IPC returned errno %d (is tmpfs loaded?)\n", rc); |
61 | return "mount() failed."; |
98 | return "mount() failed"; |
62 | } |
99 | } |
63 | - | ||
64 | if (mkdir("/mydir", 0) != 0) |
- | |
65 | return "mkdir() failed.\n"; |
- | |
66 | if (!quiet) |
- | |
67 | printf("created directory /mydir\n"); |
- | |
68 | 100 | ||
- | 101 | if (mkdir(TEST_DIRECTORY, 0) != 0) |
|
- | 102 | return "mkdir() failed"; |
|
- | 103 | TPRINTF("Created directory %s\n", TEST_DIRECTORY); |
|
- | 104 | ||
69 | int fd0 = open("/mydir/myfile", O_CREAT); |
105 | int fd0 = open(TEST_FILE, O_CREAT); |
70 | if (fd0 < 0) |
106 | if (fd0 < 0) |
71 | return "open() failed.\n"; |
107 | return "open() failed"; |
72 | if (!quiet) |
- | |
73 | printf("created file /mydir/myfile, fd=%d\n", fd0); |
108 | TPRINTF("Created file %s (fd=%d)\n", TEST_FILE, fd0); |
74 | 109 | ||
75 | ssize_t cnt; |
- | |
76 | size_t size = sizeof(text); |
110 | size_t size = sizeof(text); |
77 | cnt = write(fd0, text, size); |
111 | ssize_t cnt = write(fd0, text, size); |
78 | if (cnt < 0) |
112 | if (cnt < 0) |
79 | return "write() failed.\n"; |
113 | return "write() failed"; |
80 | if (!quiet) |
- | |
81 | printf("written %d bytes, fd=%d\n", cnt, fd0); |
114 | TPRINTF("Written %d bytes\n", cnt); |
- | 115 | ||
82 | if (lseek(fd0, 0, SEEK_SET) != 0) |
116 | if (lseek(fd0, 0, SEEK_SET) != 0) |
83 | return "lseek() failed.\n"; |
117 | return "lseek() failed"; |
84 | if (!quiet) |
- | |
85 | printf("sought to position 0, fd=%d\n", fd0); |
118 | TPRINTF("Sought to position 0\n"); |
86 | 119 | ||
87 | char buf[10]; |
120 | char buf[BUF_SIZE]; |
88 | - | ||
89 | while ((cnt = read(fd0, buf, sizeof(buf)))) { |
121 | while ((cnt = read(fd0, buf, BUF_SIZE))) { |
90 | if (cnt < 0) |
122 | if (cnt < 0) |
91 | return "read() failed.\n"; |
123 | return "read() failed"; |
92 | if (!quiet) |
124 | |
93 | printf("read %d bytes: \"%.*s\", fd=%d\n", cnt, cnt, |
125 | TPRINTF("Read %d bytes: \".*s\"\n", cnt, cnt, buf); |
94 | buf, fd0); |
- | |
95 | } |
126 | } |
96 | 127 | ||
97 | close(fd0); |
128 | close(fd0); |
98 | - | ||
99 | DIR *dirp; |
- | |
100 | struct dirent *dp; |
- | |
101 | - | ||
102 | if (!quiet) |
- | |
103 | printf("scanning the root directory...\n"); |
- | |
104 | - | ||
105 | dirp = opendir("/"); |
- | |
106 | if (!dirp) |
- | |
107 | return "opendir() failed\n"; |
- | |
108 | while ((dp = readdir(dirp))) |
- | |
109 | printf("discovered node %s in /\n", dp->d_name); |
- | |
110 | closedir(dirp); |
- | |
111 | - | ||
112 | if (rename("/mydir/myfile", "/mydir/yourfile")) |
- | |
113 | return "rename() failed.\n"; |
- | |
114 | - | ||
115 | if (!quiet) |
- | |
116 | printf("renamed /mydir/myfile to /mydir/yourfile\n"); |
- | |
117 | - | ||
118 | if (unlink("/mydir/yourfile")) |
- | |
119 | return "unlink() failed.\n"; |
- | |
120 | 129 | ||
- | 130 | char *rv = read_root(); |
|
121 | if (!quiet) |
131 | if (rv != NULL) |
- | 132 | return rv; |
|
- | 133 | ||
- | 134 | if (rename(TEST_FILE, TEST_FILE2)) |
|
- | 135 | return "rename() failed"; |
|
- | 136 | TPRINTF("Renamed %s to %s\n", TEST_FILE, TEST_FILE2); |
|
- | 137 | ||
- | 138 | if (unlink(TEST_FILE2)) |
|
- | 139 | return "unlink() failed"; |
|
122 | printf("unlinked file /mydir/yourfile\n"); |
140 | TPRINTF("Unlinked %s\n", TEST_FILE2); |
123 | 141 | ||
124 | if (rmdir("/mydir")) |
142 | if (rmdir(TEST_DIRECTORY)) |
125 | return "rmdir() failed.\n"; |
143 | return "rmdir() failed"; |
- | 144 | TPRINTF("Removed directory %s\n", TEST_DIRECTORY); |
|
126 | 145 | ||
- | 146 | rv = read_root(); |
|
127 | if (!quiet) |
147 | if (rv != NULL) |
128 | printf("removed directory /mydir\n"); |
148 | return rv; |
129 | 149 | ||
130 | if (!quiet) |
- | |
131 | printf("scanning the root directory...\n"); |
- | |
132 | - | ||
133 | dirp = opendir("/"); |
- | |
134 | if (!dirp) |
- | |
135 | return "opendir() failed\n"; |
- | |
136 | while ((dp = readdir(dirp))) |
- | |
137 | printf("discovered node %s in /\n", dp->d_name); |
- | |
138 | closedir(dirp); |
- | |
139 | - | ||
140 | return NULL; |
150 | return NULL; |
141 | } |
151 | } |
142 | - |