28 #include <sys/types.h>
45 #if defined HAVE_NFTW && !defined __MINGW32__
56 int res = system(cmd.c_str());
58 string msg =
"system(\"";
60 msg +=
"\") failed, returning ";
67 void cp_R(
const std::string &src,
const std::string &dest) {
73 string cmd(
"xcopy /E /q /Y");
93 #if defined HAVE_NFTW && !defined __MINGW32__
96 rm_rf_nftw_helper(
const char* path,
101 int r = (type == FTW_DP ? rmdir(path) : unlink(path));
105 return r < 0 ? errno : 0;
111 void rm_rf(
const string &filename) {
113 if (filename.empty() || !
dir_exists(filename))
116 #if defined HAVE_NFTW && !defined __MINGW32__
117 auto flags = FTW_DEPTH | FTW_PHYS;
120 int eno = nftw(filename.c_str(), rm_rf_nftw_helper, 10, flags);
129 }
else if (eno == EEXIST) {
137 }
else if (EEXIST != ENOTEMPTY && eno == ENOTEMPTY) {
145 string msg =
"recursive delete of \"";
147 msg +=
"\") failed, errno = ";
156 string cmd(
"rd /s /q");
158 string cmd(
"rm -rf");
166 void touch(
const string &filename) {
167 int fd =
open(filename.c_str(), O_CREAT|O_WRONLY|
O_BINARY, 0644);
168 if (fd >= 0)
close(fd);
Append filename argument to a command string with suitable escaping.
static bool append_filename_argument(std::string &cmd, const std::string &arg, bool leading_space=true)
Append filename argument arg to command cmd with suitable escaping.
void errno_to_string(int e, string &s)
Convert errno value to std::string, thread-safe if possible.
Utility functions for testing files.
bool dir_exists(const char *path)
Test if a directory exists.
void sleep(double t)
Sleep until the time represented by this object.
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
string str(int value)
Convert int to std::string.
include <fcntl.h>, but working around broken platforms.
<unistd.h>, but with compat.
Convert types to std::string.
void touch(const string &filename)
Touch a file, just like the Unix "touch" command.
void cp_R(const std::string &src, const std::string &dest)
Recursively copy a directory.
void rm_rf(const string &filename)
Remove a directory and contents, just like the Unix "rm -rf" command.
static void checked_system(const string &cmd)
Call system() and throw exception if it fails.
C++ function versions of useful Unix commands.