-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.c
405 lines (393 loc) · 13.2 KB
/
dfs.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
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
#include "header/dfs.h"
void usage() {
const char *progname = "dfs";
printf("usage: %s [-OPTION...]\n"
"\t--diff\t\t Compare current and save state and print difference\n"
"\t--update\t Save current state\n"
"\t--list\t\t List save state\n"
"\t--friendly\t human readable\n"
"\t--verbose\t Verbose mode\n"
"\t--debug\t\t Show dev info\n"
"\t-c\t\t Specify an alternative configuration file\n"
"\tex: %s --diff --update\n\n"
"\tDefault config file:\n\n"
"\t[general]\n\tdbdir=path to save db csv file (absolute)\n"
"\t[path to check (absolute)]\n\tattrib=1 or 0\t#check owner, group and right\n"
"\tcontent=1 or 0\t#check size, date and content\n"
"\trecurse=1 or 0\t#check subdirectory\n",
progname, progname);
exit(0);
}
char *getHashOfFile(char *filename) {
/* Renvoie le hash MD5 du fichier */
unsigned char c[MD5_DIGEST_LENGTH];
int i;
FILE *inFile = fopen (filename, "rb");
MD5_CTX mdContext;
int bytes;
unsigned char data[1024];
if (inFile == NULL) {
char *error = malloc(sizeof(char)*strlen("hashError"));
strcpy(error, "hashError");
//printf ("%s can't be opened.\n", filename);
return error;
}
char *szOut = malloc(sizeof(char)*33);
MD5_Init(&mdContext);
while ((bytes = fread (data, 1, 1024, inFile)) != 0)
MD5_Update (&mdContext, data, bytes);
MD5_Final(c, &mdContext);
//for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
for(i = 0; i < MD5_DIGEST_LENGTH; i++) snprintf(&(szOut[i*2]), 16*2, "%02x", c[i]);
//printf ("\n", filename);
fclose (inFile);
return szOut;
}
char *getModeProtection(mode_t protection) {
char *fDroit = malloc(sizeof(char)*10+1);
strcpy(fDroit, ((S_ISDIR(protection)) ? "d" : ((S_ISLNK(protection)) ? "l" : "-")));
strcat(fDroit, ((protection & S_IRUSR) ? "r" : "-"));
strcat(fDroit, ((protection & S_IWUSR) ? "w" : "-"));
strcat(fDroit, ((protection & S_IXUSR) ? "x" : "-"));
strcat(fDroit, ((protection & S_IRGRP) ? "r" : "-"));
strcat(fDroit, ((protection & S_IWGRP) ? "w" : "-"));
strcat(fDroit, ((protection & S_IXGRP) ? "x" : "-"));
strcat(fDroit, ((protection & S_IROTH) ? "r" : "-"));
strcat(fDroit, ((protection & S_IWOTH) ? "w" : "-"));
strcat(fDroit, ((protection & S_IXOTH) ? "x" : "-"));
return fDroit;
}
void showHumanDateModifFromStatStruct(char *date) {
time_t idate = (time_t) atoi(date);
char time_buf[25];
strftime(time_buf, sizeof(time_buf), "%d-%b-%Y %H:%M", gmtime(&idate));
printf("%s", time_buf);
}
void showOwnerNameFromUid(char *owner) {
int uid = atoi(owner);
printf("%s", getpwuid(uid)->pw_name);
}
void showGroupNameFromGid(char *group) {
int gid = atoi(group);
printf("%s", getgrgid(gid)->gr_name);
}
void showHumanFileSize(char *filesize) {
double fsize = strtod(filesize, NULL);
int i = 0;
/* Octet, Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta */
const char units[] = {'B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
while (fsize > 1024) {
fsize /= 1024;
i++;
}
printf("%.*f%c",i , fsize, units[i]);
}
void diffDB(struct conf *pcfg) {
if (verbose)
printf("Check difference...\n");
struct conf *cfg = pcfg;
char *recurse;
char *attrib;
char *content;
while (cfg != NULL) {
if (strcmp("general", cfg->section) != 0){
recurse = getValue(cfg, cfg->section, "recurse");
attrib = getValue(cfg, cfg->section, "attrib");
content = getValue(cfg, cfg->section, "content");
verifDiff(cfg->section, atoi(recurse), atoi(attrib), atoi(content));
free(recurse);
free(attrib);
free(content);
}
cfg = cfg->next;
}
if (verbose)
printf("Difference checked!\n");
}
void verifDiff(char *path, int recurse, int attrib, int content) {
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(path)) == NULL) {
if (debug || verbose) printf("cannot open directory: %s\n", path);
return;
}
chdir(path);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* C'est un dossier donc on rentre dedans */
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
continue;
/* On entre dans le dossier suivant en relatif grace à chdir(path) */
if (recurse)
verifDiff(entry->d_name, 1, attrib, content);
}
else {
/* c'est un fichier donc on récupère les infos et envoie dans fichier csv de sauvegarde */
/* nom, owner, group, droit, taille, dateModif, hash */
char strLineToVerif[512];
char *md5Hash = getHashOfFile(entry->d_name);
char *droit = getModeProtection(statbuf.st_mode);
if (attrib && content) // on envoie la totale
sprintf(strLineToVerif, "%s;%d;%d;%s;%d;%d;%s\n", entry->d_name, (int) statbuf.st_uid, (int) statbuf.st_gid, droit, (int) statbuf.st_size, (int) statbuf.st_mtime, md5Hash);
else if (attrib && !content) // on envoie la première partie
sprintf(strLineToVerif, "%s;%d;%d;%s", entry->d_name, (int) statbuf.st_uid, (int) statbuf.st_gid, droit);
else if (!attrib && content) // on envoie la seconde partie
sprintf(strLineToVerif, "%d;%d;%s", (int) statbuf.st_size, (int) statbuf.st_mtime, md5Hash);
int find = findLine(fCsvFile, strLineToVerif, attrib, content);
sprintf(strLineToVerif, "%s;%d;%d;%s;%d;%d;%s", entry->d_name, (int) statbuf.st_uid, (int) statbuf.st_gid, droit, (int) statbuf.st_size, (int) statbuf.st_mtime, md5Hash);
free(md5Hash);
free(droit);
if (find == 0) // si pas trouvé, on affiche strLineToVerif parce que ça à changé
printf("CHANGE -> %s\n", strLineToVerif);
else if (find == -1)
return;
}
}
chdir("..");
closedir(dp);
}
void updateDB(struct conf *pcfg) { // --update
if (verbose)
printf("update csv database...\n");
int section = 1;
char formatFile[25] = "save%d.csv";
char saveFile[25];
sprintf(saveFile, formatFile, section);
char *dbdir = getValue(pcfg, "general", "dbdir");
if (chdir(dbdir) == -1){
if (verbose || debug)
printf("mkdir(%s)...\n", dbdir);
mkdir(dbdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
chdir(dbdir);
}
FILE *f;
struct conf *cfg = pcfg;
char *recurse;
while (cfg != NULL) {
if (strcmp("general", cfg->section) != 0){
f = fopen(saveFile, "w");
if (f == NULL)
return;
fprintf(f, "[%s]\n", cfg->section);
recurse = getValue(cfg, cfg->section, "recurse");
updateFile(cfg->section, f, atoi(recurse));
free(recurse);
fclose(f);
section++;
sprintf(saveFile, formatFile, section);
chdir(dbdir);
}
cfg = cfg->next;
}
free(dbdir);
if (verbose)
printf("csv database updated.\n");
}
void updateFile(char *path, FILE *saveFile, int recurse) {
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(path)) == NULL) {
if (debug || verbose) printf("cannot open directory: %s\n", path);
return;
}
chdir(path);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* C'est un dossier donc on rentre dedans */
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
continue;
/* On entre dans le dossier suivant en relatif grace à chdir(path) */
if (recurse)
updateFile(entry->d_name, saveFile, 1);
}
else {
/* c'est un fichier donc on récupère les infos et envoie dans fichier csv de sauvegarde */
/* On sauvegarde tout meme si attrib ou content est à 0, on ignorera dans le --diff */
/* ordre -> nom, owner, group, droit, taille, dateModif, hash */
char *md5Hash = getHashOfFile(entry->d_name);
char *droit = getModeProtection(statbuf.st_mode);
fprintf(saveFile, "%s;%d;%d;%s;%d;%d;%s\n", entry->d_name, (int) statbuf.st_uid, (int) statbuf.st_gid, droit, (int) statbuf.st_size, (int) statbuf.st_mtime, md5Hash);
free(md5Hash);
free(droit);
}
}
chdir("..");
closedir(dp);
}
void listeDB(struct conf *pcfg)
{
struct conf *cfg = pcfg;
int section = 1;
char formatFile[25] = "save%d.csv";
char csvFile[25];
sprintf(csvFile, formatFile, section);
char *dbdir = getValue(pcfg, "general", "dbdir");
if (chdir(dbdir) == -1){
if (debug)
printf("chdir(%s) FAIL\n", dbdir);
}
if (verbose)
printf("Database: %s\n", dbdir);
while (cfg->next != NULL){
read_file(csvFile);
cfg = cfg->next;
section++;
sprintf(csvFile, formatFile, section);
}
free(dbdir);
}
void read_file(char const *saveFile) { // --list
if (verbose)
printf("Read file %s...\n", saveFile);
char s[512];
FILE *f = fopen(saveFile, "r");
if (f == NULL) {
if (verbose)
printf("Can't read file %s\n", saveFile);
return;
}
char *tok;
int indice = FILE_NAME;
while (fgets(s, 512-1, f) != NULL) {
indice = FILE_NAME;
tok = strtok(s, ";");
while (tok != NULL) {
if (friendly){
switch (indice){
case FILE_DMODIF:
showHumanDateModifFromStatStruct(tok);
break;
case FILE_OWNER:
showOwnerNameFromUid(tok);
break;
case FILE_GROUP:
showGroupNameFromGid(tok);
break;
case FILE_SIZE:
showHumanFileSize(tok);
break;
default:
printf("%s", tok);
break;
}
tok = strtok(NULL, ";");
}
else {
printf("%s", tok);
tok = strtok(NULL, ";");
}
if (tok != NULL)
printf(";");
indice++;
}
}
free(tok);
fclose(f);
}
void remplirFile(struct conf *pcfg) {
struct conf *cfg = pcfg;
int section = 1;
char formatFile[25] = "save%d.csv";
char csvFile[25];
sprintf(csvFile, formatFile, section);
char *dbdir = getValue(pcfg, "general", "dbdir");
if (chdir(dbdir) == -1){
if (debug)
printf("chdir(%s) FAIL\n", dbdir);
}
while (cfg->next != NULL){
char s[512];
FILE *f = fopen(csvFile, "r");
if (f == NULL)
return;
while (fgets(s, 512-1, f) != NULL) {
enfiler(&fCsvFile, s);
}
fclose(f);
cfg = cfg->next;
section++;
sprintf(csvFile, formatFile, section);
}
free(dbdir);
}
void enfiler(File *mafile, char *line) {
File tmp;
tmp = malloc(sizeof(struct Element));
tmp->line = malloc(sizeof(char)*strlen(line)+1);
strcpy(tmp->line, line);
tmp->suivant = *mafile;
*mafile=tmp;
}
File defiler(File *mafile) {
File tmp, tmp2;
if (*mafile == NULL)
return NULL;
else {
tmp = *mafile;
if ((*mafile)->suivant == NULL){
(*mafile) = NULL;
return tmp;
}
else {
while(tmp->suivant->suivant != NULL)
tmp = tmp->suivant;
tmp2 = tmp->suivant;
tmp->suivant = NULL;
return tmp2;
}
}
}
void afficher(File f) {
while(f!=NULL) {
printf("%s",f->line);
f=f->suivant;
}
printf("\n");
}
void detruire(File *f) {
while(*f != NULL) {
File sup = defiler(&(*f));
free(sup->line);
free(sup);
}
}
int findLine(File f, char *line, int attrib, int content) {
int find = 0;
if (f==NULL){
if (debug) printf("fCsvFile is NULL\n");
if (verbose) printf("DB File note found! --diff is not possible\n");
return -1;
}
/* Il y a 3 condition mais les deux dernieres sont pour la clarté,
ce qu'il faut vérifier est géré par la fonction appelante verifDiff() */
if (attrib && content) { // on verifie tout
while(f->suivant!=NULL && find == 0) {
if (strcmp(f->line, line) == 0)
find = 1;
f=f->suivant;
}
}
else if (attrib && !content) { // on ne verifie que OWNER, GROUP, RIGHT
while(f->suivant!=NULL && find == 0) {
if (strstr(f->line, line) != NULL)
find = 1;
f=f->suivant;
}
}
else if (!attrib && content) { // one ne verifie que SIZE, DMODIF, HASH
while(f->suivant!=NULL && find == 0) {
if (strstr(f->line, line) != NULL)
find = 1;
f=f->suivant;
}
}
else
printf("findLine(laFile, %s, %d, %d) problem;\n", line, attrib, content);
return find;
}