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
|
/*
Copyright 2008 Brain Research Institute, Melbourne, Australia
Written by J-Donald Tournier, 27/06/08.
This file is part of MRtrix.
MRtrix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MRtrix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MRtrix. If not, see <http://www.gnu.org/licenses/>.
11-07-2008 J-Donald Tournier <d.tournier@brain.org.au>
* fixed TMPFILE_ROOT_LEN - now set to 7
*/
#include <glib/gstdio.h>
#include <glibmm/stringutils.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef G_OS_WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#endif
#include "file/mmap.h"
#include "file/config.h"
namespace MR {
namespace File {
namespace {
gchar random_char ()
{
gchar c = rand () % 62;
if (c < 10) return (c+48);
if (c < 36) return (c+55);
return (c+61);
}
}
MMap::Base::~Base ()
{
unmap();
if (delete_after) {
debug ("deleting file \"" + filename + "\"...");
if (g_unlink (filename.c_str()))
error ("WARNING: error deleting file \"" + filename + "\": " + Glib::strerror(errno));
}
}
void MMap::Base::map()
{
if (msize == 0) throw Exception ("attempt to map file \"" + filename + "\" using invalid mmap!");
if (addr) return;
if ((fd = g_open (filename.c_str(), (read_only ? O_RDONLY : O_RDWR), 0644)) < 0)
throw Exception ("error opening file \"" + filename + "\": " + Glib::strerror(errno));
try {
#ifdef G_OS_WIN32
HANDLE handle = CreateFileMapping ((HANDLE) _get_osfhandle(fd), NULL,
(read_only ? PAGE_READONLY : PAGE_READWRITE), 0, msize, NULL);
if (!handle) throw 0;
addr = (void*) MapViewOfFile (handle, (read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), 0, 0, msize);
if (!addr) throw 0;
CloseHandle (handle);
#else
addr = (void *) mmap((char*)0, msize, (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) throw 0;
#endif
debug ("file \"" + filename + "\" mapped at " + str (addr)
+ ", size " + str (msize)
+ " (read-" + ( read_only ? "only" : "write" ) + ")");
}
catch (...) {
close (fd);
addr = NULL;
throw Exception ("memory-mapping failed for file \"" + filename + "\": " + Glib::strerror(
#ifdef G_OS_WIN32
GetLastError()
#else
errno
#endif
));
}
}
void MMap::Base::unmap()
{
if (!addr) return;
debug ("unmapping file \"" + filename + "\"");
#ifdef G_OS_WIN32
if (!UnmapViewOfFile ((LPVOID) addr))
#else
if (munmap (addr, msize))
#endif
error ("error unmapping file \"" + filename + "\": " + Glib::strerror(
#ifdef G_OS_WIN32
GetLastError()
#else
errno
#endif
));
close (fd);
fd = -1;
addr = NULL;
}
void MMap::Base::resize (gsize new_size)
{
debug ("resizing file \"" + filename + "\" to " + str (new_size) + "...");
if (read_only) throw Exception ("attempting to resize read-only file \"" + filename + "\"");
unmap();
if ((fd = g_open (filename.c_str(), O_RDWR, 0644)) < 0)
throw Exception ("error opening file \"" + filename + "\" for resizing: " + Glib::strerror(errno));
int status = ftruncate (fd, new_size);
close (fd);
fd = -1;
if (status) throw Exception ("cannot resize file \"" + filename + "\": " + Glib::strerror(errno));
msize = new_size;
}
void MMap::init (const String& fname, gsize desired_size_if_inexistant, const gchar* suffix)
{
base = new Base;
try {
if (fname.size()) {
debug ("preparing file \"" + fname + "\"");
base->filename = fname;
struct_stat64 sbuf;
if (STAT64 (base->filename.c_str(), &sbuf)) {
if (errno != ENOENT)
throw Exception ("cannot stat file \"" + base->filename + "\": " + Glib::strerror(errno));
if (desired_size_if_inexistant == 0)
throw Exception ("cannot access file \"" + base->filename + "\": " + Glib::strerror(errno));
int fid = g_open (base->filename.c_str(), O_CREAT | O_RDWR | O_EXCL, 0644);
if (fid < 0) throw Exception ("error creating file \"" + base->filename + "\": " + Glib::strerror(errno));
int status = ftruncate (fid, desired_size_if_inexistant);
close (fid);
if (status) throw Exception ("WARNING: cannot resize file \"" + base->filename + "\": " + Glib::strerror(errno));
base->read_only = false;
base->msize = desired_size_if_inexistant;
return;
}
else {
if (desired_size_if_inexistant)
throw Exception ("cannot create file \"" + base->filename + "\": it already exists");
base->msize = sbuf.st_size;
base->mtime = sbuf.st_mtime;
return;
}
}
}
catch (Exception) {
base = NULL;
throw;
}
if (!desired_size_if_inexistant) throw Exception ("cannot create empty scratch file");
debug ("creating and mapping scratch file");
assert (suffix);
base->filename = String (TMPFILE_ROOT) + "XXXXXX." + suffix;
int fid;
do {
for (int n = 0; n < 6; n++)
base->filename[TMPFILE_ROOT_LEN+n] = random_char();
fid = g_open (base->filename.c_str(), O_CREAT | O_RDWR | O_EXCL, 0644);
} while (fid < 0 && errno == EEXIST);
if (fid < 0)
throw Exception ("error creating temporary file in current working directory: " + Glib::strerror(errno));
int status = ftruncate (fid, desired_size_if_inexistant);
close (fid);
if (status) throw Exception ("cannot resize file \"" + base->filename + "\": " + Glib::strerror(errno));
base->msize = desired_size_if_inexistant;
base->read_only = false;
}
bool MMap::changed () const
{
if (!base) return (false);
struct_stat64 sbuf;
if (STAT64 (base->filename.c_str(), &sbuf)) return (false);
if (off_t (base->msize) != sbuf.st_size) return (true);
if (base->mtime != sbuf.st_mtime) return (true);
return (false);
}
}
}
|