[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> |
---|
[0bbc89d] | 33 | #include <imfs.h> |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 103 | /* |
---|
| 104 | * Change directory to the root and create files under |
---|
| 105 | * the base file system. |
---|
| 106 | */ |
---|
| 107 | |
---|
[0895bdb] | 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 ); |
---|
[c2f9b97] | 117 | printf("Creating : %25s %d %d ", dnames[i], status, errno ); |
---|
[0895bdb] | 118 | if ( status == 0 ) |
---|
| 119 | printf(" Success\n"); |
---|
| 120 | else |
---|
| 121 | printf(" Failure\n"); |
---|
| 122 | |
---|
| 123 | i++; |
---|
| 124 | } |
---|
| 125 | |
---|
[c2f9b97] | 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"); |
---|
[0895bdb] | 132 | fd = open ("/b/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
[c2f9b97] | 133 | assert( fd != 0 ); |
---|
[0895bdb] | 134 | close (fd); |
---|
[c2f9b97] | 135 | |
---|
| 136 | printf("Verify /b/my_file\n"); |
---|
[0895bdb] | 137 | fd = open("/b/my_file", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
[c2f9b97] | 138 | assert( fd != 0 ); |
---|
[0895bdb] | 139 | close( fd ); |
---|
| 140 | |
---|
[c2f9b97] | 141 | |
---|
| 142 | printf("create c/y/my_mount_point/my_dir/d\n"); |
---|
[0895bdb] | 143 | fd = open ("c/y/my_mount_point/my_dir/d", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
[c2f9b97] | 144 | assert( fd != 0 ); |
---|
[0895bdb] | 145 | close (fd); |
---|
[c2f9b97] | 146 | |
---|
| 147 | printf("Verify c/y/my_mount_point/my_dir/d\n"); |
---|
[0895bdb] | 148 | fd = open("c/y/my_mount_point/my_dir/d", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
[c2f9b97] | 149 | assert( fd != 0 ); |
---|
[0895bdb] | 150 | close( fd ); |
---|
| 151 | |
---|
[c2f9b97] | 152 | /* |
---|
| 153 | * Mount an the IMFS file system on the base file system. |
---|
| 154 | */ |
---|
| 155 | |
---|
[0895bdb] | 156 | printf("Attempting to mount IMFS file system at /c/z/my_mount_point \n"); |
---|
| 157 | status = mount( |
---|
| 158 | &mt_entry, |
---|
| 159 | &IMFS_ops, |
---|
[dbf969e] | 160 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 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" ); |
---|
[0895bdb] | 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 ); |
---|
[c2f9b97] | 185 | printf("Creating: %46s %d %d ", fnames[i], status, errno ); |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 200 | printf( "\nchdir to /\n" ); |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 213 | /* |
---|
| 214 | * Mount a NULL filesystem and verify it fails. |
---|
| 215 | */ |
---|
[0895bdb] | 216 | |
---|
[c2f9b97] | 217 | printf("Mount a NULL file system and verify EINVAL\n"); |
---|
[0895bdb] | 218 | status = mount( |
---|
| 219 | &mt_entry, |
---|
| 220 | NULL, |
---|
[dbf969e] | 221 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
[0895bdb] | 222 | NULL, |
---|
| 223 | mount_point_string ); |
---|
| 224 | assert( status == -1 ); |
---|
| 225 | assert( errno == EINVAL ); |
---|
| 226 | |
---|
[c2f9b97] | 227 | /* |
---|
[dbf969e] | 228 | * Verify mount with option of -62 fails with EINVAL |
---|
[c2f9b97] | 229 | */ |
---|
| 230 | |
---|
[dbf969e] | 231 | printf("mount with option of -62 should fail with EINVAL\n"); |
---|
[c2f9b97] | 232 | status = mount( |
---|
| 233 | &mt_entry, |
---|
| 234 | &IMFS_ops, |
---|
[dbf969e] | 235 | -62, |
---|
[c2f9b97] | 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"); |
---|
[0895bdb] | 246 | status = mount( |
---|
| 247 | &mt_entry, |
---|
| 248 | &IMFS_ops, |
---|
[dbf969e] | 249 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
[0895bdb] | 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 { |
---|
[c2f9b97] | 257 | printf("Read only file system successfully mounted at /c/y/my_mount_point \n"); |
---|
[0895bdb] | 258 | } |
---|
| 259 | |
---|
[c2f9b97] | 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"); |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 272 | /* |
---|
| 273 | * Attempt to mount a second file system at a used mount point. |
---|
| 274 | */ |
---|
| 275 | |
---|
[c34ac295] | 276 | printf("Verify a mount point returns EBUSY for another mount\n"); |
---|
[0895bdb] | 277 | status = mount( |
---|
| 278 | &mt_entry, |
---|
| 279 | &IMFS_ops, |
---|
[dbf969e] | 280 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
[0895bdb] | 281 | NULL, |
---|
| 282 | "/c/y/my_mount_point" ); |
---|
| 283 | assert( status == -1 ); |
---|
| 284 | assert( errno == EBUSY); |
---|
| 285 | |
---|
[c2f9b97] | 286 | /* |
---|
| 287 | * Attempt to mount at a file. |
---|
| 288 | */ |
---|
| 289 | |
---|
| 290 | printf("Mount on a file should fail with ENOTDIR\n"); |
---|
[0895bdb] | 291 | status = mount( |
---|
| 292 | &mt_entry, |
---|
| 293 | &IMFS_ops, |
---|
[dbf969e] | 294 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
[0895bdb] | 295 | NULL, |
---|
| 296 | "/b/my_file" ); |
---|
| 297 | assert( status == -1 ); |
---|
[c2f9b97] | 298 | assert( errno == ENOTDIR ); |
---|
[0895bdb] | 299 | |
---|
[c2f9b97] | 300 | |
---|
| 301 | /* |
---|
| 302 | * Verify we cannot unmount a file system while we are in it. |
---|
| 303 | */ |
---|
[0895bdb] | 304 | |
---|
[c2f9b97] | 305 | printf("Create and chdir to /c/y/my_mount_point/mydir\n"); |
---|
[0895bdb] | 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 | |
---|
[4c1b914] | 312 | printf("unmount of /c/y/my_mount_point should fail with EBUSY\n"); |
---|
[0895bdb] | 313 | status = unmount( "/c/y/my_mount_point" ); |
---|
| 314 | assert( status == -1 ); |
---|
| 315 | assert( errno == EBUSY ); |
---|
| 316 | |
---|
[c2f9b97] | 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"); |
---|
[0895bdb] | 322 | status = chdir( "/" ); |
---|
| 323 | assert( status == 0 ); |
---|
| 324 | |
---|
[4c1b914] | 325 | printf("unmount /c/y/my_mount_point \n"); |
---|
[0895bdb] | 326 | status = unmount( "/c/y/my_mount_point" ); |
---|
| 327 | assert( status == 0 ); |
---|
| 328 | |
---|
[c2f9b97] | 329 | /* |
---|
| 330 | * Attempt to unmount a directory that does not exist. |
---|
| 331 | */ |
---|
| 332 | |
---|
[22542b6b] | 333 | printf("unmount /b/mount_point should fail with EINVAL\n"); |
---|
[0895bdb] | 334 | status = unmount( "/b/mount_point" ); |
---|
| 335 | assert( status == -1 ); |
---|
[c2f9b97] | 336 | assert( errno == ENOENT ); |
---|
| 337 | |
---|
| 338 | /* |
---|
| 339 | * Remount the filesystem. |
---|
| 340 | */ |
---|
[0895bdb] | 341 | |
---|
| 342 | printf("Mount /c/y/my_mount_point\n"); |
---|
| 343 | status = mount( |
---|
| 344 | &mt_entry, |
---|
| 345 | &IMFS_ops, |
---|
[dbf969e] | 346 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
[0895bdb] | 347 | NULL, |
---|
| 348 | "/c/y/my_mount_point" ); |
---|
| 349 | assert( status == 0 ); |
---|
| 350 | |
---|
[c2f9b97] | 351 | /* |
---|
| 352 | * Create a file and directory then open the directory. |
---|
| 353 | * Verify unmount will return EBUSY while directory is open. |
---|
| 354 | */ |
---|
| 355 | |
---|
[0895bdb] | 356 | printf("Create and open /c/y/my_mount_point/my_file\n"); |
---|
[c2f9b97] | 357 | fd = open( "/c/y/my_mount_point/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
[0895bdb] | 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 ); |
---|
[c2f9b97] | 371 | assert( errno == EBUSY ); |
---|
[0895bdb] | 372 | |
---|
| 373 | printf("Close /c/y/my_mount_point/my_dir\n"); |
---|
| 374 | status = closedir( directory ); |
---|
| 375 | assert( status == 0 ); |
---|
| 376 | |
---|
[c2f9b97] | 377 | /* |
---|
| 378 | * Attempt to unmount a directory that is not a mount point. |
---|
| 379 | */ |
---|
[0895bdb] | 380 | |
---|
[1e566bbb] | 381 | printf("Unmount /c/y/my_mount_point/my_dir should fail with EACCES\n"); |
---|
[c2f9b97] | 382 | status = unmount( "/c/y/my_mount_point/my_dir" ); |
---|
[0895bdb] | 383 | assert( status == -1 ); |
---|
[c2f9b97] | 384 | assert( errno == EACCES ); |
---|
| 385 | |
---|
| 386 | /* |
---|
| 387 | * Verify a file system can not be unmounted with a mounted file system |
---|
| 388 | * in it. |
---|
| 389 | */ |
---|
[0895bdb] | 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, |
---|
[dbf969e] | 395 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
[0895bdb] | 396 | NULL, |
---|
| 397 | "/c/y/my_mount_point/my_dir"); |
---|
| 398 | assert( status == 0 ); |
---|
| 399 | |
---|
[c2f9b97] | 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 ); |
---|
[0895bdb] | 404 | |
---|
[c2f9b97] | 405 | /* |
---|
| 406 | * Verify you cannot create a hard link across mounted file systems. |
---|
| 407 | */ |
---|
[0895bdb] | 408 | |
---|
[c2f9b97] | 409 | printf("Verify a hard link across filesystems fails with EXDEV\n"); |
---|
[0895bdb] | 410 | status = mkdir( "/c/y/my_mount_point/my_dir2", S_IRWXU ); |
---|
[c2f9b97] | 411 | assert( status == 0 ); |
---|
[0895bdb] | 412 | |
---|
| 413 | status = link( "/c/y/my_mount_point/my_dir2", "/c/y/my_mount_point/my_dir/my_link" ); |
---|
| 414 | assert( status == -1 ); |
---|
[c2f9b97] | 415 | assert( errno == EXDEV ); |
---|
[0895bdb] | 416 | |
---|
[c2f9b97] | 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 ); |
---|
[0895bdb] | 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 | |
---|
[c2f9b97] | 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 | |
---|
[0895bdb] | 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 | |
---|