Skip to content

Commit 961fab7

Browse files
dpgeorgegeky
authored andcommitted
Added file config structure and lfs_file_opencfg
The optional config structure options up the possibility of adding file-level configuration in a backwards compatible manner. Also adds possibility to open multiple files with LFS_NO_MALLOC enabled thanks to dpgeorge Also bumped minor version to v1.5
1 parent f94d233 commit 961fab7

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

lfs.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ static int lfs_ctz_traverse(lfs_t *lfs,
12821282

12831283

12841284
/// Top level file operations ///
1285-
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
1286-
const char *path, int flags) {
1285+
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
1286+
const char *path, int flags,
1287+
const struct lfs_file_config *cfg) {
12871288
// deorphan if we haven't yet, needed at most once after poweron
12881289
if ((flags & 3) != LFS_O_RDONLY && !lfs->deorphaned) {
12891290
int err = lfs_deorphan(lfs);
@@ -1323,6 +1324,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
13231324
}
13241325

13251326
// setup file struct
1327+
file->cfg = cfg;
13261328
file->pair[0] = cwd.pair[0];
13271329
file->pair[1] = cwd.pair[1];
13281330
file->poff = entry.off;
@@ -1340,7 +1342,10 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
13401342
}
13411343

13421344
// allocate buffer if needed
1343-
if (lfs->cfg->file_buffer) {
1345+
file->cache.block = 0xffffffff;
1346+
if (file->cfg && file->cfg->buffer) {
1347+
file->cache.buffer = file->cfg->buffer;
1348+
} else if (lfs->cfg->file_buffer) {
13441349
if (lfs->files) {
13451350
// already in use
13461351
return LFS_ERR_NOMEM;
@@ -1368,6 +1373,11 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
13681373
return 0;
13691374
}
13701375

1376+
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
1377+
const char *path, int flags) {
1378+
return lfs_file_opencfg(lfs, file, path, flags, NULL);
1379+
}
1380+
13711381
int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
13721382
int err = lfs_file_sync(lfs, file);
13731383

@@ -1380,7 +1390,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
13801390
}
13811391

13821392
// clean up memory
1383-
if (!lfs->cfg->file_buffer) {
1393+
if (!(file->cfg && file->cfg->buffer) && !lfs->cfg->file_buffer) {
13841394
lfs_free(file->cache.buffer);
13851395
}
13861396

lfs.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C"
2121
// Software library version
2222
// Major (top-nibble), incremented on backwards incompatible changes
2323
// Minor (bottom-nibble), incremented on feature additions
24-
#define LFS_VERSION 0x00010004
24+
#define LFS_VERSION 0x00010005
2525
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
2626
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
2727

@@ -167,6 +167,12 @@ struct lfs_config {
167167
void *file_buffer;
168168
};
169169

170+
// Optional configuration provided during lfs_file_opencfg
171+
struct lfs_file_config {
172+
// Optional, statically allocated buffer for files. Must be program sized.
173+
// If NULL, malloc will be used by default.
174+
void *buffer;
175+
};
170176

171177
// File info structure
172178
struct lfs_info {
@@ -214,6 +220,7 @@ typedef struct lfs_file {
214220
lfs_block_t head;
215221
lfs_size_t size;
216222

223+
const struct lfs_file_config *cfg;
217224
uint32_t flags;
218225
lfs_off_t pos;
219226
lfs_block_t block;
@@ -281,7 +288,8 @@ typedef struct lfs {
281288
// Format a block device with the littlefs
282289
//
283290
// Requires a littlefs object and config struct. This clobbers the littlefs
284-
// object, and does not leave the filesystem mounted.
291+
// object, and does not leave the filesystem mounted. The config struct must
292+
// be zeroed for defaults and backwards compatibility.
285293
//
286294
// Returns a negative error code on failure.
287295
int lfs_format(lfs_t *lfs, const struct lfs_config *config);
@@ -290,7 +298,8 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *config);
290298
//
291299
// Requires a littlefs object and config struct. Multiple filesystems
292300
// may be mounted simultaneously with multiple littlefs objects. Both
293-
// lfs and config must be allocated while mounted.
301+
// lfs and config must be allocated while mounted. The config struct must
302+
// be zeroed for defaults and backwards compatibility.
294303
//
295304
// Returns a negative error code on failure.
296305
int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
@@ -328,14 +337,27 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
328337

329338
// Open a file
330339
//
331-
// The mode that the file is opened in is determined
332-
// by the flags, which are values from the enum lfs_open_flags
333-
// that are bitwise-ored together.
340+
// The mode that the file is opened in is determined by the flags, which
341+
// are values from the enum lfs_open_flags that are bitwise-ored together.
334342
//
335343
// Returns a negative error code on failure.
336344
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
337345
const char *path, int flags);
338346

347+
// Open a file with extra configuration
348+
//
349+
// The mode that the file is opened in is determined by the flags, which
350+
// are values from the enum lfs_open_flags that are bitwise-ored together.
351+
//
352+
// The config struct provides additional config options per file as described
353+
// above. The config struct must be allocated while the file is open, and the
354+
// config struct must be zeroed for defaults and backwards compatibility.
355+
//
356+
// Returns a negative error code on failure.
357+
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
358+
const char *path, int flags,
359+
const struct lfs_file_config *config);
360+
339361
// Close a file
340362
//
341363
// Any pending writes are written out to storage as though

0 commit comments

Comments
 (0)