Repository navigation
Expand file tree
/
Copy pathfs.c
More file actions
8239 lines (6642 loc) · 250 KB
/
Copy pathfs.c
File metadata and controls
8239 lines (6642 loc) · 250 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef fs_c
#define fs_c
#include "fs.h"
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <windows.h> /* <-- Just can't get away from this darn thing... Needed for mutexes and file iteration. */
static fs_result fs_result_from_GetLastError(DWORD error)
{
switch (error)
{
case ERROR_SUCCESS: return FS_SUCCESS;
case ERROR_NOT_ENOUGH_MEMORY: return FS_OUT_OF_MEMORY;
case ERROR_BUSY: return FS_BUSY;
case ERROR_SEM_TIMEOUT: return FS_TIMEOUT;
default: break;
}
return FS_ERROR;
}
#endif
/*
This is the maximum number of ureferenced opened archive files that will be kept in memory
before garbage collection of those archives is triggered.
*/
#ifndef FS_DEFAULT_ARCHIVE_GC_THRESHOLD
#define FS_DEFAULT_ARCHIVE_GC_THRESHOLD 10
#endif
#define FS_UNUSED(x) (void)x
#ifndef FS_MALLOC
#define FS_MALLOC(sz) malloc((sz))
#endif
#ifndef FS_REALLOC
#define FS_REALLOC(p, sz) realloc((p), (sz))
#endif
#ifndef FS_FREE
#define FS_FREE(p) free((p))
#endif
static void fs_zero_memory_default(void* p, size_t sz)
{
if (sz > 0) {
memset(p, 0, sz);
}
}
#ifndef FS_ZERO_MEMORY
#define FS_ZERO_MEMORY(p, sz) fs_zero_memory_default((p), (sz))
#endif
#ifndef FS_COPY_MEMORY
#define FS_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
#endif
#ifndef FS_MOVE_MEMORY
#define FS_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz))
#endif
#ifndef FS_ASSERT
#define FS_ASSERT(condition) assert(condition)
#endif
#define FS_ZERO_OBJECT(p) FS_ZERO_MEMORY((p), sizeof(*(p)))
#define FS_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
#define FS_MAX(x, y) (((x) > (y)) ? (x) : (y))
#define FS_MIN(x, y) (((x) < (y)) ? (x) : (y))
#define FS_ABS(x) (((x) > 0) ? (x) : -(x))
#define FS_CLAMP(x, lo, hi) (FS_MAX((lo), FS_MIN((x), (hi))))
#define FS_OFFSET_PTR(p, offset) (((unsigned char*)(p)) + (offset))
#define FS_ALIGN(x, a) ((x + (a-1)) & ~(a-1))
FS_API char* fs_strcpy(char* dst, const char* src)
{
char* dstorig;
FS_ASSERT(dst != NULL);
FS_ASSERT(src != NULL);
dstorig = dst;
/* No, we're not using this garbage: while (*dst++ = *src++); */
for (;;) {
*dst = *src;
if (*src == '\0') {
break;
}
dst += 1;
src += 1;
}
return dstorig;
}
FS_API int fs_strncpy(char* dst, const char* src, size_t count)
{
size_t maxcount;
size_t i;
if (dst == 0) {
return EINVAL;
}
if (src == 0) {
dst[0] = '\0';
return EINVAL;
}
maxcount = count;
for (i = 0; i < maxcount && src[i] != '\0'; ++i) {
dst[i] = src[i];
}
if (src[i] == '\0' || i == count || count == ((size_t)-1)) {
dst[i] = '\0';
return 0;
}
dst[0] = '\0';
return ERANGE;
}
FS_API int fs_strcpy_s(char* dst, size_t dstCap, const char* src)
{
size_t i;
if (dst == 0) {
return EINVAL;
}
if (dstCap == 0) {
return ERANGE;
}
if (src == 0) {
dst[0] = '\0';
return EINVAL;
}
for (i = 0; i < dstCap && src[i] != '\0'; ++i) {
dst[i] = src[i];
}
if (i < dstCap) {
dst[i] = '\0';
return 0;
}
dst[0] = '\0';
return ERANGE;
}
FS_API int fs_strncpy_s(char* dst, size_t dstCap, const char* src, size_t count)
{
size_t maxcount;
size_t i;
if (dst == 0) {
return EINVAL;
}
if (dstCap == 0) {
return EINVAL;
}
if (src == 0) {
dst[0] = '\0';
return EINVAL;
}
maxcount = count;
if (count == ((size_t)-1) || count >= dstCap) { /* -1 = _TRUNCATE */
maxcount = dstCap - 1;
}
for (i = 0; i < maxcount && src[i] != '\0'; ++i) {
dst[i] = src[i];
}
if (src[i] == '\0' || i == count || count == ((size_t)-1)) {
dst[i] = '\0';
return 0;
}
dst[0] = '\0';
return ERANGE;
}
FS_API int fs_strncmp(const char* str1, const char* str2, size_t maxLen)
{
if (str1 == str2) return 0;
/* These checks differ from the standard implementation. It's not important, but I prefer it just for sanity. */
if (str1 == NULL) return -1;
if (str2 == NULL) return 1;
/* This function still needs to check for null terminators even though the length has been specified. */
for (;;) {
if (maxLen == 0) {
break;
}
if (str1[0] == '\0') {
break;
}
if (str1[0] != str2[0]) {
break;
}
str1 += 1;
str2 += 1;
maxLen -= 1;
}
if (maxLen == 0) {
return 0;
}
return ((unsigned char*)str1)[0] - ((unsigned char*)str2)[0];
}
FS_API int fs_strnicmp_ascii(const char* str1, const char* str2, size_t count)
{
if (str1 == NULL || str2 == NULL) {
return 0;
}
while (*str1 != '\0' && *str2 != '\0' && count > 0) {
int c1 = (int)*str1;
int c2 = (int)*str2;
if (c1 >= 'A' && c1 <= 'Z') {
c1 += 'a' - 'A';
}
if (c2 >= 'A' && c2 <= 'Z') {
c2 += 'a' - 'A';
}
if (c1 != c2) {
return c1 - c2;
}
str1 += 1;
str2 += 1;
count -= 1;
}
if (count == 0) {
return 0;
} else if (*str1 == '\0' && *str2 == '\0') {
return 0;
} else if (*str1 == '\0') {
return -1;
} else {
return 1;
}
}
FS_API int fs_strnicmp(const char* str1, const char* str2, size_t count)
{
/* We will use the standard implementations of strnicmp() and strncasecmp() if they are available. */
#if defined(_MSC_VER) && _MSC_VER >= 1400
return _strnicmp(str1, str2, count);
#elif defined(__GNUC__) && defined(__USE_GNU)
return strncasecmp(str1, str2, count);
#else
/* It would be good if we could use a custom implementation based on the Unicode standard here. Would require a lot of work to get that right, however. */
return fs_strnicmp_ascii(str1, str2, count);
#endif
}
/* BEG fs_allocation_callbacks.c */
/* Default allocation callbacks. */
static void* fs_malloc_default(size_t sz, void* pUserData)
{
FS_UNUSED(pUserData);
return FS_MALLOC(sz);
}
static void* fs_realloc_default(void* p, size_t sz, void* pUserData)
{
FS_UNUSED(pUserData);
return FS_REALLOC(p, sz);
}
static void fs_free_default(void* p, void* pUserData)
{
FS_UNUSED(pUserData);
FS_FREE(p);
}
static fs_allocation_callbacks fs_allocation_callbacks_init_default(void)
{
fs_allocation_callbacks allocationCallbacks;
allocationCallbacks.pUserData = NULL;
allocationCallbacks.>
allocationCallbacks.>
allocationCallbacks.>
return allocationCallbacks;
}
static fs_allocation_callbacks fs_allocation_callbacks_init_copy(const fs_allocation_callbacks* pAllocationCallbacks)
{
if (pAllocationCallbacks != NULL) {
return *pAllocationCallbacks;
} else {
return fs_allocation_callbacks_init_default();
}
}
FS_API void* fs_malloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks)
{
if (pAllocationCallbacks != NULL) {
if (pAllocationCallbacks->onMalloc != NULL) {
return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData);
} else {
return NULL; /* Do not fall back to the default implementation. */
}
} else {
return fs_malloc_default(sz, NULL);
}
}
FS_API void* fs_calloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks)
{
void* p = fs_malloc(sz, pAllocationCallbacks);
if (p != NULL) {
FS_ZERO_MEMORY(p, sz);
}
return p;
}
FS_API void* fs_realloc(void* p, size_t sz, const fs_allocation_callbacks* pAllocationCallbacks)
{
if (pAllocationCallbacks != NULL) {
if (pAllocationCallbacks->onRealloc != NULL) {
return pAllocationCallbacks->onRealloc(p, sz, pAllocationCallbacks->pUserData);
} else {
return NULL; /* Do not fall back to the default implementation. */
}
} else {
return fs_realloc_default(p, sz, NULL);
}
}
FS_API void fs_free(void* p, const fs_allocation_callbacks* pAllocationCallbacks)
{
if (p == NULL) {
return;
}
if (pAllocationCallbacks != NULL) {
if (pAllocationCallbacks->onFree != NULL) {
pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
} else {
return; /* Do no fall back to the default implementation. */
}
} else {
fs_free_default(p, NULL);
}
}
/* END fs_allocation_callbacks.c */
/* BEG fs_thread.c */
/*
This section has been designed to be mostly compatible with c89thread with only a few minor changes
if you wanted to amalgamate this into another project which uses c89thread and want to avoid duplicate
code. These are the differences:
* The c89 namespace is replaced with "fs_".
* There is no c89mtx_timedlock() equivalent.
* `fs_mtx_plain`, etc. have been capitalized and taken out of the enum.
* c89thread_success is FS_SUCCESS
* c89thrd_error is EINVAL
* c89thrd_busy is EBUSY
* c89thrd_pthread_* is fs_pthread_*
Parameter ordering is the same as c89thread to make amalgamation easier.
*/
#if defined(_WIN32) && !defined(FS_USE_PTHREAD)
/* Win32. Don't include windows.h here. */
#else
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#else
#if _XOPEN_SOURCE < 500
#error _XOPEN_SOURCE must be >= 500. c89thread is not usable.
#endif
#endif
#include <pthread.h>
typedef pthread_t fs_pthread;
typedef pthread_mutex_t fs_pthread_mutex;
typedef pthread_cond_t fs_pthread_cond;
#endif
#if defined(_WIN32)
typedef struct
{
void* handle; /* HANDLE, CreateMutex(), CreateEvent() */
int type;
} fs_mtx;
#else
typedef fs_pthread_mutex fs_mtx;
#endif
#define FS_MTX_PLAIN 0
#define FS_MTX_TIMED 1
#define FS_MTX_RECURSIVE 2
enum
{
fs_mtx_plain = 0x00000000,
fs_mtx_timed = 0x00000001,
fs_mtx_recursive = 0x00000002
};
#if defined(_WIN32)
FS_API int fs_mtx_init(fs_mtx* mutex, int type)
{
HANDLE hMutex;
if (mutex == NULL) {
return EINVAL;
}
/* Initialize the object to zero for safety. */
mutex->handle = NULL;
mutex->type = 0;
/*
CreateMutex() will create a thread-aware mutex (allowing recursiveness), whereas an auto-reset
event (CreateEvent()) is not thread-aware and will deadlock (will not allow recursiveness). In
Win32 I'm making all mutex's timeable.
*/
if ((type & FS_MTX_RECURSIVE) != 0) {
hMutex = CreateMutexA(NULL, FALSE, NULL);
} else {
hMutex = CreateEventA(NULL, FALSE, TRUE, NULL);
}
if (hMutex == NULL) {
return fs_result_from_GetLastError(GetLastError());
}
mutex->handle = (void*)hMutex;
mutex->type = type;
return FS_SUCCESS;
}
FS_API void fs_mtx_destroy(fs_mtx* mutex)
{
if (mutex == NULL) {
return;
}
CloseHandle((HANDLE)mutex->handle);
}
FS_API int fs_mtx_lock(fs_mtx* mutex)
{
DWORD result;
if (mutex == NULL) {
return EINVAL;
}
result = WaitForSingleObject((HANDLE)mutex->handle, INFINITE);
if (result != WAIT_OBJECT_0) {
return EINVAL;
}
return FS_SUCCESS;
}
FS_API int fs_mtx_trylock(fs_mtx* mutex)
{
DWORD result;
if (mutex == NULL) {
return EINVAL;
}
result = WaitForSingleObject((HANDLE)mutex->handle, 0);
if (result != WAIT_OBJECT_0) {
return EBUSY;
}
return FS_SUCCESS;
}
FS_API int fs_mtx_unlock(fs_mtx* mutex)
{
BOOL result;
if (mutex == NULL) {
return EINVAL;
}
if ((mutex->type & FS_MTX_RECURSIVE) != 0) {
result = ReleaseMutex((HANDLE)mutex->handle);
} else {
result = SetEvent((HANDLE)mutex->handle);
}
if (!result) {
return EINVAL;
}
return FS_SUCCESS;
}
#else
FS_API int fs_mtx_init(fs_mtx* mutex, int type)
{
int result;
pthread_mutexattr_t attr; /* For specifying whether or not the mutex is recursive. */
if (mutex == NULL) {
return EINVAL;
}
pthread_mutexattr_init(&attr);
if ((type & FS_MTX_RECURSIVE) != 0) {
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
} else {
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); /* Will deadlock. Consistent with Win32. */
}
result = pthread_mutex_init((pthread_mutex_t*)mutex, &attr);
pthread_mutexattr_destroy(&attr);
if (result != 0) {
return EINVAL;
}
return FS_SUCCESS;
}
FS_API void fs_mtx_destroy(fs_mtx* mutex)
{
if (mutex == NULL) {
return;
}
pthread_mutex_destroy((pthread_mutex_t*)mutex);
}
FS_API int fs_mtx_lock(fs_mtx* mutex)
{
int result;
if (mutex == NULL) {
return EINVAL;
}
result = pthread_mutex_lock((pthread_mutex_t*)mutex);
if (result != 0) {
return EINVAL;
}
return FS_SUCCESS;
}
FS_API int fs_mtx_trylock(fs_mtx* mutex)
{
int result;
if (mutex == NULL) {
return EINVAL;
}
result = pthread_mutex_trylock((pthread_mutex_t*)mutex);
if (result != 0) {
if (result == EBUSY) {
return EBUSY;
}
return EINVAL;
}
return FS_SUCCESS;
}
FS_API int fs_mtx_unlock(fs_mtx* mutex)
{
int result;
if (mutex == NULL) {
return EINVAL;
}
result = pthread_mutex_unlock((pthread_mutex_t*)mutex);
if (result != 0) {
return EINVAL;
}
return FS_SUCCESS;
}
#endif
/* END fs_thread.c */
/* BEG fs_stream.c */
FS_API fs_result fs_stream_init(const fs_stream_vtable* pVTable, fs_stream* pStream)
{
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
pStream->pVTable = pVTable;
if (pVTable == NULL) {
return FS_INVALID_ARGS;
}
return FS_SUCCESS;
}
FS_API fs_result fs_stream_read(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead)
{
size_t bytesRead;
fs_result result;
if (pBytesRead != NULL) {
*pBytesRead = 0;
}
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
if (pStream->pVTable->read == NULL) {
return FS_NOT_IMPLEMENTED;
}
bytesRead = 0;
result = pStream->pVTable->read(pStream, pDst, bytesToRead, &bytesRead);
if (pBytesRead != NULL) {
*pBytesRead = bytesRead;
}
return result;
}
FS_API fs_result fs_stream_write(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten)
{
size_t bytesWritten;
fs_result result;
if (pBytesWritten != NULL) {
*pBytesWritten = 0;
}
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
if (pStream->pVTable->write == NULL) {
return FS_NOT_IMPLEMENTED;
}
bytesWritten = 0;
result = pStream->pVTable->write(pStream, pSrc, bytesToWrite, &bytesWritten);
if (pBytesWritten != NULL) {
*pBytesWritten = bytesWritten;
}
return result;
}
FS_API fs_result fs_stream_writef(fs_stream* pStream, const char* fmt, ...)
{
va_list args;
fs_result result;
va_start(args, fmt);
result = fs_stream_writefv(pStream, fmt, args);
va_end(args);
return result;
}
FS_API fs_result fs_stream_writef_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, ...)
{
va_list args;
fs_result result;
va_start(args, fmt);
result = fs_stream_writefv_ex(pStream, pAllocationCallbacks, fmt, args);
va_end(args);
return result;
}
FS_API fs_result fs_stream_writefv(fs_stream* pStream, const char* fmt, va_list args)
{
return fs_stream_writefv_ex(pStream, NULL, fmt, args);
}
FS_API fs_result fs_stream_writefv_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, va_list args)
{
fs_result result;
int strLen;
char pStrStack[1024];
if (pStream == NULL || fmt == NULL) {
return FS_INVALID_ARGS;
}
strLen = fs_vsnprintf(pStrStack, sizeof(pStrStack), fmt, args);
if (strLen < 0) {
return FS_ERROR; /* Encoding error. */
}
if (strLen < (int)sizeof(pStrStack)) {
/* Stack buffer is big enough. Output straight to the file. */
result = fs_stream_write(pStream, pStrStack, strLen, NULL);
} else {
/* Stack buffer is not big enough. Allocate space on the heap. */
char* pStrHeap = NULL;
pStrHeap = (char*)fs_malloc(strLen + 1, pAllocationCallbacks);
if (pStrHeap == NULL) {
return FS_OUT_OF_MEMORY;
}
fs_vsnprintf(pStrHeap, strLen + 1, fmt, args);
result = fs_stream_write(pStream, pStrHeap, strLen, NULL);
fs_free(pStrHeap, pAllocationCallbacks);
}
return result;
}
FS_API fs_result fs_stream_seek(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin)
{
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
if (pStream->pVTable->seek == NULL) {
return FS_NOT_IMPLEMENTED;
}
return pStream->pVTable->seek(pStream, offset, origin);
}
FS_API fs_result fs_stream_tell(fs_stream* pStream, fs_int64* pCursor)
{
if (pCursor == NULL) {
return FS_INVALID_ARGS; /* It does not make sense to call this without a variable to receive the cursor position. */
}
*pCursor = 0; /* <-- In case an error happens later. */
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
if (pStream->pVTable->tell == NULL) {
return FS_NOT_IMPLEMENTED;
}
return pStream->pVTable->tell(pStream, pCursor);
}
FS_API fs_result fs_stream_duplicate(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, fs_stream** ppDuplicatedStream)
{
fs_result result;
fs_stream* pDuplicatedStream;
if (ppDuplicatedStream == NULL) {
return FS_INVALID_ARGS;
}
*ppDuplicatedStream = NULL;
if (pStream == NULL) {
return FS_INVALID_ARGS;
}
if (pStream->pVTable->duplicate_alloc_size == NULL || pStream->pVTable->duplicate == NULL) {
return FS_NOT_IMPLEMENTED;
}
pDuplicatedStream = (fs_stream*)fs_calloc(pStream->pVTable->duplicate_alloc_size(pStream), pAllocationCallbacks);
if (pDuplicatedStream == NULL) {
return FS_OUT_OF_MEMORY;
}
result = fs_stream_init(pStream->pVTable, pDuplicatedStream);
if (result != FS_SUCCESS) {
fs_free(pDuplicatedStream, pAllocationCallbacks);
return result;
}
result = pStream->pVTable->duplicate(pStream, pDuplicatedStream);
if (result != FS_SUCCESS) {
fs_free(pDuplicatedStream, pAllocationCallbacks);
return result;
}
*ppDuplicatedStream = pDuplicatedStream;
return FS_SUCCESS;
}
FS_API void fs_stream_delete_duplicate(fs_stream* pDuplicatedStream, const fs_allocation_callbacks* pAllocationCallbacks)
{
if (pDuplicatedStream == NULL) {
return;
}
if (pDuplicatedStream->pVTable->uninit != NULL) {
pDuplicatedStream->pVTable->uninit(pDuplicatedStream);
}
fs_free(pDuplicatedStream, pAllocationCallbacks);
}
FS_API fs_result fs_stream_read_to_end(fs_stream* pStream, fs_format format, const fs_allocation_callbacks* pAllocationCallbacks, void** ppData, size_t* pDataSize)
{
fs_result result = FS_SUCCESS;
size_t dataSize = 0;
size_t dataCap = 0;
void* pData = NULL;
if (ppData != NULL) {
*ppData = NULL;
}
if (pDataSize != NULL) {
*pDataSize = 0;
}
if (pStream == NULL || ppData == NULL) {
return FS_INVALID_ARGS;
}
/* Read in a loop into a dynamically increasing buffer. */
for (;;) {
size_t chunkSize = 4096;
size_t bytesRead;
if (dataSize + chunkSize > dataCap) {
void* pNewData;
size_t newCap = dataCap * 2;
if (newCap == 0) {
newCap = chunkSize;
}
pNewData = fs_realloc(pData, newCap, pAllocationCallbacks);
if (pNewData == NULL) {
fs_free(pData, pAllocationCallbacks);
return FS_OUT_OF_MEMORY;
}
pData = pNewData;
dataCap = newCap;
}
/* At this point there should be enough data in the buffer for the next chunk. */
result = fs_stream_read(pStream, FS_OFFSET_PTR(pData, dataSize), chunkSize, &bytesRead);
dataSize += bytesRead;
if (result != FS_SUCCESS || bytesRead < chunkSize) {
break;
}
}
/* If we're opening in text mode, we need to append a null terminator. */
if (format == FS_FORMAT_TEXT) {
if (dataSize >= dataCap) {
void* pNewData;
pNewData = fs_realloc(pData, dataSize + 1, pAllocationCallbacks);
if (pNewData == NULL) {
fs_free(pData, pAllocationCallbacks);
return FS_OUT_OF_MEMORY;
}
pData = pNewData;
}
((char*)pData)[dataSize] = '\0';
}
*ppData = pData;
if (pDataSize != NULL) {
*pDataSize = dataSize;
}
/* Make sure the caller is aware of any errors. */
if (result != FS_SUCCESS && result != FS_AT_END) {
return result;
} else {
return FS_SUCCESS;
}
}
/* END fs_stream.c */
/* BEG fs_backend.c */
static size_t fs_backend_alloc_size(const fs_backend* pBackend, const void* pBackendConfig)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->alloc_size == NULL) {
return 0;
} else {
return pBackend->alloc_size(pBackendConfig);
}
}
static fs_result fs_backend_init(const fs_backend* pBackend, fs* pFS, const void* pBackendConfig, fs_stream* pStream)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->init == NULL) {
return FS_NOT_IMPLEMENTED;
} else {
return pBackend->init(pFS, pBackendConfig, pStream);
}
}
static void fs_backend_uninit(const fs_backend* pBackend, fs* pFS)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->uninit == NULL) {
return;
} else {
pBackend->uninit(pFS);
}
}
static fs_result fs_backend_ioctl(const fs_backend* pBackend, fs* pFS, int command, void* pArgs)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->ioctl == NULL) {
return FS_NOT_IMPLEMENTED;
} else {
return pBackend->ioctl(pFS, command, pArgs);
}
}
static fs_result fs_backend_remove(const fs_backend* pBackend, fs* pFS, const char* pFilePath)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->remove == NULL) {
return FS_NOT_IMPLEMENTED;
} else {
return pBackend->remove(pFS, pFilePath);
}
}
static fs_result fs_backend_rename(const fs_backend* pBackend, fs* pFS, const char* pOldName, const char* pNewName)
{
FS_ASSERT(pBackend != NULL);
if (pBackend->remove == NULL) {
return FS_NOT_IMPLEMENTED;
} else {
return pBackend->rename(pFS, pOldName, pNewName);
}
}
static fs_result fs_backend_mkdir(const fs_backend* pBackend, fs* pFS, const char* pPath)
{
FS_ASSERT(pBackend != NULL);
You can’t perform that action at this time.