-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff.c
170 lines (145 loc) · 6.76 KB
/
diff.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
/************************************************************************
* fspy - experimental POC linux filesystem activity monitor *
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *
* it's based on the new linux kernel inotify interface (merged into *
* the 2.6.13 linux kernel) *
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *
* this code was tested on 2.6.18.4 and newer kernels *
************************************************************************
* Copyright (C) 2007 Richard Sammet (e-axe) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* http://www.gnu.org/licenses/gpl.txt *
************************************************************************
* Contact, Bugs & Infos: *
************************************************************************
* Some infos: *
* - tabstop size: *
* set ts=2 *
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/inotify.h>
#include "fspy.h"
#include "output.h"
#include "fsevents.h"
#include "stating.h"
/* the base of allowed diffable fields */
/* do not forget to modify this list as needed! */
char dbasewhitelist[] = "sAMSOUGID";
/* holds the fields to be printed colored */
struct diffprint dprint;
/* holds all the needed data to perform a diff */
struct festat *felsptr;
/* global diffing element counter */
unsigned int delc_oa = 0;
struct festat *init_diff(void) {
dprint.s = dprint.A = dprint.M = dprint.S = dprint.O = dprint.U = dprint.G = dprint.G = dprint.I = dprint.D = 0;
if((felsptr = (struct festat *) malloc(DIFF_ELEMENT_INIT_COUNT * sizeof(struct festat))) == NULL) {
fprintf(stderr, "ERROR: could not allocate mem for initial festat list!\n");
exit(EXIT_FAILURE);
}
#ifdef _DEBUG
printf("INIT_FESTAT_LIST: %i\n", DIFF_ELEMENT_INIT_COUNT);
#endif
return felsptr;
}
struct festat *extend_diff_list(struct festat *felsptr) {
if((felsptr = (struct festat *) realloc(felsptr, (delc_oa + DIFF_ELEMENT_INIT_COUNT) * sizeof(struct festat))) == NULL) {
fprintf(stderr, "ERROR: could not reallocate mem to extend dir list!\n");
exit(EXIT_FAILURE);
}
#ifdef _DEBUG
printf("EXTEND_DIFF_LIST: %i (%i)\n", delc_oa + DIFF_ELEMENT_INIT_COUNT, (delc_oa + DIFF_ELEMENT_INIT_COUNT) * sizeof(struct festat));
#endif
return felsptr;
}
void diffing(char *fpath, struct stat *statdat, struct diffprint *dprintptr, char *diffstring) {
char *mystrin_tmp;
char *mystr;
char mychar;
char *freeme;
unsigned int i, id;
#ifdef _DEBUG
printf("DIFFING: %s\n", fpath);
#endif
/* getting the element id */
for(i=0; i <= delc_oa; i++) {
if(strcmp(felsptr[i].path, fpath) == 0) {
id = felsptr[i].id;
break;
}
}
mystrin_tmp = strdup(diffstring);
while((mystr = strtok(mystrin_tmp, DELIM))) {
if(strlen(mystr) == 1) {
mychar = mystr[0];
switch(mychar) {
case 's': if(memcmp(&felsptr[i].statdat.st_size, &statdat->st_size, sizeof(off_t))) {
dprintptr->s = 1;
memcpy(&felsptr[i].statdat.st_size, &statdat->st_size, sizeof(off_t));
}
break;
case 'A': if(memcmp(&felsptr[i].statdat.st_atime, &statdat->st_atime, sizeof(time_t))) {
dprintptr->A = 1;
memcpy(&felsptr[i].statdat.st_atime, &statdat->st_atime, sizeof(time_t));
}
break;
case 'M': if(memcmp(&felsptr[i].statdat.st_mtime, &statdat->st_mtime, sizeof(time_t))) {
dprintptr->M = 1;
memcpy(&felsptr[i].statdat.st_mtime, &statdat->st_mtime, sizeof(time_t));
}
break;
case 'S': if(memcmp(&felsptr[i].statdat.st_ctime, &statdat->st_ctime, sizeof(time_t))) {
dprintptr->S = 1;
memcpy(&felsptr[i].statdat.st_ctime, &statdat->st_ctime, sizeof(time_t));
}
break;
case 'O': if(memcmp(&felsptr[i].statdat.st_mode, &statdat->st_mode, sizeof(mode_t))) {
dprintptr->O = 1;
memcpy(&felsptr[i].statdat.st_mode, &statdat->st_mode, sizeof(mode_t));
}
break;
case 'U': if(memcmp(&felsptr[i].statdat.st_uid, &statdat->st_uid, sizeof(uid_t))) {
dprintptr->U = 1;
memcpy(&felsptr[i].statdat.st_uid, &statdat->st_uid, sizeof(uid_t));
}
break;
case 'G': if(memcmp(&felsptr[i].statdat.st_gid, &statdat->st_gid, sizeof(gid_t))) {
dprintptr->G = 1;
memcpy(&felsptr[i].statdat.st_gid, &statdat->st_gid, sizeof(gid_t));
}
break;
case 'I': if(memcmp(&felsptr[i].statdat.st_ino, &statdat->st_ino, sizeof(ino_t))) {
dprintptr->I = 1;
memcpy(&felsptr[i].statdat.st_ino, &statdat->st_ino, sizeof(ino_t));
}
break;
case 'D': if(memcmp(&felsptr[i].statdat.st_dev, &statdat->st_dev, sizeof(dev_t))) {
dprintptr->D = 1;
memcpy(&felsptr[i].statdat.st_dev, &statdat->st_dev, sizeof(dev_t));
}
break;
default : printf("diffing()->WARNING: This should never happen!!!\n");
break;
}
}
freeme = mystrin_tmp;
mystrin_tmp = NULL;
}
free(freeme);
}