Compare commits

...

2 commits

Author SHA1 Message Date
Mineplay5780
02090f42da fix(file): made some small fixes for tests and file functionality on linux 2025-12-20 21:39:48 +01:00
Mineplay5780
562eab114b docs(file): added commend to all file functions 2025-12-20 20:53:39 +01:00
5 changed files with 121 additions and 16 deletions

View file

@ -45,19 +45,86 @@ struct cosms_core_file {
#endif #endif
}; };
/**
* @brief Opens file in specified mode.
*
* @param file Pointer to file struct (must not be NULL)
* @param path to the file to open
* @param mode to open the file in (read, write, apped, create or a combination of these)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode); CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
/**
* @brief Closes an open file.
*
* @param file Pointer to file struct that has been opened (must not be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file); CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
/**
* @brief Gets the size of an opened file.
*
* @param file Pointer to file struct that has been opned (must not be NULL)
* @param size Pointer to the variable to store the size in (must not be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size); CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
/**
* @brief Try's to reads specified number of bytes.
*
* @param file Pointer to file struct that has been opened in read mode (must not be NULL)
* @param buffer to store the file content in (has to be preallocated stack or heap memory)
* @param bytes_to_read from the file.
* @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read); CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
/**
* @brief Reads the whole file with support for reading large files.
*
* @param file Pointer to file struct that has been opened in read mode (must not be NULL)
* @param buffer Pointer to buffer to store the file content in (needs to be empty)
* @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read); CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);
/**
* @brief Try's to write specified number of bytes.
*
* @param file Pointer to file struct that has been opened in write mode (must not be NULL)
* @param buffer that contains the bytes to write (must not be NULL)
* @param bytes_to_write to the file
* @param bytes_written Pointer to the variable to store the amount of bytes that where actually written (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written); CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
/**
* @brief Writes the full buffer to the file with support for writting large files.
*
* @param file Pointer to file struct that has been opened in write mode (must not be NULL)
* @param buffer that contains the bytes to write (must not be NULL)
* @param bytes_to_write to the file
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write); CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write);
/**
* @brief Deletes specified file from system.
*
* @param path to the file to delete
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_delete(const char *path); CosmsCoreFileError cosms_core_file_delete(const char *path);
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*
* @param error The error code to convert to a string message.
* @return The error as a string on success, or NULL
*/
const char *cosms_core_file_error_string(CosmsCoreFileError error); const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif #endif /* COSMS_CORE_FILE */

View file

@ -21,6 +21,9 @@
#include <errno.h> #include <errno.h>
#endif #endif
/**
* @brief Opens file in specified mode.
*/
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) { CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
file->mode = mode; file->mode = mode;
@ -36,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:
@ -111,6 +117,9 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Closes an open file.
*/
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) { CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
#if defined(__GNUC__) #if defined(__GNUC__)
int error = close(file->native_file); int error = close(file->native_file);
@ -135,6 +144,9 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Gets the size of an opened file.
*/
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) { CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
#if defined(__GNUC__) #if defined(__GNUC__)
struct stat file_stat; struct stat file_stat;
@ -164,6 +176,9 @@ CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsign
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Try's to reads specified number of bytes.
*/
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) { CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -188,6 +203,9 @@ CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buff
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Reads the whole file with support for reading large files.
*/
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) { CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -217,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;
@ -267,6 +285,9 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char *
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Try's to write specified number of bytes.
*/
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) { CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -294,6 +315,9 @@ CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buf
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Writes the full buffer to the file with support for writting large files.
*/
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) { CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -309,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;
} }
@ -344,6 +368,9 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Deletes specified file from system.
*/
CosmsCoreFileError cosms_core_file_delete(const char *path) { CosmsCoreFileError cosms_core_file_delete(const char *path) {
#if defined(__GNUC__) #if defined(__GNUC__)
if (unlink(path) == -1) { if (unlink(path) == -1) {
@ -384,6 +411,9 @@ CosmsCoreFileError cosms_core_file_delete(const char *path) {
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*/
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:

Binary file not shown.

View file

@ -9,15 +9,24 @@
implementation \ implementation \
} }
/**
* @brief Creates a list to export so it can be imported into the main test list.
*/
#define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \ #define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \
__VA_ARGS__, \ __VA_ARGS__, \
0 \ 0 \
} }
/**
* @brief Creates a list of lists from exported tests so all tests can be easly executed.
*/
#define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = { #define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = {
#define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name #define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name
#define COSMS_CORE_TEST_END }; #define COSMS_CORE_TEST_END };
/**
* @brief Runs all tests specified in the cosms_core_test_tests array;
*/
#define COSMS_CORE_TEST_RUN() \ #define COSMS_CORE_TEST_RUN() \
for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \ for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \
unsigned long long test_index = 0; \ unsigned long long test_index = 0; \
@ -42,4 +51,4 @@ struct cosms_core_test {
const char *(*function)(void); const char *(*function)(void);
}; };
#endif #endif /* COSMS_CORE_TEST */

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);
} }