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