mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
[os2]: Split file type from mode bits
This commit is contained in:
+63
-13
@@ -4,20 +4,57 @@ import "core:io"
|
||||
import "core:time"
|
||||
import "base:runtime"
|
||||
|
||||
/*
|
||||
Type representing a file handle.
|
||||
|
||||
This struct represents an OS-specific file-handle, which can be one of
|
||||
the following:
|
||||
- File
|
||||
- Directory
|
||||
- Pipe
|
||||
- Named pipe
|
||||
- Block Device
|
||||
- Character device
|
||||
- Symlink
|
||||
- Socket
|
||||
|
||||
See `File_Type` enum for more information on file types.
|
||||
*/
|
||||
File :: struct {
|
||||
impl: rawptr,
|
||||
stream: io.Stream,
|
||||
fstat: Fstat_Callback,
|
||||
}
|
||||
|
||||
File_Mode :: distinct u32
|
||||
File_Mode_Dir :: File_Mode(1<<16)
|
||||
File_Mode_Named_Pipe :: File_Mode(1<<17)
|
||||
File_Mode_Device :: File_Mode(1<<18)
|
||||
File_Mode_Char_Device :: File_Mode(1<<19)
|
||||
File_Mode_Sym_Link :: File_Mode(1<<20)
|
||||
/*
|
||||
Type representing the type of a file handle.
|
||||
|
||||
File_Mode_Perm :: File_Mode(0o777) // Unix permision bits
|
||||
**Note(windows)**: Socket handles can not be distinguished from
|
||||
files, as they are just a normal file handle that is being treated by
|
||||
a special driver. Windows also makes no distinction between block and
|
||||
character devices.
|
||||
*/
|
||||
File_Type :: enum {
|
||||
// The type of a file could not be determined for the current platform.
|
||||
Undetermined,
|
||||
// Represents a regular file.
|
||||
Regular,
|
||||
// Represents a directory.
|
||||
Directory,
|
||||
// Represents a symbolic link.
|
||||
Symlink,
|
||||
// Represents a named pipe (FIFO).
|
||||
Named_Pipe,
|
||||
// Represents a socket.
|
||||
// **Note(windows)**: Not returned on windows
|
||||
Socket,
|
||||
// Represents a block device.
|
||||
// **Note(windows)**: On windows represents all devices.
|
||||
Block_Device,
|
||||
// Represents a character device.
|
||||
// **Note(windows)**: Not returned on windows
|
||||
Character_Device,
|
||||
}
|
||||
|
||||
File_Flags :: distinct bit_set[File_Flag; uint]
|
||||
File_Flag :: enum {
|
||||
@@ -51,11 +88,11 @@ stderr: ^File = nil // OS-Specific
|
||||
|
||||
@(require_results)
|
||||
create :: proc(name: string) -> (^File, Error) {
|
||||
return open(name, {.Read, .Write, .Create}, File_Mode(0o777))
|
||||
return open(name, {.Read, .Write, .Create}, 0o777)
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
open :: proc(name: string, flags := File_Flags{.Read}, perm := File_Mode(0o777)) -> (^File, Error) {
|
||||
open :: proc(name: string, flags := File_Flags{.Read}, perm := 0o777) -> (^File, Error) {
|
||||
return _open(name, flags, perm)
|
||||
}
|
||||
|
||||
@@ -161,44 +198,56 @@ read_link :: proc(name: string, allocator: runtime.Allocator) -> (string, Error)
|
||||
|
||||
|
||||
chdir :: change_directory
|
||||
|
||||
change_directory :: proc(name: string) -> Error {
|
||||
return _chdir(name)
|
||||
}
|
||||
|
||||
chmod :: change_mode
|
||||
change_mode :: proc(name: string, mode: File_Mode) -> Error {
|
||||
|
||||
change_mode :: proc(name: string, mode: int) -> Error {
|
||||
return _chmod(name, mode)
|
||||
}
|
||||
|
||||
chown :: change_owner
|
||||
|
||||
change_owner :: proc(name: string, uid, gid: int) -> Error {
|
||||
return _chown(name, uid, gid)
|
||||
}
|
||||
|
||||
fchdir :: fchange_directory
|
||||
|
||||
fchange_directory :: proc(f: ^File) -> Error {
|
||||
return _fchdir(f)
|
||||
}
|
||||
|
||||
fchmod :: fchange_mode
|
||||
fchange_mode :: proc(f: ^File, mode: File_Mode) -> Error {
|
||||
|
||||
fchange_mode :: proc(f: ^File, mode: int) -> Error {
|
||||
return _fchmod(f, mode)
|
||||
}
|
||||
|
||||
fchown :: fchange_owner
|
||||
|
||||
fchange_owner :: proc(f: ^File, uid, gid: int) -> Error {
|
||||
return _fchown(f, uid, gid)
|
||||
}
|
||||
|
||||
|
||||
lchown :: change_owner_do_not_follow_links
|
||||
|
||||
change_owner_do_not_follow_links :: proc(name: string, uid, gid: int) -> Error {
|
||||
return _lchown(name, uid, gid)
|
||||
}
|
||||
|
||||
chtimes :: change_times
|
||||
|
||||
change_times :: proc(name: string, atime, mtime: time.Time) -> Error {
|
||||
return _chtimes(name, atime, mtime)
|
||||
}
|
||||
|
||||
fchtimes :: fchange_times
|
||||
|
||||
fchange_times :: proc(f: ^File, atime, mtime: time.Time) -> Error {
|
||||
return _fchtimes(f, atime, mtime)
|
||||
}
|
||||
@@ -214,6 +263,7 @@ is_file :: proc(path: string) -> bool {
|
||||
}
|
||||
|
||||
is_dir :: is_directory
|
||||
|
||||
@(require_results)
|
||||
is_directory :: proc(path: string) -> bool {
|
||||
return _is_dir(path)
|
||||
@@ -226,11 +276,11 @@ copy_file :: proc(dst_path, src_path: string) -> Error {
|
||||
|
||||
info := fstat(src, file_allocator()) or_return
|
||||
defer file_info_delete(info, file_allocator())
|
||||
if info.is_directory {
|
||||
if info.type == .Directory {
|
||||
return .Invalid_File
|
||||
}
|
||||
|
||||
dst := open(dst_path, {.Read, .Write, .Create, .Trunc}, info.mode & File_Mode_Perm) or_return
|
||||
dst := open(dst_path, {.Read, .Write, .Create, .Trunc}, info.mode & 0o777) or_return
|
||||
defer close(dst)
|
||||
|
||||
_, err := io.copy(to_writer(dst), to_reader(src))
|
||||
|
||||
Reference in New Issue
Block a user