fs: littlefs: sample: Move value increment test to separate function
The code responsible for incrementing the in-file value has been moved to the separate function for better readability and reusage. Signed-off-by: Lukasz Majewski <lukma@denx.de>
This commit is contained in:
parent
f6204e607c
commit
982a739bb1
1 changed files with 53 additions and 28 deletions
|
@ -77,6 +77,52 @@ static int lsdir(const char *path)
|
|||
return res;
|
||||
}
|
||||
|
||||
static int littlefs_increase_infile_value(char *fname)
|
||||
{
|
||||
uint8_t boot_count = 0;
|
||||
struct fs_file_t file;
|
||||
int rc, ret;
|
||||
|
||||
fs_file_t_init(&file);
|
||||
rc = fs_open(&file, fname, FS_O_CREATE | FS_O_RDWR);
|
||||
if (rc < 0) {
|
||||
LOG_ERR("FAIL: open %s: %d", log_strdup(fname), rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = fs_read(&file, &boot_count, sizeof(boot_count));
|
||||
if (rc < 0) {
|
||||
LOG_ERR("FAIL: read %s: [rd:%d]", log_strdup(fname), rc);
|
||||
goto out;
|
||||
}
|
||||
LOG_PRINTK("%s read count:%u (bytes: %d)\n", fname, boot_count, rc);
|
||||
|
||||
rc = fs_seek(&file, 0, FS_SEEK_SET);
|
||||
if (rc < 0) {
|
||||
LOG_ERR("FAIL: seek %s: %d", log_strdup(fname), rc);
|
||||
goto out;
|
||||
}
|
||||
|
||||
boot_count += 1;
|
||||
rc = fs_write(&file, &boot_count, sizeof(boot_count));
|
||||
if (rc < 0) {
|
||||
LOG_ERR("FAIL: write %s: %d", log_strdup(fname), rc);
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_PRINTK("%s write new boot count %u: [wr:%d]\n", fname,
|
||||
boot_count, rc);
|
||||
|
||||
out:
|
||||
ret = fs_close(&file);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("FAIL: close %s: %d", log_strdup(fname), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return (rc < 0 ? rc : 0);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
struct fs_mount_t *mp =
|
||||
|
@ -140,38 +186,17 @@ void main(void)
|
|||
sbuf.f_bsize, sbuf.f_frsize,
|
||||
sbuf.f_blocks, sbuf.f_bfree);
|
||||
|
||||
struct fs_file_t file;
|
||||
|
||||
fs_file_t_init(&file);
|
||||
|
||||
rc = fs_open(&file, fname, FS_O_CREATE | FS_O_RDWR);
|
||||
if (rc < 0) {
|
||||
LOG_PRINTK("FAIL: open %s: %d\n", fname, rc);
|
||||
goto out;
|
||||
}
|
||||
|
||||
uint32_t boot_count = 0;
|
||||
|
||||
if (rc >= 0) {
|
||||
rc = fs_read(&file, &boot_count, sizeof(boot_count));
|
||||
LOG_PRINTK("%s read count %u: %d\n", fname, boot_count, rc);
|
||||
rc = fs_seek(&file, 0, FS_SEEK_SET);
|
||||
LOG_PRINTK("%s seek start: %d\n", fname, rc);
|
||||
|
||||
}
|
||||
|
||||
boot_count += 1;
|
||||
rc = fs_write(&file, &boot_count, sizeof(boot_count));
|
||||
LOG_PRINTK("%s write new boot count %u: %d\n", fname,
|
||||
boot_count, rc);
|
||||
|
||||
rc = fs_close(&file);
|
||||
LOG_PRINTK("%s close: %d\n", fname, rc);
|
||||
|
||||
rc = lsdir(mp->mnt_point);
|
||||
if (rc < 0) {
|
||||
LOG_PRINTK("FAIL: lsdir %s: %d\n", mp->mnt_point, rc);
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = littlefs_increase_infile_value(fname);
|
||||
if (rc) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
rc = fs_unmount(mp);
|
||||
LOG_PRINTK("%s unmount: %d\n", mp->mnt_point, rc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue