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 <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed Mar 30, 2013 at 10:53 UTC cbe43b847379e8d3db6aab2913b29b730fa5ca45
1 file changed +23 -19
path.c
+23 -19
@@ -396,22 +396,14 @@ const char *enter_repo(const char *path, int strict)
396 return NULL;
397 }
398
399 -int adjust_shared_perm(const char *path)
399 +static int calc_shared_perm(int mode)
400 {
401 - int tweak, shared, orig_mode, mode;
401 + int tweak;
402
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;
403 if (shared_repository < 0)
411 - shared = -shared_repository;
404 + tweak = -shared_repository;
405 else
413 - shared = shared_repository;
414 - tweak = shared;
406 + tweak = shared_repository;
407
408 if (!(mode & S_IWUSR))
409 tweak &= ~0222;
@@ -423,16 +415,28 @@ int adjust_shared_perm(const char *path)
415 else
416 mode |= tweak;
417
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)) {
433 /* 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;
436 }
437
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)
440 return -2;
441 return 0;
442 }