forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsdb.cpp
396 lines (359 loc) · 8.61 KB
/
fsdb.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* UAE - The Un*x Amiga Emulator
*
* Library of functions to make emulated filesystem as independent as
* possible of the host filesystem's capabilities.
*
* Copyright 1999 Bernd Schmidt
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "uae.h"
#include "traps.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "filesys.h"
#include "autoconf.h"
#include "fsusage.h"
#include "scsidev.h"
#include "fsdb.h"
#include "uae/io.h"
/* The on-disk format is as follows:
* Offset 0, 1 byte, valid
* Offset 1, 4 bytes, mode
* Offset 5, 257 bytes, aname
* Offset 263, 257 bytes, nname
* Offset 519, 81 bytes, comment
*/
#define TRACING_ENABLED 0
#if TRACING_ENABLED
#define TRACE(x) do { write_log x; } while(0)
#else
#define TRACE(x)
#endif
TCHAR *nname_begin (TCHAR *nname)
{
TCHAR *p = _tcsrchr (nname, FSDB_DIR_SEPARATOR);
if (p)
return p + 1;
return nname;
}
#ifndef _WIN32
/* Find the name REL in directory DIRNAME. If we find a file that
* has exactly the same name, return REL. If we find a file that
* has the same name when compared case-insensitively, return a
* malloced string that contains the name we found. If no file
* exists that compares equal to REL, return 0. */
TCHAR *fsdb_search_dir (const TCHAR *dirname, TCHAR *rel)
{
TCHAR *p = 0;
int de;
my_opendir_s *dir;
TCHAR fn[MAX_DPATH];
dir = my_opendir (dirname);
/* This really shouldn't happen... */
if (! dir)
return 0;
while (p == 0 && (de = my_readdir (dir, fn)) != 0) {
if (strcmp (fn, rel) == 0)
p = rel;
else if (strcasecmp (fn, rel) == 0)
p = my_strdup (fn);
}
my_closedir (dir);
return p;
}
#endif
static FILE *get_fsdb (a_inode *dir, const TCHAR *mode)
{
TCHAR *n;
FILE *f;
if (!dir->nname)
return NULL;
n = build_nname (dir->nname, FSDB_FILE);
f = uae_tfopen (n, mode);
xfree (n);
return f;
}
static void kill_fsdb (a_inode *dir)
{
if (!dir->nname)
return;
TCHAR *n = build_nname (dir->nname, FSDB_FILE);
_wunlink (n);
xfree (n);
}
static void fsdb_fixup (FILE *f, uae_u8 *buf, int size, a_inode *base)
{
TCHAR *nname;
int ret;
if (buf[0] == 0)
return;
TCHAR *fnname = au ((char*)buf + 5 + 257);
nname = build_nname (base->nname, fnname);
xfree (fnname);
ret = fsdb_exists (nname);
if (ret) {
xfree (nname);
return;
}
TRACE ((_T("uaefsdb '%s' deleted\n"), nname));
/* someone deleted this file/dir outside of emulation.. */
buf[0] = 0;
xfree (nname);
}
/* Prune the db file the first time this directory is opened in a session. */
void fsdb_clean_dir (a_inode *dir)
{
uae_u8 buf[1 + 4 + 257 + 257 + 81];
TCHAR *n;
FILE *f;
off_t pos1 = 0, pos2;
if (!dir->nname)
return;
n = build_nname (dir->nname, FSDB_FILE);
f = uae_tfopen (n, _T("r+b"));
if (f == 0) {
xfree (n);
return;
}
for (;;) {
pos2 = ftell (f);
if (fread (buf, 1, sizeof buf, f) < sizeof buf)
break;
fsdb_fixup (f, buf, sizeof buf, dir);
if (buf[0] == 0)
continue;
if (pos1 != pos2) {
fseek (f, pos1, SEEK_SET);
fwrite (buf, 1, sizeof buf, f);
fseek (f, pos2 + sizeof buf, SEEK_SET);
}
pos1 += sizeof buf;
}
fclose (f);
if (pos1 == 0) {
kill_fsdb (dir);
} else {
my_truncate (n, pos1);
}
xfree (n);
}
static a_inode *aino_from_buf (a_inode *base, uae_u8 *buf, long off)
{
uae_u32 mode;
a_inode *aino = xcalloc (a_inode, 1);
TCHAR *s;
mode = do_get_mem_long ((uae_u32 *)(buf + 1));
buf += 5;
aino->aname = au ((char*)buf);
buf += 257;
s = au ((char*)buf);
aino->nname = build_nname (base->nname, s);
xfree (s);
buf += 257;
aino->comment = *buf != '\0' ? au ((char*)buf) : 0;
fsdb_fill_file_attrs (base, aino);
aino->amigaos_mode = mode;
aino->has_dbentry = 1;
aino->dirty = 0;
aino->db_offset = off;
return aino;
}
a_inode *fsdb_lookup_aino_aname (a_inode *base, const TCHAR *aname)
{
FILE *f;
f = get_fsdb (base, _T("r+b"));
if (f == 0) {
if (currprefs.filesys_custom_uaefsdb && (base->volflags & MYVOLUMEINFO_STREAMS))
return custom_fsdb_lookup_aino_aname (base, aname);
return 0;
}
for (;;) {
uae_u8 buf[1 + 4 + 257 + 257 + 81];
TCHAR *s;
if (fread (buf, 1, sizeof buf, f) < sizeof buf)
break;
s = au ((char*)buf + 5);
if (buf[0] != 0 && same_aname (s, aname)) {
long pos = ftell (f) - sizeof buf;
fclose (f);
xfree (s);
return aino_from_buf (base, buf, pos);
}
xfree (s);
}
fclose (f);
return 0;
}
a_inode *fsdb_lookup_aino_nname (a_inode *base, const TCHAR *nname)
{
FILE *f;
char *s;
f = get_fsdb (base, _T("r+b"));
if (f == 0) {
if (currprefs.filesys_custom_uaefsdb && (base->volflags & MYVOLUMEINFO_STREAMS))
return custom_fsdb_lookup_aino_nname (base, nname);
return 0;
}
s = ua (nname);
for (;;) {
uae_u8 buf[1 + 4 + 257 + 257 + 81];
if (fread (buf, 1, sizeof buf, f) < sizeof buf)
break;
if (buf[0] != 0 && strcmp ((char*)buf + 5 + 257, s) == 0) {
long pos = ftell (f) - sizeof buf;
fclose (f);
xfree (s);
return aino_from_buf (base, buf, pos);
}
}
xfree (s);
fclose (f);
return 0;
}
int fsdb_used_as_nname (a_inode *base, const TCHAR *nname)
{
FILE *f;
uae_u8 buf[1 + 4 + 257 + 257 + 81];
f = get_fsdb (base, _T("r+b"));
if (f == 0) {
if (currprefs.filesys_custom_uaefsdb && (base->volflags & MYVOLUMEINFO_STREAMS))
return custom_fsdb_used_as_nname (base, nname);
return 0;
}
for (;;) {
TCHAR *s;
if (fread (buf, 1, sizeof buf, f) < sizeof buf)
break;
if (buf[0] == 0)
continue;
s = au ((char*)buf + 5 + 257);
if (_tcscmp (s, nname) == 0) {
xfree (s);
fclose (f);
return 1;
}
xfree (s);
}
fclose (f);
return 0;
}
static int needs_dbentry (a_inode *aino)
{
const TCHAR *nn_begin;
if (aino->deleted)
return 0;
if (! fsdb_mode_representable_p (aino, aino->amigaos_mode) || aino->comment != 0)
return 1;
nn_begin = nname_begin (aino->nname);
return _tcscmp (nn_begin, aino->aname) != 0;
}
static void write_aino (FILE *f, a_inode *aino)
{
uae_u8 buf[1 + 4 + 257 + 257 + 81] = { 0 };
buf[0] = aino->needs_dbentry ? 1 : 0;
do_put_mem_long ((uae_u32 *)(buf + 1), aino->amigaos_mode);
ua_copy ((char*)buf + 5, 256, aino->aname);
buf[5 + 256] = '\0';
ua_copy ((char*)buf + 5 + 257, 256, nname_begin (aino->nname));
buf[5 + 257 + 256] = '\0';
ua_copy ((char*)buf + 5 + 2 * 257, 80, aino->comment ? aino->comment : _T(""));
buf[5 + 2 * 257 + 80] = '\0';
aino->db_offset = ftell (f);
fwrite (buf, 1, sizeof buf, f);
aino->has_dbentry = aino->needs_dbentry;
TRACE ((_T("%d '%s' '%s' written\n"), aino->db_offset, aino->aname, aino->nname));
}
/* Write back the db file for a directory. */
void fsdb_dir_writeback (a_inode *dir)
{
FILE *f;
int changes_needed = 0;
int entries_needed = 0;
a_inode *aino;
uae_u8 *tmpbuf;
int size, i;
TRACE ((_T("fsdb writeback %s\n"), dir->aname));
/* First pass: clear dirty bits where unnecessary, and see if any work
* needs to be done. */
for (aino = dir->child; aino; aino = aino->sibling) {
/*
int old_needs_dbentry = aino->needs_dbentry || aino->has_dbentry;
aino->needs_dbentry = needs_dbentry (aino);
entries_needed |= aino->has_dbentry | aino->needs_dbentry;
*/
int old_needs_dbentry = aino->has_dbentry;
int need = needs_dbentry (aino);
aino->needs_dbentry = need;
entries_needed |= need;
if (! aino->dirty)
continue;
if (! aino->needs_dbentry && ! old_needs_dbentry)
aino->dirty = 0;
else
changes_needed = 1;
}
if (! entries_needed) {
kill_fsdb (dir);
TRACE ((_T("fsdb removed\n")));
return;
}
if (! changes_needed) {
TRACE ((_T("not modified\n")));
return;
}
f = get_fsdb (dir, _T("r+b"));
if (f == 0) {
if ((currprefs.filesys_custom_uaefsdb && (dir->volflags & MYVOLUMEINFO_STREAMS)) || currprefs.filesys_no_uaefsdb) {
for (aino = dir->child; aino; aino = aino->sibling) {
aino->dirty = 0;
aino->has_dbentry = 0;
aino->needs_dbentry = 0;
}
return;
}
f = get_fsdb (dir, _T("w+b"));
if (f == 0) {
TRACE ((_T("failed\n")));
/* This shouldn't happen... */
return;
}
}
fseek (f, 0, SEEK_END);
size = ftell (f);
fseek (f, 0, SEEK_SET);
tmpbuf = 0;
if (size > 0) {
tmpbuf = (uae_u8*)malloc (size);
fread (tmpbuf, 1, size, f);
}
TRACE ((_T("**** updating '%s' %d\n"), dir->aname, size));
for (aino = dir->child; aino; aino = aino->sibling) {
if (! aino->dirty)
continue;
aino->dirty = 0;
i = 0;
while (!aino->has_dbentry && i < size) {
TCHAR *s = au ((char*)tmpbuf + i + 5);
if (!_tcscmp (s, aino->aname)) {
aino->has_dbentry = 1;
aino->db_offset = i;
}
xfree (s);
i += 1 + 4 + 257 + 257 + 81;
}
if (! aino->has_dbentry) {
fseek (f, 0, SEEK_END);
aino->has_dbentry = 1;
} else {
fseek (f, aino->db_offset, SEEK_SET);
}
write_aino (f, aino);
}
TRACE ((_T("end\n")));
fclose (f);
xfree (tmpbuf);
}