-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmpdir.cc
More file actions
150 lines (124 loc) · 2.7 KB
/
Copy pathcmpdir.cc
File metadata and controls
150 lines (124 loc) · 2.7 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#define _XOPEN_SOURCE 700
#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ftw.h>
#include <deque>
#include <unordered_set>
#include "hashes.h"
uint32_t fnvpath(const char *path, const struct stat *sb) {
char *p;
uint32_t q;
int fd;
fd = open(path, O_RDONLY);
if (!fd)
return 0;
p = (char *) mmap(0, sb->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
close(fd);
return 0;
}
q = FNV1A_32((const char *) p, sb->st_size);
munmap(p, sb->st_size);
close(fd);
return q;
}
/// ----
static int nFilesA, nFilesB;
static unsigned long nBytesA, nBytesB;
static std::unordered_set<uint32_t> hashesA;
static std::unordered_set<uint32_t> hashesB;
int cbA(const char *path, const struct stat *sb, int typeflag, struct FTW *ft) {
int rc;
uint32_t s;
switch (typeflag) {
case FTW_F: {
if ((sb->st_mode & S_IFMT) != S_IFREG)
break;
if (sb->st_size == 0)
break;
++nFilesA;
nBytesA += sb->st_size;
s = fnvpath(path, sb);
if (s == 0) {
printf("==> %s, err %x\n", path, s);
break;
}
printf("==> %s, %x\n", path, s);
hashesA.insert(s);
break;
}
default: {
break;
}
}
if (s == 0) return -1;
return 0;
}
int cbB(const char *path, const struct stat *sb, int typeflag, struct FTW *ft) {
int rc;
uint32_t s;
switch (typeflag) {
case FTW_F: {
if ((sb->st_mode & S_IFMT) != S_IFREG)
break;
if (sb->st_size == 0)
break;
++nFilesB;
nBytesB += sb->st_size;
s = fnvpath(path, sb);
if (s == 0) {
printf("==> %s, err %x\n", path, s);
break;
}
printf("==> %s, %x\n", path, s);
hashesB.insert(s);
break;
}
default: {
break;
}
}
if (s == 0) return -1;
return 0;
}
// Walk argv[1] and argv[2]. Make sure files, bytes, and hashes are all the same.
int main(int argc, char *argv[])
{
int i;
i = nftw(argv[1], cbA, 64, FTW_PHYS);
if (i) {
printf("%d err %d\n", i, errno);
}
i = nftw(argv[2], cbB, 64, FTW_PHYS);
if (i) {
printf("%d err %d\n", i, errno);
}
if (nFilesA != nFilesB) {
fprintf(stderr, "nFiles %d %d\n", nFilesA, nFilesB);
return -1;
}
if (nBytesA != nBytesB) {
fprintf(stderr, "nBytes %ld %ld\n", nBytesA, nBytesB);
return -2;
}
if (hashesA != hashesB) {
fprintf(stderr, "hashes\n");
return -3;
}
return 0;
}