This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
forked from bestouff/genext2fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genext2fs.c
3966 lines (3618 loc) · 103 KB
/
genext2fs.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
/* vi: set sw=8 ts=8: */
// genext2fs.c
//
// ext2 filesystem generator for embedded systems
// Copyright (C) 2000 Xavier Bestel <[email protected]>
//
// Please direct support requests to https://github.com/bestouff/genext2fs/issues
//
// 'du' portions taken from coreutils/du.c in busybox:
// Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
// Copyright (C) 1999,2000,2001 by John Beppu <[email protected]>
// Copyright (C) 2002 Edward Betts <[email protected]>
//
// 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
// 2 of the License.
//
// Changes:
// 3 Jun 2000 Initial release
// 6 Jun 2000 Bugfix: fs size multiple of 8
// Bugfix: fill blocks with inodes
// 14 Jun 2000 Bugfix: bad chdir() with -d option
// Bugfix: removed size=8n constraint
// Changed -d file to -f file
// Added -e option
// 22 Jun 2000 Changed types for 64bits archs
// 24 Jun 2000 Added endianness swap
// Bugfix: bad dir name lookup
// 03 Aug 2000 Bugfix: ind. blocks endian swap
// 09 Aug 2000 Bugfix: symlinks endian swap
// 01 Sep 2000 Bugfix: getopt returns int, not char [email protected]
// 10 Sep 2000 Bugfix: device nodes endianness [email protected]
// Bugfix: getcwd values for Solaris [email protected]
// Bugfix: ANSI scanf for non-GNU C [email protected]
// 28 Jun 2001 Bugfix: getcwd differs for Solaris/GNU [email protected]
// 8 Mar 2002 Bugfix: endianness swap of x-indirects
// 23 Mar 2002 Bugfix: test for IFCHR or IFBLK was flawed
// 10 Oct 2002 Added comments,makefile targets, [email protected]
// endianess swap assert check.
// Copyright (C) 2002 Ixia communications
// 12 Oct 2002 Added support for triple indirection [email protected]
// Copyright (C) 2002 Ixia communications
// 14 Oct 2002 Added support for groups [email protected]
// Copyright (C) 2002 Ixia communications
// 5 Jan 2003 Bugfixes: reserved inodes should be set [email protected]
// only in the first group; directory names
// need to be null padded at the end; and
// number of blocks per group should be a
// multiple of 8. Updated md5 values.
// 6 Jan 2003 Erik Andersen <[email protected]> added
// mkfs.jffs2 compatible device table support,
// along with -q, -P, -U
/*
* Allow fseeko/off_t to be 64-bit offsets to allow filesystems and
* individual files >2GB.
*/
#define _FILE_OFFSET_BITS 64
#include <config.h>
#include <stdio.h>
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
# include <sys/sysmacros.h>
#endif
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# if HAVE_STDLIB_H
# include <stdlib.h>
# endif
# if HAVE_STDDEF_H
# include <stddef.h>
# endif
#endif
#if HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
# include <memory.h>
# endif
# include <string.h>
#endif
#if HAVE_STRINGS_H
# include <strings.h>
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# if HAVE_STDINT_H
# include <stdint.h>
# endif
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_DIRENT_H
# include <dirent.h>
#else
# define dirent direct
# if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# if HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#if HAVE_LIBGEN_H
# include <libgen.h>
#endif
#include <stdarg.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#if HAVE_FCNTL_H
# include <fcntl.h>
#endif
#if HAVE_GETOPT_H
# include <getopt.h>
#endif
#if HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>
#endif
#include "cache.h"
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define FSLAYER_DIR 0
#define FSLAYER_TABLE 1
#define FSLAYER_TAR 2
struct fslayer {
int type;
char * path;
};
#define TAR_BLOCKSIZE 512
#define TAR_FULLFILENAME (100 + 155 + 1)
struct tar_header {
char filename[100];
char filemode[8];
char uid[8];
char gid[8];
char filesize[12];
char mtime[12];
char checksum[8];
char filetype;
char linkedname[100];
char ustar[8];
char owner[32];
char group[32];
char major[8];
char minor[8];
char prefix[155];
};
struct stats {
unsigned long nblocks;
unsigned long ninodes;
};
// used types
typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
// block size
static uint32 blocksize = 1024;
#define SUPERBLOCK_OFFSET 1024
#define SUPERBLOCK_SIZE 1024
#define BLOCKSIZE blocksize
#define BLOCKS_PER_GROUP 8192
#define INODES_PER_GROUP 8192
/* Percentage of blocks that are reserved.*/
#define RESERVED_BLOCKS 5/100
#define MAX_RESERVED_BLOCKS 25/100
/* The default value for s_creator_os. */
#if defined(__linux__) && defined(EXT2_OS_LINUX)
#define CREATOR_OS EXT2_OS_LINUX
#define CREATOR_OS_NAME "linux"
#else
#if defined(__GNU__) && defined(EXT2_OS_HURD)
#define CREATOR_OS EXT2_OS_HURD
#define CREATOR_OS_NAME "hurd"
#else
#if defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
#define CREATOR_OS EXT2_OS_FREEBSD
#define CREATOR_OS_NAME "freebsd"
#else
#if defined(LITES) && defined(EXT2_OS_LITES)
#define CREATOR_OS EXT2_OS_LITES
#define CREATOR_OS_NAME "lites"
#else
#define CREATOR_OS EXT2_OS_LINUX /* by default */
#define CREATOR_OS_NAME "linux"
#endif /* defined(LITES) && defined(EXT2_OS_LITES) */
#endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
#endif /* defined(__GNU__) && defined(EXT2_OS_HURD) */
#endif /* defined(__linux__) && defined(EXT2_OS_LINUX) */
// inode block size (why is it != BLOCKSIZE ?!?)
/* The field i_blocks in the ext2 inode stores the number of data blocks
but in terms of 512 bytes. That is what INODE_BLOCKSIZE represents.
INOBLK is the number of such blocks in an actual disk block */
#define INODE_BLOCKSIZE 512
#define INOBLK (BLOCKSIZE / INODE_BLOCKSIZE)
// reserved inodes
#define EXT2_BAD_INO 1 // Bad blocks inode
#define EXT2_ROOT_INO 2 // Root inode
#define EXT2_ACL_IDX_INO 3 // ACL inode
#define EXT2_ACL_DATA_INO 4 // ACL inode
#define EXT2_BOOT_LOADER_INO 5 // Boot loader inode
#define EXT2_UNDEL_DIR_INO 6 // Undelete directory inode
#define EXT2_FIRST_INO 11 // First non reserved inode
// magic number for ext2
#define EXT2_MAGIC_NUMBER 0xEF53
// direct/indirect block addresses
#define EXT2_NDIR_BLOCKS 11 // direct blocks
#define EXT2_IND_BLOCK 12 // indirect block
#define EXT2_DIND_BLOCK 13 // double indirect block
#define EXT2_TIND_BLOCK 14 // triple indirect block
#define EXT2_INIT_BLOCK 0xFFFFFFFF // just initialized (not really a block address)
// codes for operating systems
#define EXT2_OS_LINUX 0
#define EXT2_OS_HURD 1
#define EXT2_OS_MASIX 2
#define EXT2_OS_FREEBSD 3
#define EXT2_OS_LITES 4
// end of a block walk
#define WALK_END 0xFFFFFFFE
// file modes
#define FM_IFMT 0170000 // format mask
#define FM_IFSOCK 0140000 // socket
#define FM_IFLNK 0120000 // symbolic link
#define FM_IFREG 0100000 // regular file
#define FM_IFBLK 0060000 // block device
#define FM_IFDIR 0040000 // directory
#define FM_IFCHR 0020000 // character device
#define FM_IFIFO 0010000 // fifo
#define FM_IMASK 0007777 // *all* perms mask for everything below
#define FM_ISUID 0004000 // SUID
#define FM_ISGID 0002000 // SGID
#define FM_ISVTX 0001000 // sticky bit
#define FM_IRWXU 0000700 // entire "user" mask
#define FM_IRUSR 0000400 // read
#define FM_IWUSR 0000200 // write
#define FM_IXUSR 0000100 // execute
#define FM_IRWXG 0000070 // entire "group" mask
#define FM_IRGRP 0000040 // read
#define FM_IWGRP 0000020 // write
#define FM_IXGRP 0000010 // execute
#define FM_IRWXO 0000007 // entire "other" mask
#define FM_IROTH 0000004 // read
#define FM_IWOTH 0000002 // write
#define FM_IXOTH 0000001 // execute
/* Defines for accessing group details */
// Number of groups in the filesystem
#define GRP_NBGROUPS(fs) \
(((fs)->sb->s_blocks_count - fs->sb->s_first_data_block + \
(fs)->sb->s_blocks_per_group - 1) / (fs)->sb->s_blocks_per_group)
// Get group block bitmap (bbm) given the group number
#define GRP_GET_GROUP_BBM(fs,grp,bi) (get_blk((fs),(grp)->bg_block_bitmap,(bi)))
#define GRP_PUT_GROUP_BBM(bi) ( put_blk((bi)) )
// Get group inode bitmap (ibm) given the group number
#define GRP_GET_GROUP_IBM(fs,grp,bi) (get_blk((fs), (grp)->bg_inode_bitmap,(bi)))
#define GRP_PUT_GROUP_IBM(bi) ( put_blk((bi)) )
// Given an inode number find the group it belongs to
#define GRP_GROUP_OF_INODE(fs,nod) ( ((nod)-1) / (fs)->sb->s_inodes_per_group)
//Given an inode number get the inode bitmap that covers it
#define GRP_GET_INODE_BITMAP(fs,nod,bi,gi) \
( GRP_GET_GROUP_IBM((fs),get_gd(fs,GRP_GROUP_OF_INODE((fs),(nod)),gi),bi) )
#define GRP_PUT_INODE_BITMAP(bi,gi) \
( GRP_PUT_GROUP_IBM((bi)),put_gd((gi)) )
//Given an inode number find its offset within the inode bitmap that covers it
#define GRP_IBM_OFFSET(fs,nod) \
( (nod) - GRP_GROUP_OF_INODE((fs),(nod))*(fs)->sb->s_inodes_per_group )
// Given a block number find the group it belongs to
#define GRP_GROUP_OF_BLOCK(fs,blk) ( ((blk)-1) / (fs)->sb->s_blocks_per_group)
//Given a block number get/put the block bitmap that covers it
#define GRP_GET_BLOCK_BITMAP(fs,blk,bi,gi) \
( GRP_GET_GROUP_BBM((fs),get_gd(fs,GRP_GROUP_OF_BLOCK((fs),(blk)),(gi)),(bi)) )
#define GRP_PUT_BLOCK_BITMAP(bi,gi) \
( GRP_PUT_GROUP_BBM((bi)),put_gd((gi)) )
//Given a block number find its offset within the block bitmap that covers it
#define GRP_BBM_OFFSET(fs,blk) \
( (blk) - GRP_GROUP_OF_BLOCK((fs),(blk))*(fs)->sb->s_blocks_per_group )
// the GNU C library has a wonderful scanf("%as", string) which will
// allocate the string with the right size, good to avoid buffer
// overruns. the following macros use it if available or use a
// hacky workaround
// moreover it will define a snprintf() like a sprintf(), i.e.
// without the buffer overrun checking, to work around bugs in
// older solaris. Note that this is still not very portable, in that
// the return value cannot be trusted.
#if 0 // SCANF_CAN_MALLOC
// C99 define "a" for floating point, so you can have runtime surprise
// according the library versions
# define SCANF_PREFIX "a"
# define SCANF_STRING(s) (&s)
#else
# define SCANF_PREFIX "511"
# define SCANF_STRING(s) (s = malloc(512))
#endif /* SCANF_CAN_MALLOC */
#if PREFER_PORTABLE_SNPRINTF
static inline int
portable_snprintf(char *str, size_t n, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsprintf(str, fmt, ap);
va_end(ap);
return ret;
}
# define SNPRINTF portable_snprintf
#else
# define SNPRINTF snprintf
#endif /* PREFER_PORTABLE_SNPRINTF */
#if !HAVE_GETLINE
// getline() replacement for Darwin and Solaris etc.
// This code uses backward seeks (unless rchunk is set to 1) which can't work
// on pipes etc. However, add2fs_from_file() only calls getline() for
// regular files, so a larger rchunk and backward seeks are okay.
ssize_t
getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
{
char *p; // reads stored here
size_t const rchunk = 512; // number of bytes to read
size_t const mchunk = 512; // number of extra bytes to malloc
size_t m = rchunk + 1; // initial buffer size
if (*lineptr) {
if (*n < m) {
*lineptr = (char*)realloc(*lineptr, m);
if (!*lineptr) return -1;
*n = m;
}
} else {
*lineptr = (char*)malloc(m);
if (!*lineptr) return -1;
*n = m;
}
m = 0; // record length including seperator
do {
size_t i; // number of bytes read etc
size_t j = 0; // number of bytes searched
p = *lineptr + m;
i = fread(p, 1, rchunk, stream);
if (i < rchunk && ferror(stream))
return -1;
while (j < i) {
++j;
if (*p++ == (char)delim) {
*p = '\0';
if (j != i) {
if (fseek(stream, j - i, SEEK_CUR))
return -1;
if (feof(stream))
clearerr(stream);
}
m += j;
return m;
}
}
m += j;
if (feof(stream)) {
if (m) return m;
if (!i) return -1;
}
// allocate space for next read plus possible null terminator
i = ((m + (rchunk + 1 > mchunk ? rchunk + 1 : mchunk) +
mchunk - 1) / mchunk) * mchunk;
if (i != *n) {
*lineptr = (char*)realloc(*lineptr, i);
if (!*lineptr) return -1;
*n = i;
}
} while (1);
}
#define getline(a,b,c) getdelim(a,b,'\n',c)
#endif /* HAVE_GETLINE */
// Convert a numerical string to a float, and multiply the result by an
// IEC or SI multiplier if provided; supported multipliers are Ki, Mi, Gi, k, M
// and G.
float
SI_atof(const char *nptr)
{
float f = 0;
float m = 1;
char *suffixptr;
#if HAVE_STRTOF
f = strtof(nptr, &suffixptr);
#else
f = (float)strtod(nptr, &suffixptr);
#endif /* HAVE_STRTOF */
if (*suffixptr) {
if (!strcmp(suffixptr, "Ki"))
m = 1 << 10;
else if (!strcmp(suffixptr, "Mi"))
m = 1 << 20;
else if (!strcmp(suffixptr, "Gi"))
m = 1 << 30;
else if (!strcmp(suffixptr, "k"))
m = 1000;
else if (!strcmp(suffixptr, "M"))
m = 1000 * 1000;
else if (!strcmp(suffixptr, "G"))
m = 1000 * 1000 * 1000;
}
return f * m;
}
// endianness swap
static inline uint16
swab16(uint16 val)
{
return (val >> 8) | (val << 8);
}
static inline uint32
swab32(uint32 val)
{
return ((val>>24) | ((val>>8)&0xFF00) |
((val<<8)&0xFF0000) | (val<<24));
}
static inline int
is_blk_empty(uint8 *b)
{
uint32 i;
uint32 *v = (uint32 *) b;
for(i = 0; i < BLOCKSIZE / 4; i++)
if (*v++)
return 0;
return 1;
}
// on-disk structures
// this trick makes me declare things only once
// (once for the structures, once for the endianness swap)
#define superblock_decl \
udecl32(s_inodes_count) /* Count of inodes in the filesystem */ \
udecl32(s_blocks_count) /* Count of blocks in the filesystem */ \
udecl32(s_r_blocks_count) /* Count of the number of reserved blocks */ \
udecl32(s_free_blocks_count) /* Count of the number of free blocks */ \
udecl32(s_free_inodes_count) /* Count of the number of free inodes */ \
udecl32(s_first_data_block) /* The first block which contains data */ \
udecl32(s_log_block_size) /* Indicator of the block size */ \
decl32(s_log_frag_size) /* Indicator of the size of the fragments */ \
udecl32(s_blocks_per_group) /* Count of the number of blocks in each block group */ \
udecl32(s_frags_per_group) /* Count of the number of fragments in each block group */ \
udecl32(s_inodes_per_group) /* Count of the number of inodes in each block group */ \
udecl32(s_mtime) /* The time that the filesystem was last mounted */ \
udecl32(s_wtime) /* The time that the filesystem was last written to */ \
udecl16(s_mnt_count) /* The number of times the file system has been mounted */ \
decl16(s_max_mnt_count) /* The number of times the file system can be mounted */ \
udecl16(s_magic) /* Magic number indicating ex2fs */ \
udecl16(s_state) /* Flags indicating the current state of the filesystem */ \
udecl16(s_errors) /* Flags indicating the procedures for error reporting */ \
udecl16(s_minor_rev_level) /* The minor revision level of the filesystem */ \
udecl32(s_lastcheck) /* The time that the filesystem was last checked */ \
udecl32(s_checkinterval) /* The maximum time permissable between checks */ \
udecl32(s_creator_os) /* Indicator of which OS created the filesystem */ \
udecl32(s_rev_level) /* The revision level of the filesystem */ \
udecl16(s_def_resuid) /* The default uid for reserved blocks */ \
udecl16(s_def_resgid) /* The default gid for reserved blocks */ \
/* rev 1 version fields start here */ \
udecl32(s_first_ino) /* First non-reserved inode */ \
udecl16(s_inode_size) /* size of inode structure */ \
udecl16(s_block_group_nr) /* block group # of this superblock */ \
udecl32(s_feature_compat) /* compatible feature set */ \
udecl32(s_feature_incompat) /* incompatible feature set */ \
udecl32(s_feature_ro_compat) /* readonly-compatible feature set */ \
utdecl8(s_uuid,16) /* 128-bit uuid for volume */ \
utdecl8(s_volume_name,16) /* volume name */ \
utdecl8(s_last_mounted,64) /* directory where last mounted */ \
udecl32(s_algorithm_usage_bitmap) /* For compression */
#define EXT2_GOOD_OLD_FIRST_INO 11
#define EXT2_GOOD_OLD_INODE_SIZE 128
#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
#define groupdescriptor_decl \
udecl32(bg_block_bitmap) /* Block number of the block bitmap */ \
udecl32(bg_inode_bitmap) /* Block number of the inode bitmap */ \
udecl32(bg_inode_table) /* Block number of the inode table */ \
udecl16(bg_free_blocks_count) /* Free blocks in the group */ \
udecl16(bg_free_inodes_count) /* Free inodes in the group */ \
udecl16(bg_used_dirs_count) /* Number of directories in the group */ \
udecl16(bg_pad)
#define inode_decl \
udecl16(i_mode) /* Entry type and file mode */ \
udecl16(i_uid) /* User id */ \
udecl32(i_size) /* File/dir size in bytes */ \
udecl32(i_atime) /* Last access time */ \
udecl32(i_ctime) /* Creation time */ \
udecl32(i_mtime) /* Last modification time */ \
udecl32(i_dtime) /* Deletion time */ \
udecl16(i_gid) /* Group id */ \
udecl16(i_links_count) /* Number of (hard) links to this inode */ \
udecl32(i_blocks) /* Number of blocks used (1 block = 512 bytes) */ \
udecl32(i_flags) /* ??? */ \
udecl32(i_reserved1) \
utdecl32(i_block,15) /* Blocks table */ \
udecl32(i_version) /* ??? */ \
udecl32(i_file_acl) /* File access control list */ \
udecl32(i_dir_acl) /* Directory access control list */ \
udecl32(i_faddr) /* Fragment address */ \
udecl8(i_frag) /* Fragments count*/ \
udecl8(i_fsize) /* Fragment size */ \
udecl16(i_pad1)
#define directory_decl \
udecl32(d_inode) /* Inode entry */ \
udecl16(d_rec_len) /* Total size on record */ \
udecl16(d_name_len) /* Size of entry name */
#define decl8(x) int8 x;
#define udecl8(x) uint8 x;
#define utdecl8(x,n) uint8 x[n];
#define decl16(x) int16 x;
#define udecl16(x) uint16 x;
#define decl32(x) int32 x;
#define udecl32(x) uint32 x;
#define utdecl32(x,n) uint32 x[n];
typedef struct
{
superblock_decl
uint32 s_reserved[205]; // Reserved
} superblock;
typedef struct
{
groupdescriptor_decl
uint32 bg_reserved[3];
} groupdescriptor;
typedef struct
{
inode_decl
uint32 i_reserved2[2];
} inode;
typedef struct
{
directory_decl
} directory;
typedef uint8 *block;
/* blockwalker fields:
The blockwalker is used to access all the blocks of a file (including
the indirection blocks) through repeated calls to walk_bw.
bpdir -> index into the inode->i_block[]. Indicates level of indirection.
bnum -> total number of blocks so far accessed. including indirection
blocks.
bpind,bpdind,bptind -> index into indirection blocks.
bpind, bpdind, bptind do *NOT* index into single, double and triple
indirect blocks resp. as you might expect from their names. Instead
they are in order the 1st, 2nd & 3rd index to be used
As an example..
To access data block number 70000:
bpdir: 15 (we are doing triple indirection)
bpind: 0 ( index into the triple indirection block)
bpdind: 16 ( index into the double indirection block)
bptind: 99 ( index into the single indirection block)
70000 = 12 + 256 + 256*256 + 16*256 + 100 (indexing starts from zero)
So,for double indirection bpind will index into the double indirection
block and bpdind into the single indirection block. For single indirection
only bpind will be used.
*/
typedef struct
{
uint32 bnum;
uint32 bpdir;
uint32 bpind;
uint32 bpdind;
uint32 bptind;
} blockwalker;
#define HDLINK_CNT 16
struct hdlink_s
{
uint32 src_inode;
uint32 dst_nod;
};
struct hdlinks_s
{
int32 count;
struct hdlink_s *hdl;
};
/* Filesystem structure that support groups */
typedef struct
{
FILE *f;
superblock *sb;
int swapit;
int32 hdlink_cnt;
struct hdlinks_s hdlinks;
int holes;
listcache blks;
listcache gds;
listcache inodes;
listcache blkmaps;
} filesystem;
// now the endianness swap
#undef decl8
#undef udecl8
#undef utdecl8
#undef decl16
#undef udecl16
#undef decl32
#undef udecl32
#undef utdecl32
#define decl8(x)
#define udecl8(x)
#define utdecl8(x,n)
#define decl16(x) this->x = swab16(this->x);
#define udecl16(x) this->x = swab16(this->x);
#define decl32(x) this->x = swab32(this->x);
#define udecl32(x) this->x = swab32(this->x);
#define utdecl32(x,n) { int i; for(i=0; i<n; i++) this->x[i] = swab32(this->x[i]); }
static void
swap_sb(superblock *sb)
{
#define this sb
superblock_decl
#undef this
}
static void
swap_gd(groupdescriptor *gd)
{
#define this gd
groupdescriptor_decl
#undef this
}
static void
swap_nod(inode *nod)
{
#define this nod
inode_decl
#undef this
}
static void
swap_dir(directory *dir)
{
#define this dir
directory_decl
#undef this
}
static void
swap_block(block b)
{
uint32 i;
uint32 *blk = (uint32*)b;
for(i = 0; i < BLOCKSIZE/4; i++)
blk[i] = swab32(blk[i]);
}
#undef decl8
#undef udecl8
#undef utdecl8
#undef decl16
#undef udecl16
#undef decl32
#undef udecl32
#undef utdecl32
static char * app_name;
static const char *const memory_exhausted = "memory exhausted";
// error (un)handling
static void
verror_msg(const char *s, va_list p)
{
fflush(stdout);
fprintf(stderr, "%s: ", app_name);
vfprintf(stderr, s, p);
}
static void
error_msg(const char *s, ...)
{
va_list p;
va_start(p, s);
verror_msg(s, p);
va_end(p);
putc('\n', stderr);
}
static void
error_msg_and_die(const char *s, ...)
{
va_list p;
va_start(p, s);
verror_msg(s, p);
va_end(p);
putc('\n', stderr);
exit(EXIT_FAILURE);
}
static void
vperror_msg(const char *s, va_list p)
{
int err = errno;
if (s == 0)
s = "";
verror_msg(s, p);
if (*s)
s = ": ";
fprintf(stderr, "%s%s\n", s, strerror(err));
}
static void
perror_msg_and_die(const char *s, ...)
{
va_list p;
va_start(p, s);
vperror_msg(s, p);
va_end(p);
exit(EXIT_FAILURE);
}
static FILE *
xfopen(const char *path, const char *mode)
{
FILE *fp;
if ((fp = fopen(path, mode)) == NULL)
perror_msg_and_die("%s", path);
return fp;
}
static char *
xstrdup(const char *s)
{
char *t;
if (s == NULL)
return NULL;
t = strdup(s);
if (t == NULL)
error_msg_and_die(memory_exhausted);
return t;
}
int
is_hardlink(filesystem *fs, ino_t inode)
{
int i;
for(i = 0; i < fs->hdlinks.count; i++) {
if(fs->hdlinks.hdl[i].src_inode == inode)
return i;
}
return -1;
}
// printf helper macro
#define plural(a) (a), ((a) == 1) ? "" : "s"
// temporary working block
static inline uint8 *
get_workblk(void)
{
unsigned char* b=calloc(1,BLOCKSIZE);
if (!b)
error_msg_and_die("get_workblk() failed, out of memory");
return b;
}
static inline void
free_workblk(block b)
{
free(b);
}
/* Rounds qty upto a multiple of siz. siz should be a power of 2 */
static inline uint32
rndup(uint32 qty, uint32 siz)
{
return (qty + (siz - 1)) & ~(siz - 1);
}
// check if something is allocated in the bitmap
static inline uint32
allocated(block b, uint32 item)
{
return b[(item-1) / 8] & (1 << ((item-1) % 8));
}
// Used by get_blk/put_blk to hold information about a block owned
// by the user.
typedef struct
{
cache_link link;
filesystem *fs;
uint32 blk;
uint8 *b;
uint32 usecount;
} blk_info;
#define MAX_FREE_CACHE_BLOCKS 100
static uint32
blk_elem_val(cache_link *elem)
{
blk_info *bi = container_of(elem, blk_info, link);
return bi->blk;
}
static void
blk_freed(cache_link *elem)
{
blk_info *bi = container_of(elem, blk_info, link);
if (fseeko(bi->fs->f, ((off_t) bi->blk) * BLOCKSIZE, SEEK_SET))
perror_msg_and_die("fseek");
if (fwrite(bi->b, BLOCKSIZE, 1, bi->fs->f) != 1)
perror_msg_and_die("get_blk: write");
free(bi->b);
free(bi);
}
// Return a given block from a filesystem. Make sure to call
// put_blk when you are done with it.
static inline uint8 *
get_blk(filesystem *fs, uint32 blk, blk_info **rbi)
{
cache_link *curr;
blk_info *bi;
if (blk >= fs->sb->s_blocks_count)
error_msg_and_die("Internal error, block out of range");
curr = cache_find(&fs->blks, blk);
if (curr) {
bi = container_of(curr, blk_info, link);
bi->usecount++;
goto out;
}
bi = malloc(sizeof(*bi));
if (!bi)
error_msg_and_die("get_blk: out of memory");
bi->fs = fs;
bi->blk = blk;
bi->usecount = 1;
bi->b = malloc(BLOCKSIZE);
if (!bi->b)
error_msg_and_die("get_blk: out of memory");
cache_add(&fs->blks, &bi->link);
if (fseeko(fs->f, ((off_t) blk) * BLOCKSIZE, SEEK_SET))
perror_msg_and_die("fseek");
if (fread(bi->b, BLOCKSIZE, 1, fs->f) != 1) {
if (ferror(fs->f))
perror_msg_and_die("fread");
memset(bi->b, 0, BLOCKSIZE);
}
out:
*rbi = bi;
return bi->b;
}
static inline void
put_blk(blk_info *bi)
{
if (bi->usecount == 0)
error_msg_and_die("Internal error: put_blk usecount zero");
bi->usecount--;
if (bi->usecount == 0)
/* Free happens in the cache code */
cache_item_set_unused(&bi->fs->blks, &bi->link);
}
typedef struct
{
cache_link link;
filesystem *fs;
int gds;
blk_info *bi;
groupdescriptor *gd;
uint32 usecount;
} gd_info;
#define MAX_FREE_CACHE_GDS 100
static uint32
gd_elem_val(cache_link *elem)