blob: 0f56617d303670f619dd808ffe3531aca76c0b37 [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
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
20errcode_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;
Theodore Ts'o5c576471997-04-29 15:29:49 +000030 if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
31 EXT2_FLAG_SWAP_BYTES_READ)) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000032 return 0;
33 p = buf;
34 end = (char *) buf + fs->blocksize;
35 while (p < end) {
36 dirent = (struct ext2_dir_entry *) p;
37 dirent->inode = ext2fs_swab32(dirent->inode);
38 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
39 dirent->name_len = ext2fs_swab16(dirent->name_len);
40 p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
41 }
42 return 0;
43}
44
45errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
46 void *inbuf)
47{
48 errcode_t retval;
49 char *p, *end, *write_buf;
50 char *buf = 0;
51 struct ext2_dir_entry *dirent;
52
Theodore Ts'o5c576471997-04-29 15:29:49 +000053 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
54 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000055 write_buf = buf = malloc(fs->blocksize);
56 if (!buf)
57 return ENOMEM;
58 memcpy(buf, inbuf, fs->blocksize);
59 p = buf;
60 end = buf + fs->blocksize;
61 while (p < end) {
62 dirent = (struct ext2_dir_entry *) p;
63 p += (dirent->rec_len < 8) ? 8 : dirent->rec_len;
64 dirent->inode = ext2fs_swab32(dirent->inode);
65 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
66 dirent->name_len = ext2fs_swab16(dirent->name_len);
67 }
68 } else
69 write_buf = inbuf;
70 retval = io_channel_write_blk(fs->io, block, 1, write_buf);
71 if (buf)
72 free(buf);
73 return retval;
74}
75
76