-
Notifications
You must be signed in to change notification settings - Fork 21
/
write.c
3367 lines (2931 loc) · 104 KB
/
write.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
/*
* write.c
*
* Support for writing WIM files; write a WIM file, overwrite a WIM file, write
* compressed file resources, etc.
*/
/*
* Copyright (C) 2012-2016 Eric Biggers
*
* This file is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
*
* This file 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file; if not, see https://www.gnu.org/licenses/.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK)
/* On BSD, this should be included before "wimlib/list.h" so that "wimlib/list.h" can
* override the LIST_HEAD macro. */
# include <sys/file.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "wimlib/alloca.h"
#include "wimlib/assert.h"
#include "wimlib/blob_table.h"
#include "wimlib/chunk_compressor.h"
#include "wimlib/endianness.h"
#include "wimlib/error.h"
#include "wimlib/file_io.h"
#include "wimlib/header.h"
#include "wimlib/inode.h"
#include "wimlib/integrity.h"
#include "wimlib/metadata.h"
#include "wimlib/paths.h"
#include "wimlib/progress.h"
#include "wimlib/resource.h"
#include "wimlib/solid.h"
#include "wimlib/win32.h" /* win32_rename_replacement() */
#include "wimlib/write.h"
#include "wimlib/xml.h"
/* wimlib internal flags used when writing resources. */
#define WRITE_RESOURCE_FLAG_RECOMPRESS 0x00000001
#define WRITE_RESOURCE_FLAG_PIPABLE 0x00000002
#define WRITE_RESOURCE_FLAG_SOLID 0x00000004
#define WRITE_RESOURCE_FLAG_SEND_DONE_WITH_FILE 0x00000008
#define WRITE_RESOURCE_FLAG_SOLID_SORT 0x00000010
static int
write_flags_to_resource_flags(int write_flags)
{
int write_resource_flags = 0;
if (write_flags & WIMLIB_WRITE_FLAG_RECOMPRESS)
write_resource_flags |= WRITE_RESOURCE_FLAG_RECOMPRESS;
if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
write_resource_flags |= WRITE_RESOURCE_FLAG_PIPABLE;
if (write_flags & WIMLIB_WRITE_FLAG_SOLID)
write_resource_flags |= WRITE_RESOURCE_FLAG_SOLID;
if (write_flags & WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES)
write_resource_flags |= WRITE_RESOURCE_FLAG_SEND_DONE_WITH_FILE;
if ((write_flags & (WIMLIB_WRITE_FLAG_SOLID |
WIMLIB_WRITE_FLAG_NO_SOLID_SORT)) ==
WIMLIB_WRITE_FLAG_SOLID)
write_resource_flags |= WRITE_RESOURCE_FLAG_SOLID_SORT;
return write_resource_flags;
}
struct filter_context {
int write_flags;
WIMStruct *wim;
};
/*
* Determine whether the specified blob should be filtered out from the write.
*
* Return values:
*
* < 0 : The blob should be hard-filtered; that is, not included in the output
* WIM file at all.
* 0 : The blob should not be filtered out.
* > 0 : The blob should be soft-filtered; that is, it already exists in the
* WIM file and may not need to be written again.
*/
static int
blob_filtered(const struct blob_descriptor *blob,
const struct filter_context *ctx)
{
int write_flags;
WIMStruct *wim;
if (ctx == NULL)
return 0;
write_flags = ctx->write_flags;
wim = ctx->wim;
if (write_flags & WIMLIB_WRITE_FLAG_APPEND &&
blob->blob_location == BLOB_IN_WIM &&
blob->rdesc->wim == wim)
return 1;
if (write_flags & WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS &&
blob->blob_location == BLOB_IN_WIM &&
blob->rdesc->wim != wim)
return -1;
return 0;
}
static bool
blob_hard_filtered(const struct blob_descriptor *blob,
struct filter_context *ctx)
{
return blob_filtered(blob, ctx) < 0;
}
static inline bool
may_soft_filter_blobs(const struct filter_context *ctx)
{
return ctx && (ctx->write_flags & WIMLIB_WRITE_FLAG_APPEND);
}
static inline bool
may_hard_filter_blobs(const struct filter_context *ctx)
{
return ctx && (ctx->write_flags & WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS);
}
static inline bool
may_filter_blobs(const struct filter_context *ctx)
{
return (may_soft_filter_blobs(ctx) || may_hard_filter_blobs(ctx));
}
/* Return true if the specified blob is located in a WIM resource which can be
* reused in the output WIM file, without being recompressed. */
static bool
can_raw_copy(const struct blob_descriptor *blob, int write_resource_flags,
int out_ctype, u32 out_chunk_size)
{
const struct wim_resource_descriptor *rdesc;
/* Recompress everything if requested. */
if (write_resource_flags & WRITE_RESOURCE_FLAG_RECOMPRESS)
return false;
/* A blob not located in a WIM resource cannot be reused. */
if (blob->blob_location != BLOB_IN_WIM)
return false;
rdesc = blob->rdesc;
/* In the case of an in-place compaction, always reuse resources located
* in the WIM being compacted. */
if (rdesc->wim->being_compacted)
return true;
/* Otherwise, only reuse compressed resources. */
if (out_ctype == WIMLIB_COMPRESSION_TYPE_NONE ||
!(rdesc->flags & (WIM_RESHDR_FLAG_COMPRESSED |
WIM_RESHDR_FLAG_SOLID)))
return false;
/* When writing a pipable WIM, we can only reuse pipable resources; and
* when writing a non-pipable WIM, we can only reuse non-pipable
* resources. */
if (rdesc->is_pipable !=
!!(write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE))
return false;
/* When writing a solid WIM, we can only reuse solid resources; and when
* writing a non-solid WIM, we can only reuse non-solid resources. */
if (!!(rdesc->flags & WIM_RESHDR_FLAG_SOLID) !=
!!(write_resource_flags & WRITE_RESOURCE_FLAG_SOLID))
return false;
/* Note: it is theoretically possible to copy chunks of compressed data
* between non-solid, solid, and pipable resources. However, we don't
* currently implement this optimization because it would be complex and
* would usually go unused. */
if (rdesc->flags & WIM_RESHDR_FLAG_COMPRESSED) {
/* To re-use a non-solid resource, it must use the desired
* compression type and chunk size. */
return (rdesc->compression_type == out_ctype &&
rdesc->chunk_size == out_chunk_size);
} else {
/* Solid resource: Such resources may contain multiple blobs,
* and in general only a subset of them need to be written. As
* a heuristic, re-use the raw data if more than two-thirds the
* uncompressed size is being written. */
/* Note: solid resources contain a header that specifies the
* compression type and chunk size; therefore we don't need to
* check if they are compatible with @out_ctype and
* @out_chunk_size. */
/* Did we already decide to reuse the resource? */
if (rdesc->raw_copy_ok)
return true;
struct blob_descriptor *res_blob;
u64 write_size = 0;
list_for_each_entry(res_blob, &rdesc->blob_list, rdesc_node)
if (res_blob->will_be_in_output_wim)
write_size += res_blob->size;
return (write_size > rdesc->uncompressed_size * 2 / 3);
}
}
static u32
reshdr_flags_for_blob(const struct blob_descriptor *blob)
{
u32 reshdr_flags = 0;
if (blob->is_metadata)
reshdr_flags |= WIM_RESHDR_FLAG_METADATA;
return reshdr_flags;
}
static void
blob_set_out_reshdr_for_reuse(struct blob_descriptor *blob)
{
const struct wim_resource_descriptor *rdesc;
wimlib_assert(blob->blob_location == BLOB_IN_WIM);
rdesc = blob->rdesc;
if (rdesc->flags & WIM_RESHDR_FLAG_SOLID) {
blob->out_reshdr.offset_in_wim = blob->offset_in_res;
blob->out_reshdr.uncompressed_size = 0;
blob->out_reshdr.size_in_wim = blob->size;
blob->out_res_offset_in_wim = rdesc->offset_in_wim;
blob->out_res_size_in_wim = rdesc->size_in_wim;
blob->out_res_uncompressed_size = rdesc->uncompressed_size;
} else {
blob->out_reshdr.offset_in_wim = rdesc->offset_in_wim;
blob->out_reshdr.uncompressed_size = rdesc->uncompressed_size;
blob->out_reshdr.size_in_wim = rdesc->size_in_wim;
}
blob->out_reshdr.flags = rdesc->flags;
}
/* Write the header for a blob in a pipable WIM. */
static int
write_pwm_blob_header(const struct blob_descriptor *blob,
struct filedes *out_fd, bool compressed)
{
struct pwm_blob_hdr blob_hdr;
u32 reshdr_flags;
int ret;
wimlib_assert(!blob->unhashed);
blob_hdr.magic = cpu_to_le64(PWM_BLOB_MAGIC);
blob_hdr.uncompressed_size = cpu_to_le64(blob->size);
copy_hash(blob_hdr.hash, blob->hash);
reshdr_flags = reshdr_flags_for_blob(blob);
if (compressed)
reshdr_flags |= WIM_RESHDR_FLAG_COMPRESSED;
blob_hdr.flags = cpu_to_le32(reshdr_flags);
ret = full_write(out_fd, &blob_hdr, sizeof(blob_hdr));
if (ret)
ERROR_WITH_ERRNO("Error writing blob header to WIM file");
return ret;
}
struct write_blobs_progress_data {
wimlib_progress_func_t progfunc;
void *progctx;
union wimlib_progress_info progress;
u64 next_progress;
};
static int
do_write_blobs_progress(struct write_blobs_progress_data *progress_data,
u64 complete_size, u64 complete_compressed_size,
u32 complete_count, bool discarded)
{
union wimlib_progress_info *progress = &progress_data->progress;
int ret;
if (discarded) {
progress->write_streams.total_bytes -= complete_size;
progress->write_streams.total_streams -= complete_count;
if (progress_data->next_progress != ~(u64)0 &&
progress_data->next_progress > progress->write_streams.total_bytes)
{
progress_data->next_progress = progress->write_streams.total_bytes;
}
} else {
progress->write_streams.completed_bytes += complete_size;
progress->write_streams.completed_compressed_bytes +=
complete_compressed_size;
progress->write_streams.completed_streams += complete_count;
}
if (progress->write_streams.completed_bytes >= progress_data->next_progress) {
ret = call_progress(progress_data->progfunc,
WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
progress,
progress_data->progctx);
if (ret)
return ret;
set_next_progress(progress->write_streams.completed_bytes,
progress->write_streams.total_bytes,
&progress_data->next_progress);
}
return 0;
}
struct write_blobs_ctx {
/* File descriptor to which the blobs are being written. */
struct filedes *out_fd;
/* Blob table for the WIMStruct on whose behalf the blobs are being
* written. */
struct blob_table *blob_table;
/* Compression format to use. */
int out_ctype;
/* Maximum uncompressed chunk size in compressed resources to use. */
u32 out_chunk_size;
/* Flags that affect how the blobs will be written. */
int write_resource_flags;
/* Data used for issuing WRITE_STREAMS progress. */
struct write_blobs_progress_data progress_data;
struct filter_context *filter_ctx;
/* Pointer to the chunk_compressor implementation being used for
* compressing chunks of data, or NULL if chunks are being written
* uncompressed. */
struct chunk_compressor *compressor;
/* A buffer of size @out_chunk_size that has been loaned out from the
* chunk compressor and is currently being filled with the uncompressed
* data of the next chunk. */
u8 *cur_chunk_buf;
/* Number of bytes in @cur_chunk_buf that are currently filled. */
size_t cur_chunk_buf_filled;
/* List of blobs that currently have chunks being compressed. */
struct list_head blobs_being_compressed;
/* List of blobs in the solid resource. Blobs are moved here after
* @blobs_being_compressed only when writing a solid resource. */
struct list_head blobs_in_solid_resource;
/* Current uncompressed offset in the blob being written. */
u64 cur_write_blob_offset;
/* Uncompressed size of resource currently being written. */
u64 cur_write_res_size;
/* Array that is filled in with compressed chunk sizes as a resource is
* being written. */
u64 *chunk_csizes;
/* Index of next entry in @chunk_csizes to fill in. */
size_t chunk_index;
/* Number of entries in @chunk_csizes currently allocated. */
size_t num_alloc_chunks;
/* Offset in the output file of the start of the chunks of the resource
* currently being written. */
u64 chunks_start_offset;
};
/* Reserve space for the chunk table and prepare to accumulate the chunk table
* in memory. */
static int
begin_chunk_table(struct write_blobs_ctx *ctx, u64 res_expected_size)
{
u64 expected_num_chunks;
u64 expected_num_chunk_entries;
size_t reserve_size;
int ret;
/* Calculate the number of chunks and chunk entries that should be
* needed for the resource. These normally will be the final values,
* but in SOLID mode some of the blobs we're planning to write into the
* resource may be duplicates, and therefore discarded, potentially
* decreasing the number of chunk entries needed. */
expected_num_chunks = DIV_ROUND_UP(res_expected_size, ctx->out_chunk_size);
expected_num_chunk_entries = expected_num_chunks;
if (!(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID))
expected_num_chunk_entries--;
/* Make sure the chunk_csizes array is long enough to store the
* compressed size of each chunk. */
if (expected_num_chunks > ctx->num_alloc_chunks) {
u64 new_length = expected_num_chunks + 50;
if ((size_t)new_length != new_length) {
ERROR("Resource size too large (%"PRIu64" bytes!",
res_expected_size);
return WIMLIB_ERR_NOMEM;
}
FREE(ctx->chunk_csizes);
ctx->chunk_csizes = MALLOC(new_length * sizeof(ctx->chunk_csizes[0]));
if (ctx->chunk_csizes == NULL) {
ctx->num_alloc_chunks = 0;
return WIMLIB_ERR_NOMEM;
}
ctx->num_alloc_chunks = new_length;
}
ctx->chunk_index = 0;
if (!(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE)) {
/* Reserve space for the chunk table in the output file. In the
* case of solid resources this reserves the upper bound for the
* needed space, not necessarily the exact space which will
* prove to be needed. At this point, we just use @chunk_csizes
* for a buffer of 0's because the actual compressed chunk sizes
* are unknown. */
reserve_size = expected_num_chunk_entries *
get_chunk_entry_size(res_expected_size,
0 != (ctx->write_resource_flags &
WRITE_RESOURCE_FLAG_SOLID));
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID)
reserve_size += sizeof(struct alt_chunk_table_header_disk);
memset(ctx->chunk_csizes, 0, reserve_size);
ret = full_write(ctx->out_fd, ctx->chunk_csizes, reserve_size);
if (ret) {
ERROR_WITH_ERRNO("Error reserving space for chunk "
"table in WIM file");
return ret;
}
}
return 0;
}
static int
begin_write_resource(struct write_blobs_ctx *ctx, u64 res_expected_size)
{
int ret;
wimlib_assert(res_expected_size != 0);
if (ctx->compressor != NULL) {
ret = begin_chunk_table(ctx, res_expected_size);
if (ret)
return ret;
}
/* Output file descriptor is now positioned at the offset at which to
* write the first chunk of the resource. */
ctx->chunks_start_offset = ctx->out_fd->offset;
ctx->cur_write_blob_offset = 0;
ctx->cur_write_res_size = res_expected_size;
return 0;
}
static int
end_chunk_table(struct write_blobs_ctx *ctx, u64 res_actual_size,
u64 *res_start_offset_ret, u64 *res_store_size_ret)
{
size_t actual_num_chunks;
size_t actual_num_chunk_entries;
size_t chunk_entry_size;
int ret;
actual_num_chunks = ctx->chunk_index;
actual_num_chunk_entries = actual_num_chunks;
if (!(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID))
actual_num_chunk_entries--;
chunk_entry_size = get_chunk_entry_size(res_actual_size,
0 != (ctx->write_resource_flags &
WRITE_RESOURCE_FLAG_SOLID));
typedef le64 __attribute__((may_alias)) aliased_le64_t;
typedef le32 __attribute__((may_alias)) aliased_le32_t;
if (chunk_entry_size == 4) {
aliased_le32_t *entries = (aliased_le32_t*)ctx->chunk_csizes;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) {
for (size_t i = 0; i < actual_num_chunk_entries; i++)
entries[i] = cpu_to_le32(ctx->chunk_csizes[i]);
} else {
u32 offset = ctx->chunk_csizes[0];
for (size_t i = 0; i < actual_num_chunk_entries; i++) {
u32 next_size = ctx->chunk_csizes[i + 1];
entries[i] = cpu_to_le32(offset);
offset += next_size;
}
}
} else {
aliased_le64_t *entries = (aliased_le64_t*)ctx->chunk_csizes;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) {
for (size_t i = 0; i < actual_num_chunk_entries; i++)
entries[i] = cpu_to_le64(ctx->chunk_csizes[i]);
} else {
u64 offset = ctx->chunk_csizes[0];
for (size_t i = 0; i < actual_num_chunk_entries; i++) {
u64 next_size = ctx->chunk_csizes[i + 1];
entries[i] = cpu_to_le64(offset);
offset += next_size;
}
}
}
size_t chunk_table_size = actual_num_chunk_entries * chunk_entry_size;
u64 res_start_offset;
u64 res_end_offset;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
ret = full_write(ctx->out_fd, ctx->chunk_csizes, chunk_table_size);
if (ret)
goto write_error;
res_end_offset = ctx->out_fd->offset;
res_start_offset = ctx->chunks_start_offset;
} else {
res_end_offset = ctx->out_fd->offset;
u64 chunk_table_offset;
chunk_table_offset = ctx->chunks_start_offset - chunk_table_size;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) {
struct alt_chunk_table_header_disk hdr;
hdr.res_usize = cpu_to_le64(res_actual_size);
hdr.chunk_size = cpu_to_le32(ctx->out_chunk_size);
hdr.compression_format = cpu_to_le32(ctx->out_ctype);
STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_XPRESS == 1);
STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_LZX == 2);
STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_LZMS == 3);
ret = full_pwrite(ctx->out_fd, &hdr, sizeof(hdr),
chunk_table_offset - sizeof(hdr));
if (ret)
goto write_error;
res_start_offset = chunk_table_offset - sizeof(hdr);
} else {
res_start_offset = chunk_table_offset;
}
ret = full_pwrite(ctx->out_fd, ctx->chunk_csizes,
chunk_table_size, chunk_table_offset);
if (ret)
goto write_error;
}
*res_start_offset_ret = res_start_offset;
*res_store_size_ret = res_end_offset - res_start_offset;
return 0;
write_error:
ERROR_WITH_ERRNO("Error writing chunk table to WIM file");
return ret;
}
/* Finish writing a WIM resource by writing or updating the chunk table (if not
* writing the data uncompressed) and loading its metadata into @out_reshdr. */
static int
end_write_resource(struct write_blobs_ctx *ctx, struct wim_reshdr *out_reshdr)
{
int ret;
u64 res_size_in_wim;
u64 res_uncompressed_size;
u64 res_offset_in_wim;
wimlib_assert(ctx->cur_write_blob_offset == ctx->cur_write_res_size ||
(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID));
res_uncompressed_size = ctx->cur_write_res_size;
if (ctx->compressor) {
ret = end_chunk_table(ctx, res_uncompressed_size,
&res_offset_in_wim, &res_size_in_wim);
if (ret)
return ret;
} else {
res_offset_in_wim = ctx->chunks_start_offset;
res_size_in_wim = ctx->out_fd->offset - res_offset_in_wim;
}
out_reshdr->uncompressed_size = res_uncompressed_size;
out_reshdr->size_in_wim = res_size_in_wim;
out_reshdr->offset_in_wim = res_offset_in_wim;
return 0;
}
/* Call when no more data from the file at @path is needed. */
static int
done_with_file(const tchar *path, wimlib_progress_func_t progfunc, void *progctx)
{
union wimlib_progress_info info;
info.done_with_file.path_to_file = path;
return call_progress(progfunc, WIMLIB_PROGRESS_MSG_DONE_WITH_FILE,
&info, progctx);
}
static int
do_done_with_blob(struct blob_descriptor *blob,
wimlib_progress_func_t progfunc, void *progctx)
{
int ret;
struct wim_inode *inode;
const tchar *path;
tchar *cookie1;
tchar *cookie2;
if (!blob->may_send_done_with_file)
return 0;
inode = blob->file_inode;
wimlib_assert(inode != NULL);
wimlib_assert(inode->i_num_remaining_streams > 0);
if (--inode->i_num_remaining_streams > 0)
return 0;
path = blob_file_path(blob);
cookie1 = progress_get_streamless_path(path);
cookie2 = progress_get_win32_path(path);
ret = done_with_file(path, progfunc, progctx);
progress_put_win32_path(cookie2);
progress_put_streamless_path(cookie1);
return ret;
}
/* Handle WIMLIB_WRITE_FLAG_SEND_DONE_WITH_FILE_MESSAGES mode. */
static inline int
done_with_blob(struct blob_descriptor *blob, struct write_blobs_ctx *ctx)
{
if (likely(!(ctx->write_resource_flags &
WRITE_RESOURCE_FLAG_SEND_DONE_WITH_FILE)))
return 0;
return do_done_with_blob(blob, ctx->progress_data.progfunc,
ctx->progress_data.progctx);
}
/* Begin processing a blob for writing. */
static int
write_blob_begin_read(struct blob_descriptor *blob, void *_ctx)
{
struct write_blobs_ctx *ctx = _ctx;
int ret;
wimlib_assert(blob->size > 0);
/* As an optimization, we allow some blobs to be "unhashed", meaning
* their SHA-1 message digests are unknown. This is the case with blobs
* that are added by scanning a directory tree with wimlib_add_image(),
* for example. Since WIM uses single-instance blobs, we don't know
* whether such each such blob really need to written until it is
* actually checksummed, unless it has a unique size. In such cases we
* read and checksum the blob in this function, thereby advancing ahead
* of read_blob_list(), which will still provide the data again to
* write_blob_process_chunk(). This is okay because an unhashed blob
* cannot be in a WIM resource, which might be costly to decompress. */
if (ctx->blob_table != NULL && blob->unhashed && !blob->unique_size) {
struct blob_descriptor *new_blob;
ret = hash_unhashed_blob(blob, ctx->blob_table, &new_blob);
if (ret)
return ret;
if (new_blob != blob) {
/* Duplicate blob detected. */
if (new_blob->will_be_in_output_wim ||
blob_filtered(new_blob, ctx->filter_ctx))
{
/* The duplicate blob is already being included
* in the output WIM, or it would be filtered
* out if it had been. Skip writing this blob
* (and reading it again) entirely, passing its
* output reference count to the duplicate blob
* in the former case. */
ret = do_write_blobs_progress(&ctx->progress_data,
blob->size,
blob->size,
1, true);
list_del(&blob->write_blobs_list);
list_del(&blob->blob_table_list);
if (new_blob->will_be_in_output_wim)
new_blob->out_refcnt += blob->out_refcnt;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID)
ctx->cur_write_res_size -= blob->size;
if (!ret)
ret = done_with_blob(blob, ctx);
free_blob_descriptor(blob);
if (ret)
return ret;
return BEGIN_BLOB_STATUS_SKIP_BLOB;
} else {
/* The duplicate blob can validly be written,
* but was not marked as such. Discard the
* current blob descriptor and use the
* duplicate, but actually freeing the current
* blob descriptor must wait until
* read_blob_list() has finished reading its
* data. */
list_replace(&blob->write_blobs_list,
&new_blob->write_blobs_list);
list_replace(&blob->blob_table_list,
&new_blob->blob_table_list);
blob->will_be_in_output_wim = 0;
new_blob->out_refcnt = blob->out_refcnt;
new_blob->will_be_in_output_wim = 1;
new_blob->may_send_done_with_file = 0;
blob = new_blob;
}
}
}
list_move_tail(&blob->write_blobs_list, &ctx->blobs_being_compressed);
return 0;
}
/* Rewrite a blob that was just written compressed (as a non-solid WIM resource)
* as uncompressed instead. */
static int
write_blob_uncompressed(struct blob_descriptor *blob, struct filedes *out_fd)
{
int ret;
u64 begin_offset = blob->out_reshdr.offset_in_wim;
u64 end_offset = out_fd->offset;
if (filedes_seek(out_fd, begin_offset) == -1)
return 0;
ret = extract_blob_to_fd(blob, out_fd, false);
if (ret) {
/* Error reading the uncompressed data. */
if (out_fd->offset == begin_offset &&
filedes_seek(out_fd, end_offset) != -1)
{
/* Nothing was actually written yet, and we successfully
* seeked to the end of the compressed resource, so
* don't issue a hard error; just keep the compressed
* resource instead. */
WARNING("Recovered compressed resource of "
"size %"PRIu64", continuing on.", blob->size);
return 0;
}
return ret;
}
wimlib_assert(out_fd->offset - begin_offset == blob->size);
/* We could ftruncate() the file to 'out_fd->offset' here, but there
* isn't much point. Usually we will only be truncating by a few bytes
* and will just overwrite the data immediately. */
blob->out_reshdr.size_in_wim = blob->size;
blob->out_reshdr.flags &= ~(WIM_RESHDR_FLAG_COMPRESSED |
WIM_RESHDR_FLAG_SOLID);
return 0;
}
/* Returns true if the specified blob, which was written as a non-solid
* resource, should be truncated from the WIM file and re-written uncompressed.
* blob->out_reshdr must be filled in from the initial write of the blob. */
static bool
should_rewrite_blob_uncompressed(const struct write_blobs_ctx *ctx,
const struct blob_descriptor *blob)
{
/* If the compressed data is smaller than the uncompressed data, prefer
* the compressed data. */
if (blob->out_reshdr.size_in_wim < blob->out_reshdr.uncompressed_size)
return false;
/* If we're not actually writing compressed data, then there's no need
* for re-writing. */
if (!ctx->compressor)
return false;
/* If writing a pipable WIM, everything we write to the output is final
* (it might actually be a pipe!). */
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE)
return false;
/* If the blob that would need to be re-read is located in a solid
* resource in another WIM file, then re-reading it would be costly. So
* don't do it.
*
* Exception: if the compressed size happens to be *exactly* the same as
* the uncompressed size, then the blob *must* be written uncompressed
* in order to remain compatible with the Windows Overlay Filesystem
* filter driver (WOF).
*
* TODO: we are currently assuming that the optimization for
* single-chunk resources in maybe_rewrite_blob_uncompressed() prevents
* this case from being triggered too often. To fully prevent excessive
* decompressions in degenerate cases, we really should obtain the
* uncompressed data by decompressing the compressed data we wrote to
* the output file.
*/
if (blob->blob_location == BLOB_IN_WIM &&
blob->size != blob->rdesc->uncompressed_size &&
blob->size != blob->out_reshdr.size_in_wim)
return false;
return true;
}
static int
maybe_rewrite_blob_uncompressed(struct write_blobs_ctx *ctx,
struct blob_descriptor *blob)
{
if (!should_rewrite_blob_uncompressed(ctx, blob))
return 0;
/* Regular (non-solid) WIM resources with exactly one chunk and
* compressed size equal to uncompressed size are exactly the same as
* the corresponding compressed data --- since there must be 0 entries
* in the chunk table and the only chunk must be stored uncompressed.
* In this case, there's no need to rewrite anything. */
if (ctx->chunk_index == 1 &&
blob->out_reshdr.size_in_wim == blob->out_reshdr.uncompressed_size)
{
blob->out_reshdr.flags &= ~WIM_RESHDR_FLAG_COMPRESSED;
return 0;
}
return write_blob_uncompressed(blob, ctx->out_fd);
}
/* Write the next chunk of (typically compressed) data to the output WIM,
* handling the writing of the chunk table. */
static int
write_chunk(struct write_blobs_ctx *ctx, const void *cchunk,
size_t csize, size_t usize)
{
int ret;
struct blob_descriptor *blob;
u32 completed_blob_count = 0;
blob = list_entry(ctx->blobs_being_compressed.next,
struct blob_descriptor, write_blobs_list);
if (ctx->cur_write_blob_offset == 0 &&
!(ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID))
{
/* Starting to write a new blob in non-solid mode. */
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
ret = write_pwm_blob_header(blob, ctx->out_fd,
ctx->compressor != NULL);
if (ret)
return ret;
}
ret = begin_write_resource(ctx, blob->size);
if (ret)
return ret;
}
if (ctx->compressor != NULL) {
/* Record the compresed chunk size. */
wimlib_assert(ctx->chunk_index < ctx->num_alloc_chunks);
ctx->chunk_csizes[ctx->chunk_index++] = csize;
/* If writing a pipable WIM, before the chunk data write a chunk
* header that provides the compressed chunk size. */
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PIPABLE) {
struct pwm_chunk_hdr chunk_hdr = {
.compressed_size = cpu_to_le32(csize),
};
ret = full_write(ctx->out_fd, &chunk_hdr,
sizeof(chunk_hdr));
if (ret)
goto write_error;
}
}
/* Write the chunk data. */
ret = full_write(ctx->out_fd, cchunk, csize);
if (ret)
goto write_error;
ctx->cur_write_blob_offset += usize;
if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) {
/* Wrote chunk in solid mode. It may have finished multiple
* blobs. */
struct blob_descriptor *next_blob;
while (blob && ctx->cur_write_blob_offset >= blob->size) {
ctx->cur_write_blob_offset -= blob->size;
if (ctx->cur_write_blob_offset)
next_blob = list_entry(blob->write_blobs_list.next,
struct blob_descriptor,
write_blobs_list);
else
next_blob = NULL;
ret = done_with_blob(blob, ctx);
if (ret)
return ret;
list_move_tail(&blob->write_blobs_list, &ctx->blobs_in_solid_resource);
completed_blob_count++;
blob = next_blob;
}
} else {
/* Wrote chunk in non-solid mode. It may have finished a
* blob. */
if (ctx->cur_write_blob_offset == blob->size) {
wimlib_assert(ctx->cur_write_blob_offset ==
ctx->cur_write_res_size);
ret = end_write_resource(ctx, &blob->out_reshdr);
if (ret)
return ret;
blob->out_reshdr.flags = reshdr_flags_for_blob(blob);
if (ctx->compressor != NULL)
blob->out_reshdr.flags |= WIM_RESHDR_FLAG_COMPRESSED;
ret = maybe_rewrite_blob_uncompressed(ctx, blob);
if (ret)
return ret;
wimlib_assert(blob->out_reshdr.uncompressed_size == blob->size);
ctx->cur_write_blob_offset = 0;
ret = done_with_blob(blob, ctx);
if (ret)
return ret;
list_del(&blob->write_blobs_list);
completed_blob_count++;
}
}
return do_write_blobs_progress(&ctx->progress_data, usize, csize,
completed_blob_count, false);
write_error:
ERROR_WITH_ERRNO("Error writing chunk data to WIM file");
return ret;
}
static int
prepare_chunk_buffer(struct write_blobs_ctx *ctx)
{
/* While we are unable to get a new chunk buffer due to too many chunks
* already outstanding, retrieve and write the next compressed chunk. */
while (!(ctx->cur_chunk_buf =
ctx->compressor->get_chunk_buffer(ctx->compressor)))
{
const void *cchunk;
u32 csize;
u32 usize;
bool bret;
int ret;
bret = ctx->compressor->get_compression_result(ctx->compressor,