aboutsummaryrefslogtreecommitdiff
path: root/Src/nde/android/Vfs.cpp
blob: 7391576a0f830b6db106c59a73a6213cacc44325 (plain) (blame)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

/* ---------------------------------------------------------------------------
Nullsoft Database Engine
--------------------
codename: Near Death Experience
--------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------

Virtual File System

--------------------------------------------------------------------------- */
#include "../nde.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Vfs.h"

#ifndef EOF
#define EOF -1
#endif

/* the FILE (fopen/fwrite/etc) implementation for Mac and Linux */



VFILE *Vfnew(const char *fl, const char *mode, int Cached)
{
	if (!fl) return NULL;
	VFILE *f = (VFILE *)malloc(sizeof(VFILE));
	if (!f)
		return NULL;
	memset(f, 0, sizeof(VFILE));
#ifdef NDE_ALLOW_NONCACHED
	f->cached = Cached;
#else
	f->cached = 1;
#endif

	if (!strchr(mode, '+'))
	{
		if (strchr(mode, 'r'))
			f->mode = VFS_READ | VFS_MUSTEXIST;
		if (strchr(mode, 'w'))
			f->mode = VFS_WRITE | VFS_CREATE | VFS_NEWCONTENT;
		if (strchr(mode, 'a'))
			f->mode = VFS_WRITE | VFS_CREATE | VFS_SEEKEOF;
	}
	else
	{
		if (strstr(mode, "r+"))
			f->mode = VFS_WRITE | VFS_MUSTEXIST;
		if (strstr(mode, "w+"))
			f->mode = VFS_WRITE | VFS_CREATE | VFS_NEWCONTENT;
		if (strstr(mode, "a+"))
			f->mode = VFS_WRITE | VFS_CREATE | VFS_SEEKEOF;
	}

	if (f->mode == 0 || ((f->mode & VFS_READ) && (f->mode & VFS_WRITE)))
	{
		free(f);
		return NULL;
	}

	snprintf(f->lockname, sizeof(f->lockname), "%s.lock", fl);

	return f;
}

//----------------------------------------------------------------------------
VFILE *Vfopen(VFILE *f, const char *fl, const char *mode, int Cached)
{
	if (!f)
	{
		f = Vfnew(fl, mode, Cached);
		if (!f)
			return 0;
	}
	if (!f && !fl) return NULL;

#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		f->rfile = fopen(fl, mode);
		if (f->rfile)

			f->filename = _strdup(fl);
		}
		else
		{
			free(f);
			return NULL;
		}
		return f;
	}
#endif

	if (f->mode & VFS_MUSTEXIST)
	{
		if (access(fl, 0) != 0)
		{
			free(f);
			return NULL;
		}
	}

	if (!(f->mode & VFS_NEWCONTENT))
	{
		FILE *pf;
		pf = fopen(fl, "rb");
		if (!pf)
		{
			f->data = (unsigned char *)calloc(VFILE_INC, 1);
			if (f->data == NULL)
			{
				free(f);
				return NULL;
			}
			f->filesize = 0;
			f->maxsize = VFILE_INC;
		}
		else
		{
			fseek(pf, 0, SEEK_END);
			f->filesize = ftell(pf);
			fseek(pf, 0, SEEK_SET);
			f->data = (unsigned char *)calloc(f->filesize, 1);
			if (f->data == NULL)
			{
				free(f);
				return NULL;
			}
			f->maxsize = f->filesize;
			fread(f->data, f->filesize, 1, pf);
			fclose(pf);
		}
	}

	if (f->mode & VFS_SEEKEOF)
		f->ptr = f->filesize;

	f->filename = strdup(fl);
	return f;
}

//----------------------------------------------------------------------------
void Vfclose(VFILE *f)
{
	if (!f) return;

#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		free(f->filename);
		if (f->rfile)
			fclose(f->rfile);
		f->rfile=0;
//		free(f);
		return;
	}
#endif

	if (!(f->mode & VFS_WRITE))
	{
		free(f->filename);
		free(f->data);
		//free(f);
		return;
	}


	Vsync(f);
	while (f->locks)
		Vfunlock(f, 1);

	free(f->filename);
	free(f->data);
	//free(f);
}

//----------------------------------------------------------------------------
size_t Vfread(void *ptr, size_t size, VFILE *f)
{
	if (!ptr || !f) return 0;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		return fread(ptr, 1, size, f->rfile);
	}
