it's fat!

Fork of FATFileSystem by mbed official

Committer:
bwang
Date:
Wed Apr 18 12:25:04 2018 +0000
Revision:
11:f430519973b0
Parent:
10:28e685e5ff7f
timestamp modified to return 0, as this version of the board has no RTC

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"
emilmont 1:46ce1e16c870 30
emilmont 1:46ce1e16c870 31 DWORD get_fattime(void) {
bwang 11:f430519973b0 32 /*
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 );
bwang 11:f430519973b0 42 */
bwang 11:f430519973b0 43 return 0;
emilmont 1:46ce1e16c870 44 }
emilmont 1:46ce1e16c870 45
emilmont 1:46ce1e16c870 46 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
emilmont 1:46ce1e16c870 47
emilmont 1:46ce1e16c870 48 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
emilmont 1:46ce1e16c870 49 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
emilmont 1:46ce1e16c870 50 for(int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 51 if(_ffs[i] == 0) {
emilmont 1:46ce1e16c870 52 _ffs[i] = this;
mbed_official 5:b3b3370574cf 53 _fsid[0] = '0' + i;
mbed_official 5:b3b3370574cf 54 _fsid[1] = '\0';
p3p 9:e2ab678eb692 55 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
mbed_official 5:b3b3370574cf 56 f_mount(&_fs, _fsid, 0);
emilmont 1:46ce1e16c870 57 return;
emilmont 1:46ce1e16c870 58 }
emilmont 1:46ce1e16c870 59 }
emilmont 1:46ce1e16c870 60 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
emilmont 1:46ce1e16c870 61 }
emilmont 1:46ce1e16c870 62
emilmont 1:46ce1e16c870 63 FATFileSystem::~FATFileSystem() {
emilmont 1:46ce1e16c870 64 for (int i=0; i<_VOLUMES; i++) {
emilmont 1:46ce1e16c870 65 if (_ffs[i] == this) {
emilmont 1:46ce1e16c870 66 _ffs[i] = 0;
mbed_official 5:b3b3370574cf 67 f_mount(NULL, _fsid, 0);
emilmont 1:46ce1e16c870 68 }
emilmont 1:46ce1e16c870 69 }
emilmont 1:46ce1e16c870 70 }
emilmont 1:46ce1e16c870 71
emilmont 1:46ce1e16c870 72 FileHandle *FATFileSystem::open(const char* name, int flags) {
p3p 9:e2ab678eb692 73 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
emilmont 1:46ce1e16c870 74 char n[64];
mbed_official 5:b3b3370574cf 75 sprintf(n, "%s:/%s", _fsid, name);
mbed_official 4:3ff2606d5713 76
emilmont 1:46ce1e16c870 77 /* POSIX flags -> FatFS open mode */
emilmont 1:46ce1e16c870 78 BYTE openmode;
emilmont 1:46ce1e16c870 79 if (flags & O_RDWR) {
emilmont 1:46ce1e16c870 80 openmode = FA_READ|FA_WRITE;
emilmont 1:46ce1e16c870 81 } else if(flags & O_WRONLY) {
emilmont 1:46ce1e16c870 82 openmode = FA_WRITE;
emilmont 1:46ce1e16c870 83 } else {
emilmont 1:46ce1e16c870 84 openmode = FA_READ;
emilmont 1:46ce1e16c870 85 }
emilmont 1:46ce1e16c870 86 if(flags & O_CREAT) {
emilmont 1:46ce1e16c870 87 if(flags & O_TRUNC) {
emilmont 1:46ce1e16c870 88 openmode |= FA_CREATE_ALWAYS;
emilmont 1:46ce1e16c870 89 } else {
emilmont 1:46ce1e16c870 90 openmode |= FA_OPEN_ALWAYS;
emilmont 1:46ce1e16c870 91 }
emilmont 1:46ce1e16c870 92 }
mbed_official 4:3ff2606d5713 93
emilmont 1:46ce1e16c870 94 FIL fh;
emilmont 1:46ce1e16c870 95 FRESULT res = f_open(&fh, n, openmode);
mbed_official 4:3ff2606d5713 96 if (res) {
emilmont 1:46ce1e16c870 97 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
emilmont 1:46ce1e16c870 98 return NULL;
emilmont 1:46ce1e16c870 99 }
emilmont 1:46ce1e16c870 100 if (flags & O_APPEND) {
emilmont 1:46ce1e16c870 101 f_lseek(&fh, fh.fsize);
emilmont 1:46ce1e16c870 102 }
emilmont 1:46ce1e16c870 103 return new FATFileHandle(fh);
emilmont 1:46ce1e16c870 104 }
mbed_official 4:3ff2606d5713 105
geky 10:28e685e5ff7f 106 int FATFileSystem::open(FileHandle **file, const char *name, int flags) {
geky 10:28e685e5ff7f 107 FileHandle *temp = open(name, flags);
geky 10:28e685e5ff7f 108 if (!temp) {
geky 10:28e685e5ff7f 109 return -1;
geky 10:28e685e5ff7f 110 }
geky 10:28e685e5ff7f 111
geky 10:28e685e5ff7f 112 *file = temp;
geky 10:28e685e5ff7f 113 return 0;
geky 10:28e685e5ff7f 114 }
geky 10:28e685e5ff7f 115
emilmont 1:46ce1e16c870 116 int FATFileSystem::remove(const char *filename) {
emilmont 1:46ce1e16c870 117 FRESULT res = f_unlink(filename);
mbed_official 4:3ff2606d5713 118 if (res) {
emilmont 1:46ce1e16c870 119 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
emilmont 1:46ce1e16c870 120 return -1;
emilmont 1:46ce1e16c870 121 }
emilmont 1:46ce1e16c870 122 return 0;
emilmont 1:46ce1e16c870 123 }
emilmont 1:46ce1e16c870 124
mbed_official 4:3ff2606d5713 125 int FATFileSystem::rename(const char *oldname, const char *newname) {
mbed_official 4:3ff2606d5713 126 FRESULT res = f_rename(oldname, newname);
mbed_official 4:3ff2606d5713 127 if (res) {
mbed_official 4:3ff2606d5713 128 debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
mbed_official 4:3ff2606d5713 129 return -1;
mbed_official 4:3ff2606d5713 130 }
mbed_official 4:3ff2606d5713 131 return 0;
mbed_official 4:3ff2606d5713 132 }
mbed_official 4:3ff2606d5713 133
emilmont 1:46ce1e16c870 134 int FATFileSystem::format() {
emilmont 1:46ce1e16c870 135 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
emilmont 1:46ce1e16c870 136 if (res) {
emilmont 1:46ce1e16c870 137 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
emilmont 1:46ce1e16c870 138 return -1;
emilmont 1:46ce1e16c870 139 }
emilmont 1:46ce1e16c870 140 return 0;
emilmont 1:46ce1e16c870 141 }
emilmont 1:46ce1e16c870 142
emilmont 1:46ce1e16c870 143 DirHandle *FATFileSystem::opendir(const char *name) {
emilmont 1:46ce1e16c870 144 FATFS_DIR dir;
emilmont 1:46ce1e16c870 145 FRESULT res = f_opendir(&dir, name);
emilmont 1:46ce1e16c870 146 if (res != 0) {
emilmont 1:46ce1e16c870 147 return NULL;
emilmont 1:46ce1e16c870 148 }
emilmont 1:46ce1e16c870 149 return new FATDirHandle(dir);
emilmont 1:46ce1e16c870 150 }
emilmont 1:46ce1e16c870 151
geky 10:28e685e5ff7f 152 int FATFileSystem::open(DirHandle **dir, const char *name) {
geky 10:28e685e5ff7f 153 DirHandle *temp = opendir(name);
geky 10:28e685e5ff7f 154 if (!temp) {
geky 10:28e685e5ff7f 155 return -1;
geky 10:28e685e5ff7f 156 }
geky 10:28e685e5ff7f 157
geky 10:28e685e5ff7f 158 *dir = temp;
geky 10:28e685e5ff7f 159 return 0;
geky 10:28e685e5ff7f 160 }
geky 10:28e685e5ff7f 161
emilmont 1:46ce1e16c870 162 int FATFileSystem::mkdir(const char *name, mode_t mode) {
emilmont 1:46ce1e16c870 163 FRESULT res = f_mkdir(name);
emilmont 1:46ce1e16c870 164 return res == 0 ? 0 : -1;
emilmont 1:46ce1e16c870 165 }
mbed_official 4:3ff2606d5713 166
mbed_official 4:3ff2606d5713 167 int FATFileSystem::mount() {
mbed_official 5:b3b3370574cf 168 FRESULT res = f_mount(&_fs, _fsid, 1);
mbed_official 4:3ff2606d5713 169 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 170 }
mbed_official 4:3ff2606d5713 171
mbed_official 4:3ff2606d5713 172 int FATFileSystem::unmount() {
mbed_official 4:3ff2606d5713 173 if (disk_sync())
mbed_official 4:3ff2606d5713 174 return -1;
mbed_official 5:b3b3370574cf 175 FRESULT res = f_mount(NULL, _fsid, 0);
mbed_official 4:3ff2606d5713 176 return res == 0 ? 0 : -1;
mbed_official 4:3ff2606d5713 177 }