Skip to content

Commit b862b61

Browse files
moygitster
authored andcommitted
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>
1 parent 00787ed commit b862b61

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ int git_mkstemp(char *path, size_t n, const char *template);
641641

642642
int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
643643

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+
644648
/*
645649
* NOTE NOTE NOTE!!
646650
*

path.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
162162
#undef TMP_MAX
163163
#define TMP_MAX 16384
164164

165-
int gitmkstemps(char *pattern, int suffix_len)
165+
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
166166
{
167167
static const char letters[] =
168168
"abcdefghijklmnopqrstuvwxyz"
@@ -204,7 +204,7 @@ int gitmkstemps(char *pattern, int suffix_len)
204204
template[4] = letters[v % num_letters]; v /= num_letters;
205205
template[5] = letters[v % num_letters]; v /= num_letters;
206206

207-
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, 0600);
207+
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
208208
if (fd > 0)
209209
return fd;
210210
/*
@@ -226,6 +226,17 @@ int gitmkstemps(char *pattern, int suffix_len)
226226
return -1;
227227
}
228228

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+
229240
int validate_headref(const char *path)
230241
{
231242
struct stat st;

wrapper.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ int xmkstemp(char *template)
204204
return fd;
205205
}
206206

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+
207217
/*
208218
* zlib wrappers to make sure we don't silently miss errors
209219
* at init time.

0 commit comments

Comments
 (0)