-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibfatx.c
296 lines (281 loc) · 8.7 KB
/
libfatx.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include "libfatx_internal.h"
#include "libfatx.h"
fatx_t
fatx_init(const char * path,
fatx_options_t * options)
{
fatx_handle * fatx = (fatx_handle *) calloc(1, sizeof(fatx_handle));
if(options == NULL)
goto error;
memcpy(&fatx->options, options, sizeof(fatx_options_t));
fatx->options.filePerm &= 0777; // only the file permissions are allowed here.
if((fatx->dev = open(path, O_RDWR)) <= 0)
goto error;
if(pthread_mutexattr_init(&fatx->mutexAttr))
goto error;
if(pthread_mutexattr_settype(&fatx->mutexAttr, PTHREAD_MUTEX_RECURSIVE))
goto error;
if(pthread_mutex_init(&fatx->devLock, &fatx->mutexAttr))
goto error;
fatx->nClusters = fatx_calcClusters(fatx->dev);
fatx->fatType = fatx->nClusters < FATX32_MIN_CLUSTERS ? FATX16 : FATX32;
fatx->dataStart = fatx_calcDataStart(fatx->fatType, fatx->nClusters);
fatx->noFatPages = fatx_calcFatPages(fatx->dataStart);
// Load up the 0'th pages to intialize the caches
fatx_loadFatPage(fatx, 0);
fatx->rootDirEntry.firstCluster = SWAP32(1);
fatx->rootDirEntry.attributes = 0x10;
return (fatx_t) fatx;
error:
pthread_mutex_destroy(&fatx->devLock);
pthread_mutexattr_destroy(&fatx->mutexAttr);
if (fatx->dev) close(fatx->dev);
free(fatx);
return NULL;
}
void
fatx_free(fatx_t fatx)
{
int i = 0;
if (fatx == NULL)
return;
for(i = 0; i < CACHE_SIZE; i++) {
if(fatx->cache[i].dirty)
fatx_flushClusterCacheEntry(fatx, fatx->cache + i);
}
for(i = 0; i < FAT_CACHE_SIZE; i++) {
if(fatx->fatCache[i].dirty)
fatx_flushFatCacheEntry(fatx, fatx->fatCache + i);
}
pthread_mutex_destroy(&fatx->devLock);
pthread_mutexattr_destroy(&fatx->mutexAttr);
close(fatx->dev);
free(fatx);
}
void
fatx_printInfo(fatx_t fatx)
{
fatx_handle * fatx_h = (fatx_handle *) fatx;
printf("Fatx handle:\n");
printf("\tdev fd = %d\n", fatx_h->dev);
printf("\tnClusters = %u\n", fatx_h->nClusters);
printf("\tfatType = %d\n", fatx_h->fatType);
printf("\tdataStart = 0x%lx\n", fatx_h->dataStart);
printf("\tnoFatPages = 0x%x\n", fatx_h->noFatPages);
}
int
fatx_read(fatx_t fatx,
const char* path,
char* buf,
off_t offset,
size_t size)
{
fatx_directory_entry * directoryEntry;
fatx_filename_list * fnList = NULL;
int retVal = 0;
fnList = fatx_splitPath(path);
if(fnList == NULL) {
retVal = -ENOENT;
goto finish;
}
FATX_LOCK(fatx);
directoryEntry = fatx_findDirectoryEntry(fatx, fnList, &fatx->rootDirEntry);
if(directoryEntry == NULL) {
retVal = -ENOENT;
goto finish;
}
retVal = fatx_readFromDirectoryEntry(fatx, directoryEntry, buf, offset, size);
finish:
FATX_UNLOCK(fatx);
fatx_freeFilenameList(fnList);
return retVal;
}
int
fatx_write(fatx_t fatx,
const char* path,
const char* buf,
off_t offset,
size_t size)
{
fatx_directory_entry * directoryEntry;
fatx_filename_list * fnList = NULL;
int retVal = 0;
fnList = fatx_splitPath(path);
if(fnList == NULL) {
// Writing into the root directory isn't possible.
retVal = -ENOENT;
goto finish;
}
FATX_LOCK(fatx);
directoryEntry = fatx_findDirectoryEntry(fatx, fnList, &fatx->rootDirEntry);
if(directoryEntry == NULL) {
retVal = -ENOENT;
goto finish;
}
retVal = fatx_writeToDirectoryEntry(fatx, directoryEntry, buf, offset, size);
finish:
FATX_UNLOCK(fatx);
fatx_freeFilenameList(fnList);
return retVal;
}
int
fatx_stat(fatx_t fatx,
const char* path,
struct stat* st_buf)
{
fatx_directory_entry * directoryEntry;
fatx_filename_list * fnList;
int err = 0;
FATX_LOCK(fatx);
fnList = fatx_splitPath(path);
if(fnList == NULL) {
// Root directory.
st_buf->st_mode = S_IFDIR | fatx->options.filePerm;
st_buf->st_nlink = 1;
st_buf->st_size = 0;
st_buf->st_uid = fatx->options.user;
st_buf->st_gid = fatx->options.group;
} else {
directoryEntry = fatx_findDirectoryEntry(fatx, fnList, &fatx->rootDirEntry);
fatx_freeFilenameList(fnList);
if(directoryEntry == NULL) {
err = -ENOENT;
goto finish;
}
st_buf->st_mode = IS_FOLDER(directoryEntry) ? S_IFDIR : S_IFREG;
st_buf->st_mode |= fatx->options.filePerm;
st_buf->st_nlink = 1;
st_buf->st_size = SWAP32(directoryEntry->fileSize);
st_buf->st_mtime = fatx_makeTimeType(SWAP16(directoryEntry->modificationDate),
SWAP16(directoryEntry->modificationTime));
st_buf->st_atime = fatx_makeTimeType(SWAP16(directoryEntry->accessDate),
SWAP16(directoryEntry->accessTime));
st_buf->st_uid = fatx->options.user;
st_buf->st_gid = fatx->options.group;
}
finish:
FATX_UNLOCK(fatx);
return 0;
}
int
fatx_remove(fatx_t fatx,
const char* path)
{
return 0;
}
int
fatx_mkfile(fatx_t fatx,
const char* path)
{
int err = 0;
uint32_t newFileCluster = 0;
fatx_directory_entry * folder = &fatx->rootDirEntry;
fatx_directory_entry * newFile = NULL;
fatx_filename_list * splitPath = fatx_splitPath(path);
fatx_filename_list * dirname = fatx_dirname(splitPath);
fatx_filename_list * basename = fatx_basename(splitPath);
FATX_LOCK(fatx);
if(dirname != NULL) {
// dirname isn't null so need to search for the folder
folder = fatx_findDirectoryEntry(fatx, dirname, &fatx->rootDirEntry);
if(folder == NULL) {
err = -ENOENT;
goto finish;
}
}
if (fatx_findDirectoryEntry(fatx, basename, folder) != NULL) {
// See if the file already exists.
err = -ENOENT;
goto finish;
}
newFile = fatx_getFirstOpenDirectoryEntry(fatx, folder);
newFileCluster = fatx_findFreeCluster(fatx, SWAP32(folder->firstCluster));
memset(newFile, 0, sizeof(fatx_directory_entry));
newFile->filenameSz = strlen(basename->filename);
memcpy(newFile->filename, basename->filename, 42);
newFile->firstCluster = SWAP32(newFileCluster);
fatx_writeFatEntry(fatx, newFileCluster,
fatx->fatType == FATX32 ? SWAP32(0xFFFFFFF8) : SWAP16(0xFFF8));
finish:
FATX_UNLOCK(fatx);
fatx_freeFilenameList(splitPath);
fatx_freeFilenameList(dirname);
fatx_freeFilenameList(basename);
return err;
}
int
fatx_mkdir(fatx_t fatx,
const char* path)
{
return 0;
}
fatx_dir_iter_t
fatx_opendir(fatx_t fatx,
const char* path)
{
fatx_dir_iter * dirIter = NULL;
fatx_directory_entry * directoryEntry = NULL;
fatx_filename_list * fnList = NULL;
FATX_LOCK(fatx);
fnList = fatx_splitPath(path);
if(fnList == NULL) {
dirIter = fatx_createDirIter(fatx, NULL);
goto finish;
}
directoryEntry = fatx_findDirectoryEntry(fatx, fnList, directoryEntry);
if (directoryEntry == NULL) goto finish;
dirIter = fatx_createDirIter(fatx, directoryEntry);
fatx_freeFilenameList(fnList);
finish:
FATX_UNLOCK(fatx);
return (fatx_dir_iter_t) dirIter;
}
fatx_dirent_t *
fatx_readdir(fatx_dir_iter_t iter)
{
fatx_dirent_t * dirent = NULL;
fatx_directory_entry * directoryEntry = NULL;
fatx_dirent_list * direntList = NULL;
if (iter == NULL) return NULL;
FATX_LOCK(iter->fatx_h);
directoryEntry = fatx_readDirectoryEntry(iter->fatx_h, iter);
if(directoryEntry == NULL) goto finish;
if(!IS_VALID_ENTRY(directoryEntry)) {
dirent = fatx_readdir(iter);
goto finish;
}
dirent = (fatx_dirent_t *) malloc(sizeof(fatx_dirent_t));
dirent->d_namelen = directoryEntry->filenameSz;
dirent->d_name = (char *) malloc(dirent->d_namelen + 1);
strncpy(dirent->d_name, directoryEntry->filename, dirent->d_namelen);
dirent->d_name[dirent->d_namelen] = '\0';
direntList = (fatx_dirent_list *) malloc(sizeof(fatx_dirent_list));
direntList->dirEnt = dirent;
direntList->next = iter->dirEntList;
finish:
FATX_UNLOCK(iter->fatx_h);
return dirent;
}
void
fatx_closedir(fatx_dir_iter_t iter)
{
fatx_dir_iter * dirIter = (fatx_dir_iter *) iter;
fatx_dirent_list* nextDirEntList;
if(iter == NULL) return;
for(;iter->dirEntList != NULL; iter->dirEntList = nextDirEntList) {
nextDirEntList = iter->dirEntList->next;
free(iter->dirEntList->dirEnt->d_name);
free(iter->dirEntList->dirEnt);
free(iter->dirEntList);
}
free(dirIter);
}