-
Notifications
You must be signed in to change notification settings - Fork 0
/
dup_finder.c
97 lines (73 loc) · 2 KB
/
dup_finder.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <openssl/sha.h>
#include "dup_finder.h"
#define SPACES_PER_INDENT_LEVEL 3
#define MAX_PATH_LENGTH 512
#define EXIT_USAGE_ERROR 1
#define EXIT_SUCCESS 0
extern int errno;
int main(int argc, char **argv) {
struct node* root;
print_descendants(argv[1], &root);
printCopies(root);
return EXIT_SUCCESS;
}
/* returns 1 if path_name represents a directory
0 if it isn't */
int is_dir(char *path_name) {
struct stat buff;
if (stat(path_name, &buff) < 0){
fprintf(stderr, "stat: %s\n", strerror(errno));
return 0;
}
return S_ISDIR(buff.st_mode);
}
void print_descendants(char *pathname, struct node** rt) {
if (is_dir(pathname)) {
DIR *d;
struct dirent *p;
char path[MAX_PATH_LENGTH];
char *hashStr;
char *pathStr;
struct node* temp;
if ((d = opendir(pathname)) == NULL){
fprintf(stderr, "opendir %s %s\n", path, strerror(errno));
return;
}
while ((p = readdir(d)) != NULL) {
if (strcmp(".", p->d_name)==0 || /* skip "." and ".." */
strcmp("..", p->d_name)==0 || strcmp(".git", p->d_name)==0)
continue;
//printf("%s\n", p->d_name);
snprintf(path, MAX_PATH_LENGTH, "%s/%s", pathname, p->d_name);
if(!is_dir(path)) {
// Malloc hashStr
if ((hashStr = (char *)malloc(SHA_DIGEST_LENGTH*2+1)) == NULL)
{
printf("MALLOC hashStr Error\n");
exit(1);
}
getHash(path, hashStr);
// Malloc pathStr
if ((pathStr = (char *)malloc(MAX_PATH_LENGTH)) == NULL)
{
printf("MALLOC pathStr Error\n");
exit(1);
}
pathStr = path;
temp = createNode(hashStr, pathStr);
//printf("temp->filename: %s\n", temp->fileName);
insert(temp, rt);
//printf("temp->copy after insert: %d\n", temp->copy);
}
print_descendants(path, rt);
}
closedir(d);
}
}