780 lines
29 KiB
Ziran
780 lines
29 KiB
Ziran
#import "c_string"
|
|
|
|
// Positional file I/O for native Linux, Android, the web, and Windows. A
|
|
// caller-owned byte slice makes the buffer lifetime and exact archive
|
|
// encoding explicit.
|
|
#scope_file
|
|
libc :: #system_library "libc";
|
|
#if !#defined(_WIN32) {
|
|
ReadLinkRaw :: (path: *u8, output: *u8, capacity: usize) -> isize #foreign libc "readlink";
|
|
}
|
|
|
|
// Native 64-bit directory records. Darwin includes a name length before
|
|
// the kind; Linux places kind immediately after the record length.
|
|
DirectoryMetadata :: struct {
|
|
inode: u64
|
|
offset: s64
|
|
record_length: u16
|
|
#if #defined(__APPLE__) {
|
|
name_length: u16
|
|
}
|
|
kind: u8
|
|
#if #defined(__APPLE__) {
|
|
name: [1024]u8
|
|
} else {
|
|
name: [256]u8
|
|
}
|
|
}
|
|
|
|
#if #defined(_WIN32) {
|
|
// Win32 stands in for the POSIX calls below, with the same signatures,
|
|
// so each public operation keeps one body. Paths cross as UTF-16, and
|
|
// files open with every share mode, so an open file can itself be
|
|
// renamed or removed as on POSIX. Unlike POSIX, a rename cannot replace
|
|
// a destination that is still open. A kernel handle is a 32-bit value
|
|
// on every Windows ABI, so FileHandle.fd holds it and widens by sign
|
|
// extension.
|
|
kernel32 :: #system_library "kernel32";
|
|
WideFromUtf8Raw :: (code_page: u32, flags: u32, input: *u8, input_count: s32,
|
|
output: *u16, output_count: s32) -> s32 #foreign kernel32 "MultiByteToWideChar";
|
|
Utf8FromWideRaw :: (code_page: u32, flags: u32, input: *u16, input_count: s32,
|
|
output: *u8, output_count: s32, fallback: *u8,
|
|
used_fallback: *s32) -> s32 #foreign kernel32 "WideCharToMultiByte";
|
|
CreateFileRaw :: (path: *u16, access: u32, share: u32, security: *void,
|
|
disposition: u32, attributes: u32, template: *void) -> *void #foreign kernel32 "CreateFileW";
|
|
CloseHandleRaw :: (handle: *void) -> s32 #foreign kernel32 "CloseHandle";
|
|
ReadFileRaw :: (handle: *void, output: *u8, count: u32, done: *u32,
|
|
overlapped: *Overlapped) -> s32 #foreign kernel32 "ReadFile";
|
|
WriteFileRaw :: (handle: *void, input: *u8, count: u32, done: *u32,
|
|
overlapped: *Overlapped) -> s32 #foreign kernel32 "WriteFile";
|
|
SetFilePointerRaw :: (handle: *void, distance: s64, position: *s64,
|
|
method: u32) -> s32 #foreign kernel32 "SetFilePointerEx";
|
|
FlushFileRaw :: (handle: *void) -> s32 #foreign kernel32 "FlushFileBuffers";
|
|
LastErrorRaw :: () -> u32 #foreign kernel32 "GetLastError";
|
|
DeleteFileRaw :: (path: *u16) -> s32 #foreign kernel32 "DeleteFileW";
|
|
CreateDirectoryRaw :: (path: *u16, security: *void) -> s32 #foreign kernel32 "CreateDirectoryW";
|
|
MoveFileRaw :: (source: *u16, destination: *u16, flags: u32) -> s32 #foreign kernel32 "MoveFileExW";
|
|
HardLinkRaw :: (link: *u16, existing: *u16, security: *void) -> s32 #foreign kernel32 "CreateHardLinkW";
|
|
AttributesRaw :: (path: *u16) -> u32 #foreign kernel32 "GetFileAttributesW";
|
|
FindFirstRaw :: (pattern: *u16, data: *FindData) -> *void #foreign kernel32 "FindFirstFileW";
|
|
FindNextRaw :: (search: *void, data: *FindData) -> s32 #foreign kernel32 "FindNextFileW";
|
|
FindCloseRaw :: (search: *void) -> s32 #foreign kernel32 "FindClose";
|
|
AllocateRaw :: (count: usize, size: usize) -> *void #foreign libc "calloc";
|
|
FreeRaw :: (memory: *void) #foreign libc "free";
|
|
CrtErrnoRaw :: () -> *s32 #foreign libc "_errno";
|
|
|
|
Overlapped :: struct {
|
|
internal: usize
|
|
internal_high: usize
|
|
offset: u32
|
|
offset_high: u32
|
|
event: *void
|
|
}
|
|
|
|
FindData :: struct {
|
|
attributes: u32
|
|
times: [6]u32
|
|
size_high: u32
|
|
size_low: u32
|
|
reserved: [2]u32
|
|
name: [260]u16
|
|
short_name: [14]u16
|
|
}
|
|
|
|
DirectorySearch :: struct {
|
|
handle: *void
|
|
pending: bool
|
|
data: FindData
|
|
entry: DirectoryMetadata
|
|
}
|
|
|
|
// The POSIX open flags that this module passes, decoded below.
|
|
OpenWriteOnly :: 1;
|
|
OpenReadWrite :: 2;
|
|
OpenCreate :: 64;
|
|
OpenExclusive :: 128;
|
|
OpenTruncate :: 512;
|
|
OpenDirectoryOnly :: 65536;
|
|
OpenPathOnly :: 2097152;
|
|
AttributeDirectory :: 16;
|
|
MissingAttributes :: 0xffffffff;
|
|
ErrorHandleEof :: 38;
|
|
ErrorBrokenPipe :: 109;
|
|
|
|
// Invalid UTF-8 fails rather than naming a different file.
|
|
WidePath :: (path: *u8, output: []u16) -> bool {
|
|
return WideFromUtf8Raw(65001, 8, path, -1, *output[0],
|
|
cast(s32)output.count) > 0
|
|
}
|
|
|
|
HandleOf :: (fd: s32) -> *void {
|
|
return cast(*void)cast(isize)fd
|
|
}
|
|
|
|
// POSIX callers read why a call failed from errno, and Win32 reports it
|
|
// through GetLastError. A missing file or folder becomes ENOENT, so a
|
|
// document that does not exist yet is not taken for a failure.
|
|
Failed :: () -> s32 {
|
|
error := LastErrorRaw()
|
|
code: s32 = 5
|
|
if error == cast(u32)2 || error == cast(u32)3 { code = 2 }
|
|
if error == cast(u32)5 { code = 13 }
|
|
if error == cast(u32)80 || error == cast(u32)183 { code = 17 }
|
|
CrtErrnoRaw().* = code
|
|
return -1
|
|
}
|
|
|
|
Status :: (succeeded: s32) -> s32 {
|
|
if succeeded != 0 { return 0 }
|
|
return Failed()
|
|
}
|
|
|
|
OpenRaw :: (path: *u8, flags: s32, mode: s32) -> s32 {
|
|
unused mode
|
|
wide: [4096]u16
|
|
if !WidePath(path, wide[:]) { return -1 }
|
|
if (flags & OpenDirectoryOnly) != 0 {
|
|
found := AttributesRaw(*wide[0])
|
|
if found == cast(u32)MissingAttributes { return Failed() }
|
|
if (found & cast(u32)AttributeDirectory) == 0 {
|
|
CrtErrnoRaw().* = 20
|
|
return -1
|
|
}
|
|
}
|
|
access: u32 = 0x80000000
|
|
if (flags & 3) == OpenWriteOnly { access = 0x40000000 }
|
|
if (flags & 3) == OpenReadWrite { access = 0xc0000000 }
|
|
attributes: u32 = 128
|
|
if (flags & OpenPathOnly) != 0 {
|
|
// Query access with backup semantics opens directories as well.
|
|
access = 0
|
|
attributes = 0x02000000
|
|
}
|
|
disposition: u32 = 3
|
|
if (flags & OpenCreate) != 0 {
|
|
disposition = 4
|
|
if (flags & OpenTruncate) != 0 { disposition = 2 }
|
|
if (flags & OpenExclusive) != 0 { disposition = 1 }
|
|
} else if (flags & OpenTruncate) != 0 {
|
|
disposition = 5
|
|
}
|
|
handle := CreateFileRaw(*wide[0], access, 7, null, disposition,
|
|
attributes, null)
|
|
if cast(isize)handle == cast(isize)-1 { return Failed() }
|
|
return cast(s32)cast(isize)handle
|
|
}
|
|
|
|
CloseRaw :: (fd: s32) -> s32 {
|
|
return Status(CloseHandleRaw(HandleOf(fd)))
|
|
}
|
|
|
|
ReadRaw :: (fd: s32, output: *u8, count: usize) -> isize {
|
|
done: u32 = 0
|
|
if ReadFileRaw(HandleOf(fd), output, cast(u32)count, *done, null) == 0 {
|
|
if LastErrorRaw() == cast(u32)ErrorBrokenPipe { return 0 }
|
|
return -1
|
|
}
|
|
return cast(isize)done
|
|
}
|
|
|
|
WriteRaw :: (fd: s32, input: *u8, count: usize) -> isize {
|
|
done: u32 = 0
|
|
if WriteFileRaw(HandleOf(fd), input, cast(u32)count, *done, null) == 0 {
|
|
return -1
|
|
}
|
|
return cast(isize)done
|
|
}
|
|
|
|
// One call moves at most 2 GiB, a short count as pread allows.
|
|
Chunk :: (count: usize) -> u32 {
|
|
if count > cast(usize)0x7fffffff { return cast(u32)0x7fffffff }
|
|
return cast(u32)count
|
|
}
|
|
|
|
PositionAt :: (offset: s64) -> Overlapped {
|
|
position: Overlapped
|
|
position.offset = cast(u32)(offset & 0xffffffff)
|
|
position.offset_high = cast(u32)(offset >> 32)
|
|
return position
|
|
}
|
|
|
|
// An offset read or write on a synchronous handle moves the file
|
|
// pointer; restore it, as pread and pwrite leave the position alone.
|
|
ReadAtRaw :: (fd: s32, output: *u8, count: usize, offset: s64) -> isize {
|
|
handle := HandleOf(fd)
|
|
current: s64 = 0
|
|
if SetFilePointerRaw(handle, 0, *current, 1) == 0 { return -1 }
|
|
position := PositionAt(offset)
|
|
done: u32 = 0
|
|
result: isize = -1
|
|
if ReadFileRaw(handle, output, Chunk(count), *done, *position) != 0 {
|
|
result = cast(isize)done
|
|
} else if LastErrorRaw() == cast(u32)ErrorHandleEof {
|
|
result = 0
|
|
}
|
|
if SetFilePointerRaw(handle, current, null, 0) == 0 { return -1 }
|
|
return result
|
|
}
|
|
|
|
WriteAtRaw :: (fd: s32, input: *u8, count: usize, offset: s64) -> isize {
|
|
handle := HandleOf(fd)
|
|
current: s64 = 0
|
|
if SetFilePointerRaw(handle, 0, *current, 1) == 0 { return -1 }
|
|
position := PositionAt(offset)
|
|
done: u32 = 0
|
|
result: isize = -1
|
|
if WriteFileRaw(handle, input, Chunk(count), *done, *position) != 0 {
|
|
result = cast(isize)done
|
|
}
|
|
if SetFilePointerRaw(handle, current, null, 0) == 0 { return -1 }
|
|
return result
|
|
}
|
|
|
|
// The SEEK_SET, SEEK_CUR, and SEEK_END origins equal FILE_BEGIN,
|
|
// FILE_CURRENT, and FILE_END.
|
|
SeekRaw :: (fd: s32, offset: s64, origin: s32) -> s64 {
|
|
position: s64 = 0
|
|
if SetFilePointerRaw(HandleOf(fd), offset, *position,
|
|
cast(u32)origin) == 0 { return -1 }
|
|
return position
|
|
}
|
|
|
|
SyncRaw :: (fd: s32) -> s32 {
|
|
return Status(FlushFileRaw(HandleOf(fd)))
|
|
}
|
|
|
|
MakeDirectoryRaw :: (path: *u8, mode: s32) -> s32 {
|
|
unused mode
|
|
wide: [4096]u16
|
|
if !WidePath(path, wide[:]) { return -1 }
|
|
return Status(CreateDirectoryRaw(*wide[0], null))
|
|
}
|
|
|
|
UnlinkRaw :: (path: *u8) -> s32 {
|
|
wide: [4096]u16
|
|
if !WidePath(path, wide[:]) { return -1 }
|
|
return Status(DeleteFileRaw(*wide[0]))
|
|
}
|
|
|
|
LinkRaw :: (source: *u8, destination: *u8) -> s32 {
|
|
old_wide: [4096]u16
|
|
new_wide: [4096]u16
|
|
if !WidePath(source, old_wide[:]) ||
|
|
!WidePath(destination, new_wide[:]) { return -1 }
|
|
return Status(HardLinkRaw(*new_wide[0], *old_wide[0], null))
|
|
}
|
|
|
|
// MOVEFILE_WRITE_THROUGH returns once the rename is on disk; without
|
|
// MOVEFILE_REPLACE_EXISTING an existing destination fails the move.
|
|
MovePath :: (source: *u8, destination: *u8, replace: bool) -> s32 {
|
|
old_wide: [4096]u16
|
|
new_wide: [4096]u16
|
|
if !WidePath(source, old_wide[:]) ||
|
|
!WidePath(destination, new_wide[:]) { return -1 }
|
|
flags: u32 = 8
|
|
if replace { flags = 9 }
|
|
return Status(MoveFileRaw(*old_wide[0], *new_wide[0], flags))
|
|
}
|
|
|
|
RenameAtRaw :: (old_directory: s32, source: *u8,
|
|
new_directory: s32, destination: *u8) -> s32 {
|
|
unused old_directory
|
|
unused new_directory
|
|
return MovePath(source, destination, true)
|
|
}
|
|
|
|
RenameAt2Raw :: (old_directory: s32, source: *u8,
|
|
new_directory: s32, destination: *u8, flags: u32) -> s32 {
|
|
unused old_directory
|
|
unused new_directory
|
|
return MovePath(source, destination, (flags & 1) == 0)
|
|
}
|
|
|
|
OpenDirectoryRaw :: (path: *u8) -> *void {
|
|
wide: [4096]u16
|
|
if !WidePath(path, wide[:]) { return null }
|
|
length: s64 = 0
|
|
while wide[length] != 0 { length += 1 }
|
|
if length == 0 || length + 3 > 4096 { return null }
|
|
if wide[length - 1] != 47 && wide[length - 1] != 92 {
|
|
wide[length] = 92
|
|
length += 1
|
|
}
|
|
// FindFirstFileW takes a pattern; "*" names every entry.
|
|
wide[length] = 42
|
|
wide[length + 1] = 0
|
|
search := cast(*DirectorySearch)AllocateRaw(1, size_of(DirectorySearch))
|
|
if search == null { return null }
|
|
search.handle = FindFirstRaw(*wide[0], *search.data)
|
|
if cast(isize)search.handle == cast(isize)-1 {
|
|
FreeRaw(cast(*void)search)
|
|
return null
|
|
}
|
|
search.pending = true
|
|
return cast(*void)search
|
|
}
|
|
|
|
ReadDirectoryRaw :: (directory: *void) -> *DirectoryMetadata {
|
|
search := cast(*DirectorySearch)directory
|
|
if !search.pending && FindNextRaw(search.handle, *search.data) == 0 {
|
|
return null
|
|
}
|
|
search.pending = false
|
|
entry: DirectoryMetadata
|
|
// A name longer than 255 UTF-8 bytes fails, as readdir cannot return one.
|
|
if Utf8FromWideRaw(65001, 0, *search.data.name[0], -1,
|
|
*entry.name[0], 256, null, null) <= 0 { return null }
|
|
entry.kind = 8
|
|
if (search.data.attributes & cast(u32)AttributeDirectory) != 0 {
|
|
entry.kind = 4
|
|
}
|
|
if (search.data.attributes & cast(u32)1024) != 0 { entry.kind = 10 }
|
|
search.entry = entry
|
|
return *search.entry
|
|
}
|
|
|
|
CloseDirectoryRaw :: (directory: *void) -> s32 {
|
|
search := cast(*DirectorySearch)directory
|
|
status := Status(FindCloseRaw(search.handle))
|
|
FreeRaw(cast(*void)search)
|
|
return status
|
|
}
|
|
} else {
|
|
OpenRaw :: (path: *u8, flags: s32, mode: ..any) -> s32 #foreign libc "open";
|
|
CloseRaw :: (fd: s32) -> s32 #foreign libc "close";
|
|
ReadRaw :: (fd: s32, output: *u8, count: usize) -> isize #foreign libc "read";
|
|
WriteRaw :: (fd: s32, input: *u8, count: usize) -> isize #foreign libc "write";
|
|
#if #defined(__APPLE__) {
|
|
// Intel Darwin versions its 64-bit directory symbols; Apple Silicon
|
|
// uses their plain names. Resolve the matching ABI once per cursor.
|
|
DarwinOpenDirectory :: #type (path: *u8) -> *void #c_call;
|
|
DarwinReadDirectory :: #type (directory: *void) -> *DirectoryMetadata #c_call;
|
|
DarwinCloseDirectory :: #type (directory: *void) -> s32 #c_call;
|
|
DarwinDirectory :: struct {
|
|
library: *void
|
|
directory: *void
|
|
open: DarwinOpenDirectory
|
|
read: DarwinReadDirectory
|
|
close: DarwinCloseDirectory
|
|
}
|
|
DarwinLibrary :: (path: *u8, flags: s32) -> *void #foreign libc "dlopen";
|
|
DarwinSymbol :: (library: *void, name: *u8) -> *void #foreign libc "dlsym";
|
|
DarwinLibraryClose :: (library: *void) -> s32 #foreign libc "dlclose";
|
|
DarwinAllocate :: (count: usize, size: usize) -> *void #foreign libc "calloc";
|
|
DarwinFree :: (value: *void) #foreign libc "free";
|
|
DarwinCopy :: (output: *void, value: *void, count: usize) -> *void #foreign libc "memcpy";
|
|
DarwinResolve :: (library: *void, name: string, slot: *void) -> bool {
|
|
bytes: [64]u8
|
|
if !CopyCString(name, bytes[:]) { return false }
|
|
symbol := DarwinSymbol(library, *bytes[0])
|
|
if symbol == null { return false }
|
|
unused DarwinCopy(slot, cast(*void)(*symbol), size_of(*void))
|
|
return true
|
|
}
|
|
OpenDirectoryRaw :: (path: *u8) -> *void {
|
|
search := cast(*DarwinDirectory)DarwinAllocate(1, size_of(DarwinDirectory))
|
|
if search == null { return null }
|
|
search.library = DarwinLibrary(null, 2)
|
|
valid := search.library != null
|
|
if valid {
|
|
#if #defined(__x86_64__) {
|
|
valid = DarwinResolve(search.library, "opendir$INODE64", cast(*void)(*search.open)) &&
|
|
DarwinResolve(search.library, "readdir$INODE64", cast(*void)(*search.read))
|
|
} else {
|
|
valid = DarwinResolve(search.library, "opendir", cast(*void)(*search.open)) &&
|
|
DarwinResolve(search.library, "readdir", cast(*void)(*search.read))
|
|
}
|
|
valid = valid && DarwinResolve(search.library, "closedir", cast(*void)(*search.close))
|
|
}
|
|
if valid { search.directory = search.open(path); valid = search.directory != null }
|
|
if !valid {
|
|
if search.library != null { unused DarwinLibraryClose(search.library) }
|
|
DarwinFree(cast(*void)search)
|
|
return null
|
|
}
|
|
return cast(*void)search
|
|
}
|
|
ReadDirectoryRaw :: (directory: *void) -> *DirectoryMetadata {
|
|
search := cast(*DarwinDirectory)directory
|
|
return search.read(search.directory)
|
|
}
|
|
CloseDirectoryRaw :: (directory: *void) -> s32 {
|
|
search := cast(*DarwinDirectory)directory
|
|
status := search.close(search.directory)
|
|
unused DarwinLibraryClose(search.library)
|
|
DarwinFree(cast(*void)search)
|
|
return status
|
|
}
|
|
} else {
|
|
OpenDirectoryRaw :: (path: *u8) -> *void #foreign libc "opendir";
|
|
#if OS == .WEB {
|
|
// Emscripten's musl uses the ordinary 64-bit directory ABI.
|
|
ReadDirectoryRaw :: (directory: *void) -> *DirectoryMetadata #foreign libc "readdir";
|
|
} else {
|
|
ReadDirectoryRaw :: (directory: *void) -> *DirectoryMetadata #foreign libc "readdir64";
|
|
}
|
|
CloseDirectoryRaw :: (directory: *void) -> s32 #foreign libc "closedir";
|
|
}
|
|
#if OS == .ANDROID {
|
|
DirectoryErrno :: () -> *s32 #foreign libc "__errno";
|
|
} else #if #defined(__APPLE__) {
|
|
DirectoryErrno :: () -> *s32 #foreign libc "__error";
|
|
} else {
|
|
DirectoryErrno :: () -> *s32 #foreign libc "__errno_location";
|
|
}
|
|
|
|
#if OS == .ANDROID {
|
|
// Android's 32-bit off_t is narrower than the public file offset. The
|
|
// explicit large-file entry points are available from API 21 onward.
|
|
SeekRaw :: (fd: s32, offset: s64, origin: s32) -> s64 #foreign libc "lseek64";
|
|
ReadAtRaw :: (fd: s32, output: *u8, count: usize, offset: s64) -> isize #foreign libc "pread64";
|
|
WriteAtRaw :: (fd: s32, input: *u8, count: usize, offset: s64) -> isize #foreign libc "pwrite64";
|
|
}
|
|
#if OS != .ANDROID {
|
|
SeekRaw :: (fd: s32, offset: s64, origin: s32) -> s64 #foreign libc "lseek";
|
|
ReadAtRaw :: (fd: s32, output: *u8, count: usize, offset: s64) -> isize #foreign libc "pread";
|
|
WriteAtRaw :: (fd: s32, input: *u8, count: usize, offset: s64) -> isize #foreign libc "pwrite";
|
|
}
|
|
MakeDirectoryRaw :: (path: *u8, mode: s32) -> s32 #foreign libc "mkdir";
|
|
UnlinkRaw :: (path: *u8) -> s32 #foreign libc "unlink";
|
|
LinkRaw :: (source: *u8, destination: *u8) -> s32 #foreign libc "link";
|
|
RenameAtRaw :: (old_directory: s32, source: *u8,
|
|
new_directory: s32, destination: *u8) -> s32 #foreign libc "renameat";
|
|
#if OS == .ANDROID {
|
|
// Bionic exports renameat2 only from API 30. Call the kernel operation
|
|
// directly on older API levels, retaining atomic no-replace semantics.
|
|
RenameSystemCall :: (number: isize, old_directory: isize, source: *u8,
|
|
new_directory: isize, destination: *u8, flags: usize) -> isize #foreign libc "syscall";
|
|
#if #defined(__aarch64__) {
|
|
RenameAt2Number :: 276;
|
|
}
|
|
#if #defined(__arm__) {
|
|
RenameAt2Number :: 382;
|
|
}
|
|
#if #defined(__i386__) {
|
|
RenameAt2Number :: 353;
|
|
}
|
|
#if #defined(__x86_64__) {
|
|
RenameAt2Number :: 316;
|
|
}
|
|
}
|
|
#if #defined(__APPLE__) {
|
|
RenameAt2Raw :: (old_directory: s32, source: *u8,
|
|
new_directory: s32, destination: *u8, flags: u32) -> s32 #foreign libc "renameatx_np";
|
|
} else #if OS != .ANDROID && OS != .WEB {
|
|
RenameAt2Raw :: (old_directory: s32, source: *u8,
|
|
new_directory: s32, destination: *u8, flags: u32) -> s32 #foreign libc "renameat2";
|
|
}
|
|
SyncRaw :: (fd: s32) -> s32 #foreign libc "fsync";
|
|
}
|
|
|
|
#scope_export
|
|
FileHandle :: struct {
|
|
fd: s32
|
|
valid: bool
|
|
}
|
|
|
|
DirectoryCursor :: struct {
|
|
storage: *void
|
|
valid: bool
|
|
failed: bool
|
|
}
|
|
|
|
// Unknown requires a fresh path query; directory types are only a snapshot.
|
|
DirectoryKind :: enum u8 { Unknown :: 0; Directory :: 4; Regular :: 8; Link :: 10; Other :: 255; }
|
|
|
|
DirectoryEntry :: struct {
|
|
name: [256]u8
|
|
name_count: s32
|
|
valid: bool
|
|
kind: DirectoryKind
|
|
}
|
|
|
|
#scope_file
|
|
OpenPath :: (path: *u8, flags: s32, mode: s32) -> s32 {
|
|
#if #defined(__APPLE__) {
|
|
// Internal flags match the other providers; map them to Darwin's
|
|
// fcntl.h values at the native call boundary.
|
|
native_flags := flags & 3
|
|
if (flags & 64) != 0 { native_flags |= 512 }
|
|
if (flags & 128) != 0 { native_flags |= 2048 }
|
|
if (flags & 512) != 0 { native_flags |= 1024 }
|
|
if (flags & 65536) != 0 { native_flags |= 1048576 }
|
|
if (flags & 2097152) != 0 { native_flags |= 32768 }
|
|
return OpenRaw(path, native_flags, mode)
|
|
}
|
|
return OpenRaw(path, flags, mode)
|
|
}
|
|
|
|
OpenWithFlags :: (path: string, flags: s32) -> FileHandle {
|
|
result: FileHandle
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return result }
|
|
result.fd = OpenPath(*name[0], flags, 420)
|
|
result.valid = result.fd >= 0
|
|
return result
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
OpenRead :: (path: string) -> FileHandle {
|
|
return OpenWithFlags(path, 0)
|
|
}
|
|
|
|
#program_export
|
|
OpenUpdate :: (path: string) -> FileHandle {
|
|
return OpenWithFlags(path, 66)
|
|
}
|
|
|
|
#program_export
|
|
OpenWrite :: (path: string) -> FileHandle { return OpenWithFlags(path, 1) }
|
|
|
|
#program_export
|
|
OpenReplace :: (path: string) -> FileHandle {
|
|
return OpenWithFlags(path, 577)
|
|
}
|
|
|
|
// Create a writable file only when the path is unoccupied, including by a
|
|
// symbolic link. Creation is exclusive in the same native open operation.
|
|
#program_export
|
|
OpenNew :: (path: string) -> FileHandle {
|
|
return OpenWithFlags(path, 193) // O_CREAT | O_EXCL | O_WRONLY
|
|
}
|
|
|
|
#program_export
|
|
OpenExclusivePrivate :: (path: string) -> FileHandle {
|
|
result: FileHandle
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return result }
|
|
result.fd = OpenPath(*name[0], 193, 384)
|
|
result.valid = result.fd >= 0
|
|
return result
|
|
}
|
|
|
|
#program_export
|
|
RemoveFile :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
return UnlinkRaw(*name[0]) == 0
|
|
}
|
|
|
|
// Inspect the path itself, including a dangling link. No target is opened,
|
|
// no result is cached, and invalid paths cannot be shortened at a NUL.
|
|
// Windows includes junctions and other reparse points in this link policy.
|
|
#program_export
|
|
PathIsLink :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
#if #defined(_WIN32) {
|
|
wide: [4096]u16
|
|
if !WidePath(*name[0], wide[:]) { return false }
|
|
attributes := AttributesRaw(*wide[0])
|
|
return attributes != cast(u32)MissingAttributes &&
|
|
(attributes & cast(u32)1024) != cast(u32)0
|
|
} else {
|
|
byte: u8
|
|
return ReadLinkRaw(*name[0], *byte, 1) > 0
|
|
}
|
|
}
|
|
|
|
#program_export
|
|
PathExists :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
// O_PATH follows symlinks and can inspect paths without read permission.
|
|
fd := OpenPath(*name[0], 2097152, 0)
|
|
if fd < 0 { return false }
|
|
return CloseRaw(fd) == 0
|
|
}
|
|
|
|
DirectoryExists :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
fd := OpenPath(*name[0], 2097152 | 65536, 0)
|
|
if fd < 0 { return false }
|
|
return CloseRaw(fd) == 0
|
|
}
|
|
|
|
#program_export
|
|
RenamePath :: (source: string, destination: string) -> bool {
|
|
old_name: [4096]u8
|
|
new_name: [4096]u8
|
|
if !CopyCString(source, old_name[:]) ||
|
|
!CopyCString(destination, new_name[:]) { return false }
|
|
#if #defined(__APPLE__) {
|
|
return RenameAtRaw(-2, *old_name[0], -2, *new_name[0]) == 0
|
|
}
|
|
return RenameAtRaw(-100, *old_name[0], -100, *new_name[0]) == 0
|
|
}
|
|
|
|
#program_export
|
|
RenameNoReplace :: (source: string, destination: string) -> bool {
|
|
old_name: [4096]u8
|
|
new_name: [4096]u8
|
|
if !CopyCString(source, old_name[:]) ||
|
|
!CopyCString(destination, new_name[:]) { return false }
|
|
#if OS == .ANDROID {
|
|
// ENOSYS on an old kernel is a safe failure: neither path is changed.
|
|
return RenameSystemCall(cast(isize)RenameAt2Number, cast(isize)-100,
|
|
*old_name[0], cast(isize)-100, *new_name[0], cast(usize)1) == cast(isize)0
|
|
}
|
|
#if OS == .WEB {
|
|
// Emscripten's libc has no renameat2. A page runs one thread, so
|
|
// checking the destination first keeps the no-replace guarantee.
|
|
if PathExists(destination) { return false }
|
|
return RenameAtRaw(-100, *old_name[0], -100, *new_name[0]) == 0
|
|
}
|
|
#if #defined(__APPLE__) {
|
|
return RenameAt2Raw(-2, *old_name[0], -2, *new_name[0], 4) == 0
|
|
} else #if OS != .ANDROID && OS != .WEB {
|
|
return RenameAt2Raw(-100, *old_name[0], -100, *new_name[0], 1) == 0
|
|
}
|
|
}
|
|
|
|
#program_export
|
|
LinkFile :: (source: string, destination: string) -> bool {
|
|
old_name: [4096]u8
|
|
new_name: [4096]u8
|
|
if !CopyCString(source, old_name[:]) ||
|
|
!CopyCString(destination, new_name[:]) { return false }
|
|
return LinkRaw(*old_name[0], *new_name[0]) == 0
|
|
}
|
|
|
|
#program_export
|
|
CreateDirectory :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
if MakeDirectoryRaw(*name[0], 493) == 0 { return true }
|
|
return DirectoryExists(path)
|
|
}
|
|
|
|
#program_export
|
|
CreatePrivateDirectory :: (path: string) -> bool {
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return false }
|
|
if MakeDirectoryRaw(*name[0], 448) == 0 { return true }
|
|
return DirectoryExists(path)
|
|
}
|
|
|
|
#program_export
|
|
FileSize :: (file: FileHandle) -> s64 {
|
|
if !file.valid { return -1 }
|
|
return SeekRaw(file.fd, 0, 2)
|
|
}
|
|
|
|
#program_export
|
|
ReadAt :: (file: FileHandle, output: []u8, offset: s64) -> s64 {
|
|
if !file.valid || offset < 0 { return -1 }
|
|
if output.count == 0 { return 0 }
|
|
return cast(s64)ReadAtRaw(file.fd, *output[0], cast(usize)output.count, offset)
|
|
}
|
|
|
|
#program_export
|
|
WriteAt :: (file: FileHandle, input: []u8, offset: s64) -> s64 {
|
|
if !file.valid || offset < 0 { return -1 }
|
|
if input.count == 0 { return 0 }
|
|
return cast(s64)WriteAtRaw(file.fd, *input[0], cast(usize)input.count, offset)
|
|
}
|
|
|
|
// Copy immutable text through a bounded buffer before writing it. The string
|
|
// may borrow read-only storage, so it must not be exposed as a mutable slice.
|
|
#program_export
|
|
WriteTextAt :: (file: FileHandle, input: string, offset: s64) -> s64 {
|
|
if !file.valid || offset < 0 { return -1 }
|
|
length: s64 = cast(s64)input.count
|
|
if length > 9223372036854775807 - offset { return -1 }
|
|
written: s64 = 0
|
|
while written < length {
|
|
buffer: [4096]u8
|
|
count: s64 = length - written
|
|
if count > 4096 { count = 4096 }
|
|
index: s64 = 0
|
|
while index < count {
|
|
buffer[index] = input[written + index]
|
|
index += 1
|
|
}
|
|
result := WriteAt(file, buffer[0:count], offset + written)
|
|
if result <= 0 { return -1 }
|
|
written += result
|
|
}
|
|
return written
|
|
}
|
|
|
|
#program_export
|
|
CloseFile :: (file: FileHandle) -> bool {
|
|
if !file.valid { return false }
|
|
return CloseRaw(file.fd) == 0
|
|
}
|
|
|
|
#program_export
|
|
SyncFile :: (file: FileHandle) -> bool {
|
|
if !file.valid { return false }
|
|
return SyncRaw(file.fd) == 0
|
|
}
|
|
|
|
#program_export
|
|
Read :: (file: FileHandle, output: []u8) -> s64 {
|
|
if !file.valid || output.count > 2147483647 { return -1 }
|
|
if output.count == 0 { return 0 }
|
|
return cast(s64)ReadRaw(file.fd, *output[0], cast(usize)output.count)
|
|
}
|
|
|
|
#program_export
|
|
Write :: (file: FileHandle, input: []u8) -> s64 {
|
|
if !file.valid || input.count > 2147483647 { return -1 }
|
|
if input.count == 0 { return 0 }
|
|
return cast(s64)WriteRaw(file.fd, *input[0], cast(usize)input.count)
|
|
}
|
|
|
|
#program_export
|
|
OpenDirectory :: (path: string) -> DirectoryCursor {
|
|
result: DirectoryCursor
|
|
name: [4096]u8
|
|
if !CopyCString(path, name[:]) { return result }
|
|
result.storage = OpenDirectoryRaw(*name[0])
|
|
result.valid = result.storage != null
|
|
return result
|
|
}
|
|
|
|
#program_export
|
|
NextDirectoryEntry :: (cursor: *DirectoryCursor) -> DirectoryEntry {
|
|
result: DirectoryEntry
|
|
if cursor == null || !cursor.valid || cursor.failed { return result }
|
|
if cursor.storage == null { cursor.failed = true; return result }
|
|
#if !#defined(_WIN32) {
|
|
DirectoryErrno().* = 0
|
|
}
|
|
entry := ReadDirectoryRaw(cursor.storage)
|
|
if entry == null {
|
|
#if #defined(_WIN32) {
|
|
cursor.failed = LastErrorRaw() != cast(u32)18
|
|
} else {
|
|
cursor.failed = DirectoryErrno().* != 0
|
|
}
|
|
return result
|
|
}
|
|
index: s32 = 0
|
|
while entry.name[index] != 0 {
|
|
if index >= 255 { cursor.failed = true; return result }
|
|
result.name[index] = entry.name[index]
|
|
index += 1
|
|
}
|
|
result.name_count = index
|
|
result.valid = true
|
|
result.kind = .Other
|
|
if entry.kind == cast(u8)0 { result.kind = .Unknown }
|
|
if entry.kind == cast(u8)4 { result.kind = .Directory }
|
|
if entry.kind == cast(u8)8 { result.kind = .Regular }
|
|
if entry.kind == cast(u8)10 { result.kind = .Link }
|
|
return result
|
|
}
|
|
|
|
#program_export
|
|
CloseDirectory :: (cursor: *DirectoryCursor) -> bool {
|
|
if cursor == null || !cursor.valid { return false }
|
|
status := CloseDirectoryRaw(cursor.storage)
|
|
cursor.storage = null
|
|
cursor.valid = false
|
|
return status == 0
|
|
}
|