1
1
/*
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
12
3
*/
13
4
#include "cache.h"
14
5
#include "strbuf.h"
@@ -405,26 +396,14 @@ const char *enter_repo(const char *path, int strict)
405
396
return NULL ;
406
397
}
407
398
408
- int set_shared_perm ( const char * path , int mode )
399
+ static int calc_shared_perm ( int mode )
409
400
{
410
- int tweak , shared , orig_mode ;
401
+ int tweak ;
411
402
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 ;
423
403
if (shared_repository < 0 )
424
- shared = - shared_repository ;
404
+ tweak = - shared_repository ;
425
405
else
426
- shared = shared_repository ;
427
- tweak = shared ;
406
+ tweak = shared_repository ;
428
407
429
408
if (!(mode & S_IWUSR ))
430
409
tweak &= ~0222 ;
@@ -436,16 +415,28 @@ int set_shared_perm(const char *path, int mode)
436
415
else
437
416
mode |= tweak ;
438
417
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 )) {
440
433
/* 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 ;
443
436
}
444
437
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 )
449
440
return -2 ;
450
441
return 0 ;
451
442
}
0 commit comments