Skip to content

Commit 17e61b8

Browse files
committed
set_shared_perm(): sometimes we know what the final mode bits should look like
adjust_shared_perm() first obtains the mode bits from lstat(2), expecting to find what the result of applying user's umask is, and then tweaks it as necessary. When the file to be adjusted is created with mkstemp(3), however, the mode thusly obtained does not have anything to do with user's umask, and we would need to start from 0444 in such a case and there is no point running lstat(2) for such a path. This introduces a new API set_shared_perm() to bypass the lstat(2) and instead force setting the mode bits to the desired value directly. adjust_shared_perm() becomes a thin wrapper to the function. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3be1f18 commit 17e61b8

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ enum sharedrepo {
613613
PERM_EVERYBODY = 0664,
614614
};
615615
int git_config_perm(const char *var, const char *value);
616-
int adjust_shared_perm(const char *path);
616+
int set_shared_perm(const char *path, int mode);
617+
#define adjust_shared_perm(path) set_shared_perm((path), 0)
617618
int safe_create_leading_directories(char *path);
618619
int safe_create_leading_directories_const(const char *path);
619620
char *enter_repo(char *path, int strict);

path.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,23 @@ char *enter_repo(char *path, int strict)
311311
return NULL;
312312
}
313313

314-
int adjust_shared_perm(const char *path)
314+
int set_shared_perm(const char *path, int mode)
315315
{
316316
struct stat st;
317-
int mode, tweak, shared;
317+
int tweak, shared, orig_mode;
318318

319-
if (!shared_repository)
319+
if (!shared_repository) {
320+
if (mode)
321+
return chmod(path, mode & ~S_IFMT);
320322
return 0;
321-
if (lstat(path, &st) < 0)
322-
return -1;
323-
mode = st.st_mode;
323+
}
324+
if (!mode) {
325+
if (lstat(path, &st) < 0)
326+
return -1;
327+
mode = st.st_mode;
328+
orig_mode = mode;
329+
} else
330+
orig_mode = 0;
324331
if (shared_repository < 0)
325332
shared = -shared_repository;
326333
else
@@ -344,9 +351,9 @@ int adjust_shared_perm(const char *path)
344351
}
345352

346353
if (((shared_repository < 0
347-
? (st.st_mode & (FORCE_DIR_SET_GID | 0777))
348-
: (st.st_mode & mode)) != mode) &&
349-
chmod(path, mode) < 0)
354+
? (orig_mode & (FORCE_DIR_SET_GID | 0777))
355+
: (orig_mode & mode)) != mode) &&
356+
chmod(path, (mode & ~S_IFMT)) < 0)
350357
return -2;
351358
return 0;
352359
}

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
22802280
}
22812281

22822282
out:
2283-
if (chmod(filename, 0444) || adjust_shared_perm(filename))
2283+
if (set_shared_perm(filename, (S_IFREG|0444)))
22842284
return error("unable to set permission to '%s'", filename);
22852285
return 0;
22862286
}

0 commit comments

Comments
 (0)