Fix FAT driver file modes, update filewriter accordingly
Some checks failed
Build documentation / build-and-deploy (push) Has been cancelled

This commit is contained in:
2026-03-15 20:18:50 +01:00
parent d7bfc5c8fd
commit af966b5405
5 changed files with 30 additions and 20 deletions

View File

@@ -426,12 +426,19 @@ static void execute_redir (struct ast_redir* redir, struct context* context) {
return; return;
} }
uint32_t fw_flags = FW_CREATE_FILE | FW_TRUNCATE;
struct filewriter fw; struct filewriter fw;
if ((ret = filewriter_init (&fw, volume, path, FW_CREATE_FILE | FW_APPEND)) < 0) { if ((ret = filewriter_init (&fw, volume, path, fw_flags)) < 0) {
cprintf (context, "ERROR could not initialize filewriter for '%s:%s'\n", volume, path); cprintf (context, "ERROR could not initialize filewriter for '%s:%s'\n", volume, path);
return; return;
} }
if (context->strbuf.count == 0) {
filewriter_fini (&fw);
return;
}
size_t chunk_size = 1024; size_t chunk_size = 1024;
size_t chunks = (context->strbuf.count - 1) / chunk_size; size_t chunks = (context->strbuf.count - 1) / chunk_size;
size_t rem = (context->strbuf.count - 1) % chunk_size; size_t rem = (context->strbuf.count - 1) % chunk_size;

View File

@@ -1,6 +1,6 @@
#ifndef _WRITE_FILE_H #ifndef _WRITE_FILE_H
#define _WRITE_FILE_H #define _WRITE_FILE_H
#define WF_APPEND (1 << 0) #define WF_TRUNCATE (1 << 1)
#endif // _WRITE_FILE_H #endif // _WRITE_FILE_H

View File

@@ -227,10 +227,11 @@ DEFINE_VFS_WRITE_FILE (fatfs_write_file) {
FL_FILE* file; FL_FILE* file;
if ((flags & WF_APPEND)) if ((flags & WF_TRUNCATE) && off == 0) {
file = fl_fopen (fatfs_ctx, path, "wb+");
} else {
file = fl_fopen (fatfs_ctx, path, "rb+"); file = fl_fopen (fatfs_ctx, path, "rb+");
else }
file = fl_fopen (fatfs_ctx, path, "wb");
if (file == NULL) if (file == NULL)
return -ST_NOT_FOUND; return -ST_NOT_FOUND;
@@ -276,7 +277,7 @@ DEFINE_VFS_CREATE_FILE (fatfs_create_file) {
fatfs_ctx->proc = proc; fatfs_ctx->proc = proc;
fatfs_ctx->rctx = rctx; fatfs_ctx->rctx = rctx;
FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+"); FL_FILE* file = fl_fopen (fatfs_ctx, path, "ab+");
if (file == NULL) if (file == NULL)
return -ST_NOT_FOUND; return -ST_NOT_FOUND;

View File

@@ -38,8 +38,14 @@ int filewriter_init (struct filewriter* fw, const char* volume, const char* path
fw->file_size = desc.size; fw->file_size = desc.size;
fw->flags |= FW_OPEN; fw->flags |= FW_OPEN;
if ((fw->flags & FW_APPEND)) if ((fw->flags & FW_APPEND)) {
fw->write_cursor = desc.size; fw->write_cursor = desc.size;
} else if ((fw->flags & FW_TRUNCATE)) {
fw->write_cursor = 0;
fw->file_size = 0;
} else {
fw->write_cursor = 0;
}
return FW_OK; return FW_OK;
} }
@@ -57,26 +63,21 @@ int filewriter_write (struct filewriter* fw, uint8_t* buffer, size_t buffer_size
if (!(fw->flags & FW_OPEN)) if (!(fw->flags & FW_OPEN))
return -FW_VOLUME_NOT_OPENED; return -FW_VOLUME_NOT_OPENED;
struct desc desc;
int ret; int ret;
if ((fw->flags & FW_APPEND)) {
if ((ret = describe (fw->path, &desc)) < 0)
return -FW_DESC_ERROR;
fw->file_size = desc.size;
fw->write_cursor = fw->file_size;
}
if (buffer_size > 0) { if (buffer_size > 0) {
if (!(fw->flags & FW_APPEND) && !(fw->write_cursor + buffer_size <= fw->file_size)) uint32_t wf_flags = 0;
return -FW_CURSOR_OOB;
uint32_t flags = (fw->flags & FW_APPEND) ? WF_APPEND : 0; if ((fw->flags & FW_TRUNCATE) && fw->write_cursor == 0) {
if ((ret = write_file (fw->path, fw->write_cursor, buffer, buffer_size, flags)) < 0) wf_flags |= WF_TRUNCATE;
}
if ((ret = write_file (fw->path, fw->write_cursor, buffer, buffer_size, wf_flags)) < 0)
return -FW_WRITE_ERROR; return -FW_WRITE_ERROR;
fw->write_cursor += buffer_size; fw->write_cursor += buffer_size;
if (fw->write_cursor > fw->file_size)
fw->file_size = fw->write_cursor;
} }
return FW_OK; return FW_OK;

View File

@@ -17,6 +17,7 @@
#define FW_CREATE_FILE (1 << 0) #define FW_CREATE_FILE (1 << 0)
#define FW_APPEND (1 << 1) #define FW_APPEND (1 << 1)
#define FW_TRUNCATE (1 << 2)
#define FW_OPEN (1 << 31) #define FW_OPEN (1 << 31)
struct filewriter { struct filewriter {