git_mkstemp_mode, xmkstemp_mode: variants of gitmkstemps with mode argument.
gitmkstemps emulates the behavior of mkstemps, which is usually used to create files in a shared directory like /tmp/, hence, it creates files with permission 0600. Add git_mkstemps_mode() that allows us to specify the desired mode, and make git_mkstemps() a wrapper that always uses 0600 to call it. Later we will use git_mkstemps_mode() when creating pack files. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Feb 22, 2010 at 23:32 UTC
b862b61c03797fd00490bb8caf05be840b79c6cb
3 files changed
+27
-2
cache.h
+4
@@ -641,6 +641,10 @@ int git_mkstemp(char *path, size_t n, const char *template);
641
642
int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
643
644
+/* set default permissions by passing mode arguments to open(2) */
645
+int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
646
+int git_mkstemp_mode(char *pattern, int mode);
647
+
648
/*
649
* NOTE NOTE NOTE!!
650
*
path.c
+13
-2
@@ -162,7 +162,7 @@ int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
162
#undef TMP_MAX
163
#define TMP_MAX 16384
164
165
-int gitmkstemps(char *pattern, int suffix_len)
165
+int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
166
{
167
static const char letters[] =
168
"abcdefghijklmnopqrstuvwxyz"
@@ -204,7 +204,7 @@ int gitmkstemps(char *pattern, int suffix_len)
204
template[4] = letters[v % num_letters]; v /= num_letters;
205
template[5] = letters[v % num_letters]; v /= num_letters;
206
207
- fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, 0600);
207
+ fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
208
if (fd > 0)
209
return fd;
210
/*
@@ -226,6 +226,17 @@ int gitmkstemps(char *pattern, int suffix_len)
226
return -1;
227
}
228
229
+int git_mkstemp_mode(char *pattern, int mode)
230
+{
231
+ /* mkstemp is just mkstemps with no suffix */
232
+ return git_mkstemps_mode(pattern, 0, mode);
233
+}
234
+
235
+int gitmkstemps(char *pattern, int suffix_len)
236
+{
237
+ return git_mkstemps_mode(pattern, suffix_len, 0600);
238
+}
239
+
240
int validate_headref(const char *path)
241
{
242
struct stat st;
wrapper.c
+10
@@ -204,6 +204,16 @@ int xmkstemp(char *template)
204
return fd;
205
}
206
207
+int xmkstemp_mode(char *template, int mode)
208
+{
209
+ int fd;
210
+
211
+ fd = git_mkstemp_mode(template, mode);
212
+ if (fd < 0)
213
+ die_errno("Unable to create temporary file");
214
+ return fd;
215
+}
216
+
217
/*
218
* zlib wrappers to make sure we don't silently miss errors
219
* at init time.