@@ -57,7 +57,8 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
5757// User should format the disk and try again
5858bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
5959{
60- xSemaphoreTake (_mutex, portMAX_DELAY);
60+ _lockFS ();
61+
6162 bool ret;
6263 // not a loop, just an quick way to short-circuit on error
6364 do {
@@ -70,27 +71,31 @@ bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
7071 _mounted = (err == LFS_ERR_OK);
7172 ret = _mounted;
7273 } while (0 );
73- xSemaphoreGive (_mutex);
74+
75+ _unlockFS ();
7476 return ret;
7577}
7678
7779// Tear down and unmount file system
7880void Adafruit_LittleFS::end (void )
7981{
80- xSemaphoreTake (_mutex, portMAX_DELAY);
82+ _lockFS ();
83+
8184 if (_mounted)
8285 {
8386 _mounted = false ;
8487 int err = lfs_unmount (&_lfs);
8588 PRINT_LFS_ERR (err);
8689 (void )err;
8790 }
88- xSemaphoreGive (_mutex);
91+
92+ _unlockFS ();
8993}
9094
9195bool Adafruit_LittleFS::format (void )
9296{
93- xSemaphoreTake (_mutex, portMAX_DELAY);
97+ _lockFS ();
98+
9499 int err = LFS_ERR_OK;
95100 bool attemptMount = _mounted;
96101 // not a loop, just an quick way to short-circuit on error
@@ -114,7 +119,8 @@ bool Adafruit_LittleFS::format (void)
114119 }
115120 // success!
116121 } while (0 );
117- xSemaphoreGive (_mutex);
122+
123+ _unlockFS ();
118124 return LFS_ERR_OK == err;
119125}
120126
@@ -129,9 +135,11 @@ Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath,
129135bool Adafruit_LittleFS::exists (char const *filepath)
130136{
131137 struct lfs_info info;
132- xSemaphoreTake (_mutex, portMAX_DELAY);
138+ _lockFS ();
139+
133140 bool ret = (0 == lfs_stat (&_lfs, filepath, &info));
134- xSemaphoreGive (_mutex);
141+
142+ _unlockFS ();
135143 return ret;
136144}
137145
@@ -143,7 +151,7 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
143151 const char * slash = filepath;
144152 if ( slash[0 ] == ' /' ) slash++; // skip root '/'
145153
146- xSemaphoreTake (_mutex, portMAX_DELAY );
154+ _lockFS ( );
147155
148156 // make intermediate parent directory(ies)
149157 while ( NULL != (slash = strchr (slash, ' /' )) )
@@ -170,27 +178,32 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
170178 ret = false ;
171179 }
172180 }
173- xSemaphoreGive (_mutex);
181+
182+ _unlockFS ();
174183 return ret;
175184}
176185
177186// Remove a file
178187bool Adafruit_LittleFS::remove (char const *filepath)
179188{
180- xSemaphoreTake (_mutex, portMAX_DELAY);
189+ _lockFS ();
190+
181191 int err = lfs_remove (&_lfs, filepath);
182192 PRINT_LFS_ERR (err);
183- xSemaphoreGive (_mutex);
193+
194+ _unlockFS ();
184195 return LFS_ERR_OK == err;
185196}
186197
187198// Remove a folder
188199bool Adafruit_LittleFS::rmdir (char const *filepath)
189200{
190- xSemaphoreTake (_mutex, portMAX_DELAY);
201+ _lockFS ();
202+
191203 int err = lfs_remove (&_lfs, filepath);
192204 PRINT_LFS_ERR (err);
193- xSemaphoreGive (_mutex);
205+
206+ _unlockFS ();
194207 return LFS_ERR_OK == err;
195208}
196209
@@ -203,10 +216,12 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
203216 to see if issues (such as the orphans in threaded linked list) are resolved.
204217 https://github.com/ARMmbed/littlefs/issues/43
205218 */
206- xSemaphoreTake (_mutex, portMAX_DELAY);
219+ _lockFS ();
220+
207221 int err = lfs_remove (&_lfs, filepath);
208222 PRINT_LFS_ERR (err);
209- xSemaphoreGive (_mutex);
223+
224+ _unlockFS ();
210225 return LFS_ERR_OK == err;
211226}
212227
0 commit comments