Skip to content

Commit

Permalink
https://github.com/lorol/LITTLEFS/issues/10
Browse files Browse the repository at this point in the history
Following:
Sync file upon opening to free storage when overwriting a file
joltwallet/esp_littlefs#22

Fixes out-of-storage issues when overwriting a file by syncing-on-open. Not that this effectively immediately deletes the file being overwritten.

Based on:
esp8266/Arduino#7434
  • Loading branch information
lorol committed Nov 15, 2020
1 parent f1f5e3a commit 90dd66f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/esp_littlefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,22 @@ static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode)
return LFS_ERR_INVAL;
}

/* Sync after opening. If we are overwriting a file, this will free that
* file's blocks in storage, prevent OOS errors.
* See TEST_CASE:
* "Rewriting file frees space immediately (#7426)"
*/
res = lfs_file_sync(efs->fs, &file->file);
if(res < 0){
errno = -res;
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(TAG, "Failed to sync at opening file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);
#else
ESP_LOGV(TAG, "Failed to sync at opening file %d. Error %d", fd, res);
#endif
}

file->hash = compute_hash(path);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
memcpy(file->path, path, path_len);
Expand Down

0 comments on commit 90dd66f

Please sign in to comment.