Whamcloud - gitweb
506d609f0b875c78c9389923b4258a0a9ea7e6a2
[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         struct ext2_dir_entry_tail      *t;
38         int                     csum_size = 0;
39
40         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
41
42         retval = ext2fs_get_mem(fs->blocksize, &buf);
43         if (retval)
44                 return retval;
45         memset(buf, 0, fs->blocksize);
46         dir = (struct ext2_dir_entry *) buf;
47
48         if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
49                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
50                 csum_size = sizeof(struct ext2_dir_entry_tail);
51
52         retval = ext2fs_set_rec_len(fs, fs->blocksize - csum_size, dir);
53         if (retval) {
54                 ext2fs_free_mem(&buf);
55                 return retval;
56         }
57
58         if (dir_ino) {
59                 if (fs->super->s_feature_incompat &
60                     EXT2_FEATURE_INCOMPAT_FILETYPE)
61                         filetype = EXT2_FT_DIR;
62                 /*
63                  * Set up entry for '.'
64                  */
65                 dir->inode = dir_ino;
66                 ext2fs_dirent_set_name_len(dir, 1);
67                 ext2fs_dirent_set_file_type(dir, filetype);
68                 dir->name[0] = '.';
69                 rec_len = (fs->blocksize - csum_size) - EXT2_DIR_REC_LEN(1);
70                 dir->rec_len = EXT2_DIR_REC_LEN(1);
71
72                 /*
73                  * Set up entry for '..'
74                  */
75                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
76                 retval = ext2fs_set_rec_len(fs, rec_len, dir);
77                 if (retval) {
78                         ext2fs_free_mem(&buf);
79                         return retval;
80                 }
81                 dir->inode = parent_ino;
82                 ext2fs_dirent_set_name_len(dir, 2);
83                 ext2fs_dirent_set_file_type(dir, filetype);
84                 dir->name[0] = '.';
85                 dir->name[1] = '.';
86
87         }
88
89         if (csum_size) {
90                 t = EXT2_DIRENT_TAIL(buf, fs->blocksize);
91                 ext2fs_initialize_dirent_tail(fs, t);
92         }
93         *block = buf;
94         return 0;
95 }
96
97 /*
98  * Create new directory on inline data
99  */
100 errcode_t ext2fs_new_dir_inline_data(ext2_filsys fs, ext2_ino_t dir_ino,
101                                      ext2_ino_t parent_ino, __u32 *iblock)
102 {
103         struct ext2_dir_entry   *dir = NULL;
104         errcode_t               retval;
105         char                    *buf;
106         int                     rec_len;
107         int                     filetype = 0;
108
109         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
110
111         iblock[0] = ext2fs_cpu_to_le32(parent_ino);
112
113         dir = (struct ext2_dir_entry *)((char *)iblock +
114                                         EXT4_INLINE_DATA_DOTDOT_SIZE);
115         dir->inode = 0;
116         rec_len = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE;
117         retval = ext2fs_set_rec_len(fs, rec_len, dir);
118         if (retval)
119                 goto errout;
120
121 #ifdef WORDS_BIGENDIAN
122         retval = ext2fs_dirent_swab_out2(fs, (char *)dir, rec_len, 0);
123         if (retval)
124                 goto errout;
125 #endif
126
127 errout:
128         return retval;
129 }