+// 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.
+
+void
+Build_id_task_runner::run(Workqueue* workqueue, const Task*)
+{
+ Task_token* post_hash_tasks_blocker = new Task_token(true);
+ const Layout* layout = this->layout_;
+ Output_file* of = this->of_;
+ const size_t filesize = (layout->output_file_size() <= 0 ? 0
+ : static_cast<size_t>(layout->output_file_size()));
+ unsigned char* array_of_hashes = NULL;
+ size_t size_of_hashes = 0;
+
+ if (strcmp(this->options_->build_id(), "tree") == 0
+ && this->options_->build_id_chunk_size_for_treehash() > 0
+ && filesize > 0
+ && (filesize >= this->options_->build_id_min_file_size_for_treehash()))
+ {
+ static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
+ const size_t chunk_size =
+ this->options_->build_id_chunk_size_for_treehash();
+ const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
+ post_hash_tasks_blocker->add_blockers(num_hashes);
+ size_of_hashes = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
+ array_of_hashes = new unsigned char[size_of_hashes];
+ unsigned char *dst = array_of_hashes;
+ 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(of,
+ src_offset,
+ size,
+ dst,
+ post_hash_tasks_blocker));
+ }
+ }
+
+ // Queue the final task to write the build id and close the output file.
+ workqueue->queue(new Task_function(new Close_task_runner(this->options_,
+ layout,
+ of,
+ array_of_hashes,
+ size_of_hashes),
+ post_hash_tasks_blocker,
+ "Task_function Close_task_runner"));
+}
+