-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
file_io.cpp
2011 lines (1759 loc) · 43.9 KB
/
file_io.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
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
#include "file_io.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/vfs.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/magic.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include "lib/miniz/miniz.h"
#include "osd.h"
#include "fpga_io.h"
#include "menu.h"
#include "errno.h"
#include "DiskImage.h"
#include "user_io.h"
#include "cfg.h"
#include "input.h"
#include "miniz.h"
#include "scheduler.h"
#include "video.h"
#include "support.h"
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
typedef std::vector<direntext_t> DirentVector;
typedef std::set<std::string> DirNameSet;
static const size_t YieldIterations = 128;
DirentVector DirItem;
DirNameSet DirNames;
// Directory scanning can cause the same zip file to be opened multiple times
// due to testing file types to adjust the path
// (and the fact the code path is shared with regular files)
// cache the opened mz_zip_archive so we only open it once
// this has the extra benefit that if a user is navigating through multiple directories
// in a zip archive, the zip will only be opened once and things will be more responsive
// ** We have to open the file outselves with open() so we can set O_CLOEXEC to prevent
// leaking the file descriptor when the user changes cores
static mz_zip_archive last_zip_archive = {};
static int last_zip_fd = -1;
static FILE *last_zip_cfile = NULL;
static char last_zip_fname[256] = {};
static char scanned_path[1024] = {};
static int scanned_opts = 0;
static int iSelectedEntry = 0; // selected entry index
static int iFirstEntry = 0;
static char full_path[2100];
uint8_t loadbuf[LOADBUF_SZ];
fileTYPE::fileTYPE()
{
filp = 0;
mode = 0;
type = 0;
zip = 0;
size = 0;
offset = 0;
}
fileTYPE::~fileTYPE()
{
FileClose(this);
}
int fileTYPE::opened()
{
return filp || zip;
}
struct fileZipArchive
{
mz_zip_archive archive;
int index;
mz_zip_reader_extract_iter_state* iter;
__off64_t offset;
};
static int OpenZipfileCached(char *path, int flags)
{
if (last_zip_fname[0] && !strcasecmp(path, last_zip_fname))
{
return 1;
}
mz_zip_reader_end(&last_zip_archive);
mz_zip_zero_struct(&last_zip_archive);
if (last_zip_cfile)
{
fclose(last_zip_cfile);
last_zip_cfile = nullptr;
}
last_zip_fname[0] = '\0';
last_zip_fd = open(path, O_RDONLY|O_CLOEXEC);
if (last_zip_fd < 0)
{
return 0;
}
last_zip_cfile = fdopen(last_zip_fd, "r");
if (!last_zip_cfile)
{
close(last_zip_fd);
last_zip_fd = -1;
return 0;
}
int mz_ret = mz_zip_reader_init_cfile(&last_zip_archive, last_zip_cfile, 0, flags);
if (mz_ret)
{
strncpy(last_zip_fname, path, sizeof(last_zip_fname));
}
return mz_ret;
}
static int FileIsZipped(char* path, char** zip_path, char** file_path)
{
char* z = strcasestr(path, ".zip");
if (z)
{
z += 4;
if (!z[0]) z[1] = 0;
*z++ = 0;
if (zip_path) *zip_path = path;
if (file_path) *file_path = z;
return 1;
}
return 0;
}
static char* make_fullpath(const char *path, int mode = 0)
{
if (path[0] != '/')
{
sprintf(full_path, "%s/%s", (mode == -1) ? "" : getRootDir(), path);
}
else
{
sprintf(full_path, "%s",path);
}
return full_path;
}
static int get_stmode(const char *path)
{
struct stat64 st;
return (stat64(path, &st) < 0) ? 0 : st.st_mode;
}
struct stat64* getPathStat(const char *path)
{
make_fullpath(path);
static struct stat64 st;
return (stat64(full_path, &st) >= 0) ? &st : NULL;
}
static int isPathDirectory(const char *path, int use_zip = 1)
{
make_fullpath(path);
char *zip_path, *file_path;
if (use_zip && FileIsZipped(full_path, &zip_path, &file_path))
{
if (!*file_path)
{
return 1;
}
if (!OpenZipfileCached(full_path, 0))
{
printf("isPathDirectory(OpenZipfileCached) Zip:%s, error:%s\n", zip_path,
mz_zip_get_error_string(mz_zip_get_last_error(&last_zip_archive)));
return 0;
}
// Folder names always end with a slash in the zip
// file central directory.
strcat(file_path, "/");
// Some zip files don't have directory entries
// Use the locate_file call to try and find the directory entry first, since
// this is a binary search (usually) If that fails then scan for the first
// entry that starts with file_path
const int file_index = mz_zip_reader_locate_file(&last_zip_archive, file_path, NULL, 0);
if (file_index >= 0 && mz_zip_reader_is_file_a_directory(&last_zip_archive, file_index))
{
return 1;
}
for (size_t i = 0; i < mz_zip_reader_get_num_files(&last_zip_archive); i++)
{
char zip_fname[256];
mz_zip_reader_get_filename(&last_zip_archive, i, &zip_fname[0], sizeof(zip_fname));
if (strcasestr(zip_fname, file_path))
{
return 1;
}
}
return 0;
}
else
{
int stmode = get_stmode(full_path);
if (!stmode)
{
//printf("isPathDirectory(stat) path: %s, error: %s.\n", full_path, strerror(errno));
return 0;
}
if (stmode & S_IFDIR) return 1;
}
return 0;
}
static int isPathRegularFile(const char *path, int use_zip = 1)
{
make_fullpath(path);
char *zip_path, *file_path;
if (use_zip && FileIsZipped(full_path, &zip_path, &file_path))
{
//If there's no path into the zip file, don't bother opening it, we're a "directory"
if (!*file_path)
{
return 0;
}
if (!OpenZipfileCached(full_path, 0))
{
//printf("isPathRegularFile(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path,
// mz_zip_get_error_string(mz_zip_get_last_error(&z)));
return 0;
}
const int file_index = mz_zip_reader_locate_file(&last_zip_archive, file_path, NULL, 0);
if (file_index < 0)
{
//printf("isPathRegularFile(mz_zip_reader_locate_file) Zip:%s, file:%s, error: %s\n",
// zip_path, file_path,
// mz_zip_get_error_string(mz_zip_get_last_error(&z)));
return 0;
}
if (!mz_zip_reader_is_file_a_directory(&last_zip_archive, file_index) && mz_zip_reader_is_file_supported(&last_zip_archive, file_index))
{
return 1;
}
}
else
{
if (get_stmode(full_path) & S_IFREG) return true;
}
return 0;
}
void FileClose(fileTYPE *file)
{
if (file->zip)
{
if (file->zip->iter)
{
mz_zip_reader_extract_iter_free(file->zip->iter);
}
mz_zip_reader_end(&file->zip->archive);
delete file->zip;
}
if (file->filp)
{
//printf("closing %p\n", file->filp);
fclose(file->filp);
if (file->type == 1)
{
if (file->name[0] == '/')
{
shm_unlink(file->name);
}
file->type = 0;
}
}
file->zip = nullptr;
file->filp = nullptr;
file->size = 0;
}
static int zip_search_by_crc(mz_zip_archive *zipArchive, uint32_t crc32)
{
for (unsigned int file_index = 0; file_index < zipArchive->m_total_files; file_index++)
{
mz_zip_archive_file_stat s;
if (mz_zip_reader_file_stat(zipArchive, file_index, &s))
{
if (s.m_crc32 == crc32)
{
return file_index;
}
}
}
return -1;
}
int FileOpenZip(fileTYPE *file, const char *name, uint32_t crc32)
{
make_fullpath(name);
FileClose(file);
file->mode = 0;
file->type = 0;
char *p = strrchr(full_path, '/');
strcpy(file->name, (p) ? p + 1 : full_path);
char *zip_path, *file_path;
if (!FileIsZipped(full_path, &zip_path, &file_path))
{
printf("FileOpenZip: %s, is not a zip.\n", full_path);
return 0;
}
file->zip = new fileZipArchive{};
if (!mz_zip_reader_init_file(&file->zip->archive, zip_path, 0))
{
printf("FileOpenZip(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
return 0;
}
file->zip->index = -1;
if (crc32) file->zip->index = zip_search_by_crc(&file->zip->archive, crc32);
if (file->zip->index < 0) file->zip->index = mz_zip_reader_locate_file(&file->zip->archive, file_path, NULL, 0);
if (file->zip->index < 0)
{
printf("FileOpenZip(mz_zip_reader_locate_file) Zip:%s, file:%s, error: %s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
mz_zip_archive_file_stat s;
if (!mz_zip_reader_file_stat(&file->zip->archive, file->zip->index, &s))
{
printf("FileOpenZip(mz_zip_reader_file_stat) Zip:%s, file:%s, error:%s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
file->size = s.m_uncomp_size;
file->zip->iter = mz_zip_reader_extract_iter_new(&file->zip->archive, file->zip->index, 0);
if (!file->zip->iter)
{
printf("FileOpenZip(mz_zip_reader_extract_iter_new) Zip:%s, file:%s, error:%s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
file->zip->offset = 0;
file->offset = 0;
file->mode = O_RDONLY;
return 1;
}
int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute, int use_zip)
{
make_fullpath((char*)name, mode);
FileClose(file);
file->mode = 0;
file->type = 0;
char *p = strrchr(full_path, '/');
strcpy(file->name, (mode == -1) ? full_path : p + 1);
char *zip_path, *file_path;
if (use_zip && (mode != -1) && FileIsZipped(full_path, &zip_path, &file_path))
{
if (mode & O_RDWR || mode & O_WRONLY)
{
if(!mute) printf("FileOpenEx(mode) Zip:%s, writing to zipped files is not supported.\n",
full_path);
return 0;
}
file->zip = new fileZipArchive{};
if (!mz_zip_reader_init_file(&file->zip->archive, zip_path, 0))
{
if(!mute) printf("FileOpenEx(mz_zip_reader_init_file) Zip:%s, error:%s\n", zip_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
return 0;
}
file->zip->index = mz_zip_reader_locate_file(&file->zip->archive, file_path, NULL, 0);
if (file->zip->index < 0)
{
if(!mute) printf("FileOpenEx(mz_zip_reader_locate_file) Zip:%s, file:%s, error: %s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
mz_zip_archive_file_stat s;
if (!mz_zip_reader_file_stat(&file->zip->archive, file->zip->index, &s))
{
if(!mute) printf("FileOpenEx(mz_zip_reader_file_stat) Zip:%s, file:%s, error:%s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
file->size = s.m_uncomp_size;
file->zip->iter = mz_zip_reader_extract_iter_new(&file->zip->archive, file->zip->index, 0);
if (!file->zip->iter)
{
if(!mute) printf("FileOpenEx(mz_zip_reader_extract_iter_new) Zip:%s, file:%s, error:%s\n",
zip_path, file_path,
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
FileClose(file);
return 0;
}
file->zip->offset = 0;
file->offset = 0;
file->mode = mode;
}
else
{
int fd = (mode == -1) ? shm_open("/vdsk", O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, 0777) : open(full_path, mode | O_CLOEXEC, 0777);
if (fd <= 0)
{
if(!mute) printf("FileOpenEx(open) File:%s, error: %s.\n", full_path, strerror(errno));
return 0;
}
const char *fmode = mode & O_RDWR ? "w+" : "r";
file->filp = fdopen(fd, fmode);
if (!file->filp)
{
if(!mute) printf("FileOpenEx(fdopen) File:%s, error: %s.\n", full_path, strerror(errno));
close(fd);
return 0;
}
if (mode == -1)
{
file->type = 1;
file->size = 0;
file->offset = 0;
file->mode = O_CREAT | O_RDWR | O_TRUNC;
}
else
{
struct stat64 st;
int ret = fstat64(fileno(file->filp), &st);
if (ret < 0)
{
if (!mute) printf("FileOpenEx(fstat) File:%s, error: %d.\n", full_path, ret);
FileClose(file);
return 0;
}
file->size = st.st_size;
if (st.st_rdev && !st.st_size) //for special files we need an ioctl call to get the correct size
{
unsigned long long blksize;
int ret = ioctl(fd, BLKGETSIZE64, &blksize);
if (ret < 0)
{
if (!mute) printf("FileOpenEx(ioctl) File:%s, error: %d.\n", full_path, ret);
FileClose(file);
return 0;
}
file->size = blksize;
}
file->offset = 0;
file->mode = mode;
}
}
//printf("opened %s, size %llu\n", full_path, file->size);
return 1;
}
__off64_t FileGetSize(fileTYPE *file)
{
if (file->filp)
{
struct stat64 st;
if (fstat64(fileno(file->filp), &st) < 0) return 0;
if (st.st_rdev && !st.st_size) //for special files we need an ioctl call to get the correct size
{
unsigned long long blksize;
int ret = ioctl(fileno(file->filp), BLKGETSIZE64, &blksize);
if (ret < 0) return 0;
return blksize;
}
return st.st_size;
}
else if (file->zip)
{
return file->size;
}
return 0;
}
int FileOpen(fileTYPE *file, const char *name, char mute)
{
return FileOpenEx(file, name, O_RDONLY, mute);
}
int FileSeek(fileTYPE *file, __off64_t offset, int origin)
{
if (file->filp)
{
__off64_t res = fseeko64(file->filp, offset, origin);
if (res < 0)
{
printf("Fail to seek the file: offset=%lld, %s.\n", offset, file->name);
return 0;
}
offset = ftello64(file->filp);
}
else if (file->zip)
{
if (origin == SEEK_CUR)
{
offset = file->zip->offset + offset;
}
else if (origin == SEEK_END)
{
offset = file->size - offset;
}
if (offset < file->zip->offset)
{
mz_zip_reader_extract_iter_state *iter = mz_zip_reader_extract_iter_new(&file->zip->archive, file->zip->index, 0);
if (!iter)
{
printf("FileSeek(mz_zip_reader_extract_iter_new) Failed to rewind iterator, error:%s\n",
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
return 0;
}
mz_zip_reader_extract_iter_free(file->zip->iter);
file->zip->iter = iter;
file->zip->offset = 0;
}
static char buf[4*1024];
while (file->zip->offset < offset)
{
const size_t want_len = MIN((__off64_t)sizeof(buf), offset - file->zip->offset);
const size_t read_len = mz_zip_reader_extract_iter_read(file->zip->iter, buf, want_len);
file->zip->offset += read_len;
if (read_len < want_len)
{
printf("FileSeek(mz_zip_reader_extract_iter_read) Failed to advance iterator, error:%s\n",
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
return 0;
}
}
}
else
{
return 0;
}
file->offset = offset;
return 1;
}
int FileSeekLBA(fileTYPE *file, uint32_t offset)
{
__off64_t off64 = offset;
off64 <<= 9;
return FileSeek(file, off64, SEEK_SET);
}
// Read with offset advancing
int FileReadAdv(fileTYPE *file, void *pBuffer, int length, int failres)
{
ssize_t ret = 0;
if (file->filp)
{
ret = fread(pBuffer, 1, length, file->filp);
if (ret < 0)
{
printf("FileReadAdv error(%d).\n", ret);
return failres;
}
}
else if (file->zip)
{
ret = mz_zip_reader_extract_iter_read(file->zip->iter, pBuffer, length);
if (!ret)
{
printf("FileReadEx(mz_zip_reader_extract_iter_read) Failed to read, error:%s\n",
mz_zip_get_error_string(mz_zip_get_last_error(&file->zip->archive)));
return failres;
}
file->zip->offset += ret;
}
else
{
printf("FileReadAdv error(unknown file type).\n");
return failres;
}
file->offset += ret;
return ret;
}
int FileReadSec(fileTYPE *file, void *pBuffer)
{
return FileReadAdv(file, pBuffer, 512);
}
// Write with offset advancing
int FileWriteAdv(fileTYPE *file, void *pBuffer, int length, int failres)
{
int ret;
if (file->filp)
{
ret = fwrite(pBuffer, 1, length, file->filp);
fflush(file->filp);
if (ret < 0)
{
printf("FileWriteAdv error(%d).\n", ret);
return failres;
}
file->offset += ret;
if (file->offset > file->size) file->size = FileGetSize(file);
return ret;
}
else if (file->zip)
{
printf("FileWriteAdv error(not supported for zip).\n");
return failres;
}
else
{
printf("FileWriteAdv error(unknown file type).\n");
return failres;
}
}
int FileWriteSec(fileTYPE *file, void *pBuffer)
{
return FileWriteAdv(file, pBuffer, 512);
}
int FileSave(const char *name, void *pBuffer, int size)
{
make_fullpath(name);
int fd = open(full_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd < 0)
{
printf("FileSave(open) File:%s, error: %d.\n", full_path, fd);
return 0;
}
int ret = write(fd, pBuffer, size);
close(fd);
if (ret < 0)
{
printf("FileSave(write) File:%s, error: %d.\n", full_path, ret);
return 0;
}
return ret;
}
int FileDelete(const char *name)
{
make_fullpath(name);
printf("delete %s\n", full_path);
return !unlink(full_path);
}
int DirDelete(const char *name)
{
make_fullpath(name);
printf("rmdir %s\n", full_path);
return !rmdir(full_path);
}
const char* GetNameFromPath(char *path)
{
static char res[32];
char* p = strrchr(path, '/');
if (!p) p = path;
else p++;
if (strlen(p) < 19) strcpy(res, p);
else
{
strncpy(res, p, 19);
res[19] = 0;
}
return res;
}
int FileLoad(const char *name, void *pBuffer, int size)
{
fileTYPE f;
if (!FileOpen(&f, name)) return 0;
int ret = f.size;
if (pBuffer) ret = FileReadAdv(&f, pBuffer, size ? size : f.size);
FileClose(&f);
return ret;
}
int FileLoadConfig(const char *name, void *pBuffer, int size)
{
char path[256] = { CONFIG_DIR"/" };
strcat(path, name);
return FileLoad(path, pBuffer, size);
}
int FileSaveConfig(const char *name, void *pBuffer, int size)
{
char path[256] = { CONFIG_DIR };
const char *p;
while ((p = strchr(name, '/')))
{
strcat(path, "/");
strncat(path, name, p - name);
name = ++p;
FileCreatePath(path);
}
strcat(path, "/");
strcat(path, name);
return FileSave(path, pBuffer, size);
}
int FileDeleteConfig(const char *name)
{
char path[256] = { CONFIG_DIR"/" };
strcat(path, name);
return FileDelete(path);
}
int FileExists(const char *name, int use_zip)
{
return isPathRegularFile(name, use_zip);
}
int PathIsDir(const char *name, int use_zip)
{
return isPathDirectory(name, use_zip);
}
int FileCanWrite(const char *name)
{
make_fullpath(name);
if (FileIsZipped(full_path, nullptr, nullptr))
{
return 0;
}
struct stat64 st;
int ret = stat64(full_path, &st);
if (ret < 0)
{
printf("FileCanWrite(stat) File:%s, error: %d.\n", full_path, ret);
return 0;
}
//printf("FileCanWrite: mode=%04o.\n", st.st_mode);
return ((st.st_mode & S_IWUSR) != 0);
}
void create_path(const char *base_dir, const char* sub_dir)
{
make_fullpath(base_dir);
mkdir(full_path, S_IRWXU | S_IRWXG | S_IRWXO);
strcat(full_path, "/");
strcat(full_path, sub_dir);
mkdir(full_path, S_IRWXU | S_IRWXG | S_IRWXO);
}
int FileCreatePath(const char *dir)
{
int res = 1;
if (!isPathDirectory(dir)) {
make_fullpath(dir);
res = !mkdir(full_path, S_IRWXU | S_IRWXG | S_IRWXO);
}
return res;
}
void FileGenerateScreenshotName(const char *name, char *out_name, int buflen)
{
// If the name ends with .png then don't modify it
if( !strcasecmp(name + strlen(name) - 4, ".png") )
{
const char *p = strrchr(name, '/');
make_fullpath(SCREENSHOT_DIR);
if( p )
{
snprintf(out_name, buflen, "%s%s", SCREENSHOT_DIR, p);
}
else
{
snprintf(out_name, buflen, "%s/%s", SCREENSHOT_DIR, name);
}
}
else
{
create_path(SCREENSHOT_DIR, CoreName2);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char datecode[32] = {};
if (tm.tm_year >= 119) // 2019 or up considered valid time
{
strftime(datecode, 31, "%Y%m%d_%H%M%S", &tm);
snprintf(out_name, buflen, "%s/%s/%s-%s.png", SCREENSHOT_DIR, CoreName2, datecode, name[0] ? name : SCREENSHOT_DEFAULT);
}
else
{
for (int i = 1; i < 10000; i++)
{
snprintf(out_name, buflen, "%s/%s/NODATE-%s_%04d.png", SCREENSHOT_DIR, CoreName2, name[0] ? name : SCREENSHOT_DEFAULT, i);
if (!getFileType(out_name)) return;
}
}
}
}
void FileGenerateSavePath(const char *name, char* out_name, int ext_replace)
{
create_path(SAVE_DIR, CoreName2);
sprintf(out_name, "%s/%s/", SAVE_DIR, CoreName2);
char *fname = out_name + strlen(out_name);
const char *p = strrchr(name, '/');
if (p)
{
strcat(fname, p+1);
}
else
{
strcat(fname, name);
}
char *e = strrchr(fname, '.');
if (ext_replace && e)
{
strcpy(e,".sav");
}
else
{
strcat(fname, ".sav");
}
printf("SavePath=%s\n", out_name);
}
void FileGenerateSavestatePath(const char *name, char* out_name, int sufx)
{
create_path(SAVESTATE_DIR, CoreName2);
sprintf(out_name, "%s/%s/", SAVESTATE_DIR, CoreName2);
char *fname = out_name + strlen(out_name);
const char *p = strrchr(name, '/');
if (p)
{
strcat(fname, p + 1);
}
else
{
strcat(fname, name);
}
char *e = strrchr(fname, '.');
if (e) e[0] = 0;
if(sufx) sprintf(e, "_%d.ss", sufx);
else strcat(e, ".ss");
}
uint32_t getFileType(const char *name)
{
make_fullpath(name);
struct stat64 st;
if (stat64(full_path, &st)) return 0;
return st.st_mode;
}
int findPrefixDir(char *dir, size_t dir_len)
{
// Searches for the core's folder in the following order:
// /media/usb<0..5>
// /media/usb<0..5>/games
// /media/network
// /media/network/games
// /media/fat/cifs
// /media/fat/cifs/games
// /media/fat
// /media/fat/games/
// if the core folder is not found anywhere,
// it will be created in /media/fat/games/<dir>
static char temp_dir[1024];
// Usb<0..5>
for (int x = 0; x < 6; x++) {
snprintf(temp_dir, 1024, "%s%d/%s", "../usb", x, dir);
if (isPathDirectory(temp_dir)) {
printf("Found USB dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;
}
snprintf(temp_dir, 1024, "%s%d/%s/%s", "../usb", x, GAMES_DIR, dir);
if (isPathDirectory(temp_dir)) {
printf("Found USB dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;
}
}
// Network share in /media/network/
snprintf(temp_dir, 1024, "%s/%s", "../network", dir);
if (isPathDirectory(temp_dir)) {
printf("Found network dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;
}
// Network share in /media/network/games
snprintf(temp_dir, 1024, "%s/%s/%s", "../network", GAMES_DIR, dir);
if (isPathDirectory(temp_dir)) {
printf("Found network dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;
}
// CIFS_DIR directory in /media/fat/cifs
snprintf(temp_dir, 1024, "%s/%s", CIFS_DIR, dir);
if (isPathDirectory(temp_dir)) {
printf("Found CIFS dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;
}
// CIFS_DIR/GAMES_DIR directory in /media/fat/cifs/games
snprintf(temp_dir, 1024, "%s/%s/%s", CIFS_DIR, GAMES_DIR, dir);
if (isPathDirectory(temp_dir)) {
printf("Found CIFS dir: %s\n", temp_dir);
strncpy(dir, temp_dir, dir_len);
return 1;