-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.c
222 lines (198 loc) · 5.12 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#define _DEFAULT_SOURCE
#include "file.h"
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <libgen.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
FRESULT f_open(FIL* fd, const TCHAR* path, BYTE mode)
{
char *mode_string = NULL;
if (mode & FA_READ) {
mode &= ~FA_READ;
if (mode & FA_WRITE) {
mode &= ~FA_WRITE;
if (mode & FA_CREATE_ALWAYS) {
mode &= ~FA_CREATE_ALWAYS;
mode_string = "w+b";
} else if (mode & FA_OPEN_APPEND) {
mode &= ~FA_OPEN_APPEND;
mode_string = "a+b";
} else if (mode & FA_CREATE_NEW) {
mode &= ~FA_CREATE_NEW;
#ifdef __GLIBC__
mode_string = "w+x";
#else
mode_string = "w+b";
#endif
} else
mode_string = "r+b";
} else
mode_string = "rb";
} else if (mode & FA_WRITE) {
mode &= ~FA_WRITE;
if (mode & FA_CREATE_ALWAYS) {
mode &= ~FA_CREATE_ALWAYS;
mode_string = "wb";
} else if (mode & FA_OPEN_APPEND) {
mode &= ~FA_OPEN_APPEND;
mode_string = "ab";
} else if (mode & FA_CREATE_NEW) {
mode &= ~FA_CREATE_NEW;
#ifdef __GLIBC__
mode_string = "wx";
#else
mode_string = "wb";
#endif
}
// FIXME: Docs imply you can't use write flag alone, verify.
}
if (!mode_string || mode)
return FR_INVALID_PARAMETER;
if ((*fd = fopen(path, mode_string)))
return FR_OK;
// FIXME: Handle a few more of these cases
switch(errno) {
case EACCES:
return FR_DENIED;
case EBUSY:
return FR_LOCKED;
case EEXIST:
return FR_EXIST;
case EINVAL:
return FR_INVALID_PARAMETER;
case ENOENT:
return FR_NO_FILE;
case ENOMEM:
return FR_NOT_ENOUGH_CORE;
default:
fprintf(stderr, "fopen failed with errno %d\n", errno);
return FR_INT_ERR;
}
}
FRESULT f_close(FIL* fd)
{
if (fclose(*fd) == 0)
return FR_OK;
switch(errno) {
case EBADF:
return FR_INVALID_OBJECT;
case EIO:
return FR_DISK_ERR;
default:
return FR_INT_ERR;
}
}
FRESULT f_read(FIL* fd, void* buff, UINT btr, UINT* br)
{
size_t ret = fread(buff, 1, btr, *fd);
if (ret == btr || feof(*fd)) {
*br = ret;
return FR_OK;
}
// FIXME: How do you use ferror... Does it set errno?
return FR_DISK_ERR;
}
FRESULT f_lseek(FIL* fd, FSIZE_T offset)
{
if (fseek(*fd, offset, SEEK_SET) == 0)
return FR_OK;
switch(errno) {
case EINVAL:
return FR_INVALID_OBJECT;
default:
case ESPIPE:
return FR_INT_ERR;
}
}
FRESULT f_write(FIL* fd, const void* buff, UINT btw, UINT* bw)
{
size_t ret = fwrite(buff, 1, btw, *fd);
if (ret == btw) {
*bw = ret;
return FR_OK;
}
// FIXME: See f_read note.
return FR_DISK_ERR;
}
FRESULT f_opendir(DIR* dir, const TCHAR* path)
{
dir = opendir(path);
if (dir)
return FR_OK;
switch(errno) {
default:
case EACCES:
return FR_DENIED;
case EMFILE:
case ENFILE:
return FR_TOO_MANY_OPEN_FILES;
case ENOENT:
return FR_NO_PATH;
case ENOMEM:
return FR_NOT_ENOUGH_CORE;
case ENOTDIR:
// FIXME: What does FatFs actually do for this?
return FR_NO_PATH;
}
}
FRESULT f_closedir(DIR* dir)
{
if (closedir(dir))
return FR_INVALID_OBJECT;
return FR_OK;
}
FRESULT f_readdir(DIR* dir, FILINFO* info)
{
if (!info) {
rewinddir(dir);
return FR_OK;
}
errno = 0;
struct dirent* dirinfo = readdir(dir);
if (!dirinfo) {
switch(errno) {
case 0:
info->fname[0] = '\0';
return FR_OK;
case EBADF:
return FR_INVALID_OBJECT;
default:
return FR_INT_ERR;
}
}
if (strlen(dirinfo->d_name) > ARRAY_LENGTH(info->fname))
return FR_INT_ERR;
int fd = dirfd(dir);
if (fd == -1)
return FR_INT_ERR;
struct stat fileinfo;
if (fstatat(fd, dirinfo->d_name, &fileinfo, 0) == -1) {
// FIXME: Translate some of the valid error codes?
return FR_INT_ERR;
}
struct tm* timeinfo = localtime(&fileinfo.st_mtim.tv_sec);
if (!timeinfo)
return FR_INT_ERR;
info->fsize = fileinfo.st_size;
info->fdate = (MIN(2107, MAX(1980, timeinfo->tm_year - 80)) << 9) |
((timeinfo->tm_mon + 1) << 5) | timeinfo->tm_mday;
info->ftime = (timeinfo->tm_hour << 11) | (timeinfo->tm_min << 5) |
(timeinfo->tm_sec / 2);
info->fattrib = 0;
if (info->fname[0] == '.')
info->fattrib |= AM_HID;
if (faccessat(fd, dirinfo->d_name, W_OK, 0) != 0)
info->fattrib |= AM_RDO;
if (dirinfo->d_type & DT_DIR)
info->fattrib |= AM_DIR;
// FIXME: Should we set AM_SYS/AM_ARC somehow?
strcpy(info->fname, dirinfo->d_name);
return FR_OK;
}