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