@@ -44,6 +44,7 @@ Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config* cfg)
4444 varclr (&_lfs);
4545 _lfs_cfg = cfg;
4646 _mounted = false ;
47+ _mutex = xSemaphoreCreateMutexStatic (&this ->_MutexStorageSpace );
4748}
4849
4950Adafruit_LittleFS::~Adafruit_LittleFS ()
@@ -56,105 +57,157 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
5657// User should format the disk and try again
5758bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
5859{
59- if ( _mounted ) return true ;
60-
61- if (cfg) _lfs_cfg = cfg;
62- if (!_lfs_cfg) return false ;
63-
64- VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
65- _mounted = true ;
66-
67- return true ;
60+ xSemaphoreTake (_mutex, portMAX_DELAY);
61+ bool ret;
62+ // not a loop, just an quick way to short-circuit on error
63+ do {
64+ if (_mounted) { ret = true ; break ; }
65+ if (cfg) { _lfs_cfg = cfg; }
66+ if (nullptr == _lfs_cfg) { ret = false ; break ; }
67+ // actually attempt to mount, and log error if one occurs
68+ int err = lfs_mount (&_lfs, _lfs_cfg);
69+ PRINT_LFS_ERR (err);
70+ _mounted = (err == LFS_ERR_OK);
71+ ret = _mounted;
72+ } while (0 );
73+ xSemaphoreGive (_mutex);
74+ return ret;
6875}
6976
7077// Tear down and unmount file system
7178void Adafruit_LittleFS::end (void )
7279{
73- if (!_mounted) return ;
74-
75- _mounted = false ;
76- VERIFY_LFS (lfs_unmount (&_lfs), );
80+ xSemaphoreTake (_mutex, portMAX_DELAY);
81+ if (_mounted)
82+ {
83+ _mounted = false ;
84+ int err = lfs_unmount (&_lfs);
85+ PRINT_LFS_ERR (err);
86+ (void )err;
87+ }
88+ xSemaphoreGive (_mutex);
7789}
7890
7991bool Adafruit_LittleFS::format (void )
8092{
81- // if already mounted: umount -> format -> remount
82- if (_mounted) VERIFY_LFS (lfs_unmount (&_lfs), false );
83-
84- VERIFY_LFS (lfs_format (&_lfs, _lfs_cfg), false );
85-
86- if (_mounted) VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
93+ xSemaphoreTake (_mutex, portMAX_DELAY);
94+ int err = LFS_ERR_OK;
95+ bool attemptMount = _mounted;
96+ // not a loop, just an quick way to short-circuit on error
97+ do
98+ {
99+ // if already mounted: umount first -> format -> remount
100+ if (_mounted)
101+ {
102+ _mounted = false ;
103+ err = lfs_unmount (&_lfs);
104+ if ( LFS_ERR_OK != err) { PRINT_LFS_ERR (err); break ; }
105+ }
106+ err = lfs_format (&_lfs, _lfs_cfg);
107+ if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR (err); break ; }
87108
88- return true ;
109+ if (attemptMount)
110+ {
111+ err = lfs_mount (&_lfs, _lfs_cfg);
112+ if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR (err); break ; }
113+ _mounted = true ;
114+ }
115+ // success!
116+ } while (0 );
117+ xSemaphoreGive (_mutex);
118+ return LFS_ERR_OK == err;
89119}
90120
91121// Open a file or folder
92122Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
93123{
124+ // No lock is required here ... the File() object will synchronize with the mutex provided
94125 return Adafruit_LittleFS_Namespace::File (filepath, mode, *this );
95126}
96127
97128// Check if file or folder exists
98129bool Adafruit_LittleFS::exists (char const *filepath)
99130{
100131 struct lfs_info info;
101- return 0 == lfs_stat (&_lfs, filepath, &info);
132+ xSemaphoreTake (_mutex, portMAX_DELAY);
133+ bool ret = (0 == lfs_stat (&_lfs, filepath, &info));
134+ xSemaphoreGive (_mutex);
135+ return ret;
102136}
103137
138+
104139// Create a directory, create intermediate parent if needed
105140bool Adafruit_LittleFS::mkdir (char const *filepath)
106141{
142+ bool ret = true ;
107143 const char * slash = filepath;
108144 if ( slash[0 ] == ' /' ) slash++; // skip root '/'
109145
146+ xSemaphoreTake (_mutex, portMAX_DELAY);
147+
148+ // make intermediate parent directory(ies)
110149 while ( NULL != (slash = strchr (slash, ' /' )) )
111150 {
112151 char parent[slash - filepath + 1 ] = { 0 };
113152 memcpy (parent, filepath, slash - filepath);
114153
115- // make intermediate parent
116154 int rc = lfs_mkdir (&_lfs, parent);
117155 if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
118156 {
119157 PRINT_LFS_ERR (rc);
120- return false ;
158+ ret = false ;
159+ break ;
121160 }
122-
123161 slash++;
124162 }
125-
126- int rc = lfs_mkdir (&_lfs, filepath);
127- if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
163+ // make the final requested directory
164+ if (ret)
128165 {
129- PRINT_LFS_ERR (rc);
130- return false ;
166+ int rc = lfs_mkdir (&_lfs, filepath);
167+ if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
168+ {
169+ PRINT_LFS_ERR (rc);
170+ ret = false ;
171+ }
131172 }
132-
133- return true ;
173+ xSemaphoreGive (_mutex);
174+ return ret ;
134175}
135176
136177// Remove a file
137178bool Adafruit_LittleFS::remove (char const *filepath)
138179{
139- VERIFY_LFS (lfs_remove (&_lfs, filepath), false );
140- return true ;
180+ xSemaphoreTake (_mutex, portMAX_DELAY);
181+ int err = lfs_remove (&_lfs, filepath);
182+ PRINT_LFS_ERR (err);
183+ xSemaphoreGive (_mutex);
184+ return LFS_ERR_OK == err;
141185}
142186
143187// Remove a folder
144188bool Adafruit_LittleFS::rmdir (char const *filepath)
145189{
146- VERIFY_LFS (lfs_remove (&_lfs, filepath));
147- return true ;
190+ xSemaphoreTake (_mutex, portMAX_DELAY);
191+ int err = lfs_remove (&_lfs, filepath);
192+ PRINT_LFS_ERR (err);
193+ xSemaphoreGive (_mutex);
194+ return LFS_ERR_OK == err;
148195}
149196
150197// Remove a folder recursively
151198bool Adafruit_LittleFS::rmdir_r (char const *filepath)
152199{
153200 /* adafruit: lfs is modified to remove non-empty folder,
154201 According to below issue, comment these 2 line won't corrupt filesystem
155- https://github.com/ARMmbed/littlefs/issues/43 */
156- VERIFY_LFS (lfs_remove (&_lfs, filepath));
157- return true ;
202+ at least when using LFS v1. If moving to LFS v2, see tracked issue
203+ to see if issues (such as the orphans in threaded linked list) are resolved.
204+ https://github.com/ARMmbed/littlefs/issues/43
205+ */
206+ xSemaphoreTake (_mutex, portMAX_DELAY);
207+ int err = lfs_remove (&_lfs, filepath);
208+ PRINT_LFS_ERR (err);
209+ xSemaphoreGive (_mutex);
210+ return LFS_ERR_OK == err;
158211}
159212
160213// ------------- Debug -------------//
0 commit comments