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.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/std/file.c b/src/std/file.c
index 4542aba..d486843 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -28,10 +28,8 @@ static int scan_dir_recursive_internal(char *dir_path, int path_length, scan_dir
path_length_start = path_length;
if((dir = opendir(dir_path)) == NULL) {
- int error;
- error = errno;
amal_log_perror("scan_dir_recursive");
- return error;
+ return -1;
}
while((entry = readdir(dir)) != NULL) {
@@ -178,9 +176,16 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
file = fopen(filepath, "rb");
if(!file) {
- int error = errno;
- amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error));
- return error;
+ amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(errno));
+ return -1;
+ }
+
+ int fd = fileno(file);
+ struct stat s;
+ if(fstat(fd, &s) == -1 || !S_ISREG(s.st_mode)) {
+ amal_log_error("read_whole_file: %s failed, error: attempted to read a non-file", filepath);
+ fclose(file);
+ return -1;
}
result = fseek(file, 0, SEEK_END);
@@ -191,9 +196,8 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
size = ftell(file);
if(size == -1) {
- int error = errno;
- result = error;
- amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error));
+ result = -1;
+ amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(errno));
goto cleanup;
}
@@ -223,10 +227,8 @@ int read_whole_file(const char *filepath, Buffer *data_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;
+ amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(errno));
+ return -1;
}
*result_path_size = strnlen(result_path_tmp, PATH_MAX);
return_if_error(am_malloc(*result_path_size + 1, (void**)result_path));