chore: run clang-format

This commit is contained in:
MechSlayer 2025-09-09 04:01:11 +02:00
parent 053f4f9043
commit 2194e4c805
8 changed files with 75 additions and 73 deletions

View file

@ -173,6 +173,4 @@ DRANG_PLATFORM_API const char *drang_error_str(int err);
*/
#define DRANG_FAIL_IF_NULL_OOM(ptr) DRANG_FAIL_IF_NULL(ptr, DRANG_ENOMEM)
DRANG_END_DECLS

View file

@ -1,8 +1,8 @@
#pragma once
#include "platform.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
DRANG_BEGIN_DECLS

View file

@ -36,15 +36,14 @@
# define DRANG_ALLOC_ALIGN_ATTR(idx) __attribute__((alloc_align(idx)))
# define DRANG_MALLOC_ATTR __attribute__((malloc))
# define DRANG_BIT_CAST(_Expr_, _From_, _To_) __builtin_bit_cast(_To_, _Expr_)
#define DRANG_BIT_CAST(_Expr_, _From_, _To_) __builtin_bit_cast(_To_, _Expr_)
#define DRANG_MUL_OVERFLOW(_A_, _B_, _Result_) (__builtin_mul_overflow(_A_, _B_, _Result_) != 0)
#define DRANG_ADD_OVERFLOW(_A_, _B_, _Result_) (__builtin_add_overflow(_A_, _B_, _Result_) != 0)
#define DRANG_SUB_OVERFLOW(_A_, _B_, _Result_) (__builtin_sub_overflow(_A_, _B_, _Result_) != 0)
# define DRANG_MUL_OVERFLOW(_A_, _B_, _Result_) (__builtin_mul_overflow(_A_, _B_, _Result_) != 0)
# define DRANG_ADD_OVERFLOW(_A_, _B_, _Result_) (__builtin_add_overflow(_A_, _B_, _Result_) != 0)
# define DRANG_SUB_OVERFLOW(_A_, _B_, _Result_) (__builtin_sub_overflow(_A_, _B_, _Result_) != 0)
#else
#error "Unsupported compiler, please implement attribute macros"
# error "Unsupported compiler, please implement attribute macros"
#endif
#define DRANG_UNUSED(x) ((void)(x))
#define DRANG_UNUSED(x) ((void) (x))

View file

