-
Notifications
You must be signed in to change notification settings - Fork 78
/
stream.c
1945 lines (1672 loc) · 55.5 KB
/
stream.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
/*
Copyright (C) 2006-2016,2018,2021-2022 Con Kolivas
Copyright (C) 2011 Serge Belyshev
Copyright (C) 2011 Peter Hyman
Copyright (C) 1998 Andrew Tridgell
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* multiplex N streams into a file - the streams are passed
through different compressors */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/statvfs.h>
#include <pthread.h>
#include <bzlib.h>
#include <zlib.h>
#include <lzo/lzoconf.h>
#include <lzo/lzo1x.h>
#include <lz4.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_ENDIAN_H
# include <endian.h>
#elif HAVE_SYS_ENDIAN_H
# include <sys/endian.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
/* LZMA C Wrapper */
#include "lzma/C/LzmaLib.h"
#include "util.h"
#include "lrzip_core.h"
#define STREAM_BUFSIZE (1024 * 1024 * 10)
static struct compress_thread {
uchar *s_buf; /* Uncompressed buffer -> Compressed buffer */
uchar c_type; /* Compression type */
i64 s_len; /* Data length uncompressed */
i64 c_len; /* Data length compressed */
cksem_t cksem; /* This thread's semaphore */
struct stream_info *sinfo;
int streamno;
uchar salt[SALT_LEN];
} *cthreads;
typedef struct stream_thread_struct {
int i;
rzip_control *control;
struct stream_info *sinfo;
} stream_thread_struct;
static long output_thread;
static pthread_mutex_t output_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t output_cond = PTHREAD_COND_INITIALIZER;
bool init_mutex(rzip_control *control, pthread_mutex_t *mutex)
{
if (unlikely(pthread_mutex_init(mutex, NULL)))
fatal_return(("Failed to pthread_mutex_init\n"), false);
return true;
}
bool unlock_mutex(rzip_control *control, pthread_mutex_t *mutex)
{
if (unlikely(pthread_mutex_unlock(mutex)))
fatal_return(("Failed to pthread_mutex_unlock\n"), false);
return true;
}
bool lock_mutex(rzip_control *control, pthread_mutex_t *mutex)
{
if (unlikely(pthread_mutex_lock(mutex)))
fatal_return(("Failed to pthread_mutex_lock\n"), false);
return true;
}
static bool cond_wait(rzip_control *control, pthread_cond_t *cond, pthread_mutex_t *mutex)
{
if (unlikely(pthread_cond_wait(cond, mutex)))
fatal_return(("Failed to pthread_cond_wait\n"), false);
return true;
}
static bool cond_broadcast(rzip_control *control, pthread_cond_t *cond)
{
if (unlikely(pthread_cond_broadcast(cond)))
fatal_return(("Failed to pthread_cond_broadcast\n"), false);
return true;
}
bool create_pthread(rzip_control *control, pthread_t *thread, pthread_attr_t * attr,
void * (*start_routine)(void *), void *arg)
{
if (unlikely(pthread_create(thread, attr, start_routine, arg)))
fatal_return(("Failed to pthread_create\n"), false);
return true;
}
bool detach_pthread(rzip_control *control, pthread_t *thread)
{
if (unlikely(pthread_detach(*thread)))
fatal_return(("Failed to pthread_detach\n"), false);
return true;
}
bool join_pthread(rzip_control *control, pthread_t th, void **thread_return)
{
if (pthread_join(th, thread_return))
fatal_return(("Failed to pthread_join\n"), false);
return true;
}
/* just to keep things clean, declare function here
* but move body to the end since it's a work function
*/
static int lz4_compresses(rzip_control *control, uchar *s_buf, i64 s_len);
/*
***** COMPRESSION FUNCTIONS *****
ZPAQ, BZIP, GZIP, LZMA, LZO
try to compress a buffer. If compression fails for whatever reason then
leave uncompressed. Return the compression type in c_type and resulting
length in c_len
*/
static int zpaq_compress_buf(rzip_control *control, struct compress_thread *cthread, long thread)
{
i64 c_len, c_size;
uchar *c_buf;
if (!lz4_compresses(control, cthread->s_buf, cthread->s_len))
return 0;
c_size = round_up_page(control, cthread->s_len + 10000);
c_buf = malloc(c_size);
if (!c_buf) {
print_err("Unable to allocate c_buf in zpaq_compress_buf\n");
return -1;
}
c_len = 0;
zpaq_compress(c_buf, &c_len, cthread->s_buf, cthread->s_len, control->compression_level / 4 + 1,
control->msgout, SHOW_PROGRESS ? true: false, thread);
if (unlikely(c_len >= cthread->c_len)) {
print_maxverbose("Incompressible block\n");
/* Incompressible, leave as CTYPE_NONE */
dealloc(c_buf);
return 0;
}
cthread->c_len = c_len;
dealloc(cthread->s_buf);
cthread->s_buf = c_buf;
cthread->c_type = CTYPE_ZPAQ;
return 0;
}
static int bzip2_compress_buf(rzip_control *control, struct compress_thread *cthread)
{
u32 dlen = round_up_page(control, cthread->s_len);
int bzip2_ret;
uchar *c_buf;
if (!lz4_compresses(control, cthread->s_buf, cthread->s_len))
return 0;
c_buf = malloc(dlen);
if (!c_buf) {
print_err("Unable to allocate c_buf in bzip2_compress_buf\n");
return -1;
}
bzip2_ret = BZ2_bzBuffToBuffCompress((char *)c_buf, &dlen,
(char *)cthread->s_buf, cthread->s_len,
control->compression_level, 0, control->compression_level * 10);
/* if compressed data is bigger then original data leave as
* CTYPE_NONE */
if (bzip2_ret == BZ_OUTBUFF_FULL) {
print_maxverbose("Incompressible block\n");
/* Incompressible, leave as CTYPE_NONE */
dealloc(c_buf);
return 0;
}
if (unlikely(bzip2_ret != BZ_OK)) {
dealloc(c_buf);
print_maxverbose("BZ2 compress failed\n");
return -1;
}
if (unlikely(dlen >= cthread->c_len)) {
print_maxverbose("Incompressible block\n");
/* Incompressible, leave as CTYPE_NONE */
dealloc(c_buf);
return 0;
}
cthread->c_len = dlen;
dealloc(cthread->s_buf);
cthread->s_buf = c_buf;
cthread->c_type = CTYPE_BZIP2;
return 0;
}
static int gzip_compress_buf(rzip_control *control, struct compress_thread *cthread)
{
unsigned long dlen = round_up_page(control, cthread->s_len);
uchar *c_buf;
int gzip_ret;
c_buf = malloc(dlen);
if (!c_buf) {
print_err("Unable to allocate c_buf in gzip_compress_buf\n");
return -1;
}
gzip_ret = compress2(c_buf, &dlen, cthread->s_buf, cthread->s_len,
control->compression_level);
/* if compressed data is bigger then original data leave as
* CTYPE_NONE */
if (gzip_ret == Z_BUF_ERROR) {
print_maxverbose("Incompressible block\n");
/* Incompressible, leave as CTYPE_NONE */
dealloc(c_buf);
return 0;
}
if (unlikely(gzip_ret != Z_OK)) {
dealloc(c_buf);
print_maxverbose("compress2 failed\n");
return -1;
}
if (unlikely((i64)dlen >= cthread->c_len)) {
print_maxverbose("Incompressible block\n");
/* Incompressible, leave as CTYPE_NONE */
dealloc(c_buf);
return 0;
}
cthread->c_len = dlen;
dealloc(cthread->s_buf);
cthread->s_buf = c_buf;
cthread->c_type = CTYPE_GZIP;
return 0;
}
static int lzma_compress_buf(rzip_control *control, struct compress_thread *cthread)
{
unsigned char lzma_properties[5]; /* lzma properties, encoded */
int lzma_level, lzma_ret;
size_t prop_size = 5; /* return value for lzma_properties */
uchar *c_buf;
size_t dlen;
if (!lz4_compresses(control, cthread->s_buf, cthread->s_len))
return 0;
/* only 7 levels with lzma, scale them */
lzma_level = control->compression_level * 7 / 9;
if (!lzma_level)
lzma_level = 1;
print_maxverbose("Starting lzma back end compression thread...\n");
retry:
dlen = round_up_page(control, cthread->s_len);
c_buf = malloc(dlen);
if (!c_buf) {
print_err("Unable to allocate c_buf in lzma_compress_buf\n");
return -1;
}
/* with LZMA SDK 4.63, we pass compression level and threads only
* and receive properties in lzma_properties */
lzma_ret = LzmaCompress(c_buf, &dlen, cthread->s_buf,
(size_t)cthread->s_len, lzma_properties, &prop_size,
lzma_level,
0, /* dict size. set default, choose by level */
-1, -1, -1, -1, /* lc, lp, pb, fb */
control->threads > 1 ? 2: 1);
/* LZMA spec has threads = 1 or 2 only. */
if (lzma_ret != SZ_OK) {
switch (lzma_ret) {
case SZ_ERROR_MEM:
break;
case SZ_ERROR_PARAM:
print_err("LZMA Parameter ERROR: %d. This should not happen.\n", SZ_ERROR_PARAM);
break;
case SZ_ERROR_OUTPUT_EOF:
print_maxverbose("Harmless LZMA Output Buffer Overflow error: %d. Incompressible block.\n", SZ_ERROR_OUTPUT_EOF);
break;
case SZ_ERROR_THREAD:
print_err("LZMA Multi Thread ERROR: %d. This should not happen.\n", SZ_ERROR_THREAD);
break;
default:
print_err("Unidentified LZMA ERROR: %d. This should not happen.\n", lzma_ret);
break;
}
/* can pass -1 if not compressible! Thanks Lasse Collin */
dealloc(c_buf);
if (lzma_ret == SZ_ERROR_MEM) {
if (lzma_level > 1) {
lzma_level--;
print_verbose("LZMA Warning: %d. Can't allocate enough RAM for compression window, trying smaller.\n", SZ_ERROR_MEM);
goto retry;
}
/* lzma compress can be fragile on 32 bit. If it fails,
* fall back to bzip2 compression so the block doesn't
* remain uncompressed */
print_verbose("Unable to allocate enough RAM for any sized compression window, falling back to bzip2 compression.\n");
return bzip2_compress_buf(control, cthread);
} else if (lzma_ret == SZ_ERROR_OUTPUT_EOF)
return 0;
return -1;
}
if (unlikely((i64)dlen >= cthread->c_len)) {
/* Incompressible, leave as CTYPE_NONE */
print_maxverbose("Incompressible block\n");
dealloc(c_buf);
return 0;
}
/* Make sure multiple threads don't race on writing lzma_properties */
lock_mutex(control, &control->control_lock);
if (!control->lzma_prop_set) {
memcpy(control->lzma_properties, lzma_properties, 5);
control->lzma_prop_set = true;
/* Reset the magic written flag so we write it again if we
* get lzma properties and haven't written them yet. */
if (TMP_OUTBUF)
control->magic_written = 0;
}
unlock_mutex(control, &control->control_lock);
cthread->c_len = dlen;
dealloc(cthread->s_buf);
cthread->s_buf = c_buf;
cthread->c_type = CTYPE_LZMA;
return 0;
}
static int lzo_compress_buf(rzip_control *control, struct compress_thread *cthread)
{
lzo_uint in_len = cthread->s_len;
lzo_uint dlen = round_up_page(control, in_len + in_len / 16 + 64 + 3);
lzo_bytep wrkmem;
uchar *c_buf;
int ret = -1;
wrkmem = (lzo_bytep) calloc(1, LZO1X_1_MEM_COMPRESS);
if (unlikely(wrkmem == NULL)) {
print_maxverbose("Failed to malloc wkmem\n");
return ret;
}
c_buf = malloc(dlen);
if (!c_buf) {
print_err("Unable to allocate c_buf in lzo_compress_buf");
goto out_free;
}
/* lzo1x_1_compress does not return anything but LZO_OK so we ignore
* the return value */
lzo1x_1_compress(cthread->s_buf, in_len, c_buf, &dlen, wrkmem);
ret = 0;
if (dlen >= in_len){
/* Incompressible, leave as CTYPE_NONE */
print_maxverbose("Incompressible block\n");
dealloc(c_buf);
goto out_free;
}
cthread->c_len = dlen;
dealloc(cthread->s_buf);
cthread->s_buf = c_buf;
cthread->c_type = CTYPE_LZO;
out_free:
dealloc(wrkmem);
return ret;
}
/*
***** DECOMPRESSION FUNCTIONS *****
ZPAQ, BZIP, GZIP, LZMA, LZO
try to decompress a buffer. Return 0 on success and -1 on failure.
*/
static int zpaq_decompress_buf(rzip_control *control __UNUSED__, struct uncomp_thread *ucthread, long thread)
{
i64 dlen = ucthread->u_len;
uchar *c_buf;
int ret = 0;
c_buf = ucthread->s_buf;
ucthread->s_buf = malloc(round_up_page(control, dlen));
if (unlikely(!ucthread->s_buf)) {
print_err("Failed to allocate %ld bytes for decompression\n", dlen);
ret = -1;
goto out;
}
dlen = 0;
zpaq_decompress(ucthread->s_buf, &dlen, c_buf, ucthread->c_len,
control->msgout, SHOW_PROGRESS ? true: false, thread);
if (unlikely(dlen != ucthread->u_len)) {
print_err("Inconsistent length after decompression. Got %ld bytes, expected %lld\n", dlen, ucthread->u_len);
ret = -1;
} else
dealloc(c_buf);
out:
if (ret == -1) {
dealloc(ucthread->s_buf);
ucthread->s_buf = c_buf;
}
return ret;
}
static int bzip2_decompress_buf(rzip_control *control __UNUSED__, struct uncomp_thread *ucthread)
{
u32 dlen = ucthread->u_len;
int ret = 0, bzerr;
uchar *c_buf;
c_buf = ucthread->s_buf;
ucthread->s_buf = malloc(round_up_page(control, dlen));
if (unlikely(!ucthread->s_buf)) {
print_err("Failed to allocate %d bytes for decompression\n", dlen);
ret = -1;
goto out;
}
bzerr = BZ2_bzBuffToBuffDecompress((char*)ucthread->s_buf, &dlen, (char*)c_buf, ucthread->c_len, 0, 0);
if (unlikely(bzerr != BZ_OK)) {
print_err("Failed to decompress buffer - bzerr=%d\n", bzerr);
ret = -1;
goto out;
}
if (unlikely(dlen != ucthread->u_len)) {
print_err("Inconsistent length after decompression. Got %d bytes, expected %lld\n", dlen, ucthread->u_len);
ret = -1;
} else
dealloc(c_buf);
out:
if (ret == -1) {
dealloc(ucthread->s_buf);
ucthread->s_buf = c_buf;
}
return ret;
}
static int gzip_decompress_buf(rzip_control *control __UNUSED__, struct uncomp_thread *ucthread)
{
unsigned long dlen = ucthread->u_len;
int ret = 0, gzerr;
uchar *c_buf;
c_buf = ucthread->s_buf;
ucthread->s_buf = malloc(round_up_page(control, dlen));
if (unlikely(!ucthread->s_buf)) {
print_err("Failed to allocate %ld bytes for decompression\n", dlen);
ret = -1;
goto out;
}
gzerr = uncompress(ucthread->s_buf, &dlen, c_buf, ucthread->c_len);
if (unlikely(gzerr != Z_OK)) {
print_err("Failed to decompress buffer - gzerr=%d\n", gzerr);
ret = -1;
goto out;
}
if (unlikely((i64)dlen != ucthread->u_len)) {
print_err("Inconsistent length after decompression. Got %ld bytes, expected %lld\n", dlen, ucthread->u_len);
ret = -1;
} else
dealloc(c_buf);
out:
if (ret == -1) {
dealloc(ucthread->s_buf);
ucthread->s_buf = c_buf;
}
return ret;
}
static int lzma_decompress_buf(rzip_control *control, struct uncomp_thread *ucthread)
{
size_t dlen = ucthread->u_len;
int ret = 0, lzmaerr;
uchar *c_buf;
SizeT c_len = ucthread->c_len;
c_buf = ucthread->s_buf;
ucthread->s_buf = malloc(round_up_page(control, dlen));
if (unlikely(!ucthread->s_buf)) {
print_err("Failed to allocate %lld bytes for decompression\n", (i64)dlen);
ret = -1;
goto out;
}
/* With LZMA SDK 4.63 we pass control->lzma_properties
* which is needed for proper uncompress */
lzmaerr = LzmaUncompress(ucthread->s_buf, &dlen, c_buf, &c_len, control->lzma_properties, 5);
if (unlikely(lzmaerr)) {
print_err("Failed to decompress buffer - lzmaerr=%d\n", lzmaerr);
ret = -1;
goto out;
}
if (unlikely((i64)dlen != ucthread->u_len)) {
print_err("Inconsistent length after decompression. Got %lld bytes, expected %lld\n", (i64)dlen, ucthread->u_len);
ret = -1;
} else
dealloc(c_buf);
out:
if (ret == -1) {
dealloc(ucthread->s_buf);
ucthread->s_buf = c_buf;
}
return ret;
}
static int lzo_decompress_buf(rzip_control *control __UNUSED__, struct uncomp_thread *ucthread)
{
lzo_uint dlen = ucthread->u_len;
int ret = 0, lzerr;
uchar *c_buf;
c_buf = ucthread->s_buf;
ucthread->s_buf = malloc(round_up_page(control, dlen));
if (unlikely(!ucthread->s_buf)) {
print_err("Failed to allocate %lu bytes for decompression\n", (unsigned long)dlen);
ret = -1;
goto out;
}
lzerr = lzo1x_decompress_safe((uchar*)c_buf, ucthread->c_len, (uchar*)ucthread->s_buf, &dlen, NULL);
if (unlikely(lzerr != LZO_E_OK)) {
print_err("Failed to decompress buffer - lzerr=%d\n", lzerr);
ret = -1;
goto out;
}
if (unlikely((i64)dlen != ucthread->u_len)) {
print_err("Inconsistent length after decompression. Got %lu bytes, expected %lld\n", (unsigned long)dlen, ucthread->u_len);
ret = -1;
} else
dealloc(c_buf);
out:
if (ret == -1) {
dealloc(ucthread->s_buf);
ucthread->s_buf = c_buf;
}
return ret;
}
/* WORK FUNCTIONS */
/* Look at whether we're writing to a ram location or physical files and write
* the data accordingly. */
ssize_t put_fdout(rzip_control *control, void *offset_buf, ssize_t ret)
{
if (!TMP_OUTBUF)
return write(control->fd_out, offset_buf, (size_t)ret);
if (unlikely(control->out_ofs + ret > control->out_maxlen)) {
/* The data won't fit in a temporary output buffer so we have
* to fall back to temporary files. */
print_verbose("Unable to %scompress entirely in ram, will use physical files\n",
DECOMPRESS ? "de" : "");
if (unlikely(control->fd_out == -1)) {
failure("Was unable to %scompress entirely in ram and no temporary file creation was possible\n",
DECOMPRESS ? "de" : "");
}
/* Copy tmp_outbuf to tmpoutfile before deallocation */
if (unlikely(!write_fdout(control, control->tmp_outbuf, control->out_len))) {
print_err("Unable to write_fdout tmpoutbuf in put_fdout\n");
return -1;
}
/* Deallocate now unused tmpoutbuf and unset tmp_outbuf flag */
close_tmpoutbuf(control);
return write(control->fd_out, offset_buf, (size_t)ret);
}
memcpy(control->tmp_outbuf + control->out_ofs, offset_buf, ret);
control->out_ofs += ret;
if (likely(control->out_ofs > control->out_len))
control->out_len = control->out_ofs;
return ret;
}
/* This is a custom version of write() which writes in 1GB chunks to avoid
the overflows at the >= 2GB mark thanks to 32bit fuckage. */
ssize_t write_1g(rzip_control *control, void *buf, i64 len)
{
uchar *offset_buf = buf;
ssize_t ret;
i64 total;
total = 0;
while (len > 0) {
if (BITS32)
ret = MIN(len, one_g);
else
ret = len;
ret = put_fdout(control, offset_buf, (size_t)ret);
if (unlikely(ret <= 0))
return ret;
len -= ret;
offset_buf += ret;
total += ret;
}
return total;
}
/* Should be called only if we know the buffer will be large enough, otherwise
* we must dump_stdin first */
static bool read_fdin(struct rzip_control *control, i64 len)
{
int tmpchar;
i64 i;
for (i = 0; i < len; i++) {
tmpchar = getchar();
if (unlikely(tmpchar == EOF))
failure_return(("Reached end of file on STDIN prematurely on read_fdin, asked for %lld got %lld\n",
len, i), false);
control->tmp_inbuf[control->in_ofs + i] = (char)tmpchar;
}
control->in_len = control->in_ofs + len;
return true;
}
/* Dump STDIN into a temporary file */
static int dump_stdin(rzip_control *control)
{
if (unlikely(!write_fdin(control)))
return -1;
if (unlikely(!read_tmpinfile(control, control->fd_in)))
return -1;
close_tmpinbuf(control);
return 0;
}
/* Ditto for read */
ssize_t read_1g(rzip_control *control, int fd, void *buf, i64 len)
{
uchar *offset_buf = buf;
ssize_t ret;
i64 total;
if (TMP_INBUF && fd == control->fd_in) {
/* We're decompressing from STDIN */
if (unlikely(control->in_ofs + len > control->in_maxlen)) {
/* We're unable to fit it all into the temp buffer */
if (dump_stdin(control)) {
failure_return(("Inadequate ram to %scompress from STDIN and unable to create in tmpfile",
DECOMPRESS ? "de" : ""), -1);
}
goto read_fd;
}
if (control->in_ofs + len > control->in_len) {
if (unlikely(!read_fdin(control, control->in_ofs + len - control->in_len)))
return false;
}
memcpy(buf, control->tmp_inbuf + control->in_ofs, len);
control->in_ofs += len;
return len;
}
if (TMP_OUTBUF && fd == control->fd_out) {
if (unlikely(control->out_ofs + len > control->out_maxlen))
failure_return(("Trying to read beyond out_ofs in tmpoutbuf\n"), -1);
memcpy(buf, control->tmp_outbuf + control->out_ofs, len);
control->out_ofs += len;
return len;
}
read_fd:
total = 0;
while (len > 0) {
if (BITS32)
ret = MIN(len, one_g);
else
ret = len;
ret = read(fd, offset_buf, (size_t)ret);
if (unlikely(ret <= 0))
return ret;
len -= ret;
offset_buf += ret;
total += ret;
}
return total;
}
/* write to a file, return 0 on success and -1 on failure */
static int write_buf(rzip_control *control, uchar *p, i64 len)
{
ssize_t ret;
ret = write_1g(control, p, (size_t)len);
if (unlikely(ret == -1)) {
print_err("Write of length %lld failed - %s\n", len, strerror(errno));
return -1;
}
if (unlikely(ret != (ssize_t)len)) {
print_err("Partial write!? asked for %lld bytes but got %lld\n", len, (i64)ret);
return -1;
}
return 0;
}
/* write a byte */
static inline int write_u8(rzip_control *control, uchar v)
{
return write_buf(control, &v, 1);
}
static inline int write_val(rzip_control *control, i64 v, int len)
{
v = htole64(v);
return write_buf(control, (uchar *)&v, len);
}
static int read_buf(rzip_control *control, int f, uchar *p, i64 len)
{
ssize_t ret;
ret = read_1g(control, f, p, (size_t)len);
if (unlikely(ret == -1)) {
print_err("Read of length %lld failed - %s\n", len, strerror(errno));
return -1;
}
if (unlikely(ret != (ssize_t)len)) {
print_err("Partial read!? asked for %lld bytes but got %lld\n", len, (i64)ret);
return -1;
}
return 0;
}
static inline int read_u8(rzip_control *control, int f, uchar *v)
{
return read_buf(control, f, v, 1);
}
static inline int read_u32(rzip_control *control, int f, u32 *v)
{
int ret = read_buf(control, f, (uchar *)v, 4);
*v = le32toh(*v);
return ret;
}
static inline int read_val(rzip_control *control, int f, i64 *v, int len)
{
int ret;
/* We only partially read all 8 bytes so have to zero v here */
*v = 0;
ret = read_buf(control, f, (uchar *)v, len);
return ret;
}
static int fd_seekto(rzip_control *control, struct stream_info *sinfo, i64 spos, i64 pos)
{
if (unlikely(lseek(sinfo->fd, spos, SEEK_SET) != spos)) {
print_err("Failed to seek to %lld in stream\n", pos);
return -1;
}
return 0;
}
/* seek to a position within a set of streams - return -1 on failure */
static int seekto(rzip_control *control, struct stream_info *sinfo, i64 pos)
{
i64 spos = pos + sinfo->initial_pos;
if (TMP_OUTBUF) {
spos -= control->out_relofs;
control->out_ofs = spos;
if (unlikely(spos > control->out_len || spos < 0)) {
print_err("Trying to seek to %lld outside tmp outbuf in seekto\n", spos);
return -1;
}
return 0;
}
return fd_seekto(control, sinfo, spos, pos);
}
static int read_seekto(rzip_control *control, struct stream_info *sinfo, i64 pos)
{
i64 spos = pos + sinfo->initial_pos;
if (TMP_INBUF) {
if (spos > control->in_len) {
i64 len = spos - control->in_len;
if (control->in_ofs + len > control->in_maxlen) {
if (unlikely(dump_stdin(control)))
return -1;
goto fd_seek;
} else {
if (unlikely(!read_fdin(control, len)))
return -1;
}
}
control->in_ofs = spos;
if (unlikely(spos < 0)) {
print_err("Trying to seek to %lld outside tmp inbuf in read_seekto\n", spos);
return -1;
}
return 0;
}
fd_seek:
return fd_seekto(control, sinfo, spos, pos);
}
static i64 get_seek(rzip_control *control, int fd)
{
i64 ret;
if (TMP_OUTBUF)
return control->out_relofs + control->out_ofs;
ret = lseek(fd, 0, SEEK_CUR);
if (unlikely(ret == -1))
fatal_return(("Failed to lseek in get_seek\n"), -1);
return ret;
}
i64 get_readseek(rzip_control *control, int fd)
{
i64 ret;
if (TMP_INBUF)
return control->in_ofs;
ret = lseek(fd, 0, SEEK_CUR);
if (unlikely(ret == -1))
fatal_return(("Failed to lseek in get_seek\n"), -1);
return ret;
}
bool prepare_streamout_threads(rzip_control *control)
{
pthread_t *threads;
int i;
/* As we serialise the generation of threads during the rzip
* pre-processing stage, it's faster to have one more thread available
* to keep all CPUs busy. There is no point splitting up the chunks
* into multiple threads if there will be no compression back end. */
if (control->threads > 1)
++control->threads;
if (NO_COMPRESS)
control->threads = 1;
threads = control->pthreads = calloc(sizeof(pthread_t), control->threads);
if (unlikely(!threads))
fatal_return(("Unable to calloc threads in prepare_streamout_threads\n"), false);
cthreads = calloc(sizeof(struct compress_thread), control->threads);
if (unlikely(!cthreads)) {
dealloc(threads);
fatal_return(("Unable to calloc cthreads in prepare_streamout_threads\n"), false);
}
for (i = 0; i < control->threads; i++) {
cksem_init(control, &cthreads[i].cksem);
cksem_post(control, &cthreads[i].cksem);
}
return true;
}
bool close_streamout_threads(rzip_control *control)
{
int i, close_thread = output_thread;
/* Wait for the threads in the correct order in case they end up
* serialised */
for (i = 0; i < control->threads; i++) {
cksem_wait(control, &cthreads[close_thread].cksem);
if (++close_thread == control->threads)
close_thread = 0;
}
dealloc(cthreads);
dealloc(control->pthreads);
return true;
}
/* open a set of output streams, compressing with the given
compression level and algorithm */
void *open_stream_out(rzip_control *control, int f, unsigned int n, i64 chunk_limit, char cbytes)
{
struct stream_info *sinfo;
unsigned int i, testbufs;
bool threadlimit = false;
i64 testsize, limit;
uchar *testmalloc;
sinfo = calloc(sizeof(struct stream_info), 1);
if (unlikely(!sinfo))
return NULL;
if (chunk_limit < control->page_size)
chunk_limit = control->page_size;
sinfo->bufsize = sinfo->size = limit = chunk_limit;
sinfo->chunk_bytes = cbytes;
sinfo->num_streams = n;
sinfo->fd = f;
sinfo->s = calloc(sizeof(struct stream), n);
if (unlikely(!sinfo->s)) {
dealloc(sinfo);
return NULL;
}
/* Find the largest we can make the window based on ability to malloc
* ram. We need 2 buffers for each compression thread and the overhead
* of each compression back end. No 2nd buf is required when there is
* no back end compression. We limit the total regardless to 1/3 ram
* for when the OS lies due to heavy overcommit. */
if (NO_COMPRESS)
testbufs = 1;
else
testbufs = 2;
testsize = (limit * testbufs) + (control->overhead * control->threads);
if (testsize > control->usable_ram)
limit = (control->usable_ram - (control->overhead * control->threads)) / testbufs;
/* If we don't have enough ram for the number of threads, decrease the
* number of threads till we do, or only have one thread. */
while (limit < STREAM_BUFSIZE && limit < chunk_limit) {
if (control->threads > 1) {
--control->threads;
threadlimit = true;
} else
break;
limit = (control->usable_ram - (control->overhead * control->threads)) / testbufs;
limit = MIN(limit, chunk_limit);
}
if (threadlimit) {
print_output("Minimising number of threads to %d to limit memory usage\n",
control->threads);
}
if (BITS32) {
limit = MIN(limit, one_g);
if (limit + (control->overhead * control->threads) > one_g)
limit = one_g - (control->overhead * control->threads);
}
/* Use a nominal minimum size should we fail all previous shrinking */
if (limit < STREAM_BUFSIZE) {
limit = MAX(limit, STREAM_BUFSIZE);
print_output("Warning, low memory for chosen compression settings\n");
}