Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.common.file

File utilities.
Functions and objects dedicated to file I/O and management. TODO: Move here artifacts from places such as root/ so both the frontend and the backend have access to them.
Authors:
struct FileMapping(Datum);
Encapsulated management of a memory-mapped file.
Parameters:
Datum the mapped data type: Use a POD of size 1 for read/write mapping and a const version thereof for read-only mapping. Other primitive types should work, but have not been yet tested.
this(const char* filename);
Open filename and map it in memory. If Datum is const, opens for read-only and maps the content in memory; no error is issued if the file does not exist. This makes it easy to treat a non-existing file as empty.
If Datum is mutable, opens for read/write (creates file if it does not exist) and fails fatally on any error.
Due to quirks in mmap, if the file is empty, handle is valid but data is null. This state is valid and accounted for.
Parameters:
char* filename the name of the file to be mapped in memory
void close();
Frees resources associated with this mapping. However, it does not deallocate the name. Reinitializes this as a fresh object that can be reused.
bool discard();
Deletes the underlying file and frees all resources associated. Reinitializes this as a fresh object that can be reused.
This function does not abort if the file cannot be deleted, but does print a message on stderr and returns false to the caller. The underlying rationale is to give the caller the option to continue execution if deleting the file is not important.
Returns:
true iff the file was successfully deleted. If the file was not deleted, prints a message to stderr and returns false.
const pure nothrow @nogc bool active();
Queries whether this is currently associated with a file.
Returns:
true iff there is an active mapping.
const pure nothrow @nogc @safe size_t length();
Queries the length of the file associated with this mapping. If not active, returns 0.
Returns:
the length of the file, or 0 if no file associated.
pure nothrow @nogc @safe auto opSlice();
Get a slice to the contents of the entire file.
Returns:
the contents of the file. If not active, returns the null slice.
pure void resize(size_t size);
Resizes the file and mapping to the specified size.
Parameters:
size_t size new length requested
bool moveToFile(const char* filename);
Unconditionally and destructively moves the underlying file to filename. If the operation succeeds, returns true. Upon failure, prints a message to stderr and returns false. In all cases it closes the underlying file.
Parameters:
char* filename zero-terminated name of the file to move to.
Returns:
true iff the operation was successful.
static nothrow bool writeFile(const(char)* name, const void[] data);
Write a file, returning true on success.
nothrow bool touchFile(const char* namez);
Touch a file to current date