Whamcloud - gitweb
libext2fs: provide functions to safely access name_len and file_type
[tools/e2fsprogs.git] / lib / ext2fs / newdir.c
index c2ca903..d134bdf 100644 (file)
@@ -1,14 +1,15 @@
 /*
  * newdir.c --- create a new directory block
- * 
+ *
  * Copyright (C) 1994, 1995 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -33,6 +34,8 @@ errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
        char                    *buf;
        int                     rec_len;
        int                     filetype = 0;
+       struct ext2_dir_entry_tail      *t;
+       int                     csum_size = 0;
 
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
@@ -41,31 +44,47 @@ errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
                return retval;
        memset(buf, 0, fs->blocksize);
        dir = (struct ext2_dir_entry *) buf;
-       dir->rec_len = fs->blocksize;
+
+       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+                                      EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+               csum_size = sizeof(struct ext2_dir_entry_tail);
+
+       retval = ext2fs_set_rec_len(fs, fs->blocksize - csum_size, dir);
+       if (retval)
+               return retval;
 
        if (dir_ino) {
                if (fs->super->s_feature_incompat &
                    EXT2_FEATURE_INCOMPAT_FILETYPE)
-                       filetype = EXT2_FT_DIR << 8;
+                       filetype = EXT2_FT_DIR;
                /*
                 * Set up entry for '.'
                 */
                dir->inode = dir_ino;
-               dir->name_len = 1 | filetype;
+               ext2fs_dirent_set_name_len(dir, 1);
+               ext2fs_dirent_set_file_type(dir, filetype);
                dir->name[0] = '.';
-               rec_len = fs->blocksize - EXT2_DIR_REC_LEN(1);
+               rec_len = (fs->blocksize - csum_size) - EXT2_DIR_REC_LEN(1);
                dir->rec_len = EXT2_DIR_REC_LEN(1);
 
                /*
                 * Set up entry for '..'
                 */
                dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
-               dir->rec_len = rec_len;
+               retval = ext2fs_set_rec_len(fs, rec_len, dir);
+               if (retval)
+                       return retval;
                dir->inode = parent_ino;
-               dir->name_len = 2 | filetype;
+               ext2fs_dirent_set_name_len(dir, 2);
+               ext2fs_dirent_set_file_type(dir, filetype);
                dir->name[0] = '.';
                dir->name[1] = '.';
-               
+
+       }
+
+       if (csum_size) {
+               t = EXT2_DIRENT_TAIL(buf, fs->blocksize);
+               ext2fs_initialize_dirent_tail(fs, t);
        }
        *block = buf;
        return 0;