tst-rewinddir.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Test for rewinddir, using FUSE.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <dirent.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <support/check.h>
  19. #include <support/fuse.h>
  20. #include <support/readdir.h>
  21. #include <support/support.h>
  22. #include <support/xdirent.h>
  23. #include <libc-diag.h>
  24. /* Return the file name at the indicated directory offset. */
  25. static char *
  26. name_at_offset (unsigned int offset)
  27. {
  28. DIAG_PUSH_NEEDS_COMMENT_CLANG;
  29. DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
  30. if (offset <= 1)
  31. return xstrdup (".." + !offset); /* "." or "..". */
  32. else
  33. /* Pad the name with a lot of zeros, so that the dirent buffer gets
  34. filled more quickly. */
  35. return xasprintf ("file%0240u", offset);
  36. DIAG_POP_NEEDS_COMMENT_CLANG;
  37. }
  38. /* This many directory entries, including "." and "..". */
  39. enum { directory_entries = 200 };
  40. /* Add the directory entry at OFFSET to the stream D. */
  41. static uint64_t
  42. add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
  43. {
  44. if (offset >= directory_entries)
  45. return 0;
  46. char *name = name_at_offset (offset);
  47. uint64_t ino = 1000 + offset; /* Arbitrary value, distinct from 1. */
  48. uint32_t type = DT_REG;
  49. if (offset <= 1)
  50. {
  51. type = DT_DIR;
  52. ino = 1;
  53. }
  54. ++offset;
  55. bool added = support_fuse_dirstream_add (d, ino, offset, type, name);
  56. free (name);
  57. if (added)
  58. return offset;
  59. else
  60. return 0;
  61. }
  62. /* Set to true if getdents64 should produce only one entry. */
  63. static bool one_entry_per_getdents64;
  64. static void
  65. fuse_thread (struct support_fuse *f, void *closure)
  66. {
  67. struct fuse_in_header *inh;
  68. while ((inh = support_fuse_next (f)) != NULL)
  69. {
  70. if (support_fuse_handle_mountpoint (f)
  71. || (inh->nodeid == 1 && support_fuse_handle_directory (f)))
  72. continue;
  73. switch (inh->opcode)
  74. {
  75. case FUSE_READDIR:
  76. if (inh->nodeid == 1)
  77. {
  78. uint64_t offset = support_fuse_cast (READ, inh)->offset;
  79. struct support_fuse_dirstream *d
  80. = support_fuse_prepare_readdir (f);
  81. while (true)
  82. {
  83. offset = add_directory_entry (d, offset);
  84. if (offset == 0 || one_entry_per_getdents64)
  85. break;
  86. }
  87. support_fuse_reply_prepared (f);
  88. }
  89. else
  90. support_fuse_reply_error (f, EIO);
  91. break;
  92. default:
  93. FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode));
  94. support_fuse_reply_error (f, EIO);
  95. }
  96. }
  97. }
  98. /* Lists the entire directory from start to end. */
  99. static void
  100. verify_directory (DIR *dir, enum support_readdir_op op)
  101. {
  102. struct support_dirent e = { 0, };
  103. TEST_VERIFY (support_readdir (dir, op, &e));
  104. TEST_COMPARE_STRING (e.d_name, ".");
  105. TEST_VERIFY (support_readdir (dir, op, &e));
  106. TEST_COMPARE_STRING (e.d_name, "..");
  107. for (int i = 2; i < directory_entries; ++i)
  108. {
  109. char *expected = name_at_offset (i);
  110. TEST_VERIFY (support_readdir (dir, op, &e));
  111. TEST_COMPARE_STRING (e.d_name, expected);
  112. free (expected);
  113. }
  114. TEST_VERIFY (!support_readdir (dir, op, &e));
  115. free (e.d_name);
  116. }
  117. /* Run tests with rewinding after ENTRIES readdir calls. */
  118. static void
  119. rewind_after (unsigned int rewind_at)
  120. {
  121. for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op)
  122. {
  123. printf ("info: testing %s (rewind_at=%u)\n",
  124. support_readdir_function (op), rewind_at);
  125. struct support_fuse *f = support_fuse_mount (fuse_thread, NULL);
  126. DIR *dir = xopendir (support_fuse_mountpoint (f));
  127. struct support_dirent e = { 0, };
  128. switch (rewind_at)
  129. {
  130. case 0:
  131. break;
  132. case 1:
  133. TEST_VERIFY (support_readdir (dir, op, &e));
  134. TEST_COMPARE_STRING (e.d_name, ".");
  135. break;
  136. default:
  137. TEST_VERIFY (support_readdir (dir, op, &e));
  138. TEST_COMPARE_STRING (e.d_name, ".");
  139. TEST_VERIFY (support_readdir (dir, op, &e));
  140. TEST_COMPARE_STRING (e.d_name, "..");
  141. for (int i = 2; i < directory_entries; ++i)
  142. {
  143. if (i == rewind_at)
  144. break;
  145. char *expected = name_at_offset (i);
  146. TEST_VERIFY (support_readdir (dir, op, &e));
  147. TEST_COMPARE_STRING (e.d_name, expected);
  148. free (expected);
  149. }
  150. break;
  151. }
  152. errno = 0;
  153. rewinddir (dir);
  154. TEST_COMPARE (errno, 0);
  155. verify_directory (dir, op);
  156. free (e.d_name);
  157. xclosedir (dir);
  158. support_fuse_unmount (f);
  159. }
  160. }
  161. static int
  162. do_test (void)
  163. {
  164. support_fuse_init ();
  165. /* One pass without rewinding to verify that the generated directory
  166. content matches expectations. */
  167. {
  168. struct support_fuse *f = support_fuse_mount (fuse_thread, NULL);
  169. DIR *dir = xopendir (support_fuse_mountpoint (f));
  170. verify_directory (dir, SUPPORT_READDIR64);
  171. xclosedir (dir);
  172. support_fuse_unmount (f);
  173. }
  174. for (int do_unbuffered = 0; do_unbuffered < 2; ++do_unbuffered)
  175. {
  176. one_entry_per_getdents64 = do_unbuffered;
  177. for (int i = 0; i < 20; ++i)
  178. rewind_after (i);
  179. rewind_after (50);
  180. rewind_after (100);
  181. rewind_after (150);
  182. rewind_after (180);
  183. rewind_after (199);
  184. }
  185. return 0;
  186. }
  187. #include <support/test-driver.c>