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