@ -40,27 +40,35 @@ static int mode_to_open_flags(const drang_fs_mode_t mode)
static int drang_permissions_to_linux(const drang_fs_permissions_t permissions)
{
int result = 0;
if (permissions & DRANG_FS_PERM_USER_READ) result |= S_IRUSR;
if (permissions & DRANG_FS_PERM_USER_WRITE) result |= S_IWUSR;
if (permissions & DRANG_FS_PERM_USER_EXEC) result |= S_IXUSR;
if (permissions & DRANG_FS_PERM_GROUP_READ) result |= S_IRGRP;
if (permissions & DRANG_FS_PERM_GROUP_WRITE) result |= S_IWGRP;
if (permissions & DRANG_FS_PERM_GROUP_EXEC) result |= S_IXGRP;
if (permissions & DRANG_FS_PERM_OTHER_READ) result |= S_IROTH;
if (permissions & DRANG_FS_PERM_OTHER_WRITE) result |= S_IWOTH;
if (permissions & DRANG_FS_PERM_OTHER_EXEC) result |= S_IXOTH;
if (permissions & DRANG_FS_PERM_USER_READ)
result |= S_IRUSR;
if (permissions & DRANG_FS_PERM_USER_WRITE)
result |= S_IWUSR;
if (permissions & DRANG_FS_PERM_USER_EXEC)
result |= S_IXUSR;
if (permissions & DRANG_FS_PERM_GROUP_READ)
result |= S_IRGRP;
if (permissions & DRANG_FS_PERM_GROUP_WRITE)
result |= S_IWGRP;
if (permissions & DRANG_FS_PERM_GROUP_EXEC)
result |= S_IXGRP;
if (permissions & DRANG_FS_PERM_OTHER_READ)
result |= S_IROTH;
if (permissions & DRANG_FS_PERM_OTHER_WRITE)
result |= S_IWOTH;
if (permissions & DRANG_FS_PERM_OTHER_EXEC)
result |= S_IXOTH;
return result;
}
int drang_fs_open(const char *path, drang_fs_mode_t mode, drang_fs_file_t **out_file)
{
struct drang_fs_file* file = NULL;
struct drang_fs_file *file = NULL;
DRANG_BEGIN_TRY()
DRANG_FAIL_IF_NULL(path, DRANG_EINVAL);
DRANG_FAIL_IF_NULL(out_file, DRANG_EINVAL);
const int oflags = mode_to_open_flags(mode);
DRANG_CHECK(oflags != 0, DRANG_EINVAL);
@ -77,7 +85,6 @@ int drang_fs_open(const char *path, drang_fs_mode_t mode, drang_fs_file_t **out_
file->mode = mode;
DRANG_RETURN_IN(out_file, file);
DRANG_CATCH(_)
{
if (file != NULL) {
@ -92,7 +99,8 @@ int drang_fs_open(const char *path, drang_fs_mode_t mode, drang_fs_file_t **out_
int drang_fs_close(drang_fs_file_t *file)
{
bool locked = false;
if (file == NULL) return DRANG_EOK;
if (file == NULL)
return DRANG_EOK;
DRANG_BEGIN_TRY()
DRANG_TRY(drang_fs_flush(file));
@ -137,7 +145,6 @@ int drang_fs_read(drang_fs_file_t *file, void *buffer, size_t count, size_t size
size_t total_bytes = 0;
DRANG_CHECK(!DRANG_MUL_OVERFLOW(count, size, &total_bytes), DRANG_EOVERFLOW);
DRANG_TRY(drang_rwlock_rdlock(&file->lock));
locked = true;
@ -147,7 +154,7 @@ int drang_fs_read(drang_fs_file_t *file, void *buffer, size_t count, size_t size
}
if (bytes_read != NULL) {
*bytes_read = (size_t)res;
*bytes_read = (size_t) res;
}
drang_rwlock_rdunlock(&file->lock);
@ -161,8 +168,7 @@ int drang_fs_read(drang_fs_file_t *file, void *buffer, size_t count, size_t size
DRANG_END_TRY()
}
int drang_fs_write(
drang_fs_file_t *file, const void *buffer, size_t count, size_t size, size_t *bytes_written)
int drang_fs_write(drang_fs_file_t *file, const void *buffer, size_t count, size_t size, size_t *bytes_written)
{
bool locked = false;
DRANG_BEGIN_TRY()
@ -173,7 +179,6 @@ int drang_fs_write(
DRANG_CHECK(file->mode & (drang_fs_mode_write | drang_fs_mode_append), DRANG_EPERM);
DRANG_CHECK(count > 0 && size > 0, DRANG_EINVAL);
size_t total_bytes = 0;
DRANG_CHECK(!DRANG_MUL_OVERFLOW(count, size, &total_bytes), DRANG_EOVERFLOW);
DRANG_TRY(drang_rwlock_wrlock(&file->lock));
@ -184,7 +189,7 @@ int drang_fs_write(
DRANG_FAIL(drang_errno_to_error(errno));
}
if (bytes_written != NULL) {
*bytes_written = (size_t)res;
*bytes_written = (size_t) res;
}
drang_rwlock_wrunlock(&file->lock);
@ -221,12 +226,11 @@ int drang_fs_seek(drang_fs_file_t *file, int64_t offset, drang_seek_origin_t ori
DRANG_TRY(drang_rwlock_wrlock(&file->lock));
locked = true;
const off_t res = lseek(file->fd, (off_t)offset, whence);
if (res == (off_t)-1) {
const off_t res = lseek(file->fd, (off_t) offset, whence);
if (res == (off_t) -1) {
DRANG_FAIL(drang_errno_to_error(errno));
}
drang_rwlock_wrunlock(&file->lock);
DRANG_CATCH(_)
@ -249,10 +253,10 @@ int drang_fs_tell(drang_fs_file_t *file, uint64_t *position)
DRANG_TRY(drang_rwlock_rdlock(&file->lock));
locked = true;
const off_t res = lseek(file->fd, 0, SEEK_CUR);
if (res == (off_t)-1) {
if (res == (off_t) -1) {
DRANG_FAIL(drang_errno_to_error(errno));
}
*position = (uint64_t)res;
*position = (uint64_t) res;
drang_rwlock_rdunlock(&file->lock);
@ -302,7 +306,7 @@ int drang_fs_truncate(drang_fs_file_t *file, uint64_t size)
DRANG_TRY(drang_rwlock_wrlock(&file->lock));
locked = true;
const int res = ftruncate(file->fd, (off_t)size);
const int res = ftruncate(file->fd, (off_t) size);
if (res == -1) {
DRANG_FAIL(drang_errno_to_error(errno));
}
@ -358,7 +362,6 @@ int drang_fs_move(const char *old_path, const char *new_path)
DRANG_RETURN();
}
if (errno == EXDEV) { // Cross-device link, need to copy+delete
struct stat src_stat = {0};
@ -376,7 +379,7 @@ int drang_fs_move(const char *old_path, const char *new_path)
char buffer[DRANG_LINUX_FS_BUFFER_SIZE];
ssize_t bytes_read = 0;
while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) {
const ssize_t write_res = write(dst_fd, buffer, (size_t)bytes_read);
const ssize_t write_res = write(dst_fd, buffer, (size_t) bytes_read);
if (write_res == -1 || write_res != bytes_read) {
DRANG_FAIL(drang_errno_to_error(errno));
}
@ -450,22 +453,30 @@ int drang_fs_stat(const char *path, struct drang_fs_stat *out_stat)
out_stat->type = drang_fs_type_unknown;
}
out_stat->size = (uint64_t)st.st_size;
out_stat->size = (uint64_t) st.st_size;
out_stat->permissions = 0;
if (st.st_mode & S_IRUSR) out_stat->permissions |= DRANG_FS_PERM_USER_READ;
if (st.st_mode & S_IWUSR) out_stat->permissions |= DRANG_FS_PERM_USER_WRITE;
if (st.st_mode & S_IXUSR) out_stat->permissions |= DRANG_FS_PERM_USER_EXEC;
if (st.st_mode & S_IRGRP) out_stat->permissions |= DRANG_FS_PERM_GROUP_READ;
if (st.st_mode & S_IWGRP) out_stat->permissions |= DRANG_FS_PERM_GROUP_WRITE;
if (st.st_mode & S_IXGRP) out_stat->permissions |= DRANG_FS_PERM_GROUP_EXEC;
if (st.st_mode & S_IROTH) out_stat->permissions |= DRANG_FS_PERM_OTHER_READ;
if (st.st_mode & S_IWOTH) out_stat->permissions |= DRANG_FS_PERM_OTHER_WRITE;
if (st.st_mode & S_IXOTH) out_stat->permissions |= DRANG_FS_PERM_OTHER_EXEC;
if (st.st_mode & S_IRUSR)
out_stat->permissions |= DRANG_FS_PERM_USER_READ;
if (st.st_mode & S_IWUSR)
out_stat->permissions |= DRANG_FS_PERM_USER_WRITE;
if (st.st_mode & S_IXUSR)
out_stat->permissions |= DRANG_FS_PERM_USER_EXEC;
if (st.st_mode & S_IRGRP)
out_stat->permissions |= DRANG_FS_PERM_GROUP_READ;
if (st.st_mode & S_IWGRP)
out_stat->permissions |= DRANG_FS_PERM_GROUP_WRITE;
if (st.st_mode & S_IXGRP)
out_stat->permissions |= DRANG_FS_PERM_GROUP_EXEC;
if (st.st_mode & S_IROTH)
out_stat->permissions |= DRANG_FS_PERM_OTHER_READ;
if (st.st_mode & S_IWOTH)
out_stat->permissions |= DRANG_FS_PERM_OTHER_WRITE;
if (st.st_mode & S_IXOTH)
out_stat->permissions |= DRANG_FS_PERM_OTHER_EXEC;
out_stat->created_time = (drang_fs_time_t)st.st_ctim.tv_sec * 1000000000 + st.st_ctim.tv_nsec;
out_stat->modified_time = (drang_fs_time_t)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;
out_stat->accessed_time = (drang_fs_time_t)st.st_atim.tv_sec * 1000000000 + st.st_atim.tv_nsec;
out_stat->created_time = (drang_fs_time_t) st.st_ctim.tv_sec * 1000000000 + st.st_ctim.tv_nsec;
out_stat->modified_time = (drang_fs_time_t) st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;
out_stat->accessed_time = (drang_fs_time_t) st.st_atim.tv_sec * 1000000000 + st.st_atim.tv_nsec;
DRANG_END_TRY_IGNORE()
}
@ -479,6 +490,5 @@ int drang_fs_set_permissions(const char *path, drang_fs_permissions_t permission
DRANG_FAIL(drang_errno_to_error(errno));
}
DRANG_END_TRY_IGNORE()
}
}

View file

@ -1,7 +1,7 @@
#pragma once
#include "linux/sync/internal.h"
#include <drang/fs.h>
#include <unistd.h>
#include "linux/sync/internal.h"
#define DRANG_LINUX_FS_BUFFER_SIZE (4096)

View file

@ -3,7 +3,6 @@
#include <Windows.h>
#include <drang/platform.h>
DRANG_BEGIN_DECLS
DRANG_PLATFORM_API int win32_error_to_drang(DWORD error);

View file

@ -377,10 +377,10 @@ int drang_fs_stat(const char *path, struct drang_fs_stat *out_stat)
} else if (file_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
// This might be a symlink, but we need to check further
out_stat->type = drang_fs_type_symlink;
out_stat->size = ((uint64_t)file_data.nFileSizeHigh << 32) | file_data.nFileSizeLow;
out_stat->size = ((uint64_t) file_data.nFileSizeHigh << 32) | file_data.nFileSizeLow;
} else {
out_stat->type = drang_fs_type_file;
out_stat->size = ((uint64_t)file_data.nFileSizeHigh << 32) | file_data.nFileSizeLow;
out_stat->size = ((uint64_t) file_data.nFileSizeHigh << 32) | file_data.nFileSizeLow;
}
// Convert Windows permissions to Unix-style permissions
@ -392,11 +392,10 @@ int drang_fs_stat(const char *path, struct drang_fs_stat *out_stat)
}
// For directories and executables, add execute permissions
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
(strlen(path) > 4 &&
(_stricmp(path + strlen(path) - 4, ".exe") == 0 ||
_stricmp(path + strlen(path) - 4, ".bat") == 0 ||
_stricmp(path + strlen(path) - 4, ".cmd") == 0))) {
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|| (strlen(path) > 4
&& (_stricmp(path + strlen(path) - 4, ".exe") == 0 || _stricmp(path + strlen(path) - 4, ".bat") == 0
|| _stricmp(path + strlen(path) - 4, ".cmd") == 0))) {
out_stat->permissions |= DRANG_FS_PERM_USER_EXEC | DRANG_FS_PERM_GROUP_EXEC | DRANG_FS_PERM_OTHER_EXEC;
}
@ -406,18 +405,18 @@ int drang_fs_stat(const char *path, struct drang_fs_stat *out_stat)
const uint64_t WINDOWS_EPOCH_DIFF = 11644473600ULL; // Seconds between 1601 and 1970
// Convert creation time
uint64_t created_100ns = ((uint64_t)file_data.ftCreationTime.dwHighDateTime << 32) |
file_data.ftCreationTime.dwLowDateTime;
uint64_t created_100ns = ((uint64_t) file_data.ftCreationTime.dwHighDateTime << 32)
| file_data.ftCreationTime.dwLowDateTime;
out_stat->created_time = (created_100ns / 10000000ULL) - WINDOWS_EPOCH_DIFF;
// Convert modification time
uint64_t modified_100ns = ((uint64_t)file_data.ftLastWriteTime.dwHighDateTime << 32) |
file_data.ftLastWriteTime.dwLowDateTime;
uint64_t modified_100ns = ((uint64_t) file_data.ftLastWriteTime.dwHighDateTime << 32)
| file_data.ftLastWriteTime.dwLowDateTime;
out_stat->modified_time = (modified_100ns / 10000000ULL) - WINDOWS_EPOCH_DIFF;
// Convert access time
uint64_t accessed_100ns = ((uint64_t)file_data.ftLastAccessTime.dwHighDateTime << 32) |
file_data.ftLastAccessTime.dwLowDateTime;
uint64_t accessed_100ns = ((uint64_t) file_data.ftLastAccessTime.dwHighDateTime << 32)
| file_data.ftLastAccessTime.dwLowDateTime;
out_stat->accessed_time = (accessed_100ns / 10000000ULL) - WINDOWS_EPOCH_DIFF;
DRANG_END_TRY_IGNORE()
@ -435,4 +434,4 @@ int drang_fs_set_permissions(const char *path, drang_fs_permissions_t permission
DRANG_UNUSED(permissions);
DRANG_END_TRY_IGNORE()
}
}

View file

@ -5,12 +5,9 @@
#include "../sync/internal.h"
struct drang_fs_file
{
HANDLE handle;
drang_fs_mode_t mode;
struct drang_rwlock lock;
};
};