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
cc3e685d 15diff --git a/exclude.c b/exclude.c
fc557362 16index 5fa6e00..5b3b135 100644
cc3e685d
WD
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
WD
26 extern int am_sender;
27@@ -697,16 +698,15 @@ static int rule_matches(const char *fname, struct filter_struct *ex, int name_is
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 48diff --git a/flist.c b/flist.c
fc557362 49index 09b4fc5..ae70300 100644
cc3e685d
WD
50--- a/flist.c
51+++ b/flist.c
fc557362
WD
52@@ -34,6 +34,7 @@ extern int am_generator;
53 extern int inc_recurse;
9c657c9f
WD
54 extern int always_checksum;
55 extern int module_id;
56+extern int ignore_case;
57 extern int ignore_errors;
58 extern int numeric_ids;
59 extern int recurse;
fc557362 60@@ -2924,6 +2925,7 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
f80ebf7e
WD
61 {
62 int dif;
7bfcb297
WD
63 const uchar *c1, *c2;
64+ uchar ch1, ch2;
f80ebf7e
WD
65 enum fnc_state state1, state2;
66 enum fnc_type type1, type2;
67 enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
fc557362 68@@ -3034,7 +3036,15 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
3f053c45
WD
69 if (type1 != type2)
70 return type1 == t_PATH ? 1 : -1;
71 }
72- } while ((dif = (int)*c1++ - (int)*c2++) == 0);
5befb079
WD
73+ ch1 = *c1++;
74+ ch2 = *c2++;
9c657c9f 75+ if (ignore_case) {
5befb079
WD
76+ if (isupper(ch1))
77+ ch1 = tolower(ch1);
78+ if (isupper(ch2))
79+ ch2 = tolower(ch2);
f80ebf7e 80+ }
5befb079 81+ } while ((dif = (int)ch1 - (int)ch2) == 0);
3f053c45
WD
82
83 return dif;
84 }
cc3e685d 85diff --git a/ifuncs.h b/ifuncs.h
fc557362 86index 8c128d5..a4cfd3d 100644
cc3e685d
WD
87--- a/ifuncs.h
88+++ b/ifuncs.h
fc557362
WD
89@@ -77,3 +77,38 @@ init_stat_x(stat_x *sx_p)
90 sx_p->xattr = NULL;
91 #endif
5befb079
WD
92 }
93+
94+static inline int
95+strEQ(const char *s1, const char *s2)
96+{
97+ return strcmp(s1, s2) == 0;
98+}
99+
100+static inline int
101+strnEQ(const char *s1, const char *s2, size_t n)
102+{
103+ return strncmp(s1, s2, n) == 0;
104+}
105+
106+static inline int
107+ic_strEQ(const char *s1, const char *s2)
108+{
109+ extern int ignore_case;
110+ if (ignore_case)
111+ return strcasecmp(s1, s2) == 0;
112+ return strcmp(s1, s2) == 0;
113+}
114+
115+static inline int
116+ic_strnEQ(const char *s1, const char *s2, size_t n)
117+{
118+ extern int ignore_case;
119+ if (ignore_case)
c82285d5
WD
120+ return strncasecmp(s1, s2, n) == 0;
121+ return strncmp(s1, s2, n) == 0;
5befb079
WD
122+}
123+
124+#define strNE(s1,s2) (!strEQ(s1,s2))
125+#define strnNE(s1,s2,n) (!strnEQ(s1,s2,n))
126+#define ic_strNE(s1,s2) (!ic_strEQ(s1,s2))
127+#define ic_strnNE(s1,s2) (!ic_strnEQ(s1,s2,n))
cc3e685d 128diff --git a/lib/wildmatch.c b/lib/wildmatch.c
fc557362 129index f3a1731..72660ca 100644
cc3e685d
WD
130--- a/lib/wildmatch.c
131+++ b/lib/wildmatch.c
992633a5
WD
132@@ -53,6 +53,8 @@
133 #define ISUPPER(c) (ISASCII(c) && isupper(c))
134 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
135
136+extern int ignore_case;
137+
138 #ifdef WILD_TEST_ITERATIONS
139 int wildmatch_iteration_count;
140 #endif
cc3e685d 141@@ -72,6 +74,8 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
ffadcb36
WD
142 for ( ; (p_ch = *p) != '\0'; text++, p++) {
143 int matched, special;
144 uchar t_ch, prev_ch;
145+ if (ignore_case && ISUPPER(p_ch))
146+ p_ch = tolower(p_ch);
66e2df0e
WD
147 while ((t_ch = *text) == '\0') {
148 if (*a == NULL) {
149 if (p_ch != '*')
cc3e685d 150@@ -237,12 +241,21 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
6100b16e
WD
151 * of "text" and any strings in array "a". */
152 static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
153 {
154+ uchar s_ch, t_ch;
155 for ( ; *s != '\0'; text++, s++) {
156 while (*text == '\0') {
157 if ((text = *a++) == NULL)
158 return FALSE;
159 }
160- if (*text != *s)
161+ s_ch = *s;
162+ t_ch = *text;
163+ if (ignore_case) {
164+ if (ISUPPER(s_ch))
165+ s_ch = tolower(s_ch);
166+ if (ISUPPER(t_ch))
167+ t_ch = tolower(t_ch);
168+ }
169+ if (t_ch != s_ch)
170 return FALSE;
171 }
172
cc3e685d 173@@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
ffadcb36
WD
174 int wildmatch(const char *pattern, const char *text)
175 {
66e2df0e 176 static const uchar *nomore[1]; /* A NULL pointer. */
ffadcb36
WD
177+ int ret;
178 #ifdef WILD_TEST_ITERATIONS
179 wildmatch_iteration_count = 0;
180 #endif
66e2df0e 181- return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
ffadcb36 182+ force_lower_case = ignore_case;
66e2df0e 183+ ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
ffadcb36 184+ force_lower_case = 0;
66e2df0e 185+ return ret;
ffadcb36
WD
186 }
187
188 /* Match the "pattern" against the forced-to-lower-case "text" string. */
cc3e685d 189@@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
c05e712e
WD
190 if (!text)
191 return FALSE;
192
193+ force_lower_case = ignore_case;
194+
195 if ((matched = dowild(p, text, a)) != TRUE && where < 0
196 && matched != ABORT_ALL) {
197 while (1) {
198 if (*text == '\0') {
199 if ((text = (uchar*)*a++) == NULL)
200- return FALSE;
201+ break;
202 continue;
203 }
204 if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
cc3e685d 205@@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
c05e712e
WD
206 break;
207 }
208 }
209+
210+ force_lower_case = 0;
211+
212 return matched == TRUE;
213 }
214
cc3e685d 215diff --git a/options.c b/options.c
fc557362 216index e7c6c61..6e73e9c 100644
cc3e685d
WD
217--- a/options.c
218+++ b/options.c
fc557362 219@@ -117,6 +117,7 @@ OFF_T max_size = 0;
56208f7b 220 OFF_T min_size = 0;
5f7bb027
WD
221 int ignore_errors = 0;
222 int modify_window = 0;
223+int ignore_case = 0;
224 int blocking_io = -1;
225 int checksum_seed = 0;
f6c3b300 226 int inplace = 0;
fc557362 227@@ -759,6 +760,7 @@ void usage(enum logcode F)
a7219d20 228 rprintf(F," --files-from=FILE read list of source-file names from FILE\n");
896871f8 229 rprintf(F," -0, --from0 all *-from/filter files are delimited by 0s\n");
790ba11a 230 rprintf(F," -s, --protect-args no space-splitting; only wildcard special-chars\n");
5befb079
WD
231+ rprintf(F," --ignore-case ignore case when comparing filenames\n");
232 rprintf(F," --address=ADDRESS bind address for outgoing socket to daemon\n");
233 rprintf(F," --port=PORT specify double-colon alternate port number\n");
234 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
fc557362 235@@ -973,6 +975,8 @@ static struct poptOption long_options[] = {
6cbbe66d
WD
236 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
237 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
edea1111 238 {"only-write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
2c20ee4e
WD
239+ {"ignore-case", 0, POPT_ARG_VAL, &ignore_case, 1, 0, 0 },
240+ {"no-ignore-case", 0, POPT_ARG_VAL, &ignore_case, 0, 0, 0 },
6cbbe66d
WD
241 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
242 {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0},
243 {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0},
fc557362 244@@ -2505,6 +2509,9 @@ void server_options(char **args, int *argc_p)
5f7bb027
WD
245 args[ac++] = arg;
246 }
7628f156 247
5f7bb027
WD
248+ if (ignore_case)
249+ args[ac++] = "--ignore-case";
7628f156 250+
afbebe13 251 if (partial_dir && am_sender) {
9a7eef96 252 if (partial_dir != tmp_partialdir) {
27a7053c 253 args[ac++] = "--partial-dir";
cc3e685d 254diff --git a/rsync.yo b/rsync.yo
fc557362 255index 941f7a5..339aca8 100644
cc3e685d
WD
256--- a/rsync.yo
257+++ b/rsync.yo
fc557362 258@@ -414,6 +414,7 @@ to the detailed description below for a complete description. verb(
5befb079
WD
259 --files-from=FILE read list of source-file names from FILE
260 -0, --from0 all *from/filter files are delimited by 0s
261 -s, --protect-args no space-splitting; wildcard chars only
262+ --ignore-case ignore case when comparing filenames
263 --address=ADDRESS bind address for outgoing socket to daemon
264 --port=PORT specify double-colon alternate port number
265 --sockopts=OPTIONS specify custom TCP options
fc557362 266@@ -1563,6 +1564,10 @@ If you use this option with bf(--iconv), the args will also be translated
85096e5e 267 from the local to the remote character-set. The translation happens before
5befb079
WD
268 wild-cards are expanded. See also the bf(--files-from) option.
269
270+dit(bf(--ignore-case)) This option tells rsync to ignore upper-/lower-case
271+differences when comparing filenames. This can avoid problems when sending
272+files to a filesystem that ignores these differences.
273+
274 dit(bf(-T, --temp-dir=DIR)) This option instructs rsync to use DIR as a
275 scratch directory when creating temporary copies of the files transferred
276 on the receiving side. The default behavior is to create each temporary
cc3e685d 277diff --git a/wildtest.c b/wildtest.c
fc557362 278index 07351a1..c899eb8 100644
cc3e685d
WD
279--- a/wildtest.c
280+++ b/wildtest.c
fc557362
WD
281@@ -30,6 +30,7 @@
282 int fnmatch_errors = 0;
992633a5
WD
283 #endif
284
992633a5 285+int ignore_case = 0;
fc557362
WD
286 int wildmatch_errors = 0;
287 char number_separator = ',';
5f7bb027 288