2016-04-01 10:46:28 +03:00
|
|
|
// Copyright 2016-present Facebook. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// checksum.c: implementation for recalculating the checksums for
|
|
|
|
// intermediate nodes in a tree.
|
2016-04-01 21:19:53 +03:00
|
|
|
//
|
|
|
|
// no-check-code
|
2016-04-01 10:46:28 +03:00
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include "node.h"
|
|
|
|
#include "tree.h"
|
|
|
|
|
2016-04-09 08:33:07 +03:00
|
|
|
static void update_checksum(node_t *node) {
|
2016-04-01 10:46:28 +03:00
|
|
|
SHA_CTX ctx;
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
|
|
|
|
// find all the children and make sure their checksums are up-to-date.
|
2016-04-25 21:58:38 +03:00
|
|
|
for (child_num_t ix = 0; ix < node->num_children; ix++) {
|
2016-04-18 21:34:01 +03:00
|
|
|
node_t* child = get_child_by_index(node, ix);
|
2016-04-01 10:46:28 +03:00
|
|
|
if (child->checksum_valid == false) {
|
|
|
|
update_checksum(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA1_Update(&ctx, child->name, child->name_sz);
|
|
|
|
SHA1_Update(&ctx, child->checksum, child->checksum_sz);
|
|
|
|
SHA1_Update(&ctx, &child->flags, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA1_Final(node->checksum, &ctx);
|
|
|
|
node->checksum_sz = SHA1_BYTES;
|
|
|
|
node->checksum_valid = true;
|
|
|
|
}
|
|
|
|
|
2016-04-09 08:33:07 +03:00
|
|
|
void update_checksums(tree_t *tree) {
|
2016-04-01 10:46:28 +03:00
|
|
|
update_checksum(tree->shadow_root);
|
|
|
|
}
|