Skip to content

Commit f1f5e3a

Browse files
committed
Follow joltwallet/esp_littlefs undo of PR19.
Clean-up.
1 parent 4e8cbbc commit f1f5e3a

File tree

3 files changed

+64
-205
lines changed

3 files changed

+64
-205
lines changed

src/LITTLEFS.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ bool LITTLEFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpen
4242
esp_vfs_littlefs_conf_t conf = {
4343
.base_path = basePath,
4444
.partition_label = LFS_NAME,
45-
//.max_files = maxOpenFiles,
4645
.format_if_mount_failed = false
4746
};
4847

src/esp_littlefs.c

+64-203
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,25 @@
1212
*/
1313

1414
//#define LOG_LOCAL_LEVEL 5
15-
//#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */
15+
//#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF 3.2 compatibility, core release 1.0.4 - no timestamps */
16+
//#define CONFIG_LITTLEFS_USE_ONLY_HASH
17+
#define CONFIG_LITTLEFS_HUMAN_READABLE 0 /* Use 1 for verbose errors */
1618
#define CONFIG_LITTLEFS_SPIFFS_COMPAT 0 /* Use 1 for better drop-in replacement of SPIFFS */
19+
#define CONFIG_LITTLEFS_MAX_PARTITIONS 3
20+
#define CONFIG_LITTLEFS_PAGE_SIZE 256
21+
#define CONFIG_LITTLEFS_OBJ_NAME_LEN 64
22+
#define CONFIG_LITTLEFS_READ_SIZE 128
23+
#define CONFIG_LITTLEFS_WRITE_SIZE 128
24+
#define CONFIG_LITTLEFS_LOOKAHEAD_SIZE 128
25+
#define CONFIG_LITTLEFS_CACHE_SIZE 512 /* Old value was 128 */
26+
#define CONFIG_LITTLEFS_BLOCK_CYCLES 512
27+
28+
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
29+
#define CONFIG_LITTLEFS_USE_MTIME 0
30+
#else
31+
#define CONFIG_LITTLEFS_USE_MTIME 1
32+
#define CONFIG_LITTLEFS_MTIME_USE_SECONDS 1
33+
#endif
1734

1835
#include "esp_log.h"
1936
#include "esp_spi_flash.h"
@@ -48,24 +65,6 @@ static const char TAG[] = "esp_littlefs";
4865
#define CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE 4 /* Minimum size of FD cache */
4966
#define CONFIG_LITTLEFS_FD_CACHE_HYST 4 /* When shrinking, leave this many trailing FD slots available */
5067

51-
#define CONFIG_LITTLEFS_MAX_PARTITIONS 3
52-
#define CONFIG_LITTLEFS_PAGE_SIZE 256
53-
#define CONFIG_LITTLEFS_OBJ_NAME_LEN 64
54-
#define CONFIG_LITTLEFS_READ_SIZE 128
55-
#define CONFIG_LITTLEFS_WRITE_SIZE 128
56-
#define CONFIG_LITTLEFS_LOOKAHEAD_SIZE 128
57-
#define CONFIG_LITTLEFS_CACHE_SIZE 512
58-
#define CONFIG_LITTLEFS_BLOCK_CYCLES 512
59-
60-
//#define CONFIG_SECURE_FLASH_ENC_ENABLED 1 /* For encrypted, in part.csv: flash_test, data, spiffs, , 512K, encrypted */
61-
62-
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
63-
#define CONFIG_LITTLEFS_USE_MTIME 0
64-
#else
65-
#define CONFIG_LITTLEFS_USE_MTIME 1
66-
#define CONFIG_LITTLEFS_MTIME_USE_SECONDS 1
67-
#endif
68-
6968
/**
7069
* @brief littlefs DIR structure
7170
*/
@@ -80,8 +79,6 @@ typedef struct {
8079
static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode);
8180
static ssize_t vfs_littlefs_write(void* ctx, int fd, const void * data, size_t size);
8281
static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size);
83-
//static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset);
84-
//static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset);
8582
static int vfs_littlefs_close(void* ctx, int fd);
8683
static off_t vfs_littlefs_lseek(void* ctx, int fd, off_t offset, int mode);
8784
static int vfs_littlefs_stat(void* ctx, const char * path, struct stat * st);
@@ -179,10 +176,8 @@ esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t * conf)
179176
const esp_vfs_t vfs = {
180177
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
181178
.write_p = &vfs_littlefs_write,
182-
// .pwrite_p = &vfs_littlefs_pwrite,
183179
.lseek_p = &vfs_littlefs_lseek,
184180
.read_p = &vfs_littlefs_read,
185-
// .pread_p = &vfs_littlefs_pread,
186181
.open_p = &vfs_littlefs_open,
187182
.close_p = &vfs_littlefs_close,
188183
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
@@ -431,7 +426,7 @@ static esp_err_t esp_littlefs_by_label(const char* label, int * index){
431426
}
432427
}
433428

