blob: b7d873595a62a9ac75fc80e30f9f6fe5d3d7b33c [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * dir_iterate.c --- ext2fs directory iteration operations
4 *
5 * Copyright (C) 1993, 1994, 1994, 1995, 1996, 1997 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#if HAVE_ERRNO_H
19#include <errno.h>
20#endif
21
22#include "ext2_fs.h"
23#include "ext2fsP.h"
24
25/*
26 * This function checks to see whether or not a potential deleted
27 * directory entry looks valid. What we do is check the deleted entry
28 * and each successive entry to make sure that they all look valid and
29 * that the last deleted entry ends at the beginning of the next
30 * undeleted entry. Returns 1 if the deleted entry looks valid, zero
31 * if not valid.
32 */
33static int ext2fs_validate_entry(char *buf, int offset, int final_offset)
34{
35 struct ext2_dir_entry *dirent;
36
37 while (offset < final_offset) {
38 dirent = (struct ext2_dir_entry *)(buf + offset);
39 offset += dirent->rec_len;
40 if ((dirent->rec_len < 8) ||
41 ((dirent->rec_len % 4) != 0) ||
42 (((dirent->name_len & 0xFF)+8) > dirent->rec_len))
43 return 0;
44 }
45 return (offset == final_offset);
46}
47
48errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
49 ext2_ino_t dir,
50 int flags,
51 char *block_buf,
52 int (*func)(ext2_ino_t dir,
53 int entry,
54 struct ext2_dir_entry *dirent,
55 int offset,
56 int blocksize,
57 char *buf,
58 void *priv_data),
59 void *priv_data)
60{
61 struct dir_context ctx;
62 errcode_t retval;
63
64 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
65
66 retval = ext2fs_check_directory(fs, dir);
67 if (retval)
68 return retval;
69
70 ctx.dir = dir;
71 ctx.flags = flags;
72 if (block_buf)
73 ctx.buf = block_buf;
74 else {
75 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
76 if (retval)
77 return retval;
78 }
79 ctx.func = func;
80 ctx.priv_data = priv_data;
81 ctx.errcode = 0;
82 retval = ext2fs_block_iterate2(fs, dir, 0, 0,
83 ext2fs_process_dir_block, &ctx);
84 if (!block_buf)
85 ext2fs_free_mem(&ctx.buf);
86 if (retval)
87 return retval;
88 return ctx.errcode;
89}
90
91struct xlate {
92 int (*func)(struct ext2_dir_entry *dirent,
93 int offset,
94 int blocksize,
95 char *buf,
96 void *priv_data);
97 void *real_private;
98};
99
100static int xlate_func(ext2_ino_t dir EXT2FS_ATTR((unused)),
101 int entry EXT2FS_ATTR((unused)),
102 struct ext2_dir_entry *dirent, int offset,
103 int blocksize, char *buf, void *priv_data)
104{
105 struct xlate *xl = (struct xlate *) priv_data;
106
107 return (*xl->func)(dirent, offset, blocksize, buf, xl->real_private);
108}
109
110extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
111 ext2_ino_t dir,
112 int flags,
113 char *block_buf,
114 int (*func)(struct ext2_dir_entry *dirent,
115 int offset,
116 int blocksize,
117 char *buf,
118 void *priv_data),
119 void *priv_data)
120{
121 struct xlate xl;
122
123 xl.real_private = priv_data;
124 xl.func = func;
125
126 return ext2fs_dir_iterate2(fs, dir, flags, block_buf,
127 xlate_func, &xl);
128}
129
130
131/*
132 * Helper function which is private to this module. Used by
133 * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
134 */
135int ext2fs_process_dir_block(ext2_filsys fs,
136 blk_t *blocknr,
137 e2_blkcnt_t blockcnt,
138 blk_t ref_block EXT2FS_ATTR((unused)),
139 int ref_offset EXT2FS_ATTR((unused)),
140 void *priv_data)
141{
142 struct dir_context *ctx = (struct dir_context *) priv_data;
143 unsigned int offset = 0;
144 unsigned int next_real_entry = 0;
145 int ret = 0;
146 int changed = 0;
147 int do_abort = 0;
148 int entry, size;
149 struct ext2_dir_entry *dirent;
150
151 if (blockcnt < 0)
152 return 0;
153
154 entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
155
156 ctx->errcode = ext2fs_read_dir_block(fs, *blocknr, ctx->buf);
157 if (ctx->errcode)
158 return BLOCK_ABORT;
159
160 while (offset < fs->blocksize) {
161 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
162 if (((offset + dirent->rec_len) > fs->blocksize) ||
163 (dirent->rec_len < 8) ||
164 ((dirent->rec_len % 4) != 0) ||
165 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
166 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
167 return BLOCK_ABORT;
168 }
169 if (!dirent->inode &&
170 !(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
171 goto next;
172
173 ret = (ctx->func)(ctx->dir,
174 (next_real_entry > offset) ?
175 DIRENT_DELETED_FILE : entry,
176 dirent, offset,
177 fs->blocksize, ctx->buf,
178 ctx->priv_data);
179 if (entry < DIRENT_OTHER_FILE)
180 entry++;
181
182 if (ret & DIRENT_CHANGED)
183 changed++;
184 if (ret & DIRENT_ABORT) {
185 do_abort++;
186 break;
187 }
188next:
189 if (next_real_entry == offset)
190 next_real_entry += dirent->rec_len;
191
192 if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
193 size = ((dirent->name_len & 0xFF) + 11) & ~3;
194
195 if (dirent->rec_len != size) {
196 unsigned int final_offset;
197
198 final_offset = offset + dirent->rec_len;
199 offset += size;
200 while (offset < final_offset &&
201 !ext2fs_validate_entry(ctx->buf,
202 offset,
203 final_offset))
204 offset += 4;
205 continue;
206 }
207 }
208 offset += dirent->rec_len;
209 }
210
211 if (changed) {
212 ctx->errcode = ext2fs_write_dir_block(fs, *blocknr, ctx->buf);
213 if (ctx->errcode)
214 return BLOCK_ABORT;
215 }
216 if (do_abort)
217 return BLOCK_ABORT;
218 return 0;
219}
220