Add umask and rename compatibility wrappers for Windows

umask() doesn't exist on Windows and is thus a no-op.

rename() only works if the destination doesn't already exist,
so we must unlink before renaming.
This commit is contained in:
Cyril Cleaud
2014-06-26 23:03:30 -07:00
committed by Andrew Ayer
parent dcea03f0d7
commit df2b472cd9
5 changed files with 32 additions and 6 deletions
+13 -3
View File
@@ -69,14 +69,14 @@ void temp_fstream::open (std::ios_base::openmode mode)
char* path = &path_buffer[0];
std::strcpy(path, tmpdir);
std::strcpy(path + tmpdir_len, "/git-crypt.XXXXXX");
mode_t old_umask = umask(0077);
mode_t old_umask = util_umask(0077);
int fd = mkstemp(path);
if (fd == -1) {
int mkstemp_errno = errno;
umask(old_umask);
util_umask(old_umask);
throw System_error("mkstemp", "", mkstemp_errno);
}
umask(old_umask);
util_umask(old_umask);
std::fstream::open(path, mode);
if (!std::fstream::is_open()) {
unlink(path);
@@ -277,3 +277,13 @@ bool successful_exit (int status)
static void init_std_streams_platform ()
{
}
mode_t util_umask (mode_t mode)
{
return umask(mode);
}
int util_rename (const char* from, const char* to)
{
return rename(from, to);
}