2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007, 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
41 /* Arguments etc ----------------------------------------------------------- */
43 typedef int copyfn(const char *from, const char *to);
44 typedef int mkdirfn(const char *dir, mode_t mode);
46 /* Input and output directories */
47 static const char *source, *destination;
49 /* Function used to copy or link a file */
50 static copyfn *copier = link;
52 /* Function used to make a directory */
53 static mkdirfn *dirmaker = mkdir;
55 /* Various encodings */
56 static const char *fromencoding, *toencoding, *tagencoding;
58 /* Directory for untagged files */
59 static const char *untagged;
61 /* Extract tag information? */
62 static int extracttags;
64 /* Windows-friendly filenames? */
65 static int windowsfriendly;
67 /* Native character encoding (i.e. from LC_CTYPE) */
68 static const char *nativeencoding;
73 /* Included/excluded filename patterns */
74 static struct pattern {
78 } *patterns, **patterns_end = &patterns;
80 static int default_inclusion = 1;
82 static const struct option options[] = {
83 { "help", no_argument, 0, 'h' },
84 { "version", no_argument, 0, 'V' },
85 { "debug", no_argument, 0, 'd' },
86 { "from", required_argument, 0, 'f' },
87 { "to", required_argument, 0, 't' },
88 { "include", required_argument, 0, 'i' },
89 { "exclude", required_argument, 0, 'e' },
90 { "extract-tags", no_argument, 0, 'E' },
91 { "tag-encoding", required_argument, 0, 'T' },
92 { "untagged", required_argument, 0, 'u' },
93 { "windows-friendly", no_argument, 0, 'w' },
94 { "link", no_argument, 0, 'l' },
95 { "symlink", no_argument, 0, 's' },
96 { "copy", no_argument, 0, 'c' },
97 { "no-action", no_argument, 0, 'n' },
101 /* display usage message and terminate */
102 static void help(void) {
104 " disorderfm [OPTIONS] SOURCE DESTINATION\n"
106 " --from, -f ENCODING Source encoding\n"
107 " --to, -t ENCODING Destination encoding\n"
108 "If neither --from nor --to are specified then no encoding translation is\n"
109 "performed. If only one is specified then the other defaults to the current\n"
110 "locale's encoding.\n"
111 " --windows-friendly, -w Replace illegal characters with '_'\n"
112 " --include, -i PATTERN Include files matching a glob pattern\n"
113 " --exclude, -e PATTERN Include files matching a glob pattern\n"
114 "--include and --exclude may be used multiple times. They are checked in\n"
115 "order and the first match wins. If --include is ever used then nonmatching\n"
116 "files are excluded, otherwise they are included.\n"
117 " --link, -l Link files from source to destination (default)\n"
118 " --symlink, -s Symlink files from source to destination\n"
119 " --copy, -c Copy files from source to destination\n"
120 " --no-action, -n Just report what would be done\n"
121 " --debug, -d Debug mode\n"
122 " --help, -h Display usage message\n"
123 " --version, -V Display version number\n");
124 /* TODO: tag extraction stuff when implemented */
129 /* Utilities --------------------------------------------------------------- */
131 /* Copy FROM to TO. Has the same signature as link/symlink. */
132 static int copy(const char *from, const char *to) {
137 if((fdin = open(from, O_RDONLY)) < 0)
138 fatal(errno, "error opening %s", from);
139 if((fdout = open(to, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
140 fatal(errno, "error opening %s", to);
141 while((n = read(fdin, buffer, sizeof buffer)) > 0) {
142 if(write(fdout, buffer, n) < 0)
143 fatal(errno, "error writing to %s", to);
145 if(n < 0) fatal(errno, "error reading %s", from);
146 if(close(fdout) < 0) fatal(errno, "error closing %s", to);
151 static int nocopy(const char *from, const char *to) {
152 xprintf("%s -> %s\n",
153 any2mb(fromencoding, from),
154 any2mb(toencoding, to));
158 static int nomkdir(const char *dir, mode_t attribute((unused)) mode) {
159 xprintf("mkdir %s\n", any2mb(toencoding, dir));
163 /* Name translation -------------------------------------------------------- */
165 static int bad_windows_char(int c) {
169 /* Documented as bad by MS */
176 /* Not documented as bad by MS but Samba mangles anyway? */
182 /* Return the translated form of PATH */
183 static char *nametrans(const char *path) {
184 char *t = any2any(fromencoding, toencoding, path);
186 if(windowsfriendly) {
188 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp?frame=true&hidetoc=true */
189 /* List of forbidden names */
190 static const char *const devicenames[] = {
191 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5",
192 "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5",
193 "LPT6", "LPT7", "LPT8", "LPT9", "CLOCK$"
195 #define NDEVICENAMES (sizeof devicenames / sizeof *devicenames)
199 /* Certain characters are just not allowed. We replace them with
202 if(bad_windows_char((unsigned char)*s))
204 /* Trailing spaces and dots are not allowed. We just strip them. */
205 while(s > t && (s[-1] == ' ' || s[-1] == '.'))
208 /* Reject device names */
209 if((s = strchr(t, '.'))) l = s - t;
211 for(n = 0; n < NDEVICENAMES; ++n)
212 if(l == strlen(devicenames[n]) && !strncasecmp(devicenames[n], t, l))
215 byte_xasprintf(&t, "_%s", t);
220 /* The file walker --------------------------------------------------------- */
222 /* Visit file or directory PATH relative to SOURCE. SOURCE is a null pointer
225 * PATH is something we extracted from the filesystem so by assumption is in
226 * the FROM encoding, which might _not_ be the same as the current locale's
229 * For most errors we carry on as best we can.
231 static void visit(const char *path, const char *destpath) {
232 const struct pattern *p;
234 /* fullsourcepath is the full source pathname for PATH */
235 char *fullsourcepath;
236 /* fulldestpath will be the full destination pathname */
238 /* String to use in error messags. We convert to the current locale; this
239 * may be somewhat misleading but is necessary to avoid getting EILSEQ in
241 char *errsourcepath, *errdestpath;
243 D(("visit %s", path ? path : "NULL"));
245 /* Set up all the various path names */
247 byte_xasprintf(&fullsourcepath, "%s/%s",
249 byte_xasprintf(&fulldestpath, "%s/%s",
250 destination, destpath);
251 byte_xasprintf(&errsourcepath, "%s/%s",
252 source, any2mb(fromencoding, path));
253 byte_xasprintf(&errdestpath, "%s/%s",
254 destination, any2mb(toencoding, destpath));
255 for(p = patterns; p; p = p->next)
256 if(fnmatch(p->pattern, path, FNM_PATHNAME) == 0)
259 /* We found a matching pattern */
261 D(("%s matches %s therefore excluding",
266 /* We did not find a matching pattern */
267 if(!default_inclusion) {
268 D(("%s matches nothing and not including by default", path));
273 fullsourcepath = errsourcepath = (char *)source;
274 fulldestpath = errdestpath = (char *)destination;
277 /* The destination directory might be a subdirectory of the source
278 * directory. In that case we'd better not descend into it when we encounter
279 * it in the source. */
280 if(!strcmp(fullsourcepath, destination)) {
281 info("%s matches destination directory, not recursing", errsourcepath);
285 /* Find out what kind of file we're dealing with */
286 if(stat(fullsourcepath, &sb) < 0) {
287 error(errno, "cannot stat %s", errsourcepath );
291 if(S_ISREG(sb.st_mode)) {
293 if(unlink(fulldestpath) < 0 && errno != ENOENT) {
294 error(errno, "cannot remove %s", errdestpath);
298 if(copier(fullsourcepath, fulldestpath) < 0) {
299 error(errno, "cannot link %s to %s", errsourcepath, errdestpath);
303 } else if(S_ISDIR(sb.st_mode)) {
306 char *childpath, *childdestpath;
308 /* We create the directory on the destination side. If it already exists,
310 if(dirmaker(fulldestpath, 0777) < 0 && errno != EEXIST) {
311 error(errno, "cannot mkdir %s", errdestpath);
315 /* We read the directory and visit all the files in it in any old order. */
316 if(!(dp = opendir(fullsourcepath))) {
317 error(errno, "cannot open directory %s", errsourcepath);
321 while(((errno = 0), (de = readdir(dp)))) {
322 if(!strcmp(de->d_name, ".")
323 || !strcmp(de->d_name, "..")) continue;
325 byte_xasprintf(&childpath, "%s/%s", path, de->d_name);
326 byte_xasprintf(&childdestpath, "%s/%s",
327 destpath, nametrans(de->d_name));
329 childpath = de->d_name;
330 childdestpath = nametrans(de->d_name);
332 visit(childpath, childdestpath);
334 if(errno) fatal(errno, "error reading directory %s", errsourcepath);
337 /* We don't handle special files, but we'd better warn the user. */
338 info("ignoring %s", errsourcepath);
342 int main(int argc, char **argv) {
347 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
348 while((n = getopt_long(argc, argv, "hVdf:t:i:e:ET:u:wlscn", options, 0)) >= 0) {
351 case 'V': version("disorderfm");
352 case 'd': debugging = 1; break;
353 case 'f': fromencoding = optarg; break;
354 case 't': toencoding = optarg; break;
357 p = xmalloc(sizeof *p);
362 patterns_end = &p->next;
363 if(n == 'i') default_inclusion = 0;
365 case 'E': extracttags = 1; break;
366 case 'T': tagencoding = optarg; break;
367 case 'u': untagged = optarg; break;
368 case 'w': windowsfriendly = 1; break;
369 case 'l': copier = link; break;
370 case 's': copier = symlink; break;
371 case 'c': copier = copy; break;
372 case 'n': copier = nocopy; dirmaker = nomkdir; break;
373 default: fatal(0, "invalid option");
376 if(optind == argc) fatal(0, "missing SOURCE and DESTINATION arguments");
377 else if(optind + 1 == argc) fatal(0, "missing DESTINATION argument");
378 else if(optind + 2 != argc) fatal(0, "redundant extra arguments");
379 if(extracttags) fatal(0, "--extract-tags is not implemented yet"); /* TODO */
380 if(tagencoding && !extracttags)
381 fatal(0, "--tag-encoding without --extra-tags does not make sense");
382 if(untagged && !extracttags)
383 fatal(0, "--untagged without --extra-tags does not make sense");
384 source = argv[optind];
385 destination = argv[optind + 1];
386 nativeencoding = nl_langinfo(CODESET);
387 if(fromencoding || toencoding) {
388 if(!fromencoding) fromencoding = nativeencoding;
389 if(!toencoding) toencoding = nativeencoding;
391 if(!tagencoding) tagencoding = nativeencoding;
394 if(errors) fprintf(stderr, "%ld errors\n", errors);