tst-ftell-active-handler.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* Verify that ftell returns the correct value at various points before and
  2. after the handler on which it is called becomes active.
  3. Copyright (C) 2014-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <locale.h>
  23. #include <wchar.h>
  24. static int do_test (void);
  25. #define TEST_FUNCTION do_test ()
  26. #include "../test-skeleton.c"
  27. #define get_handles_fdopen(filename, fd, fp, fd_mode, mode) \
  28. ({ \
  29. int ret = 0; \
  30. (fd) = open ((filename), (fd_mode), 0); \
  31. if ((fd) == -1) \
  32. { \
  33. printf ("open failed: %m\n"); \
  34. ret = 1; \
  35. } \
  36. else \
  37. { \
  38. (fp) = fdopen ((fd), (mode)); \
  39. if ((fp) == NULL) \
  40. { \
  41. printf ("fdopen failed: %m\n"); \
  42. close (fd); \
  43. ret = 1; \
  44. } \
  45. } \
  46. ret; \
  47. })
  48. #define get_handles_fopen(filename, fd, fp, mode) \
  49. ({ \
  50. int ret = 0; \
  51. (fp) = fopen ((filename), (mode)); \
  52. if ((fp) == NULL) \
  53. { \
  54. printf ("fopen failed: %m\n"); \
  55. ret = 1; \
  56. } \
  57. else \
  58. { \
  59. (fd) = fileno (fp); \
  60. if ((fd) == -1) \
  61. { \
  62. printf ("fileno failed: %m\n"); \
  63. ret = 1; \
  64. } \
  65. } \
  66. ret; \
  67. })
  68. /* data points to either char_data or wide_data, depending on whether we're
  69. testing regular file mode or wide mode respectively. Similarly,
  70. fputs_func points to either fputs or fputws. data_len keeps track of the
  71. length of the current data and file_len maintains the current file
  72. length. */
  73. static const void *data;
  74. static const char *char_data = "abcdef";
  75. static const wchar_t *wide_data = L"abcdef";
  76. static size_t data_len;
  77. static size_t file_len;
  78. typedef int (*fputs_func_t) (const void *data, FILE *fp);
  79. typedef void *(*fgets_func_t) (void *ws, int n, FILE *fp);
  80. fputs_func_t fputs_func;
  81. fgets_func_t fgets_func;
  82. /* This test verifies that the offset reported by ftell is correct after the
  83. file is truncated using ftruncate. ftruncate does not change the file
  84. offset on truncation and hence, SEEK_CUR should continue to point to the
  85. old offset and not be changed to the new offset. */
  86. static int
  87. do_ftruncate_test (const char *filename)
  88. {
  89. FILE *fp = NULL;
  90. int fd;
  91. int ret = 0;
  92. struct test
  93. {
  94. const char *mode;
  95. int fd_mode;
  96. } test_modes[] = {
  97. {"r+", O_RDWR},
  98. {"w", O_WRONLY | O_TRUNC},
  99. {"w+", O_RDWR | O_TRUNC},
  100. {"a", O_WRONLY},
  101. {"a+", O_RDWR}
  102. };
  103. for (int j = 0; j < 2; j++)
  104. {
  105. for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
  106. {
  107. int fileret;
  108. printf ("\tftruncate: %s (file, \"%s\"): ",
  109. j == 0 ? "fopen" : "fdopen",
  110. test_modes[i].mode);
  111. if (j == 0)
  112. fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
  113. else
  114. fileret = get_handles_fdopen (filename, fd, fp,
  115. test_modes[i].fd_mode,
  116. test_modes[i].mode);
  117. if (fileret != 0)
  118. return fileret;
  119. /* Write some data. */
  120. size_t written = fputs_func (data, fp);
  121. if (written == EOF)
  122. {
  123. printf ("fputs[1] failed to write data\n");
  124. ret |= 1;
  125. }
  126. /* Record the offset. */
  127. long offset = ftell (fp);
  128. /* Flush data to allow switching active handles. */
  129. if (fflush (fp))
  130. {
  131. printf ("Flush failed: %m\n");
  132. ret |= 1;
  133. }
  134. /* Now truncate the file. */
  135. if (ftruncate (fd, 0) != 0)
  136. {
  137. printf ("Failed to truncate file: %m\n");
  138. ret |= 1;
  139. }
  140. /* ftruncate does not change the offset, so there is no need to call
  141. anything to be able to switch active handles. */
  142. long new_offset = ftell (fp);
  143. /* The offset should remain unchanged since ftruncate does not update
  144. it. */
  145. if (offset != new_offset)
  146. {
  147. printf ("Incorrect offset. Expected %ld, but got %ld\n",
  148. offset, new_offset);
  149. ret |= 1;
  150. }
  151. else
  152. printf ("offset = %ld\n", offset);
  153. fclose (fp);
  154. }
  155. }
  156. return ret;
  157. }
  158. /* Test that ftell output after a rewind is correct. */
  159. static int
  160. do_rewind_test (const char *filename)
  161. {
  162. int ret = 0;
  163. struct test
  164. {
  165. const char *mode;
  166. int fd_mode;
  167. size_t old_off;
  168. size_t new_off;
  169. } test_modes[] = {
  170. {"w", O_WRONLY | O_TRUNC, 0, data_len},
  171. {"w+", O_RDWR | O_TRUNC, 0, data_len},
  172. {"r+", O_RDWR, 0, data_len},
  173. /* The new offsets for 'a' and 'a+' modes have to factor in the
  174. previous writes since they always append to the end of the
  175. file. */
  176. {"a", O_WRONLY, 0, 3 * data_len},
  177. {"a+", O_RDWR, 0, 4 * data_len},
  178. };
  179. /* Empty the file before the test so that our offsets are simple to
  180. calculate. */
  181. FILE *fp = fopen (filename, "w");
  182. if (fp == NULL)
  183. {
  184. printf ("Failed to open file for emptying\n");
  185. return 1;
  186. }
  187. fclose (fp);
  188. for (int j = 0; j < 2; j++)
  189. {
  190. for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
  191. {
  192. FILE *fp;
  193. int fd;
  194. int fileret;
  195. printf ("\trewind: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
  196. test_modes[i].mode);
  197. if (j == 0)
  198. fileret = get_handles_fdopen (filename, fd, fp,
  199. test_modes[i].fd_mode,
  200. test_modes[i].mode);
  201. else
  202. fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
  203. if (fileret != 0)
  204. return fileret;
  205. /* Write some content to the file, rewind and ensure that the ftell
  206. output after the rewind is 0. POSIX does not specify what the
  207. behavior is when a file is rewound in 'a' mode, so we retain
  208. current behavior, which is to keep the 0 offset. */
  209. size_t written = fputs_func (data, fp);
  210. if (written == EOF)
  211. {
  212. printf ("fputs[1] failed to write data\n");
  213. ret |= 1;
  214. }
  215. rewind (fp);
  216. long offset = ftell (fp);
  217. if (offset != test_modes[i].old_off)
  218. {
  219. printf ("Incorrect old offset. Expected %zu, but got %ld, ",
  220. test_modes[i].old_off, offset);
  221. ret |= 1;
  222. }
  223. else
  224. printf ("old offset = %ld, ", offset);
  225. written = fputs_func (data, fp);
  226. if (written == EOF)
  227. {
  228. printf ("fputs[1] failed to write data\n");
  229. ret |= 1;
  230. }
  231. /* After this write, the offset in append modes should factor in the
  232. implicit lseek to the end of file. */
  233. offset = ftell (fp);
  234. if (offset != test_modes[i].new_off)
  235. {
  236. printf ("Incorrect new offset. Expected %zu, but got %ld\n",
  237. test_modes[i].new_off, offset);
  238. ret |= 1;
  239. }
  240. else
  241. printf ("new offset = %ld\n", offset);
  242. }
  243. }
  244. return ret;
  245. }
  246. /* Test that the value of ftell is not cached when the stream handle is not
  247. active. */
  248. static int
  249. do_ftell_test (const char *filename)
  250. {
  251. int ret = 0;
  252. struct test
  253. {
  254. const char *mode;
  255. int fd_mode;
  256. size_t old_off;
  257. size_t new_off;
  258. size_t eof_off;
  259. } test_modes[] = {
  260. /* In w, w+ and r+ modes, the file position should be at the
  261. beginning of the file. After the write, the offset should be
  262. updated to data_len. We don't use eof_off in w and a modes since
  263. they don't allow reading. */
  264. {"w", O_WRONLY | O_TRUNC, 0, data_len, 0},
  265. {"w+", O_RDWR | O_TRUNC, 0, data_len, 2 * data_len},
  266. {"r+", O_RDWR, 0, data_len, 3 * data_len},
  267. /* For the 'a' mode, the initial file position should be the
  268. current end of file. After the write, the offset has data_len
  269. added to the old value. For a+ mode however, the initial file
  270. position is the file position of the underlying file descriptor,
  271. since it is initially assumed to be in read mode. */
  272. {"a", O_WRONLY, 3 * data_len, 4 * data_len, 5 * data_len},
  273. {"a+", O_RDWR, 0, 5 * data_len, 6 * data_len},
  274. };
  275. for (int j = 0; j < 2; j++)
  276. {
  277. for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
  278. {
  279. FILE *fp;
  280. int fd;
  281. int fileret;
  282. printf ("\tftell: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
  283. test_modes[i].mode);
  284. if (j == 0)
  285. fileret = get_handles_fdopen (filename, fd, fp,
  286. test_modes[i].fd_mode,
  287. test_modes[i].mode);
  288. else
  289. fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
  290. if (fileret != 0)
  291. return fileret;
  292. long off = ftell (fp);
  293. if (off != test_modes[i].old_off)
  294. {
  295. printf ("Incorrect old offset. Expected %zu but got %ld, ",
  296. test_modes[i].old_off, off);
  297. ret |= 1;
  298. }
  299. else
  300. printf ("old offset = %ld, ", off);
  301. /* The effect of this write on the offset should be seen in the ftell
  302. call that follows it. */
  303. int write_ret = write (fd, data, data_len);
  304. if (write_ret != data_len)
  305. {
  306. printf ("write failed (%m)\n");
  307. ret |= 1;
  308. }
  309. off = ftell (fp);
  310. if (off != test_modes[i].new_off)
  311. {
  312. printf ("Incorrect new offset. Expected %zu but got %ld",
  313. test_modes[i].new_off, off);
  314. ret |= 1;
  315. }
  316. else
  317. printf ("new offset = %ld", off);
  318. /* Read to the end, write some data to the fd and check if ftell can
  319. see the new ofset. Do this test only for files that allow
  320. reading. */
  321. if (test_modes[i].fd_mode != O_WRONLY)
  322. {
  323. wchar_t tmpbuf[data_len];
  324. rewind (fp);
  325. while (fgets_func (tmpbuf, data_len, fp) && !feof (fp));
  326. write_ret = write (fd, data, data_len);
  327. if (write_ret != data_len)
  328. {
  329. printf ("write failed (%m)\n");
  330. ret |= 1;
  331. }
  332. off = ftell (fp);
  333. if (off != test_modes[i].eof_off)
  334. {
  335. printf (", Incorrect offset after read EOF. "
  336. "Expected %zu but got %ld\n",
  337. test_modes[i].eof_off, off);
  338. ret |= 1;
  339. }
  340. else
  341. printf (", offset after EOF = %ld\n", off);
  342. }
  343. else
  344. putc ('\n', stdout);
  345. fclose (fp);
  346. }
  347. }
  348. return ret;
  349. }
  350. /* This test opens the file for writing, moves the file offset of the
  351. underlying file, writes out data and then checks if ftell trips on it. */
  352. static int
  353. do_write_test (const char *filename)
  354. {
  355. FILE *fp = NULL;
  356. int fd;
  357. int ret = 0;
  358. struct test
  359. {
  360. const char *mode;
  361. int fd_mode;
  362. } test_modes[] = {
  363. {"w", O_WRONLY | O_TRUNC},
  364. {"w+", O_RDWR | O_TRUNC},
  365. {"r+", O_RDWR}
  366. };
  367. for (int j = 0; j < 2; j++)
  368. {
  369. for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
  370. {
  371. int fileret;
  372. printf ("\twrite: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
  373. test_modes[i].mode);
  374. if (j == 0)
  375. fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
  376. else
  377. fileret = get_handles_fdopen (filename, fd, fp,
  378. test_modes[i].fd_mode,
  379. test_modes[i].mode);
  380. if (fileret != 0)
  381. return fileret;
  382. /* Move offset to just before the end of the file. */
  383. off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
  384. if (seek_ret == -1)
  385. {
  386. printf ("lseek failed: %m\n");
  387. ret |= 1;
  388. }
  389. /* Write some data. */
  390. size_t written = fputs_func (data, fp);
  391. if (written == EOF)
  392. {
  393. printf ("fputs[1] failed to write data\n");
  394. ret |= 1;
  395. }
  396. /* Verify that the offset points to the end of the file. The length
  397. of the file would be the original length + the length of data
  398. written to it - the amount by which we moved the offset using
  399. lseek. */
  400. long offset = ftell (fp);
  401. file_len = file_len - 1 + data_len;
  402. if (offset != file_len)
  403. {
  404. printf ("Incorrect offset. Expected %zu, but got %ld\n",
  405. file_len, offset);
  406. ret |= 1;
  407. }
  408. printf ("offset = %ld\n", offset);
  409. fclose (fp);
  410. }
  411. }
  412. return ret;
  413. }
  414. /* This test opens a file in append mode, writes some data, and then verifies
  415. that ftell does not trip over it. */
  416. static int
  417. do_append_test (const char *filename)
  418. {
  419. FILE *fp = NULL;
  420. int ret = 0;
  421. int fd;
  422. struct test
  423. {
  424. const char *mode;
  425. int fd_mode;
  426. } test_modes[] = {
  427. {"a", O_WRONLY},
  428. {"a+", O_RDWR}
  429. };
  430. for (int j = 0; j < 2; j++)
  431. {
  432. for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
  433. {
  434. int fileret;
  435. printf ("\tappend: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
  436. test_modes[i].mode);
  437. if (j == 0)
  438. fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
  439. else
  440. fileret = get_handles_fdopen (filename, fd, fp,
  441. test_modes[i].fd_mode,
  442. test_modes[i].mode);
  443. if (fileret != 0)
  444. return fileret;
  445. /* Write some data. */
  446. size_t written = fputs_func (data, fp);
  447. if (written == EOF)
  448. {
  449. printf ("fputs[1] failed to write all data\n");
  450. ret |= 1;
  451. }
  452. /* Verify that the offset points to the end of the file. The file
  453. len is maintained by adding data_len each time to reflect the data
  454. written to it. */
  455. long offset = ftell (fp);
  456. file_len += data_len;
  457. if (offset != file_len)
  458. {
  459. printf ("Incorrect offset. Expected %zu, but got %ld\n",
  460. file_len, offset);
  461. ret |= 1;
  462. }
  463. printf ("offset = %ld\n", offset);
  464. fclose (fp);
  465. }
  466. }
  467. /* For fdopen in 'a' mode, the file descriptor should not change if the file
  468. is already open with the O_APPEND flag set. */
  469. fd = open (filename, O_WRONLY | O_APPEND, 0);
  470. if (fd == -1)
  471. {
  472. printf ("open(O_APPEND) failed: %m\n");
  473. return 1;
  474. }
  475. off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
  476. if (seek_ret == -1)
  477. {
  478. printf ("lseek[O_APPEND][0] failed: %m\n");
  479. ret |= 1;
  480. }
  481. fp = fdopen (fd, "a");
  482. if (fp == NULL)
  483. {
  484. printf ("fdopen(O_APPEND) failed: %m\n");
  485. close (fd);
  486. return 1;
  487. }
  488. off_t new_seek_ret = lseek (fd, 0, SEEK_CUR);
  489. if (seek_ret == -1)
  490. {
  491. printf ("lseek[O_APPEND][1] failed: %m\n");
  492. ret |= 1;
  493. }
  494. printf ("\tappend: fdopen (file, \"a\"): O_APPEND: ");
  495. if (seek_ret != new_seek_ret)
  496. {
  497. printf ("incorrectly modified file offset to %jd, should be %jd",
  498. (intmax_t) new_seek_ret, (intmax_t) seek_ret);
  499. ret |= 1;
  500. }
  501. else
  502. printf ("retained current file offset %jd", (intmax_t) seek_ret);
  503. new_seek_ret = ftello (fp);
  504. if (seek_ret != new_seek_ret)
  505. {
  506. printf (", ftello reported incorrect offset %jd, should be %jd\n",
  507. (intmax_t) new_seek_ret, (intmax_t) seek_ret);
  508. ret |= 1;
  509. }
  510. else
  511. printf (", ftello reported correct offset %jd\n", (intmax_t) seek_ret);
  512. fclose (fp);
  513. return ret;
  514. }
  515. static int
  516. do_one_test (const char *filename)
  517. {
  518. int ret = 0;
  519. ret |= do_ftell_test (filename);
  520. ret |= do_write_test (filename);
  521. ret |= do_append_test (filename);
  522. ret |= do_rewind_test (filename);
  523. ret |= do_ftruncate_test (filename);
  524. return ret;
  525. }
  526. /* Run a set of tests for ftell for regular files and wide mode files. */
  527. static int
  528. do_test (void)
  529. {
  530. int ret = 0;
  531. FILE *fp = NULL;
  532. char *filename;
  533. size_t written;
  534. int fd = create_temp_file ("tst-active-handler-tmp.", &filename);
  535. if (fd == -1)
  536. {
  537. printf ("create_temp_file: %m\n");
  538. return 1;
  539. }
  540. fp = fdopen (fd, "w");
  541. if (fp == NULL)
  542. {
  543. printf ("fdopen[0]: %m\n");
  544. close (fd);
  545. return 1;
  546. }
  547. data = char_data;
  548. data_len = strlen (char_data);
  549. file_len = strlen (char_data);
  550. written = fputs (data, fp);
  551. if (written == EOF)
  552. {
  553. printf ("fputs[1] failed to write data\n");
  554. ret = 1;
  555. }
  556. fclose (fp);
  557. if (ret)
  558. return ret;
  559. /* Tests for regular files. */
  560. puts ("Regular mode:");
  561. fputs_func = (fputs_func_t) fputs;
  562. fgets_func = (fgets_func_t) fgets;
  563. data = char_data;
  564. data_len = strlen (char_data);
  565. ret |= do_one_test (filename);
  566. /* Truncate the file before repeating the tests in wide mode. */
  567. fp = fopen (filename, "w");
  568. if (fp == NULL)
  569. {
  570. printf ("fopen failed %m\n");
  571. return 1;
  572. }
  573. fclose (fp);
  574. /* Tests for wide files. */
  575. puts ("Wide mode:");
  576. if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
  577. {
  578. printf ("Cannot set en_US.UTF-8 locale.\n");
  579. return 1;
  580. }
  581. fputs_func = (fputs_func_t) fputws;
  582. fgets_func = (fgets_func_t) fgetws;
  583. data = wide_data;
  584. data_len = wcslen (wide_data);
  585. ret |= do_one_test (filename);
  586. return ret;
  587. }