Updated patches to work with the current trunk.
[rsync/rsync-patches.git] / ignore-case.diff
1 This adds the --ignore-case option, which makes rsync compare filenames
2 in a case-insensitive manner.
3
4 To 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
10 TODO:
11
12  - Make this code handle multibyte character encodings, and honor the
13    --iconv setting when converting case.
14
15 diff --git a/exclude.c b/exclude.c
16 index 5fa6e00..5b3b135 100644
17 --- a/exclude.c
18 +++ b/exclude.c
19 @@ -21,6 +21,7 @@
20   */
21  
22  #include "rsync.h"
23 +#include "ifuncs.h"
24  
25  extern int am_server;
26  extern int am_sender;
27 @@ -697,16 +698,15 @@ static int rule_matches(const char *fname, struct filter_struct *ex, int name_is
28                 if (litmatch_array(pattern, strings, slash_handling))
29                         return ret_match;
30         } else if (anchored_match) {
31 -               if (strcmp(name, pattern) == 0)
32 +               if (ic_strEQ(name, pattern))
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
41 +                && ic_strEQ(name + (l1-l2), pattern)
42 +                && (l1 == l2 || name[l1 - (l2+1)] == '/'))
43                         return ret_match;
44 -               }
45         }
46  
47         return !ret_match;
48 diff --git a/flist.c b/flist.c
49 index 09b4fc5..ae70300 100644
50 --- a/flist.c
51 +++ b/flist.c
52 @@ -34,6 +34,7 @@ extern int am_generator;
53  extern int inc_recurse;
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;
60 @@ -2924,6 +2925,7 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
61  {
62         int dif;
63         const uchar *c1, *c2;
64 +       uchar ch1, ch2;
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;
68 @@ -3034,7 +3036,15 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
69                         if (type1 != type2)
70                                 return type1 == t_PATH ? 1 : -1;
71                 }
72 -       } while ((dif = (int)*c1++ - (int)*c2++) == 0);
73 +               ch1 = *c1++;
74 +               ch2 = *c2++;
75 +               if (ignore_case) {
76 +                       if (isupper(ch1))
77 +                               ch1 = tolower(ch1);
78 +                       if (isupper(ch2))
79 +                               ch2 = tolower(ch2);
80 +               }
81 +       } while ((dif = (int)ch1 - (int)ch2) == 0);
82  
83         return dif;
84  }
85 diff --git a/ifuncs.h b/ifuncs.h
86 index 8c128d5..a4cfd3d 100644
87 --- a/ifuncs.h
88 +++ b/ifuncs.h
89 @@ -77,3 +77,38 @@ init_stat_x(stat_x *sx_p)
90         sx_p->xattr = NULL;
91  #endif
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)
120 +               return strncasecmp(s1, s2, n) == 0;
121 +       return strncmp(s1, s2, n) == 0;
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))
128 diff --git a/lib/wildmatch.c b/lib/wildmatch.c
129 index f3a1731..72660ca 100644
130 --- a/lib/wildmatch.c
131 +++ b/lib/wildmatch.c
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
141 @@ -72,6 +74,8 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
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);
147         while ((t_ch = *text) == '\0') {
148             if (*a == NULL) {
149                 if (p_ch != '*')
150 @@ -237,12 +241,21 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
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  
173 @@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
174  int wildmatch(const char *pattern, const char *text)
175  {
176      static const uchar *nomore[1]; /* A NULL pointer. */
177 +    int ret;
178  #ifdef WILD_TEST_ITERATIONS
179      wildmatch_iteration_count = 0;
180  #endif
181 -    return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
182 +    force_lower_case = ignore_case;
183 +    ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
184 +    force_lower_case = 0;
185 +    return ret;
186  }
187  
188  /* Match the "pattern" against the forced-to-lower-case "text" string. */
189 @@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
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
205 @@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
206                 break;
207         }
208      }
209 +
210 +    force_lower_case = 0;
211 +
212      return matched == TRUE;
213  }
214  
215 diff --git a/options.c b/options.c
216 index e7c6c61..6e73e9c 100644
217 --- a/options.c
218 +++ b/options.c
219 @@ -117,6 +117,7 @@ OFF_T max_size = 0;
220  OFF_T min_size = 0;
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;
226  int inplace = 0;
227 @@ -759,6 +760,7 @@ void usage(enum logcode F)
228    rprintf(F,"     --files-from=FILE       read list of source-file names from FILE\n");
229    rprintf(F," -0, --from0                 all *-from/filter files are delimited by 0s\n");
230    rprintf(F," -s, --protect-args          no space-splitting; only wildcard special-chars\n");
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");
235 @@ -973,6 +975,8 @@ static struct poptOption long_options[] = {
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 },
238    {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
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 },
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},
244 @@ -2505,6 +2509,9 @@ void server_options(char **args, int *argc_p)
245                 args[ac++] = arg;
246         }
247  
248 +       if (ignore_case)
249 +               args[ac++] = "--ignore-case";
250 +
251         if (partial_dir && am_sender) {
252                 if (partial_dir != tmp_partialdir) {
253                         args[ac++] = "--partial-dir";
254 diff --git a/rsync.yo b/rsync.yo
255 index 941f7a5..339aca8 100644
256 --- a/rsync.yo
257 +++ b/rsync.yo
258 @@ -414,6 +414,7 @@ to the detailed description below for a complete description.  verb(
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
266 @@ -1563,6 +1564,10 @@ If you use this option with bf(--iconv), the args will also be translated
267  from the local to the remote character-set.  The translation happens before
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
277 diff --git a/wildtest.c b/wildtest.c
278 index 07351a1..c899eb8 100644
279 --- a/wildtest.c
280 +++ b/wildtest.c
281 @@ -30,6 +30,7 @@
282  int fnmatch_errors = 0;
283  #endif
284  
285 +int ignore_case = 0;
286  int wildmatch_errors = 0;
287  char number_separator = ',';
288