Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / newdir.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * newdir.c --- create a new directory block
4  *
5  * Copyright (C) 1994, 1995 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
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         dir->rec_len = fs->blocksize;
46
47         if (dir_ino) {
48                 if (fs->super->s_feature_incompat &
49                     EXT2_FEATURE_INCOMPAT_FILETYPE)
50                         filetype = EXT2_FT_DIR << 8;
51                 /*
52                  * Set up entry for '.'
53                  */
54                 dir->inode = dir_ino;
55                 dir->name_len = 1 | filetype;
56                 dir->name[0] = '.';
57                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1);
58                 dir->rec_len = EXT2_DIR_REC_LEN(1);
59
60                 /*
61                  * Set up entry for '..'
62                  */
63                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
64                 dir->rec_len = rec_len;
65                 dir->inode = parent_ino;
66                 dir->name_len = 2 | filetype;
67                 dir->name[0] = '.';
68                 dir->name[1] = '.';
69
70         }
71         *block = buf;
72         return 0;
73 }