1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
/*
fuse iconv module: file name charset conversion
Copyright (C) 2007 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU LGPLv2.
See the file LGPL2.txt
*/
#include <fuse_config.h>
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <iconv.h>
#include <pthread.h>
#include <locale.h>
#include <langinfo.h>
struct iconv {
struct fuse_fs *next;
pthread_mutex_t lock;
char *from_code;
char *to_code;
iconv_t tofs;
iconv_t fromfs;
};
struct iconv_dh {
struct iconv *ic;
void *prev_buf;
fuse_fill_dir_t prev_filler;
};
static struct iconv *iconv_get(void)
{
return fuse_get_context()->private_data;
}
static int iconv_convpath(struct iconv *ic, const char *path, char **newpathp,
int fromfs)
{
size_t pathlen;
size_t newpathlen;
char *newpath;
size_t plen;
char *p;
size_t res;
int err;
if (path == NULL) {
*newpathp = NULL;
return 0;
}
pathlen = strlen(path);
newpathlen = pathlen * 4;
newpath = malloc(newpathlen + 1);
if (!newpath)
return -ENOMEM;
plen = newpathlen;
p = newpath;
pthread_mutex_lock(&ic->lock);
do {
res = iconv(fromfs ? ic->fromfs : ic->tofs, (char **) &path,
&pathlen, &p, &plen);
if (res == (size_t) -1) {
char *tmp;
size_t inc;
err = -EILSEQ;
if (errno != E2BIG)
goto err;
inc = (pathlen + 1) * 4;
newpathlen += inc;
int dp = p - newpath;
tmp = realloc(newpath, newpathlen + 1);
err = -ENOMEM;
if (!tmp)
goto err;
p = tmp + dp;
plen += inc;
newpath = tmp;
}
} while (res == (size_t) -1);
pthread_mutex_unlock(&ic->lock);
*p = '\0';
*newpathp = newpath;
return 0;
err:
iconv(fromfs ? ic->fromfs : ic->tofs, NULL, NULL, NULL, NULL);
pthread_mutex_unlock(&ic->lock);
free(newpath);
return err;
}
static int iconv_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_getattr(ic->next, newpath, stbuf, fi);
free(newpath);
}
return err;
}
static int iconv_access(const char *path, int mask)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_access(ic->next, newpath, mask);
free(newpath);
}
return err;
}
static int iconv_readlink(const char *path, char *buf, size_t size)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_readlink(ic->next, newpath, buf, size);
if (!err) {
char *newlink;
err = iconv_convpath(ic, buf, &newlink, 1);
if (!err) {
strncpy(buf, newlink, size - 1);
buf[size - 1] = '\0';
free(newlink);
}
}
free(newpath);
}
return err;
}
static int iconv_opendir(const char *path, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_opendir(ic->next, newpath, fi);
free(newpath);
}
return err;
}
static int iconv_dir_fill(void *buf, const char *name,
const struct stat *stbuf, off_t off,
enum fuse_fill_dir_flags flags)
{
struct iconv_dh *dh = buf;
char *newname;
int res = 0;
if (iconv_convpath(dh->ic, name, &newname, 1) == 0) {
res = dh->prev_filler(dh->prev_buf, newname, stbuf, off, flags);
free(newname);
}
return res;
}
static int iconv_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
struct iconv_dh dh;
dh.ic = ic;
dh.prev_buf = buf;
dh.prev_filler = filler;
err = fuse_fs_readdir(ic->next, newpath, &dh, iconv_dir_fill,
offset, fi, flags);
free(newpath);
}
return err;
}
static int iconv_releasedir(const char *path, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_releasedir(ic->next, newpath, fi);
free(newpath);
}
return err;
}
static int iconv_mknod(const char *path, mode_t mode, dev_t rdev)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_mknod(ic->next, newpath, mode, rdev);
free(newpath);
}
return err;
}
static int iconv_mkdir(const char *path, mode_t mode)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_mkdir(ic->next, newpath, mode);
free(newpath);
}
return err;
}
static int iconv_unlink(const char *path)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_unlink(ic->next, newpath);
free(newpath);
}
return err;
}
static int iconv_rmdir(const char *path)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_rmdir(ic->next, newpath);
free(newpath);
}
return err;
}
static int iconv_symlink(const char *from, const char *to)
{
struct iconv *ic = iconv_get();
char *newfrom;
char *newto;
int err = iconv_convpath(ic, from, &newfrom, 0);
if (!err) {
err = iconv_convpath(ic, to, &newto, 0);
if (!err) {
err = fuse_fs_symlink(ic->next, newfrom, newto);
free(newto);
}
free(newfrom);
}
return err;
}
static int iconv_rename(const char *from, const char *to, unsigned int flags)
{
struct iconv *ic = iconv_get();
char *newfrom;
char *newto;
int err = iconv_convpath(ic, from, &newfrom, 0);
if (!err) {
err = iconv_convpath(ic, to, &newto, 0);
if (!err) {
err = fuse_fs_rename(ic->next, newfrom, newto, flags);
free(newto);
}
free(newfrom);
}
return err;
}
static int iconv_link(const char *from, const char *to)
{
struct iconv *ic = iconv_get();
char *newfrom;
char *newto;
int err = iconv_convpath(ic, from, &newfrom, 0);
if (!err) {
err = iconv_convpath(ic, to, &newto, 0);
if (!err) {
err = fuse_fs_link(ic->next, newfrom, newto);
free(newto);
}
free(newfrom);
}
return err;
}
static int iconv_chmod(const char *path, mode_t mode,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_chmod(ic->next, newpath, mode, fi);
free(newpath);
}
return err;
}
static int iconv_chown(const char *path, uid_t uid, gid_t gid,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_chown(ic->next, newpath, uid, gid, fi);
free(newpath);
}
return err;
}
static int iconv_truncate(const char *path, off_t size,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_truncate(ic->next, newpath, size, fi);
free(newpath);
}
return err;
}
static int iconv_utimens(const char *path, const struct timespec ts[2],
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_utimens(ic->next, newpath, ts, fi);
free(newpath);
}
return err;
}
static int iconv_create(const char *path, mode_t mode,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_create(ic->next, newpath, mode, fi);
free(newpath);
}
return err;
}
static int iconv_open_file(const char *path, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_open(ic->next, newpath, fi);
free(newpath);
}
return err;
}
static int iconv_read_buf(const char *path, struct fuse_bufvec **bufp,
size_t size, off_t offset, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_read_buf(ic->next, newpath, bufp, size, offset, fi);
free(newpath);
}
return err;
}
static int iconv_write_buf(const char *path, struct fuse_bufvec *buf,
off_t offset, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_write_buf(ic->next, newpath, buf, offset, fi);
free(newpath);
}
return err;
}
static int iconv_statfs(const char *path, struct statvfs *stbuf)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_statfs(ic->next, newpath, stbuf);
free(newpath);
}
return err;
}
static int iconv_flush(const char *path, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_flush(ic->next, newpath, fi);
free(newpath);
}
return err;
}
static int iconv_release(const char *path, struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_release(ic->next, newpath, fi);
free(newpath);
}
return err;
}
static int iconv_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_fsync(ic->next, newpath, isdatasync, fi);
free(newpath);
}
return err;
}
static int iconv_fsyncdir(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_fsyncdir(ic->next, newpath, isdatasync, fi);
free(newpath);
}
return err;
}
static int iconv_setxattr(const char *path, const char *name,
const char *value, size_t size, int flags)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_setxattr(ic->next, newpath, name, value, size,
flags);
free(newpath);
}
return err;
}
static int iconv_getxattr(const char *path, const char *name, char *value,
size_t size)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_getxattr(ic->next, newpath, name, value, size);
free(newpath);
}
return err;
}
static int iconv_listxattr(const char *path, char *list, size_t size)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_listxattr(ic->next, newpath, list, size);
free(newpath);
}
return err;
}
static int iconv_removexattr(const char *path, const char *name)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_removexattr(ic->next, newpath, name);
free(newpath);
}
return err;
}
static int iconv_lock(const char *path, struct fuse_file_info *fi, int cmd,
struct flock *lock)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_lock(ic->next, newpath, fi, cmd, lock);
free(newpath);
}
return err;
}
static int iconv_flock(const char *path, struct fuse_file_info *fi, int op)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_flock(ic->next, newpath, fi, op);
free(newpath);
}
return err;
}
static int iconv_bmap(const char *path, size_t blocksize, uint64_t *idx)
{
struct iconv *ic = iconv_get();
char *newpath;
int err = iconv_convpath(ic, path, &newpath, 0);
if (!err) {
err = fuse_fs_bmap(ic->next, newpath, blocksize, idx);
free(newpath);
}
return err;
}
static off_t iconv_lseek(const char *path, off_t off, int whence,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int res = iconv_convpath(ic, path, &newpath, 0);
if (!res) {
res = fuse_fs_lseek(ic->next, newpath, off, whence, fi);
free(newpath);
}
return res;
}
#ifdef HAVE_STATX
static int iconv_statx(const char *path, int flags, int mask, struct statx *stxbuf,
struct fuse_file_info *fi)
{
struct iconv *ic = iconv_get();
char *newpath;
int res = iconv_convpath(ic, path, &newpath, 0);
if (!res) {
res = fuse_fs_statx(ic->next, newpath, flags, mask, stxbuf, fi);
free(newpath);
}
return res;
}
#endif
static void *iconv_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
{
struct iconv *ic = iconv_get();
fuse_fs_init(ic->next, conn, cfg);
/* Don't touch cfg->nullpath_ok, we can work with
either */
return ic;
}
static void iconv_destroy(void *data)
{
struct iconv *ic = data;
fuse_fs_destroy(ic->next);
iconv_close(ic->tofs);
iconv_close(ic->fromfs);
pthread_mutex_destroy(&ic->lock);
free(ic->from_code);
free(ic->to_code);
free(ic);
}
static const struct fuse_operations iconv_oper = {
.destroy = iconv_destroy,
.init = iconv_init,
.getattr = iconv_getattr,
.access = iconv_access,
.readlink = iconv_readlink,
.opendir = iconv_opendir,
.readdir = iconv_readdir,
.releasedir = iconv_releasedir,
.mknod = iconv_mknod,
.mkdir = iconv_mkdir,
.symlink = iconv_symlink,
.unlink = iconv_unlink,
.rmdir = iconv_rmdir,
.rename = iconv_rename,
.link = iconv_link,
.chmod = iconv_chmod,
.chown = iconv_chown,
.truncate = iconv_truncate,
.utimens = iconv_utimens,
.create = iconv_create,
.open = iconv_open_file,
.read_buf = iconv_read_buf,
.write_buf = iconv_write_buf,
.statfs = iconv_statfs,
.flush = iconv_flush,
.release = iconv_release,
.fsync = iconv_fsync,
.fsyncdir = iconv_fsyncdir,
.setxattr = iconv_setxattr,
.getxattr = iconv_getxattr,
.listxattr = iconv_listxattr,
.removexattr = iconv_removexattr,
.lock = iconv_lock,
.flock = iconv_flock,
.bmap = iconv_bmap,
.lseek = iconv_lseek,
#ifdef HAVE_STATX
.statx = iconv_statx,
#endif
};
static const struct fuse_opt iconv_opts[] = {
FUSE_OPT_KEY("-h", 0),
FUSE_OPT_KEY("--help", 0),
{ "from_code=%s", offsetof(struct iconv, from_code), 0 },
{ "to_code=%s", offsetof(struct iconv, to_code), 1 },
FUSE_OPT_END
};
static void iconv_help(void)
{
char *charmap;
const char *old = setlocale(LC_CTYPE, "");
charmap = strdup(nl_langinfo(CODESET));
if (old)
setlocale(LC_CTYPE, old);
else
perror("setlocale");
printf(
" -o from_code=CHARSET original encoding of file names (default: UTF-8)\n"
" -o to_code=CHARSET new encoding of the file names (default: %s)\n",
charmap);
free(charmap);
}
static int iconv_opt_proc(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
(void) data; (void) arg; (void) outargs;
if (!key) {
iconv_help();
return -1;
}
return 1;
}
static struct fuse_fs *iconv_new(struct fuse_args *args,
struct fuse_fs *next[])
{
struct fuse_fs *fs;
struct iconv *ic;
const char *old = NULL;
const char *from;
const char *to;
ic = calloc(1, sizeof(struct iconv));
if (ic == NULL) {
fuse_log(FUSE_LOG_ERR, "fuse-iconv: memory allocation failed\n");
return NULL;
}
if (fuse_opt_parse(args, ic, iconv_opts, iconv_opt_proc) == -1)
goto out_free;
if (!next[0] || next[1]) {
fuse_log(FUSE_LOG_ERR, "fuse-iconv: exactly one next filesystem required\n");
goto out_free;
}
from = ic->from_code ? ic->from_code : "UTF-8";
to = ic->to_code ? ic->to_code : "";
/* FIXME: detect charset equivalence? */
if (!to[0])
old = setlocale(LC_CTYPE, "");
ic->tofs = iconv_open(from, to);
if (ic->tofs == (iconv_t) -1) {
fuse_log(FUSE_LOG_ERR, "fuse-iconv: cannot convert from %s to %s\n",
to, from);
goto out_free;
}
ic->fromfs = iconv_open(to, from);
if (ic->tofs == (iconv_t) -1) {
fuse_log(FUSE_LOG_ERR, "fuse-iconv: cannot convert from %s to %s\n",
from, to);
goto out_iconv_close_to;
}
if (old) {
setlocale(LC_CTYPE, old);
old = NULL;
}
ic->next = next[0];
fs = fuse_fs_new(&iconv_oper, sizeof(iconv_oper), ic);
if (!fs)
goto out_iconv_close_from;
return fs;
out_iconv_close_from:
iconv_close(ic->fromfs);
out_iconv_close_to:
iconv_close(ic->tofs);
out_free:
free(ic->from_code);
free(ic->to_code);
free(ic);
if (old) {
setlocale(LC_CTYPE, old);
}
return NULL;
}
FUSE_REGISTER_MODULE(iconv, iconv_new);
|