1 This patch adds an option --tr=BAD/GOOD to transliterate filenames. It
2 can be used to remove characters illegal on the destination filesystem.
3 Jeff Weber expressed interest in this:
5 http://lists.samba.org/archive/rsync/2007-October/018996.html
7 This patch is a COMPLETE HACK that covers the most common cases. Others
8 are welcome to improve it.
10 To use this patch, run these commands for a successful build:
12 patch -p1 <patches/transliterate.diff
13 ./configure (optional if already run)
18 @@ -80,6 +80,9 @@ extern int filesfrom_convert;
19 extern iconv_t ic_send, ic_recv;
22 +extern char *tr_opt, *tr_left, *tr_right;
23 +extern int tr_right_len;
25 #define PTR_SIZE (sizeof (struct file_struct *))
28 @@ -600,6 +603,24 @@ static void send_file_entry(int f, struc
29 stats.total_size += F_LENGTH(file);
32 +static void transliterate(char *thisname)
34 + char *p1, *p2, *pleft;
36 + for (p1 = p2 = thisname; *p1; p1++) {
37 + /* Look up the current character in the left string. */
38 + pleft = strchr(tr_left, *p1);
40 + /* Not found: no change. */
42 + else if (pleft - tr_left < tr_right_len)
43 + /* Store replacement from the right string. */
44 + *p2++ = tr_right[pleft - tr_left];
45 + /* Otherwise delete. */
50 static struct file_struct *recv_file_entry(struct file_list *flist,
53 @@ -668,6 +689,9 @@ static struct file_struct *recv_file_ent
58 + transliterate(thisname);
60 clean_fname(thisname, 0);
65 @@ -181,6 +181,8 @@ int logfile_format_has_i = 0;
66 int logfile_format_has_o_or_i = 0;
67 int always_checksum = 0;
69 +char *tr_opt = NULL, *tr_left = NULL, *tr_right = NULL;
70 +int tr_right_len = 0;
72 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
73 char *batch_name = NULL;
74 @@ -423,6 +425,7 @@ void usage(enum logcode F)
76 rprintf(F," --iconv=CONVERT_SPEC request charset conversion of filenames\n");
78 + rprintf(F," --tr=BAD/GOOD transliterate filenames\n");
79 rprintf(F," -4, --ipv4 prefer IPv4\n");
80 rprintf(F," -6, --ipv6 prefer IPv6\n");
81 rprintf(F," --version print version number\n");
82 @@ -609,6 +612,7 @@ static struct poptOption long_options[]
84 {"iconv", 0, POPT_ARG_STRING, &iconv_opt, 0, 0, 0 },
86 + {"tr", 0, POPT_ARG_STRING, &tr_opt, 0, 0, 0 },
87 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
88 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
89 {"8-bit-output", '8', POPT_ARG_NONE, &allow_8bit_chars, 0, 0, 0 },
90 @@ -1620,6 +1624,31 @@ int parse_arguments(int *argc_p, const c
94 + /* Easiest way to get a local server right is to do this on both sides */
99 + need_unsorted_flist = 1;
100 + /* Our mutation shouldn't interfere with transmission of the
101 + * original option to the server. */
102 + tr_left = strdup(tr_opt);
103 + p = strchr(tr_left, '/');
108 + tr_right_len = strlen(tr_right);
109 + if (strchr(tr_right, '/') != NULL) {
110 + snprintf(err_buf, sizeof err_buf,
111 + "--tr cannot transliterate slashes\n");
122 @@ -1988,6 +2017,12 @@ void server_options(char **args, int *ar
123 else if (remove_source_files)
124 args[ac++] = "--remove-sent-files";
127 + if (asprintf(&arg, "--tr=%s", tr_opt) < 0)
137 @@ -422,6 +422,7 @@ to the detailed description below for a
138 --read-batch=FILE read a batched update from FILE
139 --protocol=NUM force an older protocol version to be used
140 --iconv=CONVERT_SPEC request charset conversion of filenames
141 + --tr=BAD/GOOD transliterate filenames
142 --checksum-seed=NUM set block/file checksum seed (advanced)
143 -4, --ipv4 prefer IPv4
144 -6, --ipv6 prefer IPv6
145 @@ -2013,6 +2014,22 @@ specifying matching rules that can match
146 For instance, you can specify extra include/exclude rules if there are
147 filename differences on the two sides that need to be accounted for.
149 +dit(bf(--tr=BAD/GOOD)) Transliterates filenames on the receiver, after the
150 +iconv conversion (if any). This can be used to remove characters illegal
151 +on the destination filesystem. If you use this option, consider saving a
152 +"find . -ls" listing of the source in the destination to help you determine
153 +the original filenames in case of need.
155 +The argument consists of a string of characters to remove, optionally
156 +followed by a slash and a string of corresponding characters with which to
157 +replace them. The second string may be shorter, in which case any leftover
158 +characters in the first string are simply deleted. For example,
159 +bf(--tr=':\/!') replaces colons with exclamation marks and deletes backslashes.
160 +Slashes cannot be transliterated because it would cause havoc.
162 +If the receiver is invoked over a remote shell, use bf(--protect-args) to
163 +stop the shell from interpreting any nasty characters in the argument.
165 dit(bf(-4, --ipv4) or bf(-6, --ipv6)) Tells rsync to prefer IPv4/IPv6
166 when creating sockets. This only affects sockets that rsync has direct
167 control over, such as the outgoing socket when directly contacting an