12
12
*/
13
13
14
14
//#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 */
16
18
#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
17
34
18
35
#include "esp_log.h"
19
36
#include "esp_spi_flash.h"
@@ -48,24 +65,6 @@ static const char TAG[] = "esp_littlefs";
48
65
#define CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE 4 /* Minimum size of FD cache */
49
66
#define CONFIG_LITTLEFS_FD_CACHE_HYST 4 /* When shrinking, leave this many trailing FD slots available */
50
67
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
-
69
68
/**
70
69
* @brief littlefs DIR structure
71
70
*/
@@ -80,8 +79,6 @@ typedef struct {
80
79
static int vfs_littlefs_open (void * ctx , const char * path , int flags , int mode );
81
80
static ssize_t vfs_littlefs_write (void * ctx , int fd , const void * data , size_t size );
82
81
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);
85
82
static int vfs_littlefs_close (void * ctx , int fd );
86
83
static off_t vfs_littlefs_lseek (void * ctx , int fd , off_t offset , int mode );
87
84
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)
179
176
const esp_vfs_t vfs = {
180
177
.flags = ESP_VFS_FLAG_CONTEXT_PTR ,
181
178
.write_p = & vfs_littlefs_write ,
182
- // .pwrite_p = &vfs_littlefs_pwrite,
183
179
.lseek_p = & vfs_littlefs_lseek ,
184
180
.read_p = & vfs_littlefs_read ,
185
- // .pread_p = &vfs_littlefs_pread,
186
181
.open_p = & vfs_littlefs_open ,
187
182
.close_p = & vfs_littlefs_close ,
188
183
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
@@ -431,7 +426,7 @@ static esp_err_t esp_littlefs_by_label(const char* label, int * index){
431
426
}
432
427
}
433
428
434
- ESP_LOGV (TAG , "Existing filesystem \%s\" not found" , label );
429
+ ESP_LOGV (TAG , "Existing filesystem \" %s\" not found" , label );
435
430
return ESP_ERR_NOT_FOUND ;
436
431
}
437
432
@@ -740,8 +735,6 @@ static int esp_littlefs_allocate_fd(esp_littlefs_t *efs, vfs_littlefs_file_t **
740
735
*/
741
736
(* file )-> path = (char * )(* file ) + sizeof (* * file );
742
737
#endif
743
-
744
- (* file )-> open_count = 1 ;
745
738
746
739
/* Now find a free place in cache */
747
740
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)
898
891
/* Get a FD */
899
892
sem_take (efs );
900
893
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
908
895
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
909
- , path_len
896
+ , path_len
910
897
#endif
911
- );
898
+ );
912
899
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
+ }
919
906
920
907
#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 );
924
911
#endif // CONFIG_LITTLEFS_SPIFFS_COMPAT
925
912
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 );
928
915
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 );
933
920
#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 );
936
923
#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 );
939
926
#endif
940
- return LFS_ERR_INVAL ;
941
- }
927
+ return LFS_ERR_INVAL ;
928
+ }
942
929
943
- file -> hash = compute_hash (path );
930
+ file -> hash = compute_hash (path );
944
931
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
945
- memcpy (file -> path , path , path_len );
932
+ memcpy (file -> path , path , path_len );
946
933
#endif
947
- }
948
934
949
935
#if CONFIG_LITTLEFS_USE_MTIME
950
936
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) {
1019
1005
return res ;
1020
1006
}
1021
1007
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
-
1140
1008
static int vfs_littlefs_close (void * ctx , int fd ) {
1141
1009
// TODO update mtime on close? SPIFFS doesn't do this
1142
1010
esp_littlefs_t * efs = (esp_littlefs_t * )ctx ;
1143
- int res = ESP_OK ;
1011
+ int res ;
1144
1012
vfs_littlefs_file_t * file = NULL ;
1145
1013
1146
1014
sem_take (efs );
1147
-
1148
1015
if ((uint32_t )fd > efs -> cache_size ) {
1149
1016
sem_give (efs );
1150
1017
ESP_LOGE (TAG , "FD %d must be <%d." , fd , efs -> cache_size );
1151
1018
return LFS_ERR_BADF ;
1152
1019
}
1153
1020
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 );
1163
1025
#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 );
1166
1028
#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 );
1169
1031
#endif
1170
- return res ;
1171
- }
1172
- esp_littlefs_free_fd (efs , fd );
1032
+ return res ;
1173
1033
}
1174
-
1034
+ esp_littlefs_free_fd ( efs , fd );
1175
1035
sem_give (efs );
1176
1036
return res ;
1177
1037
}
@@ -1337,13 +1197,14 @@ static int vfs_littlefs_unlink(void* ctx, const char *path) {
1337
1197
ESP_LOGE (TAG , fail_str_1 " Has open FD." , path );
1338
1198
return -1 ;
1339
1199
}
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
+ */
1347
1208
res = lfs_remove (efs -> fs , path );
1348
1209
if (res < 0 ) {
1349
1210
errno = - res ;
0 commit comments