blob: 3e2c0dbec9a8e2658c9ab4aae1a8abff91427123 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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 Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include "config.h"
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 */
29errcode_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
46 retval = ext2fs_set_rec_len(fs, fs->blocksize, dir);
47 if (retval) {
48 ext2fs_free_mem(&buf);
49 return retval;
50 }
51
52 if (dir_ino) {
53 if (fs->super->s_feature_incompat &
54 EXT2_FEATURE_INCOMPAT_FILETYPE)
55 filetype = EXT2_FT_DIR << 8;
56 /*
57 * Set up entry for '.'
58 */
59 dir->inode = dir_ino;
60 dir->name_len = 1 | filetype;
61 dir->name[0] = '.';
62 rec_len = fs->blocksize - EXT2_DIR_REC_LEN(1);
63 dir->rec_len = EXT2_DIR_REC_LEN(1);
64
65 /*
66 * Set up entry for '..'
67 */
68 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
69 retval = ext2fs_set_rec_len(fs, rec_len, dir);
70 if (retval) {
71 ext2fs_free_mem(&buf);
72 return retval;
73 }
74 dir->inode = parent_ino;
75 dir->name_len = 2 | filetype;
76 dir->name[0] = '.';
77 dir->name[1] = '.';
78
79 }
80 *block = buf;
81 return 0;
82}