Compare commits
2 commits
e226da4fa0
...
02090f42da
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02090f42da | ||
|
|
562eab114b |
5 changed files with 121 additions and 16 deletions
|
|
@ -45,19 +45,86 @@ struct cosms_core_file {
|
|||
#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);
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
#endif
|
||||
#endif /* COSMS_CORE_FILE */
|
||||
|
|
|
|||
48
src/file.c
48
src/file.c
|
|
@ -21,6 +21,9 @@
|
|||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Opens file in specified mode.
|
||||
*/
|
||||
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int 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) {
|
||||
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:
|
||||
|
|
@ -111,6 +117,9 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char
|
|||
return COSMS_CORE_FILE_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Closes an open file.
|
||||
*/
|
||||
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
|
||||
#if defined(__GNUC__)
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the size of an opened file.
|
||||
*/
|
||||
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
|
||||
#if defined(__GNUC__)
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
@ -267,6 +285,9 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char *
|
|||
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) {
|
||||
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -344,6 +368,9 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
|
|||
return COSMS_CORE_FILE_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deletes specified file from system.
|
||||
*/
|
||||
CosmsCoreFileError cosms_core_file_delete(const char *path) {
|
||||
#if defined(__GNUC__)
|
||||
if (unlink(path) == -1) {
|
||||
|
|
@ -384,6 +411,9 @@ CosmsCoreFileError cosms_core_file_delete(const char *path) {
|
|||
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) {
|
||||
switch(error) {
|
||||
case COSMS_CORE_FILE_OK:
|
||||
|
|
|
|||
Binary file not shown.
11
tests/test.h
11
tests/test.h
|
|
@ -9,15 +9,24 @@
|
|||
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[] = { \
|
||||
__VA_ARGS__, \
|
||||
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_IMPORT(name) cosms_core_test_##name
|
||||
#define COSMS_CORE_TEST_END };
|
||||
|
||||
/**
|
||||
* @brief Runs all tests specified in the cosms_core_test_tests array;
|
||||
*/
|
||||
#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) { \
|
||||
unsigned long long test_index = 0; \
|
||||
|
|
@ -42,4 +51,4 @@ struct cosms_core_test {
|
|||
const char *(*function)(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif /* COSMS_CORE_TEST */
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue