97b36b5b2162096c2aedefb6947db59343e8d407
[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 based-on: 6c8ca91c731b7bf2b081694bda85b7dadc2b7aff
16 diff --git a/exclude.c b/exclude.c
17 --- a/exclude.c
18 +++ b/exclude.c
19 @@ -966,16 +966,15 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
20                 if (litmatch_array(pattern, strings, slash_handling))
21                         return ret_match;
22         } else if (anchored_match) {
23 -               if (strcmp(name, pattern) == 0)
24 +               if (ic_strEQ(name, pattern))
25                         return ret_match;
26         } else {
27                 int l1 = strlen(name);
28                 int l2 = strlen(pattern);
29 -               if (l2 <= l1 &&
30 -                   strcmp(name+(l1-l2),pattern) == 0 &&
31 -                   (l1==l2 || name[l1-(l2+1)] == '/')) {
32 +               if (l2 <= l1
33 +                && ic_strEQ(name + (l1-l2), pattern)
34 +                && (l1 == l2 || name[l1 - (l2+1)] == '/'))
35                         return ret_match;
36 -               }
37         }
38  
39         return !ret_match;
40 diff --git a/flist.c b/flist.c
41 --- a/flist.c
42 +++ b/flist.c
43 @@ -34,6 +34,7 @@ extern int am_generator;
44  extern int inc_recurse;
45  extern int always_checksum;
46  extern int module_id;
47 +extern int ignore_case;
48  extern int ignore_errors;
49  extern int numeric_ids;
50  extern int quiet;
51 @@ -2638,7 +2639,8 @@ struct file_list *recv_file_list(int f, int dir_ndx)
52                                 cur_dir++;
53                         if (cur_dir != good_dirname) {
54                                 const char *d = dir_ndx >= 0 ? f_name(dir_flist->files[dir_ndx], NULL) : empty_dir;
55 -                               if (strcmp(cur_dir, d) != 0) {
56 +                               int dir_differs = ignore_case ? strcasecmp(cur_dir, d) : strcmp(cur_dir, d);
57 +                               if (dir_differs) {
58                                         rprintf(FERROR,
59                                                 "ABORTING due to invalid path from sender: %s/%s\n",
60                                                 cur_dir, file->basename);
61 @@ -3205,6 +3207,7 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
62  {
63         int dif;
64         const uchar *c1, *c2;
65 +       uchar ch1, ch2;
66         enum fnc_state state1, state2;
67         enum fnc_type type1, type2;
68         enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
69 @@ -3315,7 +3318,15 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
70                         if (type1 != type2)
71                                 return type1 == t_PATH ? 1 : -1;
72                 }
73 -       } while ((dif = (int)*c1++ - (int)*c2++) == 0);
74 +               ch1 = *c1++;
75 +               ch2 = *c2++;
76 +               if (ignore_case) {
77 +                       if (isupper(ch1))
78 +                               ch1 = tolower(ch1);
79 +                       if (isupper(ch2))
80 +                               ch2 = tolower(ch2);
81 +               }
82 +       } while ((dif = (int)ch1 - (int)ch2) == 0);
83  
84         return dif;
85  }
86 diff --git a/ifuncs.h b/ifuncs.h
87 --- a/ifuncs.h
88 +++ b/ifuncs.h
89 @@ -110,3 +110,38 @@ static inline char *my_strdup(const char *str, const char *file, int line)
90      memcpy(buf, str, len);
91      return buf;
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 --- a/lib/wildmatch.c
130 +++ b/lib/wildmatch.c
131 @@ -53,6 +53,8 @@
132  #define ISUPPER(c) (ISASCII(c) && isupper(c))
133  #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
134  
135 +extern int ignore_case;
136 +
137  #ifdef WILD_TEST_ITERATIONS
138  int wildmatch_iteration_count;
139  #endif
140 @@ -72,6 +74,8 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
141      for ( ; (p_ch = *p) != '\0'; text++, p++) {
142         int matched, special;
143         uchar t_ch, prev_ch;
144 +       if (ignore_case && ISUPPER(p_ch))
145 +           p_ch = tolower(p_ch);
146         while ((t_ch = *text) == '\0') {
147             if (*a == NULL) {
148                 if (p_ch != '*')
149 @@ -237,12 +241,21 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
150   * of "text" and any strings in array "a". */
151  static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
152  {
153 +    uchar s_ch, t_ch;
154      for ( ; *s != '\0'; text++, s++) {
155         while (*text == '\0') {
156             if ((text = *a++) == NULL)
157                 return FALSE;
158         }
159 -       if (*text != *s)
160 +       s_ch = *s;
161 +       t_ch = *text;
162 +       if (ignore_case) {
163 +           if (ISUPPER(s_ch))
164 +               s_ch = tolower(s_ch);
165 +           if (ISUPPER(t_ch))
166 +               t_ch = tolower(t_ch);
167 +       }
168 +       if (t_ch != s_ch)
169             return FALSE;
170      }
171  
172 @@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
173  int wildmatch(const char *pattern, const char *text)
174  {
175      static const uchar *nomore[1]; /* A NULL pointer. */
176 +    int ret;
177  #ifdef WILD_TEST_ITERATIONS
178      wildmatch_iteration_count = 0;
179  #endif
180 -    return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
181 +    force_lower_case = ignore_case;
182 +    ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
183 +    force_lower_case = 0;
184 +    return ret;
185  }
186  
187  /* Match the "pattern" against the forced-to-lower-case "text" string. */
188 @@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
189      if (!text)
190         return FALSE;
191  
192 +    force_lower_case = ignore_case;
193 +
194      if ((matched = dowild(p, text, a)) != TRUE && where < 0
195       && matched != ABORT_ALL) {
196         while (1) {
197             if (*text == '\0') {
198                 if ((text = (uchar*)*a++) == NULL)
199 -                   return FALSE;
200 +                   break;
201                 continue;
202             }
203             if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
204 @@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
205                 break;
206         }
207      }
208 +
209 +    force_lower_case = 0;
210 +
211      return matched == TRUE;
212  }
213  
214 diff --git a/options.c b/options.c
215 --- a/options.c
216 +++ b/options.c
217 @@ -131,6 +131,7 @@ OFF_T max_size = -1;
218  OFF_T min_size = -1;
219  int ignore_errors = 0;
220  int modify_window = 0;
221 +int ignore_case = 0;
222  int blocking_io = -1;
223  int checksum_seed = 0;
224  int inplace = 0;
225 @@ -784,6 +785,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 @@ -2865,6 +2868,9 @@ void server_options(char **args, int *argc_p)
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 diff --git a/rsync.1.md b/rsync.1.md
245 --- a/rsync.1.md
246 +++ b/rsync.1.md
247 @@ -528,6 +528,7 @@ has its own detailed description later in this manpage.
248  --secluded-args, -s      use the protocol to safely send the args
249  --trust-sender           trust the remote sender's file list
250  --copy-as=USER[:GROUP]   specify user & optional group for the copy
251 +--ignore-case            ignore case when comparing filenames
252  --address=ADDRESS        bind address for outgoing socket to daemon
253  --port=PORT              specify double-colon alternate port number
254  --sockopts=OPTIONS       specify custom TCP options
255 @@ -2583,6 +2584,12 @@ expand it.
256  
257      >     sudo rsync -aive lsh -M--copy-as=joe src/ lh:dest/
258  
259 +0.  `--ignore-case`
260 +
261 +    This option tells rsync to ignore upper-/lower-case differences when
262 +    comparing filenames.  This can avoid problems when sending files to a
263 +    filesystem that ignores these differences.
264 +
265  0.  `--temp-dir=DIR`, `-T`
266  
267      This option instructs rsync to use DIR as a scratch directory when creating
268 diff --git a/t_stub.c b/t_stub.c
269 --- a/t_stub.c
270 +++ b/t_stub.c
271 @@ -34,6 +34,7 @@ int preserve_perms = 0;
272  int preserve_executability = 0;
273  int omit_link_times = 0;
274  int open_noatime = 0;
275 +int ignore_case = 0;
276  size_t max_alloc = 0; /* max_alloc is needed when combined with util2.o */
277  char *partial_dir;
278  char *module_dir;
279 diff --git a/wildtest.c b/wildtest.c
280 --- a/wildtest.c
281 +++ b/wildtest.c
282 @@ -30,6 +30,7 @@
283  int fnmatch_errors = 0;
284  #endif
285  
286 +int ignore_case = 0;
287  int wildmatch_errors = 0;
288  
289  typedef char bool;