-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathAdafruit_LittleFS.cpp
297 lines (244 loc) · 7.19 KB
/
Adafruit_LittleFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Arduino.h>
#include <string.h>
#include "Adafruit_LittleFS.h"
#include <Adafruit_TinyUSB.h> // for Serial
using namespace Adafruit_LittleFS_Namespace;
//--------------------------------------------------------------------+
// Implementation
//--------------------------------------------------------------------+
Adafruit_LittleFS::Adafruit_LittleFS (void)
: Adafruit_LittleFS(NULL)
{
}
Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config* cfg)
{
varclr(&_lfs);
_lfs_cfg = cfg;
_mounted = false;
_mutex = xSemaphoreCreateMutexStatic(&this->_MutexStorageSpace);
}
Adafruit_LittleFS::~Adafruit_LittleFS ()
{
}
// Initialize and mount the file system
// Return true if mounted successfully else probably corrupted.
// User should format the disk and try again
bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
{
_lockFS();
bool ret;
// not a loop, just an quick way to short-circuit on error
do {
if (_mounted) { ret = true; break; }
if (cfg) { _lfs_cfg = cfg; }
if (nullptr == _lfs_cfg) { ret = false; break; }
// actually attempt to mount, and log error if one occurs
int err = lfs_mount(&_lfs, _lfs_cfg);
PRINT_LFS_ERR(err);
_mounted = (err == LFS_ERR_OK);
ret = _mounted;
} while(0);
_unlockFS();
return ret;
}
// Tear down and unmount file system
void Adafruit_LittleFS::end(void)
{
_lockFS();
if (_mounted)
{
_mounted = false;
int err = lfs_unmount(&_lfs);
PRINT_LFS_ERR(err);
(void)err;
}
_unlockFS();
}
bool Adafruit_LittleFS::format (void)
{
_lockFS();
int err = LFS_ERR_OK;
bool attemptMount = _mounted;
// not a loop, just an quick way to short-circuit on error
do
{
// if already mounted: umount first -> format -> remount
if (_mounted)
{
_mounted = false;
err = lfs_unmount(&_lfs);
if ( LFS_ERR_OK != err) { PRINT_LFS_ERR(err); break; }
}
err = lfs_format(&_lfs, _lfs_cfg);
if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR(err); break; }
if (attemptMount)
{
err = lfs_mount(&_lfs, _lfs_cfg);
if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR(err); break; }
_mounted = true;
}
// success!
} while(0);
_unlockFS();
return LFS_ERR_OK == err;
}
// Open a file or folder
Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
{
// No lock is required here ... the File() object will synchronize with the mutex provided
return Adafruit_LittleFS_Namespace::File(filepath, mode, *this);
}
// Check if file or folder exists
bool Adafruit_LittleFS::exists (char const *filepath)
{
struct lfs_info info;
_lockFS();
bool ret = (0 == lfs_stat(&_lfs, filepath, &info));
_unlockFS();
return ret;
}
// Create a directory, create intermediate parent if needed
bool Adafruit_LittleFS::mkdir (char const *filepath)
{
bool ret = true;
const char* slash = filepath;
if ( slash[0] == '/' ) slash++; // skip root '/'
_lockFS();
// make intermediate parent directory(ies)
while ( NULL != (slash = strchr(slash, '/')) )
{
char parent[slash - filepath + 1] = { 0 };
memcpy(parent, filepath, slash - filepath);
int rc = lfs_mkdir(&_lfs, parent);
if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
{
PRINT_LFS_ERR(rc);
ret = false;
break;
}
slash++;
}
// make the final requested directory
if (ret)
{
int rc = lfs_mkdir(&_lfs, filepath);
if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
{
PRINT_LFS_ERR(rc);
ret = false;
}
}
_unlockFS();
return ret;
}
// Remove a file
bool Adafruit_LittleFS::remove (char const *filepath)
{
_lockFS();
int err = lfs_remove(&_lfs, filepath);
PRINT_LFS_ERR(err);
_unlockFS();
return LFS_ERR_OK == err;
}
// Rename a file
bool Adafruit_LittleFS::rename (char const *oldfilepath, char const *newfilepath)
{
_lockFS();
int err = lfs_rename(&_lfs, oldfilepath, newfilepath);
PRINT_LFS_ERR(err);
_unlockFS();
return LFS_ERR_OK == err;
}
// Remove a folder
bool Adafruit_LittleFS::rmdir (char const *filepath)
{
_lockFS();
int err = lfs_remove(&_lfs, filepath);
PRINT_LFS_ERR(err);
_unlockFS();
return LFS_ERR_OK == err;
}
// Remove a folder recursively
bool Adafruit_LittleFS::rmdir_r (char const *filepath)
{
/* adafruit: lfs is modified to remove non-empty folder,
According to below issue, comment these 2 line won't corrupt filesystem
at least when using LFS v1. If moving to LFS v2, see tracked issue
to see if issues (such as the orphans in threaded linked list) are resolved.
https://github.com/ARMmbed/littlefs/issues/43
*/
_lockFS();
int err = lfs_remove(&_lfs, filepath);
PRINT_LFS_ERR(err);
_unlockFS();
return LFS_ERR_OK == err;
}
static int _lfs_count(void *p, lfs_block_t block) {
*(size_t *)p += 1;
return 0;
}
size_t Adafruit_LittleFS::usedBlocks() {
_lockFS();
size_t blocks = 0;
lfs_traverse(&_lfs, _lfs_count, &blocks);
_unlockFS();
return blocks;
}
size_t Adafruit_LittleFS::usedBytes() {
if (nullptr == _lfs_cfg) return 0;
const size_t blocks_used = usedBlocks();
return _lfs_cfg->block_size * blocks_used;
}
size_t Adafruit_LittleFS::totalBytes() {
if (nullptr == _lfs_cfg) return 0;
return _lfs_cfg->block_size * _lfs_cfg->block_count;
}
//------------- Debug -------------//
#if CFG_DEBUG
const char* dbg_strerr_lfs (int32_t err)
{
switch ( err )
{
case LFS_ERR_OK : return "LFS_ERR_OK";
case LFS_ERR_IO : return "LFS_ERR_IO";
case LFS_ERR_CORRUPT : return "LFS_ERR_CORRUPT";
case LFS_ERR_NOENT : return "LFS_ERR_NOENT";
case LFS_ERR_EXIST : return "LFS_ERR_EXIST";
case LFS_ERR_NOTDIR : return "LFS_ERR_NOTDIR";
case LFS_ERR_ISDIR : return "LFS_ERR_ISDIR";
case LFS_ERR_NOTEMPTY : return "LFS_ERR_NOTEMPTY";
case LFS_ERR_BADF : return "LFS_ERR_BADF";
case LFS_ERR_INVAL : return "LFS_ERR_INVAL";
case LFS_ERR_NOSPC : return "LFS_ERR_NOSPC";
case LFS_ERR_NOMEM : return "LFS_ERR_NOMEM";
default:
static char errcode[10];
sprintf(errcode, "%ld", err);
return errcode;
}
return NULL;
}
#endif