Skip to content

Commit cbe43b8

Browse files
tboegigitster
authored andcommitted
path.c: optimize adjust_shared_perm()
Sometimes the chown() function is called even when not needed (This can be provoked by running t1301, and adding some debug code). Save a chmod from 400 to 400, or from 600 to 600 on these files: .git/info/refs+ .git/objects/info/packs+ Save chmod on directories from 2770 to 2770: .git/refs .git/refs/heads .git/refs/tags Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a429d3 commit cbe43b8

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

path.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -396,22 +396,14 @@ const char *enter_repo(const char *path, int strict)
396396
return NULL;
397397
}
398398

399-
int adjust_shared_perm(const char *path)
399+
static int calc_shared_perm(int mode)
400400
{
401-
int tweak, shared, orig_mode, mode;
401+
int tweak;
402402

403-
if (!shared_repository) {
404-
return 0;
405-
}
406-
if (get_st_mode_bits(path, &mode) < 0)
407-
return -1;
408-
409-
orig_mode = mode;
410403
if (shared_repository < 0)
411-
shared = -shared_repository;
404+
tweak = -shared_repository;
412405
else
413-
shared = shared_repository;
414-
tweak = shared;
406+
tweak = shared_repository;
415407

416408
if (!(mode & S_IWUSR))
417409
tweak &= ~0222;
@@ -423,16 +415,28 @@ int adjust_shared_perm(const char *path)
423415
else
424416
mode |= tweak;
425417

426-
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)) {
427433
/* Copy read bits to execute bits */
428-
mode |= (shared & 0444) >> 2;
429-
mode |= FORCE_DIR_SET_GID;
434+
new_mode |= (new_mode & 0444) >> 2;
435+
new_mode |= FORCE_DIR_SET_GID;
430436
}
431437

432-
if (((shared_repository < 0
433-
? (orig_mode & (FORCE_DIR_SET_GID | 0777))
434-
: (orig_mode & mode)) != mode) &&
435-
chmod(path, (mode & ~S_IFMT)) < 0)
438+
if (((old_mode ^ new_mode) & ~S_IFMT) &&
439+
chmod(path, (new_mode & ~S_IFMT)) < 0)
436440
return -2;
437441
return 0;
438442
}

0 commit comments

Comments
 (0)