Whamcloud - gitweb
4f8b94b2535e60ac0957bb12e4944be128b060b0
[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 <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #ifdef HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22
23 #include <linux/ext2_fs.h>
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 = buf;
41         end = (char *) buf + fs->blocksize;
42         while (p < end) {
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                 write_buf = buf = malloc(fs->blocksize);
63                 if (!buf)
64                         return ENOMEM;
65                 memcpy(buf, inbuf, fs->blocksize);
66                 p = buf;
67                 end = buf + fs->blocksize;
68                 while (p < end) {
69                         dirent = (struct ext2_dir_entry *) p;
70                         p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
71                         dirent->inode = ext2fs_swab32(dirent->inode);
72                         dirent->rec_len = ext2fs_swab16(dirent->rec_len);
73                         dirent->name_len = ext2fs_swab16(dirent->name_len);
74                 }
75         } else
76                 write_buf = inbuf;
77         retval = io_channel_write_blk(fs->io, block, 1, write_buf);
78         if (buf)
79                 free(buf);
80         return retval;
81 }
82
83