added a --partial option which tells rsync to keep partially
[rsync/rsync.git] / exclude.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* a lot of this stuff was originally derived from GNU tar, although
21    it has now changed so much that it is hard to tell :) */
22
23 #include "rsync.h"
24
25 extern int verbose;
26
27 static struct exclude_struct **exclude_list;
28
29 /* build an exclude structure given a exclude pattern */
30 static struct exclude_struct *make_exclude(char *pattern, int include)
31 {
32         struct exclude_struct *ret;
33
34         ret = (struct exclude_struct *)malloc(sizeof(*ret));
35         if (!ret) out_of_memory("make_exclude");
36
37         memset(ret, 0, sizeof(*ret));
38
39         ret->orig = strdup(pattern);
40
41         if (strncmp(pattern,"- ",2) == 0) {
42                 pattern += 2;
43         } else if (strncmp(pattern,"+ ",2) == 0) {
44                 ret->include = 1;
45                 pattern += 2;
46         } else {
47                 ret->include = include;
48         }
49
50         ret->pattern = strdup(pattern);
51
52         if (!ret->orig || !ret->pattern) out_of_memory("make_exclude");
53
54         if (strpbrk(pattern, "*[?")) ret->regular_exp = 1;
55
56         if (strlen(pattern) > 1 && pattern[strlen(pattern)-1] == '/') {
57                 ret->pattern[strlen(pattern)-1] = 0;
58                 ret->directory = 1;
59         }
60
61         if (!strchr(ret->pattern,'/')) {
62                 ret->local = 1;
63         }
64
65         return ret;
66 }
67
68 static void free_exclude(struct exclude_struct *ex)
69 {
70         free(ex->orig);
71         free(ex->pattern);
72         memset(ex,0,sizeof(*ex));
73         free(ex);
74 }
75
76 static int check_one_exclude(char *name,struct exclude_struct *ex,
77                              STRUCT_STAT *st)
78 {
79         char *p;
80         int match_start=0;
81         char *pattern = ex->pattern;
82
83         if (ex->local && (p=strrchr(name,'/')))
84                 name = p+1;
85
86         if (!name[0]) return 0;
87
88         if (ex->directory && !S_ISDIR(st->st_mode)) return 0;
89
90         if (*pattern == '/' && *name != '/') {
91                 match_start = 1;
92                 pattern++;
93         }
94
95         if (ex->regular_exp) {
96                 if (fnmatch(pattern, name, 0) == 0)
97                         return 1;
98         } else {
99                 int l1 = strlen(name);
100                 int l2 = strlen(pattern);
101                 if (l2 <= l1 && 
102                     strcmp(name+(l1-l2),pattern) == 0 &&
103                     (l1==l2 || (!match_start && name[l1-(l2+1)] == '/')))
104                         return 1;
105         }
106
107         return 0;
108 }
109
110
111 int check_exclude(char *name,struct exclude_struct **local_exclude_list,
112                   STRUCT_STAT *st)
113 {
114         int n;
115
116         if (exclude_list) {
117                 for (n=0; exclude_list[n]; n++)
118                         if (check_one_exclude(name,exclude_list[n],st))
119                                 return !exclude_list[n]->include;
120         }
121
122         if (local_exclude_list) {
123                 for (n=0; local_exclude_list[n]; n++)
124                         if (check_one_exclude(name,local_exclude_list[n],st))
125                                 return !local_exclude_list[n]->include;
126         }
127
128         return 0;
129 }
130
131
132 void add_exclude_list(char *pattern,struct exclude_struct ***list, int include)
133 {
134         int len=0;
135         if (list && *list)
136                 for (; (*list)[len]; len++) ;
137
138         if (strcmp(pattern,"!") == 0) {
139                 if (verbose > 2)
140                         rprintf(FINFO,"clearing exclude list\n");
141                 while ((len)--) {
142                         free_exclude((*list)[len]);
143                 }
144                 free((*list));
145                 *list = NULL;
146                 return;
147         }
148
149         *list = (struct exclude_struct **)Realloc(*list,sizeof(struct exclude_struct *)*(len+2));
150         
151         if (!*list || !((*list)[len] = make_exclude(pattern, include)))
152                 out_of_memory("add_exclude");
153         
154         if (verbose > 2)
155                 rprintf(FINFO,"add_exclude(%s)\n",pattern);
156         
157         (*list)[len+1] = NULL;
158 }
159
160 void add_exclude(char *pattern, int include)
161 {
162         add_exclude_list(pattern,&exclude_list, include);
163 }
164
165 struct exclude_struct **make_exclude_list(char *fname,
166                                           struct exclude_struct **list1,
167                                           int fatal, int include)
168 {
169         struct exclude_struct **list=list1;
170         FILE *f = fopen(fname,"r");
171         char line[MAXPATHLEN];
172         if (!f) {
173                 if (fatal) {
174                         rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
175                         exit_cleanup(1);
176                 }
177                 return list;
178         }
179
180         while (fgets(line,MAXPATHLEN,f)) {
181                 int l = strlen(line);
182                 if (l && line[l-1] == '\n') l--;
183                 line[l] = 0;
184                 if (line[0]) add_exclude_list(line,&list,include);
185         }
186         fclose(f);
187         return list;
188 }
189
190
191 void add_exclude_file(char *fname,int fatal,int include)
192 {
193         if (!fname || !*fname) return;
194
195         exclude_list = make_exclude_list(fname,exclude_list,fatal,include);
196 }
197
198
199 void send_exclude_list(int f)
200 {
201         int i;
202         extern int remote_version;
203
204         if (!exclude_list) {
205                 write_int(f,0);
206                 return;
207         }
208
209         for (i=0;exclude_list[i];i++) {
210                 char *pattern = exclude_list[i]->orig; 
211                 int l;
212
213                 if (remote_version < 19) {
214                         if (strncmp(pattern,"+ ", 2)==0) {
215                                 rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
216                                 exit_cleanup(1);
217                         }
218                         
219                         if (strncmp(pattern,"- ", 2) == 0) {
220                                 pattern += 2;
221                         }
222                 }
223                 
224                 l = strlen(pattern);
225                 if (l == 0) continue;
226                 write_int(f,l);
227                 write_buf(f,pattern,l);
228         }    
229
230         write_int(f,0);
231 }
232
233
234 void recv_exclude_list(int f)
235 {
236         char line[MAXPATHLEN];
237         int l;
238         while ((l=read_int(f))) {
239                 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
240                 read_sbuf(f,line,l);
241                 add_exclude(line,0);
242         }
243 }
244
245
246 void add_exclude_line(char *p)
247 {
248         char *tok;
249         if (!p || !*p) return;
250         p = strdup(p);
251         if (!p) out_of_memory("add_exclude_line");
252         for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
253                 add_exclude(tok, 0);
254         free(p);
255 }
256
257
258 static char *cvs_ignore_list[] = {
259   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
260   "tags","TAGS",".make.state",".nse_depinfo",
261   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
262   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
263   "core",NULL};
264
265
266
267 void add_cvs_excludes(void)
268 {
269         char fname[MAXPATHLEN];
270         char *p;
271         int i;
272   
273         for (i=0; cvs_ignore_list[i]; i++)
274                 add_exclude(cvs_ignore_list[i], 0);
275
276         if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
277                 slprintf(fname,sizeof(fname)-1, "%s/.cvsignore",p);
278                 add_exclude_file(fname,0,0);
279         }
280
281         add_exclude_line(getenv("CVSIGNORE"));
282 }