Fork of Adam Green's library with .cpp fix for current compiler

Dependents:   MSCUsbHost mpod_nhk_english mpod_picasa_photoframe mpod_nhk_english_spxml ... more

Fork of FatFileSystem by Adam Green

Committer:
igorsk
Date:
Mon Jul 30 13:45:05 2012 +0000
Revision:
1:88f22c32a456
Parent:
0:6ceefe1c53e4
renamed .c to .cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AdamGreen 0:6ceefe1c53e4 1 /* mbed Microcontroller Library - FATFileSystem
AdamGreen 0:6ceefe1c53e4 2 * Copyright (c) 2008, sford
AdamGreen 0:6ceefe1c53e4 3 */
AdamGreen 0:6ceefe1c53e4 4
AdamGreen 0:6ceefe1c53e4 5 #include "FATFileSystem.h"
AdamGreen 0:6ceefe1c53e4 6
AdamGreen 0:6ceefe1c53e4 7 #include "mbed.h"
AdamGreen 0:6ceefe1c53e4 8
AdamGreen 0:6ceefe1c53e4 9 #include "FileSystemLike.h"
AdamGreen 0:6ceefe1c53e4 10 #include "FATFileHandle.h"
AdamGreen 0:6ceefe1c53e4 11 #include "FATDirHandle.h"
AdamGreen 0:6ceefe1c53e4 12 #include "ff.h"
AdamGreen 0:6ceefe1c53e4 13 //#include "Debug.h"
AdamGreen 0:6ceefe1c53e4 14 #include <stdio.h>
AdamGreen 0:6ceefe1c53e4 15 #include <stdlib.h>
AdamGreen 0:6ceefe1c53e4 16
AdamGreen 0:6ceefe1c53e4 17 DWORD get_fattime (void) {
AdamGreen 0:6ceefe1c53e4 18 return 999;
AdamGreen 0:6ceefe1c53e4 19 }
AdamGreen 0:6ceefe1c53e4 20
AdamGreen 0:6ceefe1c53e4 21 namespace mbed {
AdamGreen 0:6ceefe1c53e4 22
AdamGreen 0:6ceefe1c53e4 23 #if FFSDEBUG_ENABLED
AdamGreen 0:6ceefe1c53e4 24 static const char *FR_ERRORS[] = {
AdamGreen 0:6ceefe1c53e4 25 "FR_OK = 0",
AdamGreen 0:6ceefe1c53e4 26 "FR_NOT_READY",
AdamGreen 0:6ceefe1c53e4 27 "FR_NO_FILE",
AdamGreen 0:6ceefe1c53e4 28 "FR_NO_PATH",
AdamGreen 0:6ceefe1c53e4 29 "FR_INVALID_NAME",
AdamGreen 0:6ceefe1c53e4 30 "FR_INVALID_DRIVE",
AdamGreen 0:6ceefe1c53e4 31 "FR_DENIED",
AdamGreen 0:6ceefe1c53e4 32 "FR_EXIST",
AdamGreen 0:6ceefe1c53e4 33 "FR_RW_ERROR",
AdamGreen 0:6ceefe1c53e4 34 "FR_WRITE_PROTECTED",
AdamGreen 0:6ceefe1c53e4 35 "FR_NOT_ENABLED",
AdamGreen 0:6ceefe1c53e4 36 "FR_NO_FILESYSTEM",
AdamGreen 0:6ceefe1c53e4 37 "FR_INVALID_OBJECT",
AdamGreen 0:6ceefe1c53e4 38 "FR_MKFS_ABORTED"
AdamGreen 0:6ceefe1c53e4 39 };
AdamGreen 0:6ceefe1c53e4 40 #endif
AdamGreen 0:6ceefe1c53e4 41
AdamGreen 0:6ceefe1c53e4 42 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
AdamGreen 0:6ceefe1c53e4 43
AdamGreen 0:6ceefe1c53e4 44 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
AdamGreen 0:6ceefe1c53e4 45 FFSDEBUG("FATFileSystem(%s)\n", n);
AdamGreen 0:6ceefe1c53e4 46 for(int i=0; i<_VOLUMES; i++) {
AdamGreen 0:6ceefe1c53e4 47 if(_ffs[i] == 0) {
AdamGreen 0:6ceefe1c53e4 48 _ffs[i] = this;
AdamGreen 0:6ceefe1c53e4 49 _fsid = i;
AdamGreen 0:6ceefe1c53e4 50 FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
AdamGreen 0:6ceefe1c53e4 51 f_mount(i, &_fs);
AdamGreen 0:6ceefe1c53e4 52 return;
AdamGreen 0:6ceefe1c53e4 53 }
AdamGreen 0:6ceefe1c53e4 54 }
AdamGreen 0:6ceefe1c53e4 55 error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n);
AdamGreen 0:6ceefe1c53e4 56 }
AdamGreen 0:6ceefe1c53e4 57
AdamGreen 0:6ceefe1c53e4 58 FATFileSystem::~FATFileSystem() {
AdamGreen 0:6ceefe1c53e4 59 for(int i=0; i<_VOLUMES; i++) {
AdamGreen 0:6ceefe1c53e4 60 if(_ffs[i] == this) {
AdamGreen 0:6ceefe1c53e4 61 _ffs[i] = 0;
AdamGreen 0:6ceefe1c53e4 62 f_mount(i, NULL);
AdamGreen 0:6ceefe1c53e4 63 }
AdamGreen 0:6ceefe1c53e4 64 }
AdamGreen 0:6ceefe1c53e4 65 }
AdamGreen 0:6ceefe1c53e4 66
AdamGreen 0:6ceefe1c53e4 67 FileHandle *FATFileSystem::open(const char* name, int flags) {
AdamGreen 0:6ceefe1c53e4 68 FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
AdamGreen 0:6ceefe1c53e4 69 char n[64];
AdamGreen 0:6ceefe1c53e4 70 sprintf(n, "%d:/%s", _fsid, name);
AdamGreen 0:6ceefe1c53e4 71
AdamGreen 0:6ceefe1c53e4 72 /* POSIX flags -> FatFS open mode */
AdamGreen 0:6ceefe1c53e4 73 BYTE openmode;
AdamGreen 0:6ceefe1c53e4 74 if(flags & O_RDWR) {
AdamGreen 0:6ceefe1c53e4 75 openmode = FA_READ|FA_WRITE;
AdamGreen 0:6ceefe1c53e4 76 } else if(flags & O_WRONLY) {
AdamGreen 0:6ceefe1c53e4 77 openmode = FA_WRITE;
AdamGreen 0:6ceefe1c53e4 78 } else {
AdamGreen 0:6ceefe1c53e4 79 openmode = FA_READ;
AdamGreen 0:6ceefe1c53e4 80 }
AdamGreen 0:6ceefe1c53e4 81 if(flags & O_CREAT) {
AdamGreen 0:6ceefe1c53e4 82 if(flags & O_TRUNC) {
AdamGreen 0:6ceefe1c53e4 83 openmode |= FA_CREATE_ALWAYS;
AdamGreen 0:6ceefe1c53e4 84 } else {
AdamGreen 0:6ceefe1c53e4 85 openmode |= FA_OPEN_ALWAYS;
AdamGreen 0:6ceefe1c53e4 86 }
AdamGreen 0:6ceefe1c53e4 87 }
AdamGreen 0:6ceefe1c53e4 88
AdamGreen 0:6ceefe1c53e4 89 FIL fh;
AdamGreen 0:6ceefe1c53e4 90 FRESULT res = f_open(&fh, n, openmode);
AdamGreen 0:6ceefe1c53e4 91 if(res) {
AdamGreen 0:6ceefe1c53e4 92 FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 93 return NULL;
AdamGreen 0:6ceefe1c53e4 94 }
AdamGreen 0:6ceefe1c53e4 95 if(flags & O_APPEND) {
AdamGreen 0:6ceefe1c53e4 96 f_lseek(&fh, fh.fsize);
AdamGreen 0:6ceefe1c53e4 97 }
AdamGreen 0:6ceefe1c53e4 98 return new FATFileHandle(fh);
AdamGreen 0:6ceefe1c53e4 99 }
AdamGreen 0:6ceefe1c53e4 100
AdamGreen 0:6ceefe1c53e4 101 int FATFileSystem::remove(const char *filename) {
AdamGreen 0:6ceefe1c53e4 102 FRESULT res = f_unlink(filename);
AdamGreen 0:6ceefe1c53e4 103 if(res) {
AdamGreen 0:6ceefe1c53e4 104 FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 105 return -1;
AdamGreen 0:6ceefe1c53e4 106 }
AdamGreen 0:6ceefe1c53e4 107 return 0;
AdamGreen 0:6ceefe1c53e4 108 }
AdamGreen 0:6ceefe1c53e4 109
AdamGreen 0:6ceefe1c53e4 110 int FATFileSystem::format() {
AdamGreen 0:6ceefe1c53e4 111 FFSDEBUG("format()\n");
AdamGreen 0:6ceefe1c53e4 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
AdamGreen 0:6ceefe1c53e4 113 if(res) {
AdamGreen 0:6ceefe1c53e4 114 FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]);
AdamGreen 0:6ceefe1c53e4 115 return -1;
AdamGreen 0:6ceefe1c53e4 116 }
AdamGreen 0:6ceefe1c53e4 117 return 0;
AdamGreen 0:6ceefe1c53e4 118 }
AdamGreen 0:6ceefe1c53e4 119
AdamGreen 0:6ceefe1c53e4 120 DirHandle *FATFileSystem::opendir(const char *name) {
AdamGreen 0:6ceefe1c53e4 121 FATFS_DIR dir;
AdamGreen 0:6ceefe1c53e4 122 FRESULT res = f_opendir(&dir, name);
AdamGreen 0:6ceefe1c53e4 123 if(res != 0) {
AdamGreen 0:6ceefe1c53e4 124 return NULL;
AdamGreen 0:6ceefe1c53e4 125 }
AdamGreen 0:6ceefe1c53e4 126 return new FATDirHandle(dir);
AdamGreen 0:6ceefe1c53e4 127 }
AdamGreen 0:6ceefe1c53e4 128
AdamGreen 0:6ceefe1c53e4 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
AdamGreen 0:6ceefe1c53e4 130 FRESULT res = f_mkdir(name);
AdamGreen 0:6ceefe1c53e4 131 return res == 0 ? 0 : -1;
AdamGreen 0:6ceefe1c53e4 132 }
AdamGreen 0:6ceefe1c53e4 133
AdamGreen 0:6ceefe1c53e4 134 } // namespace mbed