|
@@ -17,17 +17,19 @@ _ERROR_BAD_NETPATH :: 53
|
|
|
MAX_RW :: 1<<30
|
|
|
|
|
|
|
|
|
-_File_Kind :: enum u8 {
|
|
|
+File_Impl_Kind :: enum u8 {
|
|
|
File,
|
|
|
Console,
|
|
|
Pipe,
|
|
|
}
|
|
|
|
|
|
-_File :: struct {
|
|
|
+File_Impl :: struct {
|
|
|
+ file: File,
|
|
|
+
|
|
|
fd: rawptr,
|
|
|
name: string,
|
|
|
wname: win32.wstring,
|
|
|
- kind: _File_Kind,
|
|
|
+ kind: File_Impl_Kind,
|
|
|
|
|
|
allocator: runtime.Allocator,
|
|
|
|
|
@@ -75,11 +77,9 @@ _open_internal :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (han
|
|
|
access |= win32.FILE_APPEND_DATA
|
|
|
}
|
|
|
share_mode := u32(win32.FILE_SHARE_READ | win32.FILE_SHARE_WRITE)
|
|
|
- sa: ^win32.SECURITY_ATTRIBUTES
|
|
|
- if .Close_On_Exec not_in flags {
|
|
|
- sa = &win32.SECURITY_ATTRIBUTES{}
|
|
|
- sa.nLength = size_of(win32.SECURITY_ATTRIBUTES)
|
|
|
- sa.bInheritHandle = true
|
|
|
+ sa := win32.SECURITY_ATTRIBUTES {
|
|
|
+ nLength = size_of(win32.SECURITY_ATTRIBUTES),
|
|
|
+ bInheritHandle = .Close_On_Exec not_in flags,
|
|
|
}
|
|
|
|
|
|
create_mode: u32 = win32.OPEN_EXISTING
|
|
@@ -101,7 +101,7 @@ _open_internal :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (han
|
|
|
// NOTE(bill): Open has just asked to create a file in read-only mode.
|
|
|
// If the file already exists, to make it akin to a *nix open call,
|
|
|
// the call preserves the existing permissions.
|
|
|
- h := win32.CreateFileW(path, access, share_mode, sa, win32.TRUNCATE_EXISTING, win32.FILE_ATTRIBUTE_NORMAL, nil)
|
|
|
+ h := win32.CreateFileW(path, access, share_mode, &sa, win32.TRUNCATE_EXISTING, win32.FILE_ATTRIBUTE_NORMAL, nil)
|
|
|
if h == win32.INVALID_HANDLE {
|
|
|
switch e := win32.GetLastError(); e {
|
|
|
case win32.ERROR_FILE_NOT_FOUND, _ERROR_BAD_NETPATH, win32.ERROR_PATH_NOT_FOUND:
|
|
@@ -114,7 +114,7 @@ _open_internal :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (han
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- h := win32.CreateFileW(path, access, share_mode, sa, create_mode, attrs, nil)
|
|
|
+ h := win32.CreateFileW(path, access, share_mode, &sa, create_mode, attrs, nil)
|
|
|
if h == win32.INVALID_HANDLE {
|
|
|
return 0, _get_platform_error()
|
|
|
}
|
|
@@ -124,7 +124,7 @@ _open_internal :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (han
|
|
|
|
|
|
_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, err: Error) {
|
|
|
flags := flags if flags != nil else {.Read}
|
|
|
- handle := _open_internal(name, flags + {.Close_On_Exec}, perm) or_return
|
|
|
+ handle := _open_internal(name, flags, perm) or_return
|
|
|
return _new_file(handle, name), nil
|
|
|
}
|
|
|
|
|
@@ -132,75 +132,81 @@ _new_file :: proc(handle: uintptr, name: string) -> ^File {
|
|
|
if handle == INVALID_HANDLE {
|
|
|
return nil
|
|
|
}
|
|
|
- f := new(File, file_allocator())
|
|
|
+ impl := new(File_Impl, file_allocator())
|
|
|
+ impl.file.impl = impl
|
|
|
|
|
|
- f.impl.allocator = file_allocator()
|
|
|
- f.impl.fd = rawptr(handle)
|
|
|
- f.impl.name, _ = clone_string(name, f.impl.allocator)
|
|
|
- f.impl.wname = win32.utf8_to_wstring(name, f.impl.allocator)
|
|
|
+ impl.allocator = file_allocator()
|
|
|
+ impl.fd = rawptr(handle)
|
|
|
+ impl.name, _ = clone_string(name, impl.allocator)
|
|
|
+ impl.wname = win32.utf8_to_wstring(name, impl.allocator)
|
|
|
|
|
|
- handle := _handle(f)
|
|
|
- kind := _File_Kind.File
|
|
|
+ handle := _handle(&impl.file)
|
|
|
+ kind := File_Impl_Kind.File
|
|
|
if m: u32; win32.GetConsoleMode(handle, &m) {
|
|
|
kind = .Console
|
|
|
}
|
|
|
if win32.GetFileType(handle) == win32.FILE_TYPE_PIPE {
|
|
|
kind = .Pipe
|
|
|
}
|
|
|
- f.impl.kind = kind
|
|
|
+ impl.kind = kind
|
|
|
|
|
|
- f.stream = {
|
|
|
- data = f,
|
|
|
+ impl.file.stream = {
|
|
|
+ data = impl,
|
|
|
procedure = _file_stream_proc,
|
|
|
}
|
|
|
+ impl.file.fstat = _fstat
|
|
|
|
|
|
- return f
|
|
|
+ return &impl.file
|
|
|
}
|
|
|
|
|
|
_fd :: proc(f: ^File) -> uintptr {
|
|
|
- if f == nil {
|
|
|
+ if f == nil || f.impl == nil {
|
|
|
return INVALID_HANDLE
|
|
|
}
|
|
|
- return uintptr(f.impl.fd)
|
|
|
+ return uintptr((^File_Impl)(f.impl).fd)
|
|
|
}
|
|
|
|
|
|
-_destroy :: proc(f: ^File) -> Error {
|
|
|
+_destroy :: proc(f: ^File_Impl) -> Error {
|
|
|
if f == nil {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- a := f.impl.allocator
|
|
|
- free(f.impl.wname, a)
|
|
|
- delete(f.impl.name, a)
|
|
|
- free(f, a)
|
|
|
+ a := f.allocator
|
|
|
+ err0 := free(f.wname, a)
|
|
|
+ err1 := delete(f.name, a)
|
|
|
+ err2 := free(f, a)
|
|
|
+ err0 or_return
|
|
|
+ err1 or_return
|
|
|
+ err2 or_return
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
|
|
|
-_close :: proc(f: ^File) -> Error {
|
|
|
- if f == nil {
|
|
|
+_close :: proc(f: ^File_Impl) -> Error {
|
|
|
+ if f == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- if !win32.CloseHandle(win32.HANDLE(f.impl.fd)) {
|
|
|
+ if !win32.CloseHandle(win32.HANDLE(f.fd)) {
|
|
|
return .Closed
|
|
|
}
|
|
|
return _destroy(f)
|
|
|
}
|
|
|
|
|
|
_name :: proc(f: ^File) -> string {
|
|
|
- return f.impl.name if f != nil else ""
|
|
|
+ return (^File_Impl)(f.impl).name if f != nil && f.impl != nil else ""
|
|
|
}
|
|
|
|
|
|
-_seek :: proc(f: ^File, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Error) {
|
|
|
- handle := _handle(f)
|
|
|
+_seek :: proc(f: ^File_Impl, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Error) {
|
|
|
+ handle := _handle(&f.file)
|
|
|
if handle == win32.INVALID_HANDLE {
|
|
|
return 0, .Invalid_File
|
|
|
}
|
|
|
- if f.impl.kind == .Pipe {
|
|
|
+
|
|
|
+ if f.kind == .Pipe {
|
|
|
return 0, .Invalid_File
|
|
|
}
|
|
|
|
|
|
- sync.guard(&f.impl.rw_mutex)
|
|
|
+ sync.guard(&f.rw_mutex)
|
|
|
|
|
|
w: u32
|
|
|
switch whence {
|
|
@@ -218,7 +224,7 @@ _seek :: proc(f: ^File, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Er
|
|
|
return i64(hi)<<32 + i64(dw_ptr), nil
|
|
|
}
|
|
|
|
|
|
-_read :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
+_read :: proc(f: ^File_Impl, p: []byte) -> (n: i64, err: Error) {
|
|
|
read_console :: proc(handle: win32.HANDLE, b: []byte) -> (n: int, err: Error) {
|
|
|
if len(b) == 0 {
|
|
|
return 0, nil
|
|
@@ -269,18 +275,18 @@ _read :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- handle := _handle(f)
|
|
|
+ handle := _handle(&f.file)
|
|
|
|
|
|
single_read_length: win32.DWORD
|
|
|
total_read: int
|
|
|
length := len(p)
|
|
|
|
|
|
- sync.shared_guard(&f.impl.rw_mutex) // multiple readers
|
|
|
+ sync.shared_guard(&f.rw_mutex) // multiple readers
|
|
|
|
|
|
- if sync.guard(&f.impl.p_mutex) {
|
|
|
+ if sync.guard(&f.p_mutex) {
|
|
|
to_read := min(win32.DWORD(length), MAX_RW)
|
|
|
ok: win32.BOOL
|
|
|
- if f.impl.kind == .Console {
|
|
|
+ if f.kind == .Console {
|
|
|
n, cerr := read_console(handle, p[total_read:][:to_read])
|
|
|
total_read += n
|
|
|
if cerr != nil {
|
|
@@ -300,15 +306,15 @@ _read :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
return i64(total_read), err
|
|
|
}
|
|
|
|
|
|
-_read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
- pread :: proc(f: ^File, data: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
+_read_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
+ pread :: proc(f: ^File_Impl, data: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
buf := data
|
|
|
if len(buf) > MAX_RW {
|
|
|
buf = buf[:MAX_RW]
|
|
|
|
|
|
}
|
|
|
- curr_offset := seek(f, offset, .Current) or_return
|
|
|
- defer seek(f, curr_offset, .Start)
|
|
|
+ curr_offset := _seek(f, offset, .Current) or_return
|
|
|
+ defer _seek(f, curr_offset, .Start)
|
|
|
|
|
|
o := win32.OVERLAPPED{
|
|
|
OffsetHigh = u32(offset>>32),
|
|
@@ -317,7 +323,7 @@ _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
|
|
|
// TODO(bill): Determine the correct behaviour for consoles
|
|
|
|
|
|
- h := _handle(f)
|
|
|
+ h := _handle(&f.file)
|
|
|
done: win32.DWORD
|
|
|
if !win32.ReadFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
|
|
|
err = _get_platform_error()
|
|
@@ -327,7 +333,7 @@ _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- sync.guard(&f.impl.p_mutex)
|
|
|
+ sync.guard(&f.p_mutex)
|
|
|
|
|
|
p, offset := p, offset
|
|
|
for len(p) > 0 {
|
|
@@ -339,7 +345,7 @@ _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-_write :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
+_write :: proc(f: ^File_Impl, p: []byte) -> (n: i64, err: Error) {
|
|
|
if len(p) == 0 {
|
|
|
return
|
|
|
}
|
|
@@ -348,9 +354,9 @@ _write :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
total_write: i64
|
|
|
length := i64(len(p))
|
|
|
|
|
|
- handle := _handle(f)
|
|
|
+ handle := _handle(&f.file)
|
|
|
|
|
|
- sync.guard(&f.impl.rw_mutex)
|
|
|
+ sync.guard(&f.rw_mutex)
|
|
|
for total_write < length {
|
|
|
remaining := length - total_write
|
|
|
to_write := win32.DWORD(min(i32(remaining), MAX_RW))
|
|
@@ -366,22 +372,22 @@ _write :: proc(f: ^File, p: []byte) -> (n: i64, err: Error) {
|
|
|
return i64(total_write), nil
|
|
|
}
|
|
|
|
|
|
-_write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
- pwrite :: proc(f: ^File, data: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
+_write_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
+ pwrite :: proc(f: ^File_Impl, data: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
buf := data
|
|
|
if len(buf) > MAX_RW {
|
|
|
buf = buf[:MAX_RW]
|
|
|
|
|
|
}
|
|
|
- curr_offset := seek(f, offset, .Current) or_return
|
|
|
- defer seek(f, curr_offset, .Start)
|
|
|
+ curr_offset := _seek(f, offset, .Current) or_return
|
|
|
+ defer _seek(f, curr_offset, .Start)
|
|
|
|
|
|
o := win32.OVERLAPPED{
|
|
|
OffsetHigh = u32(offset>>32),
|
|
|
Offset = u32(offset),
|
|
|
}
|
|
|
|
|
|
- h := _handle(f)
|
|
|
+ h := _handle(&f.file)
|
|
|
done: win32.DWORD
|
|
|
if !win32.WriteFile(h, raw_data(buf), u32(len(buf)), &done, &o) {
|
|
|
err = _get_platform_error()
|
|
@@ -391,7 +397,7 @@ _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- sync.guard(&f.impl.p_mutex)
|
|
|
+ sync.guard(&f.p_mutex)
|
|
|
p, offset := p, offset
|
|
|
for len(p) > 0 {
|
|
|
m := pwrite(f, p, offset) or_return
|
|
@@ -402,12 +408,12 @@ _write_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-_file_size :: proc(f: ^File) -> (n: i64, err: Error) {
|
|
|
+_file_size :: proc(f: ^File_Impl) -> (n: i64, err: Error) {
|
|
|
length: win32.LARGE_INTEGER
|
|
|
- if f.impl.kind == .Pipe {
|
|
|
+ if f.kind == .Pipe {
|
|
|
return 0, .No_Size
|
|
|
}
|
|
|
- handle := _handle(f)
|
|
|
+ handle := _handle(&f.file)
|
|
|
if !win32.GetFileSizeEx(handle, &length) {
|
|
|
err = _get_platform_error()
|
|
|
}
|
|
@@ -417,11 +423,14 @@ _file_size :: proc(f: ^File) -> (n: i64, err: Error) {
|
|
|
|
|
|
|
|
|
_sync :: proc(f: ^File) -> Error {
|
|
|
- return _flush(f)
|
|
|
+ if f != nil && f.impl != nil {
|
|
|
+ return _flush((^File_Impl)(f.impl))
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
-_flush :: proc(f: ^File) -> Error {
|
|
|
- handle := _handle(f)
|
|
|
+_flush :: proc(f: ^File_Impl) -> Error {
|
|
|
+ handle := _handle(&f.file)
|
|
|
if !win32.FlushFileBuffers(handle) {
|
|
|
return _get_platform_error()
|
|
|
}
|
|
@@ -429,7 +438,7 @@ _flush :: proc(f: ^File) -> Error {
|
|
|
}
|
|
|
|
|
|
_truncate :: proc(f: ^File, size: i64) -> Error {
|
|
|
- if f == nil {
|
|
|
+ if f == nil || f.impl == nil {
|
|
|
return nil
|
|
|
}
|
|
|
curr_off := seek(f, 0, .Current) or_return
|
|
@@ -616,17 +625,18 @@ _read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, er
|
|
|
|
|
|
|
|
|
_fchdir :: proc(f: ^File) -> Error {
|
|
|
- if f == nil {
|
|
|
+ if f == nil || f.impl == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- if !win32.SetCurrentDirectoryW(f.impl.wname) {
|
|
|
+ impl := (^File_Impl)(f.impl)
|
|
|
+ if !win32.SetCurrentDirectoryW(impl.wname) {
|
|
|
return _get_platform_error()
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
_fchmod :: proc(f: ^File, mode: File_Mode) -> Error {
|
|
|
- if f == nil {
|
|
|
+ if f == nil || f.impl == nil {
|
|
|
return nil
|
|
|
}
|
|
|
d: win32.BY_HANDLE_FILE_INFORMATION
|
|
@@ -681,7 +691,7 @@ _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
|
|
|
return _fchtimes(f, atime, mtime)
|
|
|
}
|
|
|
_fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
|
|
|
- if f == nil {
|
|
|
+ if f == nil || f.impl == nil {
|
|
|
return nil
|
|
|
}
|
|
|
d: win32.BY_HANDLE_FILE_INFORMATION
|
|
@@ -737,7 +747,7 @@ _is_dir :: proc(path: string) -> bool {
|
|
|
|
|
|
@(private="package")
|
|
|
_file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
|
|
|
- f := (^File)(stream_data)
|
|
|
+ f := (^File_Impl)(stream_data)
|
|
|
ferr: Error
|
|
|
switch mode {
|
|
|
case .Read:
|