mirror of
https://github.com/AGWA/git-crypt.git
synced 2025-12-26 12:50:39 -08:00
When encrypting, use temporary file if file gets too big
This commit is contained in:
30
util.cpp
30
util.cpp
@@ -7,6 +7,7 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
|
||||
int exec_command (const char* command, std::string& output)
|
||||
{
|
||||
@@ -49,3 +50,32 @@ std::string resolve_path (const char* path)
|
||||
return resolved_path;
|
||||
}
|
||||
|
||||
void open_tempfile (std::fstream& file, std::ios_base::openmode mode)
|
||||
{
|
||||
const char* tmpdir = getenv("TMPDIR");
|
||||
size_t tmpdir_len;
|
||||
if (tmpdir) {
|
||||
tmpdir_len = strlen(tmpdir);
|
||||
} else {
|
||||
tmpdir = "/tmp";
|
||||
tmpdir_len = 4;
|
||||
}
|
||||
char* path = new char[tmpdir_len + 18];
|
||||
strcpy(path, tmpdir);
|
||||
strcpy(path + tmpdir_len, "/git-crypt.XXXXXX");
|
||||
int fd = mkstemp(path);
|
||||
if (fd == -1) {
|
||||
perror("mkstemp");
|
||||
std::exit(9);
|
||||
}
|
||||
file.open(path, mode);
|
||||
if (!file.is_open()) {
|
||||
perror("open");
|
||||
unlink(path);
|
||||
std::exit(9);
|
||||
}
|
||||
unlink(path);
|
||||
close(fd);
|
||||
delete[] path;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user