Got rid of support for protocols 17 and 18.
[rsync/rsync.git] / exclude.c
1 /* -*- c-file-style: "linux" -*-
2  *
3  * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4  * Copyright (C) 1996 by Paul Mackerras
5  * Copyright (C) 2002 by Martin Pool
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 /* a lot of this stuff was originally derived from GNU tar, although
23    it has now changed so much that it is hard to tell :) */
24
25 /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */
26
27 #include "rsync.h"
28
29 extern int verbose;
30
31 struct exclude_struct **exclude_list;
32 struct exclude_struct **local_exclude_list;
33 struct exclude_struct **server_exclude_list;
34 char *exclude_path_prefix = NULL;
35
36 /** Build an exclude structure given a exclude pattern */
37 static struct exclude_struct *make_exclude(const char *pattern, int include)
38 {
39         struct exclude_struct *ret;
40         char *cp;
41         int pat_len;
42
43         ret = new(struct exclude_struct);
44         if (!ret) out_of_memory("make_exclude");
45
46         memset(ret, 0, sizeof(*ret));
47
48         if (strncmp(pattern,"- ",2) == 0) {
49                 pattern += 2;
50         } else if (strncmp(pattern,"+ ",2) == 0) {
51                 ret->include = 1;
52                 pattern += 2;
53         } else {
54                 ret->include = include;
55         }
56
57         if (exclude_path_prefix)
58                 ret->match_flags |= MATCHFLG_ABS_PATH;
59         if (exclude_path_prefix && *pattern == '/') {
60                 ret->pattern = new_array(char,
61                         strlen(exclude_path_prefix) + strlen(pattern) + 1);
62                 if (!ret->pattern) out_of_memory("make_exclude");
63                 sprintf(ret->pattern, "%s%s", exclude_path_prefix, pattern);
64         }
65         else {
66                 ret->pattern = strdup(pattern);
67                 if (!ret->pattern) out_of_memory("make_exclude");
68         }
69
70         if (strpbrk(pattern, "*[?")) {
71                 ret->match_flags |= MATCHFLG_WILD;
72                 if (strstr(pattern, "**")) {
73                         ret->match_flags |= MATCHFLG_WILD2;
74                         /* If the pattern starts with **, note that. */
75                         if (*pattern == '*' && pattern[1] == '*')
76                                 ret->match_flags |= MATCHFLG_WILD2_PREFIX;
77                 }
78         }
79
80         pat_len = strlen(ret->pattern);
81         if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
82                 ret->pattern[pat_len-1] = 0;
83                 ret->directory = 1;
84         }
85
86         for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
87                 ret->slash_cnt++;
88
89         return ret;
90 }
91
92 static void free_exclude(struct exclude_struct *ex)
93 {
94         free(ex->pattern);
95         memset(ex,0,sizeof(*ex));
96         free(ex);
97 }
98
99
100 void free_exclude_list(struct exclude_struct ***listp)
101 {
102         struct exclude_struct **list = *listp;
103
104         if (verbose > 2)
105                 rprintf(FINFO,"clearing exclude list\n");
106
107         if (!list)
108                 return;
109
110         while (*list)
111                 free_exclude(*list++);
112
113         free(*listp);
114         *listp = NULL;
115 }
116
117 static int check_one_exclude(char *name, struct exclude_struct *ex,
118                              int name_is_dir)
119 {
120         char *p;
121         int match_start = 0;
122         char *pattern = ex->pattern;
123
124         /* If the pattern does not have any slashes AND it does not have
125          * a "**" (which could match a slash), then we just match the
126          * name portion of the path. */
127         if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
128                 if ((p = strrchr(name,'/')) != NULL)
129                         name = p+1;
130         }
131         else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') {
132                 static char full_name[MAXPATHLEN];
133                 extern char curr_dir[];
134                 int plus = curr_dir[1] == '\0'? 1 : 0;
135                 snprintf(full_name, sizeof full_name,
136                          "%s/%s", curr_dir+plus, name);
137                 name = full_name;
138         }
139
140         if (!name[0]) return 0;
141
142         if (ex->directory && !name_is_dir) return 0;
143
144         if (*pattern == '/') {
145                 match_start = 1;
146                 pattern++;
147                 if (*name == '/')
148                         name++;
149         }
150
151         if (ex->match_flags & MATCHFLG_WILD) {
152                 /* A non-anchored match with an infix slash and no "**"
153                  * needs to match the last slash_cnt+1 name elements. */
154                 if (!match_start && ex->slash_cnt &&
155                     !(ex->match_flags & MATCHFLG_WILD2)) {
156                         int cnt = ex->slash_cnt + 1;
157                         for (p = name + strlen(name) - 1; p >= name; p--) {
158                                 if (*p == '/' && !--cnt)
159                                         break;
160                         }
161                         name = p+1;
162                 }
163                 if (wildmatch(pattern, name))
164                         return 1;
165                 if (ex->match_flags & MATCHFLG_WILD2_PREFIX) {
166                         /* If the **-prefixed pattern has a '/' as the next
167                          * character, then try to match the rest of the
168                          * pattern at the root. */
169                         if (pattern[2] == '/' && wildmatch(pattern+3, name))
170                                 return 1;
171                 }
172                 else if (!match_start && ex->match_flags & MATCHFLG_WILD2) {
173                         /* A non-anchored match with an infix or trailing "**"
174                          * (but not a prefixed "**") needs to try matching
175                          * after every slash. */
176                         while ((name = strchr(name, '/')) != NULL) {
177                                 name++;
178                                 if (wildmatch(pattern, name))
179                                         return 1;
180                         }
181                 }
182         } else if (match_start) {
183                 if (strcmp(name,pattern) == 0)
184                         return 1;
185         } else {
186                 int l1 = strlen(name);
187                 int l2 = strlen(pattern);
188                 if (l2 <= l1 &&
189                     strcmp(name+(l1-l2),pattern) == 0 &&
190                     (l1==l2 || name[l1-(l2+1)] == '/')) {
191                         return 1;
192                 }
193         }
194
195         return 0;
196 }
197
198
199 static void report_exclude_result(char const *name,
200                                   struct exclude_struct const *ent,
201                                   int name_is_dir)
202 {
203         /* If a trailing slash is present to match only directories,
204          * then it is stripped out by make_exclude.  So as a special
205          * case we add it back in here. */
206
207         if (verbose >= 2)
208                 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
209                         ent->include ? "including" : "excluding",
210                         name_is_dir ? "directory" : "file",
211                         name, ent->pattern,
212                         ent->directory ? "/" : "");
213 }
214
215
216 /*
217  * Return true if file NAME is defined to be excluded by either
218  * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
219  */
220 int check_exclude(struct exclude_struct **list, char *name, int name_is_dir)
221 {
222         struct exclude_struct *ent;
223
224         while ((ent = *list++) != NULL) {
225                 if (check_one_exclude(name, ent, name_is_dir)) {
226                         report_exclude_result(name, ent, name_is_dir);
227                         return !ent->include;
228                 }
229         }
230
231         return 0;
232 }
233
234
235 void add_exclude(struct exclude_struct ***listp, const char *pattern, int include)
236 {
237         struct exclude_struct **list = *listp;
238         int len = 0;
239
240         if (*pattern == '!' && !pattern[1]) {
241                 free_exclude_list(listp);
242                 return;
243         }
244
245         if (list)
246                 for (; list[len]; len++) {}
247
248         list = *listp = realloc_array(list, struct exclude_struct *, len+2);
249
250         if (!list || !(list[len] = make_exclude(pattern, include)))
251                 out_of_memory("add_exclude");
252
253         if (verbose > 2) {
254                 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
255                         include ? "include" : "exclude");
256         }
257
258         list[len+1] = NULL;
259 }
260
261
262 void add_exclude_file(struct exclude_struct ***listp, const char *fname,
263                       int fatal, int include)
264 {
265         FILE *fp;
266         char line[MAXPATHLEN];
267         char *eob = line + MAXPATHLEN - 1;
268         extern int eol_nulls;
269
270         if (!fname || !*fname)
271                 return;
272
273         if (*fname != '-' || fname[1])
274                 fp = fopen(fname, "rb");
275         else
276                 fp = stdin;
277         if (!fp) {
278                 if (fatal) {
279                         rsyserr(FERROR, errno,
280                                 "failed to open %s file %s",
281                                 include ? "include" : "exclude",
282                                 fname);
283                         exit_cleanup(RERR_FILEIO);
284                 }
285                 return;
286         }
287
288         while (1) {
289                 char *s = line;
290                 int ch;
291                 while (1) {
292                         if ((ch = getc(fp)) == EOF) {
293                                 if (ferror(fp) && errno == EINTR)
294                                         continue;
295                                 break;
296                         }
297                         if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
298                                 break;
299                         if (s < eob)
300                                 *s++ = ch;
301                 }
302                 *s = '\0';
303                 if (*line && *line != ';' && *line != '#') {
304                         /* Skip lines starting with semicolon or pound.
305                          * It probably wouldn't cause any harm to not skip
306                          * them but there's no need to save them. */
307                         add_exclude(listp, line, include);
308                 }
309                 if (ch == EOF)
310                         break;
311         }
312         fclose(fp);
313 }
314
315
316 void send_exclude_list(int f)
317 {
318         int i;
319         extern int list_only, recurse;
320
321         /* This is a complete hack - blame Rusty.
322          *
323          * FIXME: This pattern shows up in the output of
324          * report_exclude_result(), which is not ideal. */
325         if (list_only && !recurse)
326                 add_exclude(&exclude_list, "/*/*", ADD_EXCLUDE);
327
328         if (!exclude_list) {
329                 write_int(f,0);
330                 return;
331         }
332
333         for (i=0;exclude_list[i];i++) {
334                 int l;
335                 char pattern[MAXPATHLEN];
336
337                 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
338                 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
339
340                 l = strlen(pattern);
341                 if (l == 0) continue;
342                 if (exclude_list[i]->include) {
343                         write_int(f,l+2);
344                         write_buf(f,"+ ",2);
345                 } else {
346                         write_int(f,l);
347                 }
348                 write_buf(f,pattern,l);
349         }
350
351         write_int(f,0);
352 }
353
354
355 void recv_exclude_list(int f)
356 {
357         char line[MAXPATHLEN];
358         unsigned int l;
359
360         while ((l=read_int(f))) {
361                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
362                 read_sbuf(f,line,l);
363                 add_exclude(&exclude_list, line, ADD_EXCLUDE);
364         }
365 }
366
367 /* Get the next include/exclude arg from the string. It works in a similar way
368 ** to strtok - initially an arg is sent over, from then on NULL. This
369 ** routine takes into account any +/- in the strings and does not
370 ** consider the space following it as a delimeter.
371 */
372 char *get_exclude_tok(char *p)
373 {
374         static char *s;
375         static int more;
376         char *t;
377
378         if (p) {
379                 s=p;
380                 if (*p)
381                         more=1;
382         }
383
384         if (!more)
385                 return(NULL);
386
387         /* Skip over any initial spaces */
388         while (isspace(* (unsigned char *) s))
389                 s++;
390
391         /* Are we at the end of the string? */
392         if (*s) {
393                 /* remember the beginning of the token */
394                 t=s;
395
396                 /* Is this a '+' or '-' followed by a space (not whitespace)? */
397                 if ((*s=='+' || *s=='-') && *(s+1)==' ')
398                         s+=2;
399
400                 /* Skip to the next space or the end of the string */
401                 while (!isspace(* (unsigned char *) s) && *s != '\0')
402                         s++;
403         } else {
404                 t=NULL;
405         }
406
407         /* Have we reached the end of the string? */
408         if (*s)
409                 *s++='\0';
410         else
411                 more=0;
412         return(t);
413 }
414
415
416 void add_exclude_line(struct exclude_struct ***listp,
417                       const char *line, int include)
418 {
419         char *tok, *p;
420         if (!line || !*line) return;
421         p = strdup(line);
422         if (!p) out_of_memory("add_exclude_line");
423         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
424                 add_exclude(listp, tok, include);
425         free(p);
426 }
427
428
429 static char *cvs_ignore_list[] = {
430   "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
431   "tags", "TAGS", ".make.state", ".nse_depinfo",
432   "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
433   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
434   "core", NULL};
435
436
437 void add_cvs_excludes(void)
438 {
439         char fname[MAXPATHLEN];
440         char *p;
441         int i;
442
443         for (i=0; cvs_ignore_list[i]; i++)
444                 add_exclude(&exclude_list, cvs_ignore_list[i], ADD_EXCLUDE);
445
446         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
447                 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
448                 add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
449         }
450
451         add_exclude_line(&exclude_list, getenv("CVSIGNORE"), ADD_EXCLUDE);
452 }