My File System, no changes from official.

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 "mbed.h"
emilmont 1:46ce1e16c870 23
emilmont 1:46ce1e16c870 24 #include "ffconf.h"
emilmont 2:b6669c987c8e 25 #include "mbed_debug.h"
emilmont 1:46ce1e16c870 26
emilmont 1:46ce1e16c870 27 #include "FATFileSystem.h"
emilmont 1:46ce1e16c870 28 #include "FATFileHandle.h"
emilmont 1:46ce1e16c870 29 #include "FATDirHandle.h"
Kojto 8:c4baca9a2c3d 30 #include "critical.h"
emilmont 1:46ce1e16c870 31
emilmont 1:46ce1e16c870 32 DWORD get_fattime(void) {
emilmont 1:46ce1e16c870 33 time_t rawtime;
emilmont 1:46ce1e16c870 34 time(&rawtime);
emilmont 1:46ce1e16c870 35 struct tm *ptm = localtime(&rawtime);
emilmont 1:46ce1e16c870 36 return (DWORD)(ptm->tm_year - 80) << 25
emilmont 1:46ce1e16c870 37 | (DWORD)(ptm->tm_mon + 1 ) << 21
emilmont 1:46ce1e16c870 38 | (DWORD)(ptm->tm_mday ) << 16
emilmont 1:46ce1e16c870 39 | (DWORD)(ptm->tm_hour ) << 11
emilmont 1:46ce1e16c870 40 | (DWORD)(ptm->tm_min ) << 5
emilmont 1:46ce1e16c870 41 | (DWORD)(ptm->tm_sec/2 );
emilmont 1:46ce1e16c870 42 }
emilmont 1:46ce1e16c870 43
emilmont 1:46ce1e16c870 44 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
Kojto 8:c4baca9a2c3d 45 static PlatformMutex * mutex = NULL;
emilmont 1:46ce1e16c870 46
Kojto 8:c4baca9a2c3d 47 PlatformMutex * get_fat_mutex() {
Kojto 8:c4baca9a2c3d 48 PlatformMutex * new_mutex = new PlatformMutex;
Kojto 8:c4baca9a2c3d 49
Kojto 8:c4baca9a2c3d 50 core_util_critical_section_enter();
Kojto 8:c4baca9a2c3d 51 if (NULL == mutex) {
Kojto 8:c4baca9a2c3d 52 mutex = new_mutex;
Kojto 8:c4baca9a2c3d 53 }
Kojto 8:c4baca9a2c3d 54 core_util_critical_section_exit();
Kojto 8:c4baca9a2c3d 55
Kojto 8:c4baca9a2c3d 56 if (mutex != new_mutex) {
Kojto 8:c4baca9a2c3d 57 delete new_mutex;
Kojto 8:c4baca9a2c3d 58 }
Kojto 8:c4baca9a2c3d 59 return mutex;
Kojto 8:c4baca9a2c3d 60 }
Kojto 8:c4baca9a2c3d 61
Kojto 8:c4baca9a2c3d 62 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n), _mutex(get_fat_mutex()) {
Kojto 8:c4baca9a2c3d 63 lock();
emilmont 1:46ce1e16c870 64 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
emilmont 1:46ce1e16c870 65 for(int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 66 if(_ffs[i] == 0) {
emilmont 1:46ce1e16c870 67 _ffs[i] = this;
mbed_official 5:b3b3370574cf 68 _fsid[0] = '0' + i;
mbed_official 5:b3b3370574cf 69 _fsid[1] = '\0';
Kojto 8:c4baca9a2c3d 70 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
mbed_official 5:b3b3370574cf 71 f_mount(&_fs, _fsid, 0);
Kojto 8:c4baca9a2c3d 72 unlock();
emilmont 1:46ce1e16c870 73 return;
emilmont 1:46ce1e16c870 74 }
emilmont 1:46ce1e16c870 75 }
emilmont 1:46ce1e16c870 76 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
Kojto 8:c4baca9a2c3d 77 unlock();
emilmont 1:46ce1e16c870 78 }
emilmont 1:46ce1e16c870 79
emilmont 1:46ce1e16c870 80 FATFileSystem::~FATFileSystem() {
Kojto 8:c4baca9a2c3d 81 lock();
emilmont 1:46ce1e16c870 82 for (int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 83 if (_ffs[i] == this) {
emilmont 1:46ce1e16c870 84 _ffs[i] = 0;
mbed_official 5:b3b3370574cf 85 f_mount(NULL, _fsid, 0);
emilmont 1:46ce1e16c870 86 }
emilmont 1:46ce1e16c870 87 }
Kojto 8:c4baca9a2c3d 88 unlock();
emilmont 1:46ce1e16c870 89 }
emilmont 1:46ce1e16c870 90
emilmont 1:46ce1e16c870 91 FileHandle *FATFileSystem::open(const char* name, int flags) {
Kojto 8:c4baca9a2c3d 92 lock();
Kojto 8:c4baca9a2c3d 93 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
emilmont 1:46ce1e16c870 94 char n[64];
mbed_official 5:b3b3370574cf 95 sprintf(n, "%s:/%s", _fsid, name);
mbed_official 4:3ff2606d5713 96
emilmont 1:46ce1e16c870 97 /* POSIX flags -> FatFS open mode */
emilmont 1:46ce1e16c870 98 BYTE openmode;
emilmont 1:46ce1e16c870 99 if (flags & O_RDWR) {
emilmont 1:46ce1e16c870 100 openmode = FA_READ|FA_WRITE;
emilmont 1:46ce1e16c870 101 } else if(flags & O_WRONLY) {
emilmont 1:46ce1e16c870 102 openmode = FA_WRITE;
emilmont 1:46ce1e16c870 103 } else {
emilmont 1:46ce1e16c870 104 openmode = FA_READ;
emilmont 1:46ce1e16c870 105 }
emilmont 1:46ce1e16c870 106 if(flags & O_CREAT) {
emilmont 1:46ce1e16c870 107 if(flags & O_TRUNC) {
emilmont 1:46ce1e16c870 108 openmode |= FA_CREATE_ALWAYS;
emilmont 1:46ce1e16c870 109 } else {
emilmont 1:46ce1e16c870 110 openmode |= FA_OPEN_ALWAYS;
emilmont 1:46ce1e16c870 111 }
emilmont 1:46ce1e16c870 112 }
mbed_official 4:3ff2606d5713 113
emilmont 1:46ce1e16c870 114 FIL fh;
emilmont 1:46ce1e16c870 115 FRESULT res = f_open(&fh, n, openmode);
mbed_official 4:3ff2606d5713 116 if (res) {
emilmont 1:46ce1e16c870 117 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
Kojto 8:c4baca9a2c3d 118 unlock();
emilmont 1:46ce1e16c870 119 return NULL;
emilmont 1:46ce1e16c870 120 }
emilmont 1:46ce1e16c870 121 if (flags & O_APPEND) {
emilmont 1:46ce1e16c870 122 f_lseek(&fh, fh.fsize);
emilmont 1:46ce1e16c870 123 }
Kojto 8:c4baca9a2c3d 124 FATFileHandle * handle = new FATFileHandle(fh, _mutex);
Kojto 8:c4baca9a2c3d 125 unlock();
Kojto 8:c4baca9a2c3d 126 return handle;
emilmont 1:46ce1e16c870 127 }
mbed_official 4:3ff2606d5713 128
emilmont 1:46ce1e16c870 129 int FATFileSystem::remove(const char *filename) {
Kojto 8:c4baca9a2c3d 130 lock();
emilmont 1:46ce1e16c870 131 FRESULT res = f_unlink(filename);
mbed_official 4:3ff2606d5713 132 if (res) {
emilmont 1:46ce1e16c870 133 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
Kojto 8:c4baca9a2c3d 134 unlock();
emilmont 1:46ce1e16c870 135 return -1;
emilmont 1:46ce1e16c870 136 }
Kojto 8:c4baca9a2c3d 137 unlock();
emilmont 1:46ce1e16c870 138 return 0;
emilmont 1:46ce1e16c870 139 }
emilmont 1:46ce1e16c870 140
mbed_official 4:3ff2606d5713 141 int FATFileSystem::rename(const char *oldname, const char *newname) {
Kojto 8:c4baca9a2c3d 142 lock();
mbed_official 4:3ff2606d5713 143 FRESULT res = f_rename(oldname, newname);
mbed_official 4:3ff2606d5713 144 if (res) {
mbed_official 4:3ff2606d5713 145 debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
Kojto 8:c4baca9a2c3d 146 unlock();
mbed_official 4:3ff2606d5713 147 return -1;
mbed_official 4:3ff2606d5713 148 }
Kojto 8:c4baca9a2c3d 149 unlock();
mbed_official 4:3ff2606d5713 150 return 0;
mbed_official 4:3ff2606d5713 151 }
mbed_official 4:3ff2606d5713 152
emilmont 1:46ce1e16c870 153 int FATFileSystem::format() {
Kojto 8:c4baca9a2c3d 154 lock();
emilmont 1:46ce1e16c870 155 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
emilmont 1:46ce1e16c870 156 if (res) {
emilmont 1:46ce1e16c870 157 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
Kojto 8:c4baca9a2c3d 158 unlock();
emilmont 1:46ce1e16c870 159 return -1;
emilmont 1:46ce1e16c870 160 }
Kojto 8:c4baca9a2c3d 161 unlock();
emilmont 1:46ce1e16c870 162 return 0;
emilmont 1:46ce1e16c870 163 }
emilmont 1:46ce1e16c870 164
emilmont 1:46ce1e16c870 165 DirHandle *FATFileSystem::opendir(const char *name) {
Kojto 8:c4baca9a2c3d 166 lock();
emilmont 1:46ce1e16c870 167 FATFS_DIR dir;
emilmont 1:46ce1e16c870 168 FRESULT res = f_opendir(&dir, name);
emilmont 1:46ce1e16c870 169 if (res != 0) {
Kojto 8:c4baca9a2c3d 170 unlock();
emilmont 1:46ce1e16c870 171 return NULL;
emilmont 1:46ce1e16c870 172 }
Kojto 8:c4baca9a2c3d 173 FATDirHandle *handle = new FATDirHandle(dir, _mutex);
Kojto 8:c4baca9a2c3d 174 unlock();
Kojto 8:c4baca9a2c3d 175 return handle;
emilmont 1:46ce1e16c870 176 }
emilmont 1:46ce1e16c870 177
emilmont 1:46ce1e16c870 178 int FATFileSystem::mkdir(const char *name, mode_t mode) {
Kojto 8:c4baca9a2c3d 179 lock();
emilmont 1:46ce1e16c870 180 FRESULT res = f_mkdir(name);
Kojto 8:c4baca9a2c3d 181 unlock();
emilmont 1:46ce1e16c870 182 return res == 0 ? 0 : -1;
emilmont 1:46ce1e16c870 183 }
mbed_official 4:3ff2606d5713 184
mbed_official 4:3ff2606d5713 185 int FATFileSystem::mount() {
Kojto 8:c4baca9a2c3d 186 lock();
mbed_official 5:b3b3370574cf 187 FRESULT res = f_mount(&_fs, _fsid, 1);
Kojto 8:c4baca9a2c3d 188 unlock();
mbed_official 4:3ff2606d5713 189 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 190 }
mbed_official 4:3ff2606d5713 191
mbed_official 4:3ff2606d5713 192 int FATFileSystem::unmount() {
Kojto 8:c4baca9a2c3d 193 lock();
Kojto 8:c4baca9a2c3d 194 if (disk_sync()) {
Kojto 8:c4baca9a2c3d 195 unlock();
mbed_official 4:3ff2606d5713 196 return -1;
Kojto 8:c4baca9a2c3d 197 }
mbed_official 5:b3b3370574cf 198 FRESULT res = f_mount(NULL, _fsid, 0);
Kojto 8:c4baca9a2c3d 199 unlock();
mbed_official 4:3ff2606d5713 200 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 201 }
Kojto 8:c4baca9a2c3d 202
Kojto 8:c4baca9a2c3d 203 void FATFileSystem::lock() {
Kojto 8:c4baca9a2c3d 204 _mutex->lock();
Kojto 8:c4baca9a2c3d 205 }
Kojto 8:c4baca9a2c3d 206
Kojto 8:c4baca9a2c3d 207 void FATFileSystem::unlock() {
Kojto 8:c4baca9a2c3d 208 _mutex->unlock();
Kojto 8:c4baca9a2c3d 209 }