-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperR.c
More file actions
311 lines (281 loc) · 9.3 KB
/
Copy pathhelperR.c
File metadata and controls
311 lines (281 loc) · 9.3 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
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "helperR.h"
#include "extern_module.h"
#define TRUE 1
#define FALSE 0
/* The only item we can dequeue from a Directory Queue is a directory
Parameter dequeuedItem refers to the name of the directory dequeued.
Parameter files refers to the arraylist of files
Parameter dirs refers to the arraylist of directories.
Parameter fileSuffix such as ".txt" need for comparison of directory entries that are files.
Return: we populate files and dirs by having a pointer to them, nothing returned.
*/
int directoryFunction_r(char* dequeuedItem, Node* files, Node* dirs, char* fileSuffix){
int err_flag = 0;
DIR* dirp = opendir(dequeuedItem);
// Check if we have perms to read it.
if(dirp == NULL){
perror(dequeuedItem);
return EXIT_FAILURE;
}
struct dirent* entry; // This structure may be statically allocated. Don't attempt to free it. man 3 readdir
struct stat entryStat; // temporary stat struct for each entry read from directory stream in loop.
int statReturn;
while((entry = readdir(dirp))!= NULL){
// EXCEPTION: Any entry in a directory whose name begins with a period is ignored.
if(strncmp(".", entry->d_name, strlen(".")) == 0){
continue;
}
// Relative path fully formed.
char* currentPath = generateFilePath(dequeuedItem, entry->d_name);
#ifdef DEBUG
fprintf(stdout, "currentPath: %s\n", currentPath);
#endif
statReturn = stat(currentPath, &entryStat); // Variety of reasons -> man 2 stat
if(statReturn == -1) {
perror(currentPath);
free(currentPath);
err_flag = 1;
continue;
}
// Check whether file or directory and whether each has open perms. Then add to their respective linked lists.
/* No permissions required on the file itself, but are required on all directories in pathname that lead to file */
if(S_ISREG(entryStat.st_mode)){
// If we can't open it continue forward.
int inputFD = open(currentPath, O_RDONLY);
if(inputFD == -1){
perror(currentPath);
free(currentPath);
err_flag = 1;
continue;
}
close(inputFD);
if(endsWithSuffix(fileSuffix, currentPath) == FALSE) {
free(currentPath);
continue;
}
// If we can open it and has matching suffix then add it to files
Node* temp = (Node *)malloc(sizeof(Node));
if (!temp) {
perror("helperR: directoryFunction - Node malloc failure!");
exit(EXIT_FAILURE);
}
temp->value = files->value;
temp->next = files->next;
files->value = currentPath;
files->next = temp;
}
else if(S_ISDIR(entryStat.st_mode)){
// If it has read perms then add it.
DIR* temporaryCheckForPerms = opendir(currentPath);
if(temporaryCheckForPerms == NULL){
perror(currentPath);
free(currentPath);
err_flag = 1;
continue;
}
closedir(temporaryCheckForPerms);
// Add currentPath to dir linkedlist.
Node* temp = (Node *)malloc(sizeof(Node));
if (!temp) {
perror("helperR: directoryFunction - Node malloc failure!");
exit(EXIT_FAILURE);
}
temp->value = dirs->value;
temp->next = dirs->next;
dirs->value = currentPath;
dirs->next = temp;
}
}
closedir(dirp);
free(dequeuedItem);
if (err_flag) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 1 if true, 0 if false
int endsWithSuffix(char* fileSuffix, char* fileName){
int suffixLen = strlen(fileSuffix);
int fileNameLen = strlen(fileName);
if(suffixLen > fileNameLen){
return FALSE;
}
for(int i=0; i<suffixLen; ++i){
if(fileSuffix[suffixLen-i-1] != fileName[fileNameLen-i-1]){
return FALSE;
}
}
return TRUE;
}
/* Generate file path relative to current working directory for deqeued item
Implicitly reentrant
*/
char* generateFilePath(char* directoryName, char* currPath){
strbuf_t path;
sb_initk(&path, 10);
sb_concatk(&path, directoryName);
sb_appendk(&path, '/');
sb_concatk(&path, currPath);
char* returnString = malloc(sizeof(char)* (path.used+1));
if (!returnString) {
perror("helperR: generateFilePath - malloc failure!");
exit(EXIT_FAILURE);
}
strcpy(returnString, path.data);
sb_destroyk(&path);
return returnString;
}
/* Used for generateFilepath */
int sb_resetk(strbuf_t *sb){
sb->used = 0;
sb->data[sb->used] = '\0';
return EXIT_SUCCESS;
}
int sb_initk(strbuf_t *sb, size_t length)
{
if(length < 1){
return EXIT_FAILURE;
}
sb->data = malloc(sizeof(char) * length);
if (!sb->data) {
perror("helperR: sb_initk - malloc failure!");
exit(EXIT_FAILURE);
}
sb->length = length;
sb->used = 0;
sb->data[sb->used] = '\0';
return EXIT_SUCCESS;
}
int sb_removek(strbuf_t *sb, char* item){
// if nothing to remove, can't return anything
if(sb->used == 0) return EXIT_FAILURE;
--sb->used;
if (item)
*item = sb->data[sb->used];
sb->data[sb->used] = '\0';
return EXIT_SUCCESS;
}
void sb_destroyk(strbuf_t *sb)
{
free(sb->data);
}
int sb_appendk(strbuf_t *sb, char item){
if((sb->used+1) == sb->length){
size_t size = sb->length * 2;
char *p = realloc(sb->data, sizeof(char)* size);
if(!p) {
perror("helperR: sb_appendk - realloc failure!");
exit(EXIT_FAILURE);
}
// successful
sb->data = p;
sb->length = size;
}
sb->data[sb->used] = item;
++sb->used;
sb->data[sb->used] = '\0';
return EXIT_SUCCESS;
}
/*
sb_concat adds the string str to the end of the string held in sb.
Assume that str is a null-terminated C string.
*/
int sb_concatk(strbuf_t *sb, char *str){
if((sb->used + strlen(str)) >= sb->length){
size_t size = ((sb->used + strlen(str) + 1) > 2 * sb->length) ? (sb->used + strlen(str) + 1) : 2*sb->length;
char *p = realloc(sb->data, sizeof(char)* size);
if(!p) {
perror("helperR: sb_concatk - realloc failure!");
exit(EXIT_FAILURE);
}
// successful
sb->data = p;
sb->length = size;
}
int count = 0;
while(count < strlen(str)){
sb->data[sb->used+count] = *(str+count);
++count;
}
sb->data[sb->used+count] = '\0';
sb->used += count;
return EXIT_SUCCESS;
}
int sb_insertk(strbuf_t *sb, int index, char item){
if(index <= sb->used){
++sb->used;
if(sb->used == sb->length){
size_t size = 2*sb->length;
char *p = realloc(sb->data, sizeof(char)* size);
if(!p) {
perror("helperR: sb_insertk - realloc failure!");
exit(EXIT_FAILURE);
}
// successful
sb->data = p;
sb->length = size;
} // we have enough space to shift everything over
sb->data[sb->used] = '\0';
// start from index, shift everything over until you get to null term
char temp;
for(int i=index; i<sb->used; i++){
temp = sb->data[i];
sb->data[i] = item;
item = temp;
}
return EXIT_SUCCESS;
}
else{ // we know that index > used
if((index+1) >= (2*sb->length)){
size_t size = index+2;
char *p = realloc(sb->data, sizeof(char)* size);
if(!p) {
perror("helperR: sb_insertk - realloc failure!");
exit(EXIT_FAILURE);
}
// successful
sb->data = p;
sb->length = size;
sb->data[index] = item;
sb->data[index+1] = '\0';
sb->used = index+1;
return EXIT_SUCCESS;
} // we know that index+1 is less than 2*sb->length
// so it will fit between used and length
// need to check if enough length to cover, if not double
else{
if(index+1 >= sb->length){
size_t size = 2*sb->length;
char *p = realloc(sb->data, sizeof(char)* size);
if(!p) {
perror("helperR: sb_insertk - realloc failure!");
exit(EXIT_FAILURE);
}
// successful
sb->data = p;
sb->length = size;
sb->data[index] = item;
sb->data[index+1] = '\0';
sb->used = index+1;
return EXIT_SUCCESS;
}
else{
// index+1 is > used but less than length
sb->data[index] = item;
sb->data[index+1] = '\0';
sb->used = index+1;
return EXIT_SUCCESS;
}
}
}
}