Whamcloud - gitweb
unix_io.c (unix_open): Fix 2.4 resource limit workaround so that
[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 #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, (void **) &buf);
40         if (retval)
41                 return retval;
42         memset(buf, 0, fs->blocksize);
43         dir = (struct ext2_dir_entry *) buf;
44         dir->rec_len = fs->blocksize;
45
46         if (dir_ino) {
47                 if (fs->super->s_feature_incompat &
48                     EXT2_FEATURE_INCOMPAT_FILETYPE)
49                         filetype = EXT2_FT_DIR << 8;
50                 /*
51                  * Set up entry for '.'
52                  */
53                 dir->inode = dir_ino;
54                 dir->name_len = 1 | filetype;
55                 dir->name[0] = '.';
56                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1);
57                 dir->rec_len = EXT2_DIR_REC_LEN(1);
58
59                 /*
60                  * Set up entry for '..'
61                  */
62                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
63                 dir->rec_len = rec_len;
64                 dir->inode = parent_ino;
65                 dir->name_len = 2 | filetype;
66                 dir->name[0] = '.';
67                 dir->name[1] = '.';
68                 
69         }
70         *block = buf;
71         return 0;
72 }