[fs][spifs] Add truncate functionality to spifs.
This commit is contained in:
@@ -1032,6 +1032,34 @@ err:
|
||||
return len == 0 ? (ssize_t)size : err;
|
||||
}
|
||||
|
||||
static status_t spifs_truncate(filecookie *fcookie, uint64_t len)
|
||||
{
|
||||
LTRACEF("filecookie %p, len %llu\n", fcookie, len);
|
||||
|
||||
status_t rc = NO_ERROR;
|
||||
|
||||
spifs_file_t *file = (spifs_file_t *)fcookie;
|
||||
|
||||
mutex_acquire(&file->fs_handle->lock);
|
||||
|
||||
spifs_t *spifs = (spifs_t *)(file->fs_handle);
|
||||
|
||||
// Can't use truncate to grow a file.
|
||||
if (len > file->metadata.length) {
|
||||
rc = ERR_INVALID_ARGS;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
file->metadata.length = len;
|
||||
|
||||
rc = spifs_commit_toc(spifs);
|
||||
|
||||
finish:
|
||||
mutex_release(&file->fs_handle->lock);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static status_t spifs_stat(filecookie *fcookie, struct file_stat *stat)
|
||||
{
|
||||
LTRACEF("filecookie %p stat %p\n", fcookie, stat);
|
||||
@@ -1194,6 +1222,7 @@ static const struct fs_api spifs_api = {
|
||||
|
||||
.read = spifs_read,
|
||||
.write = spifs_write,
|
||||
.truncate = spifs_truncate,
|
||||
|
||||
.stat = spifs_stat,
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ static bool test_corrupt_toc(const char *);
|
||||
static bool test_write_with_offset(const char *);
|
||||
static bool test_read_write_big(const char *);
|
||||
static bool test_rm_active_dirent(const char *);
|
||||
static bool test_truncate_file(const char *);
|
||||
|
||||
static test tests[] = {
|
||||
{&test_empty_after_format, "Test no files in ToC after format.", 1},
|
||||
@@ -72,6 +73,7 @@ static test tests[] = {
|
||||
{&test_write_with_offset, "Test that files can be written to at an offset.", 1},
|
||||
{&test_read_write_big, "Test that an unaligned ~10kb buffer can be written and read.", 1},
|
||||
{&test_rm_active_dirent, "Test that we can remove a file with an open dirent.", 1},
|
||||
{&test_truncate_file, "Test that we can truncate a file.", 1},
|
||||
};
|
||||
|
||||
bool test_setup(const char *dev_name, uint32_t toc_pages)
|
||||
@@ -627,6 +629,35 @@ static bool test_rm_active_dirent(const char *dev_name)
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool test_truncate_file(const char *dev_name)
|
||||
{
|
||||
filehandle *handle;
|
||||
status_t status =
|
||||
fs_create_file(TEST_FILE_PATH, &handle, 1024);
|
||||
if (status != NO_ERROR) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// File size is 1024?
|
||||
struct file_stat stat;
|
||||
fs_stat_file(handle, &stat);
|
||||
if (stat.size != 1024) {
|
||||
return false;
|
||||
}
|
||||
|
||||
status = fs_truncate_file(handle, 512);
|
||||
if (status != NO_ERROR) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fs_stat_file(handle, &stat);
|
||||
if (stat.size != 512) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return fs_close_file(handle) == NO_ERROR;
|
||||
}
|
||||
|
||||
// Run the SPIFS test suite.
|
||||
static int spifs_test(int argc, const cmd_args *argv)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user