command to back-up/compact DB

This commit is contained in:
Doug Hoyte
2023-01-18 15:02:15 -05:00
parent 18a7cc556a
commit 492505bdb5

30
src/cmd_compact.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <unistd.h>
#include <stdio.h>
#include <docopt.h>
#include "golpe.h"
static const char USAGE[] =
R"(
Usage:
compact <output_file>
)";
void cmd_compact(const std::vector<std::string> &subArgs) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, subArgs, true, "");
std::string outputFile = args["<output_file>"].asString();
if (outputFile == "-") {
env.copy_fd(1);
} else {
if (access(outputFile.c_str(), F_OK) == 0) throw herr("output file '", outputFile, "' exists, not overwriting");
auto *f = ::fopen(outputFile.c_str(), "w");
if (!f) throw herr("opening output file '", outputFile, "' failed: ", strerror(errno));
env.copy_fd(::fileno(f));
}
}