-
Notifications
You must be signed in to change notification settings - Fork 0
/
genext2fs.c
1819 lines (1677 loc) · 47.2 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
// genext2fs.c
//
// ext2 filesystem generator for embedded systems
// Copyright (C) 2000 Xavier Bestel <[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; either version
// 2 of the License, or (at your option) any later version.
//
// 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
// 19 Jun 2001 Erik Andersen <[email protected]> added
// the -n (nosquash) option.
// 20 Aug 2001 Erik Andersen <[email protected]> added
// device table support
// `genext2fs' is a mean to generate an ext2 filesystem
// as a normal (non-root) user. It doesn't require you to mount
// the image file to copy files on it. It doesn't even require
// you to be the superuser to make device nodes.
//
// Warning ! `genext2fs' has been designed for embedded
// systems. As such, it will generate a filesystem for single-user
// usage: all files/directories/etc... will belong to UID/GID 0
//
// Example usage:
//
// # genext2fs -b 1440 -d srcdir /dev/fd0
//
// All files in the srcdir directory will be written to /dev/fd0 as
// a new ext2 filesystem image. You can then mount the floppy as
// usual.
//
// # genext2fs -b 1024 -d builddir -f devices.txt flashdisk.img
//
// This one would build a filesystem from all the files in builddir,
// then would read a devices list and make apropriate nodes. The
// format for the device list is:
//
// drwx /dev
// crw- 10,190 /dev/lcd
// brw- 1,0 /dev/ram0
//
// This device list builds the /dev directory, a character device
// node /dev/lcd (major 10, minor 190) and a block device node
// /dev/ram0 (major 1, minor 0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#define __USE_GNU
#include <ctype.h>
#include <fcntl.h>
// block size
#define BLOCKSIZE 1024
#define BLOCKS_PER_GROUP 8192
#define BYTES_PER_INODE (8*BLOCKSIZE)
#define RESERVED_INODES 5/100
// inode block size (why is it != BLOCKSIZE ?!?)
#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)
// end of a block walk
#define WALK_END 0xFFFFFFFE
// file modes
#define FM_IFMT 0xF000 // format mask
#define FM_IFLNK 0xA000 // socket
#define FM_IFSOCK 0xC000 // symbolic link
#define FM_IFREG 0x8000 // regular file
#define FM_IFBLK 0x6000 // block device
#define FM_IFDIR 0x4000 // directory
#define FM_IFCHR 0x2000 // character device
#define FM_IFIFO 0x1000 // fifo
#define FM_ISUID 0x0800 // SUID
#define FM_ISGID 0x0400 // SGID
#define FM_ISVTX 0x0200 // sticky bit
#define FM_IRWXU 0x01C0 // user mask
#define FM_IRUSR 0x0100 // read
#define FM_IWUSR 0x0080 // write
#define FM_IXUSR 0x0040 // execute
#define FM_IRWXG 0x0038 // group mask
#define FM_IRGRP 0x0020 // read
#define FM_IWGRP 0x0010 // write
#define FM_IXGRP 0x0008 // execute
#define FM_IRWXO 0x0007 // other mask
#define FM_IROTH 0x0004 // read
#define FM_IWOTH 0x0002 // write
#define FM_IXOTH 0x0001 // execute
// options
#define OP_HOLES 0x01 // make files with holes
// 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;
// endianness swap
inline uint16 swab16(uint16 val)
{
return (val >> 8) | (val << 8);
}
inline uint32 swab32(uint32 val)
{
return ((val>>24) | ((val>>8)&0xFF00) |
((val<<8)&0xFF0000) | (val<<24));
}
// 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 */
#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 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[235]; // Reserved
} superblock;
typedef struct
{
groupdescriptor_decl
uint32 bg_reserved[3];
uint32 bg_pad_to_bk[(BLOCKSIZE-32)/sizeof(uint32)];
} groupdescriptor;
typedef struct
{
inode_decl
uint32 i_reserved2[2];
} inode;
typedef struct
{
directory_decl
char d_name[0];
} directory;
typedef uint8 block[BLOCKSIZE];
typedef struct
{
uint32 bnum;
uint32 bpdir;
uint32 bpind;
uint32 bpdind;
uint32 bptind;
} blockwalker;
#if BLOCKSIZE == 1024
typedef struct
{
block zero; // The famous block 0
superblock sb; // The superblock
groupdescriptor gd; // The group desciptor
block bbm; // The block bitmap
block ibm; // The inode bitmap
inode itab[0]; // The inode table
} filesystem;
#else
#error UNHANDLED BLOCKSIZE
#endif
// now the endianness swap
#undef decl8
#undef udecl8
#undef decl16
#undef udecl16
#undef decl32
#undef udecl32
#undef utdecl32
#define decl8(x)
#define udecl8(x)
#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]); }
void swap_sb(superblock *sb)
{
#define this sb
superblock_decl
#undef this
}
void swap_gd(groupdescriptor *gd)
{
#define this gd
groupdescriptor_decl
#undef this
}
void swap_nod(inode *nod)
{
#define this nod
inode_decl
#undef this
}
void swap_dir(directory *dir)
{
#define this dir
directory_decl
#undef this
}
void swap_block(block b)
{
int i;
uint32 *blk = (uint32*)b;
for(i = 0; i < BLOCKSIZE/4; i++)
blk[i] = swab32(blk[i]);
}
#undef decl8
#undef udecl8
#undef decl16
#undef udecl16
#undef decl32
#undef udecl32
#undef utdecl32
char * argv0;
int nosquash = 0;
extern ssize_t getline(char **lineptr, size_t *n, FILE *stream);
// error (un)handling
inline void errexit(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s: ", argv0);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
inline void pexit(const char * fname)
{
fprintf(stderr, "%s: ", argv0);
perror(fname);
exit(1);
}
// printf helper macro
#define plural(a) (a), ((a) > 1) ? "s" : ""
// temporary working block
inline uint8 * get_workblk(void)
{
static block b;
return b;
}
inline void free_workblk(block b)
{
}
// rounds a quantity up to a blocksize
uint32 rndup(uint32 qty, uint32 siz)
{
return (qty + (siz - 1)) & ~(siz - 1);
}
// check if something is allocated in the bitmap
inline uint32 allocated(block b, uint32 item)
{
return b[(item-1) / 8] & (1 << ((item-1) % 8));
}
// return a given block from a filesystem
inline uint8 * get_blk(filesystem *fs, uint32 blk)
{
return (uint8*)fs + blk*BLOCKSIZE;
}
// return a given inode from a filesystem
inline inode * get_nod(filesystem *fs, uint32 nod)
{
return &fs->itab[nod-1];
}
// allocate a given block/inode in the bitmap
// allocate first free if item == 0
uint32 allocate(block b, uint32 item)
{
if(!item)
{
int i;
uint8 bits;
for(i = 0; i < BLOCKSIZE; i++)
if((bits = b[i]) != (uint8)-1)
{
int j;
for(j = 0; j < 8; j++)
if(!(bits & (1 << j)))
break;
item = i * 8 + j + 1;
break;
}
if(i == BLOCKSIZE)
return 0;
}
b[(item-1) / 8] |= (1 << ((item-1) % 8));
return item;
}
// deallocate a given block/inode
void deallocate(block b, uint32 item)
{
b[(item-1) / 8] &= ~(1 << ((item-1) % 8));
}
// allocate a block
uint32 alloc_blk(filesystem *fs)
{
uint32 bk;
if(!(bk = allocate(fs->bbm, 0)))
errexit("couldn't allocate a block (no free space)");
if(!(fs->gd.bg_free_blocks_count--))
errexit("group descr. free blocks count == 0 (corrupted fs?)");
if(!(fs->sb.s_free_blocks_count--))
errexit("superblock free blocks count == 0 (corrupted fs?)");
return bk;
}
// allocate an inode
uint32 alloc_nod(filesystem *fs)
{
uint32 nod;
if(!(nod = allocate(fs->ibm, 0)))
errexit("couldn't allocate an inode (no free inode)");
if(!(fs->gd.bg_free_inodes_count--))
errexit("group descr. free blocks count == 0 (corrupted fs?)");
if(!(fs->sb.s_free_inodes_count--))
errexit("superblock free blocks count == 0 (corrupted fs?)");
return nod;
}
// print a bitmap allocation
void print_bm(block b, uint32 max)
{
uint32 i;
printf("----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0\n");
for(i=1; i <= max; i++)
{
putchar(allocated(b, i) ? '*' : '.');
if(!(i % 100))
printf("\n");
}
if((i-1) % 100)
printf("\n");
}
// initalize a blockwalker (iterator for blocks list)
void init_bw(filesystem *fs, uint32 nod, blockwalker *bw)
{
bw->bnum = 0;
bw->bpdir = EXT2_INIT_BLOCK;
}
// return next block of inode (WALK_END for end)
// if create>0, append a newly allocated block at the end
// if hole!=0, create a hole in the file
uint32 walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, uint32 *create, uint32 hole)
{
uint32 *bkref = 0;
uint32 *b;
uint32 extend = 0;
if(bw->bnum >= get_nod(fs, nod)->i_blocks / INOBLK)
{
if(create && (*create)--)
extend = 1;
else
return WALK_END;
}
// first direct block
if(bw->bpdir == EXT2_INIT_BLOCK)
{
bkref = &get_nod(fs, nod)->i_block[bw->bpdir = 0];
if(extend) // allocate first block
*bkref = hole ? 0 : alloc_blk(fs);
}
// direct block
else if(bw->bpdir < EXT2_NDIR_BLOCKS)
{
bkref = &get_nod(fs, nod)->i_block[++bw->bpdir];
if(extend) // allocate block
*bkref = hole ? 0 : alloc_blk(fs);
}
// first block in indirect block
else if(bw->bpdir == EXT2_NDIR_BLOCKS)
{
bw->bnum++;
bw->bpdir = EXT2_IND_BLOCK;
bw->bpind = 0;
if(extend) // allocate indirect block
get_nod(fs, nod)->i_block[bw->bpdir] = alloc_blk(fs);
b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
bkref = &b[bw->bpind];
if(extend) // allocate first block
*bkref = hole ? 0 : alloc_blk(fs);
}
// block in indirect block
else if((bw->bpdir == EXT2_IND_BLOCK) && (bw->bpind < BLOCKSIZE/4 - 1))
{
bw->bpind++;
b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
bkref = &b[bw->bpind];
if(extend) // allocate block
*bkref = hole ? 0 : alloc_blk(fs);
}
// first block in first indirect block in first double indirect block
else if(bw->bpdir == EXT2_IND_BLOCK)
{
bw->bnum += 2;
bw->bpdir = EXT2_DIND_BLOCK;
bw->bpind = 0;
bw->bpdind = 0;
if(extend) // allocate double indirect block
get_nod(fs, nod)->i_block[bw->bpdir] = alloc_blk(fs);
b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
if(extend) // allocate first indirect block
b[bw->bpind] = alloc_blk(fs);
b = (uint32*)get_blk(fs, b[bw->bpind]);
bkref = &b[bw->bpdind];
if(extend) // allocate first block
*bkref = hole ? 0 : alloc_blk(fs);
}
// block in indirect block in double indirect block
else if((bw->bpdir == EXT2_DIND_BLOCK) && (bw->bpdind < BLOCKSIZE/4 - 1))
{
bw->bpdind++;
b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
b = (uint32*)get_blk(fs, b[bw->bpind]);
bkref = &b[bw->bpdind];
if(extend) // allocate block
*bkref = hole ? 0 : alloc_blk(fs);
}
// first block in indirect block in double indirect block
else if((bw->bpdir == EXT2_DIND_BLOCK) && (bw->bpind < BLOCKSIZE/4 - 1))
{
bw->bnum++;
bw->bpdind = 0;
bw->bpind++;
b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
if(extend) // allocate indirect block
b[bw->bpind] = alloc_blk(fs);
b = (uint32*)get_blk(fs, b[bw->bpind]);
bkref = &b[bw->bpdind];
if(extend) // allocate first block
*bkref = hole ? 0 : alloc_blk(fs);
}
// I don't do triple indirect - it's such a small filesystem ...
else
errexit("file too big ! blocks list for inode %d extends past double indirect blocks!", nod);
if(*bkref)
{
bw->bnum++;
if(!allocated(fs->bbm, *bkref))
errexit("[block %d of inode %d is unallocated !]", *bkref, nod);
}
if(extend)
get_nod(fs, nod)->i_blocks = bw->bnum * INOBLK;
return *bkref;
}
// add blocks to an inode (file/dir/etc...)
void extend_blk(filesystem *fs, uint32 nod, block b, int amount)
{
int create = amount;
blockwalker bw, lbw;
uint32 bk;
init_bw(fs, nod, &bw);
lbw = bw;
while((bk = walk_bw(fs, nod, &bw, 0, 0)) != WALK_END)
lbw = bw;
bw = lbw;
while(create)
{
int i, copyb = 0;
if(!(fs->sb.s_reserved[200] & OP_HOLES))
copyb = 1;
else
for(i = 0; i < BLOCKSIZE / 4; i++)
if(((int32*)(b + BLOCKSIZE * (amount - create)))[i])
{
copyb = 1;
break;
}
if((bk = walk_bw(fs, nod, &bw, &create, !copyb)) == WALK_END)
break;
if(copyb)
memcpy(get_blk(fs, bk), b + BLOCKSIZE * (amount - create - 1), BLOCKSIZE);
}
}
// link an entry (inode #) to a directory
void add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name, uint32 mode, uid_t uid, gid_t gid, time_t ctime)
{
blockwalker bw;
uint32 bk;
uint8 *b;
directory *d;
int reclen, nlen;
inode *node;
inode *pnode;
if (nosquash == 0) {
/* Ok, squash everything */
uid = 0;
gid = 0;
if(!S_ISDIR(mode))
mode &= ~(S_IWGRP | S_IWOTH);
mode &= ~(S_ISUID | S_ISGID);
}
pnode = get_nod(fs, dnod);
if(!S_ISDIR(pnode->i_mode))
errexit("can't add '%s' to a non-directory", name);
if(!*name)
errexit("bad name '%s' (not meaningful)", name);
if(strchr(name, '/'))
errexit("bad name '%s' (contains a slash)", name);
nlen = strlen(name);
reclen = sizeof(directory) + rndup(nlen, 4);
if(reclen > BLOCKSIZE)
errexit("bad name '%s' (too long)", name);
init_bw(fs, dnod, &bw);
while((bk = walk_bw(fs, dnod, &bw, 0, 0)) != WALK_END) // for all blocks in dir
{
b = get_blk(fs, bk);
// for all dir entries in block
for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
{
// if empty dir entry, large enough, use it
if((!d->d_inode) && (d->d_rec_len >= reclen))
{
d->d_inode = nod;
node = get_nod(fs, nod);
node->i_links_count++;
d->d_name_len = nlen;
strncpy(d->d_name, name, nlen);
node->i_mode = mode;
node->i_uid = uid;
node->i_gid = gid;
node->i_atime = ctime;
node->i_ctime = ctime;
node->i_mtime = ctime;
return;
}
// if entry with enough room (last one?), shrink it & use it
if(d->d_rec_len >= (sizeof(directory) + rndup(d->d_name_len, 4) + reclen))
{
reclen = d->d_rec_len;
d->d_rec_len = sizeof(directory) + rndup(d->d_name_len, 4);
reclen -= d->d_rec_len;
d = (directory*) (((int8*)d) + d->d_rec_len);
d->d_rec_len = reclen;
d->d_inode = nod;
node = get_nod(fs, nod);
node->i_links_count++;
d->d_name_len = nlen;
strncpy(d->d_name, name, nlen);
node->i_mode = mode;
node->i_uid = uid;
node->i_gid = gid;
node->i_atime = ctime;
node->i_ctime = ctime;
node->i_mtime = ctime;
return;
}
}
}
// we found no free entry in the directory, so we add a block
b = get_workblk();
d = (directory*)b;
d->d_inode = nod;
node = get_nod(fs, nod);
node->i_links_count++;
d->d_rec_len = BLOCKSIZE;
d->d_name_len = nlen;
strncpy(d->d_name, name, nlen);
node->i_mode = mode;
node->i_uid = uid;
node->i_gid = gid;
node->i_atime = ctime;
node->i_ctime = ctime;
node->i_mtime = ctime;
extend_blk(fs, dnod, b, 1);
get_nod(fs, dnod)->i_size += BLOCKSIZE;
free_workblk(b);
}
// find an entry in a directory
uint32 find_dir(filesystem *fs, uint32 nod, const char * name)
{
blockwalker bw;
uint32 bk;
int nlen = strlen(name);
init_bw(fs, nod, &bw);
while((bk = walk_bw(fs, nod, &bw, 0, 0)) != WALK_END)
{
directory *d;
uint8 *b;
b = get_blk(fs, bk);
for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
if(d->d_inode && (nlen == d->d_name_len) && !strncmp(d->d_name, name, nlen))
return d->d_inode;
}
return 0;
}
// find the inode of a full path
uint32 find_path(filesystem *fs, uint32 nod, const char * name)
{
char *p, *n, *n2 = strdup(name);
n = n2;
while(*n == '/')
{
nod = EXT2_ROOT_INO;
n++;
}
while(*n)
{
if((p = strchr(n, '/')))
(*p) = 0;
if(!(nod = find_dir(fs, nod, n)))
break;
if(p)
n = p + 1;
else
break;
}
free(n2);
return nod;
}
// make a full-fledged directory (i.e. with "." & "..")
uint32 mkdir_fs(filesystem *fs, uint32 parent_nod, const char *name, uint32 mode, uid_t uid, gid_t gid, time_t ctime)
{
uint32 nod;
if((nod = find_dir(fs, parent_nod, name)))
return nod;
nod = alloc_nod(fs);
if (!(mode & FM_IFDIR))
mode |= FM_IFDIR;
add2dir(fs, parent_nod, nod, name, mode, uid, gid, ctime);
add2dir(fs, nod, nod, ".", mode, uid, gid, ctime);
add2dir(fs, nod, parent_nod, "..", mode, uid, gid, ctime);
fs->gd.bg_used_dirs_count++;
return nod;
}
// make a symlink
uint32 mklink_fs(filesystem *fs, uint32 parent_nod, const char *name, size_t size, uint8 * b, uid_t uid, gid_t gid, time_t ctime)
{
uint32 mode;
uint32 nod = alloc_nod(fs);
mode = FM_IFLNK | FM_IRWXU | FM_IRWXG | FM_IRWXO;
get_nod(fs, nod)->i_size = size;
add2dir(fs, parent_nod, nod, name, mode, uid, gid, ctime);
if(size <= 4 * (EXT2_TIND_BLOCK+1))
{
strncpy((char*)get_nod(fs, nod)->i_block, (char*)b, size);
return nod;
}
extend_blk(fs, nod, b, rndup(size, BLOCKSIZE) / BLOCKSIZE);
return nod;
}
// make a file from a FILE*
uint32 mkfile_fs(filesystem *fs, uint32 parent_nod, const char *name, uint32 mode, size_t size, FILE *f, uid_t uid, gid_t gid, time_t ctime)
{
uint8 * b;
uint32 nod = alloc_nod(fs);
mode |= FM_IFREG;
get_nod(fs, nod)->i_size = size;
add2dir(fs, parent_nod, nod, name, mode, uid, gid, ctime);
if(!(b = (uint8*)malloc(rndup(size, BLOCKSIZE))))
errexit("not enough mem to read file '%s'", name);
if(f)
fread(b, size, 1, f);
else
memset(b, 0, size);
extend_blk(fs, nod, b, rndup(size, BLOCKSIZE) / BLOCKSIZE);
free(b);
return nod;
}
// retrieves a mode info from a struct stat
uint32 get_mode(struct stat *st)
{
uint32 mode = 0;
if (nosquash == 1)
return st->st_mode;
if(st->st_mode & S_IRUSR)
mode |= FM_IRUSR | FM_IRGRP | FM_IROTH;
if(st->st_mode & S_IWUSR)
mode |= FM_IWUSR | FM_IWGRP | FM_IWOTH;
if(st->st_mode & S_IXUSR)
mode |= FM_IXUSR | FM_IXGRP | FM_IXOTH;
return mode;
}
// retrieves a mode info from a string
uint32 get_modestr(const char *p)
{
uint32 mode = 0;
if(p[0] == 'r')
mode |= FM_IRUSR | FM_IRGRP | FM_IROTH;
if(p[1] == 'w')
mode |= FM_IWUSR | FM_IWGRP | FM_IWOTH;
if(p[2] == 'x' || p[2] == 's')
mode |= FM_IXUSR | FM_IXGRP | FM_IXOTH;
return mode;
}
// basename of a path - free me
char * basename(const char * fullpath)
{
char * p = strrchr(fullpath, '/');
return strdup(p ? p + 1 : fullpath);
}
// dirname of a path - free me
char * dirname(const char * fullpath)
{
char * p, * n = strdup(fullpath);
if((p = strrchr(n, '/')))
*(p+1) = 0;
else
*n = 0;
return n;
}
// adds entries to the filesystem from a text file
void add2fs_from_file(filesystem *fs, uint32 this_nod, FILE * fh)
{
uint32 mode;
uint32 nod, nod2;
char cmod[11], *path, *name, *dir;
int major, minor;
while(fscanf(fh, "%10s", cmod))
{
if(feof(fh))
break;
mode = get_modestr(cmod + 1);
switch(*cmod)
{
case 'd':
fscanf(fh, "%as\n", &path);
break;
case 'c':
mode |= FM_IFCHR;
fscanf(fh, "%i, %i %as\n", &major, &minor, &path);
break;
case 'b':
mode |= FM_IFBLK;
fscanf(fh, "%i, %i %as\n", &major, &minor, &path);
break;
case '#':
while(fgetc(fh) != '\n');
continue;
default:
errexit("malformed text input file");
}
name = basename(path);
dir = dirname(path);
free(path);
if(!(nod = find_path(fs, this_nod, dir)))
errexit("can't find directory '%s' to create '%s''", dir, name);
free(dir);
if((!strcmp(name, ".")) || (!strcmp(name, "..")))
{
free(name);
continue;
}
switch(*cmod)
{
case 'd':
// FIXME - This really needs a get_uid_gid_str()
mkdir_fs(fs, nod, name, mode, 0, 0, time(NULL));
break;
case 'c':
case 'b':
nod2 = alloc_nod(fs);
((uint8*)get_nod(fs, nod2)->i_block)[0] = minor;
((uint8*)get_nod(fs, nod2)->i_block)[1] = major;
// FIXME - This really needs a get_uid_gid_str()
add2dir(fs, nod, nod2, name, mode, 0, 0, time(NULL));
break;
}
free(name);
}
}
// adds a tree of entries to the filesystem from current dir
void add2fs_from_dir(filesystem *fs, uint32 this_nod)
{
uint32 nod;
FILE *fh;
DIR *dh;
struct dirent *dent;
struct stat st;
uint8 *b;
if(!(dh = opendir(".")))
pexit(".");
while((dent = readdir(dh)))
{
if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
continue;
lstat(dent->d_name, &st);
switch(st.st_mode & S_IFMT)
{
case S_IFCHR:
case S_IFBLK:
nod = alloc_nod(fs);
get_nod(fs, nod)->i_mode = (((st.st_mode & S_IFMT) == S_IFCHR) ? FM_IFCHR : FM_IFBLK) | get_mode(&st);
((uint8*)get_nod(fs, nod)->i_block)[0] = (st.st_rdev & 0xff);
((uint8*)get_nod(fs, nod)->i_block)[1] = (st.st_rdev >> 8);
add2dir(fs, this_nod, nod, dent->d_name, st.st_mode, st.st_uid, st.st_gid, st.st_ctime);
break;
case S_IFLNK:
if(!(b = (uint8*)malloc(rndup(st.st_size, BLOCKSIZE))))
errexit("out of memory");
if(readlink(dent->d_name, (char*)b, st.st_size) < 0)
pexit(dent->d_name);
mklink_fs(fs, this_nod, dent->d_name, st.st_size, b, st.st_uid, st.st_gid, st.st_ctime);
free(b);
break;
case S_IFREG:
if(!(fh = fopen(dent->d_name, "r")))
pexit(dent->d_name);
mkfile_fs(fs, this_nod, dent->d_name, st.st_mode, st.st_size, fh, st.st_uid, st.st_gid, st.st_ctime);
fclose(fh);
break;
case S_IFDIR:
nod = mkdir_fs(fs, this_nod, dent->d_name, st.st_mode, st.st_uid, st.st_gid, st.st_ctime);
if(chdir(dent->d_name) < 0)
pexit(dent->d_name);
add2fs_from_dir(fs, nod);
chdir("..");
break;
default:
fprintf(stderr, "ignoring entry %s", dent->d_name);
}
}
closedir(dh);
}
// endianness swap of x-indirect blocks
void swap_goodblocks(filesystem *fs, inode *nod)
{
int i;
int nblk = nod->i_blocks / INOBLK;
if(nod->i_size && !nblk)