blob: 4fa711d01af87ebe2064200286069a11752b2f2a [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mkdir.c --- make a directory in the filesystem
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'o3839e651997-04-26 13:21:57 +000017#include <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include "ext2fs.h"
28
Theodore Ts'oe6198e51999-10-23 00:58:54 +000029#ifndef EXT2_FT_DIR
30#define EXT2_FT_DIR 2
31#endif
32
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000033errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
Theodore Ts'o3839e651997-04-26 13:21:57 +000034 const char *name)
35{
36 errcode_t retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000037 struct ext2_inode parent_inode, inode;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000038 ext2_ino_t ino = inum;
39 ext2_ino_t scratch_ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000040 blk_t blk;
41 char *block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042
Theodore Ts'of3db3561997-04-26 13:34:30 +000043 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44
Theodore Ts'o3839e651997-04-26 13:21:57 +000045 /*
46 * Allocate an inode, if necessary
47 */
48 if (!ino) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000049 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
50 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000051 if (retval)
52 goto cleanup;
53 }
54
55 /*
56 * Allocate a data block for the directory
57 */
58 retval = ext2fs_new_block(fs, 0, 0, &blk);
59 if (retval)
60 goto cleanup;
61
62 /*
63 * Create a scratch template for the directory
64 */
65 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
66 if (retval)
67 goto cleanup;
68
69 /*
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000070 * Get the parent's inode, if necessary
71 */
72 if (parent != ino) {
73 retval = ext2fs_read_inode(fs, parent, &parent_inode);
74 if (retval)
75 goto cleanup;
76 } else
77 memset(&parent_inode, 0, sizeof(parent_inode));
78
79 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 * Create the inode structure....
81 */
82 memset(&inode, 0, sizeof(struct ext2_inode));
Theodore Ts'o6a525062001-12-24 09:40:00 -050083 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 inode.i_uid = inode.i_gid = 0;
85 inode.i_blocks = fs->blocksize / 512;
86 inode.i_block[0] = blk;
87 inode.i_links_count = 2;
88 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
89 inode.i_size = fs->blocksize;
90
91 /*
92 * Write out the inode and inode data block
93 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000094 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 if (retval)
96 goto cleanup;
97 retval = ext2fs_write_inode(fs, ino, &inode);
98 if (retval)
99 goto cleanup;
100
101 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 * Link the directory into the filesystem hierarchy
103 */
104 if (name) {
105 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
106 &scratch_ino);
107 if (!retval) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000108 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 name = 0;
110 goto cleanup;
111 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000112 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113 goto cleanup;
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000114 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 if (retval)
116 goto cleanup;
117 }
118
119 /*
Theodore Ts'odab278a1999-11-19 18:49:27 +0000120 * Update parent inode's counts
121 */
122 if (parent != ino) {
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000123 parent_inode.i_links_count++;
124 retval = ext2fs_write_inode(fs, parent, &parent_inode);
Theodore Ts'odab278a1999-11-19 18:49:27 +0000125 if (retval)
126 goto cleanup;
127 }
128
129 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130 * Update accounting....
131 */
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500132 ext2fs_block_alloc_stats(fs, blk, +1);
133 ext2fs_inode_alloc_stats(fs, ino, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135cleanup:
136 if (block)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000137 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 return retval;
139
140}
141
142