2 * This file is part of DisOrder.
3 * Copyright (C) 2006 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
43 /* Arguments etc ----------------------------------------------------------- */
45 typedef int copyfn(const char *from
, const char *to
);
46 typedef int mkdirfn(const char *dir
, mode_t mode
);
48 /* Input and output directories */
49 static const char *source
, *destination
;
51 /* Function used to copy or link a file */
52 static copyfn
*copier
= link
;
54 /* Function used to make a directory */
55 static mkdirfn
*dirmaker
= mkdir
;
57 /* Various encodings */
58 static const char *fromencoding
, *toencoding
, *tagencoding
;
60 /* Directory for untagged files */
61 static const char *untagged
;
63 /* Extract tag information? */
64 static int extracttags
;
66 /* Windows-friendly filenames? */
67 static int windowsfriendly
;
69 /* Native character encoding (i.e. from LC_CTYPE) */
70 static const char *nativeencoding
;
75 /* Included/excluded filename patterns */
76 static struct pattern
{
80 } *patterns
, **patterns_end
= &patterns
;
82 static int default_inclusion
= 1;
84 static const struct option options
[] = {
85 { "help", no_argument
, 0, 'h' },
86 { "version", no_argument
, 0, 'V' },
87 { "debug", no_argument
, 0, 'd' },
88 { "from", required_argument
, 0, 'f' },
89 { "to", required_argument
, 0, 't' },
90 { "include", required_argument
, 0, 'i' },
91 { "exclude", required_argument
, 0, 'e' },
92 { "extract-tags", no_argument
, 0, 'E' },
93 { "tag-encoding", required_argument
, 0, 'T' },
94 { "untagged", required_argument
, 0, 'u' },
95 { "windows-friendly", no_argument
, 0, 'w' },
96 { "link", no_argument
, 0, 'l' },
97 { "symlink", no_argument
, 0, 's' },
98 { "copy", no_argument
, 0, 'c' },
99 { "no-action", no_argument
, 0, 'n' },
103 /* display usage message and terminate */
104 static void help(void) {
106 " disorderfm [OPTIONS] SOURCE DESTINATION\n"
108 " --from, -f ENCODING Source encoding\n"
109 " --to, -t ENCODING Destination encoding\n"
110 "If neither --from nor --to are specified then no encoding translation is\n"
111 "performed. If only one is specified then the other defaults to the current\n"
112 "locale's encoding.\n"
113 " --windows-friendly, -w Replace illegal characters with '_'\n"
114 " --include, -i PATTERN Include files matching a glob pattern\n"
115 " --exclude, -e PATTERN Include files matching a glob pattern\n"
116 "--include and --exclude may be used multiple times. They are checked in\n"
117 "order and the first match wins. If --include is ever used then nonmatching\n"
118 "files are excluded, otherwise they are included.\n"
119 " --link, -l Link files from source to destination (default)\n"
120 " --symlink, -s Symlink files from source to destination\n"
121 " --copy, -c Copy files from source to destination\n"
122 " --no-action, -n Just report what would be done\n"
123 " --debug, -d Debug mode\n"
124 " --help, -h Display usage message\n"
125 " --version, -V Display version number\n");
126 /* TODO: tag extraction stuff when implemented */
131 /* Utilities --------------------------------------------------------------- */
133 /* Copy FROM to TO. Has the same signature as link/symlink. */
134 static int copy(const char *from
, const char *to
) {
139 if((fdin
= open(from
, O_RDONLY
)) < 0)
140 fatal(errno
, "error opening %s", from
);
141 if((fdout
= open(to
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0)
142 fatal(errno
, "error opening %s", to
);
143 while((n
= read(fdin
, buffer
, sizeof buffer
)) > 0) {
144 if(write(fdout
, buffer
, n
) < 0)
145 fatal(errno
, "error writing to %s", to
);
147 if(n
< 0) fatal(errno
, "error reading %s", from
);
148 if(close(fdout
) < 0) fatal(errno
, "error closing %s", to
);
153 static int nocopy(const char *from
, const char *to
) {
154 xprintf("%s -> %s\n",
155 any2mb(fromencoding
, from
),
156 any2mb(toencoding
, to
));
160 static int nomkdir(const char *dir
, mode_t
attribute((unused
)) mode
) {
161 xprintf("mkdir %s\n", any2mb(toencoding
, dir
));
165 /* Name translation -------------------------------------------------------- */
167 static int bad_windows_char(int c
) {
171 /* Documented as bad by MS */
178 /* Not documented as bad by MS but Samba mangles anyway? */
184 /* Return the translated form of PATH */
185 static char *nametrans(const char *path
) {
186 char *t
= any2any(fromencoding
, toencoding
, path
);
188 if(windowsfriendly
) {
190 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp?frame=true&hidetoc=true */
191 /* List of forbidden names */
192 static const char *const devicenames
[] = {
193 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5",
194 "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5",
195 "LPT6", "LPT7", "LPT8", "LPT9", "CLOCK$"
197 #define NDEVICENAMES (sizeof devicenames / sizeof *devicenames)
201 /* Certain characters are just not allowed. We replace them with
204 if(bad_windows_char((unsigned char)*s
))
206 /* Trailing spaces and dots are not allowed. We just strip them. */
207 while(s
> t
&& (s
[-1] == ' ' || s
[-1] == '.'))
210 /* Reject device names */
211 if((s
= strchr(t
, '.'))) l
= s
- t
;
213 for(n
= 0; n
< NDEVICENAMES
; ++n
)
214 if(l
== strlen(devicenames
[n
]) && !strncasecmp(devicenames
[n
], t
, l
))
217 byte_xasprintf(&t
, "_%s", t
);
222 /* The file walker --------------------------------------------------------- */
224 /* Visit file or directory PATH relative to SOURCE. SOURCE is a null pointer
227 * PATH is something we extracted from the filesystem so by assumption is in
228 * the FROM encoding, which might _not_ be the same as the current locale's
231 * For most errors we carry on as best we can.
233 static void visit(const char *path
, const char *destpath
) {
234 const struct pattern
*p
;
236 /* fullsourcepath is the full source pathname for PATH */
237 char *fullsourcepath
;
238 /* fulldestpath will be the full destination pathname */
240 /* String to use in error messags. We convert to the current locale; this
241 * may be somewhat misleading but is necessary to avoid getting EILSEQ in
243 char *errsourcepath
, *errdestpath
;
245 D(("visit %s", path ? path
: "NULL"));
247 /* Set up all the various path names */
249 byte_xasprintf(&fullsourcepath
, "%s/%s",
251 byte_xasprintf(&fulldestpath
, "%s/%s",
252 destination
, destpath
);
253 byte_xasprintf(&errsourcepath
, "%s/%s",
254 source
, any2mb(fromencoding
, path
));
255 byte_xasprintf(&errdestpath
, "%s/%s",
256 destination
, any2mb(toencoding
, destpath
));
257 for(p
= patterns
; p
; p
= p
->next
)
258 if(fnmatch(p
->pattern
, path
, FNM_PATHNAME
) == 0)
261 /* We found a matching pattern */
263 D(("%s matches %s therefore excluding",
268 /* We did not find a matching pattern */
269 if(!default_inclusion
) {
270 D(("%s matches nothing and not including by default", path
));
275 fullsourcepath
= errsourcepath
= (char *)source
;
276 fulldestpath
= errdestpath
= (char *)destination
;
279 /* The destination directory might be a subdirectory of the source
280 * directory. In that case we'd better not descend into it when we encounter
281 * it in the source. */
282 if(!strcmp(fullsourcepath
, destination
)) {
283 info("%s matches destination directory, not recursing", errsourcepath
);
287 /* Find out what kind of file we're dealing with */
288 if(stat(fullsourcepath
, &sb
) < 0) {
289 error(errno
, "cannot stat %s", errsourcepath
);
293 if(S_ISREG(sb
.st_mode
)) {
295 if(unlink(fulldestpath
) < 0 && errno
!= ENOENT
) {
296 error(errno
, "cannot remove %s", errdestpath
);
300 if(copier(fullsourcepath
, fulldestpath
) < 0) {
301 error(errno
, "cannot link %s to %s", errsourcepath
, errdestpath
);
305 } else if(S_ISDIR(sb
.st_mode
)) {
308 char *childpath
, *childdestpath
;
310 /* We create the directory on the destination side. If it already exists,
312 if(dirmaker(fulldestpath
, 0777) < 0 && errno
!= EEXIST
) {
313 error(errno
, "cannot mkdir %s", errdestpath
);
317 /* We read the directory and visit all the files in it in any old order. */
318 if(!(dp
= opendir(fullsourcepath
))) {
319 error(errno
, "cannot open directory %s", errsourcepath
);
323 while(((errno
= 0), (de
= readdir(dp
)))) {
324 if(!strcmp(de
->d_name
, ".")
325 || !strcmp(de
->d_name
, "..")) continue;
327 byte_xasprintf(&childpath
, "%s/%s", path
, de
->d_name
);
328 byte_xasprintf(&childdestpath
, "%s/%s",
329 destpath
, nametrans(de
->d_name
));
331 childpath
= de
->d_name
;
332 childdestpath
= nametrans(de
->d_name
);
334 visit(childpath
, childdestpath
);
336 if(errno
) fatal(errno
, "error reading directory %s", errsourcepath
);
339 /* We don't handle special files, but we'd better warn the user. */
340 info("ignoring %s", errsourcepath
);
344 int main(int argc
, char **argv
) {
349 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
350 while((n
= getopt_long(argc
, argv
, "hVdf:t:i:e:ET:u:wlscn", options
, 0)) >= 0) {
353 case 'V': version("disorderfm");
354 case 'd': debugging
= 1; break;
355 case 'f': fromencoding
= optarg
; break;
356 case 't': toencoding
= optarg
; break;
359 p
= xmalloc(sizeof *p
);
364 patterns_end
= &p
->next
;
365 if(n
== 'i') default_inclusion
= 0;
367 case 'E': extracttags
= 1; break;
368 case 'T': tagencoding
= optarg
; break;
369 case 'u': untagged
= optarg
; break;
370 case 'w': windowsfriendly
= 1; break;
371 case 'l': copier
= link
; break;
372 case 's': copier
= symlink
; break;
373 case 'c': copier
= copy
; break;
374 case 'n': copier
= nocopy
; dirmaker
= nomkdir
; break;
375 default: fatal(0, "invalid option");
378 if(optind
== argc
) fatal(0, "missing SOURCE and DESTINATION arguments");
379 else if(optind
+ 1 == argc
) fatal(0, "missing DESTINATION argument");
380 else if(optind
+ 2 != argc
) fatal(0, "redundant extra arguments");
381 if(extracttags
) fatal(0, "--extract-tags is not implemented yet"); /* TODO */
382 if(tagencoding
&& !extracttags
)
383 fatal(0, "--tag-encoding without --extra-tags does not make sense");
384 if(untagged
&& !extracttags
)
385 fatal(0, "--untagged without --extra-tags does not make sense");
386 source
= argv
[optind
];
387 destination
= argv
[optind
+ 1];
388 nativeencoding
= nl_langinfo(CODESET
);
389 if(fromencoding
|| toencoding
) {
390 if(!fromencoding
) fromencoding
= nativeencoding
;
391 if(!toencoding
) toencoding
= nativeencoding
;
393 if(!tagencoding
) tagencoding
= nativeencoding
;
396 if(errors
) fprintf(stderr
, "%ld errors\n", errors
);