diff --git a/ce/interp.c b/ce/interp.c index 1c67d2d..5312106 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -426,12 +426,19 @@ static void execute_redir (struct ast_redir* redir, struct context* context) { return; } + uint32_t fw_flags = FW_CREATE_FILE | FW_TRUNCATE; + 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); return; } + if (context->strbuf.count == 0) { + filewriter_fini (&fw); + return; + } + size_t chunk_size = 1024; size_t chunks = (context->strbuf.count - 1) / chunk_size; size_t rem = (context->strbuf.count - 1) % chunk_size; diff --git a/include/write_file.h b/include/write_file.h index 9c0b06b..e210d85 100644 --- a/include/write_file.h +++ b/include/write_file.h @@ -1,6 +1,6 @@ #ifndef _WRITE_FILE_H #define _WRITE_FILE_H -#define WF_APPEND (1 << 0) +#define WF_TRUNCATE (1 << 1) #endif // _WRITE_FILE_H diff --git a/kernel/fs/fatfs.c b/kernel/fs/fatfs.c index d40eb79..ea75732 100644 --- a/kernel/fs/fatfs.c +++ b/kernel/fs/fatfs.c @@ -227,10 +227,11 @@ DEFINE_VFS_WRITE_FILE (fatfs_write_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+"); - else - file = fl_fopen (fatfs_ctx, path, "wb"); + } if (file == NULL) return -ST_NOT_FOUND; @@ -276,7 +277,7 @@ DEFINE_VFS_CREATE_FILE (fatfs_create_file) { fatfs_ctx->proc = proc; 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) return -ST_NOT_FOUND; diff --git a/libioutil/filewriter.c b/libioutil/filewriter.c index b9ae0b7..fda0ef7 100644 --- a/libioutil/filewriter.c +++ b/libioutil/filewriter.c @@ -38,8 +38,14 @@ int filewriter_init (struct filewriter* fw, const char* volume, const char* path fw->file_size = desc.size; fw->flags |= FW_OPEN; - if ((fw->flags & FW_APPEND)) + if ((fw->flags & FW_APPEND)) { 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; } @@ -57,26 +63,21 @@ int filewriter_write (struct filewriter* fw, uint8_t* buffer, size_t buffer_size if (!(fw->flags & FW_OPEN)) return -FW_VOLUME_NOT_OPENED; - struct desc desc; 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 (!(fw->flags & FW_APPEND) && !(fw->write_cursor + buffer_size <= fw->file_size)) - return -FW_CURSOR_OOB; + uint32_t wf_flags = 0; - uint32_t flags = (fw->flags & FW_APPEND) ? WF_APPEND : 0; - if ((ret = write_file (fw->path, fw->write_cursor, buffer, buffer_size, flags)) < 0) + if ((fw->flags & FW_TRUNCATE) && fw->write_cursor == 0) { + wf_flags |= WF_TRUNCATE; + } + + if ((ret = write_file (fw->path, fw->write_cursor, buffer, buffer_size, wf_flags)) < 0) return -FW_WRITE_ERROR; fw->write_cursor += buffer_size; + if (fw->write_cursor > fw->file_size) + fw->file_size = fw->write_cursor; } return FW_OK; diff --git a/libioutil/filewriter.h b/libioutil/filewriter.h index 8ae9fa3..4a8d8fd 100644 --- a/libioutil/filewriter.h +++ b/libioutil/filewriter.h @@ -17,6 +17,7 @@ #define FW_CREATE_FILE (1 << 0) #define FW_APPEND (1 << 1) +#define FW_TRUNCATE (1 << 2) #define FW_OPEN (1 << 31) struct filewriter {