2 * dir_iterate.c --- ext2fs directory iteration operations
4 * Copyright (C) 1993, 1994, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
21 #if EXT2_FLAT_INCLUDES
24 #include <linux/ext2_fs.h>
29 errcode_t ext2fs_dir_iterate(ext2_filsys fs,
33 int (*func)(struct ext2_dir_entry *dirent,
40 struct dir_context ctx;
43 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45 retval = ext2fs_check_directory(fs, dir);
54 retval = ext2fs_get_mem(fs->blocksize, (void **) &ctx.buf);
60 ctx.priv_data = priv_data;
62 retval = ext2fs_block_iterate2(fs, dir, 0, 0,
63 ext2fs_process_dir_block, &ctx);
65 ext2fs_free_mem((void **) &ctx.buf);
72 * Helper function which is private to this module. Used by
73 * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
75 extern int ext2fs_process_dir_block(ext2_filsys fs,
82 struct dir_context *ctx = (struct dir_context *) priv_data;
88 struct ext2_dir_entry *dirent;
93 entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
95 ctx->errcode = ext2fs_read_dir_block(fs, *blocknr, ctx->buf);
99 while (offset < fs->blocksize) {
100 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
101 if (!dirent->inode &&
102 !(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
106 ret = (ctx->func)(dirent, offset, fs->blocksize,
107 ctx->buf, ctx->priv_data);
108 else if (ctx->func2) {
109 ret = (ctx->func2)(ctx->dir, entry, dirent, offset,
110 fs->blocksize, ctx->buf,
112 if (entry < DIRENT_OTHER_FILE)
116 if (ret & DIRENT_CHANGED)
118 if (ret & DIRENT_ABORT) {
123 if (((offset + dirent->rec_len) > fs->blocksize) ||
124 (dirent->rec_len < 8) ||
125 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
126 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
129 offset += dirent->rec_len;
133 ctx->errcode = ext2fs_write_dir_block(fs, *blocknr, ctx->buf);