Esempio n. 1
0
static int	_build_and_dump(char *source_filepath, unsigned int blocksize)
{
    t_mt_file	mtf;
    t_mt		mt;
    int		ret;

    ret = EXIT_SUCCESS;
    if (mt_file_mmap(&mtf, source_filepath) != 0)
    {
        dprintf(2, "Error: failed to open '%s'\n", source_filepath);
        return (EXIT_FAILURE);
    }
    if (mt_tree_build(&mt, &mtf, blocksize) != 0)
    {
        dprintf(2, "Error: failed to build a merkle tree.\n");
        dprintf(2, "Possible causes are memory exhaustion, "
                "or an error while using the hashing function\n");
        ret = -1;
    }
    if (_dump_mt(&mt) != 0)
    {
        dprintf(2, "Error: failed to write completely the merkle tree in stdout\n");
        ret = EXIT_FAILURE;
    }
    mt_tree_free(&mt);
    if (mt_file_munmap(&mtf) != 0)
    {
        dprintf(2, "Error: failed to close '%s'\nData might be corrupted!!\n", source_filepath);
        return (EXIT_FAILURE);
    }
    return (ret);
}
Esempio n. 2
0
void
mt_hash_clear(MtHash* hash)
{
  assert(hash != NULL);

  MtHashElement* element = NULL;

  size_t i;
  for (i = 0; i < hash->size; ++i)
  {
    element = &hash->buckets[i];

    if (element->is_tree)
      mt_tree_free((MtTree*) element->data);

    if (element->data)
      mt_pair_free((MtPair*) element->data);

    element->is_tree = false;
    element->data = NULL;
  }

  hash->length = 0;
}