blob: 73e1f0abe5f6e050417dd23291bbd98b62a620c0 [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * dirblock.c --- directory block routines.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00009 * %End-Header%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000016#include <string.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o50e1e101997-04-26 13:58:21 +000020#include "ext2fs.h"
21
JP Abgralle0ed7402014-03-19 19:08:39 -070022errcode_t ext2fs_read_dir_block3(ext2_filsys fs, blk64_t block,
Theodore Ts'o54434922003-12-07 01:28:50 -050023 void *buf, int flags EXT2FS_ATTR((unused)))
Theodore Ts'o50e1e101997-04-26 13:58:21 +000024{
25 errcode_t retval;
26 char *p, *end;
27 struct ext2_dir_entry *dirent;
Theodore Ts'o126a2912007-08-11 01:56:48 -040028 unsigned int name_len, rec_len;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040029
Theodore Ts'o50e1e101997-04-26 13:58:21 +000030
JP Abgralle0ed7402014-03-19 19:08:39 -070031 retval = io_channel_read_blk64(fs->io, block, 1, buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000032 if (retval)
33 return retval;
Theodore Ts'o126a2912007-08-11 01:56:48 -040034
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000035 p = (char *) buf;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000036 end = (char *) buf + fs->blocksize;
Theodore Ts'oe446d712001-01-05 22:27:06 +000037 while (p < end-8) {
Theodore Ts'obfcd9a12001-05-05 16:21:48 +000038 dirent = (struct ext2_dir_entry *) p;
Theodore Ts'o126a2912007-08-11 01:56:48 -040039#ifdef WORDS_BIGENDIAN
40 dirent->inode = ext2fs_swab32(dirent->inode);
41 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
42 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'o5df55d72001-06-11 07:00:04 +000043#endif
Theodore Ts'of9190c82002-03-12 01:05:06 -050044 name_len = dirent->name_len;
45#ifdef WORDS_BIGENDIAN
46 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
47 dirent->name_len = ext2fs_swab16(dirent->name_len);
48#endif
Theodore Ts'o8a480352009-06-21 21:07:38 -040049 if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
50 return retval;
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000051 if ((rec_len < 8) || (rec_len % 4)) {
52 rec_len = 8;
53 retval = EXT2_ET_DIR_CORRUPTED;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040054 } else if (((name_len & 0xFF) + 8) > rec_len)
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000055 retval = EXT2_ET_DIR_CORRUPTED;
56 p += rec_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000057 }
Theodore Ts'o1cca4d62001-05-05 05:12:14 +000058 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059}
60
JP Abgralle0ed7402014-03-19 19:08:39 -070061errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
62 void *buf, int flags EXT2FS_ATTR((unused)))
63{
64 return ext2fs_read_dir_block3(fs, block, buf, flags);
65}
66
Theodore Ts'of9190c82002-03-12 01:05:06 -050067errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
68 void *buf)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000069{
JP Abgralle0ed7402014-03-19 19:08:39 -070070 return ext2fs_read_dir_block3(fs, block, buf, 0);
Theodore Ts'of9190c82002-03-12 01:05:06 -050071}
72
73
JP Abgralle0ed7402014-03-19 19:08:39 -070074errcode_t ext2fs_write_dir_block3(ext2_filsys fs, blk64_t block,
Theodore Ts'o54434922003-12-07 01:28:50 -050075 void *inbuf, int flags EXT2FS_ATTR((unused)))
Theodore Ts'of9190c82002-03-12 01:05:06 -050076{
Theodore Ts'o126a2912007-08-11 01:56:48 -040077#ifdef WORDS_BIGENDIAN
Theodore Ts'o50e1e101997-04-26 13:58:21 +000078 errcode_t retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -050079 char *p, *end;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000080 char *buf = 0;
Theodore Ts'o8a480352009-06-21 21:07:38 -040081 unsigned int rec_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000082 struct ext2_dir_entry *dirent;
83
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040084 retval = ext2fs_get_mem(fs->blocksize, &buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050085 if (retval)
86 return retval;
87 memcpy(buf, inbuf, fs->blocksize);
88 p = buf;
89 end = buf + fs->blocksize;
90 while (p < end) {
91 dirent = (struct ext2_dir_entry *) p;
Theodore Ts'o8a480352009-06-21 21:07:38 -040092 if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
93 return retval;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040094 if ((rec_len < 8) ||
95 (rec_len % 4)) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040096 ext2fs_free_mem(&buf);
Theodore Ts'of9190c82002-03-12 01:05:06 -050097 return (EXT2_ET_DIR_CORRUPTED);
98 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040099 p += rec_len;
Theodore Ts'o126a2912007-08-11 01:56:48 -0400100 dirent->inode = ext2fs_swab32(dirent->inode);
101 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
102 dirent->name_len = ext2fs_swab16(dirent->name_len);
103
Theodore Ts'of9190c82002-03-12 01:05:06 -0500104 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
105 dirent->name_len = ext2fs_swab16(dirent->name_len);
Theodore Ts'of9190c82002-03-12 01:05:06 -0500106 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700107 retval = io_channel_write_blk64(fs->io, block, 1, buf);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400108 ext2fs_free_mem(&buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000109 return retval;
Theodore Ts'of9190c82002-03-12 01:05:06 -0500110#else
JP Abgralle0ed7402014-03-19 19:08:39 -0700111 return io_channel_write_blk64(fs->io, block, 1, (char *) inbuf);
Theodore Ts'of9190c82002-03-12 01:05:06 -0500112#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000113}
114
JP Abgralle0ed7402014-03-19 19:08:39 -0700115errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
116 void *inbuf, int flags EXT2FS_ATTR((unused)))
117{
118 return ext2fs_write_dir_block3(fs, block, inbuf, flags);
119}
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000120
Theodore Ts'of9190c82002-03-12 01:05:06 -0500121errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
122 void *inbuf)
123{
JP Abgralle0ed7402014-03-19 19:08:39 -0700124 return ext2fs_write_dir_block3(fs, block, inbuf, 0);
Theodore Ts'of9190c82002-03-12 01:05:06 -0500125}
126