Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / mkdir.c
index 8bcb542..98c5ccf 100644 (file)
@@ -1,28 +1,45 @@
 /*
  * mkdir.c --- make a directory in the filesystem
  * 
- * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
- * under the terms of the GNU Public License.
+ * Copyright (C) 1994, 1995 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
  */
 
 #include <stdio.h>
 #include <string.h>
+#if HAVE_UNISTD_H
 #include <unistd.h>
-#include <stdlib.h>
+#endif
 #include <fcntl.h>
 #include <time.h>
+#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+#if HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
 
+#if EXT2_FLAT_INCLUDES
+#include "ext2_fs.h"
+#else
 #include <linux/ext2_fs.h>
+#endif
 
 #include "ext2fs.h"
 
+#ifndef EXT2_FT_DIR
+#define EXT2_FT_DIR            2
+#endif
+
 errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
                       const char *name)
 {
        errcode_t               retval;
-       struct ext2_inode       inode;
+       struct ext2_inode       parent_inode, inode;
        ino_t                   ino = inum;
        ino_t                   scratch_ino;
        blk_t                   blk;
@@ -35,7 +52,8 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
         * Allocate an inode, if necessary
         */
        if (!ino) {
-               retval = ext2fs_new_inode(fs, parent, S_IFDIR | 0755, 0, &ino);
+               retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
+                                         0, &ino);
                if (retval)
                        goto cleanup;
        }
@@ -55,10 +73,20 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
                goto cleanup;
 
        /*
+        * Get the parent's inode, if necessary
+        */
+       if (parent != ino) {
+               retval = ext2fs_read_inode(fs, parent, &parent_inode);
+               if (retval)
+                       goto cleanup;
+       } else
+               memset(&parent_inode, 0, sizeof(parent_inode));
+
+       /*
         * Create the inode structure....
         */
        memset(&inode, 0, sizeof(struct ext2_inode));
-       inode.i_mode = S_IFDIR | 0755;
+       inode.i_mode = LINUX_S_IFDIR | 0755;
        inode.i_uid = inode.i_gid = 0;
        inode.i_blocks = fs->blocksize / 512;
        inode.i_block[0] = blk;
@@ -69,7 +97,7 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
        /*
         * Write out the inode and inode data block
         */
-       retval = io_channel_write_blk(fs->io, blk, 1, block);
+       retval = ext2fs_write_dir_block(fs, blk, block);
        if (retval)
                goto cleanup;
        retval = ext2fs_write_inode(fs, ino, &inode); 
@@ -77,37 +105,34 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
                goto cleanup;
 
        /*
-        * Update parent inode's counts
-        */
-       if (parent != ino) {
-               retval = ext2fs_read_inode(fs, parent, &inode);
-               if (retval)
-                       goto cleanup;
-               inode.i_links_count++;
-               retval = ext2fs_write_inode(fs, parent, &inode);
-               if (retval)
-                       goto cleanup;
-       }
-       
-       /*
         * Link the directory into the filesystem hierarchy
         */
        if (name) {
                retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
                                       &scratch_ino);
                if (!retval) {
-                       retval = EEXIST;
+                       retval = EXT2_ET_DIR_EXISTS;
                        name = 0;
                        goto cleanup;
                }
-               if (retval != ENOENT)
+               if (retval != EXT2_ET_FILE_NOT_FOUND)
                        goto cleanup;
-               retval = ext2fs_link(fs, parent, name, ino, 0);
+               retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
                if (retval)
                        goto cleanup;
        }
 
        /*
+        * Update parent inode's counts
+        */
+       if (parent != ino) {
+               parent_inode.i_links_count++;
+               retval = ext2fs_write_inode(fs, parent, &parent_inode);
+               if (retval)
+                       goto cleanup;
+       }
+       
+       /*
         * Update accounting....
         */
        ext2fs_mark_block_bitmap(fs->block_map, blk);
@@ -126,7 +151,7 @@ errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
        
 cleanup:
        if (block)
-               free(block);
+               ext2fs_free_mem((void **) &block);
        return retval;
 
 }