+// Build IDs can be computed as a "flat" sha1 or md5 of a string of bytes,
+// or as a "tree" where each chunk of the string is hashed and then those
+// hashes are put into a (much smaller) string which is hashed with sha1.
+// We compute a checksum over the entire file because that is simplest.
+
+Task_token*
+Layout::queue_build_id_tasks(Workqueue* workqueue, Task_token* build_id_blocker,
+ Output_file* of)
+{
+ const size_t filesize = (this->output_file_size() <= 0 ? 0
+ : static_cast<size_t>(this->output_file_size()));
+ if (this->build_id_note_ != NULL
+ && strcmp(parameters->options().build_id(), "tree") == 0
+ && parameters->options().build_id_chunk_size_for_treehash() > 0
+ && filesize > 0
+ && (filesize >=
+ parameters->options().build_id_min_file_size_for_treehash()))
+ {
+ static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
+ const size_t chunk_size =
+ parameters->options().build_id_chunk_size_for_treehash();
+ const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
+ Task_token* post_hash_tasks_blocker = new Task_token(true);
+ post_hash_tasks_blocker->add_blockers(num_hashes);
+ this->size_of_array_of_hashes_ = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
+ const unsigned char* src = of->get_input_view(0, filesize);
+ this->input_view_ = src;
+ unsigned char *dst = new unsigned char[this->size_of_array_of_hashes_];
+ this->array_of_hashes_ = dst;
+ for (size_t i = 0, src_offset = 0; i < num_hashes;
+ i++, dst += MD5_OUTPUT_SIZE_IN_BYTES, src_offset += chunk_size)
+ {
+ size_t size = std::min(chunk_size, filesize - src_offset);
+ workqueue->queue(new Hash_task(src + src_offset,
+ size,
+ dst,
+ build_id_blocker,
+ post_hash_tasks_blocker));
+ }
+ return post_hash_tasks_blocker;
+ }
+ return build_id_blocker;
+}
+
+// If a tree-style build ID was requested, the parallel part of that computation
+// is already done, and the final hash-of-hashes is computed here. For other
+// types of build IDs, all the work is done here.