blob: fb20fa0e5d4cf5befc6717b6fc9b26c72f74665b [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * dirblock.c --- directory block routines.
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1995, 1996 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%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000016#include <string.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o50e1e101997-04-26 13:58:21 +000020#include "ext2fs.h"
21
Theodore Ts'of9190c82002-03-12 01:05:06 -050022errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
Theodore Ts'o54434922003-12-07 01:28:50 -050023 void *buf, int flags EXT2FS_ATTR((unused)))
Theodore Ts'o50e1e101997-04-26 13:58:21 +000024{
25 errcode_t retval;
26 char *p, *end;
27 struct ext2_dir_entry *dirent;
Theodore Ts'o126a2912007-08-11 01:56:48 -040028 unsigned int name_len, rec_len;
Theodore Ts'of9190c82002-03-12 01:05:06 -050029
Theodore Ts'o50e1e101997-04-26 13:58:21 +000030
31 retval = io_channel_read_blk(fs->io, block, 1, buf);
32 if (retval)
33 return retval;
Theodore Ts'o126a2912007-08-11 01:56:48 -040034
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000035 p = (char *) buf;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000036 end = (char *) buf + fs->blocksize;
Theodore Ts'oe446d712001-01-05 22:27:06 +000037 while (p < end-8) {
Theodore Ts'obfcd9a12001-05-05 16:21:48 +000038 dirent = (struct ext2_dir_entry *) p;
Theodore Ts'o126a2912007-08-11 01:56:48 -040039#ifdef WORDS_BIGENDIAN
40 dirent->inode = ext2fs_swab32(dirent->inode);
41 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
42 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'o5df55d72001-06-11 07:00:04 +000043#endif
Theodore Ts'of9190c82002-03-12 01:05:06 -050044 name_len = dirent->name_len;
45#ifdef WORDS_BIGENDIAN
46 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
47 dirent->name_len = ext2fs_swab16(dirent->name_len);
48#endif
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000049 rec_len = dirent->rec_len;
50 if ((rec_len < 8) || (rec_len % 4)) {
51 rec_len = 8;
52 retval = EXT2_ET_DIR_CORRUPTED;
53 }
Theodore Ts'of9190c82002-03-12 01:05:06 -050054 if (((name_len & 0xFF) + 8) > dirent->rec_len)
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000055 retval = EXT2_ET_DIR_CORRUPTED;
56 p += rec_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000057 }
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000058 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059}
60
Theodore Ts'of9190c82002-03-12 01:05:06 -050061errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
62 void *buf)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000063{
Theodore Ts'of9190c82002-03-12 01:05:06 -050064 return ext2fs_read_dir_block2(fs, block, buf, 0);
65}
66
67
68errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
Theodore Ts'o54434922003-12-07 01:28:50 -050069 void *inbuf, int flags EXT2FS_ATTR((unused)))
Theodore Ts'of9190c82002-03-12 01:05:06 -050070{
Theodore Ts'o126a2912007-08-11 01:56:48 -040071#ifdef WORDS_BIGENDIAN
Theodore Ts'o50e1e101997-04-26 13:58:21 +000072 errcode_t retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -050073 char *p, *end;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000074 char *buf = 0;
75 struct ext2_dir_entry *dirent;
76
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040077 retval = ext2fs_get_mem(fs->blocksize, &buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050078 if (retval)
79 return retval;
80 memcpy(buf, inbuf, fs->blocksize);
81 p = buf;
82 end = buf + fs->blocksize;
83 while (p < end) {
84 dirent = (struct ext2_dir_entry *) p;
85 if ((dirent->rec_len < 8) ||
86 (dirent->rec_len % 4)) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040087 ext2fs_free_mem(&buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050088 return (EXT2_ET_DIR_CORRUPTED);
89 }
90 p += dirent->rec_len;
Theodore Ts'o126a2912007-08-11 01:56:48 -040091 dirent->inode = ext2fs_swab32(dirent->inode);
92 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
93 dirent->name_len = ext2fs_swab16(dirent->name_len);
94
Theodore Ts'of9190c82002-03-12 01:05:06 -050095 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
96 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'of9190c82002-03-12 01:05:06 -050097 }
98 retval = io_channel_write_blk(fs->io, block, 1, buf);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040099 ext2fs_free_mem(&buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000100 return retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -0500101#else
102 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
103#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000104}
105
106
Theodore Ts'of9190c82002-03-12 01:05:06 -0500107errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
108 void *inbuf)
109{
110 return ext2fs_write_dir_block2(fs, block, inbuf, 0);
111}
112