Moved some misplaced code.
[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 @@ -32,6 +32,7 @@ extern int io_error;
18  extern int local_server;
19  extern int prune_empty_dirs;
20  extern int ignore_perishable;
21 +extern int ignore_case;
22  extern int delete_mode;
23  extern int delete_excluded;
24  extern int cvs_exclude;
25 @@ -583,16 +584,17 @@ 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 ((ignore_case ? strcasecmp(name, pattern)
31 +                                : strcmp(name, pattern)) == 0)
32                         return ret_match;
33         } else {
34                 int l1 = strlen(name);
35                 int l2 = strlen(pattern);
36 -               if (l2 <= l1 &&
37 -                   strcmp(name+(l1-l2),pattern) == 0 &&
38 -                   (l1==l2 || name[l1-(l2+1)] == '/')) {
39 +               if (l2 <= l1
40 +                && (ignore_case ? strcasecmp(name + (l1-l2), pattern)
41 +                                : strcmp(name + (l1-l2), pattern)) == 0
42 +                && (l1 == l2 || name[l1 - (l2+1)] == '/'))
43                         return ret_match;
44 -               }
45         }
46  
47         return !ret_match;
48 --- old/flist.c
49 +++ new/flist.c
50 @@ -35,6 +35,7 @@ extern int inc_recurse;
51  extern int do_progress;
52  extern int always_checksum;
53  extern int module_id;
54 +extern int ignore_case;
55  extern int ignore_errors;
56  extern int numeric_ids;
57  extern int recurse;
58 @@ -2494,6 +2495,7 @@ int f_name_cmp(const struct file_struct 
59  {
60         int dif;
61         const uchar *c1, *c2;
62 +       uchar ch1, ch2;
63         enum fnc_state state1, state2;
64         enum fnc_type type1, type2;
65         enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
66 @@ -2604,7 +2606,15 @@ int f_name_cmp(const struct file_struct 
67                         if (type1 != type2)
68                                 return type1 == t_PATH ? 1 : -1;
69                 }
70 -       } while ((dif = (int)*c1++ - (int)*c2++) == 0);
71 +               ch1 = CVAL(c1++, 0);
72 +               ch2 = CVAL(c2++, 0);
73 +               if (ignore_case) {
74 +                       if (islower(ch1))
75 +                               ch1 = toupper(ch1);
76 +                       if (islower(ch2))
77 +                               ch2 = toupper(ch2);
78 +               }
79 +       } while ((dif = ch1 - ch2) == 0);
80  
81         return dif;
82  }
83 --- old/lib/wildmatch.c
84 +++ new/lib/wildmatch.c
85 @@ -53,6 +53,8 @@
86  #define ISUPPER(c) (ISASCII(c) && isupper(c))
87  #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
88  
89 +extern int ignore_case;
90 +
91  #ifdef WILD_TEST_ITERATIONS
92  int wildmatch_iteration_count;
93  #endif
94 @@ -72,6 +74,8 @@ static int dowild(const uchar *p, const 
95      for ( ; (p_ch = *p) != '\0'; text++, p++) {
96         int matched, special;
97         uchar t_ch, prev_ch;
98 +       if (ignore_case && ISUPPER(p_ch))
99 +           p_ch = tolower(p_ch);
100         while ((t_ch = *text) == '\0') {
101             if (*a == NULL) {
102                 if (p_ch != '*')
103 @@ -237,12 +241,21 @@ static int dowild(const uchar *p, const 
104   * of "text" and any strings in array "a". */
105  static int doliteral(const uchar *s, const uchar *text, const uchar*const *a)
106  {
107 +    uchar s_ch, t_ch;
108      for ( ; *s != '\0'; text++, s++) {
109         while (*text == '\0') {
110             if ((text = *a++) == NULL)
111                 return FALSE;
112         }
113 -       if (*text != *s)
114 +       s_ch = *s;
115 +       t_ch = *text;
116 +       if (ignore_case) {
117 +           if (ISUPPER(s_ch))
118 +               s_ch = tolower(s_ch);
119 +           if (ISUPPER(t_ch))
120 +               t_ch = tolower(t_ch);
121 +       }
122 +       if (t_ch != s_ch)
123             return FALSE;
124      }
125  
126 @@ -288,10 +301,14 @@ static const uchar *trailing_N_elements(
127  int wildmatch(const char *pattern, const char *text)
128  {
129      static const uchar *nomore[1]; /* A NULL pointer. */
130 +    int ret;
131  #ifdef WILD_TEST_ITERATIONS
132      wildmatch_iteration_count = 0;
133  #endif
134 -    return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
135 +    force_lower_case = ignore_case;
136 +    ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE;
137 +    force_lower_case = 0;
138 +    return ret;
139  }
140  
141  /* Match the "pattern" against the forced-to-lower-case "text" string. */
142 @@ -331,12 +348,14 @@ int wildmatch_array(const char *pattern,
143      if (!text)
144         return FALSE;
145  
146 +    force_lower_case = ignore_case;
147 +
148      if ((matched = dowild(p, text, a)) != TRUE && where < 0
149       && matched != ABORT_ALL) {
150         while (1) {
151             if (*text == '\0') {
152                 if ((text = (uchar*)*a++) == NULL)
153 -                   return FALSE;
154 +                   break;
155                 continue;
156             }
157             if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE
158 @@ -344,6 +363,9 @@ int wildmatch_array(const char *pattern,
159                 break;
160         }
161      }
162 +
163 +    force_lower_case = 0;
164 +
165      return matched == TRUE;
166  }
167  
168 --- old/options.c
169 +++ new/options.c
170 @@ -114,6 +114,7 @@ OFF_T max_size = 0;
171  OFF_T min_size = 0;
172  int ignore_errors = 0;
173  int modify_window = 0;
174 +int ignore_case = 0;
175  int blocking_io = -1;
176  int checksum_seed = 0;
177  int inplace = 0;
178 @@ -396,6 +397,7 @@ void usage(enum logcode F)
179    rprintf(F,"     --exclude-from=FILE     read exclude patterns from FILE\n");
180    rprintf(F,"     --include=PATTERN       don't exclude files matching PATTERN\n");
181    rprintf(F,"     --include-from=FILE     read include patterns from FILE\n");
182 +  rprintf(F,"     --ignore-case           ignore case when comparing filenames\n");
183    rprintf(F,"     --files-from=FILE       read list of source-file names from FILE\n");
184    rprintf(F," -0, --from0                 all *-from/filter files are delimited by 0s\n");
185    rprintf(F," -s, --protect-args          no space-splitting; only wildcard special-chars\n");
186 @@ -593,6 +595,8 @@ static struct poptOption long_options[] 
187    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
188    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
189    {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
190 +  {"ignore-case",      0,  POPT_ARG_VAL,    &ignore_case, 1, 0, 0 },
191 +  {"no-ignore-case",   0,  POPT_ARG_VAL,    &ignore_case, 0, 0, 0 },
192    {"files-from",       0,  POPT_ARG_STRING, &files_from, 0, 0, 0 },
193    {"from0",           '0', POPT_ARG_VAL,    &eol_nulls, 1, 0, 0},
194    {"no-from0",         0,  POPT_ARG_VAL,    &eol_nulls, 0, 0, 0},
195 @@ -1907,6 +1911,9 @@ void server_options(char **args, int *ar
196                 args[ac++] = arg;
197         }
198  
199 +       if (ignore_case)
200 +               args[ac++] = "--ignore-case";
201 +
202         if (partial_dir && am_sender) {
203                 if (partial_dir != tmp_partialdir) {
204                         args[ac++] = "--partial-dir";
205 --- old/wildtest.c
206 +++ new/wildtest.c
207 @@ -31,6 +31,7 @@ int fnmatch_errors = 0;
208  #endif
209  
210  int wildmatch_errors = 0;
211 +int ignore_case = 0;
212  
213  typedef char bool;
214