forked from baxter104/fatx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatx.cpp
3855 lines (3828 loc) · 114 KB
/
fatx.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
/*
* FATX filesystem support
*
* Copyright (C) 2012, 2013, 2014 Christophe Duverger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <pwd.h>
#include "fatx.hpp"
const char* sepdir = "/"; /// using unix directories
const char* fsid = "XTAF"; /// filesystem id
const char* fidx = "name.txt"; /// file used for label name
const char* def_landf = "lost+found"; /// default directory for lost & founds
const char* def_fpre = "FILE"; /// default file prefix for lost & founds
const char* def_label = "XBOX"; /// default label name
fatx_context* fatx_context:: fatxc = nullptr;
string get_current_username() {
struct passwd *pw = getpwuid(geteuid());
return string(pw->pw_name);
}
// Set real and effective user and group to 'username'. Return false on error.
bool drop_privileges(string username) {
struct passwd *pw = getpwnam(username.c_str());
// set the real and effective UIDs and GIDs
if(setregid(pw->pw_gid, pw->pw_gid))
{
perror("setreuid");
return false;
}
if(setreuid(pw->pw_uid, pw->pw_uid))
{
printf("dropping guid to %d\n", pw->pw_gid);
string err_str = "Error dropping privileges";
#ifdef DEBUG
err_str += ": ";
err_str += strerror(errno);
dbglog(err_str);
#else // DEBUG
perror(err_str.c_str());
#endif // DEBUG
return false;
}
cout << "Successfully dropped privileges to " << username << endl;
return true;
}
template<typename key_t, typename value_t>
read_cache<key_t, value_t>:: read_cache(const fread_t& r, const fwrite_t& w, size_t c, size_t a) :
read(r), write(w), capacity(c), readahead(a), access("cache") {
assert(capacity != 0);
}
template<typename key_t, typename value_t>
read_cache<key_t, value_t>:: ~read_cache() {
container.clear();
}
template<typename key_t, typename value_t>
void read_cache<key_t, value_t>:: clear() {
#ifndef NO_LOCK
scoped_lock<mutex> lock(access);
#endif
container.clear();
}
template<typename key_t, typename value_t>
typename read_cache<key_t, value_t>::value_type read_cache<key_t, value_t>:: operator () (const key_type& k) {
#ifndef NO_LOCK
scoped_lock<mutex> lock(access);
#endif
#ifndef NO_CACHE
const typename container_type::left_iterator it = container.left.find(k);
if(it != container.left.end()) {
container.right.relocate(container.right.end(), container.project_right(it));
return it->second;
}
else {
assert(container.size() <= capacity);
lkval_t vv = read(k, readahead);
if(vv.empty()) {
#if defined DEBUG && defined DBG_CACHE
dbglog((format("... fatbuf: nothing for 0x%08X\n") % k).str())
#endif
return 0;
}
if(container.size() + vv.size() > capacity) {
typename container_type::right_iterator b = container.right.begin();
advance(b, container.size() + vv.size() - capacity);
#if defined DEBUG && defined DBG_CACHE
dbglog((format("Xx. fatbuf: reduce (%d)\n") % container.size()).str())
#endif
container.right.erase(container.right.begin(), b);
#if defined DEBUG && defined DBG_CACHE
#ifdef DBG_CACHDMP
(*this)();
#endif
#endif
}
container.insert(typename container_type::value_type(vv.front().second, vv.front().first));
container.right.insert(container.right.begin(), vv.begin() + 1, vv.end());
#if defined DEBUG && defined DBG_CACHE
dbglog((format(".xX fatbuf: 0x%08X - 0x%08X (%d/%d)\n") % k % (k + vv.size() - 1) % vv.size() % container.size()).str())
#ifdef DBG_CACHDMP
(*this)();
#endif
#endif
return vv.front().first;
}
#else
return read(k, 1).front().first;
#endif
}
template<typename key_t, typename value_t>
bool read_cache<key_t, value_t>:: operator () (const key_type& k, const value_type& v) {
#ifndef NO_LOCK
scoped_lock<mutex> lock(access);
#endif
#ifndef NO_CACHE
const typename container_type::left_iterator it = container.left.find(k);
if(it != container.left.end()) {
it->second = v;
container.right.relocate(container.right.end(), container.project_right(it));
}
else {
assert(container.size() <= capacity);
if(container.size() == capacity)
container.right.erase(container.right.begin());
container.insert(typename container_type::value_type(k,v));
}
#if defined DEBUG && defined DBG_CACHE
dbglog((format("XXX fatbuf: 0x%08X (%d)\n") % k % container.size()).str())
#endif
#endif
return write(k, v);
}
#if defined DEBUG && defined DBG_CACHDMP
template<typename key_t, typename value_t>
void read_cache<key_t, value_t>:: operator () () {
string res;
size_t j = 0;
for(const auto& i: container.right) {
res += (format(" %08X") % i.second).str();
if(++j % (DBGCR / 4) == 0) {
dbglog(res + "\n")
res.clear();
}
}
dbglog(res + "\n")
}
#endif
clusptr vareas:: first() const {
return empty() ? 0 : begin()->start;
}
clusptr vareas:: last() const {
return empty() ? 0 : (end() - 1)->stop;
}
size_t vareas:: nbcls() const {
size_t res = 0;
for(const area& i: *this)
res += i.stop - i.start + 1;
return res;
}
clusptr vareas:: at(size_t s) const {
if(s == 0)
return last();
for(const area& i: *this) {
if(s <= i.stop - i.start + 1)
return i.start + s - 1;
else
s -= i.stop - i.start + 1;
}
return 0;
}
vareas::iterator vareas:: in(size_t s) {
if(s == 0)
return end() - 1;
for(vareas::iterator i = begin(); i != end(); i++) {
if(s <= i->stop - i->start + 1)
return i;
else
s -= i->stop - i->start + 1;
}
return end();
}
bool vareas:: isin(clusptr c) const {
return find_if(begin(), end(), [c] (const area& i) -> bool { return i.start <= c && c <= i.stop; }) != end();
}
vareas vareas:: sub(filesize s, filesize o) const {
vareas res(*this);
res.erase(remove_if(
res.begin(), res.end(),
[s, o] (const area& a) -> bool {
return a.offset > o + s - 1 || a.offset + a.size - 1 < o;
}
), res.end());
#if defined DEBUG && defined DBG_AREAS
if(res.empty())
dbglog("NO SUBAREA.\n")
#endif
for(area& i: res) {
filesize ns = i.size;
filesize no = i.offset;
if(o > i.offset && o < i.offset + i.size - 1) {
no = o;
i.pointer += o - i.offset;
ns -= o - i.offset;
i.start += (o - i.offset) >> fatx_context::get()->par.clus_pow;
}
if(o + s - 1 > i.offset && o + s - 1 < i.offset + i.size - 1) {
ns -= i.offset + i.size - o - s;
i.stop -= (i.offset + i.size - o - s) >> fatx_context::get()->par.clus_pow;
}
i.offset = no;
i.size = ns;
}
#if defined DEBUG && defined DBG_AREAS
if(o > nbcls() << fatx_context::get()->par.clus_pow)
dbglog((format("AREAS: bad parameters, offset:0x%016X size:%d\n") % o % s).str())
else
dbglog("Sub" + res.print())
#endif
return o > nbcls() << fatx_context::get()->par.clus_pow ? vareas() : res;
}
void vareas:: add(vareas va) {
if(va.empty())
return;
if(!empty()) {
if(va.first() == last() + 1) {
(end() - 1)->size += va.begin()->size;
(end() - 1)->stop = va.begin()->stop;
va.erase(va.begin());
}
streamptr o = (end() - 1)->offset + (end() - 1)->size;
for(area& i: va) {
i.offset = o;
o += i.size;
}
}
insert(end(), va.begin(), va.end());
}
#if defined DEBUG && defined DBG_AREAS
string vareas:: print() const {
string sink;
sink += (format("Area: %d subarea%s with %d cluster%s [0x%08X-0x%08X]\n")
% size()
% (size() > 1 ? "s" : "")
% nbcls()
% (nbcls() > 1 ? "s" : "")
% first()
% last()
).str();
for(const area& i: *this)
sink += (format(" [0x%08X-0x%08X] off:0x%016X ptr:0x%016X len:%d\n") % i.start % i.stop % i.offset % i.pointer % i.size).str();
return sink;
}
#endif
buffer:: buffer(const streamptr o, const streamptr s) : touched(false), offset(0) {
if(s == 0) {
#if defined DEBUG && defined DBG_BUFFER
dbglog((format("... buffer: empty allocation try\n")).str())
#endif
return;
}
size_t siz = min<streamptr>((const streamptr)max_buf, s);
while(true) {
resize(siz);
if(size() >= siz)
break;
siz /= 2;
if(siz < fatx_context::get()->par.clus_size) {
clear();
#if defined DEBUG && defined DBG_BUFFER
dbglog((format("... buffer: no way to alloc 0x%08X 0x%016X %d\n") % &(*this)[0] % offset % siz).str())
#endif
return;
}
}
offset = o;
#if defined DEBUG && defined DBG_BUFFER
dbglog((format(".oO buffer: 0x%08X 0x%016X %d\n") % this % offset % (*this).size()).str())
#endif
}
buffer:: ~buffer() {
#if defined DEBUG && defined DBG_BUFFER
dbglog((format("Oo. buffer: 0x%08X 0x%016X %d\n") % this % offset % (*this).size()).str())
#endif
}
void buffer:: enlarge(const streamptr s) {
if(s > max_buf)
return;
resize(s);
if(size() < s)
return;
#if defined DEBUG && defined DBG_BUFFER
dbglog((format("OOO buffer: 0x%08X 0x%016X %d\n") % this % offset % (*this).size()).str())
#endif
}
#if defined DEBUG && defined DBGBUFDMP
void buffer:: operator () (size_t p) {
string res;
for(
size_t i = p;
i <
#ifdef DBGBUFDMP
p + min<string::size_type>((string::size_type)DBGBUFDMP, size());
#else
p + size();
#endif
i++
) {
res += (format(" %02X") % (unsigned int)(unsigned char)(*this)[i]).str();
if(((i + 1 - p) % DBGCR) == 0) {
dbglog(res + "\n")
res.clear();
}
}
dbglog(res + "\n")
}
#endif
void mutex:: genlock(char t, function<void()> lp) {
if(fatx_context::get()->mmi.prog == frontend::fuse) {
#if defined DEBUG && defined DBG_SEM
int nb = cpt++;
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(">>> Lock request [%c:%d-%s]\n") % t % nb % nam).str())
#else
(void) t;
#endif
#ifndef NO_LOCK
lp();
#else
(void) lp;
#endif
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(" done [%c:%d-%s].\n") % t % nb % nam).str())
#endif
}
}
void mutex:: genunlock(char t, function<void()> ulp) {
if(fatx_context::get()->mmi.prog == frontend::fuse) {
#if defined DEBUG && defined DBG_SEM
int nb = --cpt;
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format("<<< Lock release [%c:%d-%s]\n") % t % nb % nam).str())
#else
(void) t;
#endif
#ifndef NO_LOCK
ulp();
#else
(void) ulp;
#endif
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(" done [%c:%d-%s].\n") % t % nb % nam).str())
#endif
}
}
bool mutex:: gentimedlock(char t, function<bool()> plp) {
if(fatx_context::get()->mmi.prog == frontend::fuse) {
#if defined DEBUG && defined DBG_SEM
int nb = cpt++;
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(">>> Lock request [%c:%d-%s]\n") % t % nb % nam).str())
#else
(void) t;
#endif
bool res = true;
#ifndef NO_LOCK
res = plp();
#else
(void) plp;
#endif
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(" done [%c:%d-%s].\n") % t % nb % nam).str())
#endif
return res;
}
else
return true;
}
void mutex:: genunlockupgradableandlock(function<void()> ulualp) {
if(fatx_context::get()->mmi.prog == frontend::fuse) {
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(">>> Lock request [U->X-%s]\n") % nam).str())
#endif
#ifndef NO_LOCK
ulualp();
#else
(void) ulualp;
#endif
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(" done [%s].\n") % nam).str())
#endif
}
}
void mutex:: genunlockandlockupgradable(function<void()> ulalup) {
if(fatx_context::get()->mmi.prog == frontend::fuse) {
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format("<<< Lock release [X->U-%s]\n") % nam).str())
#endif
#ifndef NO_LOCK
ulalup();
#else
(void) ulalup;
#endif
#if defined DEBUG && defined DBG_SEM
#ifdef DBGSEM
if(nam == DBGSEM)
#endif
dbglog((format(" done [%s].\n") % nam).str())
#endif
}
}
#ifndef ENABLE_XBOX
void console:: write(const string s, bool err) {
(err ? cerr : cout) << s;
}
pair<bool, bool> console:: read() {
char c, d;
c = d = cin.get();
while(d != '\n')
d = cin.get();
return ((c == 'y') || (c == 'Y')) ? make_pair(true, true) : !((c == 'n') || (c == 'N')) ? make_pair(true, false) : make_pair(false, false);
}
#endif
fatx_context:: fatx_context(frontend& m): mmi(m), fat(nullptr), root(nullptr) {
}
fatx_context:: ~fatx_context() {
destroy();
set(nullptr);
}
bool fatx_context:: setup() {
if(!dev.setup())
return false;
if(mmi.runas.length() > 0 && !drop_privileges(mmi.runas))
return false;
if(!par.setup())
return false;
if(mmi.prog == frontend::fsck || mmi.prog == frontend::unrm || (mmi.prog == frontend::fuse && mmi.recover))
fat = new memmap(par);
else
fat = new dskmap(par);
#if defined DEBUG && defined DBG_INIT
dbglog("::EOMAP\n")
#endif
if(mmi.prog != frontend::mkfs) {
root = new entry();
#if defined DEBUG && defined DBG_INIT
dbglog("::EOENT\n")
#endif
}
return fat != nullptr && (mmi.prog == frontend::mkfs || root != nullptr);
}
void fatx_context:: destroy() {
delete root;
root = nullptr;
delete fat;
fat = nullptr;
}
#ifndef ENABLE_XBOX
frontend:: frontend(int ac, const char* const * const av) :
readonly(false), prog(unknown), force_y(false), force_n(false), force_a(false),
verbose(false), recover(false), local(false), deldate(true), dellost(true),
fuse_debug(false), fuse_foregrd(false), fuse_singlethr(false), nofat(false), argc(ac),
argv(av), progname(av[0]), dialog(true), lostfound(def_landf), foundfile(def_fpre),
filecount(0), mount(), volname(), fuse_option(), unkopt(),
partition("x2"), table(), clus_size(0), uid(getuid()), gid(getgid()),
mask(
#ifndef NO_FUSE
S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IXGRP |
S_IROTH | S_IXOTH
#else
0
#endif
), allyes(true), offset(0), size(0), input(), script(), runas("") {
}
bool frontend:: getanswer(bool def) {
bool res = false;
console::write(def ? " [Y/n] :" : " [y/N] :");
if(force_n) {
console::write("n\n");
res = false;
}
else if(force_y) {
console::write("y\n");
res = true;
}
else if(force_a) {
console::write(def ? "y\n" : "n\n");
res = def;
}
else {
pair<bool, bool> r = console::read();
res = r.first ? r.second : def;
}
allyes = allyes && res;
return res;
}
#endif
frontend:: ~frontend() {
progname.clear();
lostfound.clear();
foundfile.clear();
mount.clear();
volname.clear();
fuse_option.clear();
unkopt.clear();
partition.clear();
input.clear();
script.clear();
}
string frontend:: name() {
return
prog == fuse ? "fusefatx" :
prog == mkfs ? "mkfs.fatx" :
prog == fsck ? "fsck.fatx" :
prog == unrm ? "unrm.fatx" :
prog == label ? "label.fatx" :
"fatx"
;
}
bool frontend:: setup() {
size_t pos;
if((pos = progname.rfind(sepdir, progname.size())) != string::npos)
progname = progname.substr(pos + 1);
if(progname.compare("fusefatx") == 0)
prog = fuse;
if(progname.compare("mkfs.fatx") == 0) {
prog = mkfs;
dialog = false;
}
if(progname.compare("fsck.fatx") == 0) {
prog = fsck;
dialog = false;
}
if(progname.compare("unrm.fatx") == 0) {
prog = unrm;
recover = true;
dialog = false;
}
if(progname.compare("label.fatx") == 0) {
prog = label;
dialog = false;
}
#ifndef NO_OPTION
options_description hidden;
variables_map varmap;
hidden.add_options()
("default", "display options default values")
("as", value<string>(),
"choose program behavior\n"
"\"fuse\" for fusefatx,\n"
"\"fsck\" for fsck.fatx,\n"
"\"mkfs\" for mkfs.fatx,\n"
"\"unrm\" for unrm.fatx,\n"
"\"label\" for label.fatx"
)
("do", value<string>(), "send a script to the program")
;
vector<string> visopt;
try {
parsed_options parsed(command_line_parser(argc, argv)
.options(hidden)
.allow_unregistered()
.run()
);
store(parsed, varmap);
visopt = collect_unrecognized(parsed.options, include_positional);
notify(varmap);
}
catch(std::exception& e) {
ostringstream s;
s << hidden;
console::write(s.str());
s.str("");
s << e.what();
console::write(s.str() + "\n");
prog = unknown;
}
if(varmap.count("as")) {
if(varmap["as"].as<string>() == "fuse")
prog = fuse;
if(varmap["as"].as<string>() == "mkfs")
prog = mkfs;
if(varmap["as"].as<string>() == "fsck")
prog = fsck;
if(varmap["as"].as<string>() == "unrm")
prog = unrm;
if(varmap["as"].as<string>() == "label")
prog = label;
}
if(varmap.count("do"))
script = varmap["do"].as<string>();
#endif
if(prog == mkfs || prog == fsck || prog == unrm || prog == label)
dialog = false;
if(prog == unrm)
recover = true;
#ifdef DEBUG
dbglog((format("=> CALL: %s\n") % name()).str())
#endif
if(prog != fuse && prog != fsck && prog != mkfs && prog != unrm && prog != label) {
console::write(
"Invalid usage.\n"
"Please use link to this executable as:\n"
"- fusefatx\tto mount a filesystem with fuse\n"
"- mkfs.fatx\tto create a new filesystem\n"
"- fsck.fatx\tto check a filesystem\n"
"- unrm.fatx\tto try to recover deleted files\n"
"- label.fatx\tto display or change filesystem label\n"
);
return false;
}
#ifndef NO_OPTION
options_description visible((format("Usage: %s [options] device%s")
% name()
% (prog == fuse ? " mountpoint" : (prog == label ? " [label]" : ""))
).str());
visible.add_options()
("help,h", "produce help message")
("version", "produce version number")
("verbose,v", "verbose output")
("input,i", value<string>(), "set input device/file")
("offset", value<streamptr>(), "force partition offset")
("size", value<streamptr>(), "force partition size")
("partition,p", value<string>()->default_value("x2"),
"select partition:\n"
"\"sc\" for system cache,\n"
"\"gc\" for game cache,\n"
"\"cp\" for content partition,\n"
"\"x1\" for xbox 1,\n"
"\"x2\" for xbox 2 (default)"
)
;
if(prog == fuse) {
visible.add_options()
("mount,m", value<string>(), "set mountpoint")
("recover,r", "mount with deleted files")
("option,o", value<string>(), "mount options")
("debug,d", "enable debug output (implies -f)")
("foregrd,f", "foreground operation")
("singlethr,s", "fuse on single thread")
("uid", value<uid_t>(), "sets uid of the filesystem")
("gid", value<gid_t>(), "sets gid of the filesystem")
("mask", value<string>(), "sets mask for entries modes")
("runas", value<string>(), "drop privileges after opening input device")
;
}
if(prog == label || prog == mkfs) {
visible.add_options()
("label,l", value<string>(), "set volume name")
;
}
if(prog == mkfs) {
visible.add_options()
("cls-size,c", value<streamptr>(), "set num of blocks per cluster")
("table,b", value<string>(),
"select partition table:\n"
"\"mu\" for Memory Unit,\n"
"\"file\" for plain file,\n"
"\"hd\" for XBOX360 HDD,\n"
"\"kit\" for DevKit HDD,\n"
"\"usb\" for USB Drive"
)
;
}
if(prog == fsck || prog == unrm || prog == mkfs) {
visible.add_options()
("all,y", "answer yes to everything")
("none,n", "answer no to everything")
("auto,a", "default answer to everything")
;
}
if(prog == fsck || prog == unrm || prog == mkfs || prog == fuse) {
visible.add_options()
("test,t", "test mode, no modification done")
;
}
if(prog == unrm) {
visible.add_options()
("local,l", "recover files in local filesystem")
;
}
if(prog == fsck || prog == unrm) {
visible.add_options()
("nofat,f", "disable FAT sanity check and recovery")
;
}
if(prog == fuse || prog == unrm) {
visible.add_options()
("nodate", "dates of deleted files don't care")
("nolost", "don't care of lost chains")
;
}
positional_options_description podesc;
podesc.add("input", 1);
if(prog == fuse)
podesc.add("mount", 1);
if(prog == label)
podesc.add("label", 1);
try {
if(prog == fuse) {
parsed_options parsed(command_line_parser(visopt)
.options(visible)
.positional(podesc)
.allow_unregistered()
.run()
);
store(parsed, varmap);
unkopt = collect_unrecognized(parsed.options, exclude_positional);
}
else {
parsed_options parsed(command_line_parser(visopt)
.options(visible)
.positional(podesc)
.run()
);
store(parsed, varmap);
}
notify(varmap);
}
catch(std::exception& e) {
ostringstream s;
s << visible;
console::write(s.str());
s.str("");
s << e.what();
console::write(s.str() + "\n");
prog = unknown;
}
if(varmap.count("all"))
force_y = true;
if(varmap.count("none"))
force_n = true;
if(varmap.count("auto"))
force_a = true;
if(varmap.count("test"))
readonly = true;
if(varmap.count("verbose"))
verbose = true;
if(varmap.count("recover")) {
recover = true;
readonly = true;
}
if(varmap.count("local")) {
local = true;
readonly = true;
}
if(varmap.count("nofat")) {
nofat = true;
}
if(varmap.count("nodate")) {
deldate = false;
if(prog == fuse) {
recover = true;
readonly = true;
}
}
if(varmap.count("nolost")) {
dellost = false;
if(prog == fuse) {
recover = true;
readonly = true;
}
}
if(varmap.count("debug")) {
fuse_debug = true;
fuse_foregrd = true;
}
if(varmap.count("singlethr"))
fuse_singlethr = true;
if(varmap.count("foregrd"))
fuse_foregrd = true;
if(varmap.count("mount"))
mount = varmap["mount"].as<string>();
if(varmap.count("label"))
volname = varmap["label"].as<string>();
if(varmap.count("partition"))
partition = varmap["partition"].as<string>();
if(varmap.count("table"))
table = varmap["table"].as<string>();
if(varmap.count("cls-size"))
clus_size = varmap["cls-size"].as<streamptr>();
if(varmap.count("uid"))
uid = varmap["uid"].as<uid_t>();
if(varmap.count("gid"))
gid = varmap["gid"].as<gid_t>();
if(varmap.count("mask")) {
stringstream ss;
ss << std::oct << varmap["mask"].as<string>();
ss >> mask;
}
if(varmap.count("offset"))
offset = varmap["offset"].as<streamptr>();
if(varmap.count("size"))
size = varmap["size"].as<streamptr>();
if(varmap.count("input"))
input = varmap["input"].as<string>();
if(varmap.count("runas"))
runas = varmap["runas"].as<string>();
if(prog == label)
readonly = !varmap.count("label");
if(varmap.count("option")) {
tokenizer<char_separator<char> > opts(varmap["option"].as<string>(), char_separator<char>(","));
for(const string& o: opts) {
if(o == "ro")
readonly = true;
else {
if(!fuse_option.empty())
fuse_option += ",";
fuse_option += o;
}
}
}
if(varmap.count("version")) {
console::write((format("%s v%s.\nCopyright (C) 2012, 2013, 2014 Christophe Duverger.\n\n")
% name()
% VERSION
).str());
console::write(
"This program comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions.\n"
);
prog = unknown;
return true;
}
if(varmap.count("default")) {
console::write(
(format("force yes\t%d\n") % force_y).str() +
(format("force no\t%d\n") % force_n).str() +
(format("force auto\t%d\n") % force_a).str() +
(format("verbose\t\t%d\n") % verbose).str() +
(format("read only\t%d\n") % readonly).str() +
(format("fuse recover\t%d\n") % recover).str() +
(format("local recover\t%d\n") % local).str() +
(format("deleted dates\t%d\n") % deldate).str() +
(format("preserve losts\t%d\n") % dellost).str() +
(format("partition\t%d\n") % partition).str() +
(format("fuse debug\t%d\n") % fuse_debug).str() +
(format("fuse foregrd\t%d\n") % fuse_foregrd).str() +
(format("fuse singlethr\t%d\n") % fuse_singlethr).str() +
(format("uid\t\t%d\n") % uid).str() +
(format("gid\t\t%d\n") % gid).str() +
(format("mask\t\t%03o\n") % mask).str()
);
return false;
}
if(varmap.count("help") || !varmap.count("input") || (prog == fuse && !varmap.count("mount"))) {
ostringstream s;
s << visible;
console::write(s.str());
return false;
}
#endif
#if defined DEBUG && defined DBG_INIT
dbglog("::EOMMI\n")
#endif
return true;
}
void frontend:: parser() {
#ifndef ENABLE_XBOX
script.erase(remove_if(script.begin(), script.end(), [] (const char c) ->bool { return c == ' ' || c == '\t' || c == '\n'; }), script.end());
tokenizer<char_separator<char> > cmds(script, char_separator<char>(";"));
for(string cmd: cmds) {
tokenizer<char_separator<char> > args(cmd, char_separator<char>(","));
tokenizer<char_separator<char> >::iterator i = args.begin();
if(i->empty())
break;
else if((*i)[0] == '#')
continue;
else if(*i == "mkdir" && ++i != args.end() && !i->empty()) {
console::write("mkdir:");
if(!writeable()) {
console::write("read-only\n");
continue;
}
size_t l = i->find_last_of(sepdir);
if(l == string::npos || l == i->length() - 1) {
console::write("nothing\n");
continue;
}
entry* n = new entry(i->substr(l + 1), 0, true);
entry* s = fatx_context::get()->root->find(&(i->substr(0, l))[0]);
if(s == nullptr) {
console::write("nothing\n");
continue;
}
s->addtodir(n);
console::write(n->path() + "\n");
}
else if(*i == "rmdir" && ++i != args.end() && !i->empty()) {
console::write("rmdir:");
if(!writeable()) {
console::write("read-only\n");
continue;
}
entry* n = fatx_context::get()->root->find(&(*i)[0]);
if(n == nullptr || !n->flags.dir) {
console::write("nothing\n");
continue;
}
if(n->childs.size() != 0) {
console::write("not empty\n");
continue;
}
console::write(n->path() + "\n");
n->parent->remfrdir(n);
}
else if(*i == "cp" && ++i != args.end() && !i->empty()) {
console::write("cp:");
if(!writeable()) {
console::write("read-only\n");
continue;
}
entry* s = fatx_context::get()->root->find(&(*i++)[0]);
if(s == nullptr) {
console::write("nothing\n");
continue;
}
size_t l = i->find_last_of(sepdir);
if(l == string::npos || l == i->length() - 1) {
console::write("nothing\n");
continue;
}
entry* d = fatx_context::get()->root->find(&(i->substr(0, l))[0]);
if(d == nullptr) {
console::write("nothing\n");
continue;
}
entry* n = new entry(i->substr(l + 1), s->size);
d->addtodir(n);
string b(s->size, '\0');
s->data(&b[0], true, 0, s->size);
n->data(&b[0], false, 0, n->size);
console::write(n->path() + "\n");
}
else if(*i == "rcp" && ++i != args.end() && !i->empty()) {
console::write("rcp:");
ifstream s(&(*i++)[0], ios::binary);
if(!s) {
console::write("nothing\n");
continue;
}
s.seekg(0, ios::end);
streamptr size = s.tellg();
s.seekg(0, ios::beg);
size_t l = i->find_last_of(sepdir);
if(l == string::npos || l == i->length() - 1) {
console::write("nothing\n");
continue;