Files v0.1.0

Source
Opening, reading, writing and flushing files through handles.

GENERIC_READ

pub const GENERIC_READ: uint32 = 0x80000000;

GENERIC_READ: the handle may read.

Source

GENERIC_WRITE

pub const GENERIC_WRITE: uint32 = 0x40000000;

GENERIC_WRITE: the handle may write.

Source

FILE_APPEND_DATA

pub const FILE_APPEND_DATA: uint32 = 0x00000004;

FILE_APPEND_DATA: the handle may append, and only append. The access right that makes an append-only handle: every write lands at the end whatever the file pointer says, which GENERIC_WRITE does not promise.

Source

FILE_SHARE_READ

pub const FILE_SHARE_READ: uint32 = 0x00000001;

FILE_SHARE_READ: other openers may read while this handle is open. Windows denies sharing by default, which is the opposite of every Unix. A file opened without this cannot be opened for reading by anything else, including the same process.

Source

FILE_SHARE_WRITE

pub const FILE_SHARE_WRITE: uint32 = 0x00000002;

FILE_SHARE_WRITE: other openers may write while this handle is open.

Source

FILE_SHARE_DELETE

pub const FILE_SHARE_DELETE: uint32 = 0x00000004;

FILE_SHARE_DELETE: the file may be renamed or deleted while this handle is open.

Source

FILE_ATTRIBUTE_NORMAL

pub const FILE_ATTRIBUTE_NORMAL: uint32 = 0x00000080;

FILE_ATTRIBUTE_NORMAL: no attributes beyond being an ordinary file.

Source

FILE_FLAG_BACKUP_SEMANTICS

pub const FILE_FLAG_BACKUP_SEMANTICS: uint32 = 0x02000000;

FILE_FLAG_BACKUP_SEMANTICS: let CreateFileW open a directory. Without it a directory cannot be opened at all, which makes this the flag every metadata query needs.

Source

FILE_FLAG_OPEN_REPARSE_POINT

pub const FILE_FLAG_OPEN_REPARSE_POINT: uint32 = 0x00200000;

FILE_FLAG_OPEN_REPARSE_POINT: open the symbolic link itself rather than what it points at.

Source

FILE_FLAG_DELETE_ON_CLOSE

pub const FILE_FLAG_DELETE_ON_CLOSE: uint32 = 0x04000000;

FILE_FLAG_DELETE_ON_CLOSE: remove the file when its last handle closes.

Source

FILE_FLAG_WRITE_THROUGH

pub const FILE_FLAG_WRITE_THROUGH: uint32 = 0x80000000;

FILE_FLAG_WRITE_THROUGH: writes reach the device before the call returns.

Source

CreateFileW

pub extern func CreateFileW(
    fileName: *char16,
    desiredAccess: uint32,
    shareMode: uint32,
    securityAttributes: *var opaque,
    creationDisposition: uint32,
    flagsAndAttributes: uint32,
    templateFile: *var opaque
) -> *var opaque

pub extern func CreateFileW(
    fileName: *char16,
    desiredAccess: uint32,
    shareMode: uint32,
    securityAttributes: *opaque,
    creationDisposition: uint32,
    flagsAndAttributes: uint32,
    templateFile: *opaque
) -> *opaque

Creates or opens a file or device, taking a UTF-16 path. Returns InvalidHandleValue on failure, not null. The share mode is not a default: passing zero denies every other opener, which is Windows' behaviour and not most callers' intent.

OverloadSummary
pub extern func CreateFileW(fileName: *char16, desiredAccess: uint32, shareMode: uint32, securityAttributes: *var opaque, creationDisposition: uint32, flagsAndAttributes: uint32, templateFile: *var opaque) -> *var opaqueCreates or opens a file or device, taking a UTF-16 path.
pub extern func CreateFileW(fileName: *char16, desiredAccess: uint32, shareMode: uint32, securityAttributes: *opaque, creationDisposition: uint32, flagsAndAttributes: uint32, templateFile: *opaque) -> *opaqueCreates or opens a file or I/O device.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew

Source

Parameters

NameTypeDescription
fileName*char16the path, as a null-terminated string
desiredAccessuint32the access rights wanted, such as GENERIC_READ
shareModeuint32what other openers may do meanwhile; zero denies them everything
securityAttributes*var opaque / *opaquea security descriptor, or null for the default
creationDispositionuint32one of the CreationDisposition values
flagsAndAttributesuint32the file attributes and flags
templateFile*var opaque / *opaquea template whose attributes to copy, or null

Returns: the new handle, or InvalidHandleValue — which is not null

GetFileInformationByHandle

pub extern func GetFileInformationByHandle(
    file: *var opaque,
    fileInformation: *var opaque
) -> int32

Fills a BY_HANDLE_FILE_INFORMATION record for an open handle. The record is 52 bytes: attributes at offset 0 (4 bytes), the creation, last-access and last-write FILETIMEs at 4, 12 and 20 (8 bytes each), the volume serial number at 28, the size's high and low halves at 32 and 36, the link count at 40, and the file index's halves at 44 and 48.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle

Source

Parameters

NameTypeDescription
file*var opaquean open file handle
fileInformation*var opaquereceives the file's information record

