Fix FAT driver file modes, update filewriter accordingly
Some checks failed
Build documentation / build-and-deploy (push) Has been cancelled
Some checks failed
Build documentation / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user