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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file clients/disorderfm.c
19 * @brief DisOrder file manager
21 * Intended to support encoding conversion, tag extraction, etc. Not yet
22 * complete (and hasn't been worked on for ages).
44 /* Arguments etc ----------------------------------------------------------- */
46 typedef int copyfn(const char *from
, const char *to
);
47 typedef int mkdirfn(const char *dir
, mode_t mode
);
49 /* Input and output directories */
50 static const char *source
, *destination
;
52 /* Function used to copy or link a file */
53 static copyfn
*copier
= link
;
55 /* Function used to make a directory */
56 static mkdirfn
*dirmaker
= mkdir
;
58 /* Various encodings */
59 static const char *fromencoding
, *toencoding
, *tagencoding
;
61 /* Directory for untagged files */
62 static const char *untagged
;
64 /* Extract tag information? */
65 static int extracttags
;
67 /* Windows-friendly filenames? */
68 static int windowsfriendly
;
70 /* Native character encoding (i.e. from LC_CTYPE) */
71 static const char *nativeencoding
;
76 /** @brief Included/excluded filename pattern */
77 static struct pattern
{
81 } *patterns
, **patterns_end
= &patterns
;
83 static int default_inclusion
= 1;
85 static const struct option options
[] = {
86 { "help", no_argument
, 0, 'h' },
87 { "version", no_argument
, 0, 'V' },
88 { "debug", no_argument
, 0, 'd' },
89 { "from", required_argument
, 0, 'f' },
90 { "to", required_argument
, 0, 't' },
91 { "include", required_argument
, 0, 'i' },
92 { "exclude", required_argument
, 0, 'e' },
93 { "extract-tags", no_argument
, 0, 'E' },
94 { "tag-encoding", required_argument
, 0, 'T' },
95 { "untagged", required_argument
, 0, 'u' },
96 { "windows-friendly", no_argument
, 0, 'w' },
97 { "link", no_argument
, 0, 'l' },
98 { "symlink", no_argument
, 0, 's' },
99 { "copy", no_argument
, 0, 'c' },
100 { "no-action", no_argument
, 0, 'n' },
104 /* display usage message and terminate */
105 static void attribute((noreturn
)) help(void) {
107 " disorderfm [OPTIONS] SOURCE DESTINATION\n"
109 " --from, -f ENCODING Source encoding\n"
110 " --to, -t ENCODING Destination encoding\n"
111 "If neither --from nor --to are specified then no encoding translation is\n"
112 "performed. If only one is specified then the other defaults to the current\n"
113 "locale's encoding.\n"
114 " --windows-friendly, -w Replace illegal characters with '_'\n"
115 " --include, -i PATTERN Include files matching a glob pattern\n"
116 " --exclude, -e PATTERN Include files matching a glob pattern\n"
117 "--include and --exclude may be used multiple times. They are checked in\n"
118 "order and the first match wins. If --include is ever used then nonmatching\n"
119 "files are excluded, otherwise they are included.\n"
120 " --link, -l Link files from source to destination (default)\n"
121 " --symlink, -s Symlink files from source to destination\n"
122 " --copy, -c Copy files from source to destination\n"
123 " --no-action, -n Just report what would be done\n"
124 " --debug, -d Debug mode\n"
125 " --help, -h Display usage message\n"
126 " --version, -V Display version number\n");
127 /* TODO: tag extraction stuff when implemented */
132 /* Utilities --------------------------------------------------------------- */
134 /* Copy FROM to TO. Has the same signature as link/symlink. */
135 static int copy(const char *from
, const char *to
) {
141 if((fdin
= open(from
, O_RDONLY
)) < 0)
142 disorder_fatal(errno
, "error opening %s", from
);
143 if((fdout
= open(to
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0)
144 disorder_fatal(errno
, "error opening %s", to
);
145 while((n
= read(fdin
, buffer
, sizeof buffer
)) > 0) {
146 if(write(fdout
, buffer
, n
) < 0)
147 disorder_fatal(errno
, "error writing to %s", to
);
150 disorder_fatal(errno
, "error reading %s", from
);
151 if(fstat(fdin
, &sb
) < 0)
152 disorder_fatal(errno
, "error stating %s", from
);
153 if(fchown(fdout
, sb
.st_uid
, sb
.st_gid
) < 0)
154 disorder_fatal(errno
, "error chowning %s", from
);
155 if(fchmod(fdout
, sb
.st_mode
& 07777) < 0)
156 disorder_fatal(errno
, "error chmoding %s", from
);
157 if(close(fdout
) < 0) disorder_fatal(errno
, "error closing %s", to
);
162 static int nocopy(const char *from
, const char *to
) {
163 xprintf("%s -> %s\n",
164 any2mb(fromencoding
, from
),
165 any2mb(toencoding
, to
));
169 static int nomkdir(const char *dir
, mode_t
attribute((unused
)) mode
) {
170 xprintf("mkdir %s\n", any2mb(toencoding
, dir
));
174 /* Name translation -------------------------------------------------------- */
176 static int bad_windows_char(int c
) {
180 /* Documented as bad by MS */
187 /* Not documented as bad by MS but Samba mangles anyway? */
193 /* Return the translated form of PATH */
194 static char *nametrans(const char *path
) {
195 char *t
= any2any(fromencoding
, toencoding
, path
);
197 if(windowsfriendly
) {
199 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp?frame=true&hidetoc=true */
200 /* List of forbidden names */
201 static const char *const devicenames
[] = {
202 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5",
203 "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5",
204 "LPT6", "LPT7", "LPT8", "LPT9", "CLOCK$"
206 #define NDEVICENAMES (sizeof devicenames / sizeof *devicenames)
210 /* Certain characters are just not allowed. We replace them with
213 if(bad_windows_char((unsigned char)*s
))
215 /* Trailing spaces and dots are not allowed. We just strip them. */
216 while(s
> t
&& (s
[-1] == ' ' || s
[-1] == '.'))
219 /* Reject device names */
220 if((s
= strchr(t
, '.'))) l
= s
- t
;
222 for(n
= 0; n
< NDEVICENAMES
; ++n
)
223 if(l
== strlen(devicenames
[n
]) && !strncasecmp(devicenames
[n
], t
, l
))
226 byte_xasprintf(&t
, "_%s", t
);
231 /* The file walker --------------------------------------------------------- */
233 /* Visit file or directory PATH relative to SOURCE. SOURCE is a null pointer
236 * PATH is something we extracted from the filesystem so by assumption is in
237 * the FROM encoding, which might _not_ be the same as the current locale's
240 * For most errors we carry on as best we can.
242 static void visit(const char *path
, const char *destpath
) {
243 const struct pattern
*p
;
245 /* fullsourcepath is the full source pathname for PATH */
246 char *fullsourcepath
;
247 /* fulldestpath will be the full destination pathname */
249 /* String to use in error messags. We convert to the current locale; this
250 * may be somewhat misleading but is necessary to avoid getting EILSEQ in
252 char *errsourcepath
, *errdestpath
;
254 D(("visit %s", path ? path
: "NULL"));
256 /* Set up all the various path names */
258 byte_xasprintf(&fullsourcepath
, "%s/%s",
260 byte_xasprintf(&fulldestpath
, "%s/%s",
261 destination
, destpath
);
262 byte_xasprintf(&errsourcepath
, "%s/%s",
263 source
, any2mb(fromencoding
, path
));
264 byte_xasprintf(&errdestpath
, "%s/%s",
265 destination
, any2mb(toencoding
, destpath
));
266 for(p
= patterns
; p
; p
= p
->next
)
267 if(fnmatch(p
->pattern
, path
, FNM_PATHNAME
) == 0)
270 /* We found a matching pattern */
272 D(("%s matches %s therefore excluding",
277 /* We did not find a matching pattern */
278 if(!default_inclusion
) {
279 D(("%s matches nothing and not including by default", path
));
284 fullsourcepath
= errsourcepath
= (char *)source
;
285 fulldestpath
= errdestpath
= (char *)destination
;
288 /* The destination directory might be a subdirectory of the source
289 * directory. In that case we'd better not descend into it when we encounter
290 * it in the source. */
291 if(!strcmp(fullsourcepath
, destination
)) {
292 disorder_info("%s matches destination directory, not recursing", errsourcepath
);
296 /* Find out what kind of file we're dealing with */
297 if(stat(fullsourcepath
, &sb
) < 0) {
298 disorder_error(errno
, "cannot stat %s", errsourcepath
);
302 if(S_ISREG(sb
.st_mode
)) {
304 if(unlink(fulldestpath
) < 0 && errno
!= ENOENT
) {
305 disorder_error(errno
, "cannot remove %s", errdestpath
);
309 if(copier(fullsourcepath
, fulldestpath
) < 0) {
310 disorder_error(errno
, "cannot link %s to %s", errsourcepath
, errdestpath
);
314 } else if(S_ISDIR(sb
.st_mode
)) {
317 char *childpath
, *childdestpath
;
319 /* We create the directory on the destination side. If it already exists,
321 if(dirmaker(fulldestpath
, 0700) == 0) {
322 if(dirmaker
!= nomkdir
) {
323 /* Created new directory. Adjust permissions and ownership to match the
325 if(chown(fulldestpath
, sb
.st_uid
, sb
.st_gid
) < 0) {
326 disorder_error(errno
, "cannot chown %s", errdestpath
);
329 if(chmod(fulldestpath
, sb
.st_mode
& 07777) < 0) {
330 disorder_error(errno
, "cannot chmod %s", errdestpath
);
334 } else if(errno
!= EEXIST
) {
335 disorder_error(errno
, "cannot mkdir %s", errdestpath
);
339 /* We read the directory and visit all the files in it in any old order. */
340 if(!(dp
= opendir(fullsourcepath
))) {
341 disorder_error(errno
, "cannot open directory %s", errsourcepath
);
345 while(((errno
= 0), (de
= readdir(dp
)))) {
346 if(!strcmp(de
->d_name
, ".")
347 || !strcmp(de
->d_name
, "..")) continue;
349 byte_xasprintf(&childpath
, "%s/%s", path
, de
->d_name
);
350 byte_xasprintf(&childdestpath
, "%s/%s",
351 destpath
, nametrans(de
->d_name
));
353 childpath
= de
->d_name
;
354 childdestpath
= nametrans(de
->d_name
);
356 visit(childpath
, childdestpath
);
359 disorder_fatal(errno
, "error reading directory %s", errsourcepath
);
362 /* We don't handle special files, but we'd better warn the user. */
363 disorder_info("ignoring %s", errsourcepath
);
367 int main(int argc
, char **argv
) {
372 if(!setlocale(LC_CTYPE
, ""))
373 disorder_fatal(errno
, "error calling setlocale");
374 while((n
= getopt_long(argc
, argv
, "hVdf:t:i:e:ET:u:wlscn", options
, 0)) >= 0) {
377 case 'V': version("disorderfm");
378 case 'd': debugging
= 1; break;
379 case 'f': fromencoding
= optarg
; break;
380 case 't': toencoding
= optarg
; break;
383 p
= xmalloc(sizeof *p
);
388 patterns_end
= &p
->next
;
389 if(n
== 'i') default_inclusion
= 0;
391 case 'E': extracttags
= 1; break;
392 case 'T': tagencoding
= optarg
; break;
393 case 'u': untagged
= optarg
; break;
394 case 'w': windowsfriendly
= 1; break;
395 case 'l': copier
= link
; break;
396 case 's': copier
= symlink
; break;
397 case 'c': copier
= copy
; break;
398 case 'n': copier
= nocopy
; dirmaker
= nomkdir
; break;
399 default: disorder_fatal(0, "invalid option");
403 disorder_fatal(0, "missing SOURCE and DESTINATION arguments");
404 else if(optind
+ 1 == argc
) disorder_fatal(0, "missing DESTINATION argument");
405 else if(optind
+ 2 != argc
) disorder_fatal(0, "redundant extra arguments");
407 disorder_fatal(0, "--extract-tags is not implemented yet"); /* TODO */
408 if(tagencoding
&& !extracttags
)
409 disorder_fatal(0, "--tag-encoding without --extra-tags does not make sense");
410 if(untagged
&& !extracttags
)
411 disorder_fatal(0, "--untagged without --extra-tags does not make sense");
412 source
= argv
[optind
];
413 destination
= argv
[optind
+ 1];
414 nativeencoding
= nl_langinfo(CODESET
);
415 if(fromencoding
|| toencoding
) {
416 if(!fromencoding
) fromencoding
= nativeencoding
;
417 if(!toencoding
) toencoding
= nativeencoding
;
419 if(!tagencoding
) tagencoding
= nativeencoding
;
422 if(errors
) fprintf(stderr
, "%ld errors\n", errors
);