3
0

dir_iterate.c 5.1 KB

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