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