52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <sys/sendfile.h> // sendfile
|
|
#include <fcntl.h> // open
|
|
#include <unistd.h> // close
|
|
#include <sys/stat.h> // fstat
|
|
#include <sys/types.h> // fstat
|
|
|
|
#include "../include/exceptionhandler.hpp"
|
|
#include "../include/backup.hpp"
|
|
|
|
Backup::Backup(const std::string &prefix, const std::vector<std::string> &vec)
|
|
: _prefix(prefix), _vec(vec) {};
|
|
|
|
void Backup::Execute(const std::string &hd) const
|
|
{
|
|
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!");
|
|
}
|
|
}
|
|
|
|
}
|