TempFile

The TempFile class aims to provide a safe way of creating and destroying temporary files. The TempFile class will automatically close temporary files when the object is destroyed, so it is recommended that you make appropriate use of scoped destruction.

Temporary files can be created with one of several styles, much like normal Files. TempFile styles have the following properties:

  • Transience: this determines whether the file should be destroyed as soon as it is closed (transient,) or continue to persist even after the application has terminated (permanent.)

Eventually, this will be expanded to give you greater control over the temporary file's properties.

For the typical use-case (creating a file to temporarily store data too large to fit into memory,) the following is sufficient:

{
    scope temp = new TempFile;

    // Use temp as a normal conduit; it will be automatically closed when
    // it goes out of scope.
}

Important: It is recommended that you do not use files created by this class to store sensitive information. There are several known issues with the current implementation that could allow an attacker to access the contents of these temporary files.

Todo: Detail security properties and guarantees.

Constructors

this
this(TempStyle style)
this
this(const(char)[] prefix, TempStyle style)

Members

Enums

Transience
enum Transience

This enumeration is used to control whether the temporary file should persist after the TempFile object has been destroyed.

Functions

detach
void detach()
Undocumented in source. Be warned that the author may not have intended to support it.
tempStyle
TempStyle tempStyle()

Indicates the style that this TempFile was created with.

Static functions

tempPath
char[] tempPath()

Returns the path to the directory where temporary files will be created. The returned path is safe to mutate.

tempPath
const(char)[] tempPath()

Returns the path to the directory where temporary files will be created. The returned path is safe to mutate.

Static variables

Permanent
TempStyle Permanent;

TempStyle for creating a permanent temporary file that only the current user can access.

Transient
TempStyle Transient;

TempStyle for creating a transient temporary file that only the current user can access.

Structs

TempStyle
struct TempStyle

This structure is used to determine how the temporary files should be opened and used.

Inherited Members

From File

read
alias read = Device.read
Undocumented in source.
write
alias write = Device.write
Undocumented in source.
Style
struct Style

Fits into 32 bits ...

Access
enum Access
Open
enum Open
Share
enum Share
Cache
enum Cache
ReadExisting
enum Style ReadExisting;

Read an existing file.

ReadShared
enum Style ReadShared;

Read an existing file.

WriteExisting
enum Style WriteExisting;

Write on an existing file. Do not create.

WriteCreate
enum Style WriteCreate;

Write on a clean file. Create if necessary.

WriteAppending
enum Style WriteAppending;

Write at the end of the file.

ReadWriteExisting
enum Style ReadWriteExisting;

Read and write an existing file.

ReadWriteCreate
enum Style ReadWriteCreate;

Read & write on a clean file. Create if necessary.

ReadWriteOpen
enum Style ReadWriteOpen;

Read and Write. Use existing file if present.

style
Style style [@property getter]

Return the Style used for this file.

toString
immutable(char)[] toString()

Return the path used by this file.

get
void[] get(const(char)[] path, void[] dst)

Convenience function to return the content of a file. Returns a slice of the provided output buffer, where that has sufficient capacity, and allocates from the heap where the file content is larger.

set
void set(const(char)[] path, const(void)[] content)

Convenience function to set file content and length to reflect the given array.

append
void append(const(char)[] path, const(void)[] content)

Convenience function to append content to a file.

open
bool open(const(char)[] path, Style style, DWORD addattr)

Low level open for sub-classes that need to apply specific attributes.

open
void open(const(char[]) path, Style style)

Open a file with the provided style.

truncate
void truncate()

Set the file size to be that of the current seek position. The file must be writable for this to succeed.

truncate
void truncate(long size)

Set the file size to be the specified length. The file must be writable for this to succeed.

seek
long seek(long offset, Anchor anchor)

Set the file seek position to the specified offset from the given anchor.

position
long position [@property getter]

Return the current file position.

length
long length [@property getter]

Return the total length of this file.

sync
void sync()

Instructs the OS to flush it's internal buffers to the disk device.

open
bool open(const(char[]) path, Style style, int addflags, int access)

Low level open for sub-classes that need to apply specific attributes.

open
void open(const(char[]) path, Style style)

Open a file with the provided style.

truncate
void truncate()

Set the file size to be that of the current seek position. The file must be writable for this to succeed.

truncate
void truncate(long size)

Set the file size to be the specified length. The file must be writable for this to succeed.

seek
long seek(long offset, Anchor anchor)

Set the file seek position to the specified offset from the given anchor.

position
long position [@property getter]

Return the current file position.

length
long length [@property getter]

Return the total length of this file.

sync
void sync()

Instructs the OS to flush it's internal buffers to the disk device.

Meta