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.c47
1 files changed, 10 insertions, 37 deletions
diff --git a/src/std/file.c b/src/std/file.c
index d61e871..f0d769e 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -170,49 +170,15 @@ int mapped_file_deinit(MappedFile *self) {
}
#endif
-typedef enum {
- REGULAR,
- DIRECTORY,
- OTHER
-} FileType;
-
-/*
- TODO: Remove this and instead use fstat, and then read the file.
- Otherwise this is not atomic and the file type can change between the calls
-*/
-static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) {
- struct stat file_stats;
- if(stat(filepath, &file_stats) == -1) {
- amal_log_error("file_get_type: %s failed, error: %s", filepath, strerror(errno));
- return -1;
- }
-
- if(file_stats.st_mode & S_IFDIR)
- *type = DIRECTORY;
- else if(file_stats.st_mode & S_IFREG)
- *type = REGULAR;
- else
- *type = OTHER;
- return 0;
-}
-
int read_whole_file(const char *filepath, Buffer *data_result) {
- FileType file_type;
FILE *file;
int result;
usize bytes_read;
- usize size;
-
- return_if_error(file_get_type(filepath, &file_type));
- if(file_type != REGULAR) {
- amal_log_error("Expected file %s to be a regular file", filepath);
- return -2;
- }
+ long size;
file = fopen(filepath, "rb");
if(!file) {
- int error;
- error = errno;
+ int error = errno;
amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error));
return error;
}
@@ -224,6 +190,13 @@ 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));
+ goto cleanup;
+ }
+
result = fseek(file, 0, SEEK_SET);
if(result != 0) {
result = ferror(file);
@@ -239,7 +212,7 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
assert(data_result->capacity == 0 && data_result->size == 0);
cleanup_if_error(buffer_append_empty(data_result, size));
bytes_read = fread(data_result->data, 1, data_result->size, file);
- if(bytes_read != size)
+ if(bytes_read != (unsigned long)size)
result = ferror(file);
cleanup: