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