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(); {
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
28 # define FUSE_CONTEXT_POST
30 #include <fuse/fuse.h>
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) {
46 DEBUGf("getattr begin: %s\n",file);
50 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
52 rv = call_sv(_PLfuse_callbacks[0],G_ARRAY);
56 fprintf(stderr,"inappropriate number of returned values from getattr\n");
63 result->st_blocks = POPi;
64 result->st_blksize = POPi;
65 result->st_ctime = POPi;
66 result->st_mtime = POPi;
67 result->st_atime = POPi;
68 result->st_size = POPi;
69 result->st_rdev = POPi;
70 result->st_gid = POPi;
71 result->st_uid = POPi;
72 result->st_nlink = POPi;
73 result->st_mode = POPi;
74 /*result->st_ino =*/ POPi;
75 result->st_dev = POPi;
81 DEBUGf("getattr end: %i\n",rv);
86 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
94 DEBUGf("readlink begin\n");
98 XPUSHs(sv_2mortal(newSVpv(file,0)));
100 rv = call_sv(_PLfuse_callbacks[1],G_SCALAR);
106 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
109 strncpy(buf,SvPV_nolen(mysv),buflen);
117 DEBUGf("readlink end: %i\n",rv);
122 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
126 DEBUGf("getdir begin\n");
130 XPUSHs(sv_2mortal(newSVpv(file,0)));
132 prv = call_sv(_PLfuse_callbacks[2],G_ARRAY);
139 fprintf(stderr,"getdir() handler returned nothing!\n");
145 DEBUGf("getdir end: %i\n",rv);
150 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
156 DEBUGf("mknod begin\n");
160 XPUSHs(sv_2mortal(newSVpv(file,0)));
161 XPUSHs(sv_2mortal(newSViv(mode)));
162 XPUSHs(sv_2mortal(newSViv(dev)));
164 rv = call_sv(_PLfuse_callbacks[3],G_SCALAR);
173 DEBUGf("mknod end: %i\n",rv);
178 int _PLfuse_mkdir (const char *file, mode_t mode) {
184 DEBUGf("mkdir begin\n");
188 XPUSHs(sv_2mortal(newSVpv(file,0)));
189 XPUSHs(sv_2mortal(newSViv(mode)));
191 rv = call_sv(_PLfuse_callbacks[4],G_SCALAR);
200 DEBUGf("mkdir end: %i\n",rv);
206 int _PLfuse_unlink (const char *file) {
212 DEBUGf("unlink begin\n");
216 XPUSHs(sv_2mortal(newSVpv(file,0)));
218 rv = call_sv(_PLfuse_callbacks[5],G_SCALAR);
227 DEBUGf("unlink end: %i\n",rv);
232 int _PLfuse_rmdir (const char *file) {
238 DEBUGf("rmdir begin\n");
242 XPUSHs(sv_2mortal(newSVpv(file,0)));
244 rv = call_sv(_PLfuse_callbacks[6],G_SCALAR);
253 DEBUGf("rmdir end: %i\n",rv);
258 int _PLfuse_symlink (const char *file, const char *new) {
264 DEBUGf("symlink begin\n");
268 XPUSHs(sv_2mortal(newSVpv(file,0)));
269 XPUSHs(sv_2mortal(newSVpv(new,0)));
271 rv = call_sv(_PLfuse_callbacks[7],G_SCALAR);
280 DEBUGf("symlink end: %i\n",rv);
285 int _PLfuse_rename (const char *file, const char *new) {
291 DEBUGf("rename begin\n");
295 XPUSHs(sv_2mortal(newSVpv(file,0)));
296 XPUSHs(sv_2mortal(newSVpv(new,0)));
298 rv = call_sv(_PLfuse_callbacks[8],G_SCALAR);
307 DEBUGf("rename end: %i\n",rv);
312 int _PLfuse_link (const char *file, const char *new) {
318 DEBUGf("link begin\n");
322 XPUSHs(sv_2mortal(newSVpv(file,0)));
323 XPUSHs(sv_2mortal(newSVpv(new,0)));
325 rv = call_sv(_PLfuse_callbacks[9],G_SCALAR);
334 DEBUGf("link end: %i\n",rv);
339 int _PLfuse_chmod (const char *file, mode_t mode) {
345 DEBUGf("chmod begin\n");
349 XPUSHs(sv_2mortal(newSVpv(file,0)));
350 XPUSHs(sv_2mortal(newSViv(mode)));
352 rv = call_sv(_PLfuse_callbacks[10],G_SCALAR);
361 DEBUGf("chmod end: %i\n",rv);
366 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
372 DEBUGf("chown begin\n");
376 XPUSHs(sv_2mortal(newSVpv(file,0)));
377 XPUSHs(sv_2mortal(newSViv(uid)));
378 XPUSHs(sv_2mortal(newSViv(gid)));
380 rv = call_sv(_PLfuse_callbacks[11],G_SCALAR);
389 DEBUGf("chown end: %i\n",rv);
394 int _PLfuse_truncate (const char *file, off_t off) {
400 DEBUGf("truncate begin\n");
404 XPUSHs(sv_2mortal(newSVpv(file,0)));
405 XPUSHs(sv_2mortal(newSViv(off)));
407 rv = call_sv(_PLfuse_callbacks[12],G_SCALAR);
416 DEBUGf("truncate end: %i\n",rv);
421 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
427 DEBUGf("utime begin\n");
431 XPUSHs(sv_2mortal(newSVpv(file,0)));
432 XPUSHs(sv_2mortal(newSViv(uti->actime)));
433 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
435 rv = call_sv(_PLfuse_callbacks[13],G_SCALAR);
444 DEBUGf("utime end: %i\n",rv);
449 int _PLfuse_open (const char *file, int flags) {
455 DEBUGf("open begin\n");
459 XPUSHs(sv_2mortal(newSVpv(file,0)));
460 XPUSHs(sv_2mortal(newSViv(flags)));
462 rv = call_sv(_PLfuse_callbacks[14],G_SCALAR);
471 DEBUGf("open end: %i\n",rv);
476 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off) {
481 DEBUGf("read begin\n");
485 XPUSHs(sv_2mortal(newSVpv(file,0)));
486 XPUSHs(sv_2mortal(newSViv(buflen)));
487 XPUSHs(sv_2mortal(newSViv(off)));
489 rv = call_sv(_PLfuse_callbacks[15],G_SCALAR);
495 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
504 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
506 memcpy(buf,SvPV_nolen(mysv),rv);
512 DEBUGf("read end: %i\n",rv);
517 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off) {
522 DEBUGf("write begin\n");
526 XPUSHs(sv_2mortal(newSVpv(file,0)));
527 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
528 XPUSHs(sv_2mortal(newSViv(off)));
530 rv = call_sv(_PLfuse_callbacks[16],G_SCALAR);
539 DEBUGf("write end: %i\n",rv);
544 int _PLfuse_statfs (const char *file, struct statfs *st) {
549 DEBUGf("statfs begin\n");
554 rv = call_sv(_PLfuse_callbacks[17],G_ARRAY);
562 st->f_namelen = POPi;
569 croak("inappropriate number of returned values from statfs");
578 DEBUGf("statfs end: %i\n",rv);
583 int _PLfuse_flush (const char *file) {
588 DEBUGf("flush begin\n");
592 XPUSHs(sv_2mortal(newSVpv(file,0)));
594 rv = call_sv(_PLfuse_callbacks[18],G_SCALAR);
603 DEBUGf("flush end: %i\n",rv);
608 int _PLfuse_release (const char *file, int flags) {
613 DEBUGf("release begin\n");
617 XPUSHs(sv_2mortal(newSVpv(file,0)));
618 XPUSHs(sv_2mortal(newSViv(flags)));
620 rv = call_sv(_PLfuse_callbacks[19],G_SCALAR);
629 DEBUGf("release end: %i\n",rv);
634 int _PLfuse_fsync (const char *file, int flags) {
639 DEBUGf("fsync begin\n");
643 XPUSHs(sv_2mortal(newSVpv(file,0)));
644 XPUSHs(sv_2mortal(newSViv(flags)));
646 rv = call_sv(_PLfuse_callbacks[20],G_SCALAR);
655 DEBUGf("fsync end: %i\n",rv);
660 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
665 DEBUGf("setxattr begin\n");
669 XPUSHs(sv_2mortal(newSVpv(file,0)));
670 XPUSHs(sv_2mortal(newSVpv(name,0)));
671 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
672 XPUSHs(sv_2mortal(newSViv(flags)));
674 rv = call_sv(_PLfuse_callbacks[21],G_SCALAR);
683 DEBUGf("setxattr end: %i\n",rv);
688 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
693 DEBUGf("getxattr begin\n");
697 XPUSHs(sv_2mortal(newSVpv(file,0)));
698 XPUSHs(sv_2mortal(newSVpv(name,0)));
700 rv = call_sv(_PLfuse_callbacks[22],G_SCALAR);
708 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
716 if ((rv > 0) && (buflen > 0))
721 memcpy(buf,SvPV_nolen(mysv),rv);
728 DEBUGf("getxattr end: %i\n",rv);
733 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
738 DEBUGf("listxattr begin\n");
742 XPUSHs(sv_2mortal(newSVpv(file,0)));
744 prv = call_sv(_PLfuse_callbacks[23],G_ARRAY);
758 /* Always nul terminate */
759 if (list && (size > 0))
769 int s = SvCUR(mysv) + 1;
772 if (p && (size > 0) && (spc >= s))
774 memcpy(p,SvPV_nolen(mysv),s);
782 * If the Perl returned an error, return that.
783 * Otherwise check that the buffer was big enough.
788 if ((size > 0) && (size < total_len))
795 DEBUGf("listxattr end: %i\n",rv);
800 int _PLfuse_removexattr (const char *file, const char *name) {
805 DEBUGf("removexattr begin\n");
809 XPUSHs(sv_2mortal(newSVpv(file,0)));
810 XPUSHs(sv_2mortal(newSVpv(name,0)));
812 rv = call_sv(_PLfuse_callbacks[24],G_SCALAR);
821 DEBUGf("removexattr end: %i\n",rv);
826 struct fuse_operations _available_ops = {
827 getattr: _PLfuse_getattr,
828 readlink: _PLfuse_readlink,
829 getdir: _PLfuse_getdir,
830 mknod: _PLfuse_mknod,
831 mkdir: _PLfuse_mkdir,
832 unlink: _PLfuse_unlink,
833 rmdir: _PLfuse_rmdir,
834 symlink: _PLfuse_symlink,
835 rename: _PLfuse_rename,
837 chmod: _PLfuse_chmod,
838 chown: _PLfuse_chown,
839 truncate: _PLfuse_truncate,
840 utime: _PLfuse_utime,
843 write: _PLfuse_write,
844 statfs: _PLfuse_statfs,
845 flush: _PLfuse_flush,
846 release: _PLfuse_release,
847 fsync: _PLfuse_fsync,
848 setxattr: _PLfuse_setxattr,
849 getxattr: _PLfuse_getxattr,
850 listxattr: _PLfuse_listxattr,
851 removexattr: _PLfuse_removexattr,
854 MODULE = Fuse PACKAGE = Fuse
860 struct fuse_operations fops =
861 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
862 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
863 int i, fd, varnum = 0, debug, threaded, have_mnt;
870 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
875 threaded = SvIV(ST(1));
877 #ifdef FUSE_USE_ITHREADS
878 master_interp = PERL_GET_INTERP;
880 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
881 "mode, but your perl was not built with -Dusethreads. "
882 "Threads are disabled.\n");
886 mountpoint = SvPV_nolen(ST(2));
887 mountopts = SvPV_nolen(ST(3));
888 for(i=0;i<N_CALLBACKS;i++) {
890 /* allow symbolic references, or real code references. */
891 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
892 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
894 #ifdef FUSE_USE_ITHREADS
896 /* note: under 5.8.7, this croaks for code references. */
899 _PLfuse_callbacks[i] = var;
902 croak("invalid callback passed to perl_fuse_main "
903 "(%s is not a string, code ref, or undef).\n",
904 i+4,SvPVbyte_nolen(var));
907 /* FIXME: need to pass fusermount arguments */
908 fd = fuse_mount(mountpoint,mountopts);
910 croak("could not mount fuse filesystem!");
912 fuse_loop_mt(fuse_new(fd,debug ? "debug" : NULL,&fops));
914 fuse_loop(fuse_new(fd,debug ? "debug" : NULL,&fops));