tempfile.c: introduce 'create_tempfile_mode'

In the next patch, 'hold_lock_file_for_update' will gain an additional
'mode' parameter to specify permissions for the associated temporary
file.

Since the lockfile.c machinery uses 'create_tempfile' which always
creates a temporary file with global read-write permissions, introduce a
variant here that allows specifying the mode.

Note that the mode given to 'create_tempfile_mode' is not guaranteed to
be written to disk, since it is subject to both the umask and
'core.sharedRepository'.

Arguably, all temporary files should have permission 0444, since they
are likely to be renamed into place and then not written to again. This
is a much larger change than we may want to take on in this otherwise
small patch, so for the time being, make 'create_tempfile' behave as it
has always done by inlining it to 'create_tempfile_mode' with mode set
to '0666'.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Taylor Blau 2020-04-27 10:27:54 -06:00 коммит произвёл Junio C Hamano
Родитель e870325ee8
Коммит bef0413c35
2 изменённых файлов: 12 добавлений и 4 удалений

Просмотреть файл

@ -130,17 +130,17 @@ static void deactivate_tempfile(struct tempfile *tempfile)
}
/* Make sure errno contains a meaningful value on error */
struct tempfile *create_tempfile(const char *path)
struct tempfile *create_tempfile_mode(const char *path, int mode)
{
struct tempfile *tempfile = new_tempfile();
strbuf_add_absolute_path(&tempfile->filename, path);
tempfile->fd = open(tempfile->filename.buf,
O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode);
if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
/* Try again w/o O_CLOEXEC: the kernel might not support it */
tempfile->fd = open(tempfile->filename.buf,
O_RDWR | O_CREAT | O_EXCL, 0666);
O_RDWR | O_CREAT | O_EXCL, mode);
if (tempfile->fd < 0) {
deactivate_tempfile(tempfile);
return NULL;

Просмотреть файл

@ -88,8 +88,16 @@ struct tempfile {
* Attempt to create a temporary file at the specified `path`. Return
* a tempfile (whose "fd" member can be used for writing to it), or
* NULL on error. It is an error if a file already exists at that path.
* Note that `mode` will be further modified by the umask, and possibly
* `core.sharedRepository`, so it is not guaranteed to have the given
* mode.
*/
struct tempfile *create_tempfile(const char *path);
struct tempfile *create_tempfile_mode(const char *path, int mode);
static inline struct tempfile *create_tempfile(const char *path)
{
return create_tempfile_mode(path, 0666);
}
/*
* Register an existing file as a tempfile, meaning that it will be