| 1 |
/** |
| 2 |
* File utilities. |
| 3 |
* |
| 4 |
* Functions and objects dedicated to file I/O and management. TODO: Move here artifacts |
| 5 |
* from places such as root/ so both the frontend and the backend have access to them. |
| 6 |
* |
| 7 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 8 |
* Authors: Walter Bright, https://www.digitalmars.com |
| 9 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 10 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/common/file.d, common/_file.d) |
| 11 |
* Documentation: https://dlang.org/phobos/dmd_common_file.html |
| 12 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/common/file.d |
| 13 |
*/ |
| 14 |
|
| 15 |
module dmd.common.file; |
| 16 |
|
| 17 |
import core.stdc.stdio; |
| 18 |
import core.stdc.stdlib; |
| 19 |
import core.stdc.string; |
| 20 |
import core.stdc.limits; |
| 21 |
|
| 22 |
import core.stdc.errno : errno; |
| 23 |
import core.stdc.stdio : fprintf, remove, rename, stderr; |
| 24 |
import core.stdc.stdlib; |
| 25 |
import core.stdc.string : strerror, strlen, memcpy; |
| 26 |
|
| 27 |
import dmd.common.smallbuffer; |
| 28 |
import dmd.root.filename; |
| 29 |
import dmd.root.rmem; |
| 30 |
|
| 31 |
version (Windows) |
| 32 |
{ |
| 33 |
import core.stdc.wchar_; |
| 34 |
import core.sys.windows.winbase; |
| 35 |
import core.sys.windows.winnls : CP_ACP; |
| 36 |
import core.sys.windows.winnt; |
| 37 |
|
| 38 |
enum CodePage = CP_ACP; // assume filenames encoded in system default Windows ANSI code page |
| 39 |
enum invalidHandle = INVALID_HANDLE_VALUE; |
| 40 |
} |
| 41 |
else version (Posix) |
| 42 |
{ |
| 43 |
import core.sys.posix.dirent; |
| 44 |
import core.sys.posix.fcntl; |
| 45 |
import core.sys.posix.sys.mman; |
| 46 |
import core.sys.posix.sys.stat; |
| 47 |
import core.sys.posix.unistd; |
| 48 |
import core.sys.posix.utime; |
| 49 |
|
| 50 |
enum invalidHandle = -1; |
| 51 |
} |
| 52 |
else |
| 53 |
static assert(0); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
nothrow: |
| 59 |
|
| 60 |
/** |
| 61 |
Encapsulated management of a memory-mapped file. |
| 62 |
|
| 63 |
Params: |
| 64 |
Datum = the mapped data type: Use a POD of size 1 for read/write mapping |
| 65 |
and a `const` version thereof for read-only mapping. Other primitive types |
| 66 |
should work, but have not been yet tested. |
| 67 |
*/ |
| 68 |
struct FileMapping(Datum) |
| 69 |
{ |
| 70 |
static assert(__traits(isPOD, Datum) && Datum.sizeof == 1, |
| 71 |
"Not tested with other data types yet. Add new types with care."); |
| 72 |
|
| 73 |
// state { |
| 74 |
/// Handle of underlying file |
| 75 |
private auto handle = invalidHandle; |
| 76 |
/// File mapping object needed on Windows |
| 77 |
version(Windows) private HANDLE fileMappingObject = invalidHandle; |
| 78 |
/// Memory-mapped array |
| 79 |
private Datum[] data; |
| 80 |
/// Name of underlying file, zero-terminated |
| 81 |
private const(char)* name; |
| 82 |
// state } |
| 83 |
|
| 84 |
nothrow: |
| 85 |
|
| 86 |
/** |
| 87 |
Open `filename` and map it in memory. If `Datum` is `const`, opens for |
| 88 |
read-only and maps the content in memory; no error is issued if the file |
| 89 |
does not exist. This makes it easy to treat a non-existing file as empty. |
| 90 |
|
| 91 |
If `Datum` is mutable, opens for read/write (creates file if it does not |
| 92 |
exist) and fails fatally on any error. |
| 93 |
|
| 94 |
Due to quirks in `mmap`, if the file is empty, `handle` is valid but `data` |
| 95 |
is `null`. This state is valid and accounted for. |
| 96 |
|
| 97 |
Params: |
| 98 |
filename = the name of the file to be mapped in memory |
| 99 |
*/ |
| 100 |
this(const char* filename) |
| 101 |
{ |
| 102 |
version (Posix) |
| 103 |
{ |
| 104 |
handle = open(filename, is(Datum == const) ? O_RDONLY : (O_CREAT | O_RDWR), |
| 105 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 106 |
|
| 107 |
if (handle == invalidHandle) |
| 108 |
{ |
| 109 |
static if (is(Datum == const)) |
| 110 |
{ |
| 111 |
// No error, nonexisting file in read mode behaves like an empty file. |
| 112 |
return; |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
fprintf(stderr, "open(\"%s\") failed: %s\n", filename, strerror(errno)); |
| 117 |
exit(1); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
const size = fileSize(handle); |
| 122 |
|
| 123 |
if (size > 0 && size != ulong.max && size <= size_t.max) |
| 124 |
{ |
| 125 |
auto p = mmap(null, cast(size_t) size, is(Datum == const) ? PROT_READ : PROT_WRITE, MAP_SHARED, handle, 0); |
| 126 |
if (p == MAP_FAILED) |
| 127 |
{ |
| 128 |
fprintf(stderr, "mmap(null, %zu) for \"%s\" failed: %s\n", cast(size_t) size, filename, strerror(errno)); |
| 129 |
exit(1); |
| 130 |
} |
| 131 |
// The cast below will always work because it's gated by the `size <= size_t.max` condition. |
| 132 |
data = cast(Datum[]) p[0 .. cast(size_t) size]; |
| 133 |
} |
| 134 |
} |
| 135 |
else version(Windows) |
| 136 |
{ |
| 137 |
static if (is(Datum == const)) |
| 138 |
{ |
| 139 |
enum createFileMode = GENERIC_READ; |
| 140 |
enum openFlags = OPEN_EXISTING; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
enum createFileMode = GENERIC_READ | GENERIC_WRITE; |
| 145 |
enum openFlags = CREATE_ALWAYS; |
| 146 |
} |
| 147 |
|
| 148 |
handle = filename[0 .. strlen(filename)]. |
| 149 |
extendedPathThen!(p => CreateFileW(p.ptr, createFileMode, 0, null, openFlags, FILE_ATTRIBUTE_NORMAL, null)); |
| 150 |
if (handle == invalidHandle) |
| 151 |
{ |
| 152 |
static if (is(Datum == const)) |
| 153 |
{ |
| 154 |
return; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
fprintf(stderr, "CreateFileW() failed for \"%s\": %d\n", filename, GetLastError()); |
| 159 |
exit(1); |
| 160 |
} |
| 161 |
} |
| 162 |
createMapping(filename, fileSize(handle)); |
| 163 |
} |
| 164 |
else static assert(0); |
| 165 |
|
| 166 |
// Save the name for later. Technically there's no need: on Linux one can use readlink on /proc/self/fd/NNN. |
| 167 |
// On BSD and OSX one can use fcntl with F_GETPATH. On Windows one can use GetFileInformationByHandleEx. |
| 168 |
// But just saving the name is simplest, fastest, and most portable... |
| 169 |
const totalNameLength = filename.strlen() + 1; |
| 170 |
auto namex = cast(char*) malloc(totalNameLength); |
| 171 |
if (!namex) |
| 172 |
{ |
| 173 |
fprintf(stderr, "FileMapping: Out of memory."); |
| 174 |
exit(1); |
| 175 |
} |
| 176 |
name = cast(char*) memcpy(namex, filename, totalNameLength); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
Common code factored opportunistically. Windows only. Assumes `handle` is |
| 181 |
already pointing to an opened file. Initializes the `fileMappingObject` |
| 182 |
and `data` members. |
| 183 |
|
| 184 |
Params: |
| 185 |
filename = the file to be mapped |
| 186 |
size = the size of the file in bytes |
| 187 |
*/ |
| 188 |
version(Windows) private void createMapping(const char* filename, ulong size) |
| 189 |
{ |
| 190 |
assert(size <= size_t.max || size == ulong.max); |
| 191 |
assert(handle != invalidHandle); |
| 192 |
assert(data is null); |
| 193 |
assert(fileMappingObject == invalidHandle); |
| 194 |
|
| 195 |
if (size == 0 || size == ulong.max) |
| 196 |
return; |
| 197 |
|
| 198 |
static if (is(Datum == const)) |
| 199 |
{ |
| 200 |
enum fileMappingFlags = PAGE_READONLY; |
| 201 |
enum mapViewFlags = FILE_MAP_READ; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
enum fileMappingFlags = PAGE_READWRITE; |
| 206 |
enum mapViewFlags = FILE_MAP_WRITE; |
| 207 |
} |
| 208 |
|
| 209 |
fileMappingObject = CreateFileMappingW(handle, null, fileMappingFlags, 0, 0, null); |
| 210 |
if (!fileMappingObject) |
| 211 |
{ |
| 212 |
fprintf(stderr, "CreateFileMappingW(%p) failed for %llu bytes of \"%s\": %d\n", |
| 213 |
handle, size, filename, GetLastError()); |
| 214 |
fileMappingObject = invalidHandle; // by convention always use invalidHandle, not null |
| 215 |
exit(1); |
| 216 |
} |
| 217 |
auto p = MapViewOfFile(fileMappingObject, mapViewFlags, 0, 0, 0); |
| 218 |
if (!p) |
| 219 |
{ |
| 220 |
fprintf(stderr, "MapViewOfFile() failed for \"%s\": %d\n", filename, GetLastError()); |
| 221 |
exit(1); |
| 222 |
} |
| 223 |
data = cast(Datum[]) p[0 .. cast(size_t) size]; |
| 224 |
} |
| 225 |
|
| 226 |
// Not copyable or assignable (for now). |
| 227 |
@disable this(const FileMapping!Datum rhs); |
| 228 |
@disable void opAssign(const ref FileMapping!Datum rhs); |
| 229 |
|
| 230 |
/** |
| 231 |
Frees resources associated with this mapping. However, it does not deallocate the name. |
| 232 |
*/ |
| 233 |
~this() pure nothrow |
| 234 |
{ |
| 235 |
if (!active) |
| 236 |
return; |
| 237 |
fakePure({ |
| 238 |
version (Posix) |
| 239 |
{ |
| 240 |
// Cannot call fprintf from inside a destructor, so exiting silently. |
| 241 |
|
| 242 |
if (data.ptr && munmap(cast(void*) data.ptr, data.length) != 0) |
| 243 |
{ |
| 244 |
exit(1); |
| 245 |
} |
| 246 |
data = null; |
| 247 |
if (handle != invalidHandle && .close(handle) != 0) |
| 248 |
{ |
| 249 |
exit(1); |
| 250 |
} |
| 251 |
handle = invalidHandle; |
| 252 |
} |
| 253 |
else version(Windows) |
| 254 |
{ |
| 255 |
if (data.ptr !is null && UnmapViewOfFile(cast(void*) data.ptr) == 0) |
| 256 |
{ |
| 257 |
exit(1); |
| 258 |
} |
| 259 |
data = null; |
| 260 |
if (fileMappingObject != invalidHandle && CloseHandle(fileMappingObject) == 0) |
| 261 |
{ |
| 262 |
exit(1); |
| 263 |
} |
| 264 |
fileMappingObject = invalidHandle; |
| 265 |
if (handle != invalidHandle && CloseHandle(handle) == 0) |
| 266 |
{ |
| 267 |
exit(1); |
| 268 |
} |
| 269 |
handle = invalidHandle; |
| 270 |
} |
| 271 |
else static assert(0); |
| 272 |
}); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
Returns the zero-terminated file name associated with the mapping. Can NOT |
| 277 |
be saved beyond the lifetime of `this`. |
| 278 |
*/ |
| 279 |
private const(char)* filename() const pure @nogc @safe nothrow { return name; } |
| 280 |
|
| 281 |
/** |
| 282 |
Frees resources associated with this mapping. However, it does not deallocate the name. |
| 283 |
Reinitializes `this` as a fresh object that can be reused. |
| 284 |
*/ |
| 285 |
void close() |
| 286 |
{ |
| 287 |
__dtor(); |
| 288 |
handle = invalidHandle; |
| 289 |
version(Windows) fileMappingObject = invalidHandle; |
| 290 |
data = null; |
| 291 |
name = null; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
Deletes the underlying file and frees all resources associated. |
| 296 |
Reinitializes `this` as a fresh object that can be reused. |
| 297 |
|
| 298 |
This function does not abort if the file cannot be deleted, but does print |
| 299 |
a message on `stderr` and returns `false` to the caller. The underlying |
| 300 |
rationale is to give the caller the option to continue execution if |
| 301 |
deleting the file is not important. |
| 302 |
|
| 303 |
Returns: `true` iff the file was successfully deleted. If the file was not |
| 304 |
deleted, prints a message to `stderr` and returns `false`. |
| 305 |
*/ |
| 306 |
static if (!is(Datum == const)) |
| 307 |
bool discard() |
| 308 |
{ |
| 309 |
// Truncate file to zero so unflushed buffers are not flushed unnecessarily. |
| 310 |
resize(0); |
| 311 |
auto deleteme = name; |
| 312 |
close(); |
| 313 |
// In-memory resource freed, now get rid of the underlying temp file. |
| 314 |
version(Posix) |
| 315 |
{ |
| 316 |
if (unlink(deleteme) != 0) |
| 317 |
{ |
| 318 |
fprintf(stderr, "unlink(\"%s\") failed: %s\n", filename, strerror(errno)); |
| 319 |
return false; |
| 320 |
} |
| 321 |
} |
| 322 |
else version(Windows) |
| 323 |
{ |
| 324 |
if (deleteme[0 .. strlen(deleteme)].extendedPathThen!(p => DeleteFileW(p.ptr)) == 0) |
| 325 |
{ |
| 326 |
fprintf(stderr, "DeleteFileW error %d\n", GetLastError()); |
| 327 |
return false; |
| 328 |
} |
| 329 |
} |
| 330 |
else static assert(0); |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
Queries whether `this` is currently associated with a file. |
| 336 |
|
| 337 |
Returns: `true` iff there is an active mapping. |
| 338 |
*/ |
| 339 |
bool active() const pure @nogc nothrow |
| 340 |
{ |
| 341 |
return handle !is invalidHandle; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
Queries the length of the file associated with this mapping. If not |
| 346 |
active, returns 0. |
| 347 |
|
| 348 |
Returns: the length of the file, or 0 if no file associated. |
| 349 |
*/ |
| 350 |
size_t length() const pure @nogc @safe nothrow { return data.length; } |
| 351 |
|
| 352 |
/** |
| 353 |
Get a slice to the contents of the entire file. |
| 354 |
|
| 355 |
Returns: the contents of the file. If not active, returns the `null` slice. |
| 356 |
*/ |
| 357 |
auto opSlice() pure @nogc @safe nothrow { return data; } |
| 358 |
|
| 359 |
/** |
| 360 |
Resizes the file and mapping to the specified `size`. |
| 361 |
|
| 362 |
Params: |
| 363 |
size = new length requested |
| 364 |
*/ |
| 365 |
static if (!is(Datum == const)) |
| 366 |
void resize(size_t size) pure |
| 367 |
{ |
| 368 |
assert(handle != invalidHandle); |
| 369 |
fakePure({ |
| 370 |
version(Posix) |
| 371 |
{ |
| 372 |
if (data.length) |
| 373 |
{ |
| 374 |
assert(data.ptr, "Corrupt memory mapping"); |
| 375 |
// assert(0) here because it would indicate an internal error |
| 376 |
munmap(cast(void*) data.ptr, data.length) == 0 || assert(0); |
| 377 |
data = null; |
| 378 |
} |
| 379 |
if (ftruncate(handle, size) != 0) |
| 380 |
{ |
| 381 |
fprintf(stderr, "ftruncate() failed for \"%s\": %s\n", filename, strerror(errno)); |
| 382 |
exit(1); |
| 383 |
} |
| 384 |
if (size > 0) |
| 385 |
{ |
| 386 |
auto p = mmap(null, size, PROT_WRITE, MAP_SHARED, handle, 0); |
| 387 |
if (cast(ssize_t) p == -1) |
| 388 |
{ |
| 389 |
fprintf(stderr, "mmap() failed for \"%s\": %s\n", filename, strerror(errno)); |
| 390 |
exit(1); |
| 391 |
} |
| 392 |
data = cast(Datum[]) p[0 .. size]; |
| 393 |
} |
| 394 |
} |
| 395 |
else version(Windows) |
| 396 |
{ |
| 397 |
// Per documentation, must unmap first. |
| 398 |
if (data.length > 0 && UnmapViewOfFile(cast(void*) data.ptr) == 0) |
| 399 |
{ |
| 400 |
fprintf(stderr, "UnmapViewOfFile(%p) failed for memory mapping of \"%s\": %d\n", |
| 401 |
data.ptr, filename, GetLastError()); |
| 402 |
exit(1); |
| 403 |
} |
| 404 |
data = null; |
| 405 |
if (fileMappingObject != invalidHandle && CloseHandle(fileMappingObject) == 0) |
| 406 |
{ |
| 407 |
fprintf(stderr, "CloseHandle() failed for memory mapping of \"%s\": %d\n", filename, GetLastError()); |
| 408 |
exit(1); |
| 409 |
} |
| 410 |
fileMappingObject = invalidHandle; |
| 411 |
LARGE_INTEGER biggie; |
| 412 |
biggie.QuadPart = size; |
| 413 |
if (SetFilePointerEx(handle, biggie, null, FILE_BEGIN) == 0 || SetEndOfFile(handle) == 0) |
| 414 |
{ |
| 415 |
fprintf(stderr, "SetFilePointer() failed for \"%s\": %d\n", filename, GetLastError()); |
| 416 |
exit(1); |
| 417 |
} |
| 418 |
createMapping(name, size); |
| 419 |
} |
| 420 |
else static assert(0); |
| 421 |
}); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
Unconditionally and destructively moves the underlying file to `filename`. |
| 426 |
If the operation succeeds, returns true. Upon failure, prints a message to |
| 427 |
`stderr` and returns `false`. In all cases it closes the underlying file. |
| 428 |
|
| 429 |
Params: filename = zero-terminated name of the file to move to. |
| 430 |
|
| 431 |
Returns: `true` iff the operation was successful. |
| 432 |
*/ |
| 433 |
bool moveToFile(const char* filename) |
| 434 |
{ |
| 435 |
assert(name !is null); |
| 436 |
|
| 437 |
// Fetch the name and then set it to `null` so it doesn't get deallocated |
| 438 |
auto oldname = name; |
| 439 |
scope(exit) free(cast(void*) oldname); |
| 440 |
name = null; |
| 441 |
close(); |
| 442 |
|
| 443 |
// Rename the underlying file to the target, no copy necessary. |
| 444 |
version(Posix) |
| 445 |
{ |
| 446 |
if (.rename(oldname, filename) != 0) |
| 447 |
{ |
| 448 |
fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", oldname, filename, strerror(errno)); |
| 449 |
return false; |
| 450 |
} |
| 451 |
} |
| 452 |
else version(Windows) |
| 453 |
{ |
| 454 |
auto r = oldname[0 .. strlen(oldname)].extendedPathThen!( |
| 455 |
p1 => filename[0 .. strlen(filename)].extendedPathThen!(p2 => MoveFileExW(p1.ptr, p2.ptr, MOVEFILE_REPLACE_EXISTING)) |
| 456 |
); |
| 457 |
if (r == 0) |
| 458 |
{ |
| 459 |
fprintf(stderr, "MoveFileExW(\"%s\", \"%s\") failed: %d\n", oldname, filename, GetLastError()); |
| 460 |
return false; |
| 461 |
} |
| 462 |
} |
| 463 |
else static assert(0); |
| 464 |
return true; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
/// Write a file, returning `true` on success. |
| 469 |
extern(D) static bool writeFile(const(char)* name, const void[] data) nothrow |
| 470 |
{ |
| 471 |
version (Posix) |
| 472 |
{ |
| 473 |
int fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, (6 << 6) | (4 << 3) | 4); |
| 474 |
if (fd == -1) |
| 475 |
goto err; |
| 476 |
if (.write(fd, data.ptr, data.length) != data.length) |
| 477 |
goto err2; |
| 478 |
if (close(fd) == -1) |
| 479 |
goto err; |
| 480 |
return true; |
| 481 |
err2: |
| 482 |
close(fd); |
| 483 |
.remove(name); |
| 484 |
err: |
| 485 |
return false; |
| 486 |
} |
| 487 |
else version (Windows) |
| 488 |
{ |
| 489 |
DWORD numwritten; // here because of the gotos |
| 490 |
const nameStr = name[0 .. strlen(name)]; |
| 491 |
// work around Windows file path length limitation |
| 492 |
// (see documentation for extendedPathThen). |
| 493 |
HANDLE h = nameStr.extendedPathThen! |
| 494 |
(p => CreateFileW(p.ptr, |
| 495 |
GENERIC_WRITE, |
| 496 |
0, |
| 497 |
null, |
| 498 |
CREATE_ALWAYS, |
| 499 |
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, |
| 500 |
null)); |
| 501 |
if (h == INVALID_HANDLE_VALUE) |
| 502 |
goto err; |
| 503 |
|
| 504 |
if (WriteFile(h, data.ptr, cast(DWORD)data.length, &numwritten, null) != TRUE) |
| 505 |
goto err2; |
| 506 |
if (numwritten != data.length) |
| 507 |
goto err2; |
| 508 |
if (!CloseHandle(h)) |
| 509 |
goto err; |
| 510 |
return true; |
| 511 |
err2: |
| 512 |
CloseHandle(h); |
| 513 |
nameStr.extendedPathThen!(p => DeleteFileW(p.ptr)); |
| 514 |
err: |
| 515 |
return false; |
| 516 |
} |
| 517 |
else |
| 518 |
{ |
| 519 |
static assert(0); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
/// Touch a file to current date |
| 524 |
bool touchFile(const char* namez) |
| 525 |
{ |
| 526 |
version (Windows) |
| 527 |
{ |
| 528 |
FILETIME ft = void; |
| 529 |
SYSTEMTIME st = void; |
| 530 |
GetSystemTime(&st); |
| 531 |
SystemTimeToFileTime(&st, &ft); |
| 532 |
|
| 533 |
// get handle to file |
| 534 |
HANDLE h = namez[0 .. namez.strlen()].extendedPathThen!(p => CreateFile(p.ptr, |
| 535 |
FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 536 |
null, OPEN_EXISTING, |
| 537 |
FILE_ATTRIBUTE_NORMAL, null)); |
| 538 |
if (h == INVALID_HANDLE_VALUE) |
| 539 |
return false; |
| 540 |
|
| 541 |
const f = SetFileTime(h, null, null, &ft); // set last write time |
| 542 |
|
| 543 |
if (!CloseHandle(h)) |
| 544 |
return false; |
| 545 |
|
| 546 |
return f != 0; |
| 547 |
} |
| 548 |
else version (Posix) |
| 549 |
{ |
| 550 |
return utime(namez, null) == 0; |
| 551 |
} |
| 552 |
else |
| 553 |
static assert(0); |
| 554 |
} |
| 555 |
|
| 556 |
// Feel free to make these public if used elsewhere. |
| 557 |
/** |
| 558 |
Size of a file in bytes. |
| 559 |
Params: fd = file handle |
| 560 |
Returns: file size in bytes, or `ulong.max` on any error. |
| 561 |
*/ |
| 562 |
version (Posix) |
| 563 |
{ |
| 564 |
private ulong fileSize(int fd) |
| 565 |
{ |
| 566 |
stat_t buf; |
| 567 |
if (fstat(fd, &buf) == 0) |
| 568 |
return buf.st_size; |
| 569 |
return ulong.max; |
| 570 |
} |
| 571 |
} |
| 572 |
else version (Windows) |
| 573 |
{ |
| 574 |
/// Ditto |
| 575 |
private ulong fileSize(HANDLE fd) |
| 576 |
{ |
| 577 |
ulong result; |
| 578 |
if (GetFileSizeEx(fd, cast(LARGE_INTEGER*) &result)) |
| 579 |
return result; |
| 580 |
return ulong.max; |
| 581 |
} |
| 582 |
} |
| 583 |
else |
| 584 |
static assert(0); |
| 585 |
|
| 586 |
/** |
| 587 |
Runs a non-pure function or delegate as pure code. Use with caution. |
| 588 |
|
| 589 |
Params: |
| 590 |
fun = the delegate to run, usually inlined: `fakePure({ ... });` |
| 591 |
|
| 592 |
Returns: whatever `fun` returns. |
| 593 |
*/ |
| 594 |
private auto ref fakePure(F)(scope F fun) pure |
| 595 |
{ |
| 596 |
mixin("alias PureFun = " ~ F.stringof ~ " pure;"); |
| 597 |
return (cast(PureFun) fun)(); |
| 598 |
} |
| 599 |
|
| 600 |
/*********************************** |
| 601 |
* Recursively search all the directories and files under dir_path |
| 602 |
* for files that match one of the extensions in exts[]. |
| 603 |
* Pass the matches to sink. |
| 604 |
* Params: |
| 605 |
* dir_path = root of directories to search |
| 606 |
* exts = array of filename extensions to match |
| 607 |
* recurse = go into subdirectories |
| 608 |
* filenameSink = accepts the resulting matches |
| 609 |
* Returns: |
| 610 |
* true for failed to open the directory |
| 611 |
*/ |
| 612 |
bool findFiles(const char* dir_path, const char[][] exts, bool recurse, void delegate(const(char)[]) nothrow filenameSink) |
| 613 |
{ |
| 614 |
enum log = false; |
| 615 |
if (log) printf("findFiles() dir_path: %s\n", dir_path); |
| 616 |
version (Windows) |
| 617 |
{ |
| 618 |
debug |
| 619 |
enum BufLength = 10; // trigger any reallocation bugs |
| 620 |
else |
| 621 |
enum BufLength = 100; |
| 622 |
char[BufLength + 1] buf = void; |
| 623 |
char* fullPath = buf.ptr; |
| 624 |
size_t fullPathLength = BufLength; |
| 625 |
|
| 626 |
// fullPath = dir_path \ *.* |
| 627 |
const dir_pathLength = strlen(dir_path); |
| 628 |
auto count = dir_pathLength + 1 + 3; |
| 629 |
if (count > fullPathLength) |
| 630 |
{ |
| 631 |
fullPathLength = count; |
| 632 |
fullPath = cast(char*)Mem.xrealloc_noscan(fullPath == buf.ptr ? null : fullPath, fullPathLength + 1); |
| 633 |
} |
| 634 |
memcpy(fullPath, dir_path, dir_pathLength); |
| 635 |
strcpy(fullPath + dir_pathLength, "\\*.*".ptr); |
| 636 |
|
| 637 |
if (log) printf("fullPath: %s\n", fullPath); |
| 638 |
|
| 639 |
WIN32_FIND_DATAW ffd = void; |
| 640 |
HANDLE hFind = fullPath[0 .. strlen(fullPath)].extendedPathThen!(p => FindFirstFileW(p.ptr, &ffd)); |
| 641 |
if (hFind == INVALID_HANDLE_VALUE) |
| 642 |
return true; |
| 643 |
|
| 644 |
do |
| 645 |
{ |
| 646 |
if (log) wprintf("ffd.cFileName: %s\n", ffd.cFileName.ptr); |
| 647 |
if (ffd.cFileName[0] == 0) |
| 648 |
continue; // ignore |
| 649 |
|
| 650 |
if (ffd.cFileName[0] == '.') |
| 651 |
continue; // ignore files that start with a ., also ignore . and .. directories |
| 652 |
|
| 653 |
const(char)[] name = toNarrowStringz(ffd.cFileName[0 .. wcslen(ffd.cFileName.ptr)], null); |
| 654 |
if (log) printf("name: %s\n", name.ptr); |
| 655 |
|
| 656 |
// fullPath = dir_path \ name.ptr |
| 657 |
count = dir_pathLength + 1 + name.length; |
| 658 |
if (count > fullPathLength) |
| 659 |
{ |
| 660 |
fullPathLength = count; |
| 661 |
fullPath = cast(char*)Mem.xrealloc_noscan(fullPath == buf.ptr ? null : fullPath, fullPathLength + 1); |
| 662 |
} |
| 663 |
strcpy(fullPath + dir_pathLength + 1, name.ptr); |
| 664 |
|
| 665 |
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 666 |
{ |
| 667 |
if (recurse) |
| 668 |
findFiles(fullPath, exts, recurse, filenameSink); |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
const(char)[] nameExt = FileName.ext(name); |
| 673 |
foreach (ext; exts[]) |
| 674 |
{ |
| 675 |
if (nameExt == ext) |
| 676 |
{ |
| 677 |
if (log) printf("adding %s\n", fullPath); |
| 678 |
filenameSink(fullPath[0 .. count]); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
mem.xfree(cast(void*)name.ptr); |
| 683 |
|
| 684 |
} while (FindNextFileW(hFind, &ffd) != 0); |
| 685 |
|
| 686 |
if (fullPath != buf.ptr) |
| 687 |
mem.xfree(fullPath); |
| 688 |
FindClose(hFind); |
| 689 |
if (log) printf("findFiles() exit\n"); |
| 690 |
return false; |
| 691 |
} |
| 692 |
else version (Posix) |
| 693 |
{ |
| 694 |
DIR* dir = opendir(dir_path); |
| 695 |
if (!dir) |
| 696 |
return true; |
| 697 |
|
| 698 |
debug |
| 699 |
enum BufLength = 10; // trigger any reallocation bugs |
| 700 |
else |
| 701 |
enum BufLength = 100; |
| 702 |
char[BufLength + 1] buf = void; |
| 703 |
char* fullPath = buf.ptr; |
| 704 |
size_t fullPathLength = BufLength; |
| 705 |
|
| 706 |
dirent* entry; |
| 707 |
while ((entry = readdir(dir)) != null) |
| 708 |
{ |
| 709 |
//printf("entry: %s\n", entry.d_name.ptr); |
| 710 |
if (entry.d_name[0] == '.') |
| 711 |
continue; // ignore files that start with a . |
| 712 |
|
| 713 |
// fullPath = dir_path / entry.d_name.ptr |
| 714 |
const dir_pathLength = strlen(dir_path); |
| 715 |
const count = dir_pathLength + 1 + strlen(entry.d_name.ptr); |
| 716 |
if (count > fullPathLength) |
| 717 |
{ |
| 718 |
fullPathLength = count; |
| 719 |
fullPath = cast(char*)Mem.xrealloc_noscan(fullPath == buf.ptr ? null : fullPath, fullPathLength + 1); |
| 720 |
} |
| 721 |
memcpy(fullPath, dir_path, dir_pathLength); |
| 722 |
fullPath[dir_pathLength] = '/'; |
| 723 |
strcpy(fullPath + dir_pathLength + 1, entry.d_name.ptr); |
| 724 |
|
| 725 |
stat_t statbuf; |
| 726 |
if (lstat(fullPath, &statbuf) == -1) |
| 727 |
continue; |
| 728 |
|
| 729 |
const(char)[] name = entry.d_name.ptr[0 .. strlen(entry.d_name.ptr)]; // convert to D string |
| 730 |
if (!name.length) |
| 731 |
continue; // ignore |
| 732 |
|
| 733 |
if (S_ISDIR(statbuf.st_mode)) |
| 734 |
{ |
| 735 |
if (recurse && !(name == "." || name == "..")) |
| 736 |
findFiles(fullPath, exts, recurse, filenameSink); |
| 737 |
} |
| 738 |
else if (S_ISREG(statbuf.st_mode)) |
| 739 |
{ |
| 740 |
foreach (ext; exts) |
| 741 |
{ |
| 742 |
if (FileName.ext(name) == ext) |
| 743 |
{ |
| 744 |
//printf("%s\n", fullPath); |
| 745 |
filenameSink(fullPath[0 .. count]); |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
if (fullPath != buf.ptr) |
| 752 |
mem.xfree(fullPath); |
| 753 |
closedir(dir); |
| 754 |
return false; |
| 755 |
} |
| 756 |
else |
| 757 |
static assert(0); |
| 758 |
} |