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