2011-12-08 20:37:27 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 Andrea Mazzoleni
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
2015-01-02 18:52:04 +01:00
|
|
|
#include "support.h"
|
2011-12-08 20:37:27 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
#include "elem.h"
|
|
|
|
|
#include "state.h"
|
|
|
|
|
#include "parity.h"
|
|
|
|
|
#include "handle.h"
|
|
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
/* dup */
|
|
|
|
|
|
|
|
|
|
struct snapraid_hash {
|
|
|
|
|
struct snapraid_disk* disk; /**< Disk. */
|
|
|
|
|
struct snapraid_file* file; /**< File. */
|
2016-04-07 21:39:02 +02:00
|
|
|
unsigned char hash[HASH_MAX]; /**< Hash of the whole file. */
|
2011-12-08 20:37:27 +01:00
|
|
|
|
|
|
|
|
/* nodes for data structures */
|
2014-10-18 22:26:42 +02:00
|
|
|
tommy_hashdyn_node node;
|
2011-12-08 20:37:27 +01:00
|
|
|
};
|
|
|
|
|
|
2013-06-16 00:33:46 +02:00
|
|
|
struct snapraid_hash* hash_alloc(struct snapraid_state* state, struct snapraid_disk* disk, struct snapraid_file* file)
|
2011-12-08 20:37:27 +01:00
|
|
|
{
|
|
|
|
|
struct snapraid_hash* hash;
|
|
|
|
|
block_off_t i;
|
|
|
|
|
unsigned char* buf;
|
|
|
|
|
|
|
|
|
|
hash = malloc_nofail(sizeof(struct snapraid_hash));
|
|
|
|
|
hash->disk = disk;
|
|
|
|
|
hash->file = file;
|
|
|
|
|
|
2016-04-07 21:39:02 +02:00
|
|
|
buf = malloc_nofail(file->blockmax * BLOCK_HASH_SIZE);
|
2011-12-08 20:37:27 +01:00
|
|
|
|
|
|
|
|
/* set the back pointer */
|
2014-09-27 00:32:39 +02:00
|
|
|
for (i = 0; i < file->blockmax; ++i) {
|
2015-10-04 22:02:05 +02:00
|
|
|
struct snapraid_block* block = fs_file2block_get(file, i);
|
2011-12-08 20:37:27 +01:00
|
|
|
|
2016-04-07 21:39:02 +02:00
|
|
|
memcpy(buf + i * BLOCK_HASH_SIZE, block->hash, BLOCK_HASH_SIZE);
|
2015-10-01 18:19:48 +02:00
|
|
|
|
|
|
|
|
if (!block_has_updated_hash(block)) {
|
2011-12-08 20:37:27 +01:00
|
|
|
free(buf);
|
2014-05-09 22:07:07 +02:00
|
|
|
free(hash);
|
2011-12-08 20:37:27 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 21:39:02 +02:00
|
|
|
memhash(state->besthash, state->hashseed, hash->hash, buf, file->blockmax * BLOCK_HASH_SIZE);
|
2011-12-08 20:37:27 +01:00
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline tommy_uint32_t hash_hash(struct snapraid_hash* hash)
|
|
|
|
|
{
|
2016-04-07 21:39:02 +02:00
|
|
|
return tommy_hash_u32(0, hash->hash, HASH_MAX);
|
2011-12-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hash_free(struct snapraid_hash* hash)
|
|
|
|
|
{
|
|
|
|
|
free(hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int hash_compare(const void* void_arg, const void* void_data)
|
|
|
|
|
{
|
|
|
|
|
const char* arg = void_arg;
|
|
|
|
|
const struct snapraid_hash* hash = void_data;
|
2014-09-27 00:32:39 +02:00
|
|
|
|
2016-04-07 21:39:02 +02:00
|
|
|
return memcmp(arg, hash->hash, HASH_MAX);
|
2011-12-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void state_dup(struct snapraid_state* state)
|
|
|
|
|
{
|
|
|
|
|
tommy_hashdyn hashset;
|
|
|
|
|
tommy_node* i;
|
|
|
|
|
unsigned count;
|
|
|
|
|
data_off_t size;
|
2016-02-09 23:12:33 +01:00
|
|
|
char esc_buffer[ESC_MAX];
|
|
|
|
|
char esc_buffer_alt[ESC_MAX];
|
2011-12-08 20:37:27 +01:00
|
|
|
|
|
|
|
|
tommy_hashdyn_init(&hashset);
|
|
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
size = 0;
|
2013-09-16 18:52:10 +02:00
|
|
|
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_progress("Comparing...\n");
|
2013-10-19 13:24:35 +02:00
|
|
|
|
2011-12-08 20:37:27 +01:00
|
|
|
/* for each disk */
|
2014-09-27 00:32:39 +02:00
|
|
|
for (i = state->disklist; i != 0; i = i->next) {
|
2011-12-08 20:37:27 +01:00
|
|
|
tommy_node* j;
|
|
|
|
|
struct snapraid_disk* disk = i->data;
|
|
|
|
|
|
|
|
|
|
/* for each file */
|
2014-09-27 00:32:39 +02:00
|
|
|
for (j = disk->filelist; j != 0; j = j->next) {
|
2011-12-08 20:37:27 +01:00
|
|
|
struct snapraid_file* file = j->data;
|
2013-06-08 16:00:05 +02:00
|
|
|
struct snapraid_hash* hash;
|
2011-12-08 20:37:27 +01:00
|
|
|
tommy_hash_t hash32;
|
|
|
|
|
|
2013-06-04 23:24:50 +02:00
|
|
|
/* if empty, skip it */
|
2013-06-05 19:17:31 +02:00
|
|
|
if (file->size == 0)
|
2013-06-04 23:24:50 +02:00
|
|
|
continue;
|
|
|
|
|
|
2013-06-16 00:33:46 +02:00
|
|
|
hash = hash_alloc(state, disk, file);
|
2013-06-08 16:00:05 +02:00
|
|
|
|
|
|
|
|
/* if no hash, skip it */
|
|
|
|
|
if (!hash)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-12-08 20:37:27 +01:00
|
|
|
hash32 = hash_hash(hash);
|
|
|
|
|
|
2015-10-08 22:19:52 +02:00
|
|
|
struct snapraid_hash* found = tommy_hashdyn_search(&hashset, hash_compare, hash->hash, hash32);
|
|
|
|
|
if (found) {
|
2011-12-08 20:37:27 +01:00
|
|
|
++count;
|
2015-10-08 22:19:52 +02:00
|
|
|
size += found->file->size;
|
2016-05-29 18:19:13 +02:00
|
|
|
log_tag("dup:%s:%s:%s:%s:%" PRIu64 ": dup\n", disk->name, esc_tag(file->sub, esc_buffer), found->disk->name, esc_tag(found->file->sub, esc_buffer_alt), found->file->size);
|
2016-05-29 19:23:42 +02:00
|
|
|
printf("%12" PRIu64 " %s = %s\n", file->size, fmt_term(disk, file->sub, esc_buffer), fmt_term(found->disk, found->file->sub, esc_buffer_alt));
|
2011-12-08 20:37:27 +01:00
|
|
|
hash_free(hash);
|
|
|
|
|
} else {
|
2014-10-18 22:26:42 +02:00
|
|
|
tommy_hashdyn_insert(&hashset, &hash->node, hash, hash32);
|
2011-12-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-18 22:26:42 +02:00
|
|
|
tommy_hashdyn_foreach(&hashset, (tommy_foreach_func*)hash_free);
|
2011-12-08 20:37:27 +01:00
|
|
|
tommy_hashdyn_done(&hashset);
|
|
|
|
|
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_status("\n");
|
2015-03-29 22:18:21 +02:00
|
|
|
msg_status("%8u duplicates, for %" PRIu64 " GB\n", count, size / GIGA);
|
2013-09-30 22:24:30 +02:00
|
|
|
if (count)
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_status("There are duplicates!\n");
|
2013-09-30 22:24:30 +02:00
|
|
|
else
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_status("No duplicates\n");
|
2013-09-30 22:24:30 +02:00
|
|
|
|
2015-04-20 22:07:42 +02:00
|
|
|
log_tag("summary:dup_count:%u\n", count);
|
|
|
|
|
log_tag("summary:dup_size:%" PRIu64 "\n", size);
|
2013-10-03 22:44:33 +02:00
|
|
|
if (count == 0) {
|
2015-04-20 22:07:42 +02:00
|
|
|
log_tag("summary:exit:unique\n");
|
2013-10-03 22:44:33 +02:00
|
|
|
} else {
|
2015-04-20 22:07:42 +02:00
|
|
|
log_tag("summary:exit:dup\n");
|
2011-12-08 20:37:27 +01:00
|
|
|
}
|
2015-04-20 22:07:42 +02:00
|
|
|
log_flush();
|
2011-12-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
|