Subject: [PATCH 2/2] Resolve double chmod() in move_temp_to_file()

From: Johan Herland <[email-masked]>
Date: Thu, 26 Mar 2009 16:17:48 +0100

move_temp_to_file() used to chmod(foo, 0444) immediately before calling
adjust_shared_perm() which potentially does another call to chmod().

This patch splits a new function (called get_shared_perm()) out of
adjust_shared_perm(). The new function adjusts a given file mode value
according to the shared_repository setting, and returns the resulting
mode. It is used in move_temp_file() to generate the correct and final
permissions to pass to chmod() in a single call.

Signed-off-by: Johan Herland <[email-masked]>
---
 cache.h     |    1 +
 path.c      |   26 +++++++++++++++++++-------
 sha1_file.c |    2 +-
 3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/cache.h b/cache.h
index 9cf5a13..b24eb3a 100644
--- a/cache.h
+++ b/cache.h
@@ -623,6 +623,7 @@ enum sharedrepo {
 	PERM_EVERYBODY      = 0664,
 };
 int git_config_perm(const char *var, const char *value);
+int get_shared_perm(int mode);
 int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
 int safe_create_leading_directories_const(const char *path);
diff --git a/path.c b/path.c
index 42898e0..497db19 100644
--- a/path.c
+++ b/path.c
@@ -311,16 +311,13 @@ char *enter_repo(char *path, int strict)
 	return NULL;
 }
 
-int adjust_shared_perm(const char *path)
+int get_shared_perm(int mode)
 {
-	struct stat st;
-	int mode, tweak, shared;
+	int tweak, shared;
 
 	if (!shared_repository)
-		return 0;
-	if (lstat(path, &st) < 0)
-		return -1;
-	mode = st.st_mode;
+		return mode;
+
 	if (shared_repository < 0)
 		shared = -shared_repository;
 	else
@@ -343,6 +340,21 @@ int adjust_shared_perm(const char *path)
 		mode |= FORCE_DIR_SET_GID;
 	}
 
+	return mode;
+}
+
+int adjust_shared_perm(const char *path)
+{
+	struct stat st;
+	int mode, tweak, shared;
+
+	if (!shared_repository)
+		return 0;
+	if (lstat(path, &st) < 0)
+		return -1;
+
+	mode = get_shared_perm(st.st_mode);
+
 	if (((shared_repository < 0
 	      ? (st.st_mode & (FORCE_DIR_SET_GID | 0777))
 	      : (st.st_mode & mode)) != mode) &&
diff --git a/sha1_file.c b/sha1_file.c
index 87ac53b..05af3c5 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2252,7 +2252,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
 		/* FIXME!!! Collision check here ? */
 	}
 
-	if (chmod(filename, 0444) || adjust_shared_perm(filename))
+	if (chmod(filename, get_shared_perm(0444)))
 		return error("unable to set permission to '%s'", filename);
 	return 0;
 }
-- 
1.6.1.2.461.g5bad6