blob: 940256bcaee60a9718ab1aaf6c5a940096dc3c6a [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>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000017
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000018#if EXT2_FLAT_INCLUDES
19#include "ext2_fs.h"
20#else
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000022#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000023
24#include "ext2fs.h"
25
Theodore Ts'oe6198e51999-10-23 00:58:54 +000026#ifndef EXT2_FT_DIR
27#define EXT2_FT_DIR 2
28#endif
29
Theodore Ts'o3839e651997-04-26 13:21:57 +000030/*
31 * Create new directory block
32 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000033errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
34 ext2_ino_t parent_ino, char **block)
Theodore Ts'o3839e651997-04-26 13:21:57 +000035{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000036 struct ext2_dir_entry *dir = NULL;
37 errcode_t retval;
38 char *buf;
39 int rec_len;
Theodore Ts'oe6198e51999-10-23 00:58:54 +000040 int filetype = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000041
Theodore Ts'of3db3561997-04-26 13:34:30 +000042 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000044 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
45 if (retval)
46 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000047 memset(buf, 0, fs->blocksize);
48 dir = (struct ext2_dir_entry *) buf;
49 dir->rec_len = fs->blocksize;
50
51 if (dir_ino) {
Theodore Ts'oe5b38a52001-01-01 16:17:12 +000052 if (fs->super->s_feature_incompat &
53 EXT2_FEATURE_INCOMPAT_FILETYPE)
Theodore Ts'oe6198e51999-10-23 00:58:54 +000054 filetype = EXT2_FT_DIR << 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 /*
56 * Set up entry for '.'
57 */
58 dir->inode = dir_ino;
Theodore Ts'oe6198e51999-10-23 00:58:54 +000059 dir->name_len = 1 | filetype;
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 dir->name[0] = '.';
Theodore Ts'oe6198e51999-10-23 00:58:54 +000061 rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1);
62 dir->rec_len = EXT2_DIR_REC_LEN(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000063
64 /*
65 * Set up entry for '..'
66 */
67 dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
68 dir->rec_len = rec_len;
69 dir->inode = parent_ino;
Theodore Ts'oe6198e51999-10-23 00:58:54 +000070 dir->name_len = 2 | filetype;
Theodore Ts'o3839e651997-04-26 13:21:57 +000071 dir->name[0] = '.';
72 dir->name[1] = '.';
73
74 }
75 *block = buf;
76 return 0;
77}