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 = POPi;
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;
422 DEBUGf("open begin\n");
426 XPUSHs(sv_2mortal(newSVpv(file,0)));
427 XPUSHs(sv_2mortal(newSViv(flags)));
429 rv = call_sv(_PLfuse_callbacks[14],G_SCALAR);
438 DEBUGf("open end: %i\n",rv);
443 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
446 DEBUGf("read begin\n");
450 XPUSHs(sv_2mortal(newSVpv(file,0)));
451 XPUSHs(sv_2mortal(newSViv(buflen)));
452 XPUSHs(sv_2mortal(newSViv(off)));
454 rv = call_sv(_PLfuse_callbacks[15],G_SCALAR);
460 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
469 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
471 memcpy(buf,SvPV_nolen(mysv),rv);
477 DEBUGf("read end: %i\n",rv);
482 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
485 DEBUGf("write begin\n");
489 XPUSHs(sv_2mortal(newSVpv(file,0)));
490 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
491 XPUSHs(sv_2mortal(newSViv(off)));
493 rv = call_sv(_PLfuse_callbacks[16],G_SCALAR);
502 DEBUGf("write end: %i\n",rv);
507 int _PLfuse_statfs (const char *file, struct statvfs *st) {
510 DEBUGf("statfs begin\n");
515 rv = call_sv(_PLfuse_callbacks[17],G_ARRAY);
517 DEBUGf("statfs got %i params\n",rv);
518 if(rv == 6 || rv == 7) {
524 st->f_namemax = POPi;
525 /* zero and fill-in other */
529 st->f_bavail = st->f_bfree;
530 st->f_favail = st->f_ffree;
538 croak("inappropriate number of returned values from statfs");
547 DEBUGf("statfs end: %i\n",rv);
552 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
555 DEBUGf("flush begin\n");
559 XPUSHs(sv_2mortal(newSVpv(file,0)));
561 rv = call_sv(_PLfuse_callbacks[18],G_SCALAR);
570 DEBUGf("flush end: %i\n",rv);
575 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
577 int flags = fi->flags;
579 DEBUGf("release begin\n");
583 XPUSHs(sv_2mortal(newSVpv(file,0)));
584 XPUSHs(sv_2mortal(newSViv(flags)));
586 rv = call_sv(_PLfuse_callbacks[19],G_SCALAR);
595 DEBUGf("release end: %i\n",rv);
600 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
602 int flags = fi->flags;
604 DEBUGf("fsync begin\n");
608 XPUSHs(sv_2mortal(newSVpv(file,0)));
609 XPUSHs(sv_2mortal(newSViv(flags)));
611 rv = call_sv(_PLfuse_callbacks[20],G_SCALAR);
620 DEBUGf("fsync end: %i\n",rv);
625 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
628 DEBUGf("setxattr begin\n");
632 XPUSHs(sv_2mortal(newSVpv(file,0)));
633 XPUSHs(sv_2mortal(newSVpv(name,0)));
634 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
635 XPUSHs(sv_2mortal(newSViv(flags)));
637 rv = call_sv(_PLfuse_callbacks[21],G_SCALAR);
646 DEBUGf("setxattr end: %i\n",rv);
651 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
654 DEBUGf("getxattr begin\n");
658 XPUSHs(sv_2mortal(newSVpv(file,0)));
659 XPUSHs(sv_2mortal(newSVpv(name,0)));
661 rv = call_sv(_PLfuse_callbacks[22],G_SCALAR);
669 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
677 if ((rv > 0) && (buflen > 0))
682 memcpy(buf,SvPV_nolen(mysv),rv);
689 DEBUGf("getxattr end: %i\n",rv);
694 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
697 DEBUGf("listxattr begin\n");
701 XPUSHs(sv_2mortal(newSVpv(file,0)));
703 prv = call_sv(_PLfuse_callbacks[23],G_ARRAY);
716 /* Always nul terminate */
717 if (list && (size > 0))
727 int s = SvCUR(mysv) + 1;
730 if (p && (size > 0) && (spc >= s))
732 memcpy(p,SvPV_nolen(mysv),s);
740 * If the Perl returned an error, return that.
741 * Otherwise check that the buffer was big enough.
746 if ((size > 0) && (size < total_len))
753 DEBUGf("listxattr end: %i\n",rv);
758 int _PLfuse_removexattr (const char *file, const char *name) {
761 DEBUGf("removexattr begin\n");
765 XPUSHs(sv_2mortal(newSVpv(file,0)));
766 XPUSHs(sv_2mortal(newSVpv(name,0)));
768 rv = call_sv(_PLfuse_callbacks[24],G_SCALAR);
777 DEBUGf("removexattr end: %i\n",rv);
782 struct fuse_operations _available_ops = {
783 getattr: _PLfuse_getattr,
784 readlink: _PLfuse_readlink,
785 getdir: _PLfuse_getdir,
787 readdir: _PLfuse_readdir,
789 mknod: _PLfuse_mknod,
790 mkdir: _PLfuse_mkdir,
791 unlink: _PLfuse_unlink,
792 rmdir: _PLfuse_rmdir,
793 symlink: _PLfuse_symlink,
794 rename: _PLfuse_rename,
796 chmod: _PLfuse_chmod,
797 chown: _PLfuse_chown,
798 truncate: _PLfuse_truncate,
799 utime: _PLfuse_utime,
802 write: _PLfuse_write,
803 statfs: _PLfuse_statfs,
804 flush: _PLfuse_flush,
805 release: _PLfuse_release,
806 fsync: _PLfuse_fsync,
807 setxattr: _PLfuse_setxattr,
808 getxattr: _PLfuse_getxattr,
809 listxattr: _PLfuse_listxattr,
810 removexattr: _PLfuse_removexattr,
813 MODULE = Fuse PACKAGE = Fuse
819 struct fuse_context *fc;
821 fc = fuse_get_context();
824 hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
825 hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
826 hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
827 RETVAL = newRV_noinc((SV*)hash);
837 struct fuse_operations fops =
838 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
839 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
840 int i, fd, debug, threaded;
843 struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
844 struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
847 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
852 threaded = SvIV(ST(1));
854 #ifdef FUSE_USE_ITHREADS
855 master_interp = PERL_GET_CONTEXT;
857 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
858 "mode, but your perl was not built with -Dusethreads. "
859 "Threads are disabled.\n");
863 mountpoint = SvPV_nolen(ST(2));
864 mountopts = SvPV_nolen(ST(3));
865 for(i=0;i<N_CALLBACKS;i++) {
867 /* allow symbolic references, or real code references. */
868 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
869 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
871 #ifdef FUSE_USE_ITHREADS
873 /* note: under 5.8.7, this croaks for code references. */
876 _PLfuse_callbacks[i] = var;
879 croak("invalid callback passed to perl_fuse_main "
880 "(%s is not a string, code ref, or undef).\n",
881 i+4,SvPVbyte_nolen(var));
885 * XXX: What comes here is just a ridiculous use of the option parsing API
886 * to hack on compatibility with other parts of the new API. First and
887 * foremost, real C argc/argv would be good to get at...
890 (fuse_opt_add_arg(&margs, "") == -1 ||
891 fuse_opt_add_arg(&margs, "-o") == -1 ||
892 fuse_opt_add_arg(&margs, mountopts) == -1)) {
893 fuse_opt_free_args(&margs);
894 croak("out of memory\n");
896 fd = fuse_mount(mountpoint,&margs);
897 fuse_opt_free_args(&margs);
899 croak("could not mount fuse filesystem!\n");
901 if ( fuse_opt_add_arg(&fargs, "") == -1 ||
902 fuse_opt_add_arg(&fargs, "-d") == -1) {
903 fuse_opt_free_args(&fargs);
904 croak("out of memory\n");
907 if (fuse_opt_add_arg(&fargs, "") == -1)
908 croak("out of memory\n");
912 fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)));
914 fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)));
915 fuse_opt_free_args(&fargs);