No change from original version.
Fork of FATFileSystem by
Embed:
(wiki syntax)
Show/hide line numbers
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 #include "critical.h" 00031 00032 DWORD get_fattime(void) { 00033 time_t rawtime; 00034 time(&rawtime); 00035 struct tm *ptm = localtime(&rawtime); 00036 return (DWORD)(ptm->tm_year - 80) << 25 00037 | (DWORD)(ptm->tm_mon + 1 ) << 21 00038 | (DWORD)(ptm->tm_mday ) << 16 00039 | (DWORD)(ptm->tm_hour ) << 11 00040 | (DWORD)(ptm->tm_min ) << 5 00041 | (DWORD)(ptm->tm_sec/2 ); 00042 } 00043 00044 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; 00045 static PlatformMutex * mutex = NULL; 00046 00047 PlatformMutex * get_fat_mutex() { 00048 PlatformMutex * new_mutex = new PlatformMutex; 00049 00050 core_util_critical_section_enter(); 00051 if (NULL == mutex) { 00052 mutex = new_mutex; 00053 } 00054 core_util_critical_section_exit(); 00055 00056 if (mutex != new_mutex) { 00057 delete new_mutex; 00058 } 00059 return mutex; 00060 } 00061 00062 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n), _mutex(get_fat_mutex()) { 00063 lock(); 00064 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n); 00065 for(int i=0; i<_VOLUMES; i++) { 00066 if(_ffs[i] == 0) { 00067 _ffs[i] = this; 00068 _fsid[0] = '0' + i; 00069 _fsid[1] = '\0'; 00070 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid); 00071 f_mount(&_fs, _fsid, 0); 00072 unlock(); 00073 return; 00074 } 00075 } 00076 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n); 00077 unlock(); 00078 } 00079 00080 FATFileSystem::~FATFileSystem() { 00081 lock(); 00082 for (int i=0; i<_VOLUMES; i++) { 00083 if (_ffs[i] == this) { 00084 _ffs[i] = 0; 00085 f_mount(NULL, _fsid, 0); 00086 } 00087 } 00088 unlock(); 00089 } 00090 00091 FileHandle *FATFileSystem::open(const char* name, int flags) { 00092 lock(); 00093 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid); 00094 char n[64]; 00095 sprintf(n, "%s:/%s", _fsid, name); 00096 00097 /* POSIX flags -> FatFS open mode */ 00098 BYTE openmode; 00099 if (flags & O_RDWR) { 00100 openmode = FA_READ|FA_WRITE; 00101 } else if(flags & O_WRONLY) { 00102 openmode = FA_WRITE; 00103 } else { 00104 openmode = FA_READ; 00105 } 00106 if(flags & O_CREAT) { 00107 if(flags & O_TRUNC) { 00108 openmode |= FA_CREATE_ALWAYS; 00109 } else { 00110 openmode |= FA_OPEN_ALWAYS; 00111 } 00112 } 00113 00114 FIL fh; 00115 FRESULT res = f_open(&fh, n, openmode); 00116 if (res) { 00117 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res); 00118 unlock(); 00119 return NULL; 00120 } 00121 if (flags & O_APPEND) { 00122 f_lseek(&fh, fh.fsize); 00123 } 00124 FATFileHandle * handle = new FATFileHandle(fh, _mutex); 00125 unlock(); 00126 return handle; 00127 } 00128 00129 int FATFileSystem::remove(const char *filename) { 00130 lock(); 00131 FRESULT res = f_unlink(filename); 00132 if (res) { 00133 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res); 00134 unlock(); 00135 return -1; 00136 } 00137 unlock(); 00138 return 0; 00139 } 00140 00141 int FATFileSystem::rename(const char *oldname, const char *newname) { 00142 lock(); 00143 FRESULT res = f_rename(oldname, newname); 00144 if (res) { 00145 debug_if(FFS_DBG, "f_rename() failed: %d\n", res); 00146 unlock(); 00147 return -1; 00148 } 00149 unlock(); 00150 return 0; 00151 } 00152 00153 int FATFileSystem::format() { 00154 lock(); 00155 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) 00156 if (res) { 00157 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); 00158 unlock(); 00159 return -1; 00160 } 00161 unlock(); 00162 return 0; 00163 } 00164 00165 DirHandle *FATFileSystem::opendir(const char *name) { 00166 lock(); 00167 FATFS_DIR dir; 00168 FRESULT res = f_opendir(&dir, name); 00169 if (res != 0) { 00170 unlock(); 00171 return NULL; 00172 } 00173 FATDirHandle *handle = new FATDirHandle(dir, _mutex); 00174 unlock(); 00175 return handle; 00176 } 00177 00178 int FATFileSystem::mkdir(const char *name, mode_t mode) { 00179 lock(); 00180 FRESULT res = f_mkdir(name); 00181 unlock(); 00182 return res == 0 ? 0 : -1; 00183 } 00184 00185 int FATFileSystem::mount() { 00186 lock(); 00187 FRESULT res = f_mount(&_fs, _fsid, 1); 00188 unlock(); 00189 return res == 0 ? 0 : -1; 00190 } 00191 00192 int FATFileSystem::unmount() { 00193 lock(); 00194 if (disk_sync()) { 00195 unlock(); 00196 return -1; 00197 } 00198 FRESULT res = f_mount(NULL, _fsid, 0); 00199 unlock(); 00200 return res == 0 ? 0 : -1; 00201 } 00202 00203 void FATFileSystem::lock() { 00204 _mutex->lock(); 00205 } 00206 00207 void FATFileSystem::unlock() { 00208 _mutex->unlock(); 00209 }
Generated on Sat Jul 16 2022 10:36:32 by 1.7.2