-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileRemover.c
287 lines (242 loc) · 9.32 KB
/
fileRemover.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
#include "fileRemover.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
int main (int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "%s <option argument> <term/s>\n", argv[0]);
exit(1);
}
if (strlen(argv[ARG_INDEX]) != 2) {
fprintf(stderr, "Provide a valid argument\n");
exit(1);
}
char argumentType[ARG_TYPE_LEN];
strcpy(argumentType, argv[ARG_INDEX]);
if (!strcmp(argumentType, "-n")) {
removeNameFileDir(&argv[TERM_INDEX], argc - 2, CALL_NORM);
} else if (!strcmp(argumentType, "-p")) {
removeNameFileDir(&argv[TERM_INDEX], argc - 2, CALL_PREFIX);
} else if (!strcmp(argumentType, "-t")) {
removeNameFileDir(&argv[TERM_INDEX], argc - 2, CALL_TIME);
} else if (!strcmp(argumentType, "-g")) {
removeGreppedFile(&argv[TERM_INDEX], argc - 2, CALL_GREP);
}
return 0;
}
// This function will remove a directory using posix_spawnp. This is a better
// alternative than rmdir function. However is unsafe, as removals are permanent.
static void removeDirectory(char path[MAX_PATHNAME_LEN]) {
// Load arguments for posix_spawnp (we're using recursive removal).
pid_t pid;
extern char **environ;
char *rmDirArgv[] = {"rm", "-r", path, NULL};
if (posix_spawnp(&pid, rmDirArgv[0], NULL, NULL, rmDirArgv, environ) != 0) {
perror("posix_spawnp");
exit(1);
}
// Wait for child process to die.
if (waitpid(pid, NULL, 0) == EOF) {
perror("waitpid");
exit(1);
}
}
// Retrieves the last status change time.
static time_t retrieveTime(char path[MAX_PATHNAME_LEN]) {
struct stat file_stat;
if (lstat(path, &file_stat) == EOF) {
perror(path);
exit(1);
}
return file_stat.st_ctime;
}
// This function strips a given Time much like strptime.
static time_t stripTime(char *datetime) {
// Reference: https://stackoverflow.com/a/24701952
struct tm timeSplit;
int numVariables = sscanf(datetime, "%d/%d/%d %d:%d:%d", &timeSplit.tm_mday,
&timeSplit.tm_mon, &timeSplit.tm_year, &timeSplit.tm_hour,
&timeSplit.tm_min, &timeSplit.tm_sec);
// If we receive an error, or invalid number of variables.
if (numVariables == EOF || numVariables != 6) {
perror(datetime);
exit(1);
}
// tm_year is the time since 1900.
timeSplit.tm_year -= 1900;
// tm_mon range from 0-11.
timeSplit.tm_mon -= 1;
// Retrieves currentTime.
time_t currentTime;
time(¤tTime);
struct tm *currentTimeStruct = localtime(¤tTime);
// Check for valid time.
if (timeSplit.tm_year > (currentTimeStruct->tm_year) || timeSplit.tm_year < 70) {
fprintf(stderr, "Invalid Year Defined\n");
exit(1);
}
time_t resultTime = mktime(&timeSplit);
// If the resultTime - currentTime > 0, it means that the time is in the future.
if (difftime(resultTime, currentTime) > 0) {
fprintf(stderr, "Time is in the future\n");
exit(1);
}
return resultTime;
}
// In this function we calculate the time difference between the datetime passed,
// and filePath identified.
static int calculateTimeDiff(char *datetime, char filePath[MAX_PATHNAME_LEN]) {
// %m/%d/%y %H:%M:%S format. strptime(datetime, "%D %T", &timeRetrieved)
// We cannot use strptime since it's POSIX standard.
// Requires a macro to be defined which breaks functionality
// of other functions.
time_t timeArg = stripTime(datetime);
time_t fileTime = retrieveTime(filePath);
double difference = difftime(fileTime, timeArg);
// If difference, is less than 0, it means it is older (which is intended).
int result = difference < 0.0;
return result;
}
// This function will find and delete any paths that relate to the searchTerm.
// It will search recursively only if we observe a directory. It will return
// the number of files removed.
static int findPaths(char *searchTerm, char filePath[MAX_PATHNAME_LEN],
int callType) {
int numFilesRemoved = 0;
char tempPath[MAX_PATHNAME_LEN];
strcpy(tempPath, filePath);
DIR *de = opendir(filePath);
if (de == NULL) {
perror(filePath);
exit(1);
}
struct dirent *dirp;
while ((dirp = readdir(de)) != NULL) {
if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
strcat(tempPath, dirp->d_name);
// Check if file is directory or regular file.
if (dirp->d_type == DT_DIR) {
// Concat directory.
strcat(tempPath, DIR_DELIM);
if (callType == CALL_TIME &&
calculateTimeDiff(searchTerm, tempPath) ||
callType == CALL_PREFIX &&
!strncmp(searchTerm, dirp->d_name, strlen(searchTerm))) {
// Remove directory and add to numFilesRemoved.
removeDirectory(tempPath);
numFilesRemoved += 1;
} else {
numFilesRemoved += findPaths(searchTerm, tempPath, callType);
}
} else if (dirp->d_type == DT_REG) {
if (callType == CALL_NORM && !strcmp(dirp->d_name, searchTerm) ||
(callType == CALL_PREFIX && !strncmp(searchTerm,
dirp->d_name, strlen(searchTerm))) ||
callType == CALL_TIME &&
calculateTimeDiff(searchTerm, tempPath) ||
callType == CALL_GREP && grepFiles(searchTerm, tempPath)) {
// If we can remove a directory then we add to the numFilesRemoved.
numFilesRemoved += remove(tempPath) == 0 ? 1: 0;
}
}
// Resets tempPath.
strcpy(tempPath, filePath);
}
}
closedir(de);
return numFilesRemoved;
}
// This function receives terms and the numTerms. Based on the callType we,
// will remove the files or directories passed.
static void removeNameFileDir(char **termCollection, int numTerms, int callType) {
for (int i = 0; i < numTerms; i++) {
char filePath[MAX_PATHNAME_LEN];
strcpy(filePath, CUR_DIR);
int numFilesRemoved = findPaths(termCollection[i], filePath, callType);
if (numFilesRemoved) {
printf("File: %-10s had %d instances removed\n", termCollection[i],
numFilesRemoved);
} else {
printf("No file exist for filename: %s\n", termCollection[i]);
}
}
}
// This function will remove files based on grep.
static void removeGreppedFile(char **termCollection, int numTerms, int callType) {
char *grepTerms = calloc(MAX_GREP_TERM_REGEX, sizeof(char));
strcat(grepTerms, termCollection[0]);
for (int i = 1; i < numTerms; i++) {
// This is syntax for grep regex, when you want to have one or more terms.
strcat(grepTerms, "|");
strcat(grepTerms, termCollection[i]);
}
// filePath.
char filePath[MAX_PATHNAME_LEN];
strcpy(filePath, CUR_DIR);
int numFilesRemoved = findPaths(grepTerms, filePath, callType);
if (numFilesRemoved) {
printf("File: %-10s had %d instances removed\n", grepTerms, numFilesRemoved);
} else {
printf("No such term/s exist for %s\n", grepTerms);
}
free(grepTerms);
}
// This function will grep files using posix_spawnp. It will check if the given file
// contains any of the grepTerms.
static int grepFiles(char *grepTerms, char filePath[MAX_PATHNAME_LEN]) {
int fileDesc[2];
if (pipe(fileDesc) == EOF) {
perror("pipe");
exit(1);
}
posix_spawn_file_actions_t actions;
if (posix_spawn_file_actions_init(&actions) != 0) {
perror("posix_spawn_file_actions_init");
exit(1);
}
// Put close to read end of file descriptor.
if (posix_spawn_file_actions_addclose(&actions, fileDesc[READ_END]) != 0) {
perror("posix_spawn_file_actions_addclose");
exit(1);
}
// Changing the printing such that, the result doesn't print to stdout (fileno = 1),
// and stores it in the write end of the file descriptor.
if (posix_spawn_file_actions_adddup2(&actions, fileDesc[WRITE_END], 1) != 0) {
perror("posix_spawn_file_actions_adddup2");
exit(1);
}
pid_t pid;
extern char **environ;
// Cannot use grep -l grepTerms <*.* - all files or *.c all c files>
// -E advanced regex. -l meaning list files. -w search by word.
char *grepArgv[] = {"grep", "-E", "-l", "-w", grepTerms, filePath, NULL};
if (posix_spawnp(&pid, grepArgv[0], &actions, NULL, grepArgv, environ) != 0) {
perror("posix_spawnp");
exit(1);
}
close(fileDesc[1]);
// Open file using read end of fileDescriptor.
FILE *inputStream = fdopen(fileDesc[READ_END], "r");
if (inputStream == NULL) {
perror("fdopen");
exit(1);
}
int letter = 0;
// If we do get a result from inputStream other than EOF, then it is
// successful.
int isSuccessful = ((letter = fgetc(inputStream)) == EOF) ? FALSE: TRUE;
fclose(inputStream);
if (waitpid(pid, NULL, 0) == EOF) {
perror("waitpid");
exit(1);
}
// Destroy actions.
posix_spawn_file_actions_destroy(&actions);
return isSuccessful;
}