1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2006-2012 ARM Limited
2
+ * Copyright (c) 2017 ARM Limited
3
3
*
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
10
7
*
11
- * The above copyright notice and this permission notice shall be included in
12
- * all copies or substantial portions of the Software.
8
+ * http://www.apache.org/licenses/LICENSE-2.0
13
9
*
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
21
15
*/
22
16
#include " mbed.h"
23
17
#include " LittleFileSystem.h"
@@ -29,7 +23,8 @@ extern "C" {
29
23
30
24
31
25
// //// Conversion functions //////
32
- static int lfs_toerror (int err) {
26
+ static int lfs_toerror (int err)
27
+ {
33
28
switch (err) {
34
29
case LFS_ERR_OK: return 0 ;
35
30
case LFS_ERR_IO: return -EIO;
@@ -44,7 +39,8 @@ static int lfs_toerror(int err) {
44
39
}
45
40
}
46
41
47
- static int lfs_fromflags (int flags) {
42
+ static int lfs_fromflags (int flags)
43
+ {
48
44
return (
49
45
(((flags & 3 ) == O_RDONLY) ? LFS_O_RDONLY : 0 ) |
50
46
(((flags & 3 ) == O_WRONLY) ? LFS_O_WRONLY : 0 ) |
@@ -55,7 +51,8 @@ static int lfs_fromflags(int flags) {
55
51
((flags & O_APPEND) ? LFS_O_APPEND : 0 ));
56
52
}
57
53
58
- static int lfs_fromwhence (int whence) {
54
+ static int lfs_fromwhence (int whence)
55
+ {
59
56
switch (whence) {
60
57
case SEEK_SET: return LFS_SEEK_SET;
61
58
case SEEK_CUR: return LFS_SEEK_CUR;
@@ -64,7 +61,8 @@ static int lfs_fromwhence(int whence) {
64
61
}
65
62
}
66
63
67
- static int lfs_tomode (int type) {
64
+ static int lfs_tomode (int type)
65
+ {
68
66
int mode = S_IRWXU | S_IRWXG | S_IRWXO;
69
67
switch (type) {
70
68
case LFS_TYPE_DIR: return mode | S_IFDIR;
@@ -73,7 +71,8 @@ static int lfs_tomode(int type) {
73
71
}
74
72
}
75
73
76
- static int lfs_totype (int type) {
74
+ static int lfs_totype (int type)
75
+ {
77
76
switch (type) {
78
77
case LFS_TYPE_DIR: return DT_DIR;
79
78
case LFS_TYPE_REG: return DT_REG;
@@ -95,12 +94,14 @@ static int lfs_bd_prog(const struct lfs_config *c, lfs_block_t block,
95
94
return bd->program (buffer, block*c->block_size + off, size);
96
95
}
97
96
98
- static int lfs_bd_erase (const struct lfs_config *c, lfs_block_t block) {
97
+ static int lfs_bd_erase (const struct lfs_config *c, lfs_block_t block)
98
+ {
99
99
BlockDevice *bd = (BlockDevice *)c->context ;
100
100
return bd->erase (block*c->block_size , c->block_size );
101
101
}
102
102
103
- static int lfs_bd_sync (const struct lfs_config *c) {
103
+ static int lfs_bd_sync (const struct lfs_config *c)
104
+ {
104
105
return 0 ;
105
106
}
106
107
@@ -126,7 +127,8 @@ LittleFileSystem::~LittleFileSystem() {
126
127
unmount ();
127
128
}
128
129
129
- int LittleFileSystem::mount (BlockDevice *bd) {
130
+ int LittleFileSystem::mount (BlockDevice *bd)
131
+ {
130
132
_mutex.lock ();
131
133
LFS_INFO (" mount(%p)" , bd);
132
134
_bd = bd;
@@ -167,7 +169,8 @@ int LittleFileSystem::mount(BlockDevice *bd) {
167
169
return lfs_toerror (err);
168
170
}
169
171
170
- int LittleFileSystem::unmount () {
172
+ int LittleFileSystem::unmount ()
173
+ {
171
174
_mutex.lock ();
172
175
LFS_INFO (" unmount(%s)" , " " );
173
176
if (_bd) {
@@ -247,7 +250,8 @@ int LittleFileSystem::format(BlockDevice *bd,
247
250
return 0 ;
248
251
}
249
252
250
- int LittleFileSystem::reformat (BlockDevice *bd) {
253
+ int LittleFileSystem::reformat (BlockDevice *bd)
254
+ {
251
255
_mutex.lock ();
252
256
LFS_INFO (" reformat(%p)" , bd);
253
257
if (_bd) {
@@ -289,7 +293,8 @@ int LittleFileSystem::reformat(BlockDevice *bd) {
289
293
return 0 ;
290
294
}
291
295
292
- int LittleFileSystem::remove (const char *filename) {
296
+ int LittleFileSystem::remove (const char *filename)
297
+ {
293
298
_mutex.lock ();
294
299
LFS_INFO (" remove(\" %s\" )" , filename);
295
300
int err = lfs_remove (&_lfs, filename);
@@ -298,7 +303,8 @@ int LittleFileSystem::remove(const char *filename) {
298
303
return lfs_toerror (err);
299
304
}
300
305
301
- int LittleFileSystem::rename (const char *oldname, const char *newname) {
306
+ int LittleFileSystem::rename (const char *oldname, const char *newname)
307
+ {
302
308
_mutex.lock ();
303
309
LFS_INFO (" rename(\" %s\" , \" %s\" )" , oldname, newname);
304
310
int err = lfs_rename (&_lfs, oldname, newname);
@@ -307,7 +313,8 @@ int LittleFileSystem::rename(const char *oldname, const char *newname) {
307
313
return lfs_toerror (err);
308
314
}
309
315
310
- int LittleFileSystem::mkdir (const char *name, mode_t mode) {
316
+ int LittleFileSystem::mkdir (const char *name, mode_t mode)
317
+ {
311
318
_mutex.lock ();
312
319
LFS_INFO (" mkdir(\" %s\" , 0x%lx)" , name, mode);
313
320
int err = lfs_mkdir (&_lfs, name);
@@ -316,7 +323,8 @@ int LittleFileSystem::mkdir(const char *name, mode_t mode) {
316
323
return lfs_toerror (err);
317
324
}
318
325
319
- int LittleFileSystem::stat (const char *name, struct stat *st) {
326
+ int LittleFileSystem::stat (const char *name, struct stat *st)
327
+ {
320
328
struct lfs_info info;
321
329
_mutex.lock ();
322
330
LFS_INFO (" stat(\" %s\" , %p)" , name, st);
@@ -330,7 +338,8 @@ int LittleFileSystem::stat(const char *name, struct stat *st) {
330
338
331
339
332
340
// //// File operations //////
333
- int LittleFileSystem::file_open (fs_file_t *file, const char *path, int flags) {
341
+ int LittleFileSystem::file_open (fs_file_t *file, const char *path, int flags)
342
+ {
334
343
lfs_file_t *f = new lfs_file_t ;
335
344
_mutex.lock ();
336
345
LFS_INFO (" file_open(%p, \" %s\" , 0x%x)" , *file, path, flags);
@@ -345,7 +354,8 @@ int LittleFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
345
354
return lfs_toerror (err);
346
355
}
347
356
348
- int LittleFileSystem::file_close (fs_file_t file) {
357
+ int LittleFileSystem::file_close (fs_file_t file)
358
+ {
349
359
lfs_file_t *f = (lfs_file_t *)file;
350
360
_mutex.lock ();
351
361
LFS_INFO (" file_close(%p)" , file);
@@ -356,7 +366,8 @@ int LittleFileSystem::file_close(fs_file_t file) {
356
366
return lfs_toerror (err);
357
367
}
358
368
359
- ssize_t LittleFileSystem::file_read (fs_file_t file, void *buffer, size_t len) {
369
+ ssize_t LittleFileSystem::file_read (fs_file_t file, void *buffer, size_t len)
370
+ {
360
371
lfs_file_t *f = (lfs_file_t *)file;
361
372
_mutex.lock ();
362
373
LFS_INFO (" file_read(%p, %p, %d)" , file, buffer, len);
@@ -366,7 +377,8 @@ ssize_t LittleFileSystem::file_read(fs_file_t file, void *buffer, size_t len) {
366
377
return lfs_toerror (res);
367
378
}
368
379
369
- ssize_t LittleFileSystem::file_write (fs_file_t file, const void *buffer, size_t len) {
380
+ ssize_t LittleFileSystem::file_write (fs_file_t file, const void *buffer, size_t len)
381
+ {
370
382
lfs_file_t *f = (lfs_file_t *)file;
371
383
_mutex.lock ();
372
384
LFS_INFO (" file_write(%p, %p, %d)" , file, buffer, len);
@@ -376,7 +388,8 @@ ssize_t LittleFileSystem::file_write(fs_file_t file, const void *buffer, size_t
376
388
return lfs_toerror (res);
377
389
}
378
390
379
- int LittleFileSystem::file_sync (fs_file_t file) {
391
+ int LittleFileSystem::file_sync (fs_file_t file)
392
+ {
380
393
lfs_file_t *f = (lfs_file_t *)file;
381
394
_mutex.lock ();
382
395
LFS_INFO (" file_sync(%p)" , file);
@@ -386,7 +399,8 @@ int LittleFileSystem::file_sync(fs_file_t file) {
386
399
return lfs_toerror (err);
387
400
}
388
401
389
- off_t LittleFileSystem::file_seek (fs_file_t file, off_t offset, int whence) {
402
+ off_t LittleFileSystem::file_seek (fs_file_t file, off_t offset, int whence)
403
+ {
390
404
lfs_file_t *f = (lfs_file_t *)file;
391
405
_mutex.lock ();
392
406
LFS_INFO (" file_seek(%p, %ld, %d)" , file, offset, whence);
@@ -396,7 +410,8 @@ off_t LittleFileSystem::file_seek(fs_file_t file, off_t offset, int whence) {
396
410
return lfs_toerror (res);
397
411
}
398
412
399
- off_t LittleFileSystem::file_tell (fs_file_t file) {
413
+ off_t LittleFileSystem::file_tell (fs_file_t file)
414
+ {
400
415
lfs_file_t *f = (lfs_file_t *)file;
401
416
_mutex.lock ();
402
417
LFS_INFO (" file_tell(%p)" , file);
@@ -406,7 +421,8 @@ off_t LittleFileSystem::file_tell(fs_file_t file) {
406
421
return lfs_toerror (res);
407
422
}
408
423
409
- off_t LittleFileSystem::file_size (fs_file_t file) {
424
+ off_t LittleFileSystem::file_size (fs_file_t file)
425
+ {
410
426
lfs_file_t *f = (lfs_file_t *)file;
411
427
_mutex.lock ();
412
428
LFS_INFO (" file_size(%p)" , file);
@@ -418,7 +434,8 @@ off_t LittleFileSystem::file_size(fs_file_t file) {
418
434
419
435
420
436
// //// Dir operations //////
421
- int LittleFileSystem::dir_open (fs_dir_t *dir, const char *path) {
437
+ int LittleFileSystem::dir_open (fs_dir_t *dir, const char *path)
438
+ {
422
439
lfs_dir_t *d = new lfs_dir_t ;
423
440
_mutex.lock ();
424
441
LFS_INFO (" dir_open(%p, \" %s\" )" , *dir, path);
@@ -433,7 +450,8 @@ int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path) {
433
450
return lfs_toerror (err);
434
451
}
435
452
436
- int LittleFileSystem::dir_close (fs_dir_t dir) {
453
+ int LittleFileSystem::dir_close (fs_dir_t dir)
454
+ {
437
455
lfs_dir_t *d = (lfs_dir_t *)dir;
438
456
_mutex.lock ();
439
457
LFS_INFO (" dir_close(%p)" , dir);
@@ -444,7 +462,8 @@ int LittleFileSystem::dir_close(fs_dir_t dir) {
444
462
return lfs_toerror (err);
445
463
}
446
464
447
- ssize_t LittleFileSystem::dir_read (fs_dir_t dir, struct dirent *ent) {
465
+ ssize_t LittleFileSystem::dir_read (fs_dir_t dir, struct dirent *ent)
466
+ {
448
467
lfs_dir_t *d = (lfs_dir_t *)dir;
449
468
struct lfs_info info;
450
469
_mutex.lock ();
@@ -459,7 +478,8 @@ ssize_t LittleFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) {
459
478
return lfs_toerror (res);
460
479
}
461
480
462
- void LittleFileSystem::dir_seek (fs_dir_t dir, off_t offset) {
481
+ void LittleFileSystem::dir_seek (fs_dir_t dir, off_t offset)
482
+ {
463
483
lfs_dir_t *d = (lfs_dir_t *)dir;
464
484
_mutex.lock ();
465
485
LFS_INFO (" dir_seek(%p, %ld)" , dir, offset);
@@ -468,7 +488,8 @@ void LittleFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
468
488
_mutex.unlock ();
469
489
}
470
490
471
- off_t LittleFileSystem::dir_tell (fs_dir_t dir) {
491
+ off_t LittleFileSystem::dir_tell (fs_dir_t dir)
492
+ {
472
493
lfs_dir_t *d = (lfs_dir_t *)dir;
473
494
_mutex.lock ();
474
495
LFS_INFO (" dir_tell(%p)" , dir);
@@ -478,7 +499,8 @@ off_t LittleFileSystem::dir_tell(fs_dir_t dir) {
478
499
return lfs_toerror (res);
479
500
}
480
501
481
- void LittleFileSystem::dir_rewind (fs_dir_t dir) {
502
+ void LittleFileSystem::dir_rewind (fs_dir_t dir)
503
+ {
482
504
lfs_dir_t *d = (lfs_dir_t *)dir;
483
505
_mutex.lock ();
484
506
LFS_INFO (" dir_rewind(%p)" , dir);
0 commit comments