Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / newdir.c
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 Public
8  * License.
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 #if EXT2_FLAT_INCLUDES
19 #include "ext2_fs.h"
20 #else
21 #include <linux/ext2_fs.h>
22 #endif
23
24 #include "ext2fs.h"
25
26 #ifndef EXT2_FT_DIR
27 #define EXT2_FT_DIR             2
28 #endif
29
30 /*
31  * Create new directory block
32  */
33 errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino,
34                                char **block)
35 {
36         struct ext2_dir_entry   *dir = NULL;
37         errcode_t               retval;
38         char                    *buf;
39         int                     rec_len;
40         int                     filetype = 0;
41         struct ext2fs_sb        *sb;
42
43         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44
45         retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
46         if (retval)
47                 return retval;
48         memset(buf, 0, fs->blocksize);
49         dir = (struct ext2_dir_entry *) buf;
50         dir->rec_len = fs->blocksize;
51
52         if (dir_ino) {
53                 sb = (struct ext2fs_sb *) fs->super;
54                 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
55                         filetype = EXT2_FT_DIR << 8;
56                 /*
57                  * Set up entry for '.'
58                  */
59                 dir->inode = dir_ino;
60                 dir->name_len = 1 | filetype;
61                 dir->name[0] = '.';
62                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1);
63                 dir->rec_len = EXT2_DIR_REC_LEN(1);
64
65                 /*
66                  * Set up entry for '..'
67                  */
68                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
69                 dir->rec_len = rec_len;
70                 dir->inode = parent_ino;
71                 dir->name_len = 2 | filetype;
72                 dir->name[0] = '.';
73                 dir->name[1] = '.';
74                 
75         }
76         *block = buf;
77         return 0;
78 }