PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/testsuites/psxtests/psxmount/test.c

https://github.com/rtemss/rtems
C | 439 lines | 271 code | 74 blank | 94 comment | 55 complexity | 8d06bcd904caa2d1d98d3435976a5bd0 MD5 | raw file
  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. * COPYRIGHT (c) 1989-2009.
  19. * On-Line Applications Research Corporation (OAR).
  20. *
  21. * The license and distribution terms for this file may be
  22. * found in the file LICENSE in this distribution or at
  23. * http://www.rtems.com/license/LICENSE.
  24. *
  25. * $Id: test.c,v 1.17 2011/02/22 09:47:34 ralf Exp $
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <dirent.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <rtems.h>
  39. #include <rtems/libio.h>
  40. #include <pmacros.h>
  41. extern rtems_filesystem_location_info_t rtems_filesystem_current;
  42. DIR *directory;
  43. DIR *directory2;
  44. DIR *directory3;
  45. DIR *directory_not;
  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. 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. #if defined(__rtems__)
  83. int test_main(void)
  84. #else
  85. int main(
  86. int argc,
  87. char **argv
  88. )
  89. #endif
  90. {
  91. int i;
  92. int fd;
  93. int status;
  94. struct stat statbuf;
  95. static char mount_point_string[25] = { "/c/z/my_mount_point" };
  96. printf( "\n\n*** MOUNT/UNMOUNT TEST ***\n" );
  97. /*
  98. * Change directory to the root and create files under
  99. * the base file system.
  100. */
  101. printf( "\nchdir to the root directory\n" );
  102. status = chdir( "/" );
  103. printf( "chdir() status : %d\n\n", status );
  104. printf( "\nCreating a series of directories under /\n" );
  105. i=0;
  106. while ( strcmp(dnames[i], "END") != 0 )
  107. {
  108. status = mkdir( dnames[i], 0777 );
  109. printf("Creating : %25s %d %d ", dnames[i], status, errno );
  110. if ( status == 0 )
  111. printf(" Success\n");
  112. else
  113. printf(" Failure\n");
  114. i++;
  115. }
  116. /*
  117. * Create a Files with all rwx for others group and user. Verify
  118. * the created file.
  119. */
  120. printf("create /b/my_file\n");
  121. fd = open ("/b/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
  122. rtems_test_assert( fd != 0 );
  123. close (fd);
  124. printf("Verify /b/my_file\n");
  125. fd = open("/b/my_file", S_IRWXU|S_IRWXG|S_IRWXO);
  126. rtems_test_assert( fd != 0 );
  127. close( fd );
  128. printf("create c/y/my_mount_point/my_dir/d\n");
  129. fd = open ("c/y/my_mount_point/my_dir/d", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
  130. rtems_test_assert( fd != 0 );
  131. close (fd);
  132. printf("Verify c/y/my_mount_point/my_dir/d\n");
  133. fd = open("c/y/my_mount_point/my_dir/d", S_IRWXU|S_IRWXG|S_IRWXO);
  134. rtems_test_assert( fd != 0 );
  135. close( fd );
  136. /*
  137. * Mount an the IMFS file system on the base file system.
  138. */
  139. printf("Attempting to mount IMFS file system at /c/z/my_mount_point \n");
  140. status = mount(
  141. "null",
  142. mount_point_string,
  143. "imfs",
  144. RTEMS_FILESYSTEM_READ_WRITE,
  145. NULL );
  146. rtems_test_assert( status == 0 );
  147. printf("2nd file system successfully mounted at /c/z/my_mount_point \n");
  148. /*
  149. * Change directory to the mount point and create a group of files under
  150. * the mounted file system.
  151. */
  152. printf( "\nchdir to /c/z/my_mount_point.\n" );
  153. status = chdir( "/c/z/my_mount_point" );
  154. printf( "chdir() status : %d\n\n", status );
  155. printf( "\nCreating a series of directories under /c/z/my_mount_point\n" );
  156. i=0;
  157. while ( strcmp(fnames[i], "END") != 0 )
  158. {
  159. status = mkdir( fnames[i], 0777 );
  160. printf("Creating: %46s %d %d ", fnames[i], status, errno );
  161. if ( status == 0 )
  162. printf(" Success\n");
  163. else {
  164. printf(" Failure\n");
  165. perror("errno");
  166. }
  167. status = stat( fnames[i], &statbuf );
  168. if ( status == -1 )
  169. printf( ": %s\n", strerror( errno ) );
  170. i++;
  171. }
  172. printf( "\nchdir to /\n" );
  173. status = chdir( "/" );
  174. printf( "chdir() status : %d\n\n", status );
  175. /*
  176. * Unmount the first file system we mounted
  177. */
  178. printf( "Unmount status:");
  179. status = unmount( "/c/z/my_mount_point" );
  180. printf( " %d\n", status );
  181. /*
  182. * Mount a NULL filesystem and verify it fails.
  183. */
  184. printf("Mount a NULL file system and verify EINVAL\n");
  185. status = mount(
  186. "null",
  187. mount_point_string,
  188. "nofound",
  189. RTEMS_FILESYSTEM_READ_WRITE,
  190. NULL );
  191. rtems_test_assert( status == -1 );
  192. rtems_test_assert( errno == EINVAL );
  193. /*
  194. * Verify mount with option of -62 fails with EINVAL
  195. */
  196. printf("mount with option of -62 should fail with EINVAL\n");
  197. status = mount(
  198. "null",
  199. "/c/y/my_mount_point",
  200. "imfs",
  201. -62,
  202. NULL );
  203. rtems_test_assert( status == -1 );
  204. rtems_test_assert( errno == EINVAL );
  205. /*
  206. * Mount a Read Only File system.
  207. */
  208. printf("Mount a Read Only filesystem at /c/y/my_mount_point \n");
  209. status = mount(
  210. "null",
  211. "/c/y/my_mount_point",
  212. "imfs",
  213. RTEMS_FILESYSTEM_READ_ONLY,
  214. NULL );
  215. rtems_test_assert( status == 0 );
  216. printf("Read only file system successfully mounted at /c/y/my_mount_point \n");
  217. /*
  218. * Create a directory that passes through the read only file system.
  219. */
  220. printf("create c/y/my_mount_point/../../y/my_mount_point/new_dir\n");
  221. status = mkdir("c/y/my_mount_point/../../y/my_mount_point/new_dir",S_IRWXU );
  222. rtems_test_assert( status == 0 );
  223. status = stat("c/y/my_mount_point/../../y/my_mount_point/new_dir",&statbuf );
  224. rtems_test_assert( status == 0 );
  225. status = stat("c/y/my_mount_point/new_dir/..", &statbuf );
  226. rtems_test_assert( status == 0 );
  227. /*
  228. * Attempt to mount a second file system at a used mount point.
  229. */
  230. printf("Verify a mount point returns EBUSY for another mount\n");
  231. status = mount(
  232. "null",
  233. "/c/y/my_mount_point",
  234. "imfs",
  235. RTEMS_FILESYSTEM_READ_ONLY,
  236. NULL );
  237. rtems_test_assert( status == -1 );
  238. rtems_test_assert( errno == EBUSY);
  239. /*
  240. * Attempt to mount at a file.
  241. */
  242. printf("Mount on a file should fail with ENOTDIR\n");
  243. status = mount(
  244. "null",
  245. "/b/my_file",
  246. "imfs",
  247. RTEMS_FILESYSTEM_READ_ONLY,
  248. NULL );
  249. rtems_test_assert( status == -1 );
  250. rtems_test_assert( errno == ENOTDIR );
  251. /*
  252. * Verify we cannot unmount a file system while we are in it.
  253. */
  254. printf("Create and chdir to /c/y/my_mount_point/mydir\n");
  255. status = mkdir( "/c/y/my_mount_point/mydir", 0777);
  256. rtems_test_assert( status == 0 );
  257. status = chdir( "/c/y/my_mount_point/mydir" );
  258. rtems_test_assert( status == 0 );
  259. printf("unmount of /c/y/my_mount_point should fail with EBUSY\n");
  260. status = unmount( "/c/y/my_mount_point" );
  261. rtems_test_assert( status == -1 );
  262. rtems_test_assert( errno == EBUSY );
  263. /*
  264. * Chdir to root and verify we can unmount the file system now.
  265. */
  266. printf("chdir to / and verify we can unmount /c/y/my_mount_point\n");
  267. status = chdir( "/" );
  268. rtems_test_assert( status == 0 );
  269. printf("unmount /c/y/my_mount_point \n");
  270. status = unmount( "/c/y/my_mount_point" );
  271. rtems_test_assert( status == 0 );
  272. /*
  273. * Attempt to unmount a directory that does not exist.
  274. */
  275. printf("unmount /b/mount_point should fail with EINVAL\n");
  276. status = unmount( "/b/mount_point" );
  277. rtems_test_assert( status == -1 );
  278. rtems_test_assert( errno == ENOENT );
  279. /*
  280. * Remount the filesystem.
  281. */
  282. printf("Mount /c/y/my_mount_point\n");
  283. status = mount(
  284. "null",
  285. "/c/y/my_mount_point",
  286. "imfs",
  287. RTEMS_FILESYSTEM_READ_ONLY,
  288. NULL );
  289. rtems_test_assert( status == 0 );
  290. /*
  291. * Create a file and directory then open the directory.
  292. * Verify unmount will return EBUSY while directory is open.
  293. */
  294. printf("Create and open /c/y/my_mount_point/my_file\n");
  295. fd = open( "/c/y/my_mount_point/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
  296. rtems_test_assert( fd != -1 );
  297. status = close( fd );
  298. rtems_test_assert( status == 0 );
  299. printf("\nmkdir /c/y/my_mount_point/my_dir\n");
  300. status = mkdir( "/c/y/my_mount_point/my_dir", 0x1c0 );
  301. printf("Open /c/y/my_mount_point/my_dir\n");
  302. directory = opendir( "/c/y/my_mount_point/my_dir" );
  303. rtems_test_assert( directory );
  304. printf("Unmount /c/y/my_mount_point should fail with EBUSY\n");
  305. status = unmount( "/c/y/my_mount_point" );
  306. rtems_test_assert( status == -1 );
  307. rtems_test_assert( errno == EBUSY );
  308. printf("Close /c/y/my_mount_point/my_dir\n");
  309. status = closedir( directory );
  310. rtems_test_assert( status == 0 );
  311. /*
  312. * Attempt to unmount a directory that is not a mount point.
  313. */
  314. printf("Unmount /c/y/my_mount_point/my_dir should fail with EACCES\n");
  315. status = unmount( "/c/y/my_mount_point/my_dir" );
  316. rtems_test_assert( status == -1 );
  317. rtems_test_assert( errno == EACCES );
  318. /*
  319. * Verify a file system can not be unmounted with a mounted file system
  320. * in it.
  321. */
  322. printf("Mount a file system at /c/y/my_mount_point/my_dir\n");
  323. status = mount(
  324. "null",
  325. "/c/y/my_mount_point/my_dir",
  326. "imfs",
  327. RTEMS_FILESYSTEM_READ_WRITE,
  328. NULL );
  329. rtems_test_assert( status == 0 );
  330. printf("unmount /c/y/my_mount_point should fail with EBUSY\n");
  331. status = unmount( "/c/y/my_mount_point" );
  332. rtems_test_assert( status == -1 );
  333. rtems_test_assert( errno == EBUSY );
  334. /*
  335. * Verify you cannot create a hard link across mounted file systems.
  336. */
  337. printf("Verify a hard link across filesystems fails with EXDEV\n");
  338. status = mkdir( "/c/y/my_mount_point/my_dir2", S_IRWXU );
  339. rtems_test_assert( status == 0 );
  340. status = link( "/c/y/my_mount_point/my_dir2", "/c/y/my_mount_point/my_dir/my_link" );
  341. rtems_test_assert( status == -1 );
  342. rtems_test_assert( errno == EXDEV );
  343. /*
  344. * Create a symbolic link across mountpoints.
  345. */
  346. printf("Verify a symbolic link across file systems works\n");
  347. status = symlink( "/c/y/my_mount_point/my_dir2", "/c/y/my_mount_point/my_dir/my_link" );
  348. rtems_test_assert( status == 0 );
  349. status = stat( "/c/y/my_mount_point/my_dir/my_link", &statbuf );
  350. rtems_test_assert( status == 0 );
  351. printf("unmount /c/y/my_mount_point/my_dir\n");
  352. status = unmount( "/c/y/my_mount_point/my_dir" );
  353. rtems_test_assert( status == 0 );
  354. /*
  355. * Verify symblic link no longer works.
  356. */
  357. printf("Verify the symbolic link now fails\n");
  358. status = stat( "/c/y/my_mount_point/my_dir/my_link", &statbuf );
  359. rtems_test_assert( status != 0 );
  360. printf("unmount /c/y/my_mount_point\n");
  361. status = unmount( "/c/y/my_mount_point" );
  362. rtems_test_assert( status == 0 );
  363. printf( "\n\n*** END OF MOUNT/UNMOUNT TEST ***\n" );
  364. rtems_test_exit(0);
  365. }