Skip to contentSkip to conversation

tar-tree: add the "tar.applyUmask" config option

PATCH In review Willy Tarreau 5 participants 9 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tar-tree: add the "tar.applyUmask" config option

Hi Junio, Hi Rene,

While I agreed with Linus that the very permissive file modes set in tar archives were not particularly a problem for kernel users, I'm finding that for some other projects it sometimes becomes really annoying, to the point that I finally considered using a plain tar instead. This is a shame because tar-tree is really fast an powerful, and I like its ability to enforce permissions when those of the local dir might be wrong for various reasons.

So I added a config option to tell tar-tree to respect the user's umask to the files and dirs listed in the output tar file. By default, this option is not set, of course, but once set to true, the files look somewhat better :

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true

[tar]
applyUmask = true

$ ./git-tar-tree HEAD | tar tvf -|head
-rw-r--r-- git/git        1887 2006-07-19 23:23:00 .gitignore
-rw-r--r-- git/git       18787 2006-07-19 23:23:00 COPYING
drwxr-xr-x git/git           0 2006-07-19 23:23:00 Documentation/
-rw-r--r-- git/git          52 2006-07-19 23:23:00 Documentation/.gitignore
-rw-r--r-- git/git        2463 2006-07-19 23:23:00 Documentation/Makefile
-rw-r--r-- git/git       11550 2006-07-19 23:23:00 Documentation/SubmittingPatches
-rw-r--r-- git/git         822 2006-07-19 23:23:00 Documentation/asciidoc.conf
-rwxr-xr-x git/git        1049 2006-07-19 23:23:00 Documentation/build-docdep.perl
-rw-r--r-- git/git         596 2006-07-19 23:23:00 Documentation/callouts.xsl

Let me insist on the fact that the default behaviour is unchanged. Would you consider this for inclusion ? I've made the patch below against <master>. Please check on your side that the doc generates valid output, as I've never managed to install the asciidoc/xmlto toolchain.

Please keep me CCed in replies as I'm not subscribed to the git list.

Regards,
Willy

--

Quoted 1 line

From dcc976d83c3b9c85460329932ce22547b7f5f3f9 Mon Sep 17 00:00:00 2001

From: Willy Tarreau <[email protected]>
Date: Wed, 19 Jul 2006 23:23:00 +0200
Subject: tar-tree: add the "tar.applyUmask" config option

For some projects, producing tar files with world-writable files can be problematic. This change introduces a the "tar.applyUmask" config option to make tar-tree apply the umask to the permissions set in the tar file.

---
 Documentation/config.txt |    9 +++++++++
 builtin-tar-tree.c       |   19 ++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)
Documentation/config.txt +9 -0
index 0b434c1..32519a9 100644
@@ -208,6 +208,15 @@ showbranch.default::
 	The default set of branches for gitlink:git-show-branch[1].
 	See gitlink:git-show-branch[1].
 
+tar.applyUmask::
+	By default, git-link:git-tar-tree[1] sets file and directories modes
+	to 0666 or 0777. While this is both useful and acceptable for projects
+	such as the Linux Kernel, it might be excessive for other projects.
+	Setting this variable to true makes git-link:git-tar-tree[1] apply the
+	umask to the modes above. This should be enough for most projects, as
+	it will lead to the same permissions as git-link:git-checkout[1] would
+	use. The default is false.
+
 user.email::
 	Your email address to be recorded in any newly created commits.
 	Can be overridden by the 'GIT_AUTHOR_EMAIL' and 'GIT_COMMITTER_EMAIL'
builtin-tar-tree.c +16 -3
index f2e48aa..f66f6ad 100644
@@ -20,6 +20,7 @@ static char block[BLOCKSIZE];
 static unsigned long offset;
 
 static time_t archive_time;
+static int tar_umask;
 
 /* tries hard to write, either succeeds or dies in the attempt */
 static void reliable_write(const void *data, unsigned long size)
