Applied to the main code as option --outbuf=N|L|B.
[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: 7c8f180900432e646c0a4bd02e2c4033068dbb7c
16 diff --git a/exclude.c b/exclude.c
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 @@ -687,16 +688,15 @@ static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
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 --- a/flist.c
50 +++ b/flist.c
51 @@ -34,6 +34,7 @@ extern int am_generator;
52  extern int inc_recurse;
53  extern int always_checksum;
54  extern int module_id;
55 +extern int ignore_case;
56  extern int ignore_errors;
57  extern int numeric_ids;
58  extern int recurse;
59 @@ -3012,6 +3013,7 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
60  {
61         int dif;
62         const uchar *c1, *c2;
63 +       uchar ch1, ch2;
64         enum fnc_state state1, state2;
65         enum fnc_type type1, type2;
66         enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
67 @@ -3122,7 +3124,15 @@ int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
68                         if (type1 != type2)
69                                 return type1 == t_PATH ? 1 : -1;
70                 }
71 -       } while ((dif = (int)*c1++ - (int)*c2++) == 0);
72 +               ch1 = *c1++;
73 +               ch2 = *c2++;
74 +               if (ignore_case) {
75 +                       if (isupper(ch1))
76 +                               ch1 = tolower(ch1);
77 +                       if (isupper(ch2))
78 +                               ch2 = tolower(ch2);
79 +               }
80 +       } while ((dif = (int)ch1 - (int)ch2) == 0);
81  
82         return dif;
83  }
84 diff --git a/ifuncs.h b/ifuncs.h
85 --- a/ifuncs.h
86 +++ b/ifuncs.h
87 @@ -85,3 +85,38 @@ init_stat_x(stat_x *sx_p)
88         sx_p->xattr = NULL;
89  #endif
90  }
91 +
92 +static inline int
93 +strEQ(const char *s1, const char *s2)
94 +{
95 +       return strcmp(s1, s2) == 0;
96 +}
97 +
98 +static inline int
99 +strnEQ(const char *s1, const char *s2, size_t n)
100 +{
101 +       return strncmp(s1, s2, n) == 0;
102 +}
103 +
104 +static inline int
105 +ic_strEQ(const char *s1, const char *s2)
106 +{
107 +       extern int ignore_case;
108 +       if (ignore_case)
109 +               return strcasecmp(s1, s2) == 0;
110 +       return strcmp(s1, s2) == 0;
111 +}
112 +
113 +static inline int
114 +ic_strnEQ(const char *s1, const char *s2, size_t n)
115 +{
116 +       extern int ignore_case;
117 +       if (ignore_case)
118 +               return strncasecmp(s1, s2, n) == 0;
119 +       return strncmp(s1, s2, n) == 0;
120 +}
121 +
122 +#define strNE(s1,s2) (!strEQ(s1,s2))
123 +#define strnNE(s1,s2,n) (!strnEQ(s1,s2,n))
124 +#define ic_strNE(s1,s2) (!ic_strEQ(s1,s2))
125 +#define ic_strnNE(s1,s2) (!ic_strnEQ(s1,s2,n))
126 diff --git a/lib/wildmatch.c b/lib/wildmatch.c
127 --- a/lib/wildmatch.c
128 +++ b/lib/wildmatch.c
129 @@ -53,6 +53,8 @@
130  #define ISUPPER(c) (ISASCII(c) && isupper(c))
131  #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
132  
133 +extern int ignore_case;
134 +
135  #ifdef WILD_TEST_ITERATIONS
136  int wildmatch_iteration_count;
137  #endif
138 @@ -72,6 +74,8 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
139      for ( ; (p_ch = *p) != '\0'; text++, p++) {
140         int matched, special;
141         uchar t_ch, prev_ch;
142 +       if (ignore_case && ISUPPER(p_ch))
143 +           p_ch = tolower(p_ch);
144         while ((t_ch = *text) == '\0') {
145             if (*a == NULL) {
146                 if (p_ch != '*')
147 @@ -237,12 +241,21 @@ static int dowild(const uchar *p, const uchar *text, const uchar*const *a)
148   * of "text" and any strings in array "a". */
149  static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
150  {
151 +    uchar s_ch, t_ch;
152      for ( ; *s != '\0'; text++, s++) {
153         while (*text == '\0') {
154             if ((text = *a++) == NULL)
155                 return FALSE;
156         }
157 -       if (*text != *s)
158 +       s_ch = *s;
159 +       t_ch = *text;
160 +       if (ignore_case) {
161 +           if (ISUPPER(s_ch))
162 +               s_ch = tolower(s_ch);
163 +           if (ISUPPER(t_ch))
164 +               t_ch = tolower(t_ch);
165 +       }
166 +       if (t_ch != s_ch)
167             return FALSE;
168      }
169  
170 @@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count)
171  int wildmatch(const char *pattern, const char *text)
172  {
173      static const uchar *nomore[1]; /* A NULL pointer. */
174 +    int ret;
175  #ifdef WILD_TEST_ITERATIONS
176      wildmatch_iteration_count = 0;
177  #endif
178 -    return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
179 +    force_lower_case = ignore_case;
180 +    ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
181 +    force_lower_case = 0;
182 +    return ret;
183  }
184  
185  /* Match the "pattern" against the forced-to-lower-case "text" string. */
186 @@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
187      if (!text)
188         return FALSE;
189  
190 +    force_lower_case = ignore_case;
191 +
192      if ((matched = dowild(p, text, a)) != TRUE && where < 0
193       && matched != ABORT_ALL) {
194         while (1) {
195             if (*text == '\0') {
196                 if ((text = (uchar*)*a++) == NULL)
197 -                   return FALSE;
198 +                   break;
199                 continue;
200             }
201             if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
202 @@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern, const char*const *texts, int where)
203                 break;
204         }
205      }
206 +
207 +    force_lower_case = 0;
208 +
209      return matched == TRUE;
210  }
211  
212 diff --git a/options.c b/options.c
213 --- a/options.c
214 +++ b/options.c
215 @@ -118,6 +118,7 @@ OFF_T max_size = 0;
216  OFF_T min_size = 0;
217  int ignore_errors = 0;
218  int modify_window = 0;
219 +int ignore_case = 0;
220  int blocking_io = -1;
221  int checksum_seed = 0;
222  int inplace = 0;
223 @@ -773,6 +774,7 @@ void usage(enum logcode F)
224    rprintf(F,"     --files-from=FILE       read list of source-file names from FILE\n");
225    rprintf(F," -0, --from0                 all *-from/filter files are delimited by 0s\n");
226    rprintf(F," -s, --protect-args          no space-splitting; only wildcard special-chars\n");
227 +  rprintf(F,"     --ignore-case           ignore case when comparing filenames\n");
228    rprintf(F,"     --address=ADDRESS       bind address for outgoing socket to daemon\n");
229    rprintf(F,"     --port=PORT             specify double-colon alternate port number\n");
230    rprintf(F,"     --sockopts=OPTIONS      specify custom TCP options\n");
231 @@ -991,6 +993,8 @@ static struct poptOption long_options[] = {
232    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
233    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
234    {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
235 +  {"ignore-case",      0,  POPT_ARG_VAL,    &ignore_case, 1, 0, 0 },
236 +  {"no-ignore-case",   0,  POPT_ARG_VAL,    &ignore_case, 0, 0, 0 },
237    {"files-from",       0,  POPT_ARG_STRING, &files_from, 0, 0, 0 },
238    {"from0",           '0', POPT_ARG_VAL,    &eol_nulls, 1, 0, 0},
239    {"no-from0",         0,  POPT_ARG_VAL,    &eol_nulls, 0, 0, 0},
240 @@ -2584,6 +2588,9 @@ void server_options(char **args, int *argc_p)
241                 args[ac++] = arg;
242         }
243  
244 +       if (ignore_case)
245 +               args[ac++] = "--ignore-case";
246 +
247         if (partial_dir && am_sender) {
248                 if (partial_dir != tmp_partialdir) {
249                         args[ac++] = "--partial-dir";
250 diff --git a/rsync.yo b/rsync.yo
251 --- a/rsync.yo
252 +++ b/rsync.yo
253 @@ -430,6 +430,7 @@ to the detailed description below for a complete description.  verb(
254       --files-from=FILE       read list of source-file names from FILE
255   -0, --from0                 all *from/filter files are delimited by 0s
256   -s, --protect-args          no space-splitting; wildcard chars only
257 +     --ignore-case           ignore case when comparing filenames
258       --address=ADDRESS       bind address for outgoing socket to daemon
259       --port=PORT             specify double-colon alternate port number
260       --sockopts=OPTIONS      specify custom TCP options
261 @@ -1710,6 +1711,10 @@ default (with is overridden by both the environment and the command-line).
262  This option will eventually become a new default setting at some
263  as-yet-undetermined point in the future.
264  
265 +dit(bf(--ignore-case)) This option tells rsync to ignore upper-/lower-case
266 +differences when comparing filenames.  This can avoid problems when sending
267 +files to a filesystem that ignores these differences.
268 +
269  dit(bf(-T, --temp-dir=DIR)) This option instructs rsync to use DIR as a
270  scratch directory when creating temporary copies of the files transferred
271  on the receiving side.  The default behavior is to create each temporary
272 diff --git a/wildtest.c b/wildtest.c
273 --- a/wildtest.c
274 +++ b/wildtest.c
275 @@ -30,6 +30,7 @@
276  int fnmatch_errors = 0;
277  #endif
278  
279 +int ignore_case = 0;
280  int wildmatch_errors = 0;
281  char number_separator = ',';
282