-
Notifications
You must be signed in to change notification settings - Fork 0
/
mksquashfs.c
4916 lines (4128 loc) · 129 KB
/
mksquashfs.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
/*
* Create a squashfs filesystem. This is a highly compressed read only filesystem.
*
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
* Phillip Lougher <[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,
* or (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* mksquashfs.c
*/
#define FALSE 0
#define TRUE 1
#include <fnmatch.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <zlib.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <pthread.h>
#include <math.h>
#include <regex.h>
#ifndef linux
#define __BYTE_ORDER BYTE_ORDER
#define __BIG_ENDIAN BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#include <sys/sysctl.h>
#else
#include <endian.h>
#include <sys/sysinfo.h>
#endif
#include "squashfs_fs.h"
#include "squashfs_swap.h"
#include "mksquashfs.h"
#include "global.h"
#include "sort.h"
#include "pseudo.h"
#include "mycompress2.h"
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef SQUASHFS_TRACE
#define TRACE(s, args...) do { \
if(progress_enabled) \
printf("\n"); \
printf("mksquashfs: "s, ## args); \
} while(0)
#else
#define TRACE(s, args...)
#endif
#define INFO(s, args...) do {\
if(!silent)\
printf("mksquashfs: "s, ## args);\
} while(0)
#define ERROR(s, args...) do {\
pthread_mutex_lock(&progress_mutex); \
if(progress_enabled) \
fprintf(stderr, "\n"); \
fprintf(stderr, s, ## args);\
pthread_mutex_unlock(&progress_mutex); \
} while(0)
#define EXIT_MKSQUASHFS() do {\
if(restore)\
restorefs();\
if(delete && destination_file && !block_device)\
unlink(destination_file);\
exit(1);\
} while(0)
#define BAD_ERROR(s, args...) do {\
pthread_mutex_lock(&progress_mutex); \
if(progress_enabled) \
fprintf(stderr, "\n"); \
fprintf(stderr, "FATAL ERROR:" s, ##args);\
pthread_mutex_unlock(&progress_mutex); \
EXIT_MKSQUASHFS();\
} while(0)
/* offset of data in compressed metadata blocks (allowing room for
* compressed size */
#define BLOCK_OFFSET 2
int delete = FALSE;
int fd;
int cur_uncompressed = 0, estimated_uncompressed = 0;
int columns;
/* filesystem flags for building */
int duplicate_checking = 1, noF = 0, no_fragments = 0, always_use_fragments = 0;
int noI = 0, noD = 0;
int silent = TRUE;
long long global_uid = -1, global_gid = -1;
int exportable = TRUE;
int progress = TRUE;
int progress_enabled = FALSE;
int sparse_files = TRUE;
int old_exclude = TRUE;
int use_regex = FALSE;
/* superblock attributes */
int block_size = SQUASHFS_FILE_SIZE, block_log;
unsigned int id_count = 0;
int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0, fifo_count = 0,
sock_count = 0;
/* write position within data section */
long long bytes = 0, total_bytes = 0;
/* in memory directory table - possibly compressed */
char *directory_table = NULL;
unsigned int directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
/* cached directory table */
char *directory_data_cache = NULL;
unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
/* in memory inode table - possibly compressed */
char *inode_table = NULL;
unsigned int inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
/* cached inode table */
char *data_cache = NULL;
unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
/* inode lookup table */
squashfs_inode *inode_lookup_table = NULL;
/* in memory directory data */
#define I_COUNT_SIZE 128
#define DIR_ENTRIES 32
#define INODE_HASH_SIZE 65536
#define INODE_HASH_MASK (INODE_HASH_SIZE - 1)
#define INODE_HASH(dev, ino) (ino & INODE_HASH_MASK)
struct cached_dir_index {
squashfs_dir_index index;
char *name;
};
struct directory {
unsigned int start_block;
unsigned int size;
unsigned char *buff;
unsigned char *p;
unsigned int entry_count;
unsigned char *entry_count_p;
unsigned int i_count;
unsigned int i_size;
struct cached_dir_index *index;
unsigned char *index_count_p;
unsigned int inode_number;
};
struct inode_info *inode_info[INODE_HASH_SIZE];
/* hash tables used to do fast duplicate searches in duplicate check */
struct file_info *dupl[65536];
int dup_files = 0;
/* exclude file handling */
/* list of exclude dirs/files */
struct exclude_info {
dev_t st_dev;
ino_t st_ino;
};
#define EXCLUDE_SIZE 8192
int exclude = 0;
struct exclude_info *exclude_paths = NULL;
int old_excluded(char *filename, struct stat *buf);
struct path_entry {
char *name;
regex_t *preg;
struct pathname *paths;
};
struct pathname {
int names;
struct path_entry *name;
};
struct pathnames {
int count;
struct pathname *path[0];
};
#define PATHS_ALLOC_SIZE 10
struct pathnames *paths = NULL;
struct pathname *path = NULL;
struct pathname *stickypath = NULL;
int excluded(struct pathnames *paths, char *name, struct pathnames **new);
/* fragment block data structures */
int fragments = 0;
struct file_buffer *fragment_data = NULL;
int fragment_size = 0;
struct fragment {
unsigned int index;
int offset;
int size;
};
#define FRAG_SIZE 32768
#define FRAG_INDEX (1LL << 32)
squashfs_fragment_entry *fragment_table = NULL;
int fragments_outstanding = 0;
/* current inode number for directories and non directories */
unsigned int dir_inode_no = 1;
unsigned int inode_no = 0;
unsigned int root_inode_number = 0;
/* list of source dirs/files */
int source = 0;
char **source_path;
/* list of root directory entries read from original filesystem */
int old_root_entries = 0;
struct old_root_entry_info {
char name[SQUASHFS_NAME_LEN + 1];
squashfs_inode inode;
int type;
int inode_number;
};
struct old_root_entry_info *old_root_entry;
/* in memory file info */
struct file_info {
long long file_size;
long long bytes;
unsigned short checksum;
unsigned short fragment_checksum;
long long start;
unsigned int *block_list;
struct file_info *next;
struct fragment *fragment;
char checksum_flag;
};
/* count of how many times SIGINT or SIGQUIT has been sent */
int interrupted = 0;
/* restore orignal filesystem state if appending to existing filesystem is
* cancelled */
jmp_buf env;
char *sdata_cache, *sdirectory_data_cache, *sdirectory_compressed;
long long sbytes, stotal_bytes;
unsigned int sinode_bytes, scache_bytes, sdirectory_bytes,
sdirectory_cache_bytes, sdirectory_compressed_bytes,
stotal_inode_bytes, stotal_directory_bytes,
sinode_count = 0, sfile_count, ssym_count, sdev_count,
sdir_count, sfifo_count, ssock_count, sdup_files;
int sfragments;
int restore = 0;
int threads;
/* flag whether destination file is a block device */
int block_device = 0;
/* flag indicating whether files are sorted using sort list(s) */
int sorted = 0;
/* save destination file name for deleting on error */
char *destination_file = NULL;
/* recovery file for abnormal exit on appending */
char recovery_file[1024] = "";
int recover = TRUE;
/* struct describing a cache entry passed between threads */
struct file_buffer {
struct cache *cache;
int keep;
long long file_size;
long long index;
long long block;
long long sequence;
int size;
int c_byte;
int used;
int fragment;
int error;
struct file_buffer *hash_next;
struct file_buffer *hash_prev;
struct file_buffer *free_next;
struct file_buffer *free_prev;
struct file_buffer *next;
char data[0];
};
/* struct describing queues used to pass data between threads */
struct queue {
int size;
int readp;
int writep;
pthread_mutex_t mutex;
pthread_cond_t empty;
pthread_cond_t full;
void **data;
};
/* in memory uid tables */
#define ID_ENTRIES 256
#define ID_HASH(id) (id & (ID_ENTRIES - 1))
#define ISA_UID 1
#define ISA_GID 2
struct id {
unsigned int id;
int index;
char flags;
struct id *next;
};
struct id *id_hash_table[ID_ENTRIES];
struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
unsigned int uid_count = 0, guid_count = 0;
unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
struct cache *reader_buffer, *writer_buffer, *fragment_buffer;
struct queue *to_reader, *from_reader, *to_writer, *from_writer, *from_deflate,
*to_frag;
pthread_t *thread, *deflator_thread, *frag_deflator_thread, progress_thread;
pthread_mutex_t fragment_mutex;
pthread_cond_t fragment_waiting;
pthread_mutex_t pos_mutex;
pthread_mutex_t progress_mutex;
pthread_cond_t progress_wait;
int rotate = 0;
struct pseudo *pseudo = NULL;
/* user options that control parallelisation */
int processors = -1;
/* default size of output buffer in Mbytes */
#define WRITER_BUFFER_DEFAULT 512
/* default size of input buffer in Mbytes */
#define READER_BUFFER_DEFAULT 64
/* default size of fragment buffer in Mbytes */
#define FRAGMENT_BUFFER_DEFAULT 64
int writer_buffer_size;
int reader_buffer_size;
int fragment_buffer_size;
char *read_from_disk(long long start, unsigned int avail_bytes);
void add_old_root_entry(char *name, squashfs_inode inode, int inode_number,
int type);
extern int read_super(int fd, squashfs_super_block *sBlk, char *source);
extern long long read_filesystem(char *root_name, int fd,
squashfs_super_block *sBlk, char **cinode_table, char **data_cache,
char **cdirectory_table, char **directory_data_cache,
unsigned int *last_directory_block, unsigned int *inode_dir_offset,
unsigned int *inode_dir_file_size, unsigned int *root_inode_size,
unsigned int *inode_dir_start_block, int *file_count, int *sym_count,
int *dev_count, int *dir_count, int *fifo_count, int *sock_count,
long long *uncompressed_file, unsigned int *uncompressed_inode,
unsigned int *uncompressed_directory,
unsigned int *inode_dir_inode_number,
unsigned int *inode_dir_parent_inode,
void (push_directory_entry)(char *, squashfs_inode, int, int),
squashfs_fragment_entry **fragment_table,
squashfs_inode **inode_lookup_table);
extern int read_sort_file(char *filename, int source, char *source_path[]);
extern void sort_files_and_write(struct dir_info *dir);
struct file_info *duplicate(long long file_size, long long bytes,
unsigned int **block_list, long long *start, struct fragment **fragment,
struct file_buffer *file_buffer, int blocks, unsigned short checksum,
unsigned short fragment_checksum, int checksum_flag);
struct dir_info *dir_scan1(char *, struct pathnames *, int (_readdir)(char *,
char *, struct dir_info *));
struct dir_info *dir_scan2(struct dir_info *dir, struct pseudo *pseudo);
void dir_scan3(squashfs_inode *inode, struct dir_info *dir_info);
struct file_info *add_non_dup(long long file_size, long long bytes,
unsigned int *block_list, long long start, struct fragment *fragment,
unsigned short checksum, unsigned short fragment_checksum,
int checksum_flag);
extern void generate_file_priorities(struct dir_info *dir, int priority,
struct stat *buf);
extern struct priority_entry *priority_list[65536];
void progress_bar(long long current, long long max, int columns);
long long generic_write_table(int length, char *buffer, int uncompressed);
struct queue *queue_init(int size)
{
struct queue *queue = malloc(sizeof(struct queue));
if(queue == NULL)
return NULL;
if((queue->data = malloc(sizeof(void *) * (size + 1))) == NULL) {
free(queue);
return NULL;
}
queue->size = size + 1;
queue->readp = queue->writep = 0;
pthread_mutex_init(&queue->mutex, NULL);
pthread_cond_init(&queue->empty, NULL);
pthread_cond_init(&queue->full, NULL);
return queue;
}
void queue_put(struct queue *queue, void *data)
{
int nextp;
pthread_mutex_lock(&queue->mutex);
while((nextp = (queue->writep + 1) % queue->size) == queue->readp)
pthread_cond_wait(&queue->full, &queue->mutex);
queue->data[queue->writep] = data;
queue->writep = nextp;
pthread_cond_signal(&queue->empty);
pthread_mutex_unlock(&queue->mutex);
}
void *queue_get(struct queue *queue)
{
void *data;
pthread_mutex_lock(&queue->mutex);
while(queue->readp == queue->writep)
pthread_cond_wait(&queue->empty, &queue->mutex);
data = queue->data[queue->readp];
queue->readp = (queue->readp + 1) % queue->size;
pthread_cond_signal(&queue->full);
pthread_mutex_unlock(&queue->mutex);
return data;
}
/* Cache status struct. Caches are used to keep
track of memory buffers passed between different threads */
struct cache {
int max_buffers;
int count;
int buffer_size;
pthread_mutex_t mutex;
pthread_cond_t wait_for_free;
struct file_buffer *free_list;
struct file_buffer *hash_table[65536];
};
#define INSERT_LIST(NAME, TYPE) \
void insert_##NAME##_list(TYPE **list, TYPE *entry) { \
if(*list) { \
entry->NAME##_next = *list; \
entry->NAME##_prev = (*list)->NAME##_prev; \
(*list)->NAME##_prev->NAME##_next = entry; \
(*list)->NAME##_prev = entry; \
} else { \
*list = entry; \
entry->NAME##_prev = entry->NAME##_next = entry; \
} \
}
#define REMOVE_LIST(NAME, TYPE) \
void remove_##NAME##_list(TYPE **list, TYPE *entry) { \
if(entry->NAME##_prev == entry && entry->NAME##_next == entry) { \
/* only this entry in the list */ \
*list = NULL; \
} else if(entry->NAME##_prev != NULL && entry->NAME##_next != NULL) { \
/* more than one entry in the list */ \
entry->NAME##_next->NAME##_prev = entry->NAME##_prev; \
entry->NAME##_prev->NAME##_next = entry->NAME##_next; \
if(*list == entry) \
*list = entry->NAME##_next; \
} \
entry->NAME##_prev = entry->NAME##_next = NULL; \
}
#define CALCULATE_HASH(start) (start & 0xffff) \
/* Called with the cache mutex held */
void insert_hash_table(struct cache *cache, struct file_buffer *entry)
{
int hash = CALCULATE_HASH(entry->index);
entry->hash_next = cache->hash_table[hash];
cache->hash_table[hash] = entry;
entry->hash_prev = NULL;
if(entry->hash_next)
entry->hash_next->hash_prev = entry;
}
/* Called with the cache mutex held */
void remove_hash_table(struct cache *cache, struct file_buffer *entry)
{
if(entry->hash_prev)
entry->hash_prev->hash_next = entry->hash_next;
else
cache->hash_table[CALCULATE_HASH(entry->index)] =
entry->hash_next;
if(entry->hash_next)
entry->hash_next->hash_prev = entry->hash_prev;
entry->hash_prev = entry->hash_next = NULL;
}
/* Called with the cache mutex held */
INSERT_LIST(free, struct file_buffer)
/* Called with the cache mutex held */
REMOVE_LIST(free, struct file_buffer)
struct cache *cache_init(int buffer_size, int max_buffers)
{
struct cache *cache = malloc(sizeof(struct cache));
if(cache == NULL)
return NULL;
cache->max_buffers = max_buffers;
cache->buffer_size = buffer_size;
cache->count = 0;
cache->free_list = NULL;
memset(cache->hash_table, 0, sizeof(struct file_buffer *) * 65536);
pthread_mutex_init(&cache->mutex, NULL);
pthread_cond_init(&cache->wait_for_free, NULL);
return cache;
}
struct file_buffer *cache_lookup(struct cache *cache, long long index)
{
/* Lookup block in the cache, if found return with usage count
* incremented, if not found return NULL */
int hash = CALCULATE_HASH(index);
struct file_buffer *entry;
pthread_mutex_lock(&cache->mutex);
for(entry = cache->hash_table[hash]; entry; entry = entry->hash_next)
if(entry->index == index)
break;
if(entry) {
/* found the block in the cache, increment used count and
* if necessary remove from free list so it won't disappear
*/
entry->used ++;
remove_free_list(&cache->free_list, entry);
}
pthread_mutex_unlock(&cache->mutex);
return entry;
}
#define GET_FREELIST 1
struct file_buffer *cache_get(struct cache *cache, long long index, int keep)
{
/* Get a free block out of the cache indexed on index. */
struct file_buffer *entry;
pthread_mutex_lock(&cache->mutex);
while(1) {
/* first try to get a block from the free list */
#ifdef GET_FREELIST
if(cache->free_list) {
/* a block on the free_list is a "keep" block */
entry = cache->free_list;
remove_free_list(&cache->free_list, entry);
remove_hash_table(cache, entry);
break;
} else
#endif
if(cache->count < cache->max_buffers) {
/* next try to allocate new block */
entry = malloc(sizeof(struct file_buffer) +
cache->buffer_size);
if(entry == NULL)
goto failed;
entry->cache = cache;
entry->free_prev = entry->free_next = NULL;
cache->count ++;
break;
} else
#ifndef GET_FREELIST
if(cache->free_list) {
/* a block on the free_list is a "keep" block */
entry = cache->free_list;
remove_free_list(&cache->free_list, entry);
remove_hash_table(cache, entry);
break;
}
#endif
/* wait for a block */
pthread_cond_wait(&cache->wait_for_free, &cache->mutex);
}
/* initialise block and if a keep block insert into the hash table */
entry->used = 1;
entry->error = FALSE;
entry->keep = keep;
if(keep) {
entry->index = index;
insert_hash_table(cache, entry);
}
pthread_mutex_unlock(&cache->mutex);
return entry;
failed:
pthread_mutex_unlock(&cache->mutex);
return NULL;
}
void cache_rehash(struct file_buffer *entry, long long index)
{
struct cache *cache = entry->cache;
pthread_mutex_lock(&cache->mutex);
if(entry->keep)
remove_hash_table(cache, entry);
entry->keep = TRUE;
entry->index = index;
insert_hash_table(cache, entry);
pthread_mutex_unlock(&cache->mutex);
}
void cache_block_put(struct file_buffer *entry)
{
struct cache *cache;
/* finished with this cache entry, once the usage count reaches zero it
* can be reused and if a keep block put onto the free list. As keep
* blocks remain accessible via the hash table they can be found
* getting a new lease of life before they are reused. */
if(entry == NULL)
return;
cache = entry->cache;
pthread_mutex_lock(&cache->mutex);
entry->used --;
if(entry->used == 0) {
if(entry->keep)
insert_free_list(&cache->free_list, entry);
else {
free(entry);
cache->count --;
}
/* One or more threads may be waiting on this block */
pthread_cond_signal(&cache->wait_for_free);
}
pthread_mutex_unlock(&cache->mutex);
}
#define MKINODE(A) ((squashfs_inode)(((squashfs_inode) inode_bytes << 16) \
+ (((char *)A) - data_cache)))
inline void inc_progress_bar()
{
cur_uncompressed ++;
}
inline void update_progress_bar()
{
pthread_mutex_lock(&progress_mutex);
pthread_cond_signal(&progress_wait);
pthread_mutex_unlock(&progress_mutex);
}
inline void waitforthread(int i)
{
TRACE("Waiting for thread %d\n", i);
while(thread[i] != 0)
sched_yield();
}
void restorefs()
{
int i;
if(thread == NULL || thread[0] == 0)
return;
ERROR("Exiting - restoring original filesystem!\n\n");
for(i = 0; i < 2 + processors * 2; i++)
if(thread[i])
pthread_kill(thread[i], SIGUSR1);
for(i = 0; i < 2 + processors * 2; i++)
waitforthread(i);
TRACE("All threads in signal handler\n");
bytes = sbytes;
memcpy(data_cache, sdata_cache, cache_bytes = scache_bytes);
memcpy(directory_data_cache, sdirectory_data_cache,
sdirectory_cache_bytes);
directory_cache_bytes = sdirectory_cache_bytes;
inode_bytes = sinode_bytes;
directory_bytes = sdirectory_bytes;
memcpy(directory_table + directory_bytes, sdirectory_compressed,
sdirectory_compressed_bytes);
directory_bytes += sdirectory_compressed_bytes;
total_bytes = stotal_bytes;
total_inode_bytes = stotal_inode_bytes;
total_directory_bytes = stotal_directory_bytes;
inode_count = sinode_count;
file_count = sfile_count;
sym_count = ssym_count;
dev_count = sdev_count;
dir_count = sdir_count;
fifo_count = sfifo_count;
sock_count = ssock_count;
dup_files = sdup_files;
fragments = sfragments;
fragment_size = 0;
id_count = sid_count;
longjmp(env, 1);
}
void sighandler()
{
if(++interrupted > 2)
return;
if(interrupted == 2)
restorefs();
else {
ERROR("Interrupting will restore original filesystem!\n");
ERROR("Interrupt again to quit\n");
}
}
void sighandler2()
{
EXIT_MKSQUASHFS();
}
void sigusr1_handler()
{
int i;
sigset_t sigmask;
pthread_t thread_id = pthread_self();
for(i = 0; i < (2 + processors * 2) && thread[i] != thread_id; i++);
thread[i] = (pthread_t) 0;
TRACE("Thread %d(%p) in sigusr1_handler\n", i, &thread_id);
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGINT);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGUSR1);
while(1) {
sigsuspend(&sigmask);
TRACE("After wait in sigusr1_handler :(\n");
}
}
void sigwinch_handler()
{
struct winsize winsize;
if(ioctl(1, TIOCGWINSZ, &winsize) == -1) {
if(isatty(STDOUT_FILENO))
printf("TIOCGWINSZ ioctl failed, defaulting to 80 "
"columns\n");
columns = 80;
} else
columns = winsize.ws_col;
}
void sigalrm_handler()
{
rotate = (rotate + 1) % 4;
}
#if 1
unsigned int mangle2(z_stream **strm, char *d, char *s, int size,
int block_size, int uncompressed, int data_block)
{
unsigned long c_byte;
unsigned int res;
z_stream *stream = *strm;
if(uncompressed)
goto notcompressed;
if(stream == NULL) {
if((stream = *strm = malloc(sizeof(z_stream))) == NULL)
BAD_ERROR("mangle::compress failed, not enough "
"memory\n");
stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
stream->opaque = 0;
if((res = deflateInit(stream, 9)) != Z_OK) {
if(res == Z_MEM_ERROR)
BAD_ERROR("zlib::compress failed, not enough "
"memory\n");
else if(res == Z_STREAM_ERROR)
BAD_ERROR("zlib::compress failed, not a valid "
"compression level\n");
else if(res == Z_VERSION_ERROR)
BAD_ERROR("zlib::compress failed, incorrect "
"zlib version\n");
else
BAD_ERROR("zlib::compress failed, unknown "
"error %d\n", res);
}
} else if((res = deflateReset(stream)) != Z_OK) {
if(res == Z_STREAM_ERROR)
BAD_ERROR("zlib::compress failed, stream state "
"inconsistent\n");
else
BAD_ERROR("zlib::compress failed, unknown error %d\n",
res);
}
stream->next_in = (unsigned char *) s;
stream->avail_in = size;
stream->next_out = (unsigned char *) d;
stream->avail_out = block_size;
res = deflate(stream, Z_FINISH);
if(res != Z_STREAM_END && res != Z_OK) {
if(res == Z_STREAM_ERROR)
BAD_ERROR("zlib::compress failed, stream state "
"inconsistent\n");
else if(res == Z_BUF_ERROR)
BAD_ERROR("zlib::compress failed, no progress possible"
"\n");
else
BAD_ERROR("zlib::compress failed, unknown error %d\n",
res);
}
c_byte = stream->total_out;
if(res != Z_STREAM_END || c_byte >= size) {
notcompressed:
memcpy(d, s, size);
return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
SQUASHFS_COMPRESSED_BIT);
}
return (unsigned int) c_byte;
}
#endif
unsigned int mangle(char *d, char *s, int size, int block_size,
int uncompressed, int data_block)
{
#if 0
static z_stream *stream = NULL;
return mangle2(&stream, d, s, size, block_size, uncompressed,
data_block);
#else
unsigned long c_byte = block_size << 1;
if (!uncompressed) {
c_byte = mycompress2((unsigned char *)d, (unsigned char *)s, size, block_size, data_block);
}
if (uncompressed || c_byte >= size) {
memcpy(d, s, size);
return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK : SQUASHFS_COMPRESSED_BIT);
}
return (unsigned int) c_byte;
#endif
}
/*unsigned int mangle2(z_stream **strm, char *d, char *s, int size,
int block_size, int uncompressed, int data_block)
{
return mangle(d, s, size, block_size, uncompressed, data_block);
}*/
squashfs_base_inode_header *get_inode(int req_size)
{
int data_space;
unsigned short c_byte;
while(cache_bytes >= SQUASHFS_METADATA_SIZE) {
if((inode_size - inode_bytes) <
((SQUASHFS_METADATA_SIZE << 1)) + 2) {
inode_table = realloc(inode_table, inode_size +
(SQUASHFS_METADATA_SIZE << 1) + 2);
if(inode_table == NULL) {
goto failed;
}
inode_size += (SQUASHFS_METADATA_SIZE << 1) + 2;
}
c_byte = mangle(inode_table + inode_bytes + BLOCK_OFFSET,
data_cache, SQUASHFS_METADATA_SIZE,
SQUASHFS_METADATA_SIZE, noI, 0);
TRACE("Inode block @ 0x%x, size %d\n", inode_bytes, c_byte);
SQUASHFS_SWAP_SHORTS(&c_byte,
(unsigned short *) (inode_table + inode_bytes), 1);
inode_bytes += SQUASHFS_COMPRESSED_SIZE(c_byte) + BLOCK_OFFSET;
total_inode_bytes += SQUASHFS_METADATA_SIZE + BLOCK_OFFSET;
memcpy(data_cache, data_cache + SQUASHFS_METADATA_SIZE,
cache_bytes - SQUASHFS_METADATA_SIZE);
cache_bytes -= SQUASHFS_METADATA_SIZE;
}
data_space = (cache_size - cache_bytes);
if(data_space < req_size) {
int realloc_size = cache_size == 0 ?
((req_size + SQUASHFS_METADATA_SIZE) &
~(SQUASHFS_METADATA_SIZE - 1)) : req_size -
data_space;
data_cache = realloc(data_cache, cache_size +
realloc_size);
if(data_cache == NULL) {
goto failed;
}
cache_size += realloc_size;
}
cache_bytes += req_size;
return (squashfs_base_inode_header *)
(data_cache + (cache_bytes - req_size));
failed:
BAD_ERROR("Out of memory in inode table reallocation!\n");
}
int read_bytes(int fd, void *buff, int bytes)
{
int res, count;