forked from nlitsme/extfstools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext2rd.cpp
More file actions
1575 lines (1421 loc) · 52.1 KB
/
ext2rd.cpp
File metadata and controls
1575 lines (1421 loc) · 52.1 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
/*
* Author: Willem Hengeveld <itsme@xs4all.nl>
*/
#include <stdio.h>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <string>
#include <ctime> // gmtime, strftime
#ifdef USE_CPPUTILS
#include "mmfile.h"
#include "util/rw/MemoryReader.h"
#else
#include "util/rw/MmapReader.h"
#endif
#include "util/rw/FileReader.h"
#include "util/rw/BlockDevice.h"
#include "util/rw/OffsetReader.h"
#include "args.h"
#include <memory>
#include <system_error>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
void lutimes(const char*path, const struct timeval *times)
{
}
int symlink(const char *src, const char *dst)
{
return 0;
}
int mkdir(const char *src, int mode)
{
return _mkdir(src);
}
#endif
// ~/gitprj/repos/linux/fs/ext2/ext2.h
// todo: add 64bit support from ext4
// todo: create ExtentsFileReader and BlocksFileReader
#define ROOTDIRINODE 2
// see linux/fs/ext4/ext4.h
enum {EXT4_FT_UNKNOWN, EXT4_FT_REG_FILE, EXT4_FT_DIR, EXT4_FT_CHRDEV, EXT4_FT_BLKDEV, EXT4_FT_FIFO, EXT4_FT_SOCK, EXT4_FT_SYMLINK };
#define EXT4_FEATURE_COMPAT_DIR_PREALLOC 0x0001
#define EXT4_FEATURE_COMPAT_IMAGIC_INODES 0x0002
#define EXT4_FEATURE_COMPAT_HAS_JOURNAL 0x0004
#define EXT4_FEATURE_COMPAT_EXT_ATTR 0x0008
#define EXT4_FEATURE_COMPAT_RESIZE_INODE 0x0010
#define EXT4_FEATURE_COMPAT_DIR_INDEX 0x0020
#define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
#define EXT4_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
#define EXT4_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
#define EXT4_FEATURE_RO_COMPAT_HUGE_FILE 0x0008
#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
#define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020
#define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040
#define EXT4_FEATURE_RO_COMPAT_QUOTA 0x0100
#define EXT4_FEATURE_RO_COMPAT_BIGALLOC 0x0200
/*
* METADATA_CSUM also enables group descriptor checksums (GDT_CSUM). When
* METADATA_CSUM is set, group descriptor checksums use the same algorithm as
* all other data structures' checksums. However, the METADATA_CSUM and
* GDT_CSUM bits are mutually exclusive.
*/
#define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM 0x0400
#define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001
#define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002
#define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
#define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
#define EXT4_FEATURE_INCOMPAT_META_BG 0x0010
#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
#define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
#define EXT4_FEATURE_INCOMPAT_MMP 0x0100
#define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
#define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400 /* EA in inode */
#define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000 /* data in dirent */
#define EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */
#define EXT4_FEATURE_INCOMPAT_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */
#define EXT4_FEATURE_INCOMPAT_INLINE_DATA 0x8000 /* data in inode */
std::string timestr(uint32_t t32)
{
time_t t = t32;
struct tm tm = *std::gmtime(&t);
char str[40];
std::strftime(str, 40, "%Y-%m-%d %H:%M:%S", &tm);
return str;
}
struct DirectoryEntry {
uint32_t inode;
uint8_t filetype; // 1=file, 2=dir, 3=chardev, 4=blockdev, 5=fifo, 6=sock, 7=symlink
std::string name;
size_t parse(const uint8_t *first)
{
const uint8_t *p= first;
inode= get32le(p); p+=4;
uint32_t rec_len= get16le(p);p+=2;
uint32_t name_len= get8(p); p+=1;
filetype= get8(p); p+=1;
name= std::string((const char*)p, name_len);
return rec_len;
}
void dump() const
{
printf("%8d %c '%s'\n", inode, "0-dcbpsl"[filetype&7], name.c_str());
}
};
struct SuperBlock {
uint32_t s_inodes_count;
uint32_t s_blocks_count;
uint32_t s_r_blocks_count;
uint32_t s_free_blocks_count;
uint32_t s_free_inodes_count;
uint32_t s_first_data_block; // 0 or 1
uint32_t s_log_block_size; // blocksize = 1024<<s_log_block_size
int32_t s_log_frag_size; // fragsize = 1024<<s_log_frag_size
uint32_t s_blocks_per_group;
uint32_t s_frags_per_group;
uint32_t s_inodes_per_group;
uint32_t s_mtime;
uint32_t s_wtime;
uint16_t s_mnt_count;
uint16_t s_max_mnt_count;
uint16_t s_magic; // ef53
uint16_t s_state;
uint16_t s_errors;
uint16_t s_minor_rev_level;
uint32_t s_lastcheck;
uint32_t s_checkinterval;
uint32_t s_creator_os;
uint32_t s_rev_level;
uint16_t s_def_resuid;
uint16_t s_def_resgid;
uint32_t s_first_ino;
uint16_t s_inode_size;
uint16_t s_block_group_nr;
uint32_t s_feature_compat;
uint32_t s_feature_incompat;
uint32_t s_feature_ro_compat;
uint8_t s_uuid[16];
uint8_t s_volume_name[16];
uint8_t s_last_mounted[64];
uint32_t s_algo_bitmap;
ReadWriter_ptr r;
void parse(ReadWriter_ptr rd)
{
r= rd;
uint8_t buf[0xCC];
r->read(buf, 0xCC);
//printf("%s\n", hexdump(r->getpos()-0xCC, buf, 0xCC).c_str());
parse(buf, 0xCC);
}
void parse(const uint8_t *first, size_t size)
{
const uint8_t *p= first;
s_inodes_count = get32le(p); p+=4; // 0000;
s_blocks_count = get32le(p); p+=4; // 0004;
s_r_blocks_count = get32le(p); p+=4; // 0008;
s_free_blocks_count = get32le(p); p+=4; // 000c;
s_free_inodes_count = get32le(p); p+=4; // 0010;
s_first_data_block = get32le(p); p+=4; // 0014;
s_log_block_size = get32le(p); p+=4; // 0018;
s_log_frag_size = get32le(p); p+=4; // 001c;
s_blocks_per_group = get32le(p); p+=4; // 0020;
s_frags_per_group = get32le(p); p+=4; // 0024;
s_inodes_per_group = get32le(p); p+=4; // 0028;
s_mtime = get32le(p); p+=4; // 002c;
s_wtime = get32le(p); p+=4; // 0030;
s_mnt_count = get16le(p); p+=2; // 0034;
s_max_mnt_count = get16le(p); p+=2; // 0036;
s_magic = get16le(p); p+=2; // 0038;
s_state = get16le(p); p+=2; // 003a;
s_errors = get16le(p); p+=2; // 003c;
s_minor_rev_level = get16le(p); p+=2; // 003e;
s_lastcheck = get32le(p); p+=4; // 0040;
s_checkinterval = get32le(p); p+=4; // 0044;
s_creator_os = get32le(p); p+=4; // 0048;
s_rev_level = get32le(p); p+=4; // 004c;
s_def_resuid = get16le(p); p+=2; // 0050;
s_def_resgid = get16le(p); p+=2; // 0052;
s_first_ino= get32le(p); p+=4; // 0054;
s_inode_size= get16le(p); p+=2; // 0058;
s_block_group_nr= get16le(p); p+=2; // 005a;
s_feature_compat= get32le(p); p+=4; // 005c;
s_feature_incompat= get32le(p); p+=4; // 0060;
s_feature_ro_compat= get32le(p); p+=4; // 0064;
memcpy(s_uuid, p, 16); p+=16; // 0068;
memcpy(s_volume_name, p, 16); p+=16; // 0078;
memcpy(s_last_mounted, p, 64); p+=64; // 0088;
s_algo_bitmap= get32le(p); p+=4; // 00c8;
}
uint64_t blocksize() const { return 1024<<s_log_block_size; }
uint64_t fragsize() const {
if (s_log_frag_size <0)
return 1024>>(-s_log_frag_size);
else
return 1024<<s_log_frag_size;
}
uint64_t bytespergroup() const { return s_blocks_per_group*blocksize(); }
size_t ngroups() const { return s_inodes_count/s_inodes_per_group; }
void dump() const
{
printf("s_inodes_count=0x%08x\n", s_inodes_count);
printf("s_blocks_count=0x%08x\n", s_blocks_count);
printf("s_r_blocks_count=0x%08x\n", s_r_blocks_count);
printf("s_free_blocks_count=0x%08x\n", s_free_blocks_count);
printf("s_free_inodes_count=0x%08x\n", s_free_inodes_count);
printf("s_first_data_block=0x%08x\n", s_first_data_block);
printf("s_log_block_size=%d\n", s_log_block_size);
printf("s_log_frag_size=%d\n", s_log_frag_size);
printf("s_blocks_per_group=0x%08x\n", s_blocks_per_group);
printf("s_frags_per_group=0x%08x\n", s_frags_per_group);
printf("s_inodes_per_group=0x%08x\n", s_inodes_per_group);
printf("s_mtime=0x%08x : %s\n", s_mtime, timestr(s_mtime).c_str());
printf("s_wtime=0x%08x : %s\n", s_wtime, timestr(s_wtime).c_str());
printf("s_mnt_count=0x%04x\n", s_mnt_count);
printf("s_max_mnt_count=0x%04x\n", s_max_mnt_count);
printf("s_magic=0x%04x\n", s_magic);
printf("s_state=0x%04x\n", s_state);
printf("s_errors=0x%04x\n", s_errors);
printf("s_minor_rev_level=0x%04x\n", s_minor_rev_level);
printf("s_lastcheck=0x%08x : %s\n", s_lastcheck, timestr(s_lastcheck).c_str());
printf("s_checkinterval=0x%08x\n", s_checkinterval);
printf("s_creator_os=0x%08x\n", s_creator_os);
printf("s_rev_level=0x%08x\n", s_rev_level);
printf("s_def_resuid=0x%04x\n", s_def_resuid);
printf("s_def_resgid=0x%04x\n", s_def_resgid);
printf("i->%d, b->%d groups\n", s_inodes_count/s_inodes_per_group, (s_blocks_count+s_blocks_per_group-1)/s_blocks_per_group);
printf("s_first_ino=%d\n", s_first_ino);
printf("s_inode_size=%d\n", s_inode_size);
printf("s_block_group_nr=%d\n", s_block_group_nr);
printf("s_feature_compat=0x%08x: %s\n", s_feature_compat, feat_compat2str(s_feature_compat).c_str());
printf("s_feature_incompat=0x%08x: %s\n", s_feature_incompat, feat_incompat2str(s_feature_incompat).c_str());
printf("s_feature_ro_compat=0x%08x: %s\n", s_feature_ro_compat, feat_rocompat2str(s_feature_ro_compat).c_str());
printf("s_uuid=%s\n", hexdump(s_uuid, 16).c_str());
printf("s_volume_name=%s\n", ascdump(s_volume_name, 16).c_str());
printf("s_last_mounted=%s\n", ascdump(s_last_mounted, 16).c_str());
printf("s_algo_bitmap=0x%08x\n", s_algo_bitmap);
}
static std::string feat_compat2str(uint32_t feat)
{
StringList l;
uint32_t all = EXT4_FEATURE_COMPAT_DIR_PREALLOC|EXT4_FEATURE_COMPAT_IMAGIC_INODES|EXT4_FEATURE_COMPAT_HAS_JOURNAL|EXT4_FEATURE_COMPAT_EXT_ATTR|EXT4_FEATURE_COMPAT_RESIZE_INODE|EXT4_FEATURE_COMPAT_DIR_INDEX;
if (feat&EXT4_FEATURE_COMPAT_DIR_PREALLOC) l.push_back("DIR_PREALLOC");
if (feat&EXT4_FEATURE_COMPAT_IMAGIC_INODES) l.push_back("IMAGIC_INODES");
if (feat&EXT4_FEATURE_COMPAT_HAS_JOURNAL) l.push_back("HAS_JOURNAL");
if (feat&EXT4_FEATURE_COMPAT_EXT_ATTR) l.push_back("EXT_ATTR");
if (feat&EXT4_FEATURE_COMPAT_RESIZE_INODE) l.push_back("RESIZE_INODE");
if (feat&EXT4_FEATURE_COMPAT_DIR_INDEX) l.push_back("DIR_INDEX");
if (feat&~all) l.push_back(stringformat("unk_%x", feat&~all));
return JoinStringList(l, ",");
}
static std::string feat_incompat2str(uint32_t feat)
{
StringList l;
uint32_t all = EXT4_FEATURE_INCOMPAT_COMPRESSION|EXT4_FEATURE_INCOMPAT_FILETYPE|EXT4_FEATURE_INCOMPAT_RECOVER|EXT4_FEATURE_INCOMPAT_JOURNAL_DEV|EXT4_FEATURE_INCOMPAT_META_BG|EXT4_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_64BIT|EXT4_FEATURE_INCOMPAT_MMP|EXT4_FEATURE_INCOMPAT_FLEX_BG|EXT4_FEATURE_INCOMPAT_EA_INODE|EXT4_FEATURE_INCOMPAT_DIRDATA|EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM|EXT4_FEATURE_INCOMPAT_LARGEDIR|EXT4_FEATURE_INCOMPAT_INLINE_DATA;
if (feat&EXT4_FEATURE_INCOMPAT_COMPRESSION) l.push_back("COMPRESSION");
if (feat&EXT4_FEATURE_INCOMPAT_FILETYPE) l.push_back("FILETYPE");
if (feat&EXT4_FEATURE_INCOMPAT_RECOVER) l.push_back("RECOVER");
if (feat&EXT4_FEATURE_INCOMPAT_JOURNAL_DEV) l.push_back("JOURNAL_DEV");
if (feat&EXT4_FEATURE_INCOMPAT_META_BG) l.push_back("META_BG");
if (feat&EXT4_FEATURE_INCOMPAT_EXTENTS) l.push_back("EXTENTS");
if (feat&EXT4_FEATURE_INCOMPAT_64BIT) l.push_back("64BIT");
if (feat&EXT4_FEATURE_INCOMPAT_MMP) l.push_back("MMP");
if (feat&EXT4_FEATURE_INCOMPAT_FLEX_BG) l.push_back("FLEX_BG");
if (feat&EXT4_FEATURE_INCOMPAT_EA_INODE) l.push_back("EA_INODE");
if (feat&EXT4_FEATURE_INCOMPAT_DIRDATA) l.push_back("DIRDATA");
if (feat&EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM) l.push_back("BG_USE_META_CSUM");
if (feat&EXT4_FEATURE_INCOMPAT_LARGEDIR) l.push_back("LARGEDIR");
if (feat&EXT4_FEATURE_INCOMPAT_INLINE_DATA) l.push_back("INLINE_DATA");
if (feat&~all) l.push_back(stringformat("unk_%x", feat&~all));
return JoinStringList(l, ",");
}
static std::string feat_rocompat2str(uint32_t feat)
{
StringList l;
uint32_t all = EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER|EXT4_FEATURE_RO_COMPAT_LARGE_FILE|EXT4_FEATURE_RO_COMPAT_BTREE_DIR|EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|EXT4_FEATURE_RO_COMPAT_DIR_NLINK|EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|EXT4_FEATURE_RO_COMPAT_QUOTA|EXT4_FEATURE_RO_COMPAT_BIGALLOC|EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
if (feat&EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) l.push_back("SPARSE_SUPER");
if (feat&EXT4_FEATURE_RO_COMPAT_LARGE_FILE) l.push_back("LARGE_FILE");
if (feat&EXT4_FEATURE_RO_COMPAT_BTREE_DIR) l.push_back("BTREE_DIR");
if (feat&EXT4_FEATURE_RO_COMPAT_HUGE_FILE) l.push_back("HUGE_FILE");
if (feat&EXT4_FEATURE_RO_COMPAT_GDT_CSUM) l.push_back("GDT_CSUM");
if (feat&EXT4_FEATURE_RO_COMPAT_DIR_NLINK) l.push_back("DIR_NLINK");
if (feat&EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) l.push_back("EXTRA_ISIZE");
if (feat&EXT4_FEATURE_RO_COMPAT_QUOTA) l.push_back("QUOTA");
if (feat&EXT4_FEATURE_RO_COMPAT_BIGALLOC) l.push_back("BIGALLOC");
if (feat&EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) l.push_back("METADATA_CSUM");
if (feat&~all) l.push_back(stringformat("unk_%x", feat&~all));
return JoinStringList(l, ",");
}
ByteVector getblock(unsigned n) const
{
if (n>=s_blocks_count)
throw "blocknr too large";
ByteVector buf(blocksize());
r->setpos(blocksize()*n);
r->read(&buf[0], blocksize());
return buf;
}
};
typedef std::function<bool(const uint8_t*)> BLOCKCALLBACK;
struct ExtentNode {
virtual ~ExtentNode() { }
void parse(ReadWriter_ptr r)
{
uint8_t buf[12];
r->read(buf, 12);
parse(buf);
}
virtual void parse(const uint8_t *first)= 0;
virtual void dump() const= 0;
virtual bool enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const= 0;
};
struct ExtentLeaf : ExtentNode {
uint32_t ee_block;
uint16_t ee_len;
uint16_t ee_start_hi;
uint32_t ee_start_lo;
ExtentLeaf(const uint8_t*first)
{
parse(first);
}
virtual void parse(const uint8_t *first)
{
const uint8_t *p= first;
ee_block = get32le(p); p+=4;
ee_len = get16le(p); p+=2;
ee_start_hi = get16le(p); p+=2;
ee_start_lo = get32le(p); p+=4;
}
virtual void dump() const
{
printf("blk:%08x, l=%d, %010" PRIx64 "\n", ee_block, ee_len, startblock());
}
uint64_t startblock() const
{
return (uint64_t(ee_start_hi)<<32)|ee_start_lo;
}
virtual bool enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const
{
uint64_t blk= startblock();
for (unsigned i=0 ; i<ee_len ; i++)
if (!cb(&super.getblock(blk++).front()))
return false;
return true;
}
};
struct ExtentInternal : ExtentNode {
uint32_t ei_block;
uint32_t ei_leaf_lo;
uint16_t ei_leaf_hi;
uint16_t ei_unused;
ExtentInternal(const uint8_t*first)
{
parse(first);
}
virtual void parse(const uint8_t *first)
{
const uint8_t *p= first;
ei_block = get32le(p); p+=4;
ei_leaf_lo = get32le(p); p+=4;
ei_leaf_hi = get16le(p); p+=2;
ei_unused = get16le(p); p+=2;
}
virtual void dump() const
{
printf("blk:%08x, [%d] %010" PRIx64 "\n", ei_block, ei_unused, leafnode());
}
uint64_t leafnode() const
{
return (uint64_t(ei_leaf_hi)<<32)|ei_leaf_lo;
}
virtual bool enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const;
};
struct ExtentHeader {
uint16_t eh_magic; /* probably will support different formats */
uint16_t eh_entries; /* number of valid entries */
uint16_t eh_max; /* capacity of store in entries */
uint16_t eh_depth; /* has tree real underlying blocks? */
uint32_t eh_generation; /* generation of the tree */
void parse(const uint8_t *first)
{
const uint8_t *p= first;
eh_magic = get16le(p); p+=2;
eh_entries = get16le(p); p+=2;
eh_max = get16le(p); p+=2;
eh_depth = get16le(p); p+=2;
eh_generation= get32le(p);p+=4;
}
void dump() const
{
printf("M%04x, N%d,max%d, d:%d, g:%d\n", eh_magic, eh_entries, eh_max, eh_depth, eh_generation);
}
};
struct Extent {
ExtentHeader eh;
std::vector< std::shared_ptr<ExtentNode> > extents;
void parse(const uint8_t *first)
{
const uint8_t *p= first;
eh.parse(p); p+=12;
if (eh.eh_magic != 0xf30a) {
printf("ehmagic=%04x - %s\n", eh.eh_magic, hexdump(p-12, 12).c_str());
//throw "invalid extent hdr magic";
return;
}
for (unsigned i=0 ; i<eh.eh_entries ; i++) {
if (eh.eh_depth==0)
extents.push_back(std::make_shared<ExtentLeaf>(p));
else
extents.push_back(std::make_shared<ExtentInternal>(p));
p+=12;
}
}
virtual bool enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const
{
for (unsigned i=0 ; i<eh.eh_entries ; i++)
if (!extents[i]->enumblocks(super, cb))
return false;
return true;
}
void dump() const
{
eh.dump();
for (unsigned i=0 ; i<extents.size() ; i++) {
printf("EXT %d: ", i);
extents[i]->dump();
}
}
};
bool ExtentInternal::enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const
{
Extent e;
e.parse(&super.getblock(leafnode()).front());
return e.enumblocks(super, cb);
}
// see linux/include/uapi/linux/stat.h
enum { EXT4_S_IFIFO=0x1000, EXT4_S_IFCHR=0x2000, EXT4_S_IFDIR=0x4000, EXT4_S_IFBLK=0x6000, EXT4_S_IFREG=0x8000, EXT4_S_IFLNK=0xa000, EXT4_S_IFSOCK=0xc000 };
// see linux/fs/ext4/ext4.h
#define EXT4_SECRM_FL 0x00000001 /* Secure deletion */
#define EXT4_UNRM_FL 0x00000002 /* Undelete */
#define EXT4_COMPR_FL 0x00000004 /* Compress file */
#define EXT4_SYNC_FL 0x00000008 /* Synchronous updates */
#define EXT4_IMMUTABLE_FL 0x00000010 /* Immutable file */
#define EXT4_APPEND_FL 0x00000020 /* writes to file may only append */
#define EXT4_NODUMP_FL 0x00000040 /* do not dump file */
#define EXT4_NOATIME_FL 0x00000080 /* do not update atime */
/* Reserved for compression usage... */
#define EXT4_DIRTY_FL 0x00000100
#define EXT4_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
#define EXT4_NOCOMPR_FL 0x00000400 /* Don't compress */
#define EXT4_ECOMPR_FL 0x00000800 /* Compression error */
/* End compression flags --- maybe not all used */
#define EXT4_INDEX_FL 0x00001000 /* hash-indexed directory */
#define EXT4_IMAGIC_FL 0x00002000 /* AFS directory */
#define EXT4_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
#define EXT4_NOTAIL_FL 0x00008000 /* file tail should not be merged */
#define EXT4_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
#define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
#define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
#define EXT4_EA_INODE_FL 0x00200000 /* Inode used for large EA */
#define EXT4_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
#define EXT4_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
#define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
struct Inode {
uint16_t i_mode; // 000
uint16_t i_uid; // 002
uint32_t i_size; // 004
uint32_t i_atime; // 008
uint32_t i_ctime; // 00c
uint32_t i_mtime; // 010
uint32_t i_dtime; // 014
uint16_t i_gid; // 018
uint16_t i_links_count;// 01a
uint32_t i_blocks; // 01c// 512 byte blocks
uint32_t i_flags; // 020
uint32_t i_osd1; // 024
uint32_t i_block[15]; // 028
std::string symlink;
Extent e;
uint32_t i_generation; // 064
uint32_t i_file_acl; // 068
uint32_t i_dir_acl; // 06c
uint32_t i_faddr; // 070
uint8_t i_osd2[12]; // 074
bool _empty;
void parse(const uint8_t *first, size_t size)
{
_empty = std::find_if(first, first+size, [](uint8_t b) { return b!=0; })==first+size;
const uint8_t *p= first;
i_mode= get16le(p); p+=2;
i_uid= get16le(p); p+=2;
i_size= get32le(p); p+=4;
i_atime= get32le(p); p+=4;
i_ctime= get32le(p); p+=4;
i_mtime= get32le(p); p+=4;
i_dtime= get32le(p); p+=4;
i_gid= get16le(p); p+=2;
i_links_count= get16le(p); p+=2;
i_blocks= get32le(p); p+=4;
i_flags= get32le(p); p+=4;
i_osd1= get32le(p); p+=4;
if (issymlink()) {
symlink= std::string((const char*)p, 60);
p+=60;
}
else if (i_flags&EXT4_EXTENTS_FL) {
e.parse(p); p+=60;
}
else {
for (int i=0 ; i<15 ; i++) {
i_block[i]= get32le(p); p+=4;
}
}
i_generation= get32le(p); p+=4;
i_file_acl= get32le(p); p+=4;
i_dir_acl= get32le(p); p+=4;
i_faddr= get32le(p); p+=4;
memcpy(i_osd2, p, 12); p+=12;
}
bool issymlink() const {
return (i_mode&0xf000)==EXT4_S_IFLNK && i_size<60;
}
void dump() const
{
printf("m:%06o %4d o[%5d %5d] t[%10d %10d %10d %10d] %12" PRIu64 " [b:%8d] F:%05x(%s) X:%08x %s\n",
i_mode, i_links_count, i_gid, i_uid, i_atime, i_ctime, i_mtime, i_dtime, datasize(), i_blocks, i_flags, fl2str(i_flags).c_str(), i_file_acl, hexdump(i_osd2, 12).c_str());
if (issymlink()) {
printf("symlink: %s\n", symlink.c_str());
}
else if (i_flags&EXT4_EXTENTS_FL) {
e.dump();
}
else {
printf("blocks: ");
for (int i=0 ; i<12 ; i++)
printf(" %08x", i_block[i]);
printf(" i1:%08x, i2:%08x, i3:%08x\n", i_block[12], i_block[13], i_block[14]);
}
}
static std::string fl2str(uint32_t fl)
{
StringList l;
uint32_t all = EXT4_SECRM_FL|EXT4_UNRM_FL|EXT4_COMPR_FL|EXT4_SYNC_FL|EXT4_IMMUTABLE_FL|EXT4_APPEND_FL|EXT4_NODUMP_FL|EXT4_NOATIME_FL|EXT4_DIRTY_FL|EXT4_COMPRBLK_FL|EXT4_NOCOMPR_FL|EXT4_ECOMPR_FL|EXT4_INDEX_FL|EXT4_IMAGIC_FL|EXT4_JOURNAL_DATA_FL|EXT4_NOTAIL_FL|EXT4_DIRSYNC_FL|EXT4_TOPDIR_FL|EXT4_HUGE_FILE_FL|EXT4_EXTENTS_FL|EXT4_EA_INODE_FL|EXT4_EOFBLOCKS_FL|EXT4_INLINE_DATA_FL|EXT4_RESERVED_FL;
if (fl&EXT4_SECRM_FL) l.push_back("SECRM");
if (fl&EXT4_UNRM_FL) l.push_back("UNRM");
if (fl&EXT4_COMPR_FL) l.push_back("COMPR");
if (fl&EXT4_SYNC_FL) l.push_back("SYNC");
if (fl&EXT4_IMMUTABLE_FL) l.push_back("IMMUTABLE");
if (fl&EXT4_APPEND_FL) l.push_back("APPEND");
if (fl&EXT4_NODUMP_FL) l.push_back("NODUMP");
if (fl&EXT4_NOATIME_FL) l.push_back("NOATIME");
if (fl&EXT4_DIRTY_FL) l.push_back("DIRTY");
if (fl&EXT4_COMPRBLK_FL) l.push_back("COMPRBLK");
if (fl&EXT4_NOCOMPR_FL) l.push_back("NOCOMPR");
if (fl&EXT4_ECOMPR_FL) l.push_back("ECOMPR");
if (fl&EXT4_INDEX_FL) l.push_back("INDEX");
if (fl&EXT4_IMAGIC_FL) l.push_back("IMAGIC");
if (fl&EXT4_JOURNAL_DATA_FL) l.push_back("JOURNAL_DATA");
if (fl&EXT4_NOTAIL_FL) l.push_back("NOTAIL");
if (fl&EXT4_DIRSYNC_FL) l.push_back("DIRSYNC");
if (fl&EXT4_TOPDIR_FL) l.push_back("TOPDIR");
if (fl&EXT4_HUGE_FILE_FL) l.push_back("HUGE_FILE");
if (fl&EXT4_EXTENTS_FL) l.push_back("EXTENTS");
if (fl&EXT4_EA_INODE_FL) l.push_back("EA_INODE");
if (fl&EXT4_EOFBLOCKS_FL) l.push_back("EOFBLOCKS");
if (fl&EXT4_INLINE_DATA_FL) l.push_back("INLINE_DATA");
if (fl&EXT4_RESERVED_FL) l.push_back("RESERVED");
if (fl&~all) l.push_back(stringformat("unk_%x", fl&~all));
return JoinStringList(l, ",");
}
uint64_t datasize() const {
return uint64_t(i_size); // +(uint64_t(i_dir_acl)<<32);
}
bool enumblocks(const SuperBlock &super, BLOCKCALLBACK cb) const
{
if (issymlink()) {
// no blocks
}
else if (i_flags&EXT4_EXTENTS_FL) {
return enumextents(super, cb);
}
uint64_t bytes= 0;
for (int i=0 ; i<12 && bytes < i_size ; i++) {
if (i_block[i]) {
if (!cb(&super.getblock(i_block[i]).front()))
return false;
bytes += super.blocksize();
}
}
if (i_block[12])
if (!enumi1block(super, bytes, &super.getblock(i_block[12]).front(), cb))
return false;
if (i_block[13])
if (!enumi2block(super, bytes, &super.getblock(i_block[13]).front(), cb))
return false;
if (i_block[14])
if (!enumi3block(super, bytes, &super.getblock(i_block[14]).front(), cb))
return false;
return true;
}
bool enumi1block(const SuperBlock &super, uint64_t& bytes, const uint8_t *pblock, BLOCKCALLBACK cb) const
{
if (pblock==NULL)
return true;
const uint32_t *p= (const uint32_t *)pblock;
const uint32_t *last= (const uint32_t *)(pblock+super.blocksize());
while (p<last && bytes < i_size)
{
uint32_t blocknr= *p++;
if (blocknr)
if (!cb(&super.getblock(blocknr).front()))
return false;
bytes += super.blocksize();
}
return true;
}
bool enumi2block(const SuperBlock &super, uint64_t& bytes, const uint8_t *pblock, BLOCKCALLBACK cb) const
{
if (pblock==NULL)
return true;
const uint32_t *p= (const uint32_t *)pblock;
const uint32_t *last= (const uint32_t *)(pblock+super.blocksize());
while (p<last && bytes < i_size)
if (!enumi1block(super, bytes, &super.getblock(*p++).front(), cb))
return false;
return true;
}
bool enumi3block(const SuperBlock &super, uint64_t& bytes, const uint8_t *pblock, BLOCKCALLBACK cb) const
{
if (pblock==NULL)
return true;
const uint32_t *p= (const uint32_t *)pblock;
const uint32_t *last= (const uint32_t *)(pblock+super.blocksize());
while (p<last && bytes < i_size)
if (!enumi2block(super, bytes, &super.getblock(*p++).front(), cb))
return false;
return true;
}
bool enumextents(const SuperBlock &super, BLOCKCALLBACK cb) const
{
return e.enumblocks(super, cb);
}
static void rwx(char *str, int bits, bool extra, char xchar)
{
str[0] = (bits&4) ? 'r' : '-';
str[1] = (bits&2) ? 'w' : '-';
if (extra)
str[2] = (bits&1) ? (xchar&~0x20) : (xchar|0x20);
else
str[2] = (bits&1) ? 'x' : '-';
}
std::string modestr() const
{
std::string result(10, ' ');
const char *typechar = "?pc?d?b?-?l?s???";
result[0] = typechar[i_mode>>12];
rwx(&result[1], (i_mode>>6)&7, (i_mode>>11)&1, 's');
rwx(&result[4], (i_mode>>3)&7, (i_mode>>10)&1, 's');
rwx(&result[7], i_mode&7, (i_mode>>9)&1, 't');
return result;
}
};
struct BlockGroupDescriptor {
uint32_t bg_block_bitmap;
uint32_t bg_inode_bitmap;
uint32_t bg_inode_table;
uint16_t bg_free_blocks_count;
uint16_t bg_free_inodes_count;
uint16_t bg_used_dirs_count;
uint16_t bg_pad;
void parse(const uint8_t *first)
{
const uint8_t *p= first;
bg_block_bitmap= get32le(p); p+=4;
bg_inode_bitmap= get32le(p); p+=4;
bg_inode_table= get32le(p); p+=4;
bg_free_blocks_count= get16le(p); p+=2;
bg_free_inodes_count= get16le(p); p+=2;
bg_used_dirs_count= get16le(p); p+=2;
bg_pad= get16le(p); p+=2;
if (p-first!=20)
printf("bgdesc size err: %ld\n", p-first);
}
void dump() const
{
printf("block group B:%d, I:%d, T:%d, free:B=%d, I=%d, used:D=%d\n",
bg_block_bitmap, bg_inode_bitmap, bg_inode_table,
bg_free_blocks_count, bg_free_inodes_count, bg_used_dirs_count);
}
};
struct BlockGroup {
uint64_t itableoffset;
size_t ninodes;
size_t inodesize;
ReadWriter_ptr r;
void parse(uint64_t first, const SuperBlock &super, BlockGroupDescriptor& bgdesc)
{
r= super.r;
itableoffset= first+(bgdesc.bg_inode_table%super.s_blocks_per_group)*super.blocksize();
ninodes= super.s_inodes_per_group;
inodesize= super.s_inode_size;
}
void dump(const SuperBlock &super, uint32_t inodebase) const
{
enuminodes(inodebase, [&](int32_t nr, const Inode& ino) {
printf("INO %d: ", nr);
ino.dump();
if ((ino.i_mode&0xf000)==EXT4_S_IFDIR)
ino.enumblocks(super, [&](const uint8_t *p)->bool {
if (p==NULL) {
printf("invalid blocknr\n");
return true;
}
BlockGroup::dumpdirblock(p, super.blocksize());
return true;
});
});
}
template<typename FUNC>
void enuminodes(uint32_t inodebase, FUNC cb) const
{
for (unsigned i=0 ; i<ninodes ; i++)
{
Inode ino= getinode(i);
if (!ino._empty)
cb(inodebase+i, ino);
}
}
static void dumpdirblock(const uint8_t *first, size_t blocksize)
{
//printf("%s\n", hexdump(first, blocksize).c_str());
const uint8_t *p= first;
while (p<first+blocksize) {
DirectoryEntry ent;
size_t n= ent.parse(p);
if (n==0)
break;
ent.dump();
p+=n;
}
}
Inode getinode(uint32_t nr) const
{
uint8_t buf[128];
r->setpos(itableoffset+inodesize*nr);
r->read(buf, 128);
Inode ino;
ino.parse(buf, 128);
return ino;
}
};
struct Ext2FileSystem {
SuperBlock super;
std::vector<BlockGroupDescriptor> bgdescs;
std::vector<BlockGroup> groups;
uint64_t sb_offset;
uint64_t rootdir_in;
Ext2FileSystem() { }
void parse(ReadWriter_ptr r)
{
r->setpos(sb_offset);
super.parse(r);
if (super.s_magic != 0xef53)
throw "not an ext2 fs";
// groupdescs follow the superblock in the 2nd block
parsegroupdescs(r);
groups.resize(super.ngroups());
uint64_t off= 0;
for (unsigned i=0 ; i<super.ngroups() ; i++) {
groups[i].parse(off, super, bgdescs[i]);
off+=super.bytespergroup();
}
}
void parsegroupdescs(ReadWriter_ptr r)
{
uint64_t bgdescpos;
// When the blocksize == 1024, the superblock fills block 1
// and the block group descriptors start with block 2.
// For larger sizes the superblock fits inside block 0
// and the block group descriptors start with block 1.
if (super.blocksize() == 1024)
bgdescpos = 2048;
else
bgdescpos = super.blocksize();
r->setpos(bgdescpos);
bgdescs.resize(super.ngroups());
ByteVector buf(super.ngroups()*32);
r->read(&buf[0], buf.size());
for (unsigned i=0 ; i<super.ngroups() ; i++)
bgdescs[i].parse(&buf[i*32]);
}
void dump() const
{
super.dump();
uint32_t inodebase= 1;
for (unsigned i=0 ; i<groups.size() ; i++) {
bgdescs[i].dump();
groups[i].dump(super, inodebase);
inodebase+=super.s_inodes_per_group;
}
}
template<typename FUNC>
void enuminodes(FUNC cb) const
{
uint32_t inodebase= 1;
for (unsigned i=0 ; i<groups.size() ; i++) {
groups[i].enuminodes(inodebase, cb);
inodebase+=super.s_inodes_per_group;
}
}
Inode getinode(uint32_t nr) const
{
nr--;
if (nr>=super.s_inodes_count)
nr= 0;
return groups[nr/super.s_inodes_per_group].getinode(nr%super.s_inodes_per_group);
}
};
template<typename FN>
void recursedirs(Ext2FileSystem&fs, uint32_t nr, std::string path, FN f)
{
const Inode &i= fs.getinode(nr);
if ((i.i_mode&0xf000)!=EXT4_S_IFDIR)
return;
i.enumblocks(fs.super, [&](const uint8_t *first)->bool {
if (first==NULL)
return true;
const uint8_t *p= first;
while (p<first+fs.super.blocksize()) {
DirectoryEntry e;
size_t n= e.parse(p);
p+=n;
if (n==0)
break;
// last dir record has type=0 && inde=0,
// size is rest of block
if (e.filetype==EXT4_FT_UNKNOWN)
continue;
if (e.name=="." || e.name=="..")
continue;
f(e, path);
if (e.filetype==EXT4_FT_DIR)
recursedirs(fs, e.inode, path+"/"+e.name, f);
}
return true;
});
}
uint32_t searchpath(Ext2FileSystem&fs, uint32_t nr, std::string path)
{
const Inode &i= fs.getinode(nr);
if ((i.i_mode&0xf000)!=EXT4_S_IFDIR) {
printf("#%d is not a dir\n", nr);
return 0;
}
uint32_t found= 0;
i.enumblocks(fs.super, [&] (const uint8_t *first)->bool {
if (first==NULL)
return true;
const uint8_t *p= first;
while (p<first+fs.super.blocksize()) {
DirectoryEntry e;
size_t n= e.parse(p);
p+=n;
if (n==0)
break;
if (e.filetype==EXT4_FT_UNKNOWN)
continue;
if (path.size()<e.name.size())
continue;
if (path.size()==e.name.size() && path==e.name)
{
found= e.inode;
return false;
}
// path.size() > e.name.size()
if (path[e.name.size()]=='/' && std::equal(e.name.begin(), e.name.end(), path.begin())) {
if (e.filetype==EXT4_FT_DIR) {
found= searchpath(fs, e.inode, path.substr(e.name.size()+1));
return false;
}
else {
printf("Not a directory: %s\n", path.c_str());
return false;
}
}
}
return true;
});
return found;
}
// =============================================================================
struct action {
typedef std::shared_ptr<action> ptr;
static ptr parse(const std::string& arg);
virtual ~action() { }
virtual void perform(Ext2FileSystem &fs)=0;