3
0

dir_iterate.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * dir_iterate.c --- ext2fs directory iteration operations
  4. *
  5. * Copyright (C) 1993, 1994, 1994, 1995, 1996, 1997 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #if HAVE_ERRNO_H
  18. #include <errno.h>
  19. #endif
  20. #include "ext2_fs.h"
  21. #include "ext2fsP.h"
  22. /*
  23. * This function checks to see whether or not a potential deleted
  24. * directory entry looks valid. What we do is check the deleted entry
  25. * and each successive entry to make sure that they all look valid and
  26. * that the last deleted entry ends at the beginning of the next
  27. * undeleted entry. Returns 1 if the deleted entry looks valid, zero
  28. * if not valid.
  29. */
  30. static int ext2fs_validate_entry(char *buf, int offset, int final_offset)
  31. {
  32. struct ext2_dir_entry *dirent;
  33. while (offset < final_offset) {
  34. dirent = (struct ext2_dir_entry *)(buf + offset);
  35. offset += dirent->rec_len;
  36. if ((dirent->rec_len < 8) ||
  37. ((dirent->rec_len % 4) != 0) ||
  38. (((dirent->name_len & 0xFF)+8) > dirent->rec_len))
  39. return 0;
  40. }
  41. return (offset == final_offset);
  42. }
  43. errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
  44. ext2_ino_t dir,
  45. int flags,
  46. char *block_buf,
  47. int (*func)(ext2_ino_t dir,
  48. int entry,
  49. struct ext2_dir_entry *dirent,
  50. int offset,
  51. int blocksize,
  52. char *buf,
  53. void *priv_data),
  54. void *priv_data)
  55. {
  56. struct dir_context ctx;
  57. errcode_t retval;
  58. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  59. retval = ext2fs_check_directory(fs, dir);
  60. if (retval)
  61. return retval;
  62. ctx.dir = dir;
  63. ctx.flags = flags;
  64. if (block_buf)
  65. ctx.buf = block_buf;
  66. else {
  67. retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
  68. if (retval)
  69. return retval;
  70. }
  71. ctx.func = func;
  72. ctx.priv_data = priv_data;
  73. ctx.errcode = 0;
  74. retval = ext2fs_block_iterate2(fs, dir, 0, 0,
  75. ext2fs_process_dir_block, &ctx);
  76. if (!block_buf)
  77. ext2fs_free_mem(&ctx.buf);
  78. if (retval)
  79. return retval;
  80. return ctx.errcode;
  81. }
  82. struct xlate {
  83. int (*func)(struct ext2_dir_entry *dirent,
  84. int offset,
  85. int blocksize,
  86. char *buf,
  87. void *priv_data);
  88. void *real_private;
  89. };
  90. static int xlate_func(ext2_ino_t dir EXT2FS_ATTR((unused)),
  91. int entry EXT2FS_ATTR((unused)),
  92. struct ext2_dir_entry *dirent, int offset,
  93. int blocksize, char *buf, void *priv_data)
  94. {
  95. struct xlate *xl = (struct xlate *) priv_data;
  96. return (*xl->func)(dirent, offset, blocksize, buf, xl->real_private);
  97. }
  98. extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
  99. ext2_ino_t dir,
  100. int flags,
  101. char *block_buf,
  102. int (*func)(struct ext2_dir_entry *dirent,
  103. int offset,
  104. int blocksize,
  105. char *buf,
  106. void *priv_data),
  107. void *priv_data)
  108. {
  109. struct xlate xl;
  110. xl.real_private = priv_data;
  111. xl.func = func;
  112. return ext2fs_dir_iterate2(fs, dir, flags, block_buf,
  113. xlate_func, &xl);
  114. }
  115. /*
  116. * Helper function which is private to this module. Used by
  117. * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
  118. */
  119. int ext2fs_process_dir_block(ext2_filsys fs,
  120. blk_t *blocknr,
  121. e2_blkcnt_t blockcnt,
  122. blk_t ref_block EXT2FS_ATTR((unused)),
  123. int ref_offset EXT2FS_ATTR((unused)),
  124. void *priv_data)
  125. {
  126. struct dir_context *ctx = (struct dir_context *) priv_data;
  127. unsigned int offset = 0;
  128. unsigned int next_real_entry = 0;
  129. int ret = 0;
  130. int changed = 0;
  131. int do_abort = 0;
  132. int entry, size;
  133. struct ext2_dir_entry *dirent;
  134. if (blockcnt < 0)
  135. return 0;
  136. entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
  137. ctx->errcode = ext2fs_read_dir_block(fs, *blocknr, ctx->buf);
  138. if (ctx->errcode)
  139. return BLOCK_ABORT;
  140. while (offset < fs->blocksize) {
  141. dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
  142. if (((offset + dirent->rec_len) > fs->blocksize) ||
  143. (dirent->rec_len < 8) ||
  144. ((dirent->rec_len % 4) != 0) ||
  145. (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
  146. ctx->errcode = EXT2_ET_DIR_CORRUPTED;
  147. return BLOCK_ABORT;
  148. }
  149. if (!dirent->inode &&
  150. !(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
  151. goto next;
  152. ret = (ctx->func)(ctx->dir,
  153. (next_real_entry > offset) ?
  154. DIRENT_DELETED_FILE : entry,
  155. dirent, offset,
  156. fs->blocksize, ctx->buf,
  157. ctx->priv_data);
  158. if (entry < DIRENT_OTHER_FILE)
  159. entry++;
  160. if (ret & DIRENT_CHANGED)
  161. changed++;
  162. if (ret & DIRENT_ABORT) {
  163. do_abort++;
  164. break;
  165. }
  166. next:
  167. if (next_real_entry == offset)
  168. next_real_entry += dirent->rec_len;
  169. if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
  170. size = ((dirent->name_len & 0xFF) + 11) & ~3;
  171. if (dirent->rec_len != size) {
  172. unsigned int final_offset;
  173. final_offset = offset + dirent->rec_len;
  174. offset += size;
  175. while (offset < final_offset &&
  176. !ext2fs_validate_entry(ctx->buf,
  177. offset,
  178. final_offset))
  179. offset += 4;
  180. continue;
  181. }
  182. }
  183. offset += dirent->rec_len;
  184. }
  185. if (changed) {
  186. ctx->errcode = ext2fs_write_dir_block(fs, *blocknr, ctx->buf);
  187. if (ctx->errcode)
  188. return BLOCK_ABORT;
  189. }
  190. if (do_abort)
  191. return BLOCK_ABORT;
  192. return 0;
  193. }