fix(file): made some small fixes for tests and file functionality on linux

This commit is contained in:
Mineplay5780 2025-12-20 21:39:48 +01:00
parent 562eab114b
commit 02090f42da
3 changed files with 16 additions and 14 deletions

View file

@ -39,23 +39,26 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char
if (mode & COSMS_CORE_FILE_MODE_APPEND) { if (mode & COSMS_CORE_FILE_MODE_APPEND) {
flags |= O_APPEND; flags |= O_APPEND;
} else { } else if (mode & COSMS_CORE_FILE_MODE_CREATE) {
flags |= O_TRUNC; flags |= (O_TRUNC | O_CREAT);
if (mode & COSMS_CORE_FILE_MODE_CREATE) {
flags |= O_CREAT;
}
} }
#ifdef O_LARGEFILE
struct stat file_stat; struct stat file_stat;
if (stat(path, &file_stat) != 0) { if (stat(path, &file_stat) != 0) {
if (errno == ENOENT) {
return COSMS_CORE_FILE_NOT_FOUND;
}
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
} }
if (file_stat.st_size > COSMS_CORE_FILE_LARGE) { if (file_stat.st_size > COSMS_CORE_FILE_LARGE) {
flags |= O_LARGEFILE; flags |= O_LARGEFILE;
} }
#endif
file->native_file = open(path, flags); file->native_file = open(path, flags, 0644);
if (file->native_file == -1) { if (file->native_file == -1) {
switch(errno) { switch(errno) {
case ENOENT: case ENOENT:
@ -232,7 +235,7 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char *
current_bytes_to_read = (unsigned int)remaining_bytes; 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) { if (read_bytes == -1) {
free(*buffer); free(*buffer);
return COSMS_CORE_FILE_FAILED_TO_READ; return COSMS_CORE_FILE_FAILED_TO_READ;
@ -330,7 +333,7 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
current_bytes_to_write = (unsigned int)remaining_bytes; 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) { if (written_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_WRITE; return COSMS_CORE_FILE_FAILED_TO_WRITE;
} }

Binary file not shown.

View file

@ -170,7 +170,7 @@ COSMS_CORE_TEST_TEST(file_close,
COSMS_CORE_TEST_TEST(file_close_non_existing_file, COSMS_CORE_TEST_TEST(file_close_non_existing_file,
struct cosms_core_file file; struct cosms_core_file file;
file.native_file = 0; file.native_file = -1;
CosmsCoreFileError error = cosms_core_file_close(&file); CosmsCoreFileError error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_INVALID_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, COSMS_CORE_TEST_TEST(file_size_non_existing_file,
struct cosms_core_file file; struct cosms_core_file file;
file.native_file = 0; file.native_file = -1;
unsigned long long size = 0; unsigned long long size = 0;
CosmsCoreFileError error = cosms_core_file_get_size(&file, &size); 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, COSMS_CORE_TEST_TEST(file_read_non_existing_file,
struct cosms_core_file file; struct cosms_core_file file;
file.mode = COSMS_CORE_FILE_MODE_READ; file.mode = COSMS_CORE_FILE_MODE_READ;
file.native_file = 0; file.native_file = -1;
unsigned int bytes_to_read = 13; unsigned int bytes_to_read = 13;
unsigned int bytes_read = 0; 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, COSMS_CORE_TEST_TEST(file_read_full_non_existing_file,
struct cosms_core_file file; struct cosms_core_file file;
file.mode = COSMS_CORE_FILE_MODE_READ; file.mode = COSMS_CORE_FILE_MODE_READ;
file.native_file = 0; file.native_file = -1;
unsigned long long bytes_read = 0; unsigned long long bytes_read = 0;
char *buffer = NULL; char *buffer = NULL;
@ -665,7 +665,6 @@ COSMS_CORE_TEST_TEST(file_write_using_wrong_mode,
error = cosms_core_file_close(&file); error = cosms_core_file_close(&file);
if (error != COSMS_CORE_FILE_OK) { if (error != COSMS_CORE_FILE_OK) {
free(write_buffer);
return cosms_core_file_error_string(error); return cosms_core_file_error_string(error);
} }