Whamcloud - gitweb
Many files:
[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 <linux/ext2_fs.h>
19
20 #include "ext2fs.h"
21
22 /*
23  * Create new directory block
24  */
25 errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino,
26                                char **block)
27 {
28         struct ext2_dir_entry   *dir = NULL;
29         errcode_t               retval;
30         char                    *buf;
31         int                     rec_len;
32
33         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
34
35         retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
36         if (retval)
37                 return retval;
38         memset(buf, 0, fs->blocksize);
39         dir = (struct ext2_dir_entry *) buf;
40         dir->rec_len = fs->blocksize;
41
42         if (dir_ino) {
43                 /*
44                  * Set up entry for '.'
45                  */
46                 dir->inode = dir_ino;
47                 dir->name_len = 1;
48                 dir->name[0] = '.';
49                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len);
50                 dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len);
51
52                 /*
53                  * Set up entry for '..'
54                  */
55                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
56                 dir->rec_len = rec_len;
57                 dir->inode = parent_ino;
58                 dir->name_len = 2;
59                 dir->name[0] = '.';
60                 dir->name[1] = '.';
61                 
62         }
63         *block = buf;
64         return 0;
65 }