blob: 29b4fcd25518650582dec8b1bd791f94d21a4889 [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * dirblock.c --- directory block routines.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1995, 1996 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%
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'oefc6f622008-08-27 23:07:54 -040029
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'o8a480352009-06-21 21:07:38 -040049 if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
50 return retval;
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000051 if ((rec_len < 8) || (rec_len % 4)) {
52 rec_len = 8;
53 retval = EXT2_ET_DIR_CORRUPTED;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040054 } else if (((name_len & 0xFF) + 8) > 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;
Theodore Ts'o8a480352009-06-21 21:07:38 -040075 unsigned int rec_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000076 struct ext2_dir_entry *dirent;
77
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040078 retval = ext2fs_get_mem(fs->blocksize, &buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050079 if (retval)
80 return retval;
81 memcpy(buf, inbuf, fs->blocksize);
82 p = buf;
83 end = buf + fs->blocksize;
84 while (p < end) {
85 dirent = (struct ext2_dir_entry *) p;
Theodore Ts'o8a480352009-06-21 21:07:38 -040086 if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
87 return retval;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040088 if ((rec_len < 8) ||
89 (rec_len % 4)) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040090 ext2fs_free_mem(&buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050091 return (EXT2_ET_DIR_CORRUPTED);
92 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040093 p += rec_len;
Theodore Ts'o126a2912007-08-11 01:56:48 -040094 dirent->inode = ext2fs_swab32(dirent->inode);
95 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
96 dirent->name_len = ext2fs_swab16(dirent->name_len);
97
Theodore Ts'of9190c82002-03-12 01:05:06 -050098 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
99 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'of9190c82002-03-12 01:05:06 -0500100 }
101 retval = io_channel_write_blk(fs->io, block, 1, buf);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400102 ext2fs_free_mem(&buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000103 return retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -0500104#else
105 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
106#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000107}
108
109
Theodore Ts'of9190c82002-03-12 01:05:06 -0500110errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
111 void *inbuf)
112{
113 return ext2fs_write_dir_block2(fs, block, inbuf, 0);
114}
115