tst-readdir-zero-inode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Test that readdir does not skip entries with d_ino == 0 (bug 12165).
  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 <stdlib.h>
  16. #include <support/check.h>
  17. #include <support/fuse.h>
  18. #include <support/readdir.h>
  19. #include <support/xdirent.h>
  20. /* Add the directory entry at OFFSET to the stream D. */
  21. static uint64_t
  22. add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
  23. {
  24. bool added = false;
  25. ++offset;
  26. switch (offset - 1)
  27. {
  28. case 0:
  29. added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, ".");
  30. break;
  31. case 1:
  32. added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, "..");
  33. break;
  34. case 2:
  35. added = support_fuse_dirstream_add (d, 2, offset, DT_REG, "before");
  36. break;
  37. case 3:
  38. added = support_fuse_dirstream_add (d, 0, offset, DT_REG, "zero");
  39. break;
  40. case 4:
  41. added = support_fuse_dirstream_add (d, 3, offset, DT_REG, "after");
  42. break;
  43. }
  44. if (added)
  45. return offset;
  46. else
  47. return 0;
  48. }
  49. /* Set to true if getdents64 should produce only one entry. */
  50. static bool one_entry_per_getdents64;
  51. static void
  52. fuse_thread (struct support_fuse *f, void *closure)
  53. {
  54. struct fuse_in_header *inh;
  55. while ((inh = support_fuse_next (f)) != NULL)
  56. {
  57. if (support_fuse_handle_mountpoint (f)
  58. || (inh->nodeid == 1 && support_fuse_handle_directory (f)))
  59. continue;
  60. switch (inh->opcode)
  61. {
  62. case FUSE_READDIR:
  63. if (inh->nodeid == 1)
  64. {
  65. uint64_t offset = support_fuse_cast (READ, inh)->offset;
  66. struct support_fuse_dirstream *d
  67. = support_fuse_prepare_readdir (f);
  68. while (true)
  69. {
  70. offset = add_directory_entry (d, offset);
  71. if (offset == 0 || one_entry_per_getdents64)
  72. break;
  73. }
  74. support_fuse_reply_prepared (f);
  75. }
  76. else
  77. support_fuse_reply_error (f, EIO);
  78. break;
  79. default:
  80. FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode));
  81. support_fuse_reply_error (f, EIO);
  82. }
  83. }
  84. }
  85. static int
  86. do_test (void)
  87. {
  88. support_fuse_init ();
  89. for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op)
  90. {
  91. struct support_fuse *f = support_fuse_mount (fuse_thread, NULL);
  92. DIR *dir = xopendir (support_fuse_mountpoint (f));
  93. struct support_dirent e = { 0, };
  94. TEST_VERIFY (support_readdir (dir, op, &e));
  95. TEST_COMPARE_STRING (e.d_name, ".");
  96. TEST_COMPARE (e.d_ino, 1);
  97. TEST_VERIFY (support_readdir (dir, op, &e));
  98. TEST_COMPARE_STRING (e.d_name, "..");
  99. TEST_COMPARE (e.d_ino, 1);
  100. TEST_VERIFY (support_readdir (dir, op, &e));
  101. TEST_COMPARE_STRING (e.d_name, "before");
  102. TEST_COMPARE (e.d_ino, 2);
  103. TEST_VERIFY (support_readdir (dir, op, &e));
  104. TEST_COMPARE_STRING (e.d_name, "zero");
  105. TEST_COMPARE (e.d_ino, 0);
  106. TEST_VERIFY (support_readdir (dir, op, &e));
  107. TEST_COMPARE_STRING (e.d_name, "after");
  108. TEST_COMPARE (e.d_ino, 3);
  109. TEST_VERIFY (!support_readdir (dir, op, &e));
  110. free (e.d_name);
  111. xclosedir (dir);
  112. support_fuse_unmount (f);
  113. }
  114. return 0;
  115. }
  116. #include <support/test-driver.c>