dirseek.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * dirseek.c
  31. *
  32. * Tests seeking on directories (both legally and illegally).
  33. *
  34. * Makes a test subdirectory in the current directory.
  35. *
  36. * Intended for the file system assignment. Should run (on SFS)
  37. * when that assignment is complete.
  38. *
  39. * Note: checks a few things that are not _strictly_ guaranteed
  40. * by the official semantics of getdirentry() but that are more
  41. * or less necessary in a sane implementation, like that the
  42. * current seek position returned after seeking is the same
  43. * position that was requested. If you believe your
  44. * implementation is legal and the the test is rejecting it
  45. * gratuitously, please contact the course staff.
  46. */
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <unistd.h>
  50. #include <string.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <err.h>
  54. #define TESTDIR "seektestdir"
  55. static struct {
  56. const char *name;
  57. int make_it;
  58. off_t pos;
  59. } testfiles[] = {
  60. { ".", 0, -1 },
  61. { "..", 0, -1 },
  62. { "ridcully", 1, -1 },
  63. { "weatherwax", 1, -1 },
  64. { "ogg", 1, -1 },
  65. { "vorbis", 1, -1 },
  66. { "verence", 1, -1 },
  67. { "magrat", 1, -1 },
  68. { "agnes", 1, -1 },
  69. { "rincewind", 1, -1 },
  70. { "angua", 1, -1 },
  71. { "cherry", 1, -1 },
  72. { "dorfl", 1, -1 },
  73. { "nobby", 1, -1 },
  74. { "carrot", 1, -1 },
  75. { "vimes", 1, -1 },
  76. { "detritus", 1, -1 },
  77. { "twoflower", 1, -1 },
  78. { "teatime", 1, -1 },
  79. { "qu", 1, -1 },
  80. { NULL, 0, 0 }
  81. };
  82. /************************************************************/
  83. /* Test code */
  84. /************************************************************/
  85. static int dirfd;
  86. static
  87. int
  88. findentry(const char *name)
  89. {
  90. int i;
  91. for (i=0; testfiles[i].name; i++) {
  92. if (!strcmp(testfiles[i].name, name)) {
  93. return i;
  94. }
  95. }
  96. return -1;
  97. }
  98. static
  99. void
  100. openit(void)
  101. {
  102. dirfd = open(".", O_RDONLY);
  103. if (dirfd < 0) {
  104. err(1, ".: open");
  105. }
  106. }
  107. static
  108. void
  109. closeit(void)
  110. {
  111. if (close(dirfd)<0) {
  112. err(1, ".: close");
  113. }
  114. dirfd = -1;
  115. }
  116. static
  117. void
  118. readit(void)
  119. {
  120. char buf[4096];
  121. off_t pos;
  122. int len;
  123. int n, i, ix;
  124. for (i=0; testfiles[i].name; i++) {
  125. testfiles[i].pos = -1;
  126. }
  127. pos = lseek(dirfd, 0, SEEK_CUR);
  128. if (pos < 0) {
  129. err(1, ".: lseek(0, SEEK_CUR)");
  130. }
  131. n = 0;
  132. while ((len = getdirentry(dirfd, buf, sizeof(buf)-1)) > 0) {
  133. if ((unsigned)len >= sizeof(buf)-1) {
  134. errx(1, ".: entry %d: getdirentry returned "
  135. "invalid length %d", n, len);
  136. }
  137. buf[len] = 0;
  138. ix = findentry(buf);
  139. if (ix < 0) {
  140. errx(1, ".: entry %d: getdirentry returned "
  141. "unexpected name %s", n, buf);
  142. }
  143. if (testfiles[ix].pos >= 0) {
  144. errx(1, ".: entry %d: getdirentry returned "
  145. "%s a second time", n, buf);
  146. }
  147. testfiles[ix].pos = pos;
  148. pos = lseek(dirfd, 0, SEEK_CUR);
  149. if (pos < 0) {
  150. err(1, ".: lseek(0, SEEK_CUR)");
  151. }
  152. n++;
  153. }
  154. if (len<0) {
  155. err(1, ".: entry %d: getdirentry", n);
  156. }
  157. for (i=0; testfiles[i].name; i++) {
  158. if (testfiles[i].pos < 0) {
  159. errx(1, ".: getdirentry failed to return %s",
  160. testfiles[i].name);
  161. }
  162. }
  163. if (i!=n) {
  164. /*
  165. * If all of the other checks have passed, this should not
  166. * be able to fail. But... just in case I forgot something
  167. * or there's a bug...
  168. */
  169. errx(1, ".: getdirentry returned %d names, not %d (huh...?)",
  170. n, i);
  171. }
  172. }
  173. static
  174. void
  175. firstread(void)
  176. {
  177. off_t pos;
  178. pos = lseek(dirfd, 0, SEEK_CUR);
  179. if (pos < 0) {
  180. err(1, ".: lseek(0, SEEK_CUR)");
  181. }
  182. if (pos != 0) {
  183. errx(1, ".: File position after open not 0");
  184. }
  185. printf("Scanning directory...\n");
  186. readit();
  187. }
  188. static
  189. void
  190. doreadat0(void)
  191. {
  192. off_t pos;
  193. printf("Rewinding directory and reading it again...\n");
  194. pos = lseek(dirfd, 0, SEEK_SET);
  195. if (pos < 0) {
  196. err(1, ".: lseek(0, SEEK_SET)");
  197. }
  198. if (pos != 0) {
  199. errx(1, ".: lseek(0, SEEK_SET) returned %ld", (long) pos);
  200. }
  201. readit();
  202. }
  203. static
  204. void
  205. readone(const char *shouldbe)
  206. {
  207. char buf[4096];
  208. int len;
  209. len = getdirentry(dirfd, buf, sizeof(buf)-1);
  210. if (len < 0) {
  211. err(1, ".: getdirentry");
  212. }
  213. if ((unsigned)len >= sizeof(buf)-1) {
  214. errx(1, ".: getdirentry returned invalid length %d", len);
  215. }
  216. buf[len] = 0;
  217. if (strcmp(buf, shouldbe)) {
  218. errx(1, ".: getdirentry returned %s (expected %s)",
  219. buf, shouldbe);
  220. }
  221. }
  222. static
  223. void
  224. doreadone(int which)
  225. {
  226. off_t pos;
  227. pos = lseek(dirfd, testfiles[which].pos, SEEK_SET);
  228. if (pos<0) {
  229. err(1, ".: lseek(%ld, SEEK_SET)", (long) testfiles[which].pos);
  230. }
  231. if (pos != testfiles[which].pos) {
  232. errx(1, ".: lseek(%ld, SEEK_SET) returned %ld",
  233. (long) testfiles[which].pos, (long) pos);
  234. }
  235. readone(testfiles[which].name);
  236. }
  237. static
  238. void
  239. readallonebyone(void)
  240. {
  241. int i;
  242. printf("Trying to read each entry again...\n");
  243. for (i=0; testfiles[i].name; i++) {
  244. doreadone(i);
  245. }
  246. }
  247. static
  248. void
  249. readallrandomly(void)
  250. {
  251. int n, i, x;
  252. printf("Trying to read a bunch of entries randomly...\n");
  253. for (i=0; testfiles[i].name; i++);
  254. n = i;
  255. srandom(39584);
  256. for (i=0; i<512; i++) {
  257. x = (int)(random()%n);
  258. doreadone(x);
  259. }
  260. }
  261. static
  262. void
  263. readateof(void)
  264. {
  265. char buf[4096];
  266. int len;
  267. len = getdirentry(dirfd, buf, sizeof(buf)-1);
  268. if (len < 0) {
  269. err(1, ".: at EOF: getdirentry");
  270. }
  271. if (len==0) {
  272. return;
  273. }
  274. if ((unsigned)len >= sizeof(buf)-1) {
  275. errx(1, ".: at EOF: getdirentry returned "
  276. "invalid length %d", len);
  277. }
  278. buf[len] = 0;
  279. errx(1, ".: at EOF: got unexpected name %s", buf);
  280. }
  281. static
  282. void
  283. doreadateof(void)
  284. {
  285. off_t pos;
  286. int i;
  287. printf("Trying to read after going to EOF...\n");
  288. pos = lseek(dirfd, 0, SEEK_END);
  289. if (pos<0) {
  290. err(1, ".: lseek(0, SEEK_END)");
  291. }
  292. for (i=0; testfiles[i].name; i++) {
  293. if (pos <= testfiles[i].pos) {
  294. errx(1, ".: EOF position %ld below position %ld of %s",
  295. pos, testfiles[i].pos, testfiles[i].name);
  296. }
  297. }
  298. readateof();
  299. }
  300. static
  301. void
  302. inval_read(void)
  303. {
  304. char buf[4096];
  305. int len;
  306. len = getdirentry(dirfd, buf, sizeof(buf)-1);
  307. /* Any result is ok, as long as the system doesn't crash */
  308. (void)len;
  309. }
  310. static
  311. void
  312. dobadreads(void)
  313. {
  314. off_t pos, pos2, eof;
  315. int valid, i, k=0;
  316. printf("Trying some possibly invalid reads...\n");
  317. eof = lseek(dirfd, 0, SEEK_END);
  318. if (eof < 0) {
  319. err(1, ".: lseek(0, SEEK_END)");
  320. }
  321. for (pos=0; pos < eof; pos++) {
  322. valid = 0;
  323. for (i=0; testfiles[i].name; i++) {
  324. if (pos==testfiles[i].pos) {
  325. valid = 1;
  326. }
  327. }
  328. if (valid) {
  329. /* don't try offsets that are known to be valid */
  330. continue;
  331. }
  332. pos2 = lseek(dirfd, pos, SEEK_SET);
  333. if (pos2 < 0) {
  334. /* this is ok */
  335. }
  336. else {
  337. inval_read();
  338. k++;
  339. }
  340. }
  341. if (k>0) {
  342. printf("Survived %d invalid reads...\n", k);
  343. }
  344. else {
  345. printf("Couldn't find any invalid offsets to try...\n");
  346. }
  347. printf("Trying to read beyond EOF...\n");
  348. pos2 = lseek(dirfd, eof + 1000, SEEK_SET);
  349. if (pos2 < 0) {
  350. /* this is ok */
  351. }
  352. else {
  353. inval_read();
  354. }
  355. }
  356. static
  357. void
  358. dotest(void)
  359. {
  360. printf("Opening directory...\n");
  361. openit();
  362. printf("Running tests...\n");
  363. /* read the whole directory */
  364. firstread();
  365. /* make sure eof behaves right */
  366. readateof();
  367. /* read all the filenames again by seeking */
  368. readallonebyone();
  369. /* try reading at eof */
  370. doreadateof();
  371. /* read a bunch of the filenames over and over again */
  372. readallrandomly();
  373. /* rewind and read the whole thing again, to make sure that works */
  374. doreadat0();
  375. /* do invalid reads */
  376. dobadreads();
  377. /* rewind again to make sure the invalid attempts didn't break it */
  378. doreadat0();
  379. printf("Closing directory...\n");
  380. closeit();
  381. }
  382. /************************************************************/
  383. /* Setup code */
  384. /************************************************************/
  385. static
  386. void
  387. mkfile(const char *name)
  388. {
  389. int fd, i, r;
  390. static const char message[] = "The turtle moves!\n";
  391. char buf[32*sizeof(message)+1];
  392. buf[0]=0;
  393. for (i=0; i<32; i++) {
  394. strcat(buf, message);
  395. }
  396. /* Use O_EXCL, because we know the file shouldn't already be there */
  397. fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0664);
  398. if (fd<0) {
  399. err(1, "%s: create", name);
  400. }
  401. r = write(fd, buf, strlen(buf));
  402. if (r<0) {
  403. err(1, "%s: write", name);
  404. }
  405. if ((unsigned)r != strlen(buf)) {
  406. errx(1, "%s: short write (%d bytes)", name, r);
  407. }
  408. if (close(fd)<0) {
  409. err(1, "%s: close", name);
  410. }
  411. }
  412. static
  413. void
  414. setup(void)
  415. {
  416. int i;
  417. printf("Making directory %s...\n", TESTDIR);
  418. /* Create a directory */
  419. if (mkdir(TESTDIR, 0775)<0) {
  420. err(1, "%s: mkdir", TESTDIR);
  421. }
  422. /* Switch to it */
  423. if (chdir(TESTDIR)<0) {
  424. err(1, "%s: chdir", TESTDIR);
  425. }
  426. printf("Making some files...\n");
  427. /* Populate it */
  428. for (i=0; testfiles[i].name; i++) {
  429. if (testfiles[i].make_it) {
  430. mkfile(testfiles[i].name);
  431. }
  432. testfiles[i].pos = -1;
  433. }
  434. }
  435. static
  436. void
  437. cleanup(void)
  438. {
  439. int i;
  440. printf("Cleaning up...\n");
  441. /* Remove the files */
  442. for (i=0; testfiles[i].name; i++) {
  443. if (testfiles[i].make_it) {
  444. if (remove(testfiles[i].name)<0) {
  445. err(1, "%s: remove", testfiles[i].name);
  446. }
  447. }
  448. }
  449. /* Leave the dir */
  450. if (chdir("..")<0) {
  451. err(1, "..: chdir");
  452. }
  453. /* Remove the dir */
  454. if (rmdir(TESTDIR)<0) {
  455. err(1, "%s: rmdir", TESTDIR);
  456. }
  457. }
  458. int
  459. main()
  460. {
  461. setup();
  462. /* Do the whole thing twice */
  463. dotest();
  464. dotest();
  465. cleanup();
  466. return 0;
  467. }