Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / dirblock.c
1 /*
2  * dirblock.c --- directory block routines.
3  * 
4  * Copyright (C) 1995 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #ifdef HAVE_ERRNO_H
13 #include <errno.h>
14 #endif
15
16 #include <linux/ext2_fs.h>
17
18 #include "ext2fs.h"
19
20 errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
21                                 void *buf)
22 {
23         errcode_t       retval;
24         char            *p, *end;
25         struct ext2_dir_entry *dirent;
26
27         retval = io_channel_read_blk(fs->io, block, 1, buf);
28         if (retval)
29                 return retval;
30         if ((fs->flags & EXT2_SWAP_BYTES) == 0)
31                 return 0;
32         p = buf;
33         end = (char *) buf + fs->blocksize;
34         while (p < end) {
35                 dirent = (struct ext2_dir_entry *) p;
36                 dirent->inode = ext2fs_swab32(dirent->inode);
37                 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
38                 dirent->name_len = ext2fs_swab16(dirent->name_len);
39                 p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
40         }
41         return 0;
42 }
43
44 errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
45                                  void *inbuf)
46 {
47         errcode_t       retval;
48         char            *p, *end, *write_buf;
49         char            *buf = 0;
50         struct ext2_dir_entry *dirent;
51
52         if (fs->flags & EXT2_SWAP_BYTES) {
53                 write_buf = buf = malloc(fs->blocksize);
54                 if (!buf)
55                         return ENOMEM;
56                 memcpy(buf, inbuf, fs->blocksize);
57                 p = buf;
58                 end = buf + fs->blocksize;
59                 while (p < end) {
60                         dirent = (struct ext2_dir_entry *) p;
61                         p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
62                         dirent->inode = ext2fs_swab32(dirent->inode);
63                         dirent->rec_len = ext2fs_swab16(dirent->rec_len);
64                         dirent->name_len = ext2fs_swab16(dirent->name_len);
65                 }
66         } else
67                 write_buf = inbuf;
68         retval = io_channel_write_blk(fs->io, block, 1, write_buf);
69         if (buf)
70                 free(buf);
71         return retval;
72 }
73
74