Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / dblist_dir.c
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 Public
8  * License.
9  * %End-Header%
10  * 
11  */
12
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #ifdef HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23
24 #include <linux/ext2_fs.h>
25
26 #include "ext2fsP.h"
27
28 static int db_dir_proc(ext2_filsys fs, struct ext2_db_entry *db_info,
29                        void *private);
30
31 extern errcode_t
32         ext2fs_dblist_dir_iterate(ext2_dblist dblist,
33                                   int   flags,
34                                   char  *block_buf,
35                                   int (*func)(ino_t     dir,
36                                               int       entry,
37                                               struct ext2_dir_entry *dirent,
38                                               int       offset,
39                                               int       blocksize,
40                                               char      *buf,
41                                               void      *private),
42                                   void *private)
43 {
44         errcode_t               retval;
45         struct dir_context      ctx;
46
47         EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
48
49         ctx.dir = 0;
50         ctx.flags = flags;
51         if (block_buf)
52                 ctx.buf = block_buf;
53         else {
54                 ctx.buf = malloc(dblist->fs->blocksize);
55                 if (!ctx.buf)
56                         return ENOMEM;
57         }
58         ctx.func = 0;
59         ctx.func2 = func;
60         ctx.private = private;
61         ctx.errcode = 0;
62
63         retval = ext2fs_dblist_iterate(dblist, db_dir_proc, &ctx);
64         
65         if (!block_buf)
66                 free(ctx.buf);
67         if (retval)
68                 return retval;
69         return ctx.errcode;
70 }
71
72 static int db_dir_proc(ext2_filsys fs, struct ext2_db_entry *db_info,
73                        void *private)
74 {
75         struct dir_context      *ctx = private;
76
77         ctx->dir = db_info->ino;
78         
79         return ext2fs_process_dir_block(fs, &db_info->blk,
80                                         db_info->blockcnt, private);
81 }