@@ -188,13 +189,13 @@ static void write_entry(const unsigned c
 	} else {
 		if (S_ISDIR(mode)) {
 			*header.typeflag = TYPEFLAG_DIR;
-			mode |= 0777;
+			mode |= 0777 & ~tar_umask;
 		} else if (S_ISLNK(mode)) {
 			*header.typeflag = TYPEFLAG_LNK;
 			mode |= 0777;
 		} else if (S_ISREG(mode)) {
 			*header.typeflag = TYPEFLAG_REG;
-			mode |= (mode & 0100) ? 0777 : 0666;
+			mode |= ((mode & 0100) ? 0777 : 0666) &	~tar_umask;
 		} else {
 			error("unsupported file mode: 0%o (SHA1: %s)",
 			      mode, sha1_to_hex(sha1));
@@ -293,6 +294,18 @@ static void traverse_tree(struct tree_de
 	}
 }
 
+int git_tar_config(const char *var, const char *value)
+{
+	if (!strcmp(var, "tar.applyumask")) {
+		if (git_config_bool(var, value)) {
+			tar_umask = umask(0);
+			umask(tar_umask);
+		}
+		return 0;
+	}
+	return git_default_config(var, value);
+}
+
 static int generate_tar(int argc, const char **argv, char** envp)
 {
 	unsigned char sha1[20], tree_sha1[20];
@@ -305,7 +318,7 @@ static int generate_tar(int argc, const 
 	current_path.len = current_path.eof = 0;
 
 	setup_git_directory();
-	git_config(git_default_config);
+	git_config(git_tar_config);
 
 	switch (argc) {
 	case 3:
Signature · 1 line
1.4.1
Acked-by on v1 1/1
#1 lore raw

Hi Willy,

I kind of like the change, because it gives the user more control. Theoretically this would not be needed, because tar can apply the umask at extract time just fine. In practice I can't see why we should forbid users from creating the archives just the way they like them, no matter what their reasoning might be.

Documentation/config.txt showbranch.default:: · line 218

Quoted 8 lines · Willy Tarreau
Documentation/config.txt +8 -0
+tar.applyUmask::
+	By default, git-link:git-tar-tree[1] sets file and directories modes
+	to 0666 or 0777. While this is both useful and acceptable for projects
+	such as the Linux Kernel, it might be excessive for other projects.
+	Setting this variable to true makes git-link:git-tar-tree[1] apply the
+	umask to the modes above. This should be enough for most projects, as
+	it will lead to the same permissions as git-link:git-checkout[1] would
+	use. The default is false.

Comments about why this change is needed and that it is sufficient should go into the commit message. For the rest of the patch:

#3 lore raw
In reply to Willy Tarreau
Quoted 7 lines · Willy Tarreau

While I agreed with Linus that the very permissive file modes set in tar archives were not particularly a problem for kernel users, I'm finding that for some other projects it sometimes becomes really annoying, to the point that I finally considered using a plain tar instead. This is a shame because tar-tree is really fast an powerful, and I like its ability to enforce permissions when those of the local dir might be wrong for various reasons.

I do not have problem with an option to allow a non-default behaviour in this area. Maybe we might want to be able to set the mask in the configuration file as well, perhaps like...

tar.umask = user ;# use from the current process'
tar.umask = 0 ;# same as default
tar.umask = 002 ;# group friendly

#4 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Willy Tarreau

While I agreed with Linus that the very permissive file modes set in tar archives were not particularly a problem for kernel users, I'm finding that for some other projects it sometimes becomes really annoying, to the point that I finally considered using a plain tar instead. This is a shame because tar-tree is really fast an powerful, and I like its ability to enforce permissions when those of the local dir might be wrong for various reasons.

Quoted 7 lines

I do not have problem with an option to allow a non-default behaviour in this area. Maybe we might want to be able to set the mask in the configuration file as well, perhaps like...

tar.umask = user ;# use from the current process'
tar.umask = 0 ;# same as default
tar.umask = 002 ;# group friendly

This is an excellent idea. I will try to find some spare time tomorrow to implement it. I've also seen the proposal about the --umask= option. I don't think it's absolutely necessary since the umask has little reason to change during the repo's life, but if the implementation is obvious, I will do it too.

Thanks for your suggestion,
Willy

#5 lore raw
In reply to Junio C Hamano

Hi Junio,

Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Willy Tarreau

While I agreed with Linus that the very permissive file modes set in tar archives were not particularly a problem for kernel users, I'm finding that for some other projects it sometimes becomes really annoying, to the point that I finally considered using a plain tar instead. This is a shame because tar-tree is really fast an powerful, and I like its ability to enforce permissions when those of the local dir might be wrong for various reasons.

Quoted 7 lines

I do not have problem with an option to allow a non-default behaviour in this area. Maybe we might want to be able to set the mask in the configuration file as well, perhaps like...

tar.umask = user ;# use from the current process'
tar.umask = 0 ;# same as default
tar.umask = 002 ;# group friendly

Here's the new version following your suggestion above. I really liked it. I've also added a configuration example in the doc. Once again, please ensure that the doc generates correctly.

Thanks,
Willy

Quoted 1 line

From d8a0d2bbd2365b719a7f68edd78c77a0fd903cab Mon Sep 17 00:00:00 2001

From: Willy Tarreau <[email protected]>
Date: Thu, 20 Jul 2006 11:23:40 +0200
Subject: tar-tree: add the "tar.umask" config option

By default, git-tar-tree(1) sets file and directories modes to 0666 or 0777. While this is both useful and acceptable for projects such as the Linux Kernel, it might be excessive for other projects. With this variable, it becomes possible to tell git-tar-tree(1) to apply a specific umask to the modes above. The special value "user" indicates that the user's current umask will be used. This should be enough for most projects, as it will lead to the same permissions as git-checkout(1) would use. The default value remains 0, which means world read-write.

---
 Documentation/config.txt       |   11 +++++++++++
 Documentation/git-tar-tree.txt |   15 ++++++++++++++-
 builtin-tar-tree.c             |   21 ++++++++++++++++++---
 3 files changed, 43 insertions(+), 4 deletions(-)
Documentation/config.txt +11 -0
index 0b434c1..f4985d4 100644
@@ -208,6 +208,17 @@ showbranch.default::
 	The default set of branches for gitlink:git-show-branch[1].
 	See gitlink:git-show-branch[1].
 
+tar.umask::
+	By default, git-link:git-tar-tree[1] sets file and directories modes
+	to 0666 or 0777. While this is both useful and acceptable for projects
+	such as the Linux Kernel, it might be excessive for other projects.
+	With this variable, it becomes possible to tell
+	git-link:git-tar-tree[1] to apply a specific umask to the modes above.
+	The special value "user" indicates that the user's current umask will
+	be used. This should be enough for most projects, as it will lead to
+	the same permissions as git-link:git-checkout[1] would use. The default
+	value remains 0, which means world read-write.
+
 user.email::
 	Your email address to be recorded in any newly created commits.
 	Can be overridden by the 'GIT_AUTHOR_EMAIL' and 'GIT_COMMITTER_EMAIL'
Documentation/git-tar-tree.txt +14 -1
index f2675c4..7a99acf 100644
@@ -37,7 +37,20 @@ OPTIONS
 	Instead of making a tar archive from local repository,
 	retrieve a tar archive from a remote repository.
 
-Examples
+CONFIGURATION
+-------------
+By default, file and directories modes are set to 0666 or 0777. It is
+possible to change this by setting the "umask" variable in the
+repository configuration as follows :
+
+[tar]
+        umask = 002	;# group friendly
+
+The special umask value "user" indicates that the user's current umask
+will be used instead. The default value remains 0, which means world
+readable/writable files and directories.
+
+EXAMPLES
 --------
 git tar-tree HEAD junk | (cd /var/tmp/ && tar xf -)::
 
builtin-tar-tree.c +18 -3
index f2e48aa..e5aaded 100644
@@ -20,6 +20,7 @@ static char block[BLOCKSIZE];
 static unsigned long offset;
 
 static time_t archive_time;
+static int tar_umask;
 
 /* tries hard to write, either succeeds or dies in the attempt */
 static void reliable_write(const void *data, unsigned long size)
@@ -188,13 +189,13 @@ static void write_entry(const unsigned c
 	} else {
 		if (S_ISDIR(mode)) {
 			*header.typeflag = TYPEFLAG_DIR;
-			mode |= 0777;
+			mode = (mode | 0777) & ~tar_umask;
 		} else if (S_ISLNK(mode)) {
 			*header.typeflag = TYPEFLAG_LNK;
 			mode |= 0777;
 		} else if (S_ISREG(mode)) {
 			*header.typeflag = TYPEFLAG_REG;
-			mode |= (mode & 0100) ? 0777 : 0666;
+			mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
 		} else {
 			error("unsupported file mode: 0%o (SHA1: %s)",
 			      mode, sha1_to_hex(sha1));
@@ -293,6 +294,20 @@ static void traverse_tree(struct tree_de
 	}
 }
 
+int git_tar_config(const char *var, const char *value)
+{
+	if (!strcmp(var, "tar.umask")) {
+		if (!strcmp(value, "user")) {
+			tar_umask = umask(0);
+			umask(tar_umask);
+		} else {
+			tar_umask = git_config_int(var, value);
+		}
+		return 0;
+	}
+	return git_default_config(var, value);
+}
+
 static int generate_tar(int argc, const char **argv, char** envp)
 {
 	unsigned char sha1[20], tree_sha1[20];
@@ -305,7 +320,7 @@ static int generate_tar(int argc, const 
 	current_path.len = current_path.eof = 0;
 
 	setup_git_directory();
-	git_config(git_default_config);
+	git_config(git_tar_config);
 
 	switch (argc) {
 	case 3:
Signature · 1 line
1.4.1
#6 lore raw

builtin-tar-tree.c static void traverse_tree(struct tree_de · line 304

Quoted 8 lines · Willy Tarreau
builtin-tar-tree.c +8 -0
+int git_tar_config(const char *var, const char *value)
+{
+	if (!strcmp(var, "tar.umask")) {
+		if (!strcmp(value, "user")) {
+			tar_umask = umask(0);
+			umask(tar_umask);
+		} else {
+			tar_umask = git_config_int(var, value);

Looks like you forgot:
umask(tar_umask);

builtin-tar-tree.c static void traverse_tree(struct tree_de · line 309

Quoted 5 lines
builtin-tar-tree.c +5 -0
+		}
+		return 0;
+	}
+	return git_default_config(var, value);
+}

Or else move it to just before the "return 0;" line.

Rogan

#7 lore raw
Quoted 11 lines · Rogan Dawes

builtin-tar-tree.c static void traverse_tree(struct tree_de · line 304

Quoted 8 lines · Willy Tarreau
builtin-tar-tree.c +8 -0
+int git_tar_config(const char *var, const char *value)
+{
+	if (!strcmp(var, "tar.umask")) {
+		if (!strcmp(value, "user")) {
+			tar_umask = umask(0);
+			umask(tar_umask);
+		} else {
+			tar_umask = git_config_int(var, value);

Looks like you forgot:

Quoted 1 line
   			umask(tar_umask);

not at all : we don't want to change the process's umask, but set the mask that will be used to position file modes in the output archive.

The reason for umask(tar_umask) above is because you cannot read the process umask without changing it, so you have to do it twice with a dummy value first.

Quoted 9 lines

builtin-tar-tree.c static void traverse_tree(struct tree_de · line 309

Quoted 5 lines
builtin-tar-tree.c +5 -0
+		}
+		return 0;
+	}
+	return git_default_config(var, value);
+}

Or else move it to just before the "return 0;" line.

Rogan

Regards,
Willy