#endif
	size_t s = size;
	if (!s) return 0;
	if (s + f->ptr > f->filesize)
	{
		//FUCKO: remove this
		if (!(f->ptr < f->filesize))
		{
			//      if (!f->flushtable) // this would be ideal, if we could figure out f->flushtable
			//      f->flushtable=MessageBox(g_hwnd,"DB read failed, DB may be corrupted.\r\n\r\n"
			//    "Hit Retry to continue, or Cancel to clear the DB and start over","Winamp Library Error",MB_RETRYCANCEL) == IDCANCEL; //fucko
			//MessageBox(g_hwnd,"DB read failed, DB may be corrupted. If this error persists, remove all files from the library.",
			//        "Winamp Library Error",MB_OK);
			return 0;
		}
		s = f->filesize - f->ptr;
	}
	memcpy(ptr, f->data+f->ptr, s);
	f->ptr += s;
	return (s/size);
}

//----------------------------------------------------------------------------
void Vfwrite(const void *ptr, size_t size, VFILE *f)
{
	if (!ptr || !f) return;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		fwrite(ptr, size*n,  f->rfile);
		return;
	}
#endif
	f->dirty=1;
	if (size + f->ptr > f->maxsize)
	{
		// grow f->data,f->maxsize to be (size + f->ptr + VFILE_INC-1)&~(VFILE_INC-1)
		// instead of calling Vgrow again which gets kinda slow
		size_t newsize=(size + f->ptr + VFILE_INC-1)&~(VFILE_INC-1);
		f->data=(unsigned char *)realloc(f->data,newsize);
		if (f->data == NULL) return;
		memset(f->data+f->maxsize,0,newsize-f->maxsize);
		f->maxsize=newsize;
	}
	memcpy(f->data + f->ptr, ptr, size);
	f->ptr += size;
	if (f->ptr > f->filesize)
		f->filesize = f->ptr;
}

//----------------------------------------------------------------------------
void Vgrow(VFILE *f)
{
	if (!f) return;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached) return;
#endif
	f->data = (unsigned char *)realloc(f->data, f->maxsize + VFILE_INC);
	f->maxsize += VFILE_INC;
}

//----------------------------------------------------------------------------
void Vfputs(char *str, VFILE *f)
{
	if (!f) return;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		fputs(str, f->rfile);
		return;
	}
#endif
	Vfwrite(str, strlen(str), f);
}

//----------------------------------------------------------------------------
void Vfputc(char c, VFILE *f)
{
	if (!f) return;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		fputc(c, f->rfile);
		return;
	}
#endif
	Vfwrite(&c, 1, f);
}

/* benski> unused:
// not mac compliant
//----------------------------------------------------------------------------
char *Vfgets(char *dest, int n, VFILE *f) {
if (!f) return NULL;
#ifdef NDE_ALLOW_NONCACHED
if (!f->cached)
{
#ifdef NDE_NOWIN32FILEIO
return fgets(dest, n, f->rfile);
#else
#error port me!
#endif
}
#endif

unsigned char c=0;
char *p;
int l=0;

p = dest;
while (l < n && !Vfeof(f)) {
c = f->data[f->ptr];
f->ptr++;
*p = c;
p++;
l++;
if (c == '\n') {
if (!Vfeof(f) && f->data[f->ptr] == '\r') {
f->ptr++;
}
break;
}
}
*p=0;
return dest;
}
*/

/* benski> unused:
//----------------------------------------------------------------------------
char Vfgetc(VFILE *f) {
if (!f) return EOF;
#ifdef NDE_ALLOW_NONCACHED
if (!f->cached)
#ifdef NDE_NOWIN32FILEIO
return fgetc(f->rfile);
#else
#error port me#
#endif
#endif
if (!Vfeof(f))
return f->data[f->ptr++];
return EOF;
}
*/

//----------------------------------------------------------------------------
unsigned long Vftell(VFILE *f)
{
	if (!f) return (unsigned)-1;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		return ftell(f->rfile);
	}
#endif
	return f->ptr;
}

//----------------------------------------------------------------------------
void Vfseek(VFILE *f, long i, int whence)
{
	if (!f) return;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		fseek(f->rfile, i, whence);
		return;
	}
