Whamcloud - gitweb
libext2fs: move the alignment field from unix_io to the io_manager
[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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19 #include "ext2_fs.h"
20 #include "ext2fs.h"
21
22 #ifndef EXT2_FT_DIR
23 #define EXT2_FT_DIR             2
24 #endif
25
26 /*
27  * Create new directory block
28  */
29 errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
30                                ext2_ino_t parent_ino, char **block)
31 {
32         struct ext2_dir_entry   *dir = NULL;
33         errcode_t               retval;
34         char                    *buf;
35         int                     rec_len;
36         int                     filetype = 0;
37
38         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
39
40         retval = ext2fs_get_mem(fs->blocksize, &buf);
41         if (retval)
42                 return retval;
43         memset(buf, 0, fs->blocksize);
44         dir = (struct ext2_dir_entry *) buf;
45
46         retval = ext2fs_set_rec_len(fs, fs->blocksize, dir);
47         if (retval)
48                 return retval;
49
50         if (dir_ino) {
51                 if (fs->super->s_feature_incompat &
52                     EXT2_FEATURE_INCOMPAT_FILETYPE)
53                         filetype = EXT2_FT_DIR << 8;
54                 /*
55                  * Set up entry for '.'
56                  */
57                 dir->inode = dir_ino;
58                 dir->name_len = 1 | filetype;
59                 dir->name[0] = '.';
60                 rec_len = fs->blocksize - EXT2_DIR_REC_LEN(1);
61                 dir->rec_len = EXT2_DIR_REC_LEN(1);
62
63                 /*
64                  * Set up entry for '..'
65                  */
66                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
67                 retval = ext2fs_set_rec_len(fs, rec_len, dir);
68                 if (retval)
69                         return retval;
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 }