-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcmpdir.c
More file actions
451 lines (376 loc) · 10.6 KB
/
cmpdir.c
File metadata and controls
451 lines (376 loc) · 10.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#define ONEK 1024.0
#define ONEM 1048576.0
#define ONEG 1073741824.0
#define ONET 1099511627776.0
#define PATH_MAX 4096
#define OLDDIR 0x01
#define NEWDIR 0x02
#define START_HASHBITS 12
/* max number of linked list before double the hash table size */
#define MAX_DEPTH 9
#define SSIZE 0
#define SNAME 1
struct d_node {
ino_t d_ino;
char *f_name;
unsigned char dir_id;
unsigned char depth;
off_t f_size;
struct d_node *prev;
struct d_node *next;
};
static struct d_node **nodes;
static unsigned int hashsize = 1 << START_HASHBITS;
/* prototypes */
struct d_node *dalloc(char *d_name);
void add_or_update(struct d_node *dnp);
void dir_walk(char *dirpath, int dir_id);
void print_tree(char *olddir, char *newdir, int sortmode);
void upsizing(void);
void humanize_bytes(char buf[], int bufsize, unsigned long long n);
static inline int is_old_only(int dir_id);
static inline int is_new_only(int dir_id);
static inline uint32_t hash_inode(ino_t ino, unsigned int hashsize);
static int dnode_cmp_size(const void *p1, const void *p2);
static int dnode_cmp_name(const void *p1, const void *p2);
void sort_tree(struct d_node ***arry_removed, int *ar_len,
struct d_node ***arry_added, int *aa_len, int sortmode);
void debug_verify_hash(void);
void humanize_bytes(char buf[], int bufsize, unsigned long long n)
{
double b;
memset(buf, 0, bufsize);
b = (double) n;
if (b > ONET)
snprintf(buf, bufsize, "%.2f TiB", b / ONET);
else if (b > ONEG)
snprintf(buf, bufsize, "%.2f GiB", b / ONEG);
else if (b > ONEM)
snprintf(buf, bufsize, "%.2f MiB", b / ONEM);
else if (b > ONEK)
snprintf(buf, bufsize, "%.2f KiB", b / ONEK);
else
snprintf(buf, bufsize, "%llu Bytes", n);
}
struct d_node *dalloc(char *d_name)
{
struct d_node *dp;
dp = malloc(sizeof(*dp));
if (dp == NULL) {
fprintf(stderr, "Error: d_node malloc failed\n");
exit(2);
}
memset(dp, 0, sizeof(*dp));
dp->f_name = strdup(d_name);
dp->prev = NULL;
dp->next = NULL;
dp->dir_id = 0;
dp->depth = 0;
return dp;
}
void dir_walk(char *dirpath, int dir_id)
{
DIR *dirp;
struct dirent *dp;
char path[PATH_MAX];
struct stat statbuf;
struct d_node *dnp;
errno = 0;
dirp = opendir(dirpath);
if (dirp == NULL) {
fprintf(stderr, "Error: opendir failed on %s, ", dirpath);
switch (errno) {
case EACCES:
fprintf(stderr, "Permission denied.\n");
break;
case EBADF:
fprintf(stderr, "fd is not a valid file desar_leniptor.\n");
break;
case EMFILE:
fprintf(stderr, "too many fd used by process.\n");
break;
case ENFILE:
fprintf(stderr, "too many fd used by system.\n");
break;
case ENOENT:
fprintf(stderr, "dir does not exist.\n");
break;
case ENOMEM:
fprintf(stderr, "not enough memory.\n");
break;
case ENOTDIR:
fprintf(stderr, "not a directory.\n");
break;
default:
fprintf(stderr, "errno = %d.\n", errno);
break;
}
return;
}
memset(&statbuf, 0, sizeof(statbuf));
for (;;) {
dp = readdir(dirp);
if (dp == NULL) {
if (errno != 0)
fprintf(stderr, "readdir error, errno = %d.\n", errno);
closedir(dirp);
break;
}
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
continue;
memset(path, 0, PATH_MAX);
strcat(path, dirpath);
strcat(path, "/");
strcat(path, dp->d_name);
lstat(path, &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
dir_walk(path, dir_id);
} else {
dnp = dalloc(path);
dnp->d_ino = statbuf.st_ino;
dnp->f_size = statbuf.st_size;
dnp->dir_id = dir_id;
add_or_update(dnp);
}
}
}
static inline int is_old_only(int dir_id)
{
return dir_id == OLDDIR;
}
static inline int is_new_only(int dir_id)
{
return dir_id == NEWDIR;
}
/* works very well for test set from dozens to 30K inodes */
static inline uint32_t hash_inode(ino_t ino, unsigned int hashsize)
{
return ino & (hashsize - 1);
}
static int dnode_cmp_size(const void *p1, const void *p2)
{
struct d_node *np1, *np2;
np1 = *(struct d_node **)p1;
np2 = *(struct d_node **)p2;
if (np1->f_size < np2->f_size)
return -1;
else if (np1->f_size == np2->f_size)
return 0;
else
return 1;
}
static int dnode_cmp_name(const void *p1, const void *p2)
{
struct d_node *np1, *np2;
np1 = *(struct d_node **)p1;
np2 = *(struct d_node **)p2;
return strcmp(np1->f_name, np2->f_name);
}
void sort_tree(struct d_node ***arry_removed, int *ar_len,
struct d_node ***arry_added, int *aa_len, int sortmode)
{
int i, m, n;
int cr, ca;
struct d_node *np, **ar, **aa;
cr = 0;
ca = 0;
for (i = 0; i < hashsize; i++)
for (np = nodes[i]; np != NULL; np = np->next) {
if (is_old_only(np->dir_id))
cr++;
else if (is_new_only(np->dir_id))
ca++;
}
ar = malloc(cr * sizeof(struct d_node **));
aa = malloc(ca * sizeof(struct d_node **));
if (aa == NULL || ar == NULL) {
fprintf(stderr, "Error: malloc for sort array failed\n");
exit(2);
}
for (i = 0; i < cr; i++)
ar[i] = NULL;
for (i = 0; i < ca; i++)
aa[i] = NULL;
m = 0;
n = 0;
for (i = 0; i < hashsize; i++)
for (np = nodes[i]; np != NULL; np = np->next) {
if (is_old_only(np->dir_id))
ar[m++] = np;
else if (is_new_only(np->dir_id))
aa[n++] = np;
}
switch (sortmode) {
case SNAME:
qsort(aa, ca, sizeof(struct d_node **), dnode_cmp_name);
qsort(ar, cr, sizeof(struct d_node **), dnode_cmp_name);
break;
case SSIZE:
qsort(aa, ca, sizeof(struct d_node **), dnode_cmp_size);
qsort(ar, cr, sizeof(struct d_node **), dnode_cmp_size);
break;
default:
break;
}
*arry_removed = ar;
*arry_added = aa;
*ar_len = cr;
*aa_len = ca;
}
void print_tree(char *olddir, char *newdir, int sortmode)
{
int i;
int ar_len, aa_len;
unsigned long long removed = 0;
unsigned long long added = 0;
struct d_node *np;
struct d_node **ar, **aa;
char buf[64];
sort_tree(&ar, &ar_len, &aa, &aa_len, sortmode);
for (i = 0; i < ar_len; i++) {
np = ar[i];
removed += np->f_size;
humanize_bytes(buf, 64, np->f_size);
printf(" Removed: %11s %s\n", buf, np->f_name);
}
printf("\n-----------------------------------------------------------\n\n");
for (i = 0; i < aa_len; i++) {
np = aa[i];
added += np->f_size;
humanize_bytes(buf, 64, np->f_size);
printf(" New: %15s %s\n", buf, np->f_name);
}
printf("\n-----------------------------------------------------------\n");
humanize_bytes(buf, 64, removed);
printf("Removed %5d files from %s, %s\n", ar_len, olddir, buf);
humanize_bytes(buf, 64, added);
printf("Added %5d files to %s, %s\n", aa_len, olddir, buf);
free(ar);
free(aa);
}
void add_or_update(struct d_node *dnp)
{
int i;
struct d_node *np;
i = hash_inode(dnp->d_ino, hashsize);
for (np = nodes[i]; np != NULL; np = np->next) {
if (np->d_ino == dnp->d_ino) {
np->dir_id |= dnp->dir_id;
free(dnp->f_name);
free(dnp);
return;
}
}
dnp->next = nodes[i];
if (dnp->next != NULL) {
dnp->next->prev = dnp;
dnp->depth = dnp->next->depth + 1;
}
nodes[i] = dnp;
if (nodes[i]->depth > MAX_DEPTH)
upsizing();
}
void upsizing(void)
{
struct d_node **old_nodes;
struct d_node *np, *next;
int i, k;
unsigned int old_hashsize;
//debug_verify_hash();
old_nodes = nodes;
old_hashsize = hashsize;
hashsize *= 2;
nodes = malloc(hashsize * sizeof(struct d_node *));
if (nodes == NULL) {
fprintf(stderr, "Error: malloc for new nodes failed\n");
exit(2);
}
for (i = 0; i < hashsize; i++)
nodes[i] = NULL;
for (i = 0; i < old_hashsize; i++) {
np = old_nodes[i];
while (np != NULL) {
next = np->next;
k = hash_inode(np->d_ino, hashsize);
if (nodes[k] == NULL)
np->depth = 0; /* reset depth for new hash table*/
else
np->depth = nodes[k]->depth + 1;
if (nodes[k] != NULL)
nodes[k]->prev = np;
np->prev = NULL;
np->next = nodes[k];
nodes[k] = np;
np = next;
}
}
free(old_nodes);
return;
}
void debug_verify_hash(void)
{
int i, max;
struct d_node *np;
double used, md;
used = 0.0;
md = 0.0;
max = -1;
for (i = 0; i < hashsize; i++) {
np = nodes[i];
if (np) {
used += 1.0;
md += np->depth;
if (np->depth > max)
max = np->depth;
}
}
printf("total %d bucket, %.0f used, %.1f%%, depth: mean %.1f, max %d\n",
hashsize,
used,
100.0 * used / hashsize,
md / used, max);
}
int main(int argc, char *argv[])
{
int i, opt, mode;
char *d1, *d2;
if (argc < 3) {
printf("Usage: %s [-s] old_dir new_dir\n"
" -s: sort output by file size\n"
" defalut sort output by file name\n", basename(argv[0]));
exit(2);
}
mode = SNAME;
while ((opt = getopt(argc, argv, "s")) != -1) {
switch (opt) {
case 's':
mode = SSIZE;
break;
default:
break;
}
}
nodes = malloc(hashsize * sizeof(struct d_node *));
if (nodes == NULL) {
fprintf(stderr, "Error: initial nodes malloc failed\n");
exit(2);
}
for (i = 0; i < hashsize; i++)
nodes[i] = NULL;
d1 = argv[argc - 2];
d2 = argv[argc - 1];
dir_walk(d1, OLDDIR); /* dir to compare with */
dir_walk(d2, NEWDIR); /* new dir */
print_tree(d1, d2, mode);
return 0;
}