From 06d2777426e16ae9c354f1c14b779d666492802c Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sun, 25 May 2025 12:51:36 -0400 Subject: [PATCH] mke2fs: propagate some chattr flags into the fs image when using mke2fs -d When copying files from a source directory, propagate chattr flags such as the immutable, append-only, nodump, etc. into the files in the destination file system. Flags in directory inodes are also propagated. Signed-off-by: Theodore Ts'o --- debugfs/debugfs.c | 2 +- misc/create_inode.c | 21 +++++++++++++++------ misc/create_inode.h | 3 ++- misc/create_inode_libarchive.c | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c index 87ae7463..b9f248be 100644 --- a/debugfs/debugfs.c +++ b/debugfs/debugfs.c @@ -1834,7 +1834,7 @@ void do_mkdir(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)), "", CHECK_FS_RW)) return; - retval = do_mkdir_internal(current_fs, cwd, argv[1], root); + retval = do_mkdir_internal(current_fs, cwd, argv[1], 0, root); if (retval) com_err(argv[0], retval, 0); diff --git a/misc/create_inode.c b/misc/create_inode.c index 4fc7714b..e7917b0e 100644 --- a/misc/create_inode.c +++ b/misc/create_inode.c @@ -35,9 +35,10 @@ #include #endif -#include -#include -#include +#include "ext2fs/ext2_fs.h" +#include "ext2fs/ext2fs.h" +#include "ext2fs/ext2_types.h" +#include "ext2fs/fiemap.h" #include "create_inode.h" #include "support/nls-enable.h" @@ -49,6 +50,13 @@ int link_append_flag = EXT2FS_LINK_EXPAND; +#define COPY_FLAGS_MASK (EXT2_SECRM_FL | EXT2_UNRM_FL | EXT2_COMPR_FL | \ + EXT2_SYNC_FL | EXT2_IMMUTABLE_FL | EXT2_APPEND_FL | \ + EXT2_NODUMP_FL | EXT2_NOATIME_FL | \ + EXT3_JOURNAL_DATA_FL | EXT2_NOTAIL_FL | \ + EXT2_DIRSYNC_FL | EXT2_TOPDIR_FL | FS_DAX_FL | \ + EXT4_PROJINHERIT_FL | EXT4_CASEFOLD_FL) + static int ext2_file_type(unsigned int mode) { if (LINUX_S_ISREG(mode)) @@ -368,7 +376,7 @@ errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, /* Make a directory in the fs */ errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, - ext2_ino_t root) + unsigned long flags, ext2_ino_t root) { char *cp; ext2_ino_t parent_ino; @@ -388,7 +396,7 @@ errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, } else parent_ino = cwd; - retval = ext2fs_mkdir2(fs, parent_ino, 0, 0, + retval = ext2fs_mkdir2(fs, parent_ino, 0, (flags & COPY_FLAGS_MASK), link_append_flag, name, NULL); if (retval) com_err("ext2fs_mkdir2", retval, @@ -830,6 +838,7 @@ errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src, goto out; ext2fs_extent_free(handle); } + inode.i_flags |= (flags & COPY_FLAGS_MASK); retval = ext2fs_write_new_inode(fs, newfile, &inode); if (retval) @@ -1076,7 +1085,7 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino, strcmp(name, "lost+found") == 0) goto find_lnf; retval = do_mkdir_internal(fs, parent_ino, name, - root); + fl, root); if (retval) { com_err(__func__, retval, _("while making dir \"%s\""), name); diff --git a/misc/create_inode.h b/misc/create_inode.h index 4472162e..b6f1d9eb 100644 --- a/misc/create_inode.h +++ b/misc/create_inode.h @@ -60,7 +60,8 @@ extern errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, char *target, ext2_ino_t root); extern errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, - const char *name, ext2_ino_t root); + const char *name, unsigned long flags, + ext2_ino_t root); extern errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src, const char *dest, unsigned long flags, ext2_ino_t root); diff --git a/misc/create_inode_libarchive.c b/misc/create_inode_libarchive.c index 118bf4f8..7745c038 100644 --- a/misc/create_inode_libarchive.c +++ b/misc/create_inode_libarchive.c @@ -535,7 +535,7 @@ static errcode_t handle_entry(ext2_filsys fs, ext2_ino_t root_ino, } break; case S_IFDIR: - retval = do_mkdir_internal(fs, dirinode, name, root); + retval = do_mkdir_internal(fs, dirinode, name, 0, root); if (retval) { com_err(__func__, retval, _("while making dir \"%s\""), name); -- 2.47.2