forked from kazel1990/silo_fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduptest.cpp
86 lines (82 loc) · 2.07 KB
/
duptest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <array>
#include <openssl/md5.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include "shtable.h"
#include "libsilo.h"
#include <errno.h>
#include <fstream>
#include <istream>
#include <iterator>
using namespace std;
char buf[65536];
shtable sht;
unsigned int num_chunk, num_dup;
unsigned long long size_dup, size_org;
void dedup(const std::string &d)
{
std::vector<size_t> chunked;
do_chunking(d.c_str(), d.size(), chunked);
num_chunk += chunked.size();
size_org += d.size();
size_t left = 0;
for(size_t c : chunked)
{
size_t sz = c - left;
std::array<unsigned char,16> hash;
MD5((unsigned char*)(d.c_str()+left),sz,&hash[0]);
left=c;
int bnum;
if (sht.find(bnum, hash))
{
num_dup++;
size_dup += sz;
}
else sht.insert(hash, 1);
}
}
void createdata(DIR* d, std::string addr)
{
struct dirent* e;
struct stat st;
while((e = readdir(d)) != NULL)
{
if(!strcmp(e->d_name,".") || !strcmp(e->d_name,"..")) continue;
sprintf(buf,"%s/%s",addr.c_str(),e->d_name);
lstat(buf, &st);
if( S_ISDIR(st.st_mode))
{
DIR* dc = opendir(buf);
createdata(dc, buf);
closedir(dc);
}
else if( S_ISREG(st.st_mode))
{
ifstream in(buf);
if (in.is_open() == false)
{
printf("%s fail\n", buf);
exit(1);
}
std::string tmp{istream_iterator<char>{in}, istream_iterator<char>{}};
dedup(tmp);
}
}
}
int main(int argc, char* argv[])
{
if (argc == 1) return 0;
DIR *f = opendir(argv[1]);
createdata(f, argv[1]);
closedir(f);
printf("number of chunks: %u\n", num_chunk);
printf("number of dup chunks: %u\n", num_dup);
printf("total memory if no dedup: %lluB\n", size_org);
printf("memory saved if optimal: %lluB\n", size_dup);
}