Browse Source

Use `fstat` on `os2.File` directly

gingerBill 1 year ago
parent
commit
3d38f14202

+ 2 - 0
core/os/os2/errors.odin

@@ -22,6 +22,7 @@ General_Error :: enum u32 {
 	Invalid_File,
 	Invalid_File,
 	Invalid_Dir,
 	Invalid_Dir,
 	Invalid_Path,
 	Invalid_Path,
+	Invalid_Callback,
 
 
 	Pattern_Has_Separator,
 	Pattern_Has_Separator,
 
 
@@ -64,6 +65,7 @@ error_string :: proc(ferr: Error) -> string {
 		case .Invalid_File:      return "invalid file"
 		case .Invalid_File:      return "invalid file"
 		case .Invalid_Dir:       return "invalid directory"
 		case .Invalid_Dir:       return "invalid directory"
 		case .Invalid_Path:      return "invalid path"
 		case .Invalid_Path:      return "invalid path"
+		case .Invalid_Callback:  return "invalid callback"
 		case .Unsupported:       return "unsupported"
 		case .Unsupported:       return "unsupported"
 		case .Pattern_Has_Separator: return "pattern has separator"
 		case .Pattern_Has_Separator: return "pattern has separator"
 		}
 		}

+ 1 - 1
core/os/os2/file.odin

@@ -7,7 +7,7 @@ import "base:runtime"
 File :: struct {
 File :: struct {
 	impl:   _File,
 	impl:   _File,
 	stream: io.Stream,
 	stream: io.Stream,
-	user_fstat: Fstat_Callback,
+	fstat: Fstat_Callback,
 }
 }
 
 
 File_Mode :: distinct u32
 File_Mode :: distinct u32

+ 10 - 15
core/os/os2/file_linux.odin

@@ -90,22 +90,17 @@ _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, er
 
 
 _new_file :: proc(fd: uintptr, _: string = "") -> ^File {
 _new_file :: proc(fd: uintptr, _: string = "") -> ^File {
 	file := new(File, file_allocator())
 	file := new(File, file_allocator())
-	_construct_file(file, fd, "")
-	return file
-}
-
-_construct_file :: proc(file: ^File, fd: uintptr, _: string = "") {
-	file^ = {
-		impl = {
-			fd = linux.Fd(fd),
-			allocator = file_allocator(),
-			name = _get_full_path(file.impl.fd, file.impl.allocator),
-		},
-		stream = {
-			data = file,
-			procedure = _file_stream_proc,
-		},
+	file.impl = {
+		fd = linux.Fd(fd),
+		allocator = file_allocator(),
+		name = _get_full_path(file.impl.fd, file.impl.allocator),
+	}
+	file.stream = {
+		data = file,
+		procedure = _file_stream_proc,
 	}
 	}
+	file.fstat = _fstat
+	return file
 }
 }
 
 
 _destroy :: proc(f: ^File) -> Error {
 _destroy :: proc(f: ^File) -> Error {

+ 1 - 0
core/os/os2/file_windows.odin

@@ -151,6 +151,7 @@ _new_file :: proc(handle: uintptr, name: string) -> ^File {
 		data = f,
 		data = f,
 		procedure = _file_stream_proc,
 		procedure = _file_stream_proc,
 	}
 	}
+	f.fstat = _fstat
 
 
 	return f
 	return f
 }
 }

+ 5 - 3
core/os/os2/stat.odin

@@ -29,10 +29,12 @@ file_info_delete :: proc(fi: File_Info, allocator: runtime.Allocator) {
 
 
 @(require_results)
 @(require_results)
 fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
 fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) {
-	if f != nil && f.user_fstat != nil {
-		return f->user_fstat(allocator)
+	if f == nil {
+		return {}, nil
+	} else if f.fstat != nil {
+		return f->fstat(allocator)
 	}
 	}
-	return _fstat(f, allocator)
+	return {}, .Invalid_Callback
 }
 }
 
 
 @(require_results)
 @(require_results)