1 /*
2 * dblist_dir.c --- iterate by directory entry
3 *
4 * Copyright 1997 by Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <time.h>
19
20 #include "ext2_fs.h"
21 #include "ext2fsP.h"
22
23 static int db_dir_proc(ext2_filsys fs, struct ext2_db_entry2 *db_info,
24 void *priv_data);
25
ext2fs_dblist_dir_iterate(ext2_dblist dblist,int flags,char * block_buf,int (* func)(ext2_ino_t dir,int entry,struct ext2_dir_entry * dirent,int offset,int blocksize,char * buf,void * priv_data),void * priv_data)26 errcode_t ext2fs_dblist_dir_iterate(ext2_dblist dblist,
27 int flags,
28 char *block_buf,
29 int (*func)(ext2_ino_t dir,
30 int entry,
31 struct ext2_dir_entry *dirent,
32 int offset,
33 int blocksize,
34 char *buf,
35 void *priv_data),
36 void *priv_data)
37 {
38 errcode_t retval;
39 struct dir_context ctx;
40
41 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
42
43 ctx.dir = 0;
44 ctx.flags = flags;
45 if (block_buf)
46 ctx.buf = block_buf;
47 else {
48 retval = ext2fs_get_mem(dblist->fs->blocksize, &ctx.buf);
49 if (retval)
50 return retval;
51 }
52 ctx.func = func;
53 ctx.priv_data = priv_data;
54 ctx.errcode = 0;
55
56 retval = ext2fs_dblist_iterate2(dblist, db_dir_proc, &ctx);
57
58 if (!block_buf)
59 ext2fs_free_mem(&ctx.buf);
60 if (retval)
61 return retval;
62 return ctx.errcode;
63 }
64
db_dir_proc(ext2_filsys fs,struct ext2_db_entry2 * db_info,void * priv_data)65 static int db_dir_proc(ext2_filsys fs, struct ext2_db_entry2 *db_info,
66 void *priv_data)
67 {
68 struct ext2_inode inode;
69 struct dir_context *ctx;
70 int ret;
71
72 ctx = (struct dir_context *) priv_data;
73 ctx->dir = db_info->ino;
74 ctx->errcode = 0;
75
76 ctx->errcode = ext2fs_read_inode(fs, ctx->dir, &inode);
77 if (ctx->errcode)
78 return DBLIST_ABORT;
79 if (inode.i_flags & EXT4_INLINE_DATA_FL)
80 ret = ext2fs_inline_data_dir_iterate(fs, ctx->dir, ctx);
81 else
82 ret = ext2fs_process_dir_block(fs, &db_info->blk,
83 db_info->blockcnt, 0, 0,
84 priv_data);
85 if ((ret & BLOCK_ABORT) && !ctx->errcode)
86 return DBLIST_ABORT;
87 return 0;
88 }
89