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