s

Fork of FATFileSystem by Chaiyaporn Boonyasathian

Committer:
583405000008
Date:
Tue Dec 06 07:22:42 2016 +0000
Revision:
1:6fa3f673d44c
Parent:
0:9296bb2a98a8
d;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cha45689 0:9296bb2a98a8 1 /* mbed Microcontroller Library
cha45689 0:9296bb2a98a8 2 * Copyright (c) 2006-2012 ARM Limited
cha45689 0:9296bb2a98a8 3 *
cha45689 0:9296bb2a98a8 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
cha45689 0:9296bb2a98a8 5 * of this software and associated documentation files (the "Software"), to deal
cha45689 0:9296bb2a98a8 6 * in the Software without restriction, including without limitation the rights
cha45689 0:9296bb2a98a8 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cha45689 0:9296bb2a98a8 8 * copies of the Software, and to permit persons to whom the Software is
cha45689 0:9296bb2a98a8 9 * furnished to do so, subject to the following conditions:
cha45689 0:9296bb2a98a8 10 *
cha45689 0:9296bb2a98a8 11 * The above copyright notice and this permission notice shall be included in
cha45689 0:9296bb2a98a8 12 * all copies or substantial portions of the Software.
cha45689 0:9296bb2a98a8 13 *
cha45689 0:9296bb2a98a8 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cha45689 0:9296bb2a98a8 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cha45689 0:9296bb2a98a8 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cha45689 0:9296bb2a98a8 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cha45689 0:9296bb2a98a8 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cha45689 0:9296bb2a98a8 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
cha45689 0:9296bb2a98a8 20 * SOFTWARE.
cha45689 0:9296bb2a98a8 21 */
cha45689 0:9296bb2a98a8 22 #include "mbed.h"
cha45689 0:9296bb2a98a8 23
cha45689 0:9296bb2a98a8 24 #include "ffconf.h"
cha45689 0:9296bb2a98a8 25 #include "mbed_debug.h"
cha45689 0:9296bb2a98a8 26
cha45689 0:9296bb2a98a8 27 #include "FATFileSystem.h"
cha45689 0:9296bb2a98a8 28 #include "FATFileHandle.h"
cha45689 0:9296bb2a98a8 29 #include "FATDirHandle.h"
cha45689 0:9296bb2a98a8 30
cha45689 0:9296bb2a98a8 31 DWORD get_fattime(void) {
cha45689 0:9296bb2a98a8 32 time_t rawtime;
cha45689 0:9296bb2a98a8 33 time(&rawtime);
cha45689 0:9296bb2a98a8 34 struct tm *ptm = localtime(&rawtime);
cha45689 0:9296bb2a98a8 35 return (DWORD)(ptm->tm_year - 80) << 25
cha45689 0:9296bb2a98a8 36 | (DWORD)(ptm->tm_mon + 1 ) << 21
cha45689 0:9296bb2a98a8 37 | (DWORD)(ptm->tm_mday ) << 16
cha45689 0:9296bb2a98a8 38 | (DWORD)(ptm->tm_hour ) << 11
cha45689 0:9296bb2a98a8 39 | (DWORD)(ptm->tm_min ) << 5
cha45689 0:9296bb2a98a8 40 | (DWORD)(ptm->tm_sec/2 );
cha45689 0:9296bb2a98a8 41 }
cha45689 0:9296bb2a98a8 42
cha45689 0:9296bb2a98a8 43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
cha45689 0:9296bb2a98a8 44
cha45689 0:9296bb2a98a8 45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
cha45689 0:9296bb2a98a8 46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
cha45689 0:9296bb2a98a8 47 for(int i=0; i<_VOLUMES; i++) {
cha45689 0:9296bb2a98a8 48 if(_ffs[i] == 0) {
cha45689 0:9296bb2a98a8 49 _ffs[i] = this;
cha45689 0:9296bb2a98a8 50 _fsid = i;
583405000008 1:6fa3f673d44c 51 //debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
cha45689 0:9296bb2a98a8 52 f_mount(i, &_fs);
cha45689 0:9296bb2a98a8 53 return;
cha45689 0:9296bb2a98a8 54 }
cha45689 0:9296bb2a98a8 55 }
cha45689 0:9296bb2a98a8 56 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
cha45689 0:9296bb2a98a8 57 }
cha45689 0:9296bb2a98a8 58
cha45689 0:9296bb2a98a8 59 FATFileSystem::~FATFileSystem() {
cha45689 0:9296bb2a98a8 60 for (int i=0; i<_VOLUMES; i++) {
cha45689 0:9296bb2a98a8 61 if (_ffs[i] == this) {
cha45689 0:9296bb2a98a8 62 _ffs[i] = 0;
cha45689 0:9296bb2a98a8 63 f_mount(i, NULL);
cha45689 0:9296bb2a98a8 64 }
cha45689 0:9296bb2a98a8 65 }
cha45689 0:9296bb2a98a8 66 }
cha45689 0:9296bb2a98a8 67
cha45689 0:9296bb2a98a8 68 FileHandle *FATFileSystem::open(const char* name, int flags) {
583405000008 1:6fa3f673d44c 69 //debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
cha45689 0:9296bb2a98a8 70 char n[64];
cha45689 0:9296bb2a98a8 71 sprintf(n, "%d:/%s", _fsid, name);
cha45689 0:9296bb2a98a8 72
cha45689 0:9296bb2a98a8 73 /* POSIX flags -> FatFS open mode */
cha45689 0:9296bb2a98a8 74 BYTE openmode;
cha45689 0:9296bb2a98a8 75 if (flags & O_RDWR) {
cha45689 0:9296bb2a98a8 76 openmode = FA_READ|FA_WRITE;
cha45689 0:9296bb2a98a8 77 } else if(flags & O_WRONLY) {
cha45689 0:9296bb2a98a8 78 openmode = FA_WRITE;
cha45689 0:9296bb2a98a8 79 } else {
cha45689 0:9296bb2a98a8 80 openmode = FA_READ;
cha45689 0:9296bb2a98a8 81 }
cha45689 0:9296bb2a98a8 82 if(flags & O_CREAT) {
cha45689 0:9296bb2a98a8 83 if(flags & O_TRUNC) {
cha45689 0:9296bb2a98a8 84 openmode |= FA_CREATE_ALWAYS;
cha45689 0:9296bb2a98a8 85 } else {
cha45689 0:9296bb2a98a8 86 openmode |= FA_OPEN_ALWAYS;
cha45689 0:9296bb2a98a8 87 }
cha45689 0:9296bb2a98a8 88 }
cha45689 0:9296bb2a98a8 89
cha45689 0:9296bb2a98a8 90 FIL fh;
cha45689 0:9296bb2a98a8 91 FRESULT res = f_open(&fh, n, openmode);
cha45689 0:9296bb2a98a8 92 if (res) {
cha45689 0:9296bb2a98a8 93 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
cha45689 0:9296bb2a98a8 94 return NULL;
cha45689 0:9296bb2a98a8 95 }
cha45689 0:9296bb2a98a8 96 if (flags & O_APPEND) {
cha45689 0:9296bb2a98a8 97 f_lseek(&fh, fh.fsize);
cha45689 0:9296bb2a98a8 98 }
cha45689 0:9296bb2a98a8 99 return new FATFileHandle(fh);
cha45689 0:9296bb2a98a8 100 }
cha45689 0:9296bb2a98a8 101
cha45689 0:9296bb2a98a8 102 int FATFileSystem::remove(const char *filename) {
cha45689 0:9296bb2a98a8 103 FRESULT res = f_unlink(filename);
cha45689 0:9296bb2a98a8 104 if (res) {
cha45689 0:9296bb2a98a8 105 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
cha45689 0:9296bb2a98a8 106 return -1;
cha45689 0:9296bb2a98a8 107 }
cha45689 0:9296bb2a98a8 108 return 0;
cha45689 0:9296bb2a98a8 109 }
cha45689 0:9296bb2a98a8 110
cha45689 0:9296bb2a98a8 111 int FATFileSystem::format() {
cha45689 0:9296bb2a98a8 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
cha45689 0:9296bb2a98a8 113 if (res) {
cha45689 0:9296bb2a98a8 114 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
cha45689 0:9296bb2a98a8 115 return -1;
cha45689 0:9296bb2a98a8 116 }
cha45689 0:9296bb2a98a8 117 return 0;
cha45689 0:9296bb2a98a8 118 }
cha45689 0:9296bb2a98a8 119
cha45689 0:9296bb2a98a8 120 DirHandle *FATFileSystem::opendir(const char *name) {
cha45689 0:9296bb2a98a8 121 FATFS_DIR dir;
cha45689 0:9296bb2a98a8 122 FRESULT res = f_opendir(&dir, name);
cha45689 0:9296bb2a98a8 123 if (res != 0) {
cha45689 0:9296bb2a98a8 124 return NULL;
cha45689 0:9296bb2a98a8 125 }
cha45689 0:9296bb2a98a8 126 return new FATDirHandle(dir);
cha45689 0:9296bb2a98a8 127 }
cha45689 0:9296bb2a98a8 128
cha45689 0:9296bb2a98a8 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
cha45689 0:9296bb2a98a8 130 FRESULT res = f_mkdir(name);
cha45689 0:9296bb2a98a8 131 return res == 0 ? 0 : -1;
cha45689 0:9296bb2a98a8 132 }
cha45689 0:9296bb2a98a8 133