2025-12-05 16:57:14 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
|
|
|
|
*
|
|
|
|
|
* This software is licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License in the file LICENSE or at
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*/
|
|
|
|
|
#include "cosms-core/file.h"
|
2025-11-28 23:59:06 +01:00
|
|
|
|
2025-12-05 18:53:35 +01:00
|
|
|
#include <stdlib.h>
|
2025-12-08 23:04:51 +01:00
|
|
|
#include <stdint.h>
|
2025-12-13 18:36:48 +01:00
|
|
|
#include <limits.h>
|
2025-11-28 23:59:06 +01:00
|
|
|
|
2025-12-05 18:53:35 +01:00
|
|
|
#if defined(__GNUC__)
|
2025-12-13 18:36:48 +01:00
|
|
|
#define _FILE_OFFSET_BITS_ 64
|
|
|
|
|
|
2025-12-05 18:53:35 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Opens file in specified mode.
|
|
|
|
|
*/
|
2025-12-17 20:31:33 +01:00
|
|
|
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
|
|
|
|
|
file->mode = mode;
|
|
|
|
|
|
2025-12-05 18:53:35 +01:00
|
|
|
#if defined(__GNUC__)
|
2025-12-17 20:31:33 +01:00
|
|
|
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) {
|
2025-12-18 22:36:14 +01:00
|
|
|
flags |= O_WRONLY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
|
|
|
|
|
flags |= O_APPEND;
|
|
|
|
|
} else {
|
|
|
|
|
flags |= O_TRUNC;
|
|
|
|
|
if (mode & COSMS_CORE_FILE_MODE_CREATE) {
|
|
|
|
|
flags |= O_CREAT;
|
|
|
|
|
}
|
2025-12-17 20:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2025-12-05 18:53:35 +01:00
|
|
|
switch(errno) {
|
|
|
|
|
case ENOENT:
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_NOT_FOUND;
|
2025-12-05 18:53:35 +01:00
|
|
|
|
|
|
|
|
case EACCES:
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_NO_ACCESS;
|
2025-12-05 18:53:35 +01:00
|
|
|
|
|
|
|
|
case EMFILE:
|
|
|
|
|
case ENFILE:
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_LIMIT_REACHED;
|
2025-12-05 18:53:35 +01:00
|
|
|
|
|
|
|
|
default:
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_UNKOWN_ERROR;
|
2025-12-05 18:53:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-17 20:31:33 +01:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
DWORD access_flags = 0;
|
2025-12-17 21:16:00 +01:00
|
|
|
DWORD create_flag = OPEN_EXISTING;
|
2025-12-17 20:31:33 +01:00
|
|
|
|
|
|
|
|
if (mode & COSMS_CORE_FILE_MODE_READ) {
|
|
|
|
|
access_flags |= GENERIC_READ;
|
|
|
|
|
}
|
2025-12-05 18:53:35 +01:00
|
|
|
|
2025-12-17 20:31:33 +01:00
|
|
|
if (mode & COSMS_CORE_FILE_MODE_WRITE) {
|
|
|
|
|
access_flags |= GENERIC_WRITE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode & COSMS_CORE_FILE_MODE_APPEND) {
|
|
|
|
|
create_flag = OPEN_ALWAYS;
|
2025-12-18 22:36:14 +01:00
|
|
|
} else if (mode & COSMS_CORE_FILE_MODE_CREATE) {
|
2025-12-17 20:31:33 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Closes an open file.
|
|
|
|
|
*/
|
2025-12-17 20:31:33 +01:00
|
|
|
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)
|
2025-12-17 21:16:00 +01:00
|
|
|
BOOL error = CloseHandle(file->native_file);
|
2025-12-17 20:31:33 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Gets the size of an opened file.
|
|
|
|
|
*/
|
2025-12-17 21:16:00 +01:00
|
|
|
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
|
2025-12-17 20:40:01 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
struct stat file_stat;
|
2025-12-17 21:16:00 +01:00
|
|
|
if (fstat(file->native_file, &file_stat) != 0) {
|
|
|
|
|
if (errno == EBADF) {
|
|
|
|
|
return COSMS_CORE_FILE_INVALID_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 20:40:01 +01:00
|
|
|
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*size) = file_stat.st_size;
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
DWORD high_size;
|
2025-12-17 21:16:00 +01:00
|
|
|
DWORD low_size = GetFileSize(file->native_file, &high_size);
|
|
|
|
|
if (low_size == INVALID_FILE_SIZE) {
|
|
|
|
|
if (GetLastError() == ERROR_INVALID_HANDLE) {
|
|
|
|
|
return COSMS_CORE_FILE_INVALID_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 20:40:01 +01:00
|
|
|
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*size) = ((unsigned long long)high_size << 32) | low_size;
|
|
|
|
|
#endif
|
2025-12-17 21:16:00 +01:00
|
|
|
|
2025-12-17 20:40:01 +01:00
|
|
|
return COSMS_CORE_FILE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Try's to reads specified number of bytes.
|
|
|
|
|
*/
|
2025-12-18 17:03:38 +01:00
|
|
|
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
|
2025-12-17 23:45:55 +01:00
|
|
|
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
|
|
|
|
|
return COSMS_CORE_FILE_INVALID_OPERATION;
|
2025-12-05 18:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
#if defined(__GNUC__)
|
2025-12-18 17:03:38 +01:00
|
|
|
int read_bytes = read(file->native_file, buffer, bytes_to_read);
|
2025-12-17 23:45:55 +01:00
|
|
|
if (read_bytes == -1) {
|
|
|
|
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
2025-12-17 20:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
if (bytes_read != NULL) {
|
|
|
|
|
(*bytes_read) = read_bytes;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(_WIN32)
|
2025-12-18 17:03:38 +01:00
|
|
|
if (ReadFile(file->native_file, buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) {
|
2025-12-17 23:45:55 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2025-12-17 20:31:33 +01:00
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
buffer[bytes_to_read] = '\0';
|
|
|
|
|
return COSMS_CORE_FILE_OK;
|
|
|
|
|
}
|
2025-12-17 20:31:33 +01:00
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Reads the whole file with support for reading large files.
|
|
|
|
|
*/
|
2025-12-17 23:45:55 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2025-12-17 20:31:33 +01:00
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
struct stat file_stat;
|
|
|
|
|
if (fstat(file->native_file, &file_stat) != 0) {
|
|
|
|
|
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
2025-12-17 20:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
if (bytes_read != NULL) {
|
|
|
|
|
(*bytes_read) = file_stat.st_size;
|
2025-12-10 20:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
(*buffer) = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
|
|
|
|
|
if ((*buffer) == NULL) {
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
|
2025-12-05 18:53:35 +01:00
|
|
|
}
|
2025-12-17 23:45:55 +01:00
|
|
|
|
2025-12-08 23:18:35 +01:00
|
|
|
unsigned long long remaining_bytes = file_stat.st_size;
|
2025-12-05 18:53:35 +01:00
|
|
|
while (remaining_bytes != 0) {
|
2025-12-17 23:45:55 +01:00
|
|
|
unsigned int current_bytes_to_read;
|
2025-12-08 23:18:35 +01:00
|
|
|
if (remaining_bytes > UINT_MAX) {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_read = UINT_MAX;
|
2025-12-08 23:18:35 +01:00
|
|
|
} else {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_read = (unsigned int)remaining_bytes;
|
2025-12-08 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
int read_bytes = read(file, (*buffer) + (file_stat.st_size - remaining_bytes), current_bytes_to_read);
|
2025-12-05 18:53:35 +01:00
|
|
|
if (read_bytes == -1) {
|
2025-12-17 23:45:55 +01:00
|
|
|
free(*buffer);
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
2025-12-05 18:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remaining_bytes -= read_bytes;
|
|
|
|
|
}
|
2025-12-08 23:04:51 +01:00
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
(*buffer)[file_stat.st_size] = '\0';
|
|
|
|
|
#elif defined(_WIN32)
|
2025-12-08 23:04:51 +01:00
|
|
|
DWORD high_size;
|
2025-12-17 23:45:55 +01:00
|
|
|
DWORD low_size = GetFileSize(file->native_file, &high_size);
|
2025-12-08 23:04:51 +01:00
|
|
|
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
|
2025-12-17 23:45:55 +01:00
|
|
|
if (bytes_read != NULL) {
|
|
|
|
|
(*bytes_read) = file_size;
|
2025-12-10 20:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
(*buffer) = (char*)malloc((file_size + 1) * sizeof(char));
|
|
|
|
|
if ((*buffer) == NULL) {
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long long remaining_bytes = file_size;
|
|
|
|
|
while (remaining_bytes != 0) {
|
2025-12-17 23:45:55 +01:00
|
|
|
DWORD current_bytes_to_read, read_bytes;
|
2025-12-08 23:04:51 +01:00
|
|
|
if (remaining_bytes > UINT_MAX) {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_read = UINT_MAX;
|
2025-12-08 23:04:51 +01:00
|
|
|
} else {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_read = (DWORD)remaining_bytes;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
if (ReadFile(file->native_file, (*buffer) + (file_size - remaining_bytes), current_bytes_to_read, &read_bytes, NULL) == 0) {
|
|
|
|
|
free(*buffer);
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_READ;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remaining_bytes -= read_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
(*buffer)[file_size] = '\0';
|
2025-12-05 18:53:35 +01:00
|
|
|
#endif
|
2025-12-17 23:45:55 +01:00
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_OK;
|
2025-11-28 23:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Try's to write specified number of bytes.
|
|
|
|
|
*/
|
2025-12-17 23:45:55 +01:00
|
|
|
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;
|
2025-12-17 20:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
int written_bytes = write(file->native_file, buffer, bytes_to_write);
|
|
|
|
|
if (written_bytes == -1) {
|
|
|
|
|
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
2025-12-05 20:05:14 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
if (bytes_written != NULL) {
|
|
|
|
|
(*bytes_written) = written_bytes;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(_WIN32)
|
2025-12-18 22:36:14 +01:00
|
|
|
if (file->mode & COSMS_CORE_FILE_MODE_APPEND) {
|
|
|
|
|
SetFilePointer(file->native_file, 0, NULL, FILE_END);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
if (WriteFile(file->native_file, buffer, bytes_to_write, (LPDWORD)bytes_written, NULL) == 0) {
|
|
|
|
|
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return COSMS_CORE_FILE_OK;
|
|
|
|
|
}
|
2025-12-05 20:05:14 +01:00
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Writes the full buffer to the file with support for writting large files.
|
|
|
|
|
*/
|
2025-12-17 23:45:55 +01:00
|
|
|
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;
|
2025-12-05 20:05:14 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
unsigned long long remaining_bytes = bytes_to_write;
|
2025-12-05 20:05:14 +01:00
|
|
|
while (remaining_bytes != 0) {
|
2025-12-17 23:45:55 +01:00
|
|
|
unsigned int current_bytes_to_write;
|
2025-12-08 23:18:35 +01:00
|
|
|
if (remaining_bytes > UINT_MAX) {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_write = UINT_MAX;
|
2025-12-08 23:18:35 +01:00
|
|
|
} else {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_write = (unsigned int)remaining_bytes;
|
2025-12-08 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-18 22:36:14 +01:00
|
|
|
int written_bytes = write(file->native_file, content + (bytes_to_write - remaining_bytes), current_bytes_to_write);
|
2025-12-05 20:05:14 +01:00
|
|
|
if (written_bytes == -1) {
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
2025-12-05 20:05:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remaining_bytes -= written_bytes;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(_WIN32)
|
2025-12-18 22:36:14 +01:00
|
|
|
if (file->mode & COSMS_CORE_FILE_MODE_APPEND) {
|
|
|
|
|
LARGE_INTEGER zero;
|
|
|
|
|
zero.QuadPart = 0;
|
|
|
|
|
|
|
|
|
|
SetFilePointerEx(file->native_file, zero, NULL, FILE_END);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
unsigned long long remaining_bytes = bytes_to_write;
|
2025-12-08 23:04:51 +01:00
|
|
|
while (remaining_bytes != 0) {
|
2025-12-17 23:45:55 +01:00
|
|
|
DWORD current_bytes_to_write, written_bytes;
|
2025-12-08 23:04:51 +01:00
|
|
|
if (remaining_bytes > UINT_MAX) {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_write = UINT_MAX;
|
2025-12-08 23:04:51 +01:00
|
|
|
} else {
|
2025-12-17 23:45:55 +01:00
|
|
|
current_bytes_to_write = (DWORD)remaining_bytes;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-18 22:36:14 +01:00
|
|
|
if (WriteFile(file->native_file, buffer + (bytes_to_write - remaining_bytes), current_bytes_to_write, &written_bytes, NULL) == 0) {
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_FAILED_TO_WRITE;
|
2025-12-08 23:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remaining_bytes -= written_bytes;
|
|
|
|
|
}
|
2025-12-05 20:05:14 +01:00
|
|
|
#endif
|
2025-12-17 23:45:55 +01:00
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
return COSMS_CORE_FILE_OK;
|
2025-11-28 23:59:06 +01:00
|
|
|
}
|
2025-12-08 23:32:43 +01:00
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Deletes specified file from system.
|
|
|
|
|
*/
|
2025-12-11 22:38:30 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 20:53:39 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Converts error code to a string that can be used for printing/logging errors.
|
|
|
|
|
*/
|
2025-12-10 20:43:16 +01:00
|
|
|
const char *cosms_core_file_error_string(CosmsCoreFileError error) {
|
2025-12-08 23:32:43 +01:00
|
|
|
switch(error) {
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_OK:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file everything ok";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_NOT_FOUND:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file path not found";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_NO_ACCESS:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file no access";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_LIMIT_REACHED:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file to many open files";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_COULD_NOT_READ_SIZE:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file failed to read size";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_UNKOWN_ERROR:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file unkown error occured";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_FAILED_TO_ALLOCATE:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file failed to allocate memory";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_FAILED_TO_READ:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file failed to read content of file";
|
|
|
|
|
|
2025-12-10 20:43:16 +01:00
|
|
|
case COSMS_CORE_FILE_FAILED_TO_WRITE:
|
2025-12-08 23:32:43 +01:00
|
|
|
return "cosms-file failed to write content to file";
|
|
|
|
|
|
2025-12-11 22:38:30 +01:00
|
|
|
case COSMS_CORE_FILE_STILL_OPEN:
|
|
|
|
|
return "cosms-file still open";
|
|
|
|
|
|
2025-12-17 21:16:00 +01:00
|
|
|
case COSMS_CORE_FILE_INVALID_FILE:
|
|
|
|
|
return "cosms-file invalid native file given";
|
|
|
|
|
|
2025-12-17 23:45:55 +01:00
|
|
|
case COSMS_CORE_FILE_INVALID_OPERATION:
|
|
|
|
|
return "cosms-file invalid operation for current file";
|
|
|
|
|
|
2025-12-08 23:32:43 +01:00
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-12-13 18:36:48 +01:00
|
|
|
}
|