#endif
	switch (whence)
	{
	case SEEK_SET:
		f->ptr = i;
		break;
	case SEEK_CUR:
		f->ptr += i;
		break;
	case SEEK_END:
		f->ptr = f->filesize+i;
		break;
	}
}

//----------------------------------------------------------------------------
int Vfeof(VFILE *f)
{
	if (!f) return -1;
#ifdef NDE_ALLOW_NONCACHED
	if (!f->cached)
	{
		return feof(f->rfile);
	}
#endif
	return (f->ptr >= f->filesize);
}

//----------------------------------------------------------------------------
int Vsync(VFILE *f)
{
	if (!f) return 0;
	if (!f->dirty) return 0;

	if (f->mode & VFS_WRITE)
	{
#ifdef NDE_ALLOW_NONCACHED
		if (!f->cached)
		{
			int p=ftell(f->rfile);
			fclose(f->rfile);
			f->rfile = fopen(f->filename, "r+b");
			if (!f->rfile)
				return 1;
			fseek(f->rfile, p, SEEK_SET);

			return 0;
		}
#endif

		char newfn[1024], oldfn[1024];
		DWORD mypid=getpid();

		snprintf(newfn,sizeof(newfn), "%s.n3w%08X",f->filename,mypid);
		snprintf(oldfn,sizeof(oldfn), "%s.o1d%08X",f->filename,mypid);

		unlink(newfn);
		unlink(oldfn);


tryagain:
		FILE *pf = fopen(newfn, "wb");
		if (!pf || fwrite(f->data, f->filesize, 1, pf) != 1)
		{
			if (pf) fclose(pf);


			printf("Error flushing DB to disk (is your drive full?)\n\nHit (R)etry to try again (recommended), or (C)ancel to abort (NOT recommended, and may leave the DB in an unuseable state)");
			fflush(stdout);
			char c;
			while (1)
			{
				scanf("%c", &c);
//				clear_stdin();
				c = toupper(c);
				if (c == 'R') goto tryagain;
				else if (c == 'C') break;
			}
			unlink(newfn);
			return 1;
		}
		fclose(pf);
		rename(f->filename,oldfn); // save old file



		int rv=0;

tryagain2:
		if (rename(newfn,f->filename))
		{

			printf("Error updating DB file on disk. This should never really happen\n\nHit (R)etry to try again (recommended), or (C)ancel to abort (NOT recommended, and may leave the DB in an unuseable state)");
			fflush(stdout);
			char c;
			while (1)
			{
				scanf("%c", &c);
//				clear_stdin();
				c = toupper(c);
				if (c == 'R') goto tryagain2;
				else if (c == 'C') break;
			}

			rename(oldfn,f->filename); // restore old file
			rv=1;
		}

		// clean up our temp files
		unlink(oldfn);
		unlink(newfn);


		//free(newfn);
		//free(oldfn);
		return rv;
	}
	f->dirty=0;
	return 0;
}

void Vfdestroy(VFILE *f)
{
	// benski> TODO: 
	if (f)
	{
		Vfclose(f);
		
		while (f->locks)
			Vfunlock(f, 1);
		
// TODO		if (f->mutex) CloseHandle(f->mutex);
		free(f);
	}
}

// benski> TODO implement these with fopen

// returns 0 on failure
int Vflock(VFILE *fl, bool is_sync)
{	
#ifndef NO_TABLE_WIN32_LOCKING
	if (!fl) return 0;
	if (!is_sync && fl->cached)
		return 1;
	if (fl->locks++ == 0)
	{
		int retry_cnt=0;
		FILE *fFile;
		do
		{
			fFile = fopen(fl->lockname, "wb");
			if (fFile == 0) Sleep(100);
		}
		while (fFile == 0 && retry_cnt++ < 100); // try for 10 seconds

		if (fFile == INVALID_HANDLE_VALUE) return 0; // db already locked, fail gracefully
		fl->lockFile = fFile;
	}
#endif
	return 1;

}

void Vfunlock(VFILE *fl, bool is_sync)
{
#ifndef NO_TABLE_WIN32_LOCKING
	if (!is_sync && fl->cached)
		return;

	if (--fl->locks == 0)
	{
		if (fl && fl->lockFile && fl->lockFile != INVALID_HANDLE_VALUE)
		{
			fclose(fl->lockFile);
			unlink(fl->lockname);
		}
	}
#endif
}