blob: 5125d19919bb5904cabf2be5aff51771f42e4568 [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
2 * dir_iterate.c --- ext2fs directory iteration operations
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1993, 1994, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00009 * %End-Header%
10 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o21c84b71997-04-29 16:15:03 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000018#if HAVE_ERRNO_H
19#include <errno.h>
20#endif
21
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000022#include "ext2_fs.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000023#include "ext2fsP.h"
24
Theodore Ts'o8a480352009-06-21 21:07:38 -040025#define EXT4_MAX_REC_LEN ((1<<16)-1)
26
27errcode_t ext2fs_get_rec_len(ext2_filsys fs,
28 struct ext2_dir_entry *dirent,
29 unsigned int *rec_len)
30{
31 unsigned int len = dirent->rec_len;
32
Theodore Ts'oa4fdf092010-12-22 13:53:02 -050033 if (fs->blocksize < 65536)
34 *rec_len = len;
35 else if (len == EXT4_MAX_REC_LEN || len == 0)
Theodore Ts'o8a480352009-06-21 21:07:38 -040036 *rec_len = fs->blocksize;
37 else
38 *rec_len = (len & 65532) | ((len & 3) << 16);
39 return 0;
40}
41
42errcode_t ext2fs_set_rec_len(ext2_filsys fs,
43 unsigned int len,
44 struct ext2_dir_entry *dirent)
45{
46 if ((len > fs->blocksize) || (fs->blocksize > (1 << 18)) || (len & 3))
47 return EINVAL;
48 if (len < 65536) {
49 dirent->rec_len = len;
50 return 0;
51 }
52 if (len == fs->blocksize) {
53 if (fs->blocksize == 65536)
54 dirent->rec_len = EXT4_MAX_REC_LEN;
55 else
56 dirent->rec_len = 0;
57 } else
58 dirent->rec_len = (len & 65532) | ((len >> 16) & 3);
59 return 0;
60}
61
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050062/*
63 * This function checks to see whether or not a potential deleted
64 * directory entry looks valid. What we do is check the deleted entry
65 * and each successive entry to make sure that they all look valid and
66 * that the last deleted entry ends at the beginning of the next
67 * undeleted entry. Returns 1 if the deleted entry looks valid, zero
68 * if not valid.
69 */
Nic Case6a8da462009-06-29 01:24:40 -040070static int ext2fs_validate_entry(ext2_filsys fs, char *buf,
71 unsigned int offset,
72 unsigned int final_offset)
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050073{
74 struct ext2_dir_entry *dirent;
Theodore Ts'o8a480352009-06-21 21:07:38 -040075 unsigned int rec_len;
Nic Case6a8da462009-06-29 01:24:40 -040076#define DIRENT_MIN_LENGTH 12
Theodore Ts'oefc6f622008-08-27 23:07:54 -040077
Nic Case6a8da462009-06-29 01:24:40 -040078 while ((offset < final_offset) &&
79 (offset <= fs->blocksize - DIRENT_MIN_LENGTH)) {
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050080 dirent = (struct ext2_dir_entry *)(buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -040081 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
82 return 0;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040083 offset += rec_len;
84 if ((rec_len < 8) ||
85 ((rec_len % 4) != 0) ||
Theodore Ts'o25c7e0c2009-07-12 01:13:06 -040086 ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len))
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050087 return 0;
88 }
89 return (offset == final_offset);
90}
91
92errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
93 ext2_ino_t dir,
94 int flags,
95 char *block_buf,
96 int (*func)(ext2_ino_t dir,
97 int entry,
98 struct ext2_dir_entry *dirent,
99 int offset,
100 int blocksize,
101 char *buf,
102 void *priv_data),
103 void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000104{
105 struct dir_context ctx;
106 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400107
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000108 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
109
110 retval = ext2fs_check_directory(fs, dir);
111 if (retval)
112 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400113
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000114 ctx.dir = dir;
115 ctx.flags = flags;
116 if (block_buf)
117 ctx.buf = block_buf;
118 else {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400119 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000120 if (retval)
121 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000122 }
123 ctx.func = func;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000124 ctx.priv_data = priv_data;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000125 ctx.errcode = 0;
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400126 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY, 0,
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000127 ext2fs_process_dir_block, &ctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000128 if (!block_buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400129 ext2fs_free_mem(&ctx.buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000130 if (retval)
131 return retval;
132 return ctx.errcode;
133}
134
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500135struct xlate {
136 int (*func)(struct ext2_dir_entry *dirent,
137 int offset,
138 int blocksize,
139 char *buf,
140 void *priv_data);
141 void *real_private;
142};
143
Theodore Ts'o54434922003-12-07 01:28:50 -0500144static int xlate_func(ext2_ino_t dir EXT2FS_ATTR((unused)),
145 int entry EXT2FS_ATTR((unused)),
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500146 struct ext2_dir_entry *dirent, int offset,
147 int blocksize, char *buf, void *priv_data)
148{
149 struct xlate *xl = (struct xlate *) priv_data;
150
151 return (*xl->func)(dirent, offset, blocksize, buf, xl->real_private);
152}
153
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400154extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500155 ext2_ino_t dir,
156 int flags,
157 char *block_buf,
158 int (*func)(struct ext2_dir_entry *dirent,
159 int offset,
160 int blocksize,
161 char *buf,
162 void *priv_data),
163 void *priv_data)
164{
165 struct xlate xl;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400166
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500167 xl.real_private = priv_data;
168 xl.func = func;
169
170 return ext2fs_dir_iterate2(fs, dir, flags, block_buf,
171 xlate_func, &xl);
172}
173
174
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000175/*
176 * Helper function which is private to this module. Used by
177 * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
178 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500179int ext2fs_process_dir_block(ext2_filsys fs,
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400180 blk64_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500181 e2_blkcnt_t blockcnt,
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400182 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500183 int ref_offset EXT2FS_ATTR((unused)),
184 void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000185{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000186 struct dir_context *ctx = (struct dir_context *) priv_data;
Theodore Ts'o54434922003-12-07 01:28:50 -0500187 unsigned int offset = 0;
188 unsigned int next_real_entry = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000189 int ret = 0;
190 int changed = 0;
191 int do_abort = 0;
Theodore Ts'o25c7e0c2009-07-12 01:13:06 -0400192 unsigned int rec_len, size;
193 int entry;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000194 struct ext2_dir_entry *dirent;
195
196 if (blockcnt < 0)
197 return 0;
198
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000199 entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400200
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400201 ctx->errcode = ext2fs_read_dir_block3(fs, *blocknr, ctx->buf, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000202 if (ctx->errcode)
203 return BLOCK_ABORT;
204
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000205 while (offset < fs->blocksize) {
206 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -0400207 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
208 return BLOCK_ABORT;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400209 if (((offset + rec_len) > fs->blocksize) ||
210 (rec_len < 8) ||
211 ((rec_len % 4) != 0) ||
Theodore Ts'o25c7e0c2009-07-12 01:13:06 -0400212 ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len)) {
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000213 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
214 return BLOCK_ABORT;
215 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000216 if (!dirent->inode &&
217 !(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
218 goto next;
219
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500220 ret = (ctx->func)(ctx->dir,
221 (next_real_entry > offset) ?
222 DIRENT_DELETED_FILE : entry,
223 dirent, offset,
224 fs->blocksize, ctx->buf,
225 ctx->priv_data);
226 if (entry < DIRENT_OTHER_FILE)
227 entry++;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400228
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400229 if (ret & DIRENT_CHANGED) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400230 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
231 return BLOCK_ABORT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000232 changed++;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400233 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000234 if (ret & DIRENT_ABORT) {
235 do_abort++;
236 break;
237 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400238next:
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500239 if (next_real_entry == offset)
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400240 next_real_entry += rec_len;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400241
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500242 if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
243 size = ((dirent->name_len & 0xFF) + 11) & ~3;
244
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400245 if (rec_len != size) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500246 unsigned int final_offset;
247
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400248 final_offset = offset + rec_len;
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500249 offset += size;
250 while (offset < final_offset &&
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400251 !ext2fs_validate_entry(fs, ctx->buf,
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500252 offset,
253 final_offset))
254 offset += 4;
255 continue;
256 }
257 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400258 offset += rec_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000259 }
260
261 if (changed) {
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400262 ctx->errcode = ext2fs_write_dir_block3(fs, *blocknr, ctx->buf,
263 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000264 if (ctx->errcode)
265 return BLOCK_ABORT;
266 }
267 if (do_abort)
268 return BLOCK_ABORT;
269 return 0;
270}
271