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 */
ext2fs_new_dir_block(ext2_filsys fs,ext2_ino_t dir_ino,ext2_ino_t parent_ino,char ** block)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 ext2fs_free_mem(&buf);
48 return retval;
49 }
50
51 if (dir_ino) {
52 if (fs->super->s_feature_incompat &
53 EXT2_FEATURE_INCOMPAT_FILETYPE)
54 filetype = EXT2_FT_DIR << 8;
55 /*
56 * Set up entry for '.'
57 */
58 dir->inode = dir_ino;
59 dir->name_len = 1 | filetype;
60 dir->name[0] = '.';
61 rec_len = fs->blocksize - EXT2_DIR_REC_LEN(1);
62 dir->rec_len = EXT2_DIR_REC_LEN(1);
63
64 /*
65 * Set up entry for '..'
66 */
67 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
68 retval = ext2fs_set_rec_len(fs, rec_len, dir);
69 if (retval) {
70 ext2fs_free_mem(&buf);
71 return retval;
72 }
73 dir->inode = parent_ino;
74 dir->name_len = 2 | filetype;
75 dir->name[0] = '.';
76 dir->name[1] = '.';
77
78 }
79 *block = buf;
80 return 0;
81 }
82