diff --git a/src/file.c b/src/file.c index ff5b8fc..c45fd6c 100644 --- a/src/file.c +++ b/src/file.c @@ -36,23 +36,26 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char if (mode & COSMS_CORE_FILE_MODE_APPEND) { flags |= O_APPEND; - } else { - flags |= O_TRUNC; - if (mode & COSMS_CORE_FILE_MODE_CREATE) { - flags |= O_CREAT; - } + } else if (mode & COSMS_CORE_FILE_MODE_CREATE) { + flags |= (O_TRUNC | O_CREAT); } + #ifdef O_LARGEFILE struct stat file_stat; if (stat(path, &file_stat) != 0) { + if (errno == ENOENT) { + return COSMS_CORE_FILE_NOT_FOUND; + } + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; } - + if (file_stat.st_size > COSMS_CORE_FILE_LARGE) { flags |= O_LARGEFILE; } + #endif - file->native_file = open(path, flags); + file->native_file = open(path, flags, 0644); if (file->native_file == -1) { switch(errno) { case ENOENT: @@ -217,7 +220,7 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char * current_bytes_to_read = (unsigned int)remaining_bytes; } - int read_bytes = read(file, (*buffer) + (file_stat.st_size - remaining_bytes), current_bytes_to_read); + int read_bytes = read(file->native_file, (*buffer) + (file_stat.st_size - remaining_bytes), current_bytes_to_read); if (read_bytes == -1) { free(*buffer); return COSMS_CORE_FILE_FAILED_TO_READ; @@ -309,7 +312,7 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char current_bytes_to_write = (unsigned int)remaining_bytes; } - int written_bytes = write(file->native_file, content + (bytes_to_write - remaining_bytes), current_bytes_to_write); + int written_bytes = write(file->native_file, buffer + (bytes_to_write - remaining_bytes), current_bytes_to_write); if (written_bytes == -1) { return COSMS_CORE_FILE_FAILED_TO_WRITE; } diff --git a/tests/data/large-file.txt b/tests/data/large-file.txt index d4c5011..d6cf58d 100644 Binary files a/tests/data/large-file.txt and b/tests/data/large-file.txt differ diff --git a/tests/unit/file.c b/tests/unit/file.c index b95958c..a80a0cc 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -170,7 +170,7 @@ COSMS_CORE_TEST_TEST(file_close, COSMS_CORE_TEST_TEST(file_close_non_existing_file, struct cosms_core_file file; - file.native_file = 0; + file.native_file = -1; CosmsCoreFileError error = cosms_core_file_close(&file); if (error != COSMS_CORE_FILE_INVALID_FILE) { @@ -236,7 +236,7 @@ COSMS_CORE_TEST_TEST(file_size_large, COSMS_CORE_TEST_TEST(file_size_non_existing_file, struct cosms_core_file file; - file.native_file = 0; + file.native_file = -1; unsigned long long size = 0; CosmsCoreFileError error = cosms_core_file_get_size(&file, &size); @@ -330,7 +330,7 @@ COSMS_CORE_TEST_TEST(file_read_large_file, COSMS_CORE_TEST_TEST(file_read_non_existing_file, struct cosms_core_file file; file.mode = COSMS_CORE_FILE_MODE_READ; - file.native_file = 0; + file.native_file = -1; unsigned int bytes_to_read = 13; unsigned int bytes_read = 0; @@ -347,7 +347,7 @@ COSMS_CORE_TEST_TEST(file_read_non_existing_file, COSMS_CORE_TEST_TEST(file_read_full_non_existing_file, struct cosms_core_file file; file.mode = COSMS_CORE_FILE_MODE_READ; - file.native_file = 0; + file.native_file = -1; unsigned long long bytes_read = 0; char *buffer = NULL; @@ -665,7 +665,6 @@ COSMS_CORE_TEST_TEST(file_write_using_wrong_mode, error = cosms_core_file_close(&file); if (error != COSMS_CORE_FILE_OK) { - free(write_buffer); return cosms_core_file_error_string(error); }