X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fwrite.c;h=b595c875e08e08e80313aa3bd2e363e8577df2d3;hb=80c2fe3e6463cfd0eca5bead23a08731b6db9576;hp=a71359420aed78ade13a05b0f2523c645974e1c2;hpb=a4204528d3fbd8b959025b897493cf68d5cbfc0f;p=wimlib diff --git a/src/write.c b/src/write.c index a7135942..b595c875 100644 --- a/src/write.c +++ b/src/write.c @@ -52,9 +52,7 @@ #include "wimlib/progress.h" #include "wimlib/resource.h" #include "wimlib/solid.h" -#ifdef __WIN32__ -# include "wimlib/win32.h" /* win32_rename_replacement() */ -#endif +#include "wimlib/win32.h" /* win32_rename_replacement() */ #include "wimlib/write.h" #include "wimlib/xml.h" @@ -279,7 +277,7 @@ struct write_blobs_progress_data { wimlib_progress_func_t progfunc; void *progctx; union wimlib_progress_info progress; - uint64_t next_progress; + u64 next_progress; }; static int @@ -292,7 +290,7 @@ do_write_blobs_progress(struct write_blobs_progress_data *progress_data, if (discarded) { progress->write_streams.total_bytes -= complete_size; progress->write_streams.total_streams -= complete_count; - if (progress_data->next_progress != ~(uint64_t)0 && + 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; @@ -302,8 +300,8 @@ do_write_blobs_progress(struct write_blobs_progress_data *progress_data, progress->write_streams.completed_streams += complete_count; } - if (progress->write_streams.completed_bytes >= progress_data->next_progress) - { + if (progress->write_streams.completed_bytes >= progress_data->next_progress) { + ret = call_progress(progress_data->progfunc, WIMLIB_PROGRESS_MSG_WRITE_STREAMS, progress, @@ -311,32 +309,9 @@ do_write_blobs_progress(struct write_blobs_progress_data *progress_data, if (ret) return ret; - if (progress_data->next_progress == progress->write_streams.total_bytes) { - progress_data->next_progress = ~(uint64_t)0; - } else { - /* Handle rate-limiting of messages */ - - /* Send new message as soon as another 1/128 of the - * total has been written. (Arbitrary number.) */ - progress_data->next_progress = - progress->write_streams.completed_bytes + - progress->write_streams.total_bytes / 128; - - /* ... Unless that would be more than 5000000 bytes, in - * which case send the next after the next 5000000 - * bytes. (Another arbitrary number.) */ - if (progress->write_streams.completed_bytes + 5000000 < - progress_data->next_progress) - progress_data->next_progress = - progress->write_streams.completed_bytes + 5000000; - - /* ... But always send a message as soon as we're - * completely done. */ - if (progress->write_streams.total_bytes < - progress_data->next_progress) - progress_data->next_progress = - progress->write_streams.total_bytes; - } + set_next_progress(progress->write_streams.completed_bytes, + progress->write_streams.total_bytes, + &progress_data->next_progress); } return 0; } @@ -573,9 +548,9 @@ end_chunk_table(struct write_blobs_ctx *ctx, u64 res_actual_size, hdr.chunk_size = cpu_to_le32(ctx->out_chunk_size); hdr.compression_format = cpu_to_le32(ctx->out_ctype); - BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1); - BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2); - BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3); + 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)); @@ -649,6 +624,8 @@ do_done_with_blob(struct blob_descriptor *blob, { int ret; struct wim_inode *inode; + tchar *cookie1; + tchar *cookie2; if (!blob->may_send_done_with_file) return 0; @@ -656,42 +633,18 @@ do_done_with_blob(struct blob_descriptor *blob, inode = blob->file_inode; wimlib_assert(inode != NULL); - wimlib_assert(inode->num_remaining_streams > 0); - if (--inode->num_remaining_streams > 0) + wimlib_assert(inode->i_num_remaining_streams > 0); + if (--inode->i_num_remaining_streams > 0) return 0; -#ifdef __WIN32__ - /* XXX: This logic really should be somewhere else. */ - - /* We want the path to the file, but blob->file_on_disk might actually - * refer to a named data stream. Temporarily strip the named data - * stream from the path. */ - wchar_t *p_colon = NULL; - wchar_t *p_question_mark = NULL; - const wchar_t *p_stream_name; - - p_stream_name = path_stream_name(blob->file_on_disk); - if (unlikely(p_stream_name)) { - p_colon = (wchar_t *)(p_stream_name - 1); - wimlib_assert(*p_colon == L':'); - *p_colon = L'\0'; - } - - /* We also should use a fake Win32 path instead of a NT path */ - if (!wcsncmp(blob->file_on_disk, L"\\??\\", 4)) { - p_question_mark = &blob->file_on_disk[1]; - *p_question_mark = L'\\'; - } -#endif + cookie1 = progress_get_streamless_path(blob->file_on_disk); + cookie2 = progress_get_win32_path(blob->file_on_disk); ret = done_with_file(blob->file_on_disk, progfunc, progctx); -#ifdef __WIN32__ - if (p_colon) - *p_colon = L':'; - if (p_question_mark) - *p_question_mark = L'?'; -#endif + progress_put_win32_path(cookie2); + progress_put_streamless_path(cookie1); + return ret; } @@ -797,7 +750,7 @@ write_blob_uncompressed(struct blob_descriptor *blob, struct filedes *out_fd) if (filedes_seek(out_fd, begin_offset) == -1) return 0; - ret = extract_full_blob_to_fd(blob, out_fd); + ret = extract_blob_to_fd(blob, out_fd); if (ret) { /* Error reading the uncompressed data. */ if (out_fd->offset == begin_offset && @@ -816,13 +769,9 @@ write_blob_uncompressed(struct blob_descriptor *blob, struct filedes *out_fd) wimlib_assert(out_fd->offset - begin_offset == blob->size); - if (out_fd->offset < end_offset && - 0 != ftruncate(out_fd->fd, out_fd->offset)) - { - ERROR_WITH_ERRNO("Can't truncate output file to " - "offset %"PRIu64, out_fd->offset); - return WIMLIB_ERR_WRITE; - } + /* 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 | @@ -1335,19 +1284,13 @@ finish_remaining_chunks(struct write_blobs_ctx *ctx) } static void -remove_empty_blobs(struct list_head *blob_list) +validate_blob_list(struct list_head *blob_list) { - struct blob_descriptor *blob, *tmp; + struct blob_descriptor *blob; - list_for_each_entry_safe(blob, tmp, blob_list, write_blobs_list) { + list_for_each_entry(blob, blob_list, write_blobs_list) { wimlib_assert(blob->will_be_in_output_wim); - if (blob->size == 0) { - list_del(&blob->write_blobs_list); - blob->out_reshdr.offset_in_wim = 0; - blob->out_reshdr.size_in_wim = 0; - blob->out_reshdr.uncompressed_size = 0; - blob->out_reshdr.flags = reshdr_flags_for_blob(blob); - } + wimlib_assert(blob->size != 0); } } @@ -1369,7 +1312,7 @@ init_done_with_file_info(struct list_head *blob_list) list_for_each_entry(blob, blob_list, write_blobs_list) { if (blob_is_in_file(blob)) { - blob->file_inode->num_remaining_streams = 0; + blob->file_inode->i_num_remaining_streams = 0; blob->may_send_done_with_file = 1; } else { blob->may_send_done_with_file = 0; @@ -1378,7 +1321,7 @@ init_done_with_file_info(struct list_head *blob_list) list_for_each_entry(blob, blob_list, write_blobs_list) if (blob->may_send_done_with_file) - blob->file_inode->num_remaining_streams++; + blob->file_inode->i_num_remaining_streams++; } /* @@ -1506,7 +1449,7 @@ write_blob_list(struct list_head *blob_list, (WRITE_RESOURCE_FLAG_SOLID | WRITE_RESOURCE_FLAG_PIPABLE)); - remove_empty_blobs(blob_list); + validate_blob_list(blob_list); if (list_empty(blob_list)) return 0; @@ -1617,13 +1560,11 @@ write_blob_list(struct list_head *blob_list, /* Read the list of blobs needing to be compressed, using the specified * callbacks to execute processing of the data. */ - struct read_blob_list_callbacks cbs = { - .begin_blob = write_blob_begin_read, - .begin_blob_ctx = &ctx, - .consume_chunk = write_blob_process_chunk, - .consume_chunk_ctx = &ctx, - .end_blob = write_blob_end_read, - .end_blob_ctx = &ctx, + struct read_blob_callbacks cbs = { + .begin_blob = write_blob_begin_read, + .consume_chunk = write_blob_process_chunk, + .end_blob = write_blob_end_read, + .ctx = &ctx, }; ret = read_blob_list(blob_list, @@ -1691,16 +1632,6 @@ write_file_data_blobs(WIMStruct *wim, write_resource_flags = write_flags_to_resource_flags(write_flags); - /* wimlib v1.7.0: create a solid WIM file by default if the WIM version - * has been set to WIM_VERSION_SOLID and at least one blob in the WIM's - * blob table is located in a solid resource (may be the same WIM, or a - * different one in the case of export). */ - if (wim->out_hdr.wim_version == WIM_VERSION_SOLID && - wim_has_solid_resources(wim)) - { - write_resource_flags |= WRITE_RESOURCE_FLAG_SOLID; - } - if (write_resource_flags & WRITE_RESOURCE_FLAG_SOLID) { out_chunk_size = wim->out_solid_chunk_size; out_ctype = wim->out_solid_compression_type; @@ -1759,6 +1690,13 @@ write_wim_resource_from_buffer(const void *buf, int ret; struct blob_descriptor blob; + if (unlikely(buf_size == 0)) { + zero_reshdr(out_reshdr); + if (hash_ret) + copy_hash(hash_ret, zero_hash); + return 0; + } + blob_set_is_located_in_attached_buffer(&blob, (void *)buf, buf_size); sha1_buffer(buf, buf_size, blob.hash); blob.unhashed = 0; @@ -2627,6 +2565,15 @@ write_pipable_wim(WIMStruct *wim, int image, int write_flags, * finish_write(). */ } +static bool +should_default_to_solid_compression(WIMStruct *wim, int write_flags) +{ + return wim->out_hdr.wim_version == WIM_VERSION_SOLID && + !(write_flags & (WIMLIB_WRITE_FLAG_SOLID | + WIMLIB_WRITE_FLAG_PIPABLE)) && + wim_has_solid_resources(wim); +} + /* Write a standalone WIM or split WIM (SWM) part to a new file or to a file * descriptor. */ int @@ -2714,6 +2661,12 @@ write_wim_part(WIMStruct *wim, else wim->out_hdr.wim_version = WIM_VERSION_DEFAULT; + /* Default to solid compression if it is valid in the chosen WIM file + * format and the WIMStruct references any solid resources. This is + * useful when exporting an image from a solid WIM. */ + if (should_default_to_solid_compression(wim, write_flags)) + write_flags |= WIMLIB_WRITE_FLAG_SOLID; + /* Set the header flags. */ wim->out_hdr.flags = (wim->hdr.flags & (WIM_HDR_FLAG_RP_FIX | WIM_HDR_FLAG_READONLY)); @@ -2741,9 +2694,9 @@ write_wim_part(WIMStruct *wim, if (write_flags & WIMLIB_WRITE_FLAG_RETAIN_GUID) guid = wim->hdr.guid; if (guid) - memcpy(wim->out_hdr.guid, guid, WIMLIB_GUID_LEN); + copy_guid(wim->out_hdr.guid, guid); else - randomize_byte_array(wim->out_hdr.guid, WIMLIB_GUID_LEN); + generate_guid(wim->out_hdr.guid); /* Set the part number and total parts. */ wim->out_hdr.part_number = part_number; @@ -2991,6 +2944,12 @@ overwrite_wim_inplace(WIMStruct *wim, int write_flags, unsigned num_threads) if (write_flags & WIMLIB_WRITE_FLAG_SOLID) wim->out_hdr.wim_version = WIM_VERSION_SOLID; + /* Default to solid compression if it is valid in the chosen WIM file + * format and the WIMStruct references any solid resources. This is + * useful when updating a solid WIM. */ + if (should_default_to_solid_compression(wim, write_flags)) + write_flags |= WIMLIB_WRITE_FLAG_SOLID; + /* Set additional flags for overwrite. */ write_flags |= WIMLIB_WRITE_FLAG_OVERWRITE | WIMLIB_WRITE_FLAG_STREAMS_OK;