-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscandir.c
237 lines (186 loc) · 5.59 KB
/
scandir.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
#include "scandir.h"
int FilesFound=0;
int FilesFailed=0;
int FilesUpdated=0;
int upload(struct frontdown_config *config, const char *source, char *relpath, char *name, struct stat filestat){
char srcpathstring[FD_PATHLEN*2]={0};
char dstpathstring[FD_PATHLEN*2]={0};
char ret;
int len=-1;
// strcpy(srcpathstring, source);
// len=strlen(source);
// srcpathstring[len]='/';
// strcpy(&srcpathstring[len+1], relpath);
// len+=1+strlen(relpath);
// srcpathstring[len]='/';
strcpy(&srcpathstring[len+1], name);
strcpy(dstpathstring, config->destination);
len=strlen(config->destination);
dstpathstring[len]='/';
strcpy(&dstpathstring[len+1], relpath);
len+=1+strlen(relpath);
dstpathstring[len]='/';
strcpy(&dstpathstring[len+1], name);
config->info(dstpathstring);
if((ret=put_file(config, srcpathstring, name, dstpathstring, filestat.st_size))==0){
FilesUpdated++;
return 0;
} else if(ret==-1){
FilesFailed++;
return -1;
} else {
return -2;
}
}
int fd_scandir(struct frontdown_config *config, long long timestamp){
char pathstring[FD_PATHLEN*2]={0};
char *dirpointer[128];
int dirptrc=0;
char ret;
struct dirnode *node, *root, *freewilli;
char sourcepath[FD_PATHLEN*2];
strcpy(sourcepath, config->source);
char orig_dir[FD_PATHLEN*2];
strcpy(orig_dir, get_current_dir_name());
if(chdir(config->source)<0){
perror("chdir");
return -1;
}
root=calloc(1,sizeof(struct dirnode));
strcpy(root->path, ".");
pathstring[0]='.';
dirpointer[dirptrc]=&pathstring[1];
create_dest_dir(config, pathstring);
node=root;
do{
config->info(pathstring);
if((ret=anakin_filewalker(config, node, node->sub, sourcepath, pathstring, timestamp, config->excludes))==0){ //No more subdirs
next_dir:
freewilli=node;
node=node->next; //Try next dir in folder
if(freewilli->top==NULL) break; //Make sure we're not in root
freewilli->top->sub=node; //Attach new folder as primary subdir of its parent
if(node==NULL){ //if no directory left in this stage
node=freewilli->top; //move one stage up
free(freewilli); //free old stage
if(node->top==NULL)break; //validate stage
if(chdir("..")<0){ //change directory
perror("chdir");
return -1;
}
dirptrc--;
*dirpointer[dirptrc]='\0'; //edit dir string
goto next_dir; //last subdir of current dir was handled, so we need to get the next
}
free(freewilli); //free old dir
if(chdir("..")<0){
perror("chdir");
return -1;
}
dirptrc--;
*dirpointer[dirptrc]='\0';
} else if(ret==1) { //More subdirs
node=node->sub; //goto subdir
} else {
return -1;
}
if(chdir(node->path)<0){ //change to new dir
perror("chdir");
puts(node->path);
return -1;
}
//update pathstring
dirptrc++;
*dirpointer[dirptrc-1]='/';
memcpy(dirpointer[dirptrc-1]+1, node->path, strlen(node->path));
dirpointer[dirptrc]=dirpointer[dirptrc-1]+1+strlen(node->path);
*dirpointer[dirptrc]='\0';
create_dest_dir(config, pathstring);
}while(node->top!=NULL);
printf("Total %d files\n \
\rUpdated %d files\n \
\rFailed on %d files\n", FilesFound, FilesUpdated, FilesFailed);
chdir(orig_dir);
return 0;
}
int filter(char *path, char *name, long long timestamp, long long time, struct frontdown_exclude_list *excludes){
int status;
char pathstring[FD_PATHLEN]={0};
struct frontdown_exclude_list *excl_walker;
regex_t re;
excl_walker=excludes;
strcpy(pathstring, path);
status=strlen(path);
pathstring[status]='/';
strcpy(&pathstring[status+1], name);
while(excl_walker!=NULL){
if(excl_walker->exclude_path[0]==0){
strcpy(excl_walker->exclude_path, "^index.db$");
}
if (regcomp(&re, excl_walker->exclude_path, REG_EXTENDED|REG_NOSUB) != 0) {
excl_walker=excl_walker->next;
continue;
}
status = regexec(&re, &pathstring[2], (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
excl_walker=excl_walker->next;
continue;
}
printf("IGNORE: %s MATCH: %s\n", &pathstring[2], excl_walker->exclude_path);
return -1;
}
return 0;
}
int anakin_filewalker(struct frontdown_config *config, struct dirnode *luke, struct dirnode *leia, const char *source, char *cpath, long long time, struct frontdown_exclude_list *excludes){
struct dirent *pwd_ent;
struct stat buf;
DIR* pwd;
char path[FD_NAMELEN]={0};
if((pwd=opendir("."))==NULL){
perror("opendir");
FilesFailed++;
if(leia==NULL) return 0;
return 1;
}
while((pwd_ent=readdir(pwd))){
if((pwd_ent->d_name[0]=='.') && ( \
(pwd_ent->d_name[1]=='\0') || \
((pwd_ent->d_name[1]=='.')&&(pwd_ent->d_name[2]=='\0'))));
else{
strcpy(path, pwd_ent->d_name);
#ifdef WIN32
if(stat(path, &buf)<0){
#else
if(lstat(path, &buf)<0){
#endif
FilesFailed++;
perror("stat");
} else {
if(S_ISREG(buf.st_mode)){
FilesFound += 1;
}
if(filter(cpath, pwd_ent->d_name, buf.st_mtime, time, excludes)==0){
if(S_ISREG(buf.st_mode)){
if(time<buf.st_mtime){
if(upload(config, source, cpath, pwd_ent->d_name, buf)<-1)return -1;
}
}
if(S_ISDIR(buf.st_mode)){
if(leia!=NULL){
leia->next=calloc(1,sizeof(struct dirnode));
leia=leia->next;
}else{
luke->sub=leia=calloc(1,sizeof(struct dirnode));
}
leia->top=luke;
strcpy(leia->path, path);
}
}
}
}
}
closedir(pwd);
if(leia==NULL) return 0;
return 1;
}