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