mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 08:42:40 +01:00 
			
		
		
		
	Fixed missing erase during file relocation
This was an easy fix, but highlighted the fact that the current testing framework doesn't detect when a block is written to without an associated erase. Added a quick solution that creates an empty file during an erase.
This commit is contained in:
		| @@ -127,13 +127,13 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block, | |||||||
|     snprintf(emu->child, LFS_NAME_MAX, "%x", block); |     snprintf(emu->child, LFS_NAME_MAX, "%x", block); | ||||||
|  |  | ||||||
|     FILE *f = fopen(emu->path, "r+b"); |     FILE *f = fopen(emu->path, "r+b"); | ||||||
|     if (!f && errno == ENOENT) { |     if (!f && errno != ENOENT) { | ||||||
|         f = fopen(emu->path, "w+b"); |         return -errno; | ||||||
|         if (!f) { |  | ||||||
|             return -errno; |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // Check that file was erased | ||||||
|  |     assert(f); | ||||||
|  |  | ||||||
|     int err = fseek(f, off, SEEK_SET); |     int err = fseek(f, off, SEEK_SET); | ||||||
|     if (err) { |     if (err) { | ||||||
|         return -errno; |         return -errno; | ||||||
| @@ -185,6 +185,18 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     if (err || S_ISREG(st.st_mode)) { | ||||||
|  |         FILE *f = fopen(emu->path, "w"); | ||||||
|  |         if (!f) { | ||||||
|  |             return -errno; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         err = fclose(f); | ||||||
|  |         if (err) { | ||||||
|  |             return -errno; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     emu->stats.erase_count += 1; |     emu->stats.erase_count += 1; | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -1196,6 +1196,14 @@ relocate: | |||||||
|         return err; |         return err; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     err = lfs_bd_erase(lfs, nblock); | ||||||
|  |     if (err) { | ||||||
|  |         if (err == LFS_ERR_CORRUPT) { | ||||||
|  |             goto relocate; | ||||||
|  |         } | ||||||
|  |         return err; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     // either read from dirty cache or disk |     // either read from dirty cache or disk | ||||||
|     for (lfs_off_t i = 0; i < file->off; i++) { |     for (lfs_off_t i = 0; i < file->off; i++) { | ||||||
|         uint8_t data; |         uint8_t data; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user