1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#ifndef __file_ops_h__
#define __file_ops_h__
#include <cstdint>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "debug.h"
#include "app.h"
#include "mrtrix.h"
#include "types.h"
#include "file/path.h"
#include "file/config.h"
namespace MR
{
namespace File
{
namespace
{
inline char random_char ()
{
char c = rand () % 62;
if (c < 10) return c+48;
if (c < 36) return c+55;
return c+61;
}
//CONF option: TmpFileDir
//CONF default: `/tmp` (on Unix), `.` (on Windows)
//CONF The prefix for temporary files (as used in pipelines). By default,
//CONF these files get written to the current folder on Windows machines,
//CONF which may cause performance issues, particularly when operating
//CONF over distributed file systems. On Unix machines, the default is
//CONF /tmp/, which is typically a RAM file system and should therefore
//CONF be fast; but may cause issues on machines with little RAM
//CONF capacity or where write-access to this location is not permitted.
//CONF
//CONF Note that this location can also be manipulated using the
//CONF :envvar:`MRTRIX_TMPFILE_DIR` environment variable, without editing the
//CONF config file. Note also that this setting does not influence the
//CONF location in which Python scripts construct their scratch
//CONF directories; that is determined based on config file option
//CONF ScriptScratchDir.
//ENVVAR name: MRTRIX_TMPFILE_DIR
//ENVVAR This has the same effect as the :option:`TmpFileDir`
//ENVVAR configuration file entry, and can be used to set the location of
//ENVVAR temporary files (as used in Unix pipes) for a single session,
//ENVVAR within a single script, or for a single command without
//ENVVAR modifying the configuration file.
const std::string __get_tmpfile_dir () {
const char* from_env_mrtrix = getenv ("MRTRIX_TMPFILE_DIR");
if (from_env_mrtrix)
return from_env_mrtrix;
const char* default_tmpdir =
#ifdef MRTRIX_WINDOWS
"."
#else
"/tmp"
#endif
;
const char* from_env_general = getenv ("TMPDIR");
if (from_env_general)
default_tmpdir = from_env_general;
return File::Config::get ("TmpFileDir", default_tmpdir);
}
const std::string& tmpfile_dir () {
static const std::string __tmpfile_dir = __get_tmpfile_dir();
return __tmpfile_dir;
}
//CONF option: TmpFilePrefix
//CONF default: `mrtrix-tmp-`
//CONF The prefix to use for the basename of temporary files. This will
//CONF be used to generate a unique filename for the temporary file, by
//CONF adding random characters to this prefix, followed by a suitable
//CONF suffix (depending on file type). Note that this prefix can also be
//CONF manipulated using the `MRTRIX_TMPFILE_PREFIX` environment
//CONF variable, without editing the config file.
//ENVVAR name: MRTRIX_TMPFILE_PREFIX
//ENVVAR This has the same effect as the :option:`TmpFilePrefix`
//ENVVAR configuration file entry, and can be used to set the prefix for
//ENVVAR the name of temporary files (as used in Unix pipes) for a
//ENVVAR single session, within a single script, or for a single command
//ENVVAR without modifying the configuration file.
const std::string __get_tmpfile_prefix () {
const char* from_env = getenv ("MRTRIX_TMPFILE_PREFIX");
if (from_env) return from_env;
return File::Config::get ("TmpFilePrefix", "mrtrix-tmp-");
}
const std::string& tmpfile_prefix () {
static const std::string __tmpfile_prefix = __get_tmpfile_prefix();
return __tmpfile_prefix;
}
/* Config file options listed here so that they can be scraped by
* generate_user_docs.sh and added to the list of config file options in
* the documentation without modifying the script to read from the scripts
* directory.
*/
//CONF option: ScriptScratchDir
//CONF default: `.`
//CONF The location in which to generate the scratch directories to be
//CONF used by MRtrix Python scripts. By default they will be generated
//CONF in the working directory.
//CONF Note that this setting does not influence the location in which
//CONF piped images and other temporary files are created by MRtrix3;
//CONF that is determined based on config file option :option:`TmpFileDir`.
//CONF option: ScriptScratchPrefix
//CONF default: `<script>-tmp-`
//CONF The prefix to use when generating a unique name for a Python
//CONF script scratch directory. By default the name of the invoked
//CONF script itself will be used, followed by `-tmp-` (six random
//CONF characters are then appended to produce a unique name in cases
//CONF where a script may be run multiple times in parallel).
}
inline void remove (const std::string& file)
{
if (std::remove (file.c_str()))
throw Exception ("error deleting file \"" + file + "\": " + strerror (errno));;
}
inline void create (const std::string& filename, int64_t size = 0)
{
DEBUG (std::string("creating ") + (size ? "" : "empty ") + "file \"" + filename + "\"" + (size ? " with size " + str (size) : ""));
int fid;
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) {
if (errno == EEXIST) {
App::check_overwrite (filename);
INFO ("file \"" + filename + "\" already exists - removing");
remove (filename);
}
else
throw Exception ("error creating output file \"" + filename + "\": " + std::strerror (errno));
}
if (fid < 0) {
std::string mesg ("error creating file \"" + filename + "\": " + strerror (errno));
if (errno == EEXIST)
mesg += " (use -force option to force overwrite)";
throw Exception (mesg);
}
if (size) size = ftruncate (fid, size);
close (fid);
if (size)
throw Exception ("cannot resize file \"" + filename + "\": " + strerror (errno));
}
inline void resize (const std::string& filename, int64_t size)
{
DEBUG ("resizing file \"" + filename + "\" to " + str (size));
int fd = open (filename.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd < 0)
throw Exception ("error opening file \"" + filename + "\" for resizing: " + strerror (errno));
int status = ftruncate (fd, size);
close (fd);
if (status)
throw Exception ("cannot resize file \"" + filename + "\": " + strerror (errno));
}
inline bool is_tempfile (const std::string& name, const char* suffix = NULL)
{
if (Path::basename (name).compare (0, tmpfile_prefix().size(), tmpfile_prefix()))
return false;
if (suffix)
if (!Path::has_suffix (name, suffix))
return false;
return true;
}
inline std::string create_tempfile (int64_t size = 0, const char* suffix = NULL)
{
DEBUG ("creating temporary file of size " + str (size));
std::string filename (Path::join (tmpfile_dir(), tmpfile_prefix()) + "XXXXXX.");
int rand_index = filename.size() - 7;
if (suffix) filename += suffix;
int fid;
do {
for (int n = 0; n < 6; n++)
filename[rand_index+n] = random_char();
fid = open (filename.c_str(), O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
} while (fid < 0 && errno == EEXIST);
if (fid < 0)
throw Exception (std::string ("error creating temporary file in directory \"" + tmpfile_dir() + "\": ") + strerror (errno));
int status = size ? ftruncate (fid, size) : 0;
close (fid);
if (status) throw Exception ("cannot resize file \"" + filename + "\": " + strerror (errno));
return filename;
}
inline void mkdir (const std::string& folder)
{
if (::mkdir (folder.c_str()
#ifndef MRTRIX_WINDOWS
, 0777
#endif
))
throw Exception ("error creating folder \"" + folder + "\": " + strerror (errno));
}
inline void rmdir (const std::string& folder, bool recursive = false)
{
if (recursive) {
Path::Dir dir (folder);
std::string entry;
while ((entry = dir.read_name()).size()) {
std::string path = Path::join (folder, entry);
if (Path::is_dir (path))
rmdir (path, true);
else
remove (path);
}
}
DEBUG ("deleting folder \"" + folder + "\"...");
if (::rmdir (folder.c_str()))
throw Exception ("error deleting folder \"" + folder + "\": " + strerror (errno));
}
}
}
#endif
|