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 <gitster@pobox.com>
Junio C Hamano committed
Mar 27, 2009 at 23:21 UTC
17e61b82887fb71800b0fcd39ffe89ddf4d2492e
3 files changed
+19
-11
cache.h
+2
-1
@@ -613,7 +613,8 @@ enum sharedrepo {
613
PERM_EVERYBODY = 0664,
614
};
615
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)
618
int safe_create_leading_directories(char *path);
619
int safe_create_leading_directories_const(const char *path);
620
char *enter_repo(char *path, int strict);
path.c
+16
-9
@@ -311,16 +311,23 @@ char *enter_repo(char *path, int strict)
311
return NULL;
312
}
313
314
-int adjust_shared_perm(const char *path)
314
+int set_shared_perm(const char *path, int mode)
315
{
316
struct stat st;
317
- int mode, tweak, shared;
317
+ int tweak, shared, orig_mode;
318
319
- if (!shared_repository)
319
+ if (!shared_repository) {
320
+ if (mode)
321
+ return chmod(path, mode & ~S_IFMT);
322
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;
331
if (shared_repository < 0)
332
shared = -shared_repository;
333
else
@@ -344,9 +351,9 @@ int adjust_shared_perm(const char *path)
351
}
352
353
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)
357
return -2;
358
return 0;
359
}
sha1_file.c
+1
-1
@@ -2280,7 +2280,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
2280
}
2281
2282
out:
2283
- if (chmod(filename, 0444) || adjust_shared_perm(filename))
2283
+ if (set_shared_perm(filename, (S_IFREG|0444)))
2284
return error("unable to set permission to '%s'", filename);
2285
return 0;
2286
}