Compare commits

..

3 commits

Author SHA1 Message Date
Mineplay5780
79e9bf2a8c feat(file): implemented getting file size 2025-12-17 20:40:01 +01:00
Mineplay5780
84b4f0dc59 refactor(file): removed boolean override for mode in write function 2025-12-17 20:36:35 +01:00
Mineplay5780
5498b060b8 feat(file): added manually opening and closing files 2025-12-17 20:31:33 +01:00
3 changed files with 189 additions and 22 deletions

View file

@ -9,7 +9,9 @@
#ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE
#include <stdbool.h>
#if defined(_WIN32)
#include <windows.h>
#endif
typedef enum {
COSMS_CORE_FILE_OK = 0,
@ -22,10 +24,33 @@ typedef enum {
COSMS_CORE_FILE_FAILED_TO_READ = -7,
COSMS_CORE_FILE_FAILED_TO_WRITE = -8,
COSMS_CORE_FILE_STILL_OPEN = -9,
COSMS_CORE_FILE_INVALID_FILE = -10
} CosmsCoreFileError;
#define COSMS_CORE_FILE_LARGE 0x7FFFFFFF
#define COSMS_CORE_FILE_MODE_READ (1u << 0)
#define COSMS_CORE_FILE_MODE_WRITE (1u << 1)
#define COSMS_CORE_FILE_MODE_APPEND (1u << 2)
#define COSMS_CORE_FILE_MODE_CREATE (1u << 3)
struct cosms_core_file {
int mode;
#if defined(__GNUC__)
int native_file;
#elif defined(_WIN32)
HANDLE native_file;
#endif
};
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, int mode);
CosmsCoreFileError cosms_core_file_delete(const char *path);

View file

@ -19,14 +19,38 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
file->mode = mode;
#if defined(__GNUC__)
int file = open(path, O_RDONLY);
if (file == -1) {
int flags = 0;
if (mode & (COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE)) {
flags |= O_RDWR;
} else if (mode & COSMS_CORE_FILE_MODE_READ) {
flags |= O_RDONLY;
} else if (mode & COSMS_CORE_FILE_MODE_WRITE) {
flags |= (O_WRONLY | O_TRUNC);
} else if (mode & COSMS_CORE_FILE_MODE_APPEND) {
flags |= (O_WRONLY | O_APPEND);
}
if (mode & COSMS_CORE_FILE_MODE_CREATE) {
flags |= O_CREAT;
}
struct stat file_stat;
if (stat(path, &file_stat) != 0) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
if (file_stat.st_size > COSMS_CORE_FILE_LARGE) {
flags |= O_LARGEFILE;
}
file->native_file = open(path, flags);
if (file->native_file == -1) {
switch(errno) {
case ENOENT:
return COSMS_CORE_FILE_NOT_FOUND;
@ -42,13 +66,127 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#elif defined(_WIN32)
DWORD access_flags = 0;
DWORD create_flag = 0;
if (mode & COSMS_CORE_FILE_MODE_READ) {
access_flags |= GENERIC_READ;
}
if (mode & COSMS_CORE_FILE_MODE_WRITE) {
access_flags |= GENERIC_WRITE;
}
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
access_flags |= GENERIC_WRITE;
create_flag = OPEN_ALWAYS;
}
if (mode & COSMS_CORE_FILE_MODE_CREATE) {
create_flag = CREATE_ALWAYS;
}
file->native_file = CreateFile(path, access_flags, 0, NULL, create_flag, FILE_ATTRIBUTE_NORMAL, NULL);
if (file->native_file == INVALID_HANDLE_VALUE) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_CORE_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
return COSMS_CORE_FILE_NO_ACCESS;
case ERROR_TOO_MANY_OPEN_FILES:
return COSMS_CORE_FILE_LIMIT_REACHED;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
#if defined(__GNUC__)
int error = close(file->native_file);
if (error == -1) {
if (errno == EBADF) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
#elif defined(_WIN32)
BOOL error = CloseHandle(file);
if (error == 0) {
if (GetLastError() == ERROR_INVALID_HANDLE) {
return COSMS_CORE_FILE_INVALID_FILE;
}
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_get_size(unsigned long long *size) {
#if defined(__GNUC__)
struct stat file_stat;
if (stat(path, &file_stat) != 0) {
close(file);
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
(*size) = file_stat.st_size;
#elif defined(_WIN32)
DWORD high_size;
DWORD low_size = GetFileSize(file, &high_size);
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
CloseHandle(file);
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
(*size) = ((unsigned long long)high_size << 32) | low_size;
#endif
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
#if defined(__GNUC__)
struct stat file_stat;
if (stat(path, &file_stat) != 0) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
int flags = O_RDONLY;
if (file_stat.st_size > COSMS_CORE_FILE_LARGE) {
flags |= O_LARGEFILE;
}
int file = open(path, flags);
if (file == -1) {
switch(errno) {
case ENOENT:
return COSMS_CORE_FILE_NOT_FOUND;
case EACCES:
return COSMS_CORE_FILE_NO_ACCESS;
case EMFILE:
case ENFILE:
return COSMS_CORE_FILE_LIMIT_REACHED;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
if (size != NULL) {
*size = file_stat.st_size;
}
@ -109,7 +247,6 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign
}
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
if (size != NULL) {
*size = file_size;
}
@ -145,15 +282,20 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign
return COSMS_CORE_FILE_OK;
}
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) {
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, int mode) {
#if defined(__GNUC__)
int file;
if (override) {
file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else {
file = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
int flags = O_WRONLY | O_CREAT;
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
file |= O_APPEND;
} else if (mode) {
file |= O_TRUNC;
}
if (size > COSMS_CORE_FILE_LARGE) {
flags |= O_LARGEFILE;
}
int file = open(path, flags, 0644);
if (file == -1) {
switch (errno) {
case ENOENT:
@ -192,10 +334,10 @@ CosmsCoreFileError cosms_core_file_write(const char *path, const char *content,
close(file);
#elif defined(_WIN32)
HANDLE file;
if (override) {
file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
} else {
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
file = CreateFile(path, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
} else {
file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
if (file == INVALID_HANDLE_VALUE) {

View file

@ -71,7 +71,7 @@ COSMS_CORE_TEST_TEST(file_read,
COSMS_CORE_TEST_TEST(file_write,
COSMS_CORE_TEST_SUB_TEST(writting small file,
char *file_buffer = "Hello, World!";
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true);
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_WRITE);
if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
@ -103,7 +103,7 @@ COSMS_CORE_TEST_TEST(file_write,
memset(large_file_buffer, 'a', large_file_size);
large_file_buffer[large_file_size] = '\0';
CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true);
CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, COSMS_CORE_FILE_MODE_WRITE);
if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL);
@ -127,7 +127,7 @@ COSMS_CORE_TEST_TEST(file_write,
COSMS_CORE_TEST_SUB_TEST(appending file,
char *file_buffer = "Hello, World!";
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false);
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_APPEND);
if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
@ -150,7 +150,7 @@ COSMS_CORE_TEST_TEST(file_write,
COSMS_CORE_TEST_SUB_TEST(appending to non existing file,
char *file_buffer = "Hello, World!";
CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false);
CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_APPEND);
if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);