40 lines
912 B
C
40 lines
912 B
C
#include "install.h"
|
|
|
|
int
|
|
backup()
|
|
{
|
|
struct stat sb;
|
|
|
|
const std::string bkp = hd + _prefix + "_backup/";
|
|
|
|
struct stat stat_dir;
|
|
if (stat(bkp.c_str(), &stat_dir) != 0)
|
|
{
|
|
mkdir(bkp.c_str(), 0700);
|
|
}
|
|
|
|
for (auto &i : _vec)
|
|
{
|
|
const std::string source_file = hd + _prefix + "/" + i;
|
|
const std::string dest_file = bkp + i;
|
|
|
|
std::cout << "Backing up " << _prefix << "/" << i << "\n";
|
|
|
|
int source = open(source_file.c_str(), O_RDONLY, 0);
|
|
int dest = open(dest_file.c_str(), O_WRONLY | O_CREAT /*| O_TRUNC/**/, 0644);
|
|
|
|
// struct required, rationale: function stat() exists also
|
|
struct stat stat_source;
|
|
fstat(source, &stat_source);
|
|
sendfile(dest, source, 0, stat_source.st_size);
|
|
|
|
close(source);
|
|
close(dest);
|
|
|
|
if (access(dest_file.c_str(), F_OK) != 0)
|
|
{
|
|
throw ExceptionHandler(CUSTOM_ERROR, "Backing up " + dest_file + " failed!");
|
|
}
|
|
}
|
|
}
|