Whamcloud - gitweb
8c0511ec08d636743c092bb296670f3f49c95a00
[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 /*
27  * Create new directory block
28  */
29 errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino,
30                                char **block)
31 {
32         struct ext2_dir_entry   *dir = NULL;
33         errcode_t               retval;
34         char                    *buf;
35         int                     rec_len;
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                 /*
48                  * Set up entry for '.'
49                  */
50                 dir->inode = dir_ino;
51                 dir->name_len = 1;
52                 dir->name[0] = '.';
53                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len);
54                 dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len);
55
56                 /*
57                  * Set up entry for '..'
58                  */
59                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
60                 dir->rec_len = rec_len;
61                 dir->inode = parent_ino;
62                 dir->name_len = 2;
63                 dir->name[0] = '.';
64                 dir->name[1] = '.';
65                 
66         }
67         *block = buf;
68         return 0;
69 }