Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * newdir.c --- create a new directory block
      3  *
      4  * Copyright (C) 1994, 1995 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Library
      8  * General Public License, version 2.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #include <string.h>
     14 #if HAVE_UNISTD_H
     15 #include <unistd.h>
     16 #endif
     17 
     18 #include "ext2_fs.h"
     19 #include "ext2fs.h"
     20 
     21 #ifndef EXT2_FT_DIR
     22 #define EXT2_FT_DIR		2
     23 #endif
     24 
     25 /*
     26  * Create new directory block
     27  */
     28 errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
     29 			       ext2_ino_t parent_ino, char **block)
     30 {
     31 	struct ext2_dir_entry 	*dir = NULL;
     32 	errcode_t		retval;
     33 	char			*buf;
     34 	int			rec_len;
     35 	int			filetype = 0;
     36 
     37 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
     38 
     39 	retval = ext2fs_get_mem(fs->blocksize, &buf);
     40 	if (retval)
     41 		return retval;
     42 	memset(buf, 0, fs->blocksize);
     43 	dir = (struct ext2_dir_entry *) buf;
     44 
     45 	retval = ext2fs_set_rec_len(fs, fs->blocksize, dir);
     46 	if (retval)
     47 		return retval;
     48 
     49 	if (dir_ino) {
     50 		if (fs->super->s_feature_incompat &
     51 		    EXT2_FEATURE_INCOMPAT_FILETYPE)
     52 			filetype = EXT2_FT_DIR << 8;
     53 		/*
     54 		 * Set up entry for '.'
     55 		 */
     56 		dir->inode = dir_ino;
     57 		dir->name_len = 1 | filetype;
     58 		dir->name[0] = '.';
     59 		rec_len = fs->blocksize - EXT2_DIR_REC_LEN(1);
     60 		dir->rec_len = EXT2_DIR_REC_LEN(1);
     61 
     62 		/*
     63 		 * Set up entry for '..'
     64 		 */
     65 		dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
     66 		retval = ext2fs_set_rec_len(fs, rec_len, dir);
     67 		if (retval)
     68 			return retval;
     69 		dir->inode = parent_ino;
     70 		dir->name_len = 2 | filetype;
     71 		dir->name[0] = '.';
     72 		dir->name[1] = '.';
     73 
     74 	}
     75 	*block = buf;
     76 	return 0;
     77 }
     78