Returns: non-zero on success

SetFileTime

pub extern func SetFileTime(
    file: *var opaque,
    creationTime: *FileTime,
    lastAccessTime: *FileTime,
    lastWriteTime: *FileTime
) -> int32

Sets the creation, last-access and last-write times of an open handle. Each pointer may be null to leave that time alone.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime

Source

Parameters

NameTypeDescription
file*var opaquean open file handle
creationTime*FileTimethe creation time to set, or null to leave it
lastAccessTime*FileTimethe access time to set, or null to leave it
lastWriteTime*FileTimethe write time to set, or null to leave it

Returns: non-zero on success

GetFinalPathNameByHandleW

pub extern func GetFinalPathNameByHandleW(
    file: *var opaque,
    filePath: *var char16,
    filePathLength: uint32,
    flags: uint32
) -> uint32

Writes the final, resolved path of an open handle: symbolic links followed, short names expanded, case as the filesystem stores it. Returns the length written, without the terminator, or the required length when the buffer is too small, or zero on failure. VOLUME_NAME_DOS (0) spells the answer with a drive letter where the file has one.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew

Source

Parameters

NameTypeDescription
file*var opaquean open file handle
filePath*var char16receives the resolved path
filePathLengthuint32how many units filePath holds
flagsuint32the call's flag bits, combined with |

Returns: the length written, or zero on failure

FlushFileBuffers

pub extern func FlushFileBuffers(file: *var opaque) -> int32

Flushes a file handle's writes to the device. Returns zero on failure. This is what makes a write durable; FILE_FLAG_WRITE_THROUGH is the alternative that does it on every write instead.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers

Source

Parameters

NameTypeDescription
file*var opaquean open file handle

Returns: non-zero on success

SetEndOfFile

pub extern func SetEndOfFile(file: *var opaque) -> int32

Sets the size of a file to the handle's current position.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setendoffile

Source

Parameters

NameTypeDescription
file*var opaquean open file handle

Returns: non-zero on success

CloseHandle

pub extern func CloseHandle(handle: *opaque) -> bool32

Closes an open object handle.

See also: https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle

Source

Parameters

NameTypeDescription
handle*opaquethe handle to act on

Returns: non-zero on success, and zero on failure with the reason in GetLastError

CreateFileA

pub extern func CreateFileA(
    fileName: *char8,
    desiredAccess: uint32,
    shareMode: uint32,
    securityAttributes: *opaque,
    creationDisposition: uint32,
    flagsAndAttributes: uint32,
    templateFile: *opaque
) -> *opaque

Creates or opens a file or I/O device.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea

Source

Parameters

NameTypeDescription
fileName*char8the path, as a null-terminated string
desiredAccessuint32the access rights wanted, such as GENERIC_READ
shareModeuint32what other openers may do meanwhile; zero denies them everything
securityAttributes*opaquea security descriptor, or null for the default
creationDispositionuint32one of the CreationDisposition values
flagsAndAttributesuint32the file attributes and flags
templateFile*opaquea template whose attributes to copy, or null

Returns: the new handle, or InvalidHandleValue — which is not null

GetFileSizeEx

pub extern func GetFileSizeEx(
    file: *opaque,
    size: *int64
) -> bool32

Retrieves the size of the specified file.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfilesizeex

Source

Parameters

NameTypeDescription
file*opaquean open file handle
size*int64a size in bytes

Returns: non-zero on success, and zero on failure with the reason in GetLastError

ReadFile

pub extern func ReadFile(
    file: *opaque,
    buffer: *opaque,
    numberOfBytesToRead: uint32,
    numberOfBytesRead: *uint32,
    overlapped: *opaque
) -> bool32

Reads data from the specified file or input/output (I/O) device.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile

Source

Parameters

NameTypeDescription
file*opaquean open file handle
buffer*opaquethe bytes
numberOfBytesToReaduint32how many bytes to ask for
numberOfBytesRead*uint32receives how many were read
overlapped*opaquean overlapped record, or null for a synchronous call

Returns: non-zero on success, and zero on failure with the reason in GetLastError

SetFilePointerEx

pub extern func SetFilePointerEx(
    file: *opaque,
    distanceToMov: int64,
    newFilePointer: *int64,
    moveMethod: uint32
) -> bool32

Moves the file pointer of the specified file.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointerex

Source

Parameters

NameTypeDescription
file*opaquean open file handle
distanceToMovint64how far to move, interpreted by moveMethod
newFilePointer*int64receives the resulting offset, or null
moveMethoduint32where the move is measured from

Returns: non-zero on success, and zero on failure with the reason in GetLastError

WriteFile

pub extern func WriteFile(
    file: *opaque,
    buffer: *opaque,
    bytesToWrite: uint32,
    bytesWritten: *uint32,
    overlapped: *opaque
) -> bool32

Writes data to the specified file or input/output (I/O) device.

See also: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile

Source

Parameters

NameTypeDescription
file*opaquean open file handle
buffer*opaquethe bytes
bytesToWriteuint32how many bytes to offer
bytesWritten*uint32receives how many were written
overlapped*opaquean overlapped record, or null for a synchronous call

Returns: non-zero on success, and zero on failure with the reason in GetLastError