a leading / in a pattern now means "use a absolute path match". This
[rsync/rsync.git] / exclude.c
CommitLineData
c627d613
AT
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/*
21 a lot of this stuff was derived from GNU tar
22 */
23
24#include "rsync.h"
25
26extern int verbose;
6ba9279f 27extern int am_server;
c627d613 28
3a6a366f 29static char **exclude_list;
c627d613
AT
30
31static int is_regex(char *str)
32{
33 return strchr(str, '*') || strchr(str, '[') || strchr(str, '?');
34}
35
36
37static int check_one_exclude(char *name,char *pattern)
38{
c627d613
AT
39 char *p;
40
41 if (!strchr(pattern,'/') && (p=strrchr(name,'/')))
42 name = p+1;
43
44 if (!name[0]) return 0;
45
0944563e
AT
46 if (*pattern == '/' && *name != '/') pattern++;
47
c627d613
AT
48 if (is_regex(pattern)) {
49 if (fnmatch(pattern, name, 0) == 0)
50 return 1;
51 } else {
52 int l1 = strlen(name);
53 int l2 = strlen(pattern);
54 if (l2 <= l1 &&
55 strcmp(name+(l1-l2),pattern) == 0 &&
56 (l1==l2 || name[l1-(l2+1)] == '/'))
57 return 1;
58 }
59
60 return 0;
61}
62
63
64int check_exclude(char *name,char **local_exclude_list)
65{
66 int n;
67
68 if (exclude_list) {
69 for (n=0; exclude_list[n]; n++)
70 if (check_one_exclude(name,exclude_list[n]))
71 return 1;
72 }
73
74 if (local_exclude_list) {
75 for (n=0; local_exclude_list[n]; n++)
76 if (check_one_exclude(name,local_exclude_list[n]))
77 return 1;
78 }
79
80 return 0;
81}
82
83
84void add_exclude_list(char *pattern,char ***list)
85{
86 int len=0;
87 if (list && *list)
88 for (; (*list)[len]; len++) ;
89
90 if (strcmp(pattern,"!") == 0) {
91 if (verbose > 2)
6ba9279f 92 fprintf(FINFO,"clearing exclude list\n");
c627d613
AT
93 while ((len)--)
94 free((*list)[len]);
95 free((*list));
96 *list = NULL;
97 return;
98 }
99
100 if (!*list) {
101 *list = (char **)malloc(sizeof(char *)*2);
102 } else {
103 *list = (char **)realloc(*list,sizeof(char *)*(len+2));
104 }
105
106 if (!*list || !((*list)[len] = strdup(pattern)))
107 out_of_memory("add_exclude");
108
109 if (verbose > 2)
6ba9279f 110 fprintf(FINFO,"add_exclude(%s)\n",pattern);
c627d613
AT
111
112 (*list)[len+1] = NULL;
113}
114
115void add_exclude(char *pattern)
116{
117 add_exclude_list(pattern,&exclude_list);
118}
119
120char **make_exclude_list(char *fname,char **list1,int fatal)
121{
122 char **list=list1;
123 FILE *f = fopen(fname,"r");
124 char line[MAXPATHLEN];
125 if (!f) {
126 if (fatal) {
dc5ddbcc 127 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
34ccb63e 128 exit_cleanup(1);
c627d613
AT
129 }
130 return list;
131 }
132
133 while (fgets(line,MAXPATHLEN,f)) {
134 int l = strlen(line);
135 if (l && line[l-1] == '\n') l--;
136 line[l] = 0;
137 if (line[0]) add_exclude_list(line,&list);
138 }
139 fclose(f);
140 return list;
141}
142
143
144void add_exclude_file(char *fname,int fatal)
145{
146 exclude_list = make_exclude_list(fname,exclude_list,fatal);
147}
148
149
150void send_exclude_list(int f)
151{
152 int i;
153 if (exclude_list)
154 for (i=0;exclude_list[i];i++) {
155 int l = strlen(exclude_list[i]);
156 if (l == 0) continue;
157 write_int(f,l);
158 write_buf(f,exclude_list[i],l);
159 }
160 write_int(f,0);
161}
162
163
164void recv_exclude_list(int f)
165{
166 char line[MAXPATHLEN];
167 int l;
168 while ((l=read_int(f))) {
575f2fca
AT
169 if (l >= MAXPATHLEN) overflow("recv_exclude_list");
170 read_sbuf(f,line,l);
171 add_exclude(line);
c627d613
AT
172 }
173}
174
175
176static char *cvs_ignore_list[] = {
177 "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
178 "tags","TAGS",".make.state",".nse_depinfo",
179 "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
180 "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
181 "core",NULL};
182
183
184
185void add_cvs_excludes(void)
186{
187 char fname[MAXPATHLEN];
188 char *p;
189 int i;
190
191 for (i=0; cvs_ignore_list[i]; i++)
192 add_exclude(cvs_ignore_list[i]);
193
13a1f792 194 if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
c627d613
AT
195 sprintf(fname,"%s/.cvsignore",p);
196 add_exclude_file(fname,0);
197 }
198
199 if ((p=getenv("CVSIGNORE"))) {
200 char *tok;
201 for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
202 add_exclude(tok);
203 }
204}