littlefs icon indicating copy to clipboard operation
littlefs copied to clipboard

Add lfs_file_getattr and lfs_file_setattr

Open DS-LK opened this issue 3 years ago • 4 comments

I found that in the process of using littlefs, sometimes the save path is not obtained or obtained, and the custom attribute cannot be used at this time. I think it is better to add a file parameter, it allows us to apply more scenarios, and can also save memory usage in some cases.

DS-LK avatar Jul 13 '21 02:07 DS-LK

Add a function to get the path

DS-LK avatar Oct 19 '21 02:10 DS-LK

@geky Hello, can you take some time to review it, thank you very much.

DS-LK avatar Oct 19 '21 02:10 DS-LK

Would love for this feature to be merged.

dpkristensen avatar Dec 20 '22 16:12 dpkristensen

Hi @DS-LK, thanks for creating a PR, sorry about the extremely late response. I should have commented on this earlier.

This functionality is a great idea, but unfortunately I don't think it fits into how littlefs currently models open files.

Consider:

// attr in RAM
struct lfs_attr example_attrs[1] = {
    {.type=1, .buffer=&(uint8_t[1]){}, .size=1}
};
struct lfs_file_config file_config = {.attrs=example_attrs, .attr_count=1};

lfs_file_t file;
lfs_file_opencfg(lfs, &file, "example_file", LFS_O_RDWR, &file_config);

// write RAM attr
memset(example_attrs[0].buffer, "a", 1);

// write file attr
lfs_file_setattr(lfs, &file, "b", 1);

// close file 
lfs_file_close(lfs, &file);

With this implementation the file attr will be set to "a".

A bit less clear is that this implementation also immediately writes the attrs to disk, where as other lfs_file_* functions stage changes to be written until the next lfs_file_sync.

There may be ways of making this work where lfs_file_setattr only updates RAM-backed attrs or errors, but I think a better solution would be to address the core problem @dpkristensen highlighted in #759. i.e. make it so RAM-backed attrs are always fetched during lfs_file_open.

geky avatar May 01 '23 19:05 geky