Whamcloud - gitweb
e2fsprogs: fix compile error and warnings for old gcc versions
[tools/e2fsprogs.git] / lib / ext2fs / dirblock.c
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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <time.h>
19
20 #include "ext2_fs.h"
21 #include "ext2fs.h"
22
23 errcode_t ext2fs_read_dir_block4(ext2_filsys fs, blk64_t block,
24                                  void *buf, int flags EXT2FS_ATTR((unused)),
25                                  ext2_ino_t ino)
26 {
27         errcode_t       retval;
28         int             corrupt = 0;
29
30         retval = io_channel_read_blk64(fs->io, block, 1, buf);
31         if (retval)
32                 return retval;
33
34         if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
35             !ext2fs_dir_block_csum_verify(fs, ino,
36                                           (struct ext2_dir_entry *)buf))
37                 corrupt = 1;
38
39 #ifdef WORDS_BIGENDIAN
40         retval = ext2fs_dirent_swab_in(fs, buf, flags);
41 #endif
42         if (!retval && corrupt)
43                 retval = EXT2_ET_DIR_CSUM_INVALID;
44         return retval;
45 }
46
47 errcode_t ext2fs_read_dir_block3(ext2_filsys fs, blk64_t block,
48                                  void *buf, int flags EXT2FS_ATTR((unused)))
49 {
50         return ext2fs_read_dir_block4(fs, block, buf, flags, 0);
51 }
52
53 /*
54  * Compute the dirdata length. This includes only optional extensions.
55  * Each extension has a bit set in the high 4 bits of
56  * de->file_type, and the extension length is the first byte in each entry.
57  */
58 int ext2_get_dirdata_field_size(struct ext2_dir_entry *de,
59                                  char dirdata_flags)
60 {
61         char *lenp = de->name + (de->name_len & EXT2_NAME_LEN) + 1 /* NUL */;
62         __u8 extra_data_flags = (de->name_len & ~(EXT2_FT_MASK << 8)) >> 12;
63         int dlen = 0;
64
65         dirdata_flags >>= 4;
66         while ((extra_data_flags & dirdata_flags) != 0) {
67                 if (extra_data_flags & 1) {
68                         if (dirdata_flags & 1)
69                                 dlen += *lenp;
70
71                         lenp += *lenp;
72                 }
73                 extra_data_flags >>= 1;
74                 dirdata_flags >>= 1;
75         }
76
77         /* add NUL terminator byte to dirdata length */
78         return dlen + (dlen != 0);
79 }
80
81 int ext2_get_dirdata_size(struct ext2_dir_entry *de)
82 {
83         return ext2_get_dirdata_field_size(de, ~EXT2_FT_MASK);
84 }
85
86 errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
87                                  void *buf, int flags EXT2FS_ATTR((unused)))
88 {
89         return ext2fs_read_dir_block3(fs, block, buf, flags);
90 }
91
92 errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
93                                  void *buf)
94 {
95         return ext2fs_read_dir_block3(fs, block, buf, 0);
96 }
97
98
99 errcode_t ext2fs_write_dir_block4(ext2_filsys fs, blk64_t block,
100                                   void *inbuf, int flags EXT2FS_ATTR((unused)),
101                                   ext2_ino_t ino)
102 {
103         errcode_t       retval;
104         char            *buf = inbuf;
105
106 #ifdef WORDS_BIGENDIAN
107         retval = ext2fs_get_mem(fs->blocksize, &buf);
108         if (retval)
109                 return retval;
110         memcpy(buf, inbuf, fs->blocksize);
111         retval = ext2fs_dirent_swab_out(fs, buf, flags);
112         if (retval)
113                 return retval;
114 #endif
115         retval = ext2fs_dir_block_csum_set(fs, ino,
116                                            (struct ext2_dir_entry *)buf);
117         if (retval)
118                 goto out;
119
120         retval = io_channel_write_blk64(fs->io, block, 1, buf);
121
122 out:
123 #ifdef WORDS_BIGENDIAN
124         ext2fs_free_mem(&buf);
125 #endif
126         return retval;
127 }
128
129 errcode_t ext2fs_write_dir_block3(ext2_filsys fs, blk64_t block,
130                                   void *inbuf, int flags EXT2FS_ATTR((unused)))
131 {
132         return ext2fs_write_dir_block4(fs, block, inbuf, flags, 0);
133 }
134
135 errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
136                                  void *inbuf, int flags EXT2FS_ATTR((unused)))
137 {
138         return ext2fs_write_dir_block3(fs, block, inbuf, flags);
139 }
140
141 errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
142                                  void *inbuf)
143 {
144         return ext2fs_write_dir_block3(fs, block, inbuf, 0);
145 }
146