moved file system handling to unix
This commit is contained in:
parent
88de96e68a
commit
4abdf2f530
|
@ -52,7 +52,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <memory.h>
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <xmmintrin.h>
|
#include <xmmintrin.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
@ -98,7 +97,8 @@
|
||||||
#define LINUX_FN_DEBUG(fmt, ...)
|
#define LINUX_FN_DEBUG(fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define InterlockedCompareExchange(dest, ex, comp) __sync_val_compare_and_swap((dest), (comp), (ex))
|
#define InterlockedCompareExchange(dest, ex, comp) \
|
||||||
|
__sync_val_compare_and_swap((dest), (comp), (ex))
|
||||||
|
|
||||||
//
|
//
|
||||||
// Linux structs / enums
|
// Linux structs / enums
|
||||||
|
@ -240,68 +240,6 @@ internal Sys_Release_Lock_Sig(system_release_lock);
|
||||||
internal void system_wait_cv(i32, i32);
|
internal void system_wait_cv(i32, i32);
|
||||||
internal void system_signal_cv(i32, i32);
|
internal void system_signal_cv(i32, i32);
|
||||||
|
|
||||||
//
|
|
||||||
// File System
|
|
||||||
//
|
|
||||||
|
|
||||||
internal
|
|
||||||
Sys_File_Exists_Sig(system_file_exists){
|
|
||||||
int result = 0;
|
|
||||||
char buff[PATH_MAX] = {};
|
|
||||||
|
|
||||||
if(len + 1 > PATH_MAX){
|
|
||||||
fputs("system_directory_has_file: path too long\n", stderr);
|
|
||||||
} else {
|
|
||||||
memcpy(buff, filename, len);
|
|
||||||
buff[len] = 0;
|
|
||||||
struct stat st;
|
|
||||||
result = stat(buff, &st) == 0 && S_ISREG(st.st_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
LINUX_FN_DEBUG("%s: %d", buff, result);
|
|
||||||
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal
|
|
||||||
Sys_Directory_CD_Sig(system_directory_cd){
|
|
||||||
String directory = make_string_cap(dir, *len, cap);
|
|
||||||
b32 result = 0;
|
|
||||||
i32 old_size;
|
|
||||||
|
|
||||||
if (rel_path[0] != 0){
|
|
||||||
if (rel_path[0] == '.' && rel_path[1] == 0){
|
|
||||||
result = 1;
|
|
||||||
}
|
|
||||||
else if (rel_path[0] == '.' && rel_path[1] == '.' && rel_path[2] == 0){
|
|
||||||
result = remove_last_folder(&directory);
|
|
||||||
terminate_with_null(&directory);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (directory.size + rel_len + 1 > directory.memory_size){
|
|
||||||
old_size = directory.size;
|
|
||||||
append_partial_sc(&directory, rel_path);
|
|
||||||
append_s_char(&directory, '/');
|
|
||||||
terminate_with_null(&directory);
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if (stat(directory.str, &st) == 0 && S_ISDIR(st.st_mode)){
|
|
||||||
result = 1;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
directory.size = old_size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*len = directory.size;
|
|
||||||
|
|
||||||
LINUX_FN_DEBUG("%.*s: %d", directory.size, directory.str, result);
|
|
||||||
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal
|
internal
|
||||||
Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){
|
Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){
|
||||||
linuxvars.hide_cursor = !show;
|
linuxvars.hide_cursor = !show;
|
||||||
|
|
|
@ -145,13 +145,6 @@ Sys_Memory_Free_Sig(system_memory_free){
|
||||||
|
|
||||||
internal
|
internal
|
||||||
Sys_Set_File_List_Sig(system_set_file_list){
|
Sys_Set_File_List_Sig(system_set_file_list){
|
||||||
DIR *d;
|
|
||||||
struct dirent *entry;
|
|
||||||
char *fname, *cursor, *cursor_start;
|
|
||||||
File_Info *info_ptr;
|
|
||||||
i32 character_count, file_count, size, required_size, length;
|
|
||||||
b32 clear_list = false;
|
|
||||||
|
|
||||||
if (directory == 0){
|
if (directory == 0){
|
||||||
system_memory_free(file_list->block, file_list->block_size);
|
system_memory_free(file_list->block, file_list->block_size);
|
||||||
file_list->block = 0;
|
file_list->block = 0;
|
||||||
|
@ -163,8 +156,8 @@ Sys_Set_File_List_Sig(system_set_file_list){
|
||||||
|
|
||||||
LOGF("%s", directory);
|
LOGF("%s", directory);
|
||||||
|
|
||||||
d = opendir(directory);
|
DIR *d = opendir(directory);
|
||||||
if (d){
|
if (d != 0){
|
||||||
if (canon_directory_out != 0){
|
if (canon_directory_out != 0){
|
||||||
u32 length = copy_fast_unsafe_cc(canon_directory_out, directory);
|
u32 length = copy_fast_unsafe_cc(canon_directory_out, directory);
|
||||||
if (canon_directory_out[length-1] != '/'){
|
if (canon_directory_out[length-1] != '/'){
|
||||||
|
@ -174,21 +167,22 @@ Sys_Set_File_List_Sig(system_set_file_list){
|
||||||
*canon_directory_size_out = length;
|
*canon_directory_size_out = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
character_count = 0;
|
i32 character_count = 0;
|
||||||
file_count = 0;
|
i32 file_count = 0;
|
||||||
for (entry = readdir(d);
|
for (struct dirent *entry = readdir(d);
|
||||||
entry != 0;
|
entry != 0;
|
||||||
entry = readdir(d)){
|
entry = readdir(d)){
|
||||||
fname = entry->d_name;
|
char *fname = entry->d_name;
|
||||||
if (match_cc(fname, ".") || match_cc(fname, "..")){
|
if (match_cc(fname, ".") || match_cc(fname, "..")){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
++file_count;
|
++file_count;
|
||||||
for (size = 0; fname[size]; ++size);
|
i32 size = 0;
|
||||||
|
for (; fname[size]; ++size);
|
||||||
character_count += size + 1;
|
character_count += size + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
required_size = character_count + file_count * sizeof(File_Info);
|
i32 required_size = character_count + file_count * sizeof(File_Info);
|
||||||
if (file_list->block_size < required_size){
|
if (file_list->block_size < required_size){
|
||||||
system_memory_free(file_list->block, file_list->block_size);
|
system_memory_free(file_list->block, file_list->block_size);
|
||||||
file_list->block = system_memory_allocate(required_size);
|
file_list->block = system_memory_allocate(required_size);
|
||||||
|
@ -196,30 +190,32 @@ Sys_Set_File_List_Sig(system_set_file_list){
|
||||||
}
|
}
|
||||||
|
|
||||||
file_list->infos = (File_Info*)file_list->block;
|
file_list->infos = (File_Info*)file_list->block;
|
||||||
cursor = (char*)(file_list->infos + file_count);
|
char *cursor = (char*)(file_list->infos + file_count);
|
||||||
|
|
||||||
if (file_list->block != 0){
|
if (file_list->block != 0){
|
||||||
rewinddir(d);
|
rewinddir(d);
|
||||||
info_ptr = file_list->infos;
|
File_Info *info_ptr = file_list->infos;
|
||||||
for (entry = readdir(d);
|
for (struct dirent *entry = readdir(d);
|
||||||
entry != 0;
|
entry != 0;
|
||||||
entry = readdir(d)){
|
entry = readdir(d)){
|
||||||
fname = entry->d_name;
|
char *fname = entry->d_name;
|
||||||
if (match_cc(fname, ".") || match_cc(fname, "..")){
|
if (match_cc(fname, ".") || match_cc(fname, "..")){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cursor_start = cursor;
|
char *cursor_start = cursor;
|
||||||
length = copy_fast_unsafe_cc(cursor_start, fname);
|
i32 length = copy_fast_unsafe_cc(cursor_start, fname);
|
||||||
cursor += length;
|
cursor += length;
|
||||||
|
|
||||||
if(entry->d_type == DT_LNK){
|
if(entry->d_type == DT_LNK){
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if(stat(entry->d_name, &st) != -1){
|
if(stat(entry->d_name, &st) != -1){
|
||||||
info_ptr->folder = S_ISDIR(st.st_mode);
|
info_ptr->folder = S_ISDIR(st.st_mode);
|
||||||
} else {
|
}
|
||||||
|
else{
|
||||||
info_ptr->folder = 0;
|
info_ptr->folder = 0;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else{
|
||||||
info_ptr->folder = (entry->d_type == DT_DIR);
|
info_ptr->folder = (entry->d_type == DT_DIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +229,8 @@ Sys_Set_File_List_Sig(system_set_file_list){
|
||||||
file_list->count = file_count;
|
file_list->count = file_count;
|
||||||
|
|
||||||
closedir(d);
|
closedir(d);
|
||||||
} else {
|
}
|
||||||
|
else{
|
||||||
system_memory_free(file_list->block, file_list->block_size);
|
system_memory_free(file_list->block, file_list->block_size);
|
||||||
file_list->block = 0;
|
file_list->block = 0;
|
||||||
file_list->block_size = 0;
|
file_list->block_size = 0;
|
||||||
|
@ -322,7 +319,8 @@ Sys_Load_Size_Sig(system_load_size){
|
||||||
|
|
||||||
if(fstat(fd, &st) == -1){
|
if(fstat(fd, &st) == -1){
|
||||||
LOG("fstat");
|
LOG("fstat");
|
||||||
} else {
|
}
|
||||||
|
else{
|
||||||
result = st.st_size;
|
result = st.st_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,6 +330,7 @@ Sys_Load_Size_Sig(system_load_size){
|
||||||
internal
|
internal
|
||||||
Sys_Load_File_Sig(system_load_file){
|
Sys_Load_File_Sig(system_load_file){
|
||||||
int fd = *(int*)&handle;
|
int fd = *(int*)&handle;
|
||||||
|
|
||||||
do{
|
do{
|
||||||
ssize_t n = read(fd, buffer, size);
|
ssize_t n = read(fd, buffer, size);
|
||||||
if(n == -1){
|
if(n == -1){
|
||||||
|
@ -339,7 +338,8 @@ Sys_Load_File_Sig(system_load_file){
|
||||||
LOG("read");
|
LOG("read");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else{
|
||||||
size -= n;
|
size -= n;
|
||||||
buffer += n;
|
buffer += n;
|
||||||
}
|
}
|
||||||
|
@ -387,6 +387,67 @@ Sys_Save_File_Sig(system_save_file){
|
||||||
return (size == 0);
|
return (size == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// File System
|
||||||
|
//
|
||||||
|
|
||||||
|
internal
|
||||||
|
Sys_File_Exists_Sig(system_file_exists){
|
||||||
|
int result = 0;
|
||||||
|
char buff[PATH_MAX] = {};
|
||||||
|
|
||||||
|
if (len + 1 > PATH_MAX){
|
||||||
|
LOG("system_directory_has_file: path too long\n");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
memcpy(buff, filename, len);
|
||||||
|
buff[len] = 0;
|
||||||
|
struct stat st;
|
||||||
|
result = stat(buff, &st) == 0 && S_ISREG(st.st_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGF("%s: %d", buff, result);
|
||||||
|
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal
|
||||||
|
Sys_Directory_CD_Sig(system_directory_cd){
|
||||||
|
String directory = make_string_cap(dir, *len, cap);
|
||||||
|
b32 result = false;
|
||||||
|
|
||||||
|
if (rel_path[0] != 0){
|
||||||
|
if (rel_path[0] == '.' && rel_path[1] == 0){
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else if (rel_path[0] == '.' && rel_path[1] == '.' && rel_path[2] == 0){
|
||||||
|
result = remove_last_folder(&directory);
|
||||||
|
terminate_with_null(&directory);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if (directory.size + rel_len + 1 > directory.memory_size){
|
||||||
|
i32 old_size = directory.size;
|
||||||
|
append_partial_sc(&directory, rel_path);
|
||||||
|
append_s_char(&directory, '/');
|
||||||
|
terminate_with_null(&directory);
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
if (stat(directory.str, &st) == 0 && S_ISDIR(st.st_mode)){
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
directory.size = old_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*len = directory.size;
|
||||||
|
LOGF("%.*s: %d", directory.size, directory.str, result);
|
||||||
|
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Time
|
// Time
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue