40 inline char random_char ()
42 char c = rand () % 62;
43 if (c < 10)
return c+48;
44 if (c < 36)
return c+55;
71 const std::string __get_tmpfile_dir () {
72 const char* from_env_mrtrix = getenv (
"MRTRIX_TMPFILE_DIR");
74 return from_env_mrtrix;
76 const char* default_tmpdir =
84 const char* from_env_general = getenv (
"TMPDIR");
86 default_tmpdir = from_env_general;
91 const std::string& tmpfile_dir () {
92 static const std::string __tmpfile_dir = __get_tmpfile_dir();
111 const std::string __get_tmpfile_prefix () {
112 const char* from_env = getenv (
"MRTRIX_TMPFILE_PREFIX");
113 if (from_env)
return from_env;
117 const std::string& tmpfile_prefix () {
118 static const std::string __tmpfile_prefix = __get_tmpfile_prefix();
119 return __tmpfile_prefix;
150 inline void remove (
const std::string& file)
153 throw Exception (
"error deleting file \"" + file +
"\": " + strerror (errno));;
157 inline void create (
const std::string& filename, int64_t size = 0)
159 DEBUG (std::string(
"creating ") + (size ?
"" :
"empty ") +
"file \"" + filename +
"\"" + (size ?
" with size " +
str (size) :
""));
162 while ( (fid = open (filename.c_str(), O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
163 if (errno == EEXIST) {
165 INFO (
"file \"" + filename +
"\" already exists - removing");
169 throw Exception (
"error creating output file \"" + filename +
"\": " + std::strerror (errno));
172 std::string mesg (
"error creating file \"" + filename +
"\": " + strerror (errno));
174 mesg +=
" (use -force option to force overwrite)";
178 if (size) size = ftruncate (fid, size);
182 throw Exception (
"cannot resize file \"" + filename +
"\": " + strerror (errno));
187 inline void resize (
const std::string& filename, int64_t size)
189 DEBUG (
"resizing file \"" + filename +
"\" to " +
str (size));
191 int fd = open (filename.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
193 throw Exception (
"error opening file \"" + filename +
"\" for resizing: " + strerror (errno));
194 int status = ftruncate (fd, size);
197 throw Exception (
"cannot resize file \"" + filename +
"\": " + strerror (errno));
218 DEBUG (
"creating temporary file of size " +
str (size));
220 std::string filename (
Path::join (tmpfile_dir(), tmpfile_prefix()) +
"XXXXXX.");
221 int rand_index = filename.size() - 7;
222 if (suffix) filename += suffix;
226 for (
int n = 0; n < 6; n++)
227 filename[rand_index+n] = random_char();
228 fid = open (filename.c_str(), O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
229 }
while (fid < 0 && errno == EEXIST);
232 throw Exception (std::string (
"error creating temporary file in directory \"" + tmpfile_dir() +
"\": ") + strerror (errno));
234 int status = size ? ftruncate (fid, size) : 0;
236 if (status)
throw Exception (
"cannot resize file \"" + filename +
"\": " + strerror (errno));
242 inline void mkdir (
const std::string& folder)
245#ifndef MRTRIX_WINDOWS
249 throw Exception (
"error creating folder \"" + folder +
"\": " + strerror (errno));
252 inline void rmdir (
const std::string& folder,
bool recursive =
false)
257 while ((entry = dir.
read_name()).size()) {
258 std::string path =
Path::join (folder, entry);
265 DEBUG (
"deleting folder \"" + folder +
"\"...");
267 throw Exception (
"error deleting folder \"" + folder +
"\": " + strerror (errno));
static std::string get(const std::string &key)
void check_overwrite(const std::string &name)
void create(const std::string &filename, int64_t size=0)
bool is_tempfile(const std::string &name, const char *suffix=NULL)
void resize(const std::string &filename, int64_t size)
void mkdir(const std::string &folder)
void remove(const std::string &file)
void rmdir(const std::string &folder, bool recursive=false)
std::string create_tempfile(int64_t size=0, const char *suffix=NULL)
std::string join(const std::string &first, const std::string &second)
std::string basename(const std::string &name)
bool is_dir(const std::string &path)
bool has_suffix(const std::string &name, const std::string &suffix)
std::string str(const T &value, int precision=0)