blob: 9f82b500231ac16129d2ca3cc257de8e5f815b3a [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * dirblock.c --- directory block routines.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +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%
10 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16#include <string.h>
17#include <time.h>
18
19#include "ext2_fs.h"
20#include "ext2fs.h"
21
22errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
23 void *buf, int flags EXT2FS_ATTR((unused)))
24{
25 errcode_t retval;
26 char *p, *end;
27 struct ext2_dir_entry *dirent;
Mike Frysingere70aa9d2005-07-30 09:43:58 +000028 unsigned int name_len, rec_len;
29#ifdef EXT2FS_ENABLE_SWAPFS
30 unsigned int do_swap;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000031#endif
Mike Frysinger1fd98e02005-05-09 22:10:42 +000032
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033 retval = io_channel_read_blk(fs->io, block, 1, buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000034 if (retval)
35 return retval;
36#ifdef EXT2FS_ENABLE_SWAPFS
37 do_swap = (fs->flags & (EXT2_FLAG_SWAP_BYTES|
38 EXT2_FLAG_SWAP_BYTES_READ)) != 0;
39#endif
40 p = (char *) buf;
41 end = (char *) buf + fs->blocksize;
42 while (p < end-8) {
43 dirent = (struct ext2_dir_entry *) p;
44#ifdef EXT2FS_ENABLE_SWAPFS
45 if (do_swap) {
46 dirent->inode = ext2fs_swab32(dirent->inode);
47 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
48 dirent->name_len = ext2fs_swab16(dirent->name_len);
49 }
50#endif
51 name_len = dirent->name_len;
52#ifdef WORDS_BIGENDIAN
53 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
54 dirent->name_len = ext2fs_swab16(dirent->name_len);
55#endif
56 rec_len = dirent->rec_len;
57 if ((rec_len < 8) || (rec_len % 4)) {
58 rec_len = 8;
59 retval = EXT2_ET_DIR_CORRUPTED;
60 }
61 if (((name_len & 0xFF) + 8) > dirent->rec_len)
62 retval = EXT2_ET_DIR_CORRUPTED;
63 p += rec_len;
64 }
65 return retval;
66}
67
68errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
69 void *buf)
70{
71 return ext2fs_read_dir_block2(fs, block, buf, 0);
72}
73
74
75errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
76 void *inbuf, int flags EXT2FS_ATTR((unused)))
77{
78#ifdef EXT2FS_ENABLE_SWAPFS
79 int do_swap = 0;
80 errcode_t retval;
81 char *p, *end;
82 char *buf = 0;
83 struct ext2_dir_entry *dirent;
84
85 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
86 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
87 do_swap = 1;
88
89#ifndef WORDS_BIGENDIAN
90 if (!do_swap)
91 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
92#endif
93
94 retval = ext2fs_get_mem(fs->blocksize, &buf);
95 if (retval)
96 return retval;
97 memcpy(buf, inbuf, fs->blocksize);
98 p = buf;
99 end = buf + fs->blocksize;
100 while (p < end) {
101 dirent = (struct ext2_dir_entry *) p;
102 if ((dirent->rec_len < 8) ||
103 (dirent->rec_len % 4)) {
104 ext2fs_free_mem(&buf);
105 return (EXT2_ET_DIR_CORRUPTED);
106 }
107 p += dirent->rec_len;
108 if (do_swap) {
109 dirent->inode = ext2fs_swab32(dirent->inode);
110 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
111 dirent->name_len = ext2fs_swab16(dirent->name_len);
112 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000113#ifdef WORDS_BIGENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000114 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
115 dirent->name_len = ext2fs_swab16(dirent->name_len);
116#endif
117 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000118 retval = io_channel_write_blk(fs->io, block, 1, buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000119 ext2fs_free_mem(&buf);
120 return retval;
121#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122 return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000123#endif
124}
125
126
127errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
128 void *inbuf)
129{
130 return ext2fs_write_dir_block2(fs, block, inbuf, 0);
131}
132