7 /* perl implements threads with pthread. So, we use the pthread API for
8 * handling thread-local storage. */
10 PerlInterpreter *master_interp = NULL;
11 static inline void create_perl_context() {
13 PerlInterpreter *me = PERL_GET_CONTEXT;
15 PERL_SET_CONTEXT(master_interp);
16 me = perl_clone(master_interp, CLONEf_CLONE_HOST);
20 # define FUSE_CONTEXT_PRE create_perl_context(); { dSP
21 # define FUSE_CONTEXT_POST }
22 # define FUSE_USE_ITHREADS
24 # error "Sorry, I don't know how to handle ithreads on this architecture."
27 # define FUSE_CONTEXT_PRE dSP
28 # define FUSE_CONTEXT_POST
34 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
39 #define N_CALLBACKS 25
40 SV *_PLfuse_callbacks[N_CALLBACKS];
42 int _PLfuse_getattr(const char *file, struct stat *result) {
45 DEBUGf("getattr begin: %s\n",file);
49 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
51 rv = call_sv(_PLfuse_callbacks[0],G_ARRAY);
55 fprintf(stderr,"inappropriate number of returned values from getattr\n");
62 result->st_blocks = POPi;
63 result->st_blksize = POPi;
64 result->st_ctime = POPi;
65 result->st_mtime = POPi;
66 result->st_atime = POPi;
67 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
68 result->st_rdev = POPi;
69 result->st_gid = POPi;
70 result->st_uid = POPi;
71 result->st_nlink = POPi;
72 result->st_mode = POPi;
73 result->st_ino = POPi;
74 result->st_dev = POPi;
80 DEBUGf("getattr end: %i\n",rv);
85 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
90 DEBUGf("readlink begin\n");
94 XPUSHs(sv_2mortal(newSVpv(file,0)));
96 rv = call_sv(_PLfuse_callbacks[1],G_SCALAR);
102 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
105 strncpy(buf,SvPV_nolen(mysv),buflen);
113 DEBUGf("readlink end: %i\n",rv);
120 * This doesn't yet work... we alwas get ENOSYS when trying to use readdir().
121 * Well, of course, getdir() is fine as well.
123 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil, off_t off, struct fuse_file_info *fi) {
125 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
128 DEBUGf("getdir begin\n");
132 XPUSHs(sv_2mortal(newSVpv(file,0)));
134 prv = call_sv(_PLfuse_callbacks[2],G_ARRAY);
139 dirfil(dirh,POPp,0,0);
141 fprintf(stderr,"getdir() handler returned nothing!\n");
147 DEBUGf("getdir end: %i\n",rv);
152 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
155 DEBUGf("mknod begin\n");
159 XPUSHs(sv_2mortal(newSVpv(file,0)));
160 XPUSHs(sv_2mortal(newSViv(mode)));
161 XPUSHs(sv_2mortal(newSViv(dev)));
163 rv = call_sv(_PLfuse_callbacks[3],G_SCALAR);
172 DEBUGf("mknod end: %i\n",rv);
177 int _PLfuse_mkdir (const char *file, mode_t mode) {
180 DEBUGf("mkdir begin\n");
184 XPUSHs(sv_2mortal(newSVpv(file,0)));
185 XPUSHs(sv_2mortal(newSViv(mode)));
187 rv = call_sv(_PLfuse_callbacks[4],G_SCALAR);
196 DEBUGf("mkdir end: %i\n",rv);
202 int _PLfuse_unlink (const char *file) {
205 DEBUGf("unlink begin\n");
209 XPUSHs(sv_2mortal(newSVpv(file,0)));
211 rv = call_sv(_PLfuse_callbacks[5],G_SCALAR);
220 DEBUGf("unlink end: %i\n",rv);
225 int _PLfuse_rmdir (const char *file) {
228 DEBUGf("rmdir begin\n");
232 XPUSHs(sv_2mortal(newSVpv(file,0)));
234 rv = call_sv(_PLfuse_callbacks[6],G_SCALAR);
243 DEBUGf("rmdir end: %i\n",rv);
248 int _PLfuse_symlink (const char *file, const char *new) {
251 DEBUGf("symlink begin\n");
255 XPUSHs(sv_2mortal(newSVpv(file,0)));
256 XPUSHs(sv_2mortal(newSVpv(new,0)));
258 rv = call_sv(_PLfuse_callbacks[7],G_SCALAR);
267 DEBUGf("symlink end: %i\n",rv);
272 int _PLfuse_rename (const char *file, const char *new) {
275 DEBUGf("rename begin\n");
279 XPUSHs(sv_2mortal(newSVpv(file,0)));
280 XPUSHs(sv_2mortal(newSVpv(new,0)));
282 rv = call_sv(_PLfuse_callbacks[8],G_SCALAR);
291 DEBUGf("rename end: %i\n",rv);
296 int _PLfuse_link (const char *file, const char *new) {
299 DEBUGf("link begin\n");
303 XPUSHs(sv_2mortal(newSVpv(file,0)));
304 XPUSHs(sv_2mortal(newSVpv(new,0)));
306 rv = call_sv(_PLfuse_callbacks[9],G_SCALAR);
315 DEBUGf("link end: %i\n",rv);
320 int _PLfuse_chmod (const char *file, mode_t mode) {
323 DEBUGf("chmod begin\n");
327 XPUSHs(sv_2mortal(newSVpv(file,0)));
328 XPUSHs(sv_2mortal(newSViv(mode)));
330 rv = call_sv(_PLfuse_callbacks[10],G_SCALAR);
339 DEBUGf("chmod end: %i\n",rv);
344 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
347 DEBUGf("chown begin\n");
351 XPUSHs(sv_2mortal(newSVpv(file,0)));
352 XPUSHs(sv_2mortal(newSViv(uid)));
353 XPUSHs(sv_2mortal(newSViv(gid)));
355 rv = call_sv(_PLfuse_callbacks[11],G_SCALAR);
364 DEBUGf("chown end: %i\n",rv);
369 int _PLfuse_truncate (const char *file, off_t off) {
372 DEBUGf("truncate begin\n");
376 XPUSHs(sv_2mortal(newSVpv(file,0)));
377 XPUSHs(sv_2mortal(newSViv(off)));
379 rv = call_sv(_PLfuse_callbacks[12],G_SCALAR);
388 DEBUGf("truncate end: %i\n",rv);
393 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
396 DEBUGf("utime begin\n");
400 XPUSHs(sv_2mortal(newSVpv(file,0)));
401 XPUSHs(sv_2mortal(newSViv(uti->actime)));
402 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
404 rv = call_sv(_PLfuse_callbacks[13],G_SCALAR);
413 DEBUGf("utime end: %i\n",rv);
418 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
420 int flags = fi->flags;
423 DEBUGf("open begin\n");
427 XPUSHs(sv_2mortal(newSVpv(file,0)));
428 XPUSHs(sv_2mortal(newSViv(flags)));
429 /* Create a hashref containing the details from fi
430 * which we can look at or modify.
432 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
434 #if FUSE_VERSION >= 24
435 hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
436 hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
438 #if FUSE_VERSION >= 29
439 hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
441 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
442 /* All hashref things done */
445 /* Open called with filename, flags */
446 rv = call_sv(_PLfuse_callbacks[14],G_ARRAY);
456 /* We're holding on to the sv reference until
457 * after exit of this function, so we need to
458 * increment its reference count
460 fi->fh = SvREFCNT_inc(sv);
469 /* Success, so copy the file handle which they returned */
470 #if FUSE_VERSION >= 24
472 svp = hv_fetch(fihash, "direct_io", 9, 0);
475 fi->direct_io = SvIV(*svp);
477 svp = hv_fetch(fihash, "keep_cache", 10, 0);
480 fi->keep_cache = SvIV(*svp);
483 #if FUSE_VERSION >= 29
484 svp = hv_fetch(fihash, "nonseekable", 11, 0);
487 fi->nonseekable = SvIV(*svp);
494 DEBUGf("open end: %i\n",rv);
499 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
502 DEBUGf("read begin\n");
506 XPUSHs(sv_2mortal(newSVpv(file,0)));
507 XPUSHs(sv_2mortal(newSViv(buflen)));
508 XPUSHs(sv_2mortal(newSViv(off)));
509 XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
511 rv = call_sv(_PLfuse_callbacks[15],G_SCALAR);
517 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
526 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
528 memcpy(buf,SvPV_nolen(mysv),rv);
534 DEBUGf("read end: %i\n",rv);
539 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
542 DEBUGf("write begin\n");
546 XPUSHs(sv_2mortal(newSVpv(file,0)));
547 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
548 XPUSHs(sv_2mortal(newSViv(off)));
549 XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
551 rv = call_sv(_PLfuse_callbacks[16],G_SCALAR);
560 DEBUGf("write end: %i\n",rv);
565 int _PLfuse_statfs (const char *file, struct statvfs *st) {
568 DEBUGf("statfs begin\n");
573 rv = call_sv(_PLfuse_callbacks[17],G_ARRAY);
575 DEBUGf("statfs got %i params\n",rv);
576 if(rv == 6 || rv == 7) {
582 st->f_namemax = POPi;
583 /* zero and fill-in other */
587 st->f_bavail = st->f_bfree;
588 st->f_favail = st->f_ffree;
596 croak("inappropriate number of returned values from statfs");
605 DEBUGf("statfs end: %i\n",rv);
610 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
613 DEBUGf("flush begin\n");
617 XPUSHs(sv_2mortal(newSVpv(file,0)));
618 XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
620 rv = call_sv(_PLfuse_callbacks[18],G_SCALAR);
629 DEBUGf("flush end: %i\n",rv);
634 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
636 int flags = fi->flags;
638 DEBUGf("release begin\n");
642 XPUSHs(sv_2mortal(newSVpv(file,0)));
643 XPUSHs(sv_2mortal(newSViv(flags)));
644 XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
646 rv = call_sv(_PLfuse_callbacks[19],G_SCALAR);
652 /* We're now finished with the handle that we were given, so
653 * we should decrement its count so that it can be freed.
657 SvREFCNT_dec((SV *)fi->fh);
663 DEBUGf("release end: %i\n",rv);
668 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
670 int flags = fi->flags;
672 DEBUGf("fsync begin\n");
676 XPUSHs(sv_2mortal(newSVpv(file,0)));
677 XPUSHs(sv_2mortal(newSViv(flags)));
678 XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
680 rv = call_sv(_PLfuse_callbacks[20],G_SCALAR);
689 DEBUGf("fsync end: %i\n",rv);
694 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
697 DEBUGf("setxattr begin\n");
701 XPUSHs(sv_2mortal(newSVpv(file,0)));
702 XPUSHs(sv_2mortal(newSVpv(name,0)));
703 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
704 XPUSHs(sv_2mortal(newSViv(flags)));
706 rv = call_sv(_PLfuse_callbacks[21],G_SCALAR);
715 DEBUGf("setxattr end: %i\n",rv);
720 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
723 DEBUGf("getxattr begin\n");
727 XPUSHs(sv_2mortal(newSVpv(file,0)));
728 XPUSHs(sv_2mortal(newSVpv(name,0)));
730 rv = call_sv(_PLfuse_callbacks[22],G_SCALAR);
738 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
746 if ((rv > 0) && (buflen > 0))
751 memcpy(buf,SvPV_nolen(mysv),rv);
758 DEBUGf("getxattr end: %i\n",rv);
763 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
766 DEBUGf("listxattr begin\n");
770 XPUSHs(sv_2mortal(newSVpv(file,0)));
772 prv = call_sv(_PLfuse_callbacks[23],G_ARRAY);
785 /* Always nul terminate */
786 if (list && (size > 0))
796 int s = SvCUR(mysv) + 1;
799 if (p && (size > 0) && (spc >= s))
801 memcpy(p,SvPV_nolen(mysv),s);
809 * If the Perl returned an error, return that.
810 * Otherwise check that the buffer was big enough.
815 if ((size > 0) && (size < total_len))
822 DEBUGf("listxattr end: %i\n",rv);
827 int _PLfuse_removexattr (const char *file, const char *name) {
830 DEBUGf("removexattr begin\n");
834 XPUSHs(sv_2mortal(newSVpv(file,0)));
835 XPUSHs(sv_2mortal(newSVpv(name,0)));
837 rv = call_sv(_PLfuse_callbacks[24],G_SCALAR);
846 DEBUGf("removexattr end: %i\n",rv);
851 struct fuse_operations _available_ops = {
852 getattr: _PLfuse_getattr,
853 readlink: _PLfuse_readlink,
854 getdir: _PLfuse_getdir,
856 readdir: _PLfuse_readdir,
858 mknod: _PLfuse_mknod,
859 mkdir: _PLfuse_mkdir,
860 unlink: _PLfuse_unlink,
861 rmdir: _PLfuse_rmdir,
862 symlink: _PLfuse_symlink,
863 rename: _PLfuse_rename,
865 chmod: _PLfuse_chmod,
866 chown: _PLfuse_chown,
867 truncate: _PLfuse_truncate,
868 utime: _PLfuse_utime,
871 write: _PLfuse_write,
872 statfs: _PLfuse_statfs,
873 flush: _PLfuse_flush,
874 release: _PLfuse_release,
875 fsync: _PLfuse_fsync,
876 setxattr: _PLfuse_setxattr,
877 getxattr: _PLfuse_getxattr,
878 listxattr: _PLfuse_listxattr,
879 removexattr: _PLfuse_removexattr,
882 MODULE = Fuse PACKAGE = Fuse
888 struct fuse_context *fc;
890 fc = fuse_get_context();
893 hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
894 hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
895 hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
896 RETVAL = newRV_noinc((SV*)hash);
906 struct fuse_operations fops =
907 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
908 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
909 int i, fd, debug, threaded;
912 struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
913 struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
916 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
921 threaded = SvIV(ST(1));
923 #ifdef FUSE_USE_ITHREADS
924 master_interp = PERL_GET_CONTEXT;
926 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
927 "mode, but your perl was not built with -Dusethreads. "
928 "Threads are disabled.\n");
932 mountpoint = SvPV_nolen(ST(2));
933 mountopts = SvPV_nolen(ST(3));
934 for(i=0;i<N_CALLBACKS;i++) {
936 /* allow symbolic references, or real code references. */
937 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
938 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
940 #ifdef FUSE_USE_ITHREADS
942 /* note: under 5.8.7, this croaks for code references. */
945 _PLfuse_callbacks[i] = var;
948 croak("invalid callback (%i) passed to perl_fuse_main "
949 "(%s is not a string, code ref, or undef).\n",
950 i+4,SvPVbyte_nolen(var));
954 * XXX: What comes here is just a ridiculous use of the option parsing API
955 * to hack on compatibility with other parts of the new API. First and
956 * foremost, real C argc/argv would be good to get at...
959 (fuse_opt_add_arg(&margs, "") == -1 ||
960 fuse_opt_add_arg(&margs, "-o") == -1 ||
961 fuse_opt_add_arg(&margs, mountopts) == -1)) {
962 fuse_opt_free_args(&margs);
963 croak("out of memory\n");
965 fd = fuse_mount(mountpoint,&margs);
966 fuse_opt_free_args(&margs);
968 croak("could not mount fuse filesystem!\n");
970 if ( fuse_opt_add_arg(&fargs, "") == -1 ||
971 fuse_opt_add_arg(&fargs, "-d") == -1) {
972 fuse_opt_free_args(&fargs);
973 croak("out of memory\n");
976 if (fuse_opt_add_arg(&fargs, "") == -1)
977 croak("out of memory\n");
981 fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)));
983 fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)));
984 fuse_opt_free_args(&fargs);