feat(file): added delete file function

This commit is contained in:
Mineplay5780 2025-12-11 22:38:30 +01:00
parent 3f0299bef1
commit 77f1518abc
8 changed files with 93 additions and 2 deletions

View file

@ -21,11 +21,14 @@ typedef enum {
COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6, COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6,
COSMS_CORE_FILE_FAILED_TO_READ = -7, COSMS_CORE_FILE_FAILED_TO_READ = -7,
COSMS_CORE_FILE_FAILED_TO_WRITE = -8, COSMS_CORE_FILE_FAILED_TO_WRITE = -8,
COSMS_CORE_FILE_STILL_OPEN = -9,
} CosmsCoreFileError; } CosmsCoreFileError;
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, 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, bool override);
CosmsCoreFileError cosms_core_file_delete(const char *path);
const char *cosms_core_file_error_string(CosmsCoreFileError error); const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif #endif

View file

@ -237,6 +237,46 @@ CosmsCoreFileError cosms_core_file_write(const char *path, const char *content,
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
CosmsCoreFileError cosms_core_file_delete(const char *path) {
#if defined(__GNUC__)
if (unlink(path) == -1) {
switch (errno) {
case ENOENT:
return COSMS_CORE_FILE_NOT_FOUND;
case EACCES:
return COSMS_CORE_FILE_NO_ACCESS;
case EBUSY:
return COSMS_CORE_FILE_STILL_OPEN;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#elif defined(_WIN32)
if (!DeleteFile(path)) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_CORE_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
return COSMS_CORE_FILE_NO_ACCESS;
case ERROR_LOCK_VIOLATION:
case ERROR_SHARING_VIOLATION:
return COSMS_CORE_FILE_STILL_OPEN;
default:
return COSMS_CORE_FILE_UNKOWN_ERROR;
}
}
#endif
return COSMS_CORE_FILE_OK;
}
const char *cosms_core_file_error_string(CosmsCoreFileError error) { const char *cosms_core_file_error_string(CosmsCoreFileError error) {
switch(error) { switch(error) {
case COSMS_CORE_FILE_OK: case COSMS_CORE_FILE_OK:
@ -266,6 +306,9 @@ const char *cosms_core_file_error_string(CosmsCoreFileError error) {
case COSMS_CORE_FILE_FAILED_TO_WRITE: case COSMS_CORE_FILE_FAILED_TO_WRITE:
return "cosms-file failed to write content to file"; return "cosms-file failed to write content to file";
case COSMS_CORE_FILE_STILL_OPEN:
return "cosms-file still open";
default: default:
return NULL; return NULL;
} }

View file

@ -1 +0,0 @@
Hello, World!

Binary file not shown.

View file

@ -1 +0,0 @@
Hello, World!Hello, World!

View file

@ -1,7 +1,9 @@
#include "unit/file.h"
#include "unit/unit.h" #include "unit/unit.h"
int main(void) { int main(void) {
cosms_core_file_read_test(); cosms_core_file_read_test();
cosms_core_file_write_test(); cosms_core_file_write_test();
cosms_core_file_delete_test();
return 0; return 0;
} }

View file

@ -145,4 +145,48 @@ void cosms_core_file_write_test() {
free(file_content); free(file_content);
} }
}
void cosms_core_file_delete_test() {
COSMS_CORE_TEST_HEADER("file delete");
const char *current_test_name = "deleting small file";
COSMS_CORE_TEST_START(current_test_name);
CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-write-file.txt");
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
if (file_content != NULL) {
free(file_content);
}
}
current_test_name = "deleting large file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_delete("tests/data/append-write-file.txt");
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content = NULL;
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
if (file_content != NULL) {
free(file_content);
}
}
current_test_name = "deleting non existing file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_delete("non-existing-file.cosms");
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
} }

View file

@ -5,5 +5,6 @@
void cosms_core_file_read_test(); void cosms_core_file_read_test();
void cosms_core_file_write_test(); void cosms_core_file_write_test();
void cosms_core_file_delete_test();
#endif #endif