Updated patches to work with the current trunk.
[rsync/rsync-patches.git] / ignore-case.diff
CommitLineData
992633a5
WD
1This adds the --ignore-case option, which makes rsync compare filenames
2in a case-insensitive manner.
8a524ae0 3
03019e41
WD
4To use this patch, run these commands for a successful build:
5
6 patch -p1 <patches/ignore-case.diff
7 ./configure (optional if already run)
8 make
9
0f8eae47
WD
10TODO:
11
12 - Make this code handle multibyte character encodings, and honor the
13 --iconv setting when converting case.
14
c1ff70aa 15based-on: a01e3b490eb36ccf9e704840e1b6683dab867550
cc3e685d
WD
16diff --git a/exclude.c b/exclude.c
17--- a/exclude.c
18+++ b/exclude.c
c82285d5
WD
19@@ -21,6 +21,7 @@
20 */
21
22 #include "rsync.h"
23+#include "ifuncs.h"
24
c82285d5 25 extern int am_server;
fc557362 26 extern int am_sender;
7170ca8d 27@@ -687,16 +688,15 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
6100b16e
WD
28 if (litmatch_array(pattern, strings, slash_handling))
29 return ret_match;
30 } else if (anchored_match) {
31- if (strcmp(name, pattern) == 0)
5befb079 32+ if (ic_strEQ(name, pattern))
6100b16e
WD
33 return ret_match;
34 } else {
35 int l1 = strlen(name);
36 int l2 = strlen(pattern);
37- if (l2 <= l1 &&
38- strcmp(name+(l1-l2),pattern) == 0 &&
39- (l1==l2 || name[l1-(l2+1)] == '/')) {
40+ if (l2 <= l1
5befb079 41+ && ic_strEQ(name + (l1-l2), pattern)
6100b16e
WD
42+ && (l1 == l2 || name[l1 - (l2+1)] == '/'))
43 return ret_match;
44- }
45 }
46
47 return !ret_match;
cc3e685d
WD
48diff --git a/flist.c b/flist.c
49--- a/flist.c
50+++ b/flist.c
fc557362
WD
51@@ -34,6 +34,7 @@ extern int am_generator;
52 extern int inc_recurse;
9c657c9f
WD
53 extern int always_checksum;
54 extern int module_id;
55+extern int ignore_case;
56 extern int ignore_errors;
57 extern int numeric_ids;
58 extern int recurse;
c1ff70aa 59@@ -3010,6 +3011,7 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
f80ebf7e
WD
60 {
61 int dif;
7bfcb297
WD
62 const uchar *c1, *c2;
63+ uchar ch1, ch2;
f80ebf7e
WD
64 enum fnc_state state1, state2;
65 enum fnc_type type1, type2;
66 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
c1ff70aa 67@@ -3120,7 +3122,15 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
3f053c45
WD
68 if (type1 != type2)
69 return type1 == t_PATH ? 1 : -1;
70 }
71- } while ((dif = (int)*c1++ - (int)*c2++) == 0);
5befb079
WD
72+ ch1 = *c1++;
73+ ch2 = *c2++;
9c657c9f 74+ if (ignore_case) {
5befb079
WD
75+ if (isupper(ch1))
76+ ch1 = tolower(ch1);
77+ if (isupper(ch2))
78+ ch2 = tolower(ch2);
f80ebf7e 79+ }
5befb079 80+ } while ((dif = (int)ch1 - (int)ch2) == 0);
3f053c45
WD
81
82 return dif;
83 }
cc3e685d
WD
84diff --git a/ifuncs.h b/ifuncs.h
85--- a/ifuncs.h
86+++ b/ifuncs.h
5214a41b 87@@ -85,3 +85,38 @@ init_stat_x(stat_x *sx_p)
fc557362
WD
88 sx_p->xattr = NULL;
89 #endif
5befb079
WD
90 }
91+
92+static inline int
93+strEQ(const char *s1, const char *s2)
94+{
95+ return strcmp(s1, s2) == 0;
96+}
97+
98+static inline int
99+strnEQ(const char *s1, const char *s2, size_t n)
100+{
101+ return strncmp(s1, s2, n) == 0;
102+}
103+
104+static inline int
105+ic_strEQ(const char *s1, const char *s2)
106+{
107+ extern int ignore_case;
108+ if (ignore_case)
109+ return strcasecmp(s1, s2) == 0;
110+ return strcmp(s1, s2) == 0;
111+}
112+
113+static inline int
114+ic_strnEQ(const char *s1, const char *s2, size_t n)
115+{
116+ extern int ignore_case;
117+ if (ignore_case)
c82285d5
WD
118+ return strncasecmp(s1, s2, n) == 0;
119+ return strncmp(s1, s2, n) == 0;
5befb079
WD
120+}
121+
122+#define strNE(s1,s2) (!strEQ(s1,s2))
123+#define strnNE(s1,s2,n) (!strnEQ(s1,s2,n))
124+#define ic_strNE(s1,s2) (!ic_strEQ(s1,s2))
125+#define ic_strnNE(s1,s2) (!ic_strnEQ(s1,s2,n))
cc3e685d
WD
126diff --git a/lib/wildmatch.c b/lib/wildmatch.c
127--- a/lib/wildmatch.c
128+++ b/lib/wildmatch.c
992633a5
WD
129@@ -53,6 +53,8 @@
130 #define ISUPPER(c) (ISASCII(c) && isupper(c))
131 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
132
133+extern int ignore_case;
134+
135 #ifdef WILD_TEST_ITERATIONS
136 int wildmatch_iteration_count;
137 #endif
cc3e685d 138@@ -72,6 +74,8 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
ffadcb36
WD
139 for ( ; (p_ch = *p) != '\0'; text++, p++) {
140 int matched, special;
141 uchar t_ch, prev_ch;
142+ if (ignore_case && ISUPPER(p_ch))
143+ p_ch = tolower(p_ch);
66e2df0e
WD
144 while ((t_ch = *text) == '\0') {
145 if (*a == NULL) {
146 if (p_ch != '*')
cc3e685d 147@@ -237,12 +241,21 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
6100b16e
WD
148 * of "text" and any strings in array "a". */
149 static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
150 {
151+ uchar s_ch, t_ch;
152 for ( ; *s != '\0'; text++, s++) {
153 while (*text == '\0') {
154 if ((text = *a++) == NULL)
155 return FALSE;
156 }
157- if (*text != *s)
158+ s_ch = *s;
159+ t_ch = *text;
160+ if (ignore_case) {
161+ if (ISUPPER(s_ch))
162+ s_ch = tolower(s_ch);
163+ if (ISUPPER(t_ch))
164+ t_ch = tolower(t_ch);
165+ }
166+ if (t_ch != s_ch)
167 return FALSE;
168 }
169
cc3e685d 170@@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
ffadcb36
WD
171 int wildmatch(const char *pattern, const char *text)
172 {
66e2df0e 173 static const uchar *nomore[1]; /* A NULL pointer. */
ffadcb36
WD
174+ int ret;
175 #ifdef WILD_TEST_ITERATIONS
176 wildmatch_iteration_count = 0;
177 #endif
66e2df0e 178- return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
ffadcb36 179+ force_lower_case = ignore_case;
66e2df0e 180+ ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
ffadcb36 181+ force_lower_case = 0;
66e2df0e 182+ return ret;
ffadcb36
WD
183 }
184
185 /* Match the "pattern" against the forced-to-lower-case "text" string. */
cc3e685d 186@@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
c05e712e
WD
187 if (!text)
188 return FALSE;
189
190+ force_lower_case = ignore_case;
191+
192 if ((matched = dowild(p, text, a)) != TRUE && where < 0
193 && matched != ABORT_ALL) {
194 while (1) {
195 if (*text == '\0') {
196 if ((text = (uchar*)*a++) == NULL)
197- return FALSE;
198+ break;
199 continue;
200 }
201 if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
cc3e685d 202@@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
c05e712e
WD
203 break;
204 }
205 }
206+
207+ force_lower_case = 0;
208+
209 return matched == TRUE;
210 }
211
cc3e685d
WD
212diff --git a/options.c b/options.c
213--- a/options.c
214+++ b/options.c
fc557362 215@@ -117,6 +117,7 @@ OFF_T max_size = 0;
56208f7b 216 OFF_T min_size = 0;
5f7bb027
WD
217 int ignore_errors = 0;
218 int modify_window = 0;
219+int ignore_case = 0;
220 int blocking_io = -1;
221 int checksum_seed = 0;
f6c3b300 222 int inplace = 0;
72e5645e 223@@ -760,6 +761,7 @@ void usage(enum logcode F)
a7219d20 224 rprintf(F," --files-from=FILE read list of source-file names from FILE\n");
896871f8 225 rprintf(F," -0, --from0 all *-from/filter files are delimited by 0s\n");
790ba11a 226 rprintf(F," -s, --protect-args no space-splitting; only wildcard special-chars\n");
5befb079
WD
227+ rprintf(F," --ignore-case ignore case when comparing filenames\n");
228 rprintf(F," --address=ADDRESS bind address for outgoing socket to daemon\n");
229 rprintf(F," --port=PORT specify double-colon alternate port number\n");
230 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
72e5645e 231@@ -974,6 +976,8 @@ static struct poptOption long_options[] = {
6cbbe66d
WD
232 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
233 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
edea1111 234 {"only-write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
2c20ee4e
WD
235+ {"ignore-case", 0, POPT_ARG_VAL, &ignore_case, 1, 0, 0 },
236+ {"no-ignore-case", 0, POPT_ARG_VAL, &ignore_case, 0, 0, 0 },
6cbbe66d
WD
237 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
238 {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0},
239 {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0},
c1ff70aa 240@@ -2557,6 +2561,9 @@ void server_options(char **args, int *argc_p)
5f7bb027
WD
241 args[ac++] = arg;
242 }
7628f156 243
5f7bb027
WD
244+ if (ignore_case)
245+ args[ac++] = "--ignore-case";
7628f156 246+
afbebe13 247 if (partial_dir && am_sender) {
9a7eef96 248 if (partial_dir != tmp_partialdir) {
27a7053c 249 args[ac++] = "--partial-dir";
cc3e685d
WD
250diff --git a/rsync.yo b/rsync.yo
251--- a/rsync.yo
252+++ b/rsync.yo
fc557362 253@@ -414,6 +414,7 @@ to the detailed description below for a complete description. verb(
5befb079
WD
254 --files-from=FILE read list of source-file names from FILE
255 -0, --from0 all *from/filter files are delimited by 0s
256 -s, --protect-args no space-splitting; wildcard chars only
257+ --ignore-case ignore case when comparing filenames
258 --address=ADDRESS bind address for outgoing socket to daemon
259 --port=PORT specify double-colon alternate port number
260 --sockopts=OPTIONS specify custom TCP options
c1ff70aa
WD
261@@ -1598,6 +1599,10 @@ default (with is overridden by both the environment and the command-line).
262 This option will eventually become a new default setting at some
263 as-yet-undetermined point in the future.
5befb079
WD
264
265+dit(bf(--ignore-case)) This option tells rsync to ignore upper-/lower-case
266+differences when comparing filenames. This can avoid problems when sending
267+files to a filesystem that ignores these differences.
268+
269 dit(bf(-T, --temp-dir=DIR)) This option instructs rsync to use DIR as a
270 scratch directory when creating temporary copies of the files transferred
271 on the receiving side. The default behavior is to create each temporary
cc3e685d
WD
272diff --git a/wildtest.c b/wildtest.c
273--- a/wildtest.c
274+++ b/wildtest.c
fc557362
WD
275@@ -30,6 +30,7 @@
276 int fnmatch_errors = 0;
992633a5
WD
277 #endif
278
992633a5 279+int ignore_case = 0;
fc557362
WD
280 int wildmatch_errors = 0;
281 char number_separator = ',';
5f7bb027 282