81c28b2b8f1093112fc6addb0218da3c5ffc2b65
[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 --- old/exclude.c
16 +++ new/exclude.c
17 @@ -21,6 +21,7 @@
18   */
19  
20  #include "rsync.h"
21 +#include "ifuncs.h"
22  
23  extern int verbose;
24  extern int am_server;
25 @@ -583,16 +584,15 @@ static int rule_matches(char *name, stru
26                 if (litmatch_array(pattern, strings, slash_handling))
27                         return ret_match;
28         } else if (anchored_match) {
29 -               if (strcmp(name, pattern) == 0)
30 +               if (ic_strEQ(name, pattern))
31                         return ret_match;
32         } else {
33                 int l1 = strlen(name);
34                 int l2 = strlen(pattern);
35 -               if (l2 <= l1 &&
36 -                   strcmp(name+(l1-l2),pattern) == 0 &&
37 -                   (l1==l2 || name[l1-(l2+1)] == '/')) {
38 +               if (l2 <= l1
39 +                && ic_strEQ(name + (l1-l2), pattern)
40 +                && (l1 == l2 || name[l1 - (l2+1)] == '/'))
41                         return ret_match;
42 -               }
43         }
44  
45         return !ret_match;
46 --- old/flist.c
47 +++ new/flist.c
48 @@ -35,6 +35,7 @@ extern int inc_recurse;
49  extern int do_progress;
50  extern int always_checksum;
51  extern int module_id;
52 +extern int ignore_case;
53  extern int ignore_errors;
54  extern int numeric_ids;
55  extern int recurse;
56 @@ -2570,6 +2571,7 @@ int f_name_cmp(const struct file_struct 
57  {
58         int dif;
59         const uchar *c1, *c2;
60 +       uchar ch1, ch2;
61         enum fnc_state state1, state2;
62         enum fnc_type type1, type2;
63         enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
64 @@ -2680,7 +2682,15 @@ int f_name_cmp(const struct file_struct 
65                         if (type1 != type2)
66                                 return type1 == t_PATH ? 1 : -1;
67                 }
68 -       } while ((dif = (int)*c1++ - (int)*c2++) == 0);
69 +               ch1 = *c1++;
70 +               ch2 = *c2++;
71 +               if (ignore_case) {
72 +                       if (isupper(ch1))
73 +                               ch1 = tolower(ch1);
74 +                       if (isupper(ch2))
75 +                               ch2 = tolower(ch2);
76 +               }
77 +       } while ((dif = (int)ch1 - (int)ch2) == 0);
78  
79         return dif;
80  }
81 --- old/ifuncs.h
82 +++ new/ifuncs.h
83 @@ -98,3 +98,38 @@ toUpper(const char *ptr)
84  {
85         return toupper(*(unsigned char *)ptr);
86  }
87 +
88 +static inline int
89 +strEQ(const char *s1, const char *s2)
90 +{
91 +       return strcmp(s1, s2) == 0;
92 +}
93 +
94 +static inline int
95 +strnEQ(const char *s1, const char *s2, size_t n)
96 +{
97 +       return strncmp(s1, s2, n) == 0;
98 +}
99 +
100 +static inline int
101 +ic_strEQ(const char *s1, const char *s2)
102 +{
103 +       extern int ignore_case;
104 +       if (ignore_case)
105 +               return strcasecmp(s1, s2) == 0;
106 +       return strcmp(s1, s2) == 0;
107 +}
108 +
109 +static inline int
110 +ic_strnEQ(const char *s1, const char *s2, size_t n)
111 +{
112 +       extern int ignore_case;
113 +       if (ignore_case)
114 +               return strncasecmp(s1, s2, n) == 0;
115 +       return strncmp(s1, s2, n) == 0;
116 +}
117 +
118 +#define strNE(s1,s2) (!strEQ(s1,s2))
119 +#define strnNE(s1,s2,n) (!strnEQ(s1,s2,n))
120 +#define ic_strNE(s1,s2) (!ic_strEQ(s1,s2))
121 +#define ic_strnNE(s1,s2) (!ic_strnEQ(s1,s2,n))
122 --- old/lib/wildmatch.c
123 +++ new/lib/wildmatch.c
124 @@ -53,6 +53,8 @@
125  #define ISUPPER(c) (ISASCII(c) && isupper(c))
126  #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
127  
128 +extern int ignore_case;
129 +
130  #ifdef WILD_TEST_ITERATIONS
131  int wildmatch_iteration_count;
132  #endif
133 @@ -72,6 +74,8 @@ static int dowild(const uchar *p, const 
134      for ( ; (p_ch = *p) != '\0'; text++, p++) {
135         int matched, special;
136         uchar t_ch, prev_ch;
137 +       if (ignore_case && ISUPPER(p_ch))
138 +           p_ch = tolower(p_ch);
139         while ((t_ch = *text) == '\0') {
140             if (*a == NULL) {
141                 if (p_ch != '*')
142 @@ -237,12 +241,21 @@ static int dowild(const uchar *p, const 
143   * of "text" and any strings in array "a". */
144  static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
145  {
146 +    uchar s_ch, t_ch;
147      for ( ; *s != '\0'; text++, s++) {
148         while (*text == '\0') {
149             if ((text = *a++) == NULL)
150                 return FALSE;
151         }
152 -       if (*text != *s)
153 +       s_ch = *s;
154 +       t_ch = *text;
155 +       if (ignore_case) {
156 +           if (ISUPPER(s_ch))
157 +               s_ch = tolower(s_ch);
158 +           if (ISUPPER(t_ch))
159 +               t_ch = tolower(t_ch);
160 +       }
161 +       if (t_ch != s_ch)
162             return FALSE;
163      }
164  
165 @@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(
166  int wildmatch(const char *pattern, const char *text)
167  {
168      static const uchar *nomore[1]; /* A NULL pointer. */
169 +    int ret;
170  #ifdef WILD_TEST_ITERATIONS
171      wildmatch_iteration_count = 0;
172  #endif
173 -    return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
174 +    force_lower_case = ignore_case;
175 +    ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
176 +    force_lower_case = 0;
177 +    return ret;
178  }
179  
180  /* Match the "pattern" against the forced-to-lower-case "text" string. */
181 @@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern,
182      if (!text)
183         return FALSE;
184  
185 +    force_lower_case = ignore_case;
186 +
187      if ((matched = dowild(p, text, a)) != TRUE && where < 0
188       && matched != ABORT_ALL) {
189         while (1) {
190             if (*text == '\0') {
191                 if ((text = (uchar*)*a++) == NULL)
192 -                   return FALSE;
193 +                   break;
194                 continue;
195             }
196             if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
197 @@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern,
198                 break;
199         }
200      }
201 +
202 +    force_lower_case = 0;
203 +
204      return matched == TRUE;
205  }
206  
207 --- old/options.c
208 +++ new/options.c
209 @@ -115,6 +115,7 @@ OFF_T max_size = 0;
210  OFF_T min_size = 0;
211  int ignore_errors = 0;
212  int modify_window = 0;
213 +int ignore_case = 0;
214  int blocking_io = -1;
215  int checksum_seed = 0;
216  int inplace = 0;
217 @@ -400,6 +401,7 @@ void usage(enum logcode F)
218    rprintf(F,"     --files-from=FILE       read list of source-file names from FILE\n");
219    rprintf(F," -0, --from0                 all *-from/filter files are delimited by 0s\n");
220    rprintf(F," -s, --protect-args          no space-splitting; only wildcard special-chars\n");
221 +  rprintf(F,"     --ignore-case           ignore case when comparing filenames\n");
222    rprintf(F,"     --address=ADDRESS       bind address for outgoing socket to daemon\n");
223    rprintf(F,"     --port=PORT             specify double-colon alternate port number\n");
224    rprintf(F,"     --sockopts=OPTIONS      specify custom TCP options\n");
225 @@ -593,6 +595,8 @@ static struct poptOption long_options[] 
226    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
227    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
228    {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
229 +  {"ignore-case",      0,  POPT_ARG_VAL,    &ignore_case, 1, 0, 0 },
230 +  {"no-ignore-case",   0,  POPT_ARG_VAL,    &ignore_case, 0, 0, 0 },
231    {"files-from",       0,  POPT_ARG_STRING, &files_from, 0, 0, 0 },
232    {"from0",           '0', POPT_ARG_VAL,    &eol_nulls, 1, 0, 0},
233    {"no-from0",         0,  POPT_ARG_VAL,    &eol_nulls, 0, 0, 0},
234 @@ -1909,6 +1913,9 @@ void server_options(char **args, int *ar
235                 args[ac++] = arg;
236         }
237  
238 +       if (ignore_case)
239 +               args[ac++] = "--ignore-case";
240 +
241         if (partial_dir && am_sender) {
242                 if (partial_dir != tmp_partialdir) {
243                         args[ac++] = "--partial-dir";
244 --- old/rsync.yo
245 +++ new/rsync.yo
246 @@ -402,6 +402,7 @@ to the detailed description below for a 
247       --files-from=FILE       read list of source-file names from FILE
248   -0, --from0                 all *from/filter files are delimited by 0s
249   -s, --protect-args          no space-splitting; wildcard chars only
250 +     --ignore-case           ignore case when comparing filenames
251       --address=ADDRESS       bind address for outgoing socket to daemon
252       --port=PORT             specify double-colon alternate port number
253       --sockopts=OPTIONS      specify custom TCP options
254 @@ -1383,6 +1384,10 @@ If you use this option with bf(--iconv),
255  from the local to the remote character set.  The translation happens before
256  wild-cards are expanded.  See also the bf(--files-from) option.
257  
258 +dit(bf(--ignore-case)) This option tells rsync to ignore upper-/lower-case
259 +differences when comparing filenames.  This can avoid problems when sending
260 +files to a filesystem that ignores these differences.
261 +
262  dit(bf(-T, --temp-dir=DIR)) This option instructs rsync to use DIR as a
263  scratch directory when creating temporary copies of the files transferred
264  on the receiving side.  The default behavior is to create each temporary
265 --- old/wildtest.c
266 +++ new/wildtest.c
267 @@ -31,6 +31,7 @@ int fnmatch_errors = 0;
268  #endif
269  
270  int wildmatch_errors = 0;
271 +int ignore_case = 0;
272  
273  typedef char bool;
274