MCPcopy Create free account
hub / github.com/ElementsProject/elements / AllocateFileRange

Function AllocateFileRange

src/util/system.cpp:1231–1276  ·  view source on GitHub ↗

* this function tries to make a particular range of a file allocated (corresponding to disk space) * it is advisory, and the range specified in the arguments will never contain live data */

Source from the content-addressed store, hash-verified

1229 * it is advisory, and the range specified in the arguments will never contain live data
1230 */
1231void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
1232#if defined(WIN32)
1233 // Windows-specific version
1234 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
1235 LARGE_INTEGER nFileSize;
1236 int64_t nEndPos = (int64_t)offset + length;
1237 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
1238 nFileSize.u.HighPart = nEndPos >> 32;
1239 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
1240 SetEndOfFile(hFile);
1241#elif defined(MAC_OSX)
1242 // OSX specific version
1243 // NOTE: Contrary to other OS versions, the OSX version assumes that
1244 // NOTE: offset is the size of the file.
1245 fstore_t fst;
1246 fst.fst_flags = F_ALLOCATECONTIG;
1247 fst.fst_posmode = F_PEOFPOSMODE;
1248 fst.fst_offset = 0;
1249 fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
1250 fst.fst_bytesalloc = 0;
1251 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
1252 fst.fst_flags = F_ALLOCATEALL;
1253 fcntl(fileno(file), F_PREALLOCATE, &fst);
1254 }
1255 ftruncate(fileno(file), static_cast<off_t>(offset) + length);
1256#else
1257 #if defined(HAVE_POSIX_FALLOCATE)
1258 // Version using posix_fallocate
1259 off_t nEndPos = (off_t)offset + length;
1260 if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
1261 #endif
1262 // Fallback version
1263 // TODO: just write one byte per block
1264 static const char buf[65536] = {};
1265 if (fseek(file, offset, SEEK_SET)) {
1266 return;
1267 }
1268 while (length > 0) {
1269 unsigned int now = 65536;
1270 if (length < now)
1271 now = length;
1272 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
1273 length -= now;
1274 }
1275#endif
1276}
1277
1278#ifdef WIN32
1279fs::path GetSpecialFolderPath(int nFolder, bool fCreate)

Callers 1

AllocateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected