first 2016/02 SDFileSystemDMA inherited from Official SDFileSystem.

Dependents:   SDFileSystemDMA-test DmdFullRGB_0_1

Fork of SDFileSystemDMA by mi mi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FATFileSystem.cpp Source File

FATFileSystem.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #include "mbed.h"
00023 
00024 #include "ffconf.h"
00025 #include "mbed_debug.h"
00026 
00027 #include "FATFileSystem.h"
00028 #include "FATFileHandle.h"
00029 #include "FATDirHandle.h"
00030 
00031 DWORD get_fattime(void) {
00032     time_t rawtime;
00033     time(&rawtime);
00034     struct tm *ptm = localtime(&rawtime);
00035     return (DWORD)(ptm->tm_year - 80) << 25
00036          | (DWORD)(ptm->tm_mon + 1  ) << 21
00037          | (DWORD)(ptm->tm_mday     ) << 16
00038          | (DWORD)(ptm->tm_hour     ) << 11
00039          | (DWORD)(ptm->tm_min      ) << 5
00040          | (DWORD)(ptm->tm_sec/2    );
00041 }
00042 
00043 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
00044 
00045 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
00046     debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
00047     for(int i=0; i<_VOLUMES; i++) {
00048         if(_ffs[i] == 0) {
00049             _ffs[i] = this;
00050             _fsid[0] = '0' + i;
00051             _fsid[1] = '\0';
00052             debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
00053             f_mount(&_fs, _fsid, 0);
00054             return;
00055         }
00056     }
00057     error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
00058 }
00059 
00060 FATFileSystem::~FATFileSystem() {
00061     for (int i=0; i<_VOLUMES; i++) {
00062         if (_ffs[i] == this) {
00063             _ffs[i] = 0;
00064             f_mount(NULL, _fsid, 0);
00065         }
00066     }
00067 }
00068 
00069 FileHandle *FATFileSystem::open(const char* name, int flags) {
00070     debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
00071     char n[64];
00072     sprintf(n, "%s:/%s", _fsid, name);
00073 
00074     /* POSIX flags -> FatFS open mode */
00075     BYTE openmode;
00076 #if _FS_READONLY==1
00077     openmode = FA_READ;
00078 #else
00079     if (flags & O_RDWR) {
00080         openmode = FA_READ|FA_WRITE;
00081     } else if(flags & O_WRONLY) {
00082         openmode = FA_WRITE;
00083     } else {
00084         openmode = FA_READ;
00085     }
00086     if(flags & O_CREAT) {
00087         if(flags & O_TRUNC) {
00088             openmode |= FA_CREATE_ALWAYS;
00089         } else {
00090             openmode |= FA_OPEN_ALWAYS;
00091         }
00092     }
00093 #endif
00094 
00095     FIL fh;
00096     FRESULT res = f_open(&fh, n, openmode);
00097     if (res) {
00098         debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
00099         return NULL;
00100     }
00101 #if !_FS_READONLY
00102     if (flags & O_APPEND) {
00103         f_lseek(&fh, fh.fsize);
00104     }
00105 #endif
00106     return new FATFileHandle(fh);
00107 }
00108 
00109 int FATFileSystem::remove(const char *filename) {
00110 #if !_FS_READONLY
00111     FRESULT res = f_unlink(filename);
00112     if (res) {
00113         debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
00114         return -1;
00115     }
00116     return 0;
00117 #else
00118     return -1;
00119 #endif
00120 }
00121 
00122 int FATFileSystem::rename(const char *oldname, const char *newname) {
00123 #if !_FS_READONLY
00124     FRESULT res = f_rename(oldname, newname);
00125     if (res) {
00126         debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
00127         return -1;
00128     }
00129     return 0;
00130 #else
00131     return -1;
00132 #endif
00133 }
00134 
00135 int FATFileSystem::format() {
00136 #if !_FS_READONLY
00137     FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
00138     if (res) {
00139         debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
00140         return -1;
00141     }
00142     return 0;
00143 #else
00144     return -1;
00145 #endif
00146 }
00147 
00148 DirHandle *FATFileSystem::opendir(const char *name) {
00149     FATFS_DIR dir;
00150     FRESULT res = f_opendir(&dir, name);
00151     if (res != 0) {
00152         return NULL;
00153     }
00154     return new FATDirHandle(dir);
00155 }
00156 
00157 int FATFileSystem::mkdir(const char *name, mode_t mode) {
00158 #if !_FS_READONLY
00159     FRESULT res = f_mkdir(name);
00160     return res == 0 ? 0 : -1;
00161 #else
00162     return -1;
00163 #endif
00164 }
00165 
00166 int FATFileSystem::mount() {
00167     FRESULT res = f_mount(&_fs, _fsid, 1);
00168     return res == 0 ? 0 : -1;
00169 }
00170 
00171 int FATFileSystem::unmount() {
00172     if (disk_sync())
00173         return -1;
00174     FRESULT res = f_mount(NULL, _fsid, 0);
00175     return res == 0 ? 0 : -1;
00176 }