Some minor updates

Fork of FATFileSystem by mbed official

Committer:
Kojto
Date:
Thu Aug 25 09:34:29 2016 +0100
Revision:
8:c4baca9a2c3d
Parent:
5:b3b3370574cf
Synchronized with git revision 4047ff95767d307

- fix _name error (getName())
- add thread safety

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:46ce1e16c870 1 /* mbed Microcontroller Library
emilmont 1:46ce1e16c870 2 * Copyright (c) 2006-2012 ARM Limited
emilmont 1:46ce1e16c870 3 *
emilmont 1:46ce1e16c870 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
emilmont 1:46ce1e16c870 5 * of this software and associated documentation files (the "Software"), to deal
emilmont 1:46ce1e16c870 6 * in the Software without restriction, including without limitation the rights
emilmont 1:46ce1e16c870 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emilmont 1:46ce1e16c870 8 * copies of the Software, and to permit persons to whom the Software is
emilmont 1:46ce1e16c870 9 * furnished to do so, subject to the following conditions:
emilmont 1:46ce1e16c870 10 *
emilmont 1:46ce1e16c870 11 * The above copyright notice and this permission notice shall be included in
emilmont 1:46ce1e16c870 12 * all copies or substantial portions of the Software.
emilmont 1:46ce1e16c870 13 *
emilmont 1:46ce1e16c870 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emilmont 1:46ce1e16c870 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emilmont 1:46ce1e16c870 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emilmont 1:46ce1e16c870 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emilmont 1:46ce1e16c870 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 1:46ce1e16c870 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
emilmont 1:46ce1e16c870 20 * SOFTWARE.
emilmont 1:46ce1e16c870 21 */
emilmont 1:46ce1e16c870 22 #include <string.h>
emilmont 1:46ce1e16c870 23 #include "ff.h"
emilmont 1:46ce1e16c870 24 #include "FATDirHandle.h"
emilmont 1:46ce1e16c870 25
emilmont 1:46ce1e16c870 26 using namespace mbed;
emilmont 1:46ce1e16c870 27
Kojto 8:c4baca9a2c3d 28 FATDirHandle::FATDirHandle(const FATFS_DIR &the_dir, PlatformMutex * mutex): _mutex(mutex) {
emilmont 1:46ce1e16c870 29 dir = the_dir;
emilmont 1:46ce1e16c870 30 }
emilmont 1:46ce1e16c870 31
emilmont 1:46ce1e16c870 32 int FATDirHandle::closedir() {
Kojto 8:c4baca9a2c3d 33 lock();
mbed_official 5:b3b3370574cf 34 int retval = f_closedir(&dir);
Kojto 8:c4baca9a2c3d 35 unlock();
emilmont 1:46ce1e16c870 36 delete this;
mbed_official 5:b3b3370574cf 37 return retval;
emilmont 1:46ce1e16c870 38 }
emilmont 1:46ce1e16c870 39
emilmont 1:46ce1e16c870 40 struct dirent *FATDirHandle::readdir() {
emilmont 1:46ce1e16c870 41 FILINFO finfo;
emilmont 1:46ce1e16c870 42
Kojto 8:c4baca9a2c3d 43 lock();
emilmont 1:46ce1e16c870 44 #if _USE_LFN
emilmont 1:46ce1e16c870 45 finfo.lfname = cur_entry.d_name;
emilmont 1:46ce1e16c870 46 finfo.lfsize = sizeof(cur_entry.d_name);
emilmont 1:46ce1e16c870 47 #endif // _USE_LFN
emilmont 1:46ce1e16c870 48
emilmont 1:46ce1e16c870 49 FRESULT res = f_readdir(&dir, &finfo);
emilmont 1:46ce1e16c870 50
emilmont 1:46ce1e16c870 51 #if _USE_LFN
emilmont 1:46ce1e16c870 52 if(res != 0 || finfo.fname[0]==0) {
Kojto 8:c4baca9a2c3d 53 unlock();
emilmont 1:46ce1e16c870 54 return NULL;
emilmont 1:46ce1e16c870 55 } else {
emilmont 1:46ce1e16c870 56 if(cur_entry.d_name[0]==0) {
emilmont 1:46ce1e16c870 57 // No long filename so use short filename.
emilmont 1:46ce1e16c870 58 memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
emilmont 1:46ce1e16c870 59 }
Kojto 8:c4baca9a2c3d 60 unlock();
emilmont 1:46ce1e16c870 61 return &cur_entry;
emilmont 1:46ce1e16c870 62 }
emilmont 1:46ce1e16c870 63 #else
emilmont 1:46ce1e16c870 64 if(res != 0 || finfo.fname[0]==0) {
Kojto 8:c4baca9a2c3d 65 unlock();
emilmont 1:46ce1e16c870 66 return NULL;
emilmont 1:46ce1e16c870 67 } else {
emilmont 1:46ce1e16c870 68 memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
Kojto 8:c4baca9a2c3d 69 unlock();
emilmont 1:46ce1e16c870 70 return &cur_entry;
emilmont 1:46ce1e16c870 71 }
emilmont 1:46ce1e16c870 72 #endif /* _USE_LFN */
emilmont 1:46ce1e16c870 73 }
emilmont 1:46ce1e16c870 74
emilmont 1:46ce1e16c870 75 void FATDirHandle::rewinddir() {
Kojto 8:c4baca9a2c3d 76 lock();
emilmont 1:46ce1e16c870 77 dir.index = 0;
Kojto 8:c4baca9a2c3d 78 unlock();
emilmont 1:46ce1e16c870 79 }
emilmont 1:46ce1e16c870 80
emilmont 1:46ce1e16c870 81 off_t FATDirHandle::telldir() {
Kojto 8:c4baca9a2c3d 82 lock();
Kojto 8:c4baca9a2c3d 83 off_t offset = dir.index;
Kojto 8:c4baca9a2c3d 84 unlock();
Kojto 8:c4baca9a2c3d 85 return offset;
emilmont 1:46ce1e16c870 86 }
emilmont 1:46ce1e16c870 87
emilmont 1:46ce1e16c870 88 void FATDirHandle::seekdir(off_t location) {
Kojto 8:c4baca9a2c3d 89 lock();
emilmont 1:46ce1e16c870 90 dir.index = location;
Kojto 8:c4baca9a2c3d 91 unlock();
emilmont 1:46ce1e16c870 92 }
emilmont 1:46ce1e16c870 93
Kojto 8:c4baca9a2c3d 94 void FATDirHandle::lock() {
Kojto 8:c4baca9a2c3d 95 _mutex->lock();
Kojto 8:c4baca9a2c3d 96 }
Kojto 8:c4baca9a2c3d 97
Kojto 8:c4baca9a2c3d 98 void FATDirHandle::unlock() {
Kojto 8:c4baca9a2c3d 99 _mutex->unlock();
Kojto 8:c4baca9a2c3d 100 }
Kojto 8:c4baca9a2c3d 101