-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacorn-fs.c
346 lines (317 loc) · 10.2 KB
/
acorn-fs.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "acorn-fs.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <fcntl.h>
#endif
static acorn_fs *open_list;
static int check_adfs(FILE *fp, unsigned off1, unsigned off2, const char *pattern, size_t len)
{
char id1[10], id2[10];
if (fseek(fp, off1, SEEK_SET))
return errno;
if (fread(id1, len, 1, fp) != 1)
return ferror(fp) ? errno : AFS_BAD_EOF;
if (memcmp(id1+1, pattern, len-1))
return AFS_NOT_ACORN;
if (fseek(fp, off2, SEEK_SET))
return errno;
if (fread(id2, len, 1, fp) != 1)
return ferror(fp) ? errno : AFS_BAD_EOF;
if (memcmp(id1, id2, len))
return AFS_BROKEN_DIR;
return AFS_OK;
}
static int rdsect_simple(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
if (fseek(fs->fp, ssect * ACORN_FS_SECT_SIZE, SEEK_SET))
return errno;
if (fread(buf, size, 1, fs->fp) != 1)
return ferror(fs->fp) ? errno : AFS_BAD_EOF;
return AFS_OK;
}
static int wrsect_simple(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
if (fseek(fs->fp, ssect * ACORN_FS_SECT_SIZE, SEEK_SET))
return errno;
if (fwrite(buf, size, 1, fs->fp) != 1)
return errno;
return AFS_OK;
}
static int rdsect_ide(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
unsigned char tbuf[2*ACORN_FS_SECT_SIZE];
if (fseek(fs->fp, ssect * ACORN_FS_SECT_SIZE * 2, SEEK_SET))
return errno;
while (size) {
unsigned chunk = size;
if (chunk > ACORN_FS_SECT_SIZE)
chunk = ACORN_FS_SECT_SIZE;
if (fread(tbuf, chunk * 2, 1, fs->fp) == 1) {
unsigned char *ptr = tbuf;
unsigned char *end = buf + chunk;
while (buf < end) {
*buf++ = *ptr;
ptr += 2;
}
size -= chunk;
}
else
return ferror(fs->fp) ? errno : AFS_BAD_EOF;
}
return AFS_OK;
}
static int wrsect_ide(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
unsigned char tbuf[2*ACORN_FS_SECT_SIZE];
if (fseek(fs->fp, ssect * ACORN_FS_SECT_SIZE * 2, SEEK_SET))
return errno;
while (size) {
unsigned chunk = size;
if (chunk > ACORN_FS_SECT_SIZE)
chunk = ACORN_FS_SECT_SIZE;
unsigned char *ptr = tbuf;
unsigned char *end = buf + chunk;
while (buf < end) {
*ptr++ = *buf++;
*ptr++ = 0;
}
if (fwrite(tbuf, chunk * 2, 1, fs->fp) != 1)
return errno;
size -= chunk;
}
return AFS_OK;
}
static int ileave_seek(acorn_fs *fs, int ssect, int sect_per_track)
{
int track = ssect / sect_per_track;
int sector = ssect % sect_per_track;
if (track >= 80)
sector += (((track - 80) * 2 + 1) * sect_per_track);
else
sector += track * 2 * sect_per_track;
return fseek(fs->fp, sector * ACORN_FS_SECT_SIZE, SEEK_SET);
}
typedef size_t (*cb_type)(void *ptr, size_t size, size_t nmemb, FILE *fp);
static int interleaved(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size, int sect_per_track, cb_type callback)
{
while (size > ACORN_FS_SECT_SIZE) {
if (ileave_seek(fs, ssect, sect_per_track))
return errno;
if (callback(buf, ACORN_FS_SECT_SIZE, 1, fs->fp) != 1)
return ferror(fs->fp) ? errno : AFS_BAD_EOF;
ssect++;
buf += ACORN_FS_SECT_SIZE;
size -= ACORN_FS_SECT_SIZE;
}
if (size > 0) {
if (ileave_seek(fs, ssect, sect_per_track))
return errno;
if (callback(buf, size, 1, fs->fp) != 1)
return ferror(fs->fp) ? errno : AFS_BAD_EOF;
}
return AFS_OK;
}
static int rdsect_ileave16(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
return interleaved(fs, ssect, buf, size, 16, (cb_type)fread);
}
static int wrsect_ileave16(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
return interleaved(fs, ssect, buf, size, 16, (cb_type)fwrite);
}
static int rdsect_ileave10(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
return interleaved(fs, ssect, buf, size, 10, (cb_type)fread);
}
static int wrsect_ileave10(acorn_fs *fs, int ssect, unsigned char *buf, unsigned size)
{
return interleaved(fs, ssect, buf, size, 10, (cb_type)fwrite);
}
static int lock_file(FILE *fp, bool writable)
{
#ifdef WIN32
return AFS_OK
#else
struct flock fl;
int fd = fileno(fp);
fl.l_type = writable ? F_WRLCK : F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = 0;
do {
if (!fcntl(fd, F_SETLKW, &fl))
return AFS_OK;
} while (errno == EINTR);
return errno;
#endif
}
static void init_link(acorn_fs *fs, FILE *fp, const char *filename)
{
fs->fp = fp;
strcpy(fs->filename, filename);
fs->next = open_list;
open_list = fs;
}
acorn_fs *acorn_fs_open(const char *filename, bool writable)
{
for (acorn_fs *fs = open_list; fs; fs = fs->next)
if (!strcmp(fs->filename, filename))
return fs;
acorn_fs *fs = malloc(sizeof(acorn_fs) + strlen(filename));
if (fs) {
const char *mode = writable ? "rb+" : "rb";
FILE *fp = fopen(filename, mode);
if (fp) {
int status = lock_file(fp, writable);
if (status == AFS_OK) {
if ((status = check_adfs(fp, 0x200, 0x6fa, "Hugo", 5)) == AFS_OK) {
const char *ext = strrchr(filename, '.');
if (ext && !strcasecmp(ext, ".adl")) {
fs->rdsect = rdsect_ileave16;
fs->wrsect = wrsect_ileave16;
}
else {
fs->rdsect = rdsect_simple;
fs->wrsect = wrsect_simple;
}
acorn_fs_adfs_init(fs);
init_link(fs, fp, filename);
return fs;
}
else if (status == AFS_NOT_ACORN) {
if ((status = check_adfs(fp, 0x400, 0xdf4, "\0H\0u\0g\0o", 10)) == AFS_OK) {
fs->rdsect = rdsect_ide;
fs->wrsect = wrsect_ide;
acorn_fs_adfs_init(fs);
init_link(fs, fp, filename);
return fs;
}
}
if (status == AFS_NOT_ACORN || status == AFS_BAD_EOF) {
unsigned char *dir = malloc(0x200);
if (dir) {
if (fseek(fp, 0L, SEEK_SET) == 0) {
if (fread(dir, 0x200, 1, fp) == 1) {
fs->priv = dir;
if (acorn_fs_dfs_check(fs, NULL, NULL) == AFS_OK) {
const char *ext = strrchr(filename, '.');
if (ext && !strcasecmp(ext, ".dsd")) {
fs->rdsect = rdsect_ileave10;
fs->wrsect = wrsect_ileave10;
}
else {
fs->rdsect = rdsect_simple;
fs->wrsect = wrsect_simple;
}
acorn_fs_dfs_init(fs);
init_link(fs, fp, filename);
return fs;
}
}
else if (!ferror(fp))
errno = AFS_BAD_EOF;
}
}
}
}
fclose(fp);
errno = status;
}
free(fs);
}
return NULL;
}
static int close_fs(acorn_fs *fs)
{
int status = AFS_OK;
if (fs->priv)
free(fs->priv);
if (fs->fp)
if (fclose(fs->fp))
status = errno;
free(fs);
return status;
}
int acorn_fs_close(acorn_fs *fs)
{
if (fs == open_list) {
open_list = fs->next;
return close_fs(fs);
}
else {
acorn_fs *prev = open_list;
acorn_fs *cur = prev->next;
while (cur) {
if (fs == cur) {
prev->next = cur->next;
return close_fs(fs);
}
prev = cur;
cur = cur->next;
}
}
return AFS_OK;
}
int acorn_fs_close_all(void)
{
int status = AFS_OK;
acorn_fs *ent = open_list;
while (ent) {
acorn_fs *next = ent->next;
int result = close_fs(ent);
if (result != AFS_OK)
status = result;
ent = next;
}
open_list = NULL;
return status;
}
static const char *msgs[] = {
/* AFS_BAD_EOF */ "Unexpected EOF on disc image",
/* AFS_NOT_ACORN */ "Not a recognised Acorn filing system",
/* AFS_BROKEN_DIR */ "Broken directory",
/* AFS_BAD_FSMAP */ "Bad free space map",
/* AFS_BUG */ "Bug in Acorn FS utils",
/* AFS_MAP_FULL */ "Free space map full",
/* AFS_DIR_FULL */ "Directory full",
/* AFS_CORRUPT */ "Filesystem is corrupt",
/* AFS_REMOVED */ "Directory entry removed"
};
const char *acorn_fs_strerr(int status)
{
if (status >= 0)
return strerror(status);
else {
status = -1 - status;
if (status < sizeof(msgs)/sizeof(char *))
return msgs[status];
else
return "Unknown error";
}
}
void acorn_fs_free_obj(acorn_fs_object *obj)
{
if (obj->data) {
free(obj->data);
obj->data = NULL;
}
}
int acorn_fs_info(acorn_fs_object *obj, FILE *fp)
{
char str[9];
memset(str, '-', 9);
unsigned a = obj->attr;
if (a & AFS_ATTR_DIR) str[0] = 'D';
if (a & AFS_ATTR_LOCKED) str[1] = 'L';
if (a & AFS_ATTR_UREAD) str[2] = 'R';
if (a & AFS_ATTR_UWRITE) str[3] = 'W';
if (a & AFS_ATTR_UEXEC) str[4] = 'E';
if (a & AFS_ATTR_OREAD) str[5] = 'r';
if (a & AFS_ATTR_OWRITE) str[6] = 'w';
if (a & AFS_ATTR_OEXEC) str[7] = 'e';
if (a & AFS_ATTR_PRIV) str[8] = 'P';
return fprintf(fp, "%.9s %08X %08X %'10d %06X", str, obj->load_addr, obj->exec_addr, obj->length, obj->sector);
}