Skip to content

Commit e65cdde

Browse files
committed
Merge branch 'tb/shared-perm'
Simplifies adjust_shared_perm() implementation. * tb/shared-perm: path.c: optimize adjust_shared_perm() path.c: simplify adjust_shared_perm()
2 parents 60eea92 + cbe43b8 commit e65cdde

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

cache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,7 @@ enum sharedrepo {
719719
PERM_EVERYBODY = 0664
720720
};
721721
int git_config_perm(const char *var, const char *value);
722-
int set_shared_perm(const char *path, int mode);
723-
#define adjust_shared_perm(path) set_shared_perm((path), 0)
722+
int adjust_shared_perm(const char *path);
724723
int safe_create_leading_directories(char *path);
725724
int safe_create_leading_directories_const(const char *path);
726725
int mkdir_in_gitdir(const char *path);

path.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
/*
2-
* I'm tired of doing "vsnprintf()" etc just to open a
3-
* file, so here's a "return static buffer with printf"
4-
* interface for paths.
5-
*
6-
* It's obviously not thread-safe. Sue me. But it's quite
7-
* useful for doing things like
8-
*
9-
* f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
10-
*
11-
* which is what it's designed for.
2+
* Utilities for paths and pathnames
123
*/
134
#include "cache.h"
145
#include "strbuf.h"
@@ -405,26 +396,14 @@ const char *enter_repo(const char *path, int strict)
405396
return NULL;
406397
}
407398

408-
int set_shared_perm(const char *path, int mode)
399+
static int calc_shared_perm(int mode)
409400
{
410-
int tweak, shared, orig_mode;
401+
int tweak;
411402

412-
if (!shared_repository) {
413-
if (mode)
414-
return chmod(path, mode & ~S_IFMT);
415-
return 0;
416-
}
417-
if (!mode) {
418-
if (get_st_mode_bits(path, &mode) < 0)
419-
return -1;
420-
orig_mode = mode;
421-
} else
422-
orig_mode = 0;
423403
if (shared_repository < 0)
424-
shared = -shared_repository;
404+
tweak = -shared_repository;
425405
else
426-
shared = shared_repository;
427-
tweak = shared;
406+
tweak = shared_repository;
428407

429408
if (!(mode & S_IWUSR))
430409
tweak &= ~0222;
@@ -436,16 +415,28 @@ int set_shared_perm(const char *path, int mode)
436415
else
437416
mode |= tweak;
438417

439-
if (S_ISDIR(mode)) {
418+
return mode;
419+
}
420+
421+
422+
int adjust_shared_perm(const char *path)
423+
{
424+
int old_mode, new_mode;
425+
426+
if (!shared_repository)
427+
return 0;
428+
if (get_st_mode_bits(path, &old_mode) < 0)
429+
return -1;
430+
431+
new_mode = calc_shared_perm(old_mode);
432+
if (S_ISDIR(old_mode)) {
440433
/* Copy read bits to execute bits */
441-
mode |= (shared & 0444) >> 2;
442-
mode |= FORCE_DIR_SET_GID;
434+
new_mode |= (new_mode & 0444) >> 2;
435+
new_mode |= FORCE_DIR_SET_GID;
443436
}
444437

445-
if (((shared_repository < 0
446-
? (orig_mode & (FORCE_DIR_SET_GID | 0777))
447-
: (orig_mode & mode)) != mode) &&
448-
chmod(path, (mode & ~S_IFMT)) < 0)
438+
if (((old_mode ^ new_mode) & ~S_IFMT) &&
439+
chmod(path, (new_mode & ~S_IFMT)) < 0)
449440
return -2;
450441
return 0;
451442
}

0 commit comments

Comments
 (0)