mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 16:14:16 +01:00 
			
		
		
		
	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
This commit is contained in:
		
				
					committed by
					
						 Christopher Haster
						Christopher Haster
					
				
			
			
				
	
			
			
			
						parent
						
							f94d233deb
						
					
				
				
					commit
					961fab70c3
				
			
							
								
								
									
										18
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -1282,8 +1282,9 @@ static int lfs_ctz_traverse(lfs_t *lfs, | |||||||
|  |  | ||||||
|  |  | ||||||
| /// Top level file operations /// | /// Top level file operations /// | ||||||
| int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, | ||||||
|         const char *path, int flags) { |         const char *path, int flags, | ||||||
|  |         const struct lfs_file_config *cfg) { | ||||||
|     // deorphan if we haven't yet, needed at most once after poweron |     // deorphan if we haven't yet, needed at most once after poweron | ||||||
|     if ((flags & 3) != LFS_O_RDONLY && !lfs->deorphaned) { |     if ((flags & 3) != LFS_O_RDONLY && !lfs->deorphaned) { | ||||||
|         int err = lfs_deorphan(lfs); |         int err = lfs_deorphan(lfs); | ||||||
| @@ -1323,6 +1324,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // setup file struct |     // setup file struct | ||||||
|  |     file->cfg = cfg; | ||||||
|     file->pair[0] = cwd.pair[0]; |     file->pair[0] = cwd.pair[0]; | ||||||
|     file->pair[1] = cwd.pair[1]; |     file->pair[1] = cwd.pair[1]; | ||||||
|     file->poff = entry.off; |     file->poff = entry.off; | ||||||
| @@ -1340,7 +1342,10 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // allocate buffer if needed |     // allocate buffer if needed | ||||||
|     if (lfs->cfg->file_buffer) { |     file->cache.block = 0xffffffff; | ||||||
|  |     if (file->cfg && file->cfg->buffer) { | ||||||
|  |         file->cache.buffer = file->cfg->buffer; | ||||||
|  |     } else if (lfs->cfg->file_buffer) { | ||||||
|         if (lfs->files) { |         if (lfs->files) { | ||||||
|             // already in use |             // already in use | ||||||
|             return LFS_ERR_NOMEM; |             return LFS_ERR_NOMEM; | ||||||
| @@ -1368,6 +1373,11 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | |||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | ||||||
|  |         const char *path, int flags) { | ||||||
|  |     return lfs_file_opencfg(lfs, file, path, flags, NULL); | ||||||
|  | } | ||||||
|  |  | ||||||
| int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { | int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { | ||||||
|     int err = lfs_file_sync(lfs, file); |     int err = lfs_file_sync(lfs, file); | ||||||
|  |  | ||||||
| @@ -1380,7 +1390,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // clean up memory |     // clean up memory | ||||||
|     if (!lfs->cfg->file_buffer) { |     if (!(file->cfg && file->cfg->buffer) && !lfs->cfg->file_buffer) { | ||||||
|         lfs_free(file->cache.buffer); |         lfs_free(file->cache.buffer); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										34
									
								
								lfs.h
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								lfs.h
									
									
									
									
									
								
							| @@ -21,7 +21,7 @@ extern "C" | |||||||
| // Software library version | // Software library version | ||||||
| // Major (top-nibble), incremented on backwards incompatible changes | // Major (top-nibble), incremented on backwards incompatible changes | ||||||
| // Minor (bottom-nibble), incremented on feature additions | // Minor (bottom-nibble), incremented on feature additions | ||||||
| #define LFS_VERSION 0x00010004 | #define LFS_VERSION 0x00010005 | ||||||
| #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16)) | #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16)) | ||||||
| #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >>  0)) | #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >>  0)) | ||||||
|  |  | ||||||
| @@ -167,6 +167,12 @@ struct lfs_config { | |||||||
|     void *file_buffer; |     void *file_buffer; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | // Optional configuration provided during lfs_file_opencfg | ||||||
|  | struct lfs_file_config { | ||||||
|  |     // Optional, statically allocated buffer for files. Must be program sized. | ||||||
|  |     // If NULL, malloc will be used by default. | ||||||
|  |     void *buffer; | ||||||
|  | }; | ||||||
|  |  | ||||||
| // File info structure | // File info structure | ||||||
| struct lfs_info { | struct lfs_info { | ||||||
| @@ -214,6 +220,7 @@ typedef struct lfs_file { | |||||||
|     lfs_block_t head; |     lfs_block_t head; | ||||||
|     lfs_size_t size; |     lfs_size_t size; | ||||||
|  |  | ||||||
|  |     const struct lfs_file_config *cfg; | ||||||
|     uint32_t flags; |     uint32_t flags; | ||||||
|     lfs_off_t pos; |     lfs_off_t pos; | ||||||
|     lfs_block_t block; |     lfs_block_t block; | ||||||
| @@ -281,7 +288,8 @@ typedef struct lfs { | |||||||
| // Format a block device with the littlefs | // Format a block device with the littlefs | ||||||
| // | // | ||||||
| // Requires a littlefs object and config struct. This clobbers the littlefs | // Requires a littlefs object and config struct. This clobbers the littlefs | ||||||
| // object, and does not leave the filesystem mounted. | // object, and does not leave the filesystem mounted. The config struct must | ||||||
|  | // be zeroed for defaults and backwards compatibility. | ||||||
| // | // | ||||||
| // Returns a negative error code on failure. | // Returns a negative error code on failure. | ||||||
| int lfs_format(lfs_t *lfs, const struct lfs_config *config); | 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); | |||||||
| // | // | ||||||
| // Requires a littlefs object and config struct. Multiple filesystems | // Requires a littlefs object and config struct. Multiple filesystems | ||||||
| // may be mounted simultaneously with multiple littlefs objects. Both | // may be mounted simultaneously with multiple littlefs objects. Both | ||||||
| // lfs and config must be allocated while mounted. | // lfs and config must be allocated while mounted. The config struct must | ||||||
|  | // be zeroed for defaults and backwards compatibility. | ||||||
| // | // | ||||||
| // Returns a negative error code on failure. | // Returns a negative error code on failure. | ||||||
| int lfs_mount(lfs_t *lfs, const struct lfs_config *config); | 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); | |||||||
|  |  | ||||||
| // Open a file | // Open a file | ||||||
| // | // | ||||||
| // The mode that the file is opened in is determined | // The mode that the file is opened in is determined by the flags, which | ||||||
| // by the flags, which are values from the enum lfs_open_flags | // are values from the enum lfs_open_flags that are bitwise-ored together. | ||||||
| // that are bitwise-ored together. |  | ||||||
| // | // | ||||||
| // Returns a negative error code on failure. | // Returns a negative error code on failure. | ||||||
| int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | int lfs_file_open(lfs_t *lfs, lfs_file_t *file, | ||||||
|         const char *path, int flags); |         const char *path, int flags); | ||||||
|  |  | ||||||
|  | // Open a file with extra configuration | ||||||
|  | // | ||||||
|  | // The mode that the file is opened in is determined by the flags, which | ||||||
|  | // are values from the enum lfs_open_flags that are bitwise-ored together. | ||||||
|  | // | ||||||
|  | // The config struct provides additional config options per file as described | ||||||
|  | // above. The config struct must be allocated while the file is open, and the | ||||||
|  | // config struct must be zeroed for defaults and backwards compatibility. | ||||||
|  | // | ||||||
|  | // Returns a negative error code on failure. | ||||||
|  | int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, | ||||||
|  |         const char *path, int flags, | ||||||
|  |         const struct lfs_file_config *config); | ||||||
|  |  | ||||||
| // Close a file | // Close a file | ||||||
| // | // | ||||||
| // Any pending writes are written out to storage as though | // Any pending writes are written out to storage as though | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user