Home | History | Annotate | Download | only in ext2fs
      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 <string.h>
     17 #include <time.h>
     18 
     19 #include "ext2_fs.h"
     20 #include "ext2fs.h"
     21 
     22 errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
     23 				 void *buf, int flags EXT2FS_ATTR((unused)))
     24 {
     25 	errcode_t	retval;
     26 	char		*p, *end;
     27 	struct ext2_dir_entry *dirent;
     28 	unsigned int	name_len, rec_len;
     29 
     30 
     31  	retval = io_channel_read_blk(fs->io, block, 1, buf);
     32 	if (retval)
     33 		return retval;
     34 
     35 	p = (char *) buf;
     36 	end = (char *) buf + fs->blocksize;
     37 	while (p < end-8) {
     38 		dirent = (struct ext2_dir_entry *) p;
     39 #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);
     43 #endif
     44 		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
     49 		if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
     50 			return retval;
     51 		if ((rec_len < 8) || (rec_len % 4)) {
     52 			rec_len = 8;
     53 			retval = EXT2_ET_DIR_CORRUPTED;
     54 		} else if (((name_len & 0xFF) + 8) > rec_len)
     55 			retval = EXT2_ET_DIR_CORRUPTED;
     56 		p += rec_len;
     57 	}
     58 	return retval;
     59 }
     60 
     61 errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
     62 				 void *buf)
     63 {
     64 	return ext2fs_read_dir_block2(fs, block, buf, 0);
     65 }
     66 
     67 
     68 errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
     69 				  void *inbuf, int flags EXT2FS_ATTR((unused)))
     70 {
     71 #ifdef WORDS_BIGENDIAN
     72 	errcode_t	retval;
     73 	char		*p, *end;
     74 	char		*buf = 0;
     75 	unsigned int	rec_len;
     76 	struct ext2_dir_entry *dirent;
     77 
     78 	retval = ext2fs_get_mem(fs->blocksize, &buf);
     79 	if (retval)
     80 		return retval;
     81 	memcpy(buf, inbuf, fs->blocksize);
     82 	p = buf;
     83 	end = buf + fs->blocksize;
     84 	while (p < end) {
     85 		dirent = (struct ext2_dir_entry *) p;
     86 		if ((retval = ext2fs_get_rec_len(fs, dirent, &rec_len)) != 0)
     87 			return retval;
     88 		if ((rec_len < 8) ||
     89 		    (rec_len % 4)) {
     90 			ext2fs_free_mem(&buf);
     91 			return (EXT2_ET_DIR_CORRUPTED);
     92 		}
     93 		p += rec_len;
     94 		dirent->inode = ext2fs_swab32(dirent->inode);
     95 		dirent->rec_len = ext2fs_swab16(dirent->rec_len);
     96 		dirent->name_len = ext2fs_swab16(dirent->name_len);
     97 
     98 		if (flags & EXT2_DIRBLOCK_V2_STRUCT)
     99 			dirent->name_len = ext2fs_swab16(dirent->name_len);
    100 	}
    101  	retval = io_channel_write_blk(fs->io, block, 1, buf);
    102 	ext2fs_free_mem(&buf);
    103 	return retval;
    104 #else
    105  	return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
    106 #endif
    107 }
    108 
    109 
    110 errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
    111 				 void *inbuf)
    112 {
    113 	return ext2fs_write_dir_block2(fs, block, inbuf, 0);
    114 }
    115 
    116