Files
Odin/core/mem/virtual/file.odin
imp0s5ible e2bb7d70af Make map_file_from_path use the open flags
map_file_from_path now passes the appropriate flags on to os.open
instead of always calling it with os.O_RDWR.
It will no longer try to open a file with write permissions if the user
didn't request write access to the file mapping (or vice-versa).
2026-03-20 15:21:12 +01:00

48 lines
962 B
Odin

#+build !freestanding
#+build !js
package mem_virtual
import "core:os"
map_file :: proc{
map_file_from_path,
map_file_from_file,
}
map_file_from_path :: proc(filename: string, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
open_flags : os.File_Flags
if .Read in flags {
open_flags += {.Read}
}
if .Write in flags {
open_flags += {.Write}
}
f, err := os.open(filename, open_flags)
if err != nil {
return nil, .Open_Failure
}
defer os.close(f)
return map_file_from_file(f, flags)
}
map_file_from_file :: proc(f: ^os.File, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
size, os_err := os.file_size(f)
if os_err != nil {
return nil, .Stat_Failure
}
if size < 0 {
return nil, .Negative_Size
}
if size != i64(int(size)) {
return nil, .Too_Large_Size
}
fd := os.fd(f)
return _map_file(fd, size, flags)
}
unmap_file :: proc(data: []byte) {
if raw_data(data) != nil {
_unmap_file(data)
}
}