aboutsummaryrefslogtreecommitdiff
path: root/src/std/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/std/file.c')
-rw-r--r--src/std/file.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/std/file.c b/src/std/file.c
index 177ccbc..7de2b6c 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -6,6 +6,8 @@
#include <errno.h>
#include <assert.h>
#include <string.h>
+#include <limits.h>
+#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
@@ -177,7 +179,7 @@ typedef enum {
static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) {
struct stat file_stats;
if(stat(filepath, &file_stats) == -1) {
- amal_log_perror(filepath);
+ amal_log_error("file_get_type: %s failed: ", filepath, strerror(errno));
return -1;
}
@@ -202,12 +204,11 @@ int read_whole_file(const char *filepath, char **data, usize *size) {
return -2;
}
- result = 0;
file = fopen(filepath, "rb");
if(!file) {
int error;
error = errno;
- amal_log_perror(filepath);
+ amal_log_error("read_whole_file: %s failed: ", filepath, strerror(error));
return error;
}
@@ -241,6 +242,21 @@ int read_whole_file(const char *filepath, char **data, usize *size) {
return result;
}
+int file_get_canonical_path(const char *filepath, char **result_path, usize *result_path_size) {
+ char result_path_tmp[PATH_MAX];
+ if(!realpath(filepath, result_path_tmp)) {
+ int ret;
+ ret = errno;
+ amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(ret));
+ return ret;
+ }
+ *result_path_size = strnlen(result_path_tmp, PATH_MAX);
+ return_if_error(am_malloc(*result_path_size + 1, (void**)result_path));
+ am_memcpy(*result_path, result_path_tmp, *result_path_size);
+ (*result_path)[*result_path_size] = '\0';
+ return 0;
+}
+
BufferView file_get_parent_directory(BufferView filepath) {
int i;
for(i = filepath.size - 1; i >= 0; --i) {