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
24
errcode_t ext2fs_dir_iterate(ext2_filsys fs,
28
int (*func)(struct ext2_dir_entry *dirent,
35
struct dir_context ctx;
38
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
40
retval = ext2fs_check_directory(fs, dir);
49
retval = ext2fs_get_mem(fs->blocksize, (void **) &ctx.buf);
55
ctx.priv_data = priv_data;
57
retval = ext2fs_block_iterate2(fs, dir, 0, 0,
58
ext2fs_process_dir_block, &ctx);
60
ext2fs_free_mem((void **) &ctx.buf);
67
* Helper function which is private to this module. Used by
68
* ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
70
int ext2fs_process_dir_block(ext2_filsys fs,
77
struct dir_context *ctx = (struct dir_context *) priv_data;
83
struct ext2_dir_entry *dirent;
88
entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
90
ctx->errcode = ext2fs_read_dir_block(fs, *blocknr, ctx->buf);
94
while (offset < fs->blocksize) {
95
dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
96
if (((offset + dirent->rec_len) > fs->blocksize) ||
97
(dirent->rec_len < 8) ||
98
((dirent->rec_len % 4) != 0) ||
99
(((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
100
ctx->errcode = EXT2_ET_DIR_CORRUPTED;
103
if (!dirent->inode &&
104
!(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
108
ret = (ctx->func)(dirent, offset, fs->blocksize,
109
ctx->buf, ctx->priv_data);
110
else if (ctx->func2) {
111
ret = (ctx->func2)(ctx->dir, entry, dirent, offset,
112
fs->blocksize, ctx->buf,
114
if (entry < DIRENT_OTHER_FILE)
118
if (ret & DIRENT_CHANGED)
120
if (ret & DIRENT_ABORT) {
125
offset += dirent->rec_len;
129
ctx->errcode = ext2fs_write_dir_block(fs, *blocknr, ctx->buf);