threads / patch / 4921

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

Subject: [PATCH] tar-tree: add the "tar.applyUmask" config option

## tl;dr

9 messages between Jul 19, 2006 and Jul 24, 2006. Diffs are folded; open one to read it.

replies: 8people: 5as markdown or json

Willy Tarreau· Jul 19, 2006, 21:40 UTC · lore
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

--
>From dcc976d83c3b9c85460329932ce22547b7f5f3f9 Mon Sep 17 00:00:00 2001
From: Willy Tarreau <w@1wt.eu>
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.

Signed-off-by: Willy Tarreau <w@1wt.eu>
---
 Documentation/config.txt |    9 +++++++++
 builtin-tar-tree.c       |   19 ++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)
Show changes to 2 files +25 −3

Documentation/config.txt, builtin-tar-tree.c

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 0b434c1..32519a9 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -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'
diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
index f2e48aa..f66f6ad 100644
--- a/builtin-tar-tree.c
+++ b/builtin-tar-tree.c
@@ -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:
-- 
1.4.1
Junio C Hamano· Jul 19, 2006, 22:33 UTC · re: Willy Tarreau · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

Willy Tarreau <w@1wt.eu> writes:
Show 7 quoted lines
> 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
Willy Tarreau· Jul 19, 2006, 22:50 UTC · re: Junio C Hamano · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

On Wed, Jul 19, 2006 at 03:33:48PM -0700, Junio C Hamano wrote:
Show 17 quoted lines
> Willy Tarreau <w@1wt.eu> writes:
> 
> > 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

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

Willy Tarreau· Jul 20, 2006, 09:30 UTC · re: Junio C Hamano · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

Hi Junio,
On Wed, Jul 19, 2006 at 03:33:48PM -0700, Junio C Hamano wrote:
Show 17 quoted lines
> Willy Tarreau <w@1wt.eu> writes:
> 
> > 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

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

>From d8a0d2bbd2365b719a7f68edd78c77a0fd903cab Mon Sep 17 00:00:00 2001
From: Willy Tarreau <w@1wt.eu>
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.

Signed-off-by: Willy Tarreau <w@1wt.eu>
---
 Documentation/config.txt       |   11 +++++++++++
 Documentation/git-tar-tree.txt |   15 ++++++++++++++-
 builtin-tar-tree.c             |   21 ++++++++++++++++++---
 3 files changed, 43 insertions(+), 4 deletions(-)
Show changes to 3 files +43 −4

Documentation/config.txt, Documentation/git-tar-tree.txt, builtin-tar-tree.c

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 0b434c1..f4985d4 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -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'
diff --git a/Documentation/git-tar-tree.txt b/Documentation/git-tar-tree.txt
index f2675c4..7a99acf 100644
--- a/Documentation/git-tar-tree.txt
+++ b/Documentation/git-tar-tree.txt
@@ -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 -)::
 
diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c
index f2e48aa..e5aaded 100644
--- a/builtin-tar-tree.c
+++ b/builtin-tar-tree.c
@@ -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:
-- 
1.4.1
Rogan Dawes· Jul 20, 2006, 09:44 UTC · re: Willy Tarreau · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

Willy Tarreau wrote:
Show 8 quoted lines
> +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);
Show 5 quoted lines
> +		}
> +		return 0;
> +	}
> +	return git_default_config(var, value);
> +}
Or else move it to just before the "return 0;" line.
Rogan
Willy Tarreau· Jul 20, 2006, 09:55 UTC · re: Rogan Dawes · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

On Thu, Jul 20, 2006 at 11:44:26AM +0200, Rogan Dawes wrote:
Show 12 quoted lines
> Willy Tarreau wrote:
> >+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);

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.

Show 9 quoted lines
> >+		}
> >+		return 0;
> >+	}
> >+	return git_default_config(var, value);
> >+}
> 
> Or else move it to just before the "return 0;" line.
> 
> Rogan

Regards, Willy

Rene Scharfe· Jul 20, 2006, 08:14 UTC · re: Willy Tarreau · lore

Re: [PATCH] tar-tree: add the "tar.applyUmask" config option

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.

Willy Tarreau schrieb:
Show 8 quoted lines
> +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:

Acked-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>

← back to recent threads