linux-erofs.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Hu Weiwen <sehuww@mail.scut.edu.cn>
To: hsiangkao@redhat.com, linux-erofs@lists.ozlabs.org
Subject: [PATCH] erofs-utils: add command line argument to override uid/gid
Date: Thu,  1 Apr 2021 13:29:03 +0800	[thread overview]
Message-ID: <20210401052903.18700-1-sehuww@mail.scut.edu.cn> (raw)

Also added '--all-root' option to set uid and gid to root conveniently.

This function can be useful if we want to pack some data owned by user with
large uid, but we want to use compact inode.

Signed-off-by: Hu Weiwen <sehuww@mail.scut.edu.cn>
---
 include/erofs/config.h |  2 ++
 lib/config.c           |  2 ++
 lib/inode.c            |  4 ++--
 mkfs/main.c            | 23 ++++++++++++++++++++++-
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/include/erofs/config.h b/include/erofs/config.h
index 02ddf59..e6eaef6 100644
--- a/include/erofs/config.h
+++ b/include/erofs/config.h
@@ -54,6 +54,8 @@ struct erofs_configure {
 	/* < 0, xattr disabled and INT_MAX, always use inline xattrs */
 	int c_inline_xattr_tolerance;
 	u64 c_unix_timestamp;
+	u32 c_uid;
+	u32 c_gid;
 #ifdef WITH_ANDROID
 	char *mount_point;
 	char *target_out_path;
diff --git a/lib/config.c b/lib/config.c
index 3ecd481..b8df239 100644
--- a/lib/config.c
+++ b/lib/config.c
@@ -24,6 +24,8 @@ void erofs_init_configure(void)
 	cfg.c_force_inodeversion = 0;
 	cfg.c_inline_xattr_tolerance = 2;
 	cfg.c_unix_timestamp = -1;
+	cfg.c_uid = -1;
+	cfg.c_gid = -1;
 }
 
 void erofs_show_config(void)
diff --git a/lib/inode.c b/lib/inode.c
index 40189fe..d52facf 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -752,8 +752,8 @@ int erofs_fill_inode(struct erofs_inode *inode,
 	if (err)
 		return err;
 	inode->i_mode = st->st_mode;
-	inode->i_uid = st->st_uid;
-	inode->i_gid = st->st_gid;
+	inode->i_uid = cfg.c_uid == -1 ? st->st_uid : cfg.c_uid;
+	inode->i_gid = cfg.c_gid == -1 ? st->st_gid : cfg.c_gid;
 	inode->i_ctime = st->st_ctime;
 	inode->i_ctime_nsec = st->st_ctim.tv_nsec;
 
diff --git a/mkfs/main.c b/mkfs/main.c
index d9c4c7f..49b94b4 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -36,6 +36,7 @@ static struct option long_options[] = {
 #ifdef HAVE_LIBSELINUX
 	{"file-contexts", required_argument, NULL, 4},
 #endif
+	{"all-root", no_argument, NULL, 5},
 #ifdef WITH_ANDROID
 	{"mount-point", required_argument, NULL, 10},
 	{"product-out", required_argument, NULL, 11},
@@ -74,6 +75,9 @@ static void usage(void)
 #ifdef HAVE_LIBSELINUX
 	      " --file-contexts=X  specify a file contexts file to setup selinux labels\n"
 #endif
+	      " --all-root         make all files owned by root\n"
+	      " -u#                set all file uids to #\n"
+	      " -g#                set all file gids to #\n"
 	      " --help             display this help and exit\n"
 #ifdef WITH_ANDROID
 	      "\nwith following android-specific options:\n"
@@ -152,7 +156,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 	char *endptr;
 	int opt, i;
 
-	while((opt = getopt_long(argc, argv, "d:x:z:E:T:U:",
+	while ((opt = getopt_long(argc, argv, "d:x:z:E:T:U:u:g:",
 				 long_options, NULL)) != -1) {
 		switch (opt) {
 		case 'z':
@@ -203,6 +207,20 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			}
 			cfg.c_timeinherit = TIMESTAMP_FIXED;
 			break;
+		case 'u':
+			cfg.c_uid = strtoul(optarg, &endptr, 0);
+			if (cfg.c_uid == -1 || *endptr != '\0') {
+				erofs_err("invalid uid %s", optarg);
+				return -EINVAL;
+			}
+			break;
+		case 'g':
+			cfg.c_gid = strtoul(optarg, &endptr, 0);
+			if (cfg.c_gid == -1 || *endptr != '\0') {
+				erofs_err("invalid gid %s", optarg);
+				return -EINVAL;
+			}
+			break;
 #ifdef HAVE_LIBUUID
 		case 'U':
 			if (uuid_parse(optarg, sbi.uuid)) {
@@ -233,6 +251,9 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			if (opt && opt != -EBUSY)
 				return opt;
 			break;
+		case 5:
+			cfg.c_uid = cfg.c_gid = 0;
+			break;
 #ifdef WITH_ANDROID
 		case 10:
 			cfg.mount_point = optarg;
-- 
2.25.1


             reply	other threads:[~2021-04-01  5:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01  5:29 Hu Weiwen [this message]
2021-04-01  6:01 ` [PATCH] erofs-utils: add command line argument to override uid/gid Gao Xiang
2021-04-01 11:36   ` [PATCH v2] erofs-utils: add cmd " Hu Weiwen
2021-04-01 12:01     ` Gao Xiang
2021-04-11 13:50       ` Li GuiFu via Linux-erofs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210401052903.18700-1-sehuww@mail.scut.edu.cn \
    --to=sehuww@mail.scut.edu.cn \
    --cc=hsiangkao@redhat.com \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).