blob: 185384b185c3a0972b6367a124d20444916d63a0 [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,
23 void *buf, int flags)
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'of9190c82002-03-12 01:05:06 -050028 unsigned int name_len, rec_len, do_swap;
29
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'o5df55d72001-06-11 07:00:04 +000034#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000035 do_swap = (fs->flags & (EXT2_FLAG_SWAP_BYTES|
36 EXT2_FLAG_SWAP_BYTES_READ)) != 0;
Theodore Ts'o5df55d72001-06-11 07:00:04 +000037#endif
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000038 p = (char *) buf;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000039 end = (char *) buf + fs->blocksize;
Theodore Ts'oe446d712001-01-05 22:27:06 +000040 while (p < end-8) {
Theodore Ts'obfcd9a12001-05-05 16:21:48 +000041 dirent = (struct ext2_dir_entry *) p;
Theodore Ts'o5df55d72001-06-11 07:00:04 +000042#ifdef EXT2FS_ENABLE_SWAPFS
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000043 if (do_swap) {
44 dirent->inode = ext2fs_swab32(dirent->inode);
45 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
46 dirent->name_len = ext2fs_swab16(dirent->name_len);
47 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +000048#endif
Theodore Ts'of9190c82002-03-12 01:05:06 -050049 name_len = dirent->name_len;
50#ifdef WORDS_BIGENDIAN
51 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
52 dirent->name_len = ext2fs_swab16(dirent->name_len);
53#endif
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000054 rec_len = dirent->rec_len;
55 if ((rec_len < 8) || (rec_len % 4)) {
56 rec_len = 8;
57 retval = EXT2_ET_DIR_CORRUPTED;
58 }
Theodore Ts'of9190c82002-03-12 01:05:06 -050059 if (((name_len & 0xFF) + 8) > dirent->rec_len)
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000060 retval = EXT2_ET_DIR_CORRUPTED;
61 p += rec_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000062 }
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000063 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000064}
65
Theodore Ts'of9190c82002-03-12 01:05:06 -050066errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
67 void *buf)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000068{
Theodore Ts'of9190c82002-03-12 01:05:06 -050069 return ext2fs_read_dir_block2(fs, block, buf, 0);
70}
71
72
73errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
74 void *inbuf, int flags)
75{
76#ifdef EXT2FS_ENABLE_SWAPFS
77 int do_swap = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000078 errcode_t retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -050079 char *p, *end;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000080 char *buf = 0;
81 struct ext2_dir_entry *dirent;
82
Theodore Ts'o5c576471997-04-29 15:29:49 +000083 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
Theodore Ts'of9190c82002-03-12 01:05:06 -050084 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
85 do_swap = 1;
86
87#ifndef WORDS_BIGENDIAN
88 if (!do_swap)
89 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
90#endif
91
92 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
93 if (retval)
94 return retval;
95 memcpy(buf, inbuf, fs->blocksize);
96 p = buf;
97 end = buf + fs->blocksize;
98 while (p < end) {
99 dirent = (struct ext2_dir_entry *) p;
100 if ((dirent->rec_len < 8) ||
101 (dirent->rec_len % 4)) {
102 ext2fs_free_mem((void **) &buf);
103 return (EXT2_ET_DIR_CORRUPTED);
104 }
105 p += dirent->rec_len;
106 if (do_swap) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000107 dirent->inode = ext2fs_swab32(dirent->inode);
108 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
109 dirent->name_len = ext2fs_swab16(dirent->name_len);
110 }
Theodore Ts'of9190c82002-03-12 01:05:06 -0500111#ifdef WORDS_BIGENDIAN
112 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
113 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000114#endif
Theodore Ts'of9190c82002-03-12 01:05:06 -0500115 }
116 retval = io_channel_write_blk(fs->io, block, 1, buf);
117 ext2fs_free_mem((void **) &buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000118 return retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -0500119#else
120 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
121#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000122}
123
124
Theodore Ts'of9190c82002-03-12 01:05:06 -0500125errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
126 void *inbuf)
127{
128 return ext2fs_write_dir_block2(fs, block, inbuf, 0);
129}
130