|
| 1 | +// `sha256_traced` accepts a file system path and a tracer. |
| 2 | +// |
| 3 | +// If the path does not exist, print an error. |
| 4 | +// |
| 5 | +// If the path exists and is a regular file, print the SHA256 digest of the |
| 6 | +// file's contents. Produce a single tracing span indicating the calculation. |
| 7 | +// |
| 8 | +// If the path exists and is a directory, calculate the SHA256 digest of the |
| 9 | +// directory from the names and digests of its children, combined in some |
| 10 | +// canonical format. Produce a trace whose structure reflects the directory |
| 11 | +// structure. |
| 12 | +// |
| 13 | +// Files that are neither regular files nor directories are ignored. |
| 14 | + |
| 15 | +#include "hasher.h" |
| 16 | + |
| 17 | +#include <datadog/span_config.h> |
| 18 | +#include <datadog/tags.h> |
| 19 | +#include <datadog/tracer.h> |
| 20 | +#include <datadog/tracer_config.h> |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <array> |
| 24 | +#include <cstddef> |
| 25 | +#include <cstdint> |
| 26 | +#include <cstdio> |
| 27 | +#include <iostream> |
| 28 | +#include <string> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include "picosha2.h" |
| 32 | + |
| 33 | +namespace fs = std::filesystem; |
| 34 | +namespace dd = datadog::tracing; |
| 35 | + |
| 36 | +using Digest = std::array<char, picosha2::k_digest_size>; |
| 37 | + |
| 38 | +// Return the specified `digest` formatted as a lower case hexadecimal string. |
| 39 | +std::string hex(const Digest &digest) { |
| 40 | + std::string result; |
| 41 | + for (std::size_t i = 0; i < digest.size(); ++i) { |
| 42 | + char buf[2 + 1]; |
| 43 | + std::snprintf(buf, sizeof buf, "%02x", |
| 44 | + static_cast<unsigned char>(digest[i])); |
| 45 | + result.append(buf, 2); |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +// Store into the specified `digest` the SHA256 digest of the contents of the |
| 51 | +// specified `file`. Return zero on success, or a nonzero value if an error |
| 52 | +// occurs. |
| 53 | +int sha256(Digest &digest, const fs::path &file) { |
| 54 | + std::ifstream in(file); |
| 55 | + if (!in) { |
| 56 | + return 1; |
| 57 | + } |
| 58 | + picosha2::hash256(in, digest.begin(), digest.end()); |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +// Return the SHA256 digest of a directory having the specified `children`. |
| 63 | +// This function will sort `children` in place. |
| 64 | +Digest sha256(std::vector<std::pair<fs::path, Digest>> &children) { |
| 65 | + std::sort(children.begin(), children.end()); |
| 66 | + |
| 67 | + std::vector<char> descriptor; |
| 68 | + for (const auto &record : children) { |
| 69 | + const std::string path = record.first.filename().u8string(); |
| 70 | + const Digest &hash = record.second; |
| 71 | + descriptor.insert(descriptor.end(), path.begin(), path.end()); |
| 72 | + descriptor.insert(descriptor.end(), hash.begin(), hash.end()); |
| 73 | + } |
| 74 | + |
| 75 | + Digest digest; |
| 76 | + picosha2::hash256(descriptor, digest); |
| 77 | + return digest; |
| 78 | +} |
| 79 | + |
| 80 | +int sha256_traced(Digest &digest, const fs::path &path, |
| 81 | + const dd::Span &active_span) try { |
| 82 | + if (fs::is_directory(path)) { |
| 83 | + // Directory: Calculate hash of children, and then combine them. |
| 84 | + dd::SpanConfig config; |
| 85 | + config.name = "sha256.directory"; |
| 86 | + auto span = active_span.create_child(config); |
| 87 | + span.set_tag("path", path.u8string()); |
| 88 | + span.set_tag("file_name", path.u8string()); |
| 89 | + span.set_tag("directory_name", path.u8string()); |
| 90 | + |
| 91 | + std::vector<std::pair<fs::path, Digest>> children; |
| 92 | + const auto options = fs::directory_options::skip_permission_denied; |
| 93 | + for (const auto &entry : fs::directory_iterator(path, options)) { |
| 94 | + if (!(entry.is_regular_file() || entry.is_directory())) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + Digest hash; |
| 98 | + const fs::path &child = entry; |
| 99 | + if (sha256_traced(hash, child, span)) { |
| 100 | + span.set_error_message( |
| 101 | + "unable to calculate digest of " + child.u8string()); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + children.emplace_back(child, hash); |
| 105 | + } |
| 106 | + span.set_tag("number_of_children_included", |
| 107 | + std::to_string(children.size())); |
| 108 | + digest = sha256(children); |
| 109 | + span.set_tag("sha256_hex", hex(digest)); |
| 110 | + return 0; |
| 111 | + } else if (fs::is_regular_file(path)) { |
| 112 | + // Regular file: Calculate hash of file contents. |
| 113 | + dd::SpanConfig config; |
| 114 | + config.name = "sha256.file"; |
| 115 | + auto span = active_span.create_child(config); |
| 116 | + span.set_tag("path", path.u8string()); |
| 117 | + span.set_tag("file_name", path.u8string()); |
| 118 | + span.set_tag("file_size_bytes", std::to_string(fs::file_size(path))); |
| 119 | + const int rc = sha256(digest, path); |
| 120 | + if (rc) { |
| 121 | + span.set_error_message("Unable to calculate sha256 hash."); |
| 122 | + } else { |
| 123 | + span.set_tag("sha256_hex", hex(digest)); |
| 124 | + } |
| 125 | + return rc; |
| 126 | + } else { |
| 127 | + // Other kind of file (neither directory nor regular file): Ignore. |
| 128 | + return 1; |
| 129 | + } |
| 130 | +} catch (const fs::filesystem_error &) { |
| 131 | + return 1; |
| 132 | +} catch (const std::ios_base::failure &) { |
| 133 | + return 1; |
| 134 | +} |
| 135 | + |
| 136 | +void sha256_traced(const fs::path &path, dd::Tracer &tracer) { |
| 137 | + // Create a root span for the current request. |
| 138 | + dd::SpanConfig config; |
| 139 | + config.name = "sha256.request"; |
| 140 | + auto root = tracer.create_span(config); |
| 141 | + root.set_tag("path", path.u8string()); |
| 142 | + |
| 143 | + if (!fs::exists(path)) { |
| 144 | + root.set_error_message("The file does not exist."); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + Digest digest; |
| 149 | + if (sha256_traced(digest, path, root)) { |
| 150 | + root.set_error_message("Unable to calculate sha256 hash."); |
| 151 | + } else { |
| 152 | + const std::string hex_digest = hex(digest); |
| 153 | + root.set_tag("sha256_hex", hex_digest); |
| 154 | + } |
| 155 | +} |
0 commit comments