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 #include <stdlib.h>
18 #if HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21
22 #include <linux/ext2_fs.h>
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         char    *buf;
33         struct ext2_dir_entry *dir = NULL;
34         int     rec_len;
35
36         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
37
38         buf = malloc(fs->blocksize);
39         if (!buf)
40                 return ENOMEM;
41         memset(buf, 0, fs->blocksize);
42         dir = (struct ext2_dir_entry *) buf;
43         dir->rec_len = fs->blocksize;
44
45         if (dir_ino) {
46                 /*
47                  * Set up entry for '.'
48                  */
49                 dir->inode = dir_ino;
50                 dir->name_len = 1;
51                 dir->name[0] = '.';
52                 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len);
53                 dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len);
54
55                 /*
56                  * Set up entry for '..'
57                  */
58                 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
59                 dir->rec_len = rec_len;
60                 dir->inode = parent_ino;
61                 dir->name_len = 2;
62                 dir->name[0] = '.';
63                 dir->name[1] = '.';
64                 
65         }
66         *block = buf;
67         return 0;
68 }