github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/third_party/bazil.org/fuse/mount_darwin.go (about)

     1  // See the file LICENSE for copyright and licensing information.
     2  
     3  // TODO: Rewrite using package syscall not cgo
     4  
     5  package fuse
     6  
     7  /*
     8  
     9  // Adapted from Plan 9 from User Space's src/cmd/9pfuse/fuse.c,
    10  // which carries this notice:
    11  //
    12  // The files in this directory are subject to the following license.
    13  //
    14  // The author of this software is Russ Cox.
    15  //
    16  //         Copyright (c) 2006 Russ Cox
    17  //
    18  // Permission to use, copy, modify, and distribute this software for any
    19  // purpose without fee is hereby granted, provided that this entire notice
    20  // is included in all copies of any software which is or includes a copy
    21  // or modification of this software and in all copies of the supporting
    22  // documentation for such software.
    23  //
    24  // THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
    25  // WARRANTY.  IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR WARRANTY
    26  // OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS
    27  // FITNESS FOR ANY PARTICULAR PURPOSE.
    28  
    29  #include <stdlib.h>
    30  #include <sys/param.h>
    31  #include <sys/mount.h>
    32  #include <unistd.h>
    33  #include <string.h>
    34  #include <stdio.h>
    35  #include <errno.h>
    36  #include <fcntl.h>
    37  
    38  #define nil ((void*)0)
    39  
    40  static int
    41  mountfuse(char *mtpt, char **err)
    42  {
    43  	int i, pid, fd, r;
    44  	char buf[200];
    45  	struct vfsconf vfs;
    46  	char *f;
    47  
    48  	if(getvfsbyname("fusefs", &vfs) < 0){
    49  		if(access(f="/Library/Filesystems/osxfusefs.fs"
    50  			"/Support/load_osxfusefs", 0) < 0){
    51  		         *err = strdup("cannot find load_fusefs");
    52  			return -1;
    53  		}
    54  		if((r=system(f)) < 0){
    55  			snprintf(buf, sizeof buf, "%s: %s", f, strerror(errno));
    56  			*err = strdup(buf);
    57  			return -1;
    58  		}
    59  		if(r != 0){
    60  			snprintf(buf, sizeof buf, "load_fusefs failed: exit %d", r);
    61  			*err = strdup(buf);
    62  			return -1;
    63  		}
    64  		if(getvfsbyname("osxfusefs", &vfs) < 0){
    65  			snprintf(buf, sizeof buf, "getvfsbyname osxfusefs: %s", strerror(errno));
    66  			*err = strdup(buf);
    67  			return -1;
    68  		}
    69  	}
    70  
    71  	// Look for available FUSE device.
    72  	for(i=0;; i++){
    73  		snprintf(buf, sizeof buf, "/dev/osxfuse%d", i);
    74  		if(access(buf, 0) < 0){
    75  			*err = strdup("no available fuse devices");
    76  			return -1;
    77  		}
    78  		if((fd = open(buf, O_RDWR)) >= 0)
    79  			break;
    80  	}
    81  
    82  	pid = fork();
    83  	if(pid < 0)
    84  		return -1;
    85  	if(pid == 0){
    86  		snprintf(buf, sizeof buf, "%d", fd);
    87  		setenv("MOUNT_FUSEFS_CALL_BY_LIB", "", 1);
    88  		// Different versions of MacFUSE put the
    89  		// mount_fusefs binary in different places.
    90  		// Try all.
    91  		// Leopard location
    92  		setenv("MOUNT_FUSEFS_DAEMON_PATH",
    93  			   "/Library/Filesystems/osxfusefs.fs/Support/mount_osxfusefs", 1);
    94  		execl("/Library/Filesystems/osxfusefs.fs/Support/mount_osxfusefs",
    95  			  "mount_osxfusefs",
    96  			  "-o", "iosize=4096", buf, mtpt, nil);
    97  		fprintf(stderr, "exec mount_osxfusefs: %s\n", strerror(errno));
    98  		_exit(1);
    99  	}
   100  	return fd;
   101  }
   102  
   103  */
   104  import "C"
   105  
   106  import "unsafe"
   107  
   108  func mount(dir string) (int, string) {
   109  	errp := (**C.char)(C.malloc(16))
   110  	*errp = nil
   111  	defer C.free(unsafe.Pointer(errp))
   112  	cdir := C.CString(dir)
   113  	defer C.free(unsafe.Pointer(cdir))
   114  	fd := C.mountfuse(cdir, errp)
   115  	var err string
   116  	if *errp != nil {
   117  		err = C.GoString(*errp)
   118  	}
   119  	return int(fd), err
   120  }