Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

the function lfs_file_flush does so much reiterated work? #439

Open
chrome777 opened this issue Jun 10, 2020 · 2 comments
Open

the function lfs_file_flush does so much reiterated work? #439

chrome777 opened this issue Jun 10, 2020 · 2 comments

Comments

@chrome777
Copy link

chrome777 commented Jun 10, 2020

static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
    LFS_ASSERT(file->flags & LFS_F_OPENED);

    ......
            while (file->pos < file->ctz.size) {
                // copy over a byte at a time, leave it up to caching
                // to make this efficient
                uint8_t data;
                lfs_ssize_t res = lfs_file_read(lfs, &orig, &data, 1);
                if (res < 0) {
                    return res;
                }

                res = lfs_file_write(lfs, file, &data, 1);
                if (res < 0) {
                    return res;
                }

                // keep our reference to the rcache in sync
                if (lfs->rcache.block != LFS_BLOCK_NULL) {
                    lfs_cache_drop(lfs, &orig.cache);
                    lfs_cache_drop(lfs, &lfs->rcache);
                }
            }

    ......

    return 0;
}

when we use lfs_file_seek to change the file->pos, it repeats reading and writing till the file->pos is equal to the file->ctz.size; any problems with my understanding?

eg: we have already created a 1MiB file, when we change the file->pos to the head, and close the file after writen a byte data 0x05, then the function lfs_file_seek takes effect, repeats 1M times to read and write, it may take a long time waiting, does this design reasonable?

Ignore my poor English. Hope to get some suggestions. thanks

@Johnxjj
Copy link

Johnxjj commented Jun 12, 2020

Modifying the header data will overwrite the following 1M data。

@geky
Copy link
Member

geky commented Jul 17, 2020

then the function lfs_file_seek takes effect, repeats 1M times to read and write

This sounds like an issue with littlefs's append-only behavior. Unfortunately, random writes turn into rewriting the entire files.
#27 (comment)

It's something I want to fix, but will take some time.

(also your English is very understandable)

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants