1 | /*
|
---|
2 | * dirblock.c --- directory block routines.
|
---|
3 | *
|
---|
4 | * 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 |
|
---|
22 | errcode_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;
|
---|
28 | unsigned int name_len, rec_len;
|
---|
29 | #if BB_BIG_ENDIAN
|
---|
30 | unsigned int do_swap;
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | retval = io_channel_read_blk(fs->io, block, 1, buf);
|
---|
34 | if (retval)
|
---|
35 | return retval;
|
---|
36 | #if BB_BIG_ENDIAN
|
---|
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 | #if BB_BIG_ENDIAN
|
---|
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 |
|
---|
68 | errcode_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 |
|
---|
75 | errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
|
---|
76 | void *inbuf, int flags EXT2FS_ATTR((unused)))
|
---|
77 | {
|
---|
78 | #if BB_BIG_ENDIAN
|
---|
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 | }
|
---|
113 | #ifdef WORDS_BIGENDIAN
|
---|
114 | if (flags & EXT2_DIRBLOCK_V2_STRUCT)
|
---|
115 | dirent->name_len = ext2fs_swab16(dirent->name_len);
|
---|
116 | #endif
|
---|
117 | }
|
---|
118 | retval = io_channel_write_blk(fs->io, block, 1, buf);
|
---|
119 | ext2fs_free_mem(&buf);
|
---|
120 | return retval;
|
---|
121 | #else
|
---|
122 | return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
|
---|
123 | #endif
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | errcode_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 |
|
---|