Skip to content

Commit

Permalink
Merge pull request #172 from wang33winner/master
Browse files Browse the repository at this point in the history
fix the bug: if LITTLEFS_SDMMC_SUPPORT turns on, some features are pr…
  • Loading branch information
BrianPugh authored Jan 29, 2024
2 parents 5a0a9a0 + 63ba9c6 commit 1e2bb0a
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions src/esp_littlefs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file esp_littlefs.c
* @brief Maps LittleFS <-> ESP_VFS
* @brief Maps LittleFS <-> ESP_VFS
* @author Brian Pugh
*/

Expand Down Expand Up @@ -186,7 +186,7 @@ static void esp_littlefs_free_fds(esp_littlefs_t * efs) {
free(efs->file);
efs->file = next;
}
free(efs->cache);
free(efs->cache);
efs->cache = 0;
efs->cache_size = efs->fd_count = 0;
}
Expand Down Expand Up @@ -253,13 +253,12 @@ esp_err_t format_from_efs(esp_littlefs_t *efs)
if (efs->sdcard) {
res = lfs_format(efs->fs, &efs->cfg);
} else
#else
#endif
{
efs->cfg.block_count = efs->partition->size / efs->cfg.block_size;
res = lfs_format(efs->fs, &efs->cfg);
efs->cfg.block_count = 0;
}
#endif

if( res != LFS_ERR_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to format filesystem");
Expand All @@ -281,7 +280,7 @@ esp_err_t format_from_efs(esp_littlefs_t *efs)
efs->cache = esp_littlefs_calloc(sizeof(*efs->cache), efs->cache_size);
}
ESP_LOGV(ESP_LITTLEFS_TAG, "Format Success!");

return ESP_OK;
}

Expand Down Expand Up @@ -489,7 +488,7 @@ esp_err_t esp_littlefs_format(const char* partition_label) {
efs_free = true;
const esp_vfs_littlefs_conf_t conf = {
/* base_name not necessary for initializing */
.dont_mount = true,
.dont_mount = true,
.partition_label = partition_label,
};
err = esp_littlefs_init(&conf);
Expand Down Expand Up @@ -530,7 +529,7 @@ esp_err_t esp_littlefs_format_partition(const esp_partition_t* partition) {
efs_free = true;
const esp_vfs_littlefs_conf_t conf = {
/* base_name not necessary for initializing */
.dont_mount = true,
.dont_mount = true,
.partition_label = NULL,
.partition = partition,
};
Expand Down Expand Up @@ -798,7 +797,7 @@ static esp_err_t esp_littlefs_by_sdmmc_handle(sdmmc_card_t *handle, int *index)

/**
* @brief Get the index of an unallocated LittleFS slot.
* @param[out] index Indexd of free LittleFS slot
* @param[out] index Indexd of free LittleFS slot
* @return ESP_OK on success
*/
static esp_err_t esp_littlefs_get_empty(int *index) {
Expand Down Expand Up @@ -976,7 +975,7 @@ static esp_err_t esp_littlefs_init_efs(esp_littlefs_t** efs, const esp_partition
}

/**
* @brief Initialize and mount littlefs
* @brief Initialize and mount littlefs
* @param[in] conf Filesystem Configuration
* @return ESP_OK on success
*/
Expand Down Expand Up @@ -1072,11 +1071,10 @@ static esp_err_t esp_littlefs_init(const esp_vfs_littlefs_conf_t* conf)
if (conf->sdcard) {
err = esp_littlefs_format_sdmmc(conf->sdcard);
} else
#else
#endif
{
err = esp_littlefs_format_partition(efs->partition);
}
#endif
if(err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "format failed");
err = ESP_FAIL;
Expand All @@ -1093,15 +1091,14 @@ static esp_err_t esp_littlefs_init(const esp_vfs_littlefs_conf_t* conf)
efs->cache = esp_littlefs_calloc(sizeof(*efs->cache), efs->cache_size);

if(conf->grow_on_mount){
#ifndef CONFIG_LITTLEFS_SDMMC_SUPPORT
res = lfs_fs_grow(efs->fs, efs->partition->size / efs->cfg.block_size);
#else
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (efs->sdcard) {
res = lfs_fs_grow(efs->fs, efs->sdcard->csd.capacity);
} else {
} else
#endif
{
res = lfs_fs_grow(efs->fs, efs->partition->size / efs->cfg.block_size);
}
#endif
if (res != LFS_ERR_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "FS grow failed, %s (%i)", esp_littlefs_errno(res), res);
err = ESP_FAIL;
Expand Down Expand Up @@ -1153,18 +1150,18 @@ static inline int sem_give(esp_littlefs_t *efs) {
}


/* We are using a double allocation system here, which an array and a linked list.
/* We are using a double allocation system here, which an array and a linked list.
The array contains the pointer to the file descriptor (the index in the array is what's returned to the user).
The linked list is used for file descriptors.
This means that position of nodes in the list must stay consistent:
- Allocation is obvious (append to the list from the head, and realloc the pointers array)
There is still a O(N) search in the cache for a free position to store
- Searching is a O(1) process (good)
- Deallocation is more tricky. That is, for example,
- Deallocation is more tricky. That is, for example,
if you need to remove node 5 in a 12 nodes list, you'll have to:
1) Mark the 5th position as freed (if it's the last position of the array realloc smaller)
2) Walk the list until finding the pointer to the node O(N) and scrub the node so the chained list stays consistent
3) Deallocate the node
3) Deallocate the node
*/

/**
Expand Down Expand Up @@ -1212,21 +1209,21 @@ static int esp_littlefs_allocate_fd(esp_littlefs_t *efs, vfs_littlefs_file_t **
if (*file == NULL) {
/* If it fails here, the file system might have a larger cache, but it's harmless, no need to reverse it */
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to allocate FD");
return -1;
return -1;
}

/* Starting from here, nothing can fail anymore */

#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
/* The trick here is to avoid dual allocation so the path pointer
/* The trick here is to avoid dual allocation so the path pointer
should point to the next byte after it:
file => [ lfs_file | # | next | path | free_space ]
| /\
|__/
*/
(*file)->path = (char*)(*file) + sizeof(**file);
#endif

/* Now find a free place in cache */
for(i=0; i < efs->cache_size; i++) {
if (efs->cache[i] == NULL) {
Expand Down Expand Up @@ -1271,7 +1268,7 @@ static int esp_littlefs_free_fd(esp_littlefs_t *efs, int fd){
ESP_LOGE(ESP_LITTLEFS_TAG, "Inconsistent list");
return -1;
}
/* Transaction starts here and can't fail anymore */
/* Transaction starts here and can't fail anymore */
head->next = file->next;
}
efs->cache[fd] = NULL;
Expand Down Expand Up @@ -1320,7 +1317,7 @@ static int esp_littlefs_free_fd(esp_littlefs_t *efs, int fd){
/**
* @brief Compute the 32bit DJB2 hash of the given string.
* @param[in] path the path to hash
* @returns the hash for this path
* @returns the hash for this path
*/
static uint32_t compute_hash(const char * path) {
uint32_t hash = 5381;
Expand All @@ -1346,7 +1343,7 @@ static int esp_littlefs_get_fd_by_name(esp_littlefs_t *efs, const char *path){

for(uint16_t i=0, j=0; i < efs->cache_size && j < efs->fd_count; i++){
if (efs->cache[i]) {
++j;
++j;

if (
efs->cache[i]->hash == hash // Faster than strcmp
Expand Down Expand Up @@ -1732,7 +1729,7 @@ static off_t vfs_littlefs_lseek(void* ctx, int fd, off_t offset, int mode) {
case SEEK_SET: whence = LFS_SEEK_SET; break;
case SEEK_CUR: whence = LFS_SEEK_CUR; break;
case SEEK_END: whence = LFS_SEEK_END; break;
default:
default:
ESP_LOGE(ESP_LITTLEFS_TAG, "Invalid mode");
errno = EINVAL;
return -1;
Expand Down Expand Up @@ -1823,7 +1820,7 @@ static int vfs_littlefs_fstat(void* ctx, int fd, struct stat * st) {
return -1;
}

#if CONFIG_LITTLEFS_USE_MTIME
#if CONFIG_LITTLEFS_USE_MTIME
st->st_mtime = vfs_littlefs_get_mtime(efs, file->path);
#endif

Expand Down Expand Up @@ -1856,7 +1853,7 @@ static int vfs_littlefs_stat(void* ctx, const char * path, struct stat * st) {
path, esp_littlefs_errno(res), res);
return -1;
}
#if CONFIG_LITTLEFS_USE_MTIME
#if CONFIG_LITTLEFS_USE_MTIME
st->st_mtime = vfs_littlefs_get_mtime(efs, path);
#endif
sem_give(efs);
Expand Down Expand Up @@ -1984,7 +1981,7 @@ static DIR* vfs_littlefs_opendir(void* ctx, const char* name) {
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to opendir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);
#else
Expand All @@ -2011,7 +2008,7 @@ static int vfs_littlefs_closedir(void* ctx, DIR* pdir) {
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to closedir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);
#else
Expand Down Expand Up @@ -2050,7 +2047,7 @@ static int vfs_littlefs_readdir_r(void* ctx, DIR* pdir,
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to readdir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);
#else
Expand Down Expand Up @@ -2321,7 +2318,7 @@ static time_t vfs_littlefs_get_mtime(esp_littlefs_t *efs, const char *path)
&t, sizeof(t));
if( size < 0 ) {
errno = lfs_errno_remap(size);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to get mtime attribute %s (%d)",
esp_littlefs_errno(size), size);
#else
Expand Down

0 comments on commit 1e2bb0a

Please sign in to comment.