forked from ps2homebrew/wLaunchELF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filer.c
4215 lines (3940 loc) · 142 KB
/
filer.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
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
//--------------------------------------------------------------
//File name: filer.c
//--------------------------------------------------------------
#include "launchelf.h"
typedef struct
{
unsigned char unknown;
unsigned char sec; // date/time (second)
unsigned char min; // date/time (minute)
unsigned char hour; // date/time (hour)
unsigned char day; // date/time (day)
unsigned char month; // date/time (month)
unsigned short year; // date/time (year)
} PS2TIME __attribute__((aligned(2)));
#define MC_SFI 0xFEED //flag value used for mcSetFileInfo at MC file restoration
#define MC_ATTR_norm_folder 0x8427 //Normal folder on PS2 MC
#define MC_ATTR_prot_folder 0x842F //Protected folder on PS2 MC
#define MC_ATTR_PS1_folder 0x9027 //PS1 save folder on PS2 MC
#define MC_ATTR_norm_file 0x8497 //file (PS2/PS1) on PS2 MC
#define MC_ATTR_PS1_file 0x9417 //PS1 save file on PS1 MC
#define IOCTL_RENAME 0xFEEDC0DE //Ioctl request code for Rename function
enum {
COPY,
CUT,
PASTE,
MCPASTE,
PSUPASTE,
DELETE,
RENAME,
NEWDIR,
NEWICON,
MOUNTVMC0,
MOUNTVMC1,
GETSIZE,
NUM_MENU
} R1_menu_enum;
#define PM_NORMAL 0 //PasteMode value for normal copies
#define PM_MC_BACKUP 1 //PasteMode value for gamesave backup from MC
#define PM_MC_RESTORE 2 //PasteMode value for gamesave restore to MC
#define PM_PSU_BACKUP 3 //PasteMode value for gamesave backup from MC to PSU
#define PM_PSU_RESTORE 4 //PasteMode value for gamesave restore to MC from PSU
#define PM_RENAME 5 //PasteMode value for normal copies with new names
#define MAX_RECURSE 16 //Maximum folder recursion for MC Backup/Restore
int PasteProgress_f = 0; //Flags progress report having been made in Pasting
int PasteMode; //Top-level PasteMode flag
int PM_flag[MAX_RECURSE]; //PasteMode flag for each 'copy' recursion level
int PM_file[MAX_RECURSE]; //PasteMode attribute file descriptors
char mountedParty[MOUNT_LIMIT][MAX_NAME];
int latestMount = -1;
int vmcMounted[2] = {0, 0}; //flags true for mounted VMC false for unmounted
int vmc_PartyIndex[2] = {-1, -1}; //PFS index for each VMC, unless -1
int Party_vmcIndex[MOUNT_LIMIT] = {-1, -1, -1, -1}; //VMC for each PFS, unless -1
unsigned char *elisaFnt = NULL;
int elisa_failed = FALSE; //Set at failure to load font, cleared at browser entry
u64 freeSpace;
int mcfreeSpace;
int mctype_PSx; //dlanor: Needed for proper scaling of mcfreespace
int vfreeSpace; //flags validity of freespace value
int browser_cut;
int nclipFiles, nmarks, nparties;
int file_show = 1; //dlanor: 0==name_only, 1==name+size+time, 2==title+size+time
int file_sort = 1; //dlanor: 0==none, 1==name, 2==title, 3==mtime
int size_valid = 0;
int time_valid = 0;
char parties[MAX_PARTITIONS][MAX_PART_NAME + 1];
char clipPath[MAX_PATH], LastDir[MAX_NAME], marks[MAX_ENTRY];
FILEINFO clipFiles[MAX_ENTRY];
int fileMode = FIO_S_IRUSR | FIO_S_IWUSR | FIO_S_IXUSR | FIO_S_IRGRP | FIO_S_IWGRP | FIO_S_IXGRP | FIO_S_IROTH | FIO_S_IWOTH | FIO_S_IXOTH;
char cnfmode_extU[CNFMODE_CNT][4] = {
"*", // cnfmode FALSE
"ELF", // cnfmode TRUE
"IRX", // cnfmode USBD_IRX_CNF
"JPG", // cnfmode SKIN_CNF
"JPG", // cnfmode GUI_SKIN_CNF
"IRX", // cnfmode USBKBD_IRX_CNF
"KBD", // cnfmode KBDMAP_FILE_CNF
"CNF", // cnfmode CNF_PATH_CNF
"*", // cnfmode TEXT_CNF
"", // cnfmode DIR_CNF
"JPG", // cnfmode JPG_CNF
"IRX", // cnfmode USBMASS_IRX_CNF
"LNG", // cnfmode LANG_CNF
"FNT", // cnfmode FONT_CNF
"*" // cnfmode SAVE_CNF
};
char cnfmode_extL[CNFMODE_CNT][4] = {
"*", // cnfmode FALSE
"elf", // cnfmode TRUE
"irx", // cnfmode USBD_IRX_CNF
"jpg", // cnfmode SKIN_CNF
"jpg", // cnfmode GUI_SKIN_CNF
"irx", // cnfmode USBKBD_IRX_CNF
"kbd", // cnfmode KBDMAP_FILE_CNF
"cnf", // cnfmode CNF_PATH_CNF
"*", // cnfmode TEXT_CNF
"", // cnfmode DIR_CNF
"jpg", // cnfmode JPG_CNF
"irx", // cnfmode USBMASS_IRX_CNF
"lng", // cnfmode LANG_CNF
"fnt", // cnfmode FONT_CNF
"*" // cnfmode SAVE_CNF
};
int host_ready = 0;
int host_error = 0;
int host_elflist = 0;
int host_use_Bsl = 1; //By default assume that host paths use backslash
unsigned long written_size; //Used for pasting progress report
u64 PasteTime; //Used for pasting progress report
typedef struct
{
u8 unused;
u8 sec;
u8 min;
u8 hour;
u8 day;
u8 month;
u16 year;
} ps2time;
typedef struct
{ //Offs: Example content
ps2time cTime; //0x00: 8 bytes creation timestamp (struct above)
ps2time mTime; //0x08: 8 bytes modification timestamp (struct above)
u32 size; //0x10: file size
u16 attr; //0x14: 0x8427 (=normal folder, 8497 for normal file)
u16 unknown_1_u16; //0x16: 2 zero bytes
u64 unknown_2_u64; //0x18: 8 zero bytes
u8 name[32]; //0x20: 32 name bytes, padded with zeroes
} mcT_header __attribute__((aligned(64)));
typedef struct
{ //Offs: Example content
u16 attr; //0x00: 0x8427 (=normal folder, 8497 for normal file)
u16 unknown_1_u16; //0x02: 2 zero bytes
u32 size; //0x04: header_count-1, file size, 0 for pseudo
ps2time cTime; //0x08: 8 bytes creation timestamp (struct above)
u64 EMS_used_u64; //0x10: 8 zero bytes (but used by EMS)
ps2time mTime; //0x18: 8 bytes modification timestamp (struct above)
u64 unknown_2_u64; //0x20: 8 bytes from mcTable
u8 unknown_3_24_bytes[24]; //0x28: 24 zero bytes
u8 name[32]; //0x40: 32 name bytes, padded with zeroes
u8 unknown_4_416_bytes[0x1A0]; //0x60: zero byte padding to reach 0x200 size
} psu_header; //0x200: End of psu_header struct
int PSU_content; //Used to count PSU content headers for the main header
//USB_mass definitions for multiple drive usage
char USB_mass_ix[10] = {'0', 0, 0, 0, 0, 0, 0, 0, 0, 0};
int USB_mass_max_drives = USB_MASS_MAX_DRIVES;
u64 USB_mass_scan_time = 0;
int USB_mass_scanned = 0; //0==Not_found_OR_No_Multi 1==found_Multi_mass_once
int USB_mass_loaded = 0; //0==none, 1==internal, 2==external
//char debugs[4096]; //For debug display strings. Comment it out when unused
//--------------------------------------------------------------
//executable code
//--------------------------------------------------------------
void clear_mcTable(sceMcTblGetDir *mcT)
{
memset((void *)mcT, 0, sizeof(sceMcTblGetDir));
}
//--------------------------------------------------------------
void clear_psu_header(psu_header *psu)
{
memset((void *)psu, 0, sizeof(psu_header));
}
//--------------------------------------------------------------
void pad_psu_header(psu_header *psu)
{
memset((void *)psu, 0xFF, sizeof(psu_header));
}
//--------------------------------------------------------------
// getHddParty below takes as input the string path and the struct file
// and uses these to calculate the output strings party and dir. If the
// file struct is not passed as NULL, then its file->name entry will be
// added to the internal copy of the path string (which remains unchanged),
// and if that file struct entry is for a folder, then a slash is also added.
// The modified path is then used to calculate the output strings as follows.
//-----
// party = the full path string with "hdd0:" and partition spec, but without
// the slash character between them, used in user specified full paths. So
// the first slash in that string will come after the partition name.
//-----
// dir = the pfs path string, starting like "pfs0:" (but may use different
// pfs index), and this is then followed by the path within that partition.
// Note that despite the name 'dir', this is also used for files.
//-----
//NB: From the first slash character those two strings are identical when
// both are used, but either pointer may be set to NULL in the function call,
// as an indication that the caller isn't interested in that part.
//--------------------------------------------------------------
int getHddParty(const char *path, const FILEINFO *file, char *party, char *dir)
{
char fullpath[MAX_PATH], *p;
if (strncmp(path, "hdd", 3))
return -1;
strcpy(fullpath, path);
if (file != NULL) {
strcat(fullpath, file->name);
if (file->stats.AttrFile & sceMcFileAttrSubdir)
strcat(fullpath, "/");
}
if ((p = strchr(&fullpath[6], '/')) == NULL)
return -1;
if (dir != NULL)
sprintf(dir, "pfs0:%s", p);
*p = 0;
if (party != NULL)
if (strcmp(&fullpath[6], "__xcontents") == 0 || strcmp(&fullpath[6], "__extend") == 0 || strcmp(&fullpath[6], "__xdata") == 0)
sprintf(party, "bhdd0:%s", &fullpath[6]);
else
sprintf(party, "hdd0:%s", &fullpath[6]);
return 0;
}
//--------------------------------------------------------------
int mountParty(const char *party)
{
int i, j;
int mount_mode;
char pfs_str[6];
for (i = 0; i < MOUNT_LIMIT; i++) { //Here we check already mounted PFS indexes
if (!strcmp(party, mountedParty[i]))
goto return_i;
}
for (i = 0, j = -1; i < MOUNT_LIMIT; i++) { //Here we search for a free PFS index
if (mountedParty[i][0] == 0) {
j = i;
break;
}
}
if (j == -1) { //Here we search for a suitable PFS index to unmount
for (i = 0; i < MOUNT_LIMIT; i++) {
if ((i != latestMount) && (Party_vmcIndex[i] < 0)) {
j = i;
break;
}
}
unmountParty(j);
}
//Here j is the index of a free PFS mountpoint
//But 'free' only means that the main uLE program isn't using it
//If the ftp server is running, that may have used the mountpoints
//RA NB: The old code to reclaim FTP partitions was seriously bugged...
i = j;
strcpy(pfs_str, "pfs0:");
mount_mode = FIO_MT_RDWR;
if (strncmp(party, "bhdd", 4) == 0)
{
mount_mode = FIO_MT_RDONLY;
}
pfs_str[3] = '0' + i;
if (fileXioMount(pfs_str, party, mount_mode) < 0) { //if FTP stole it
for (i = 0; i <= 4; i++) { //for loop to kill FTP partition mountpoints
if ((i != latestMount) && (Party_vmcIndex[i] < 0)) { //if unneeded by uLE
unmountParty(i); //unmount partition mountpoint
pfs_str[3] = '0' + i; //prepare to reuse that mountpoint
if (fileXioMount(pfs_str, party, mount_mode) >= 0)
break; //break from the loop on successful mount
} //ends if unneeded by uLE
} //ends for loop to kill FTP partition mountpoints
//Here i indicates what happened above with the following meanings:
//0..4==Success after trying i mountpoints, 5==Failure
if (i > 4)
return -1;
} //ends if clause for mountpoints stolen by FTP
strcpy(mountedParty[i], party);
return_i:
latestMount = i;
return i;
}
//--------------------------------------------------------------
void unmountParty(int party_ix)
{
char pfs_str[6];
strcpy(pfs_str, "pfs0:");
pfs_str[3] += party_ix;
if (fileXioUmount(pfs_str) < 0)
return; //leave variables unchanged if unmount failed (remember true state)
if (party_ix < MOUNT_LIMIT) {
mountedParty[party_ix][0] = 0;
}
if (latestMount == party_ix)
latestMount = -1;
}
//--------------------------------------------------------------
// unmountAll can unmount all mountpoints from 0 to MOUNT_LIMIT,
// but unlike the individual unmountParty, it will only do so
// for mountpoints indicated as used by the matching string in
// the string array 'mountedParty'.
// From v4.23 this routine is also used to unmount VMC devices
//------------------------------
void unmountAll(void)
{
char pfs_str[6];
char vmc_str[6];
int i;
strcpy(vmc_str, "vmc0:");
for (i = 0; i < 2; i++) {
if (vmcMounted[i]) {
vmc_str[3] = '0' + i;
fileXioUmount(vmc_str);
vmcMounted[i] = 0;
vmc_PartyIndex[i] = -1;
}
}
strcpy(pfs_str, "pfs0:");
for (i = 0; i < MOUNT_LIMIT; i++) {
Party_vmcIndex[i] = -1;
if (mountedParty[i][0] != 0) {
pfs_str[3] = '0' + i;
fileXioUmount(pfs_str);
mountedParty[i][0] = 0;
}
}
latestMount = -1;
} //ends unmountAll
//--------------------------------------------------------------
int ynDialog(const char *message)
{
char msg[512];
int dh, dw, dx, dy;
int sel = 0, a = 6, b = 4, c = 2, n, tw;
int i, x, len, ret;
int event, post_event = 0;
strcpy(msg, message);
for (i = 0, n = 1; msg[i] != 0; i++) { //start with one string at pos zero
if (msg[i] == '\n') { //line separator at current pos ?
msg[i] = 0; //split old line to separate string
n++; //increment string count
}
} //loop back for next character pos
for (i = len = tw = 0; i < n; i++) { //start with string 0, assume 0 length & width
ret = printXY(&msg[len], 0, 0, 0, FALSE, 0); //get width of current string
if (ret > tw)
tw = ret; //tw = largest text width of strings so far
len += strlen(&msg[len]) + 1; //len = pos of next string start
}
if (tw < 96)
tw = 96;
dh = FONT_HEIGHT * (n + 1) + 2 * 2 + a + b + c;
dw = 2 * 2 + a * 2 + tw;
dx = (SCREEN_WIDTH - dw) / 2;
dy = (SCREEN_HEIGHT - dh) / 2;
// printf("tw=%d\ndh=%d\ndw=%d\ndx=%d\ndy=%d\n", tw,dh,dw,dx,dy);
event = 1; //event = initial entry
while (1) {
//Pad response section
waitPadReady(0, 0);
if (readpad()) {
if (new_pad & PAD_LEFT) {
event |= 2; //event |= valid pad command
sel = 0;
} else if (new_pad & PAD_RIGHT) {
event |= 2; //event |= valid pad command
sel = 1;
} else if ((!swapKeys && new_pad & PAD_CROSS) || (swapKeys && new_pad & PAD_CIRCLE)) {
ret = -1;
break;
} else if ((swapKeys && new_pad & PAD_CROSS) || (!swapKeys && new_pad & PAD_CIRCLE)) {
if (sel == 0)
ret = 1;
else
ret = -1;
break;
}
}
if (event || post_event) { //NB: We need to update two frame buffers per event
//Display section
drawPopSprite(setting->color[COLOR_BACKGR],
dx, dy,
dx + dw, (dy + dh));
drawFrame(dx, dy, dx + dw, (dy + dh), setting->color[COLOR_FRAME]);
for (i = len = 0; i < n; i++) {
printXY(&msg[len], dx + 2 + a, (dy + a + 2 + i * 16), setting->color[COLOR_TEXT], TRUE, 0);
len += strlen(&msg[len]) + 1;
}
//Cursor positioning section
x = (tw - 96) / 4;
printXY(LNG(OK), dx + a + x + FONT_WIDTH,
(dy + a + b + 2 + n * FONT_HEIGHT), setting->color[COLOR_TEXT], TRUE, 0);
printXY(LNG(CANCEL), dx + dw - x - (strlen(LNG(CANCEL)) + 1) * FONT_WIDTH,
(dy + a + b + 2 + n * FONT_HEIGHT), setting->color[COLOR_TEXT], TRUE, 0);
if (sel == 0)
drawChar(LEFT_CUR, dx + a + x, (dy + a + b + 2 + n * FONT_HEIGHT), setting->color[COLOR_TEXT]);
else
drawChar(LEFT_CUR, dx + dw - x - (strlen(LNG(CANCEL)) + 2) * FONT_WIDTH - 1,
(dy + a + b + 2 + n * FONT_HEIGHT), setting->color[COLOR_TEXT]);
} //ends if(event||post_event)
drawLastMsg();
post_event = event;
event = 0;
} //ends while
drawSprite(setting->color[COLOR_BACKGR], dx, dy, dx + dw + 1, (dy + dh) + 1);
drawScr();
drawSprite(setting->color[COLOR_BACKGR], dx, dy, dx + dw + 1, (dy + dh) + 1);
drawScr();
return ret;
}
//------------------------------
//endfunc ynDialog
//--------------------------------------------------------------
void nonDialog(char *message)
{
char msg[80 * 30]; //More than this can't be shown on screen, even in PAL
static int dh, dw, dx, dy; //These are static, to allow cleanup
int a = 6, b = 4, c = 2, n, tw;
int i, len, ret;
if (message == NULL) { //NULL message means cleanup for last nonDialog
drawSprite(setting->color[COLOR_BACKGR],
dx, dy,
dx + dw, (dy + dh));
return;
}
strcpy(msg, message);
for (i = 0, n = 1; msg[i] != 0; i++) { //start with one string at pos zero
if (msg[i] == '\n') { //line separator at current pos ?
msg[i] = 0; //split old line to separate string
n++; //increment string count
}
} //loop back for next character pos
for (i = len = tw = 0; i < n; i++) { //start with string 0, assume 0 length & width
ret = printXY(&msg[len], 0, 0, 0, FALSE, 0); //get width of current string
if (ret > tw)
tw = ret; //tw = largest text width of strings so far
len += strlen(&msg[len]) + 1; //len = pos of next string start
}
if (tw < 96)
tw = 96;
dh = 16 * n + 2 * 2 + a + b + c;
dw = 2 * 2 + a * 2 + tw;
dx = (SCREEN_WIDTH - dw) / 2;
dy = (SCREEN_HEIGHT - dh) / 2;
//printf("tw=%d\ndh=%d\ndw=%d\ndx=%d\ndy=%d\n", tw,dh,dw,dx,dy);
drawPopSprite(setting->color[COLOR_BACKGR],
dx, dy,
dx + dw, (dy + dh));
drawFrame(dx, dy, dx + dw, (dy + dh), setting->color[COLOR_FRAME]);
for (i = len = 0; i < n; i++) {
printXY(&msg[len], dx + 2 + a, (dy + a + 2 + i * FONT_HEIGHT), setting->color[COLOR_TEXT], TRUE, 0);
len += strlen(&msg[len]) + 1;
}
}
//------------------------------
//endfunc nonDialog
//--------------------------------------------------------------
// cmpFile below returns negative if the 'a' entry is 'lower'
// than the 'b' entry, normally meaning that 'a' should be in
// a higher/earlier screen position than 'b'. Such negative
// return value causes the calling sort routine to adjust the
// entry order, which does not occur for other return values.
//--------------------------------------------------------------
int cmpFile(FILEINFO *a, FILEINFO *b) //Used for directory sort
{
char ca, cb;
int i, n, ret, aElf = FALSE, bElf = FALSE, t = (file_sort == 2);
if (file_sort == 0)
return 0; //return 0 for unsorted mode
if ((a->stats.AttrFile & MC_ATTR_OBJECT) == (b->stats.AttrFile & MC_ATTR_OBJECT)) {
if (a->stats.AttrFile & sceMcFileAttrFile) {
if (genCmpFileExt(a->name, "ELF"))
aElf = TRUE;
if (genCmpFileExt(b->name, "ELF"))
bElf = TRUE;
if (aElf && !bElf)
return -1;
else if (!aElf && bElf)
return 1;
}
if (file_sort == 3) { //Sort by timestamp
t = (file_show == 2); //Set secondary sort criterion
if (time_valid) {
u64 time_a = *(u64 *)&a->stats._Modify;
u64 time_b = *(u64 *)&b->stats._Modify;
if (time_a > time_b)
return -1; //NB: reversed comparison for falling order
if (time_a < time_b)
return 1;
}
}
if (t) {
if (a->title[0] != 0 && b->title[0] == 0)
return -1;
else if (a->title[0] == 0 && b->title[0] != 0)
return 1;
else if (a->title[0] == 0 && b->title[0] == 0)
t = FALSE;
}
if (t)
n = strlen((const char *)a->title);
else
n = strlen(a->name);
for (i = 0; i <= n; i++) {
if (t) {
ca = a->title[i];
cb = b->title[i];
} else {
ca = a->name[i];
cb = b->name[i];
if (ca >= 'a' && ca <= 'z')
ca -= 0x20;
if (cb >= 'a' && cb <= 'z')
cb -= 0x20;
}
ret = ca - cb;
if (ret != 0)
return ret;
}
return 0;
}
if (a->stats.AttrFile & sceMcFileAttrSubdir)
return -1;
else
return 1;
}
//--------------------------------------------------------------
void sort(FILEINFO *a, int left, int right)
{
FILEINFO tmp, pivot;
int i, p;
if (left < right) {
pivot = a[left];
p = left;
for (i = left + 1; i <= right; i++) {
if (cmpFile(&a[i], &pivot) < 0) {
p = p + 1;
tmp = a[p];
a[p] = a[i];
a[i] = tmp;
}
}
a[left] = a[p];
a[p] = pivot;
sort(a, left, p - 1);
sort(a, p + 1, right);
}
}
//--------------------------------------------------------------
int readMC(const char *path, FILEINFO *info, int max)
{
static sceMcTblGetDir mcDir[MAX_ENTRY] __attribute__((aligned(64)));
char dir[MAX_PATH];
int i, j, ret;
mcSync(0, NULL, NULL);
mcGetInfo(path[2] - '0', 0, &mctype_PSx, NULL, NULL);
mcSync(0, NULL, &ret);
if (mctype_PSx == 2) //PS2 MC ?
time_valid = 1;
size_valid = 1;
strcpy(dir, &path[4]);
strcat(dir, "*");
mcGetDir(path[2] - '0', 0, dir, 0, MAX_ENTRY - 2, mcDir);
mcSync(0, NULL, &ret);
for (i = j = 0; i < ret; i++) {
if (mcDir[i].AttrFile & sceMcFileAttrSubdir &&
(!strcmp((char *)mcDir[i].EntryName, ".") || !strcmp((char *)mcDir[i].EntryName, "..")))
continue; //Skip pseudopaths "." and ".."
strcpy(info[j].name, (char *)mcDir[i].EntryName);
info[j].stats = mcDir[i];
j++;
}
return j;
}
//------------------------------
//endfunc readMC
//--------------------------------------------------------------
int readCD(const char *path, FILEINFO *info, int max)
{
iox_dirent_t record;
int n = 0, dd = -1;
u64 wait_start;
if (sceCdGetDiskType() <= SCECdUNKNOWN) {
wait_start = Timer();
while ((Timer() < wait_start + 500) && !uLE_cdDiscValid()) {
if (cdmode == SCECdNODISC)
return 0;
}
if (cdmode == SCECdNODISC)
return 0;
if ((cdmode < SCECdPSCD) || (cdmode > SCECdPS2DVD)) {
uLE_cdStop();
return 0;
}
}
if ((dd = fileXioDopen(path)) < 0)
goto exit; //exit if error opening directory
while (fileXioDread(dd, &record) > 0) {
if ((FIO_S_ISDIR(record.stat.mode)) && (!strcmp(record.name, ".") || !strcmp(record.name, "..")))
continue; //Skip entry if pseudo-folder "." or ".."
strcpy(info[n].name, record.name);
clear_mcTable(&info[n].stats);
if (FIO_S_ISDIR(record.stat.mode)) {
info[n].stats.AttrFile = MC_ATTR_norm_folder;
} else if (FIO_S_ISREG(record.stat.mode)) {
info[n].stats.AttrFile = MC_ATTR_norm_file;
info[n].stats.FileSizeByte = record.stat.size;
info[n].stats.Reserve2 = 0;
} else
continue; //Skip entry which is neither a file nor a folder
strncpy((char *)info[n].stats.EntryName, info[n].name, 32);
memcpy((void *)&info[n].stats._Create, record.stat.ctime, 8);
memcpy((void *)&info[n].stats._Modify, record.stat.mtime, 8);
n++;
if (n == max)
break;
} //ends while
size_valid = 1;
exit:
if (dd >= 0)
fileXioDclose(dd); //Close directory if opened above
return n;
}
//------------------------------
//endfunc readCD
//--------------------------------------------------------------
void setPartyList(void)
{
iox_dirent_t dirEnt;
int hddFd;
nparties = 0;
if ((hddFd = fileXioDopen("hdd0:")) < 0)
return;
while (fileXioDread(hddFd, &dirEnt) > 0) {
if (nparties >= MAX_PARTITIONS)
break;
if ((dirEnt.stat.attr != ATTR_MAIN_PARTITION) || (dirEnt.stat.mode != FS_TYPE_PFS))
continue;
//Patch this to see if new CB versions use valid PFS format
//NB: All CodeBreaker versions up to v9.3 use invalid formats
/* if(!strncmp(dirEnt.name, "PP.",3)){
int len = strlen(dirEnt.name);
if(!strcmp(dirEnt.name+len-4, ".PCB"))
continue;
}
if(!strncmp(dirEnt.name, "__", 2) &&
strcmp(dirEnt.name, "__boot") &&
strcmp(dirEnt.name, "__net") &&
strcmp(dirEnt.name, "__system") &&
strcmp(dirEnt.name, "__sysconf") &&
strcmp(dirEnt.name, "__contents") && // this is where PSBBN used to store it's downloaded contents. Adding it is useful.
strcmp(dirEnt.name, "__common"))
continue;
*/
strncpy(parties[nparties], dirEnt.name, MAX_PART_NAME);
parties[nparties++][MAX_PART_NAME] = '\0';
}
fileXioDclose(hddFd);
if ((hddFd = fileXioDopen("bhdd0:")) < 0)
return;
while (fileXioDread(hddFd, &dirEnt) > 0) {
if (nparties >= MAX_PARTITIONS)
break;
if ((dirEnt.stat.attr != ATTR_MAIN_PARTITION) || (dirEnt.stat.mode != FS_TYPE_PFS))
continue;
//Patch this to see if new CB versions use valid PFS format
//NB: All CodeBreaker versions up to v9.3 use invalid formats
/* if(!strncmp(dirEnt.name, "PP.",3)){
int len = strlen(dirEnt.name);
if(!strcmp(dirEnt.name+len-4, ".PCB"))
continue;
}
if(!strncmp(dirEnt.name, "__", 2) &&
strcmp(dirEnt.name, "__boot") &&
strcmp(dirEnt.name, "__net") &&
strcmp(dirEnt.name, "__system") &&
strcmp(dirEnt.name, "__sysconf") &&
strcmp(dirEnt.name, "__contents") && // this is where PSBBN used to store it's downloaded contents. Adding it is useful.
strcmp(dirEnt.name, "__common"))
continue;
*/
strncpy(parties[nparties], dirEnt.name, MAX_PART_NAME);
parties[nparties++][MAX_PART_NAME] = '\0';
}
fileXioDclose(hddFd);
}
//--------------------------------------------------------------
// The following group of file handling functions are used to allow
// the main program to access files without having to deal with the
// difference between device-specific needs directly in each call.
// Even so, special paths are assumed to be already prepared so
// as to be accepted by fileXio calls (so HDD file access use "pfs")
// Generic functions for this purpose that have been added so far are:
// genInit(void), genLimObjName(uLE_path, reserve),
// genFixPath(uLE_path, gen_path),
// genOpen(path, mode), genClose(fd), genDopen(path), genDclose(fd),
// genLseek(fd,where,how), genRead(fd,buf,size), genWrite(fd,buf,size)
// genRemove(path), genRmdir(path)
//--------------------------------------------------------------
void genLimObjName(char *uLE_path, int reserve)
{
char *p, *q, *r;
int limit = 256; //enforce a generic limit of 256 characters
int folder_flag = (uLE_path[strlen(uLE_path) - 1] == '/'); //flag folder object
int overflow;
if (!strncmp(uLE_path, "mc", 2) || !strncmp(uLE_path, "vmc", 3))
limit = 32; //enforce MC limit of 32 characters
if (folder_flag) //if path ends with path separator
uLE_path[strlen(uLE_path) - 1] = 0; // remove final path separator (temporarily)
p = uLE_path; //initially assume a pure object name (quite insanely :))
if ((q = strchr(p, ':')) != NULL) //if a drive separator is present
p = q + 1; // object name may start after drive separator
if ((q = strrchr(p, '/')) != NULL) //If there's any path separator in the string
p = q + 1; // object name starts after last path separator
limit -= reserve; //lower limit by reserved character space
overflow = strlen(p) - limit; //Calculate length of string to remove (if positive)
if ((limit <= 0) || (overflow <= 0)) //if limit invalid, or not exceeded
goto limited; // no further limitation is needed
if ((q = strrchr(p, '.')) == NULL) //if there's no extension separator
goto limit_end; //limitation must be done at end of full name
r = q - overflow; //r is the place to recopy file extension
if (r > p) { //if this place is above string start
strcpy(r, q); //remove overflow from end of prefix part
goto limited; //which concludes the limitation
} //if we fall through here, the prefix part was too short for the limitation needed
limit_end:
p[limit] = 0; // remove overflow from end of full name
limited:
if (folder_flag) //if original path ended with path separator
strcat(uLE_path, "/"); // reappend final path separator after name
}
//------------------------------
//endfunc genLimObjName
//--------------------------------------------------------------
int genFixPath(const char *inp_path, char *gen_path)
{
char uLE_path[MAX_PATH], loc_path[MAX_PATH], party[MAX_NAME], *p;
char *pathSep;
int part_ix;
part_ix = 99; //Assume valid non-HDD path
if (!uLE_related(uLE_path, inp_path))
part_ix = -99; //Assume invalid uLE_related path
strcpy(gen_path, uLE_path); //Assume no path patching needed
pathSep = strchr(uLE_path, '/');
if (!strncmp(uLE_path, "cdfs", 4)) { //if using CD or DVD disc path
// TODO: Flush CDFS cache
sceCdDiskReady(0);
//end of clause for using a CD or DVD path
} else if (!strncmp(uLE_path, "mass", 4)) { //if using USB mass: path
if (pathSep && (pathSep - uLE_path < 7) && pathSep[-1] == ':')
strcpy(gen_path + (pathSep - uLE_path), pathSep + 1);
//end of clause for using a USB mass: path
} else if (!strncmp(uLE_path, "hdd0:/", 6)) { //If using HDD path
//Get path on HDD unit, LaunchELF's format (e.g. hdd0:/partition/path/to/file)
strcpy(loc_path, uLE_path + 6);
if ((p = strchr(loc_path, '/')) != NULL) {
//Extract path to file within partition. Make a new path, relative to the filesystem root.
//hdd0:/partition/path/to/file becomes pfs0:/path/to/file.
sprintf(gen_path, "pfs0:%s", p);
*p = 0; //null-terminate the block device path (hdd0:/partition).
} else {
//Otherwise, default to /
strcpy(gen_path, "pfs0:/");
}
//Generate standard path to the block device (i.e. hdd0:/partition results in hdd0:partition)
sprintf(party, "hdd0:%s", loc_path);
if (nparties == 0) {
//No partitions recognized? Load modules & populate partition list.
loadHddModules();
setPartyList();
}
//Mount the partition.
if ((part_ix = mountParty(party)) >= 0)
gen_path[3] = part_ix + '0';
//end of clause for using an HDD path
} else if (!strncmp(uLE_path, "bhdd0:/", 7)) { //If using HDD path
//Get path on HDD unit, LaunchELF's format (e.g. hdd0:/partition/path/to/file)
strcpy(loc_path, uLE_path + 7);
if ((p = strchr(loc_path, '/')) != NULL) {
//Extract path to file within partition. Make a new path, relative to the filesystem root.
//hdd0:/partition/path/to/file becomes pfs0:/path/to/file.
sprintf(gen_path, "pfs0:%s", p);
*p = 0; //null-terminate the block device path (hdd0:/partition).
} else {
//Otherwise, default to /
strcpy(gen_path, "pfs0:/");
}
//Generate standard path to the block device (i.e. hdd0:/partition results in hdd0:partition)
sprintf(party, "bhdd0:%s", loc_path);
if (nparties == 0) {
//No partitions recognized? Load modules & populate partition list.
loadHddModules();
setPartyList();
}
//Mount the partition.
if ((part_ix = mountParty(party)) >= 0)
gen_path[3] = part_ix + '0';
//end of clause for using an HDD path
}
genLimObjName(gen_path, 0);
return part_ix;
//non-HDD Path => 99, Good HDD Path => 0-3, Bad Path => negative
}
//------------------------------
//endfunc genFixPath
//--------------------------------------------------------------
int genRmdir(char *path)
{
int ret;
genLimObjName(path, 0);
ret = fileXioRmdir(path);
if (!strncmp(path, "vmc", 3))
fileXioDevctl("vmc0:", DEVCTL_VMCFS_CLEAN, NULL, 0, NULL, 0);
return ret;
}
//------------------------------
//endfunc genRmdir
//--------------------------------------------------------------
int genRemove(char *path)
{
int ret;
genLimObjName(path, 0);
ret = fileXioRemove(path);
if (!strncmp(path, "vmc", 3))
fileXioDevctl("vmc0:", DEVCTL_VMCFS_CLEAN, NULL, 0, NULL, 0);
return ret;
}
//------------------------------
//endfunc genRemove
//--------------------------------------------------------------
int genOpen(char *path, int mode)
{
genLimObjName(path, 0);
return open(path, mode, fileMode);
}
//------------------------------
//endfunc genOpen
//--------------------------------------------------------------
int genDopen(char *path)
{
int fd;
if (!strncmp(path, "pfs", 3) || !strncmp(path, "vmc", 3)) {
char tmp[MAX_PATH];
strcpy(tmp, path);
if (tmp[strlen(tmp) - 1] == '/')
tmp[strlen(tmp) - 1] = '\0';
fd = fileXioDopen(tmp);
} else {
fd = fileXioDopen(path);
}
return fd;
}
//------------------------------
//endfunc genDopen
//--------------------------------------------------------------
int genLseek(int fd, int where, int how)
{
return lseek(fd, where, how);
}
//------------------------------
//endfunc genLseek
//--------------------------------------------------------------
int genRead(int fd, void *buf, int size)
{
return read(fd, buf, size);
}
//------------------------------
//endfunc genRead
//--------------------------------------------------------------
int genWrite(int fd, void *buf, int size)
{
return write(fd, buf, size);
}
//------------------------------
//endfunc genWrite
//--------------------------------------------------------------
int genClose(int fd)
{
return close(fd);
}
//------------------------------
//endfunc genClose
//--------------------------------------------------------------
int genDclose(int fd)
{
return fileXioDclose(fd);
}
//------------------------------
//endfunc genDclose
//--------------------------------------------------------------
int genCmpFileExt(const char *filename, const char *extension)
{
const char *p;
p = strrchr(filename, '.');
return (p != NULL && !strcasecmp(p + 1, extension));
}
//------------------------------
//endfunc genDclose
//--------------------------------------------------------------
int readVMC(const char *path, FILEINFO *info, int max)
{
iox_dirent_t dirbuf;
char dir[MAX_PATH];
int i = 0, fd;
strcpy(dir, path);
if ((fd = fileXioDopen(dir)) < 0)
return 0;
while (fileXioDread(fd, &dirbuf) > 0) {
// if(dirbuf.stat.mode & FIO_S_IFDIR && //NB: normal usage (non-vmcfs)
if (dirbuf.stat.mode & sceMcFileAttrSubdir && //NB: nonstandard usage of vmcfs
(!strcmp(dirbuf.name, ".") || !strcmp(dirbuf.name, "..")))
continue; //Skip pseudopaths "." and ".."
strcpy(info[i].name, dirbuf.name);
clear_mcTable(&info[i].stats);
// if(dirbuf.stat.mode & FIO_S_IFDIR){ //NB: normal usage (non-vmcfs)
// info[i].stats.attrFile = MC_ATTR_norm_folder;
// }
if (dirbuf.stat.mode & sceMcFileAttrSubdir) { //NB: vmcfs usage
info[i].stats.AttrFile = dirbuf.stat.mode;
}
// else if(dirbuf.stat.mode & FIO_S_IFREG){ //NB: normal usage (non-vmcfs)
// info[i].stats.attrFile = MC_ATTR_norm_file;
// info[i].stats.fileSizeByte = dirbuf.stat.size;
// info[i].stats.unknown4[0] = dirbuf.stat.hisize;
// }
else if (dirbuf.stat.mode & sceMcFileAttrFile) { //NB: vmcfs usage
info[i].stats.AttrFile = dirbuf.stat.mode;
info[i].stats.FileSizeByte = dirbuf.stat.size;
info[i].stats.Reserve2 = dirbuf.stat.hisize;
} else
continue; //Skip entry which is neither a file nor a folder
strncpy((char *)info[i].stats.EntryName, info[i].name, 32);
memcpy((void *)&info[i].stats._Create, dirbuf.stat.ctime, 8);
memcpy((void *)&info[i].stats._Modify, dirbuf.stat.mtime, 8);
i++;
if (i == max)
break;
}