blob: 863960fac1a16f165fface712124d61df1ae7392 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * newdir.c --- create a new directory block
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15#include <stdlib.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000016#if HAVE_ERRNO_H
17#include <errno.h>
18#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000019
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <linux/ext2_fs.h>
21
22#include "ext2fs.h"
23
24/*
25 * Create new directory block
26 */
27errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino,
28 char **block)
29{
30 char *buf;
31 struct ext2_dir_entry *dir = NULL;
32 int rec_len;
33
Theodore Ts'of3db3561997-04-26 13:34:30 +000034 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
35
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 buf = malloc(fs->blocksize);
37 if (!buf)
38 return ENOMEM;
39 memset(buf, 0, fs->blocksize);
40 dir = (struct ext2_dir_entry *) buf;
41 dir->rec_len = fs->blocksize;
42
43 if (dir_ino) {
44 /*
45 * Set up entry for '.'
46 */
47 dir->inode = dir_ino;
48 dir->name_len = 1;
49 dir->name[0] = '.';
50 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len);
51 dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len);
52
53 /*
54 * Set up entry for '..'
55 */
56 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
57 dir->rec_len = rec_len;
58 dir->inode = parent_ino;
59 dir->name_len = 2;
60 dir->name[0] = '.';
61 dir->name[1] = '.';
62
63 }
64 *block = buf;
65 return 0;
66}