Whamcloud - gitweb
ChangeLog, dirblock.c:
[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 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 #if EXT2_FLAT_INCLUDES
20 #include "ext2_fs.h"
21 #else
22 #include <linux/ext2_fs.h>
23 #endif
24
25 #include "ext2fs.h"
26
27 errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
28                                 void *buf)
29 {
30         errcode_t       retval;
31         char            *p, *end;
32         struct ext2_dir_entry *dirent;
33
34         retval = io_channel_read_blk(fs->io, block, 1, buf);
35         if (retval)
36                 return retval;
37         if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
38                           EXT2_FLAG_SWAP_BYTES_READ)) == 0)
39                 return 0;
40         p = (char *) buf;
41         end = (char *) buf + fs->blocksize;
42         while (p < end-8) {
43                 dirent = (struct ext2_dir_entry *) p;
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                 p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
48         }
49         return 0;
50 }
51
52 errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
53                                  void *inbuf)
54 {
55         errcode_t       retval;
56         char            *p, *end, *write_buf;
57         char            *buf = 0;
58         struct ext2_dir_entry *dirent;
59
60         if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
61             (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
62                 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
63                 if (retval)
64                         return retval;
65                 write_buf = buf;
66                 memcpy(buf, inbuf, fs->blocksize);
67                 p = buf;
68                 end = buf + fs->blocksize;
69                 while (p < end) {
70                         dirent = (struct ext2_dir_entry *) p;
71                         p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
72                         dirent->inode = ext2fs_swab32(dirent->inode);
73                         dirent->rec_len = ext2fs_swab16(dirent->rec_len);
74                         dirent->name_len = ext2fs_swab16(dirent->name_len);
75                 }
76         } else
77                 write_buf = (char *) inbuf;
78         retval = io_channel_write_blk(fs->io, block, 1, write_buf);
79         if (buf)
80                 ext2fs_free_mem((void **) &buf);
81         return retval;
82 }
83
84