2024-02-08 07:50:36 -08:00
|
|
|
// TODO(Ed) : Move this to a grime package
|
2024-01-21 20:38:02 -08:00
|
|
|
package sectr
|
|
|
|
|
|
|
|
import "core:fmt"
|
|
|
|
import "core:os"
|
|
|
|
import "core:runtime"
|
|
|
|
|
2024-03-07 16:15:54 -08:00
|
|
|
file_copy_sync :: proc( path_src, path_dst: string, allocator := context.temp_allocator ) -> b32
|
2024-01-21 20:38:02 -08:00
|
|
|
{
|
2024-01-22 00:47:53 -08:00
|
|
|
file_size : i64
|
2024-01-21 20:38:02 -08:00
|
|
|
{
|
2024-03-07 16:15:54 -08:00
|
|
|
path_info, result := file_status( path_src, allocator )
|
2024-01-21 20:38:02 -08:00
|
|
|
if result != os.ERROR_NONE {
|
2024-02-08 19:33:53 -08:00
|
|
|
logf("Could not get file info: %v", result, LogLevel.Error )
|
2024-01-21 20:38:02 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
file_size = path_info.size
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:15:54 -08:00
|
|
|
src_content, result := os.read_entire_file( path_src, allocator )
|
2024-01-21 20:38:02 -08:00
|
|
|
if ! result {
|
2024-02-08 19:33:53 -08:00
|
|
|
logf( "Failed to read file to copy: %v", path_src, LogLevel.Error )
|
2024-01-21 20:38:02 -08:00
|
|
|
runtime.debug_trap()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
result = os.write_entire_file( path_dst, src_content, false )
|
|
|
|
if ! result {
|
2024-02-08 19:33:53 -08:00
|
|
|
logf( "Failed to copy file: %v", path_dst, LogLevel.Error )
|
2024-01-21 20:38:02 -08:00
|
|
|
runtime.debug_trap()
|
|
|
|
return false
|
|
|
|
}
|
2024-01-22 00:47:53 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-02-23 06:36:23 -08:00
|
|
|
file_exists :: proc( file_path : string ) -> b32 {
|
2024-03-07 16:15:54 -08:00
|
|
|
path_info, result := file_status( file_path, frame_allocator() )
|
2024-02-08 07:50:36 -08:00
|
|
|
if result != os.ERROR_NONE {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
file_is_locked :: proc( file_path : string ) -> b32 {
|
|
|
|
handle, err := file_open(file_path, os.O_RDONLY)
|
2024-01-22 00:47:53 -08:00
|
|
|
if err != os.ERROR_NONE {
|
|
|
|
// If the error indicates the file is in use, return true.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file opens successfully, close it and return false.
|
2024-02-27 04:50:57 -08:00
|
|
|
file_close(handle)
|
2024-01-22 00:47:53 -08:00
|
|
|
return false
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
2024-02-08 13:05:15 -08:00
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
file_rewind :: proc( file : os.Handle ) {
|
|
|
|
file_seek( file, 0, 0 )
|
2024-02-08 13:05:15 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
file_read_looped :: proc( file : os.Handle, data : []byte ) {
|
|
|
|
total_read, result_code := file_read( file, data )
|
2024-02-08 13:05:15 -08:00
|
|
|
if result_code == os.ERROR_HANDLE_EOF {
|
2024-02-27 04:50:57 -08:00
|
|
|
file_rewind( file )
|
2024-02-08 13:05:15 -08:00
|
|
|
}
|
|
|
|
}
|