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
22 #include <linux/ext2_fs.h>
26 errcode_t ext2fs_dir_iterate(ext2_filsys fs,
30 int (*func)(struct ext2_dir_entry *dirent,
37 struct dir_context ctx;
40 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
42 retval = ext2fs_check_directory(fs, dir);
51 retval = ext2fs_get_mem(fs->blocksize, (void **) &ctx.buf);
57 ctx.private = private;
59 retval = ext2fs_block_iterate(fs, dir, 0, 0,
60 ext2fs_process_dir_block, &ctx);
62 ext2fs_free_mem((void **) &ctx.buf);
69 * Helper function which is private to this module. Used by
70 * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
72 extern int ext2fs_process_dir_block(ext2_filsys fs,
77 struct dir_context *ctx = (struct dir_context *) private;
83 struct ext2_dir_entry *dirent;
88 ctx->errcode = ext2fs_read_dir_block(fs, *blocknr, ctx->buf);
92 entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
94 while (offset < fs->blocksize) {
95 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
97 !(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
101 ret = (ctx->func)(dirent, offset, fs->blocksize,
102 ctx->buf, ctx->private);
103 else if (ctx->func2) {
104 ret = (ctx->func2)(ctx->dir, entry, dirent, offset,
105 fs->blocksize, ctx->buf,
107 if (entry < DIRENT_OTHER_FILE)
111 if (ret & DIRENT_CHANGED)
113 if (ret & DIRENT_ABORT) {
118 if (((offset + dirent->rec_len) > fs->blocksize) ||
119 (dirent->rec_len < 8) ||
120 ((dirent->name_len+8) > dirent->rec_len)) {
121 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
124 offset += dirent->rec_len;
128 ctx->errcode = ext2fs_write_dir_block(fs, *blocknr, ctx->buf);