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