Got rid of O_TEXT_STR and added code to strip '\r' from the end of the
[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 extern int delete_mode;
31
32 static struct exclude_struct **exclude_list;
33
34 /** Build an exclude structure given a exclude pattern */
35 static struct exclude_struct *make_exclude(const char *pattern, int include)
36 {
37         struct exclude_struct *ret;
38
39         ret = (struct exclude_struct *)malloc(sizeof(*ret));
40         if (!ret) out_of_memory("make_exclude");
41
42         memset(ret, 0, sizeof(*ret));
43
44         if (strncmp(pattern,"- ",2) == 0) {
45                 pattern += 2;
46         } else if (strncmp(pattern,"+ ",2) == 0) {
47                 ret->include = 1;
48                 pattern += 2;
49         } else {
50                 ret->include = include;
51         }
52
53         ret->pattern = strdup(pattern);
54
55         if (!ret->pattern) out_of_memory("make_exclude");
56
57         if (strpbrk(pattern, "*[?")) {
58             ret->regular_exp = 1;
59             ret->fnmatch_flags = FNM_PATHNAME;
60             if (strstr(pattern, "**")) {
61                     static int tested;
62                     if (!tested) {
63                             tested = 1;
64                             if (fnmatch("a/b/*", "a/b/c/d", FNM_PATHNAME)==0) {
65                                     rprintf(FERROR,"WARNING: fnmatch FNM_PATHNAME is broken on your system\n");
66                             }
67                     }
68                     ret->fnmatch_flags = 0;
69             }
70         }
71
72         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
73                 ret->pattern[strlen(pattern)-1] = 0;
74                 ret->directory = 1;
75         }
76
77         if (!strchr(ret->pattern,'/')) {
78                 ret->local = 1;
79         }
80
81         return ret;
82 }
83
84 static void free_exclude(struct exclude_struct *ex)
85 {
86         free(ex->pattern);
87         memset(ex,0,sizeof(*ex));
88         free(ex);
89 }
90
91 static int check_one_exclude(char *name, struct exclude_struct *ex,
92                              STRUCT_STAT *st)
93 {
94         char *p;
95         int match_start=0;
96         char *pattern = ex->pattern;
97
98         if (ex->local && (p=strrchr(name,'/')))
99                 name = p+1;
100
101         if (!name[0]) return 0;
102
103         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
104
105         if (*pattern == '/' && *name != '/') {
106                 match_start = 1;
107                 pattern++;
108         }
109
110         if (ex->regular_exp) {
111                 if (fnmatch(pattern, name, ex->fnmatch_flags) == 0) {
112                         return 1;
113                 }
114         } else {
115                 int l1 = strlen(name);
116                 int l2 = strlen(pattern);
117                 if (l2 <= l1 && 
118                     strcmp(name+(l1-l2),pattern) == 0 &&
119                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/'))) {
120                         return 1;
121                 }
122         }
123
124         return 0;
125 }
126
127
128 static void report_exclude_result(char const *name,
129                                   struct exclude_struct const *ent,
130                                   STRUCT_STAT const *st)
131 {
132         /* If a trailing slash is present to match only directories,
133          * then it is stripped out by make_exclude.  So as a special
134          * case we add it back in here. */
135         
136         if (verbose >= 2)
137                 rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
138                         ent->include ? "including" : "excluding",
139                         S_ISDIR(st->st_mode) ? "directory" : "file",
140                         name, ent->pattern,
141                         ent->directory ? "/" : "");
142 }
143
144
145 /*
146  * Return true if file NAME is defined to be excluded by either
147  * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST.
148  */
149 int check_exclude(char *name, struct exclude_struct **local_exclude_list,
150                   STRUCT_STAT *st)
151 {
152         int n;
153         struct exclude_struct *ent;
154
155         if (name && (name[0] == '.') && !name[1])
156                 /* never exclude '.', even if somebody does --exclude '*' */
157                 return 0;
158
159         if (exclude_list) {
160                 for (n=0; exclude_list[n]; n++) {
161                         ent = exclude_list[n];
162                         if (check_one_exclude(name, ent, st)) {
163                                 report_exclude_result(name, ent, st);
164                                 return !ent->include;
165                         }
166                 }
167         }
168
169         if (local_exclude_list) {
170                 for (n=0; local_exclude_list[n]; n++) {
171                         ent = local_exclude_list[n];
172                         if (check_one_exclude(name, ent, st)) {
173                                 report_exclude_result(name, ent, st);
174                                 return !ent->include;
175                         }
176                 }
177         }
178
179         return 0;
180 }
181
182
183 void add_exclude_list(const char *pattern, struct exclude_struct ***list, int include)
184 {
185         int len=0;
186         if (list && *list)
187                 for (; (*list)[len]; len++) ;
188
189         if (strcmp(pattern,"!") == 0) {
190                 if (verbose > 2)
191                         rprintf(FINFO,"clearing exclude list\n");
192                 while ((len)--) {
193                         free_exclude((*list)[len]);
194                 }
195                 free((*list));
196                 *list = NULL;
197                 return;
198         }
199
200         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
201         
202         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
203                 out_of_memory("add_exclude");
204         
205         if (verbose > 2) {
206                 rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
207                               include ? "include" : "exclude");
208         }
209
210         (*list)[len+1] = NULL;
211 }
212
213 void add_exclude(const char *pattern, int include)
214 {
215         add_exclude_list(pattern,&exclude_list, include);
216 }
217
218 struct exclude_struct **make_exclude_list(const char *fname,
219                                           struct exclude_struct **list1,
220                                           int fatal, int include)
221 {
222         struct exclude_struct **list=list1;
223         FILE *f;
224         char line[MAXPATHLEN];
225
226         if (strcmp(fname, "-")) {
227                 f = fopen(fname,"r");
228         } else {
229                 f = fdopen(0, "r");
230         }
231         if (!f) {
232                 if (fatal) {
233                         rsyserr(FERROR, errno,
234                                 "failed to open %s file %s",
235                                 include ? "include" : "exclude",
236                                 fname);
237                         exit_cleanup(RERR_FILEIO);
238                 }
239                 return list;
240         }
241
242         while (fgets(line,MAXPATHLEN,f)) {
243                 int l = strlen(line);
244                 while (l && (line[l-1] == '\n' || line[l-1] == '\r')) l--;
245                 line[l] = 0;
246                 if (line[0] && (line[0] != ';') && (line[0] != '#')) {
247                         /* Skip lines starting with semicolon or pound.
248                            It probably wouldn't cause any harm to not skip
249                              them but there's no need to save them. */
250                         add_exclude_list(line,&list,include);
251                 }
252         }
253         fclose(f);
254         return list;
255 }
256
257
258 void add_exclude_file(const char *fname, int fatal, int include)
259 {
260         if (!fname || !*fname) return;
261
262         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
263 }
264
265
266 void send_exclude_list(int f)
267 {
268         int i;
269         extern int remote_version;
270         extern int list_only, recurse;
271
272         /* This is a complete hack - blame Rusty.
273          *
274          * FIXME: This pattern shows up in the output of
275          * report_exclude_result(), which is not ideal. */
276         if (list_only && !recurse) {
277                 add_exclude("/*/*", 0);
278         }
279
280         if (!exclude_list) {
281                 write_int(f,0);
282                 return;
283         }
284
285         for (i=0;exclude_list[i];i++) {
286                 int l;
287                 char pattern[MAXPATHLEN];
288
289                 strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern)); 
290                 if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
291
292                 l = strlen(pattern);
293                 if (l == 0) continue;
294                 if (exclude_list[i]->include) {
295                         if (remote_version < 19) {
296                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
297                                 exit_cleanup(RERR_UNSUPPORTED);
298                         }
299                         write_int(f,l+2);
300                         write_buf(f,"+ ",2);
301                 } else {
302                         write_int(f,l);
303                 }
304                 write_buf(f,pattern,l);
305         }    
306
307         write_int(f,0);
308 }
309
310
311 void recv_exclude_list(int f)
312 {
313         char line[MAXPATHLEN];
314         unsigned int l;
315
316         while ((l=read_int(f))) {
317                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
318                 read_sbuf(f,line,l);
319                 add_exclude(line,0);
320         }
321 }
322
323 /* Get the next include/exclude arg from the string. It works in a similar way
324 ** to strtok - initially an arg is sent over, from then on NULL. This
325 ** routine takes into account any +/- in the strings and does not
326 ** consider the space following it as a delimeter.
327 */
328 char *get_exclude_tok(char *p)
329 {
330         static char *s;
331         static int more;
332         char *t;
333
334         if (p) {
335                 s=p;
336                 if (*p)
337                         more=1;
338         }
339
340         if (!more)
341                 return(NULL);
342
343         /* Skip over any initial spaces */
344         while (isspace(* (unsigned char *) s))
345                 s++;
346
347         /* Are we at the end of the string? */
348         if (*s) {
349                 /* remember the beginning of the token */
350                 t=s;
351
352                 /* Is this a '+' or '-' followed by a space (not whitespace)? */
353                 if ((*s=='+' || *s=='-') && *(s+1)==' ')
354                         s+=2;
355         
356                 /* Skip to the next space or the end of the string */
357                 while (!isspace(* (unsigned char *) s) && *s != '\0')
358                         s++;
359         } else {
360                 t=NULL;
361         }
362
363         /* Have we reached the end of the string? */
364         if (*s)
365                 *s++='\0';
366         else
367                 more=0;
368         return(t);
369 }
370
371         
372 void add_exclude_line(char *p)
373 {
374         char *tok;
375         if (!p || !*p) return;
376         p = strdup(p);
377         if (!p) out_of_memory("add_exclude_line");
378         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
379                 add_exclude(tok, 0);
380         free(p);
381 }
382
383 void add_include_line(char *p)
384 {
385         char *tok;
386         if (!p || !*p) return;
387         p = strdup(p);
388         if (!p) out_of_memory("add_include_line");
389         for (tok=get_exclude_tok(p); tok; tok=get_exclude_tok(NULL))
390                 add_exclude(tok, 1);
391         free(p);
392 }
393
394
395 static char *cvs_ignore_list[] = {
396   "RCS/", "SCCS/", "CVS/", ".svn/", "CVS.adm", "RCSLOG", "cvslog.*",
397   "tags", "TAGS", ".make.state", ".nse_depinfo",
398   "*~", "#*", ".#*", ", *", "*.old", "*.bak", "*.BAK", "*.orig",
399   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
400   "core", NULL};
401
402
403 void add_cvs_excludes(void)
404 {
405         char fname[MAXPATHLEN];
406         char *p;
407         int i;
408   
409         for (i=0; cvs_ignore_list[i]; i++)
410                 add_exclude(cvs_ignore_list[i], 0);
411
412         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
413                 snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
414                 add_exclude_file(fname,0,0);
415         }
416
417         add_exclude_line(getenv("CVSIGNORE"));
418 }