]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/mkdir.c
Many files:
[thirdparty/e2fsprogs.git] / lib / ext2fs / mkdir.c
1 /*
2 * mkdir.c --- make a directory in the filesystem
3 *
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <time.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16
17 #include <linux/ext2_fs.h>
18
19 #include "ext2fs.h"
20
21 errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
22 const char *name)
23 {
24 errcode_t retval;
25 struct ext2_inode inode;
26 ino_t ino = inum;
27 ino_t scratch_ino;
28 blk_t blk;
29 char *block = 0;
30 int group;
31
32 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
33
34 /*
35 * Allocate an inode, if necessary
36 */
37 if (!ino) {
38 retval = ext2fs_new_inode(fs, parent, S_IFDIR | 0755, 0, &ino);
39 if (retval)
40 goto cleanup;
41 }
42
43 /*
44 * Allocate a data block for the directory
45 */
46 retval = ext2fs_new_block(fs, 0, 0, &blk);
47 if (retval)
48 goto cleanup;
49
50 /*
51 * Create a scratch template for the directory
52 */
53 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
54 if (retval)
55 goto cleanup;
56
57 /*
58 * Create the inode structure....
59 */
60 memset(&inode, 0, sizeof(struct ext2_inode));
61 inode.i_mode = S_IFDIR | 0755;
62 inode.i_uid = inode.i_gid = 0;
63 inode.i_blocks = fs->blocksize / 512;
64 inode.i_block[0] = blk;
65 inode.i_links_count = 2;
66 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
67 inode.i_size = fs->blocksize;
68
69 /*
70 * Write out the inode and inode data block
71 */
72 retval = io_channel_write_blk(fs->io, blk, 1, block);
73 if (retval)
74 goto cleanup;
75 retval = ext2fs_write_inode(fs, ino, &inode);
76 if (retval)
77 goto cleanup;
78
79 /*
80 * Update parent inode's counts
81 */
82 if (parent != ino) {
83 retval = ext2fs_read_inode(fs, parent, &inode);
84 if (retval)
85 goto cleanup;
86 inode.i_links_count++;
87 retval = ext2fs_write_inode(fs, parent, &inode);
88 if (retval)
89 goto cleanup;
90 }
91
92 /*
93 * Link the directory into the filesystem hierarchy
94 */
95 if (name) {
96 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
97 &scratch_ino);
98 if (!retval) {
99 retval = EEXIST;
100 name = 0;
101 goto cleanup;
102 }
103 if (retval != ENOENT)
104 goto cleanup;
105 retval = ext2fs_link(fs, parent, name, ino, 0);
106 if (retval)
107 goto cleanup;
108 }
109
110 /*
111 * Update accounting....
112 */
113 ext2fs_mark_block_bitmap(fs->block_map, blk);
114 ext2fs_mark_bb_dirty(fs);
115 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
116 ext2fs_mark_ib_dirty(fs);
117
118 group = ext2fs_group_of_blk(fs, blk);
119 fs->group_desc[group].bg_free_blocks_count--;
120 group = ext2fs_group_of_ino(fs, ino);
121 fs->group_desc[group].bg_free_inodes_count--;
122 fs->group_desc[group].bg_used_dirs_count++;
123 fs->super->s_free_blocks_count--;
124 fs->super->s_free_inodes_count--;
125 ext2fs_mark_super_dirty(fs);
126
127 cleanup:
128 if (block)
129 free(block);
130 return retval;
131
132 }
133
134