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