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(istring 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
istring 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

O_CLOEXEC
auto O_CLOEXEC;
Undocumented in source.
read
alias read = Device.read
Undocumented in source.
write
alias write = Device.write
Undocumented in source.
IOException
class IOException

Exception class thrown on errors.

Style
struct Style

Fits into 32 bits ...

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

Read an existing file.

ReadShared
Style ReadShared;

Read an existing file.

WriteExisting
Style WriteExisting;

Write on an existing file. Do not create.

WriteCreate
Style WriteCreate;

Write on a clean file. Create if necessary.

ReadWriteAppending
Style ReadWriteAppending;

Read from the beginning, append at the end of the file.

WriteAppending
Style WriteAppending;

Write at the end of the file.

ReadWriteExisting
Style ReadWriteExisting;

Read and write an existing file.

ReadWriteCreate
Style ReadWriteCreate;

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

ReadWriteOpen
Style ReadWriteOpen;

Read and Write. Use existing file if present.

style
Style style()

Return the Style used for this file.

toString
istring toString()

Return the path used by this file.

path
cstring path()
get
void[] get(cstring path)

Convenience function to return the content of a file.

get
void[] get(cstring path, void[] dst)

Convenience function to return the content of a file.

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

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

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

Convenience function to append content to a file.

open
bool open(cstring path, Style style, int addflags, int access)

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

open
void open(cstring path, Style style)

Open a file with the provided style.

setFileHandle
void setFileHandle(int fd)

Wraps the already open file descriptor into a File instance.

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()

Return the current file position.

length
long length()

Return the total length of this file.

sync
void sync()

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

error
void error(int error_code, istring func_name, istring msg, istring file, long line)

Throw a potentially reusable IOException, with the provided message, function name and error code.

error
alias error = Conduit.error

Throw an IOException, with the provided message.

error
alias error = Device.error

Throw an IOException noting the last error.

Meta