434-
ESP_LOGV(TAG, "Existing filesystem \%s\" not found", label);
429+
ESP_LOGV(TAG, "Existing filesystem \"%s\" not found", label);
435430
return ESP_ERR_NOT_FOUND;
436431
}
437432

@@ -740,8 +735,6 @@ static int esp_littlefs_allocate_fd(esp_littlefs_t *efs, vfs_littlefs_file_t **
740735
*/
741736
(*file)->path = (char*)(*file) + sizeof(**file);
742737
#endif
743-
744-
(*file)->open_count = 1;
745738

746739
/* Now find a free place in cache */
747740
for(i=0; i < efs->cache_size; i++) {
@@ -898,53 +891,46 @@ static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode)
898891
/* Get a FD */
899892
sem_take(efs);
900893

901-
if((fd = esp_littlefs_get_fd_by_name(efs, path)) >= 0) {
902-
/* FD is already open, increase the reference counter*/
903-
efs->cache[fd]->open_count++;
904-
}
905-
else {
906-
/* Need to allocate a new FD */
907-
fd = esp_littlefs_allocate_fd(efs, &file
894+
fd = esp_littlefs_allocate_fd(efs, &file
908895
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
909-
, path_len
896+
, path_len
910897
#endif
911-
);
898+
);
912899

913-
if(fd < 0) {
914-
errno = -fd;
915-
sem_give(efs);
916-
ESP_LOGV(TAG, "Error obtaining FD");
917-
return LFS_ERR_INVAL;
918-
}
900+
if(fd < 0) {
901+
errno = -fd;
902+
sem_give(efs);
903+
ESP_LOGV(TAG, "Error obtaining FD");
904+
return LFS_ERR_INVAL;
905+
}
919906

920907
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
921-
/* Create all parent directories (if necessary) */
922-
ESP_LOGV(TAG, "LITTLEFS_SPIFFS_COMPAT attempting to create all directories for %s", path);
923-
mkdirs(efs, path);
908+
/* Create all parent directories (if necessary) */
909+
ESP_LOGV(TAG, "LITTLEFS_SPIFFS_COMPAT attempting to create all directories for %s", path);
910+
mkdirs(efs, path);
924911
#endif // CONFIG_LITTLEFS_SPIFFS_COMPAT
925912

926-
/* Open File */
927-
res = lfs_file_open(efs->fs, &file->file, path, lfs_flags);
913+
/* Open File */
914+
res = lfs_file_open(efs->fs, &file->file, path, lfs_flags);
928915

929-
if( res < 0 ) {
930-
errno = -res;
931-
esp_littlefs_free_fd(efs, fd);
932-
sem_give(efs);
916+
if( res < 0 ) {
917+
errno = -res;
918+
esp_littlefs_free_fd(efs, fd);
919+
sem_give(efs);
933920
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
934-
ESP_LOGV(TAG, "Failed to open file %s. Error %s (%d)",
935-
path, esp_littlefs_errno(res), res);
921+
ESP_LOGV(TAG, "Failed to open file %s. Error %s (%d)",
922+
path, esp_littlefs_errno(res), res);
936923
#else
937-
ESP_LOGV(TAG, "Failed to open file. Error %s (%d)",
938-
esp_littlefs_errno(res), res);
924+
ESP_LOGV(TAG, "Failed to open file. Error %s (%d)",
925+
esp_littlefs_errno(res), res);
939926
#endif
940-
return LFS_ERR_INVAL;
941-
}
927+
return LFS_ERR_INVAL;
928+
}
942929

943-
file->hash = compute_hash(path);
930+
file->hash = compute_hash(path);
944931
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
945-
memcpy(file->path, path, path_len);
932+
memcpy(file->path, path, path_len);
946933
#endif
947-
}
948934

949935
#if CONFIG_LITTLEFS_USE_MTIME
950936
if (lfs_flags != LFS_O_RDONLY) {
@@ -1019,159 +1005,33 @@ static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size) {
10191005
return res;
10201006
}
10211007

1022-
#if 0 //disabled
1023-
1024-
static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset)
1025-
{
1026-
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
1027-
ssize_t res, save_res;
1028-
vfs_littlefs_file_t *file = NULL;
1029-
1030-
sem_take(efs);
1031-
if ((uint32_t)fd > efs->cache_size)
1032-
{
1033-
sem_give(efs);
1034-
ESP_LOGE(TAG, "FD %d must be <%d.", fd, efs->cache_size);
1035-
return LFS_ERR_BADF;
1036-
}
1037-
file = efs->cache[fd];
1038-
1039-
off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
1040-
if (old_offset < (off_t)0)
1041-
{
1042-
res = old_offset;
1043-
goto exit;
1044-
}
1045-
1046-
/* Set to wanted position. */
1047-
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
1048-
if (res < (off_t)0)
1049-
goto exit;
1050-
1051-
/* Write out the data. */
1052-
res = lfs_file_write(efs->fs, &file->file, src, size);
1053-
1054-
/* Now we have to restore the position. If this fails we have to
1055-
return this as an error. But if the writing also failed we
1056-
return writing error. */
1057-
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
1058-
if (res >= (ssize_t)0 && save_res < (off_t)0)
1059-
{
1060-
res = save_res;
1061-
}
1062-
sem_give(efs);
1063-
1064-
exit:
1065-
if (res < 0)
1066-
{
1067-
errno = -res;
1068-
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
1069-
ESP_LOGV(TAG, "Failed to write FD %d; path \"%s\". Error %s (%d)",
1070-
fd, file->path, esp_littlefs_errno(res), res);
1071-
#else
1072-
ESP_LOGV(TAG, "Failed to write FD %d. Error %s (%d)",
1073-
fd, esp_littlefs_errno(res), res);
1074-
#endif
1075-
return -1;
1076-
}
1077-
1078-
return res;
1079-
}
1080-
1081-
static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset)
1082-
{
1083-
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
1084-
ssize_t res, save_res;
1085-
vfs_littlefs_file_t *file = NULL;
1086-
1087-
sem_take(efs);
1088-
if ((uint32_t)fd > efs->cache_size)
1089-
{
1090-
sem_give(efs);
1091-
ESP_LOGE(TAG, "FD %d must be <%d.", fd, efs->cache_size);
1092-
return LFS_ERR_BADF;
1093-
}
1094-
file = efs->cache[fd];
1095-
1096-
off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
1097-
if (old_offset < (off_t)0)
1098-
{
1099-
res = old_offset;
1100-
goto exit;
1101-
}
1102-
1103-
/* Set to wanted position. */
1104-
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
1105-
if (res < (off_t)0)
1106-
goto exit;
1107-
1108-
/* Read the data. */
1109-
res = lfs_file_read(efs->fs, &file->file, dst, size);
1110-
1111-
/* Now we have to restore the position. If this fails we have to
1112-
return this as an error. But if the reading also failed we
1113-
return reading error. */
1114-
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
1115-
if (res >= (ssize_t)0 && save_res < (off_t)0)
1116-
{
1117-
res = save_res;
1118-
}
1119-
sem_give(efs);
1120-
1121-
exit:
1122-
if (res < 0)
1123-
{
1124-
errno = -res;
1125-
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
1126-
ESP_LOGV(TAG, "Failed to read file \"%s\". Error %s (%d)",
1127-
file->path, esp_littlefs_errno(res), res);
1128-
#else
1129-
ESP_LOGV(TAG, "Failed to read FD %d. Error %s (%d)",
1130-
fd, esp_littlefs_errno(res), res);
1131-
#endif
1132-
return -1;
1133-
}
1134-
1135-
return res;
1136-
}
1137-
1138-
#endif //disabled
1139-
11401008
static int vfs_littlefs_close(void* ctx, int fd) {
11411009
// TODO update mtime on close? SPIFFS doesn't do this
11421010
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
1143-
int res = ESP_OK;
1011+
int res;
11441012
vfs_littlefs_file_t *file = NULL;
11451013

11461014
sem_take(efs);
1147-
11481015
if((uint32_t)fd > efs->cache_size) {
11491016
sem_give(efs);
11501017
ESP_LOGE(TAG, "FD %d must be <%d.", fd, efs->cache_size);
11511018
return LFS_ERR_BADF;
11521019
}
11531020
file = efs->cache[fd];
1154-
assert(file->open_count > 0);
1155-
file->open_count--;
1156-
1157-
if(file->open_count == 0) {
1158-
/* Actually close the file and release the descriptor */
1159-
res = lfs_file_close(efs->fs, &file->file);
1160-
if(res < 0){
1161-
errno = -res;
1162-
sem_give(efs);
1021+
res = lfs_file_close(efs->fs, &file->file);
1022+
if(res < 0){
1023+
errno = -res;
1024+
sem_give(efs);
11631025
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
1164-
ESP_LOGV(TAG, "Failed to close file \"%s\". Error %s (%d)",
1165-
file->path, esp_littlefs_errno(res), res);
1026+
ESP_LOGV(TAG, "Failed to close file \"%s\". Error %s (%d)",
1027+
file->path, esp_littlefs_errno(res), res);
11661028
#else
1167-
ESP_LOGV(TAG, "Failed to close Fd %d. Error %s (%d)",
1168-
fd, esp_littlefs_errno(res), res);
1029+
ESP_LOGV(TAG, "Failed to close Fd %d. Error %s (%d)",
1030+
fd, esp_littlefs_errno(res), res);
11691031
#endif
1170-
return res;
1171-
}
1172-
esp_littlefs_free_fd(efs, fd);
1032+
return res;
11731033
}
1174-
1034+
esp_littlefs_free_fd(efs, fd);
11751035
sem_give(efs);
11761036
return res;
11771037
}
@@ -1337,13 +1197,14 @@ static int vfs_littlefs_unlink(void* ctx, const char *path) {
13371197
ESP_LOGE(TAG, fail_str_1 " Has open FD.", path);
13381198
return -1;
13391199
}
1340-
1341-
//if (info.type == LFS_TYPE_DIR) {
1342-
// sem_give(efs);
1343-
// ESP_LOGV(TAG, "Cannot unlink a directory.");
1344-
// return LFS_ERR_ISDIR;
1345-
//}
1346-
1200+
/* commented for old core revisions compatibility */
1201+
/*
1202+
if (info.type == LFS_TYPE_DIR) {
1203+
sem_give(efs);
1204+
ESP_LOGV(TAG, "Cannot unlink a directory.");
1205+
return LFS_ERR_ISDIR;
1206+
}
1207+
*/
13471208
res = lfs_remove(efs->fs, path);
13481209
if (res < 0) {
13491210
errno = -res;

src/littlefs_api.h

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ typedef struct _vfs_littlefs_file_t {
3434
lfs_file_t file;
3535
uint32_t hash;
3636
struct _vfs_littlefs_file_t * next; /*!< Pointer to next file in Singly Linked List */
37-
uint8_t open_count; /*!< Number of times this file has been opened */
3837
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
3938
char * path;
4039
#endif

0 commit comments

Comments
 (0)