1 | /* |
---|
2 | * This is a native test to explore how the readdir() family works. |
---|
3 | * Newlib supports the following readdir() family members: |
---|
4 | * |
---|
5 | * closedir() - |
---|
6 | * readdir() - |
---|
7 | * scandir() - |
---|
8 | * opendir() - |
---|
9 | * rewinddir() - |
---|
10 | * telldir() - BSD not in POSIX |
---|
11 | * seekdir() - BSD not in POSIX |
---|
12 | * |
---|
13 | * |
---|
14 | * seekdir() takes an offset which is a byte offset. The Linux |
---|
15 | * implementation of this appears to seek to the ((off/DIRENT_SIZE) + 1) |
---|
16 | * record where DIRENT_SIZE seems to be 12 bytes. |
---|
17 | * |
---|
18 | * |
---|
19 | * |
---|
20 | * $Id$ |
---|
21 | */ |
---|
22 | #include <stdio.h> |
---|
23 | #include <sys/types.h> |
---|
24 | #include <sys/stat.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <dirent.h> |
---|
27 | #include <string.h> |
---|
28 | #include <assert.h> |
---|
29 | #include <unistd.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <rtems.h> |
---|
32 | #include <rtems/libio.h> |
---|
33 | #include <imfs.h> |
---|
34 | |
---|
35 | extern rtems_filesystem_location_info_t rtems_filesystem_current; |
---|
36 | |
---|
37 | DIR *directory; |
---|
38 | DIR *directory2; |
---|
39 | DIR *directory3; |
---|
40 | DIR *directory_not; |
---|
41 | |
---|
42 | #ifndef __P |
---|
43 | #define __P(args)() |
---|
44 | #endif |
---|
45 | |
---|
46 | char *dnames[] = { |
---|
47 | "a", |
---|
48 | "b", |
---|
49 | "c", |
---|
50 | "d", |
---|
51 | "e", |
---|
52 | "f", |
---|
53 | "c/y", |
---|
54 | "c/z", |
---|
55 | "c/x", |
---|
56 | "c/y/a3333", |
---|
57 | "c/y/j123", |
---|
58 | "c/y/my_mount_point", |
---|
59 | "c/y/my_mount_point/my_dir", |
---|
60 | "c/z/my_mount_point", |
---|
61 | "END" |
---|
62 | }; |
---|
63 | |
---|
64 | char *fnames[] = { |
---|
65 | "a", |
---|
66 | "b", |
---|
67 | "c", |
---|
68 | "d", |
---|
69 | "e", |
---|
70 | "f", |
---|
71 | "c/y", |
---|
72 | "c/z", |
---|
73 | "c/x", |
---|
74 | "c/y/a3333", |
---|
75 | "c/y/j123", |
---|
76 | "c/y/my_mount_point", |
---|
77 | "c/y/my_mount_point/my_dir", |
---|
78 | "c/y/my_mount_point/my_dir/d", |
---|
79 | "c/z/my_mount_point", |
---|
80 | "/c/z/my_mount_point/a/../../my_mount_point/a/g", |
---|
81 | "END" |
---|
82 | }; |
---|
83 | |
---|
84 | #if defined(__rtems__) |
---|
85 | int test_main(void) |
---|
86 | #else |
---|
87 | int main( |
---|
88 | int argc, |
---|
89 | char **argv |
---|
90 | ) |
---|
91 | #endif |
---|
92 | { |
---|
93 | int i; |
---|
94 | int fd; |
---|
95 | int status; |
---|
96 | struct stat statbuf; |
---|
97 | rtems_filesystem_mount_table_entry_t *mt_entry; |
---|
98 | static char mount_point_string[25] = { "/c/z/my_mount_point" }; |
---|
99 | |
---|
100 | |
---|
101 | printf( "\n\n*** MOUNT/UNMOUNT TEST ***\n" ); |
---|
102 | |
---|
103 | /* |
---|
104 | * Change directory to the root and create files under |
---|
105 | * the base file system. |
---|
106 | */ |
---|
107 | |
---|
108 | printf( "\nchdir to the root directory\n" ); |
---|
109 | status = chdir( "/" ); |
---|
110 | printf( "chdir() status : %d\n\n", status ); |
---|
111 | |
---|
112 | printf( "\nCreating a series of directories under /\n" ); |
---|
113 | i=0; |
---|
114 | while ( strcmp(dnames[i], "END") != 0 ) |
---|
115 | { |
---|
116 | status = mkdir( dnames[i], 0777 ); |
---|
117 | printf("Creating : %25s %d %d ", dnames[i], status, errno ); |
---|
118 | if ( status == 0 ) |
---|
119 | printf(" Success\n"); |
---|
120 | else |
---|
121 | printf(" Failure\n"); |
---|
122 | |
---|
123 | i++; |
---|
124 | } |
---|
125 | |
---|
126 | /* |
---|
127 | * Create a Files with all rwx for others group and user. Verify |
---|
128 | * the created file. |
---|
129 | */ |
---|
130 | |
---|
131 | printf("create /b/my_file\n"); |
---|
132 | fd = open ("/b/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
133 | assert( fd != 0 ); |
---|
134 | close (fd); |
---|
135 | |
---|
136 | printf("Verify /b/my_file\n"); |
---|
137 | fd = open("/b/my_file", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
138 | assert( fd != 0 ); |
---|
139 | close( fd ); |
---|
140 | |
---|
141 | |
---|
142 | printf("create c/y/my_mount_point/my_dir/d\n"); |
---|
143 | fd = open ("c/y/my_mount_point/my_dir/d", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
144 | assert( fd != 0 ); |
---|
145 | close (fd); |
---|
146 | |
---|
147 | printf("Verify c/y/my_mount_point/my_dir/d\n"); |
---|
148 | fd = open("c/y/my_mount_point/my_dir/d", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
149 | assert( fd != 0 ); |
---|
150 | close( fd ); |
---|
151 | |
---|
152 | /* |
---|
153 | * Mount an the IMFS file system on the base file system. |
---|
154 | */ |
---|
155 | |
---|
156 | printf("Attempting to mount IMFS file system at /c/z/my_mount_point \n"); |
---|
157 | status = mount( |
---|
158 | &mt_entry, |
---|
159 | &IMFS_ops, |
---|
160 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
161 | NULL, |
---|
162 | mount_point_string ); |
---|
163 | assert( status == 0 ); |
---|
164 | if( mt_entry == NULL ){ |
---|
165 | printf(" NULL mount table entry was returned\n"); |
---|
166 | } |
---|
167 | else { |
---|
168 | printf("2nd file system successfully mounted at /c/z/my_mount_point \n"); |
---|
169 | } |
---|
170 | |
---|
171 | /* |
---|
172 | * Change directory to the mount point and create a group of files under |
---|
173 | * the mounted file system. |
---|
174 | */ |
---|
175 | |
---|
176 | printf( "\nchdir to /c/z/my_mount_point.\n" ); |
---|
177 | status = chdir( "/c/z/my_mount_point" ); |
---|
178 | printf( "chdir() status : %d\n\n", status ); |
---|
179 | |
---|
180 | printf( "\nCreating a series of directories under /c/z/my_mount_point\n" ); |
---|
181 | i=0; |
---|
182 | while ( strcmp(fnames[i], "END") != 0 ) |
---|
183 | { |
---|
184 | status = mkdir( fnames[i], 0777 ); |
---|
185 | printf("Creating: %46s %d %d ", fnames[i], status, errno ); |
---|
186 | if ( status == 0 ) |
---|
187 | printf(" Success\n"); |
---|
188 | else { |
---|
189 | printf(" Failure\n"); |
---|
190 | perror("errno"); |
---|
191 | } |
---|
192 | |
---|
193 | status = stat( fnames[i], &statbuf ); |
---|
194 | if ( status == -1 ) |
---|
195 | printf( ": %s\n", strerror( errno ) ); |
---|
196 | |
---|
197 | i++; |
---|
198 | } |
---|
199 | |
---|
200 | printf( "\nchdir to /\n" ); |
---|
201 | status = chdir( "/" ); |
---|
202 | printf( "chdir() status : %d\n\n", status ); |
---|
203 | |
---|
204 | /* |
---|
205 | * Unmount the first file system we mounted |
---|
206 | */ |
---|
207 | |
---|
208 | printf( "Unmount status:"); |
---|
209 | status = unmount( "/c/z/my_mount_point" ); |
---|
210 | printf( " %d\n", status ); |
---|
211 | |
---|
212 | |
---|
213 | /* |
---|
214 | * Mount a NULL filesystem and verify it fails. |
---|
215 | */ |
---|
216 | |
---|
217 | printf("Mount a NULL file system and verify EINVAL\n"); |
---|
218 | status = mount( |
---|
219 | &mt_entry, |
---|
220 | NULL, |
---|
221 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
222 | NULL, |
---|
223 | mount_point_string ); |
---|
224 | assert( status == -1 ); |
---|
225 | assert( errno == EINVAL ); |
---|
226 | |
---|
227 | /* |
---|
228 | * Verify mount with option of -62 fails with EINVAL |
---|
229 | */ |
---|
230 | |
---|
231 | printf("mount with option of -62 should fail with EINVAL\n"); |
---|
232 | status = mount( |
---|
233 | &mt_entry, |
---|
234 | &IMFS_ops, |
---|
235 | -62, |
---|
236 | NULL, |
---|
237 | "/c/y/my_mount_point" ); |
---|
238 | assert( status == -1 ); |
---|
239 | assert( errno == EINVAL ); |
---|
240 | |
---|
241 | /* |
---|
242 | * Mount a Read Only File system. |
---|
243 | */ |
---|
244 | |
---|
245 | printf("Mount a Read Only filesystem at /c/y/my_mount_point \n"); |
---|
246 | status = mount( |
---|
247 | &mt_entry, |
---|
248 | &IMFS_ops, |
---|
249 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
250 | NULL, |
---|
251 | "/c/y/my_mount_point" ); |
---|
252 | assert( status == 0 ); |
---|
253 | if( mt_entry == NULL ){ |
---|
254 | printf(" NULL mount table entry was returned\n"); |
---|
255 | } |
---|
256 | else { |
---|
257 | printf("Read only file system successfully mounted at /c/y/my_mount_point \n"); |
---|
258 | } |
---|
259 | |
---|
260 | /* |
---|
261 | * Create a directory that passes through the read only file system. |
---|
262 | */ |
---|
263 | |
---|
264 | printf("create c/y/my_mount_point/../../y/my_mount_point/new_dir\n"); |
---|
265 | status = mkdir("c/y/my_mount_point/../../y/my_mount_point/new_dir",S_IRWXU ); |
---|
266 | assert( status == 0 ); |
---|
267 | status = stat("c/y/my_mount_point/../../y/my_mount_point/new_dir",&statbuf ); |
---|
268 | assert( status == 0 ); |
---|
269 | status = stat("c/y/my_mount_point/new_dir/..", &statbuf ); |
---|
270 | assert( status == 0 ); |
---|
271 | |
---|
272 | /* |
---|
273 | * Attempt to mount a second file system at a used mount point. |
---|
274 | */ |
---|
275 | |
---|
276 | printf("Verify a mount point returns EBUSY for another mount\n"); |
---|
277 | status = mount( |
---|
278 | &mt_entry, |
---|
279 | &IMFS_ops, |
---|
280 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
281 | NULL, |
---|
282 | "/c/y/my_mount_point" ); |
---|
283 | assert( status == -1 ); |
---|
284 | assert( errno == EBUSY); |
---|
285 | |
---|
286 | /* |
---|
287 | * Attempt to mount at a file. |
---|
288 | */ |
---|
289 | |
---|
290 | printf("Mount on a file should fail with ENOTDIR\n"); |
---|
291 | status = mount( |
---|
292 | &mt_entry, |
---|
293 | &IMFS_ops, |
---|
294 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
295 | NULL, |
---|
296 | "/b/my_file" ); |
---|
297 | assert( status == -1 ); |
---|
298 | assert( errno == ENOTDIR ); |
---|
299 | |
---|
300 | |
---|
301 | /* |
---|
302 | * Verify we cannot unmount a file system while we are in it. |
---|
303 | */ |
---|
304 | |
---|
305 | printf("Create and chdir to /c/y/my_mount_point/mydir\n"); |
---|
306 | status = mkdir( "/c/y/my_mount_point/mydir", 0777); |
---|
307 | assert( status == 0 ); |
---|
308 | |
---|
309 | status = chdir( "/c/y/my_mount_point/mydir" ); |
---|
310 | assert( status == 0 ); |
---|
311 | |
---|
312 | printf("unmount of /c/y/my_mount_point should fail with EBUSY\n"); |
---|
313 | status = unmount( "/c/y/my_mount_point" ); |
---|
314 | assert( status == -1 ); |
---|
315 | assert( errno == EBUSY ); |
---|
316 | |
---|
317 | /* |
---|
318 | * Chdir to root and verify we can unmount the file system now. |
---|
319 | */ |
---|
320 | |
---|
321 | printf("chdir to / and verify we can unmount /c/y/my_mount_point\n"); |
---|
322 | status = chdir( "/" ); |
---|
323 | assert( status == 0 ); |
---|
324 | |
---|
325 | printf("unmount /c/y/my_mount_point \n"); |
---|
326 | status = unmount( "/c/y/my_mount_point" ); |
---|
327 | assert( status == 0 ); |
---|
328 | |
---|
329 | /* |
---|
330 | * Attempt to unmount a directory that does not exist. |
---|
331 | */ |
---|
332 | |
---|
333 | printf("unmount /b/mount_point should fail with EINVAL\n"); |
---|
334 | status = unmount( "/b/mount_point" ); |
---|
335 | assert( status == -1 ); |
---|
336 | assert( errno == ENOENT ); |
---|
337 | |
---|
338 | /* |
---|
339 | * Remount the filesystem. |
---|
340 | */ |
---|
341 | |
---|
342 | printf("Mount /c/y/my_mount_point\n"); |
---|
343 | status = mount( |
---|
344 | &mt_entry, |
---|
345 | &IMFS_ops, |
---|
346 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
347 | NULL, |
---|
348 | "/c/y/my_mount_point" ); |
---|
349 | assert( status == 0 ); |
---|
350 | |
---|
351 | /* |
---|
352 | * Create a file and directory then open the directory. |
---|
353 | * Verify unmount will return EBUSY while directory is open. |
---|
354 | */ |
---|
355 | |
---|
356 | printf("Create and open /c/y/my_mount_point/my_file\n"); |
---|
357 | fd = open( "/c/y/my_mount_point/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
358 | assert( fd != -1 ); |
---|
359 | status = close( fd ); |
---|
360 | assert( status == 0 ); |
---|
361 | |
---|
362 | printf("\nmkdir /c/y/my_mount_point/my_dir\n"); |
---|
363 | status = mkdir( "/c/y/my_mount_point/my_dir", 0x1c0 ); |
---|
364 | printf("Open /c/y/my_mount_point/my_dir\n"); |
---|
365 | directory = opendir( "/c/y/my_mount_point/my_dir" ); |
---|
366 | assert( directory ); |
---|
367 | |
---|
368 | printf("Unmount /c/y/my_mount_point should fail with EBUSY\n"); |
---|
369 | status = unmount( "/c/y/my_mount_point" ); |
---|
370 | assert( status == -1 ); |
---|
371 | assert( errno == EBUSY ); |
---|
372 | |
---|
373 | printf("Close /c/y/my_mount_point/my_dir\n"); |
---|
374 | status = closedir( directory ); |
---|
375 | assert( status == 0 ); |
---|
376 | |
---|
377 | /* |
---|
378 | * Attempt to unmount a directory that is not a mount point. |
---|
379 | */ |
---|
380 | |
---|
381 | printf("Unmount /c/y/my_mount_point/my_dir\n"); |
---|
382 | status = unmount( "/c/y/my_mount_point/my_dir" ); |
---|
383 | assert( status == -1 ); |
---|
384 | assert( errno == EACCES ); |
---|
385 | |
---|
386 | /* |
---|
387 | * Verify a file system can not be unmounted with a mounted file system |
---|
388 | * in it. |
---|
389 | */ |
---|
390 | |
---|
391 | printf("Mount a file system at /c/y/my_mount_point/my_dir\n"); |
---|
392 | status = mount( |
---|
393 | &mt_entry, |
---|
394 | &IMFS_ops, |
---|
395 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
396 | NULL, |
---|
397 | "/c/y/my_mount_point/my_dir"); |
---|
398 | assert( status == 0 ); |
---|
399 | |
---|
400 | printf("unmount /c/y/my_mount_point should fail with EBUSY\n"); |
---|
401 | status = unmount( "/c/y/my_mount_point" ); |
---|
402 | assert( status == -1 ); |
---|
403 | assert( errno == EBUSY ); |
---|
404 | |
---|
405 | /* |
---|
406 | * Verify you cannot create a hard link across mounted file systems. |
---|
407 | */ |
---|
408 | |
---|
409 | printf("Verify a hard link across filesystems fails with EXDEV\n"); |
---|
410 | status = mkdir( "/c/y/my_mount_point/my_dir2", S_IRWXU ); |
---|
411 | assert( status == 0 ); |
---|
412 | |
---|
413 | status = link( "/c/y/my_mount_point/my_dir2", "/c/y/my_mount_point/my_dir/my_link" ); |
---|
414 | assert( status == -1 ); |
---|
415 | assert( errno == EXDEV ); |
---|
416 | |
---|
417 | /* |
---|
418 | * Create a symbolic link across mountpoints. |
---|
419 | */ |
---|
420 | |
---|
421 | printf("Verify a symbolic link across file systems works\n"); |
---|
422 | status = symlink( "/c/y/my_mount_point/my_dir2", "/c/y/my_mount_point/my_dir/my_link" ); |
---|
423 | assert( status == 0 ); |
---|
424 | status = stat( "/c/y/my_mount_point/my_dir/my_link", &statbuf ); |
---|
425 | assert( status == 0 ); |
---|
426 | |
---|
427 | printf("unmount /c/y/my_mount_point/my_dir\n"); |
---|
428 | status = unmount( "/c/y/my_mount_point/my_dir" ); |
---|
429 | assert( status == 0 ); |
---|
430 | |
---|
431 | /* |
---|
432 | * Verify symblic link no longer works. |
---|
433 | */ |
---|
434 | |
---|
435 | printf("Verify the symbolic link now fails\n"); |
---|
436 | status = stat( "/c/y/my_mount_point/my_dir/my_link", &statbuf ); |
---|
437 | assert( status != 0 ); |
---|
438 | |
---|
439 | printf("unmount /c/y/my_mount_point\n"); |
---|
440 | status = unmount( "/c/y/my_mount_point" ); |
---|
441 | assert( status == 0 ); |
---|
442 | |
---|
443 | printf( "\n\n*** END OF MOUNT/UNMOUNT TEST ***\n" ); |
---|
444 | exit(0); |
---|
445 | } |
---|
446 | |
---|