chiark / gitweb /
Separate notion of configured audio backend from Disobedience/playrtp
[disorder] / clients / disorderfm.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2006, 2007, 2008 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <getopt.h>
25#include <unistd.h>
26#include <locale.h>
27#include <fcntl.h>
28#include <errno.h>
29#include <dirent.h>
30#include <sys/stat.h>
31#include <langinfo.h>
32#include <string.h>
33#include <fnmatch.h>
34
35#include "syscalls.h"
36#include "log.h"
37#include "printf.h"
38#include "charset.h"
39#include "defs.h"
40#include "mem.h"
3fbdc96d 41#include "version.h"
460b9539 42
43/* Arguments etc ----------------------------------------------------------- */
44
45typedef int copyfn(const char *from, const char *to);
46typedef int mkdirfn(const char *dir, mode_t mode);
47
48/* Input and output directories */
49static const char *source, *destination;
50
51/* Function used to copy or link a file */
52static copyfn *copier = link;
53
54/* Function used to make a directory */
55static mkdirfn *dirmaker = mkdir;
56
57/* Various encodings */
58static const char *fromencoding, *toencoding, *tagencoding;
59
60/* Directory for untagged files */
61static const char *untagged;
62
63/* Extract tag information? */
64static int extracttags;
65
66/* Windows-friendly filenames? */
67static int windowsfriendly;
68
69/* Native character encoding (i.e. from LC_CTYPE) */
70static const char *nativeencoding;
71
72/* Count of errors */
73static long errors;
74
75/* Included/excluded filename patterns */
76static struct pattern {
77 struct pattern *next;
78 const char *pattern;
79 int type;
80} *patterns, **patterns_end = &patterns;
81
82static int default_inclusion = 1;
83
84static 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' },
100 { 0, 0, 0, 0 }
101};
102
103/* display usage message and terminate */
104static void help(void) {
105 xprintf("Usage:\n"
106" disorderfm [OPTIONS] SOURCE DESTINATION\n"
107"Options:\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 */
127 xfclose(stdout);
128 exit(0);
129}
130
460b9539 131/* Utilities --------------------------------------------------------------- */
132
133/* Copy FROM to TO. Has the same signature as link/symlink. */
134static int copy(const char *from, const char *to) {
135 int fdin, fdout;
136 char buffer[4096];
137 int n;
138
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);
146 }
147 if(n < 0) fatal(errno, "error reading %s", from);
148 if(close(fdout) < 0) fatal(errno, "error closing %s", to);
149 xclose(fdin);
150 return 0;
151}
152
153static int nocopy(const char *from, const char *to) {
154 xprintf("%s -> %s\n",
155 any2mb(fromencoding, from),
156 any2mb(toencoding, to));
157 return 0;
158}
159
160static int nomkdir(const char *dir, mode_t attribute((unused)) mode) {
161 xprintf("mkdir %s\n", any2mb(toencoding, dir));
162 return 0;
163}
164
165/* Name translation -------------------------------------------------------- */
166
167static int bad_windows_char(int c) {
168 switch(c) {
169 default:
170 return 0;
171 /* Documented as bad by MS */
172 case '<':
173 case '>':
174 case ':':
175 case '"':
176 case '\\':
177 case '|':
178 /* Not documented as bad by MS but Samba mangles anyway? */
179 case '*':
180 return 1;
181 }
182}
183
184/* Return the translated form of PATH */
185static char *nametrans(const char *path) {
186 char *t = any2any(fromencoding, toencoding, path);
187
188 if(windowsfriendly) {
189 /* See:
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$"
196 };
197#define NDEVICENAMES (sizeof devicenames / sizeof *devicenames)
198 char *s;
199 size_t n, l;
200
201 /* Certain characters are just not allowed. We replace them with
202 * underscores. */
203 for(s = t; *s; ++s)
204 if(bad_windows_char((unsigned char)*s))
205 *s = '_';
206 /* Trailing spaces and dots are not allowed. We just strip them. */
207 while(s > t && (s[-1] == ' ' || s[-1] == '.'))
208 --s;
209 *s = 0;
210 /* Reject device names */
211 if((s = strchr(t, '.'))) l = s - t;
212 else l = 0;
213 for(n = 0; n < NDEVICENAMES; ++n)
214 if(l == strlen(devicenames[n]) && !strncasecmp(devicenames[n], t, l))
215 break;
216 if(n < NDEVICENAMES)
217 byte_xasprintf(&t, "_%s", t);
218 }
219 return t;
220}
221
222/* The file walker --------------------------------------------------------- */
223
224/* Visit file or directory PATH relative to SOURCE. SOURCE is a null pointer
225 * at the top level.
226 *
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
229 * encoding.
230 *
231 * For most errors we carry on as best we can.
232 */
233static void visit(const char *path, const char *destpath) {
234 const struct pattern *p;
235 struct stat sb;
236 /* fullsourcepath is the full source pathname for PATH */
237 char *fullsourcepath;
238 /* fulldestpath will be the full destination pathname */
239 char *fulldestpath;
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
242 * error messages. */
243 char *errsourcepath, *errdestpath;
244
245 D(("visit %s", path ? path : "NULL"));
246
247 /* Set up all the various path names */
248 if(path) {
249 byte_xasprintf(&fullsourcepath, "%s/%s",
250 source, path);
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)
259 break;
260 if(p) {
261 /* We found a matching pattern */
262 if(p->type == 'e') {
263 D(("%s matches %s therefore excluding",
264 path, p->pattern));
265 return;
266 }
267 } else {
268 /* We did not find a matching pattern */
269 if(!default_inclusion) {
270 D(("%s matches nothing and not including by default", path));
271 return;
272 }
273 }
274 } else {
275 fullsourcepath = errsourcepath = (char *)source;
276 fulldestpath = errdestpath = (char *)destination;
277 }
278
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);
284 return;
285 }
286
287 /* Find out what kind of file we're dealing with */
288 if(stat(fullsourcepath, &sb) < 0) {
289 error(errno, "cannot stat %s", errsourcepath );
290 ++errors;
291 return;
292 }
293 if(S_ISREG(sb.st_mode)) {
294 if(copier != nocopy)
295 if(unlink(fulldestpath) < 0 && errno != ENOENT) {
296 error(errno, "cannot remove %s", errdestpath);
297 ++errors;
298 return;
299 }
300 if(copier(fullsourcepath, fulldestpath) < 0) {
301 error(errno, "cannot link %s to %s", errsourcepath, errdestpath);
302 ++errors;
303 return;
304 }
305 } else if(S_ISDIR(sb.st_mode)) {
306 DIR *dp;
307 struct dirent *de;
308 char *childpath, *childdestpath;
309
310 /* We create the directory on the destination side. If it already exists,
311 * that's fine. */
312 if(dirmaker(fulldestpath, 0777) < 0 && errno != EEXIST) {
313 error(errno, "cannot mkdir %s", errdestpath);
314 ++errors;
315 return;
316 }
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);
320 ++errors;
321 return;
322 }
323 while(((errno = 0), (de = readdir(dp)))) {
324 if(!strcmp(de->d_name, ".")
325 || !strcmp(de->d_name, "..")) continue;
326 if(path) {
327 byte_xasprintf(&childpath, "%s/%s", path, de->d_name);
328 byte_xasprintf(&childdestpath, "%s/%s",
329 destpath, nametrans(de->d_name));
330 } else {
331 childpath = de->d_name;
332 childdestpath = nametrans(de->d_name);
333 }
334 visit(childpath, childdestpath);
335 }
336 if(errno) fatal(errno, "error reading directory %s", errsourcepath);
337 closedir(dp);
338 } else {
339 /* We don't handle special files, but we'd better warn the user. */
340 info("ignoring %s", errsourcepath);
341 }
342}
343
344int main(int argc, char **argv) {
345 int n;
346 struct pattern *p;
347
320598d4 348 mem_init();
460b9539 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) {
351 switch(n) {
352 case 'h': help();
3fbdc96d 353 case 'V': version("disorderfm");
460b9539 354 case 'd': debugging = 1; break;
355 case 'f': fromencoding = optarg; break;
356 case 't': toencoding = optarg; break;
357 case 'i':
358 case 'e':
359 p = xmalloc(sizeof *p);
360 p->type = n;
361 p->pattern = optarg;
362 p->next = 0;
363 *patterns_end = p;
364 patterns_end = &p->next;
365 if(n == 'i') default_inclusion = 0;
366 break;
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");
376 }
377 }
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;
392 }
393 if(!tagencoding) tagencoding = nativeencoding;
394 visit(0, 0);
395 xfclose(stdout);
396 if(errors) fprintf(stderr, "%ld errors\n", errors);
397 return !!errors;
398}
399
400/*
401Local Variables:
402c-basic-offset:2
403comment-column:40
404fill-column:79
405indent-tabs-mode:nil
406End:
407*/