func TestNonseekable(t *testing.T) {
	fs := &nonseekFs{FileSystem: pathfs.NewDefaultFileSystem()}
	fs.Length = 200 * 1024

	dir := testutil.TempDir()
	defer os.RemoveAll(dir)
	nfs := pathfs.NewPathNodeFs(fs, nil)
	opts := nodefs.NewOptions()
	opts.Debug = testutil.VerboseTest()
	state, _, err := nodefs.MountRoot(dir, nfs.Root(), opts)
	if err != nil {
		t.Fatalf("failed: %v", err)
	}
	defer state.Unmount()

	go state.Serve()
	if err := state.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}

	f, err := os.Open(dir + "/file")
	if err != nil {
		t.Fatalf("failed: %v", err)
	}
	defer f.Close()

	b := make([]byte, 200)
	n, err := f.ReadAt(b, 20)
	if err == nil || n > 0 {
		t.Errorf("file was opened nonseekable, but seek successful")
	}
}
func setupCacheTest(t *testing.T) (string, *pathfs.PathNodeFs, func()) {
	dir := testutil.TempDir()
	os.Mkdir(dir+"/mnt", 0755)
	os.Mkdir(dir+"/orig", 0755)

	fs := &cacheFs{
		pathfs.NewLoopbackFileSystem(dir + "/orig"),
	}
	pfs := pathfs.NewPathNodeFs(fs, &pathfs.PathNodeFsOptions{Debug: testutil.VerboseTest()})

	opts := nodefs.NewOptions()
	opts.Debug = testutil.VerboseTest()
	state, _, err := nodefs.MountRoot(dir+"/mnt", pfs.Root(), opts)
	if err != nil {
		t.Fatalf("MountNodeFileSystem failed: %v", err)
	}
	go state.Serve()
	if err := state.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}
	return dir, pfs, func() {
		err := state.Unmount()
		if err == nil {
			os.RemoveAll(dir)
		}
	}
}
Exemple #3
0
// this function was borrowed from https://raw.githubusercontent.com/hanwen/go-fuse/master/example/memfs/main.go
func main() {
	// Scans the arg list and sets up flags
	debug := flag.Bool("debug", false, "print debugging messages.")
	flag.Parse()
	if flag.NArg() < 3 {
		fmt.Println("usage: appendfs <mountpoint> <datafile> <metadatafile>")
		os.Exit(2)
	}

	mountPoint := flag.Arg(0)
	fs, err := appendfs.NewAppendFS(flag.Arg(1), flag.Arg(2))
	if err != nil {
		fmt.Printf("Mount fail: %v\n", err)
		os.Exit(1)
	}
	options := nodefs.NewOptions()
	options.Owner = nil
	conn := nodefs.NewFileSystemConnector(fs.Root(), options)
	server, err := fuse.NewServer(conn.RawFS(), mountPoint, nil)
	if err != nil {
		fmt.Printf("Mount fail: %v\n", err)
		os.Exit(1)
	}
	server.SetDebug(*debug)
	fmt.Println("Mounted!")
	server.Serve()
	fmt.Println("Closing filesystem")
	err = fs.Close()
	if err != nil {
		fmt.Printf("Unmount fail: %v\n", err)
		os.Exit(1)
	}
}
Exemple #4
0
func setupFAttrTest(t *testing.T, fs pathfs.FileSystem) (dir string, clean func()) {
	dir, err := ioutil.TempDir("", "go-fuse-fsetattr_test")
	if err != nil {
		t.Fatalf("TempDir failed: %v", err)
	}
	nfs := pathfs.NewPathNodeFs(fs, nil)
	opts := nodefs.NewOptions()
	opts.Debug = VerboseTest()

	state, _, err := nodefs.MountRoot(dir, nfs.Root(), opts)
	if err != nil {
		t.Fatalf("MountNodeFileSystem failed: %v", err)
	}

	go state.Serve()
	if err := state.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}

	if state.KernelSettings().Flags&fuse.CAP_FILE_OPS == 0 {
		t.Skip("Mount does not support file operations")
	}

	return dir, func() {
		if err := state.Unmount(); err != nil {
			t.Errorf("cleanup: Unmount: %v", err)
		} else {
			os.RemoveAll(dir)
		}
	}
}
Exemple #5
0
func defaultReadTest(t *testing.T) (root string, cleanup func()) {
	fs := &DefaultReadFS{
		FileSystem: pathfs.NewDefaultFileSystem(),
		size:       22,
	}

	var err error
	dir, err := ioutil.TempDir("", "go-fuse")
	if err != nil {
		t.Fatalf("TempDir failed: %v", err)
	}
	pathfs := pathfs.NewPathNodeFs(fs, nil)
	opts := nodefs.NewOptions()
	opts.Debug = VerboseTest()

	state, _, err := nodefs.MountRoot(dir, pathfs.Root(), opts)
	if err != nil {
		t.Fatalf("MountNodeFileSystem failed: %v", err)
	}
	go state.Serve()
	if err := state.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}
	return dir, func() {
		state.Unmount()
		os.Remove(dir)
	}
}
func TestDefaultXAttr(t *testing.T) {
	dir := testutil.TempDir()
	defer os.RemoveAll(dir)

	root := &xattrNode{
		Node: nodefs.NewDefaultNode(),
	}

	opts := nodefs.NewOptions()
	opts.Debug = testutil.VerboseTest()
	s, _, err := nodefs.MountRoot(dir, root, opts)
	if err != nil {
		t.Fatalf("MountRoot: %v", err)
	}
	go s.Serve()
	if err := s.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}

	defer s.Unmount()

	var data [1024]byte
	sz, err := syscall.Getxattr(filepath.Join(dir, "child"), "attr", data[:])
	if err != nil {
		t.Fatalf("Getxattr: %v", err)
	} else if val := string(data[:sz]); val != "value" {
		t.Fatalf("got %v, want 'value'", val)
	}
}
Exemple #7
0
func main() {
	fsdebug := flag.Bool("fs-debug", false, "switch on FS debugging")
	p4port := flag.String("p4-server", "", "address for P4 server")
	p4binary := flag.String("p4-binary", "p4", "binary for P4 commandline client")
	backingDir := flag.String("backing", "", "directory to store file contents.")
	profile := flag.String("profile", "", "record cpu profile.")
	flag.Parse()

	if len(flag.Args()) != 1 {
		log.Fatal("Usage: p4fs MOUNT-POINT")
	}
	mountpoint := flag.Arg(0)

	opts := p4.ConnOptions{
		Binary:  *p4binary,
		Address: *p4port,
	}
	p4conn := p4.NewConn(opts)

	if *backingDir == "" {
		d, err := ioutil.TempDir("", "p4fs")
		if err != nil {
			log.Fatalf("TempDir failed: %v", err)
		}
		*backingDir = d
		defer os.RemoveAll(d)
	}

	root := NewP4FSRoot(p4conn, *backingDir)
	conn := nodefs.NewFileSystemConnector(root, nodefs.NewOptions())

	mount, err := fuse.NewServer(conn.RawFS(), mountpoint, nil)
	if err != nil {
		log.Fatalf("mount failed: %v", err)
	}

	conn.SetDebug(*fsdebug)
	mount.SetDebug(*fsdebug)
	log.Println("starting FUSE.")

	if *profile != "" {
		profFile, err := os.Create(*profile)
		if err != nil {
			log.Fatalf("os.Create: %v", err)
		}
		pprof.StartCPUProfile(profFile)
		defer pprof.StopCPUProfile()
	}

	mount.Serve()
}
Exemple #8
0
func TestOwnerDefault(t *testing.T) {
	wd, cleanup := setupOwnerTest(t, nodefs.NewOptions())
	defer cleanup()

	var stat syscall.Stat_t
	err := syscall.Lstat(wd+"/foo", &stat)
	if err != nil {
		t.Fatalf("Lstat failed: %v", err)
	}

	if int(stat.Uid) != os.Getuid() || int(stat.Gid) != os.Getgid() {
		t.Fatal("Should use current uid for mount")
	}
}
func setupWithoutServer(t *testing.T) (workdir string, cleanup func()) {
	tmpDir, err := ioutil.TempDir("", "ec2metadata-test")
	if err != nil {
		t.Fatalf("creating tempdir failed: %v", err)
	}

	fs := New("")
	nfs := pathfs.NewPathNodeFs(fs, nil)
	state, _, err := nodefs.MountRoot(tmpDir, nfs.Root(), nodefs.NewOptions())
	if err != nil {
		t.Fatalf("mounting filesystem failed: %v", err)
	}

	go state.Serve()

	return tmpDir, func() {
		state.Unmount()
		os.RemoveAll(tmpDir)
	}
}
Exemple #10
0
func main() {
	// Scans the arg list and sets up flags
	debug := flag.Bool("debug", false, "debug on")
	flag.Parse()
	if flag.NArg() < 1 {
		_, prog := filepath.Split(os.Args[0])
		fmt.Printf("usage: %s MOUNTPOINT\n", prog)
		os.Exit(2)
	}

	fs := zipfs.NewMultiZipFs()
	nfs := pathfs.NewPathNodeFs(fs, nil)
	opts := nodefs.NewOptions()
	opts.Debug = *debug
	state, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), opts)
	if err != nil {
		fmt.Printf("Mount fail: %v\n", err)
		os.Exit(1)
	}

	state.Serve()
}
func setup(t *testing.T) (svc *ec2.EC2, dir string, cleanup func()) {
	svc = ec2.New(session.New())
	svc.Handlers.Clear()

	tmpDir, err := ioutil.TempDir("", "ec2metadata-test")
	if err != nil {
		t.Fatalf("creating tempdir failed: %v", err)
	}

	fs := New(svc, "i-123456")
	nfs := pathfs.NewPathNodeFs(fs, nil)
	state, _, err := nodefs.MountRoot(tmpDir, nfs.Root(), nodefs.NewOptions())
	if err != nil {
		t.Fatalf("mounting filesystem failed: %v", err)
	}

	go state.Serve()

	return svc, tmpDir, func() {
		state.Unmount()
		os.RemoveAll(tmpDir)
	}
}
func setup(t *testing.T) (mux *http.ServeMux, workdir string, cleanup func()) {
	mux = http.NewServeMux()
	server := httptest.NewServer(mux)

	tmpDir, err := ioutil.TempDir("", "ec2metadata-test")
	if err != nil {
		t.Fatalf("creating tempdir failed: %v", err)
	}

	fs := New(server.URL + "/")
	nfs := pathfs.NewPathNodeFs(fs, nil)
	state, _, err := nodefs.MountRoot(tmpDir, nfs.Root(), nodefs.NewOptions())
	if err != nil {
		t.Fatalf("mounting filesystem failed: %v", err)
	}

	go state.Serve()

	return mux, tmpDir, func() {
		server.Close()
		state.Unmount()
		os.RemoveAll(tmpDir)
	}
}
Exemple #13
0
func startFs(t *testing.T, useAndroid bool) (root string, cleanup func()) {
	dev, err := mtp.SelectDevice("")
	if err != nil {
		t.Fatalf("SelectDevice failed: %v", err)
	}
	defer func() {
		if dev != nil {
			dev.Close()
		}
	}()

	if err = dev.Configure(); err != nil {
		t.Fatalf("Configure failed: %v", err)
	}

	sids, err := SelectStorages(dev, "")
	if err != nil {
		t.Fatalf("selectStorages failed: %v", err)
	}
	if len(sids) == 0 {
		t.Fatal("no storages found. Unlock device?")
	}
	tempdir, err := ioutil.TempDir("", "mtpfs")
	if err != nil {
		t.Fatal(err)
	}
	opts := DeviceFsOptions{
		Android: useAndroid,
	}
	fs, err := NewDeviceFs(dev, sids, opts)
	if err != nil {
		t.Fatal("NewDeviceFs failed:", err)
	}
	conn := nodefs.NewFileSystemConnector(fs, nodefs.NewOptions())
	rawFs := fuse.NewLockingRawFileSystem(conn.RawFS())
	mount, err := fuse.NewServer(rawFs, tempdir, nil)
	if err != nil {
		t.Fatalf("mount failed: %v", err)
	}

	mount.SetDebug(fuse.VerboseTest())
	dev.MTPDebug = fuse.VerboseTest()
	dev.USBDebug = fuse.VerboseTest()
	dev.DataDebug = fuse.VerboseTest()
	go mount.Serve()

	for i := 0; i < 10; i++ {
		fis, err := ioutil.ReadDir(tempdir)
		if err == nil && len(fis) > 0 {
			root = filepath.Join(tempdir, fis[0].Name())
			break
		}
		time.Sleep(1)
	}

	if root == "" {
		mount.Unmount()
		t.Fatal("could not find entries in mount point.")
	}

	d := dev
	dev = nil
	return root, func() {
		mount.Unmount()
		d.Close()
	}
}
Exemple #14
0
func main() {
	debug := flag.String("debug", "", "comma-separated list of debugging options: usb, data, mtp, fuse")
	usbTimeout := flag.Int("usb-timeout", 5000, "timeout in milliseconds")
	vfat := flag.Bool("vfat", true, "assume removable RAM media uses VFAT, and rewrite names.")
	other := flag.Bool("allow-other", false, "allow other users to access mounted fuse. Default: false.")
	deviceFilter := flag.String("dev", "",
		"regular expression to filter device IDs, "+
			"which are composed of manufacturer/product/serial.")
	storageFilter := flag.String("storage", "", "regular expression to filter storage areas.")
	android := flag.Bool("android", true, "use android extensions if available")
	flag.Parse()

	if len(flag.Args()) != 1 {
		log.Fatalf("Usage: %s [options] MOUNT-POINT\n", os.Args[0])
	}
	mountpoint := flag.Arg(0)

	dev, err := mtp.SelectDevice(*deviceFilter)
	if err != nil {
		log.Fatalf("detect failed: %v", err)
	}
	defer dev.Close()
	debugs := map[string]bool{}
	for _, s := range strings.Split(*debug, ",") {
		debugs[s] = true
	}
	dev.MTPDebug = debugs["mtp"]
	dev.DataDebug = debugs["data"]
	dev.USBDebug = debugs["usb"]
	dev.Timeout = *usbTimeout
	if err = dev.Configure(); err != nil {
		log.Fatalf("Configure failed: %v", err)
	}

	sids, err := fs.SelectStorages(dev, *storageFilter)
	if err != nil {
		log.Fatalf("selectStorages failed: %v", err)
	}

	opts := fs.DeviceFsOptions{
		RemovableVFat: *vfat,
		Android:       *android,
	}
	fs, err := fs.NewDeviceFs(dev, sids, opts)
	if err != nil {
		log.Fatalf("NewDeviceFs failed: %v", err)
	}
	conn := nodefs.NewFileSystemConnector(fs, nodefs.NewOptions())
	rawFs := fuse.NewLockingRawFileSystem(conn.RawFS())

	mOpts := &fuse.MountOptions{
		AllowOther: *other,
	}
	mount, err := fuse.NewServer(rawFs, mountpoint, mOpts)
	if err != nil {
		log.Fatalf("mount failed: %v", err)
	}

	conn.SetDebug(debugs["fuse"] || debugs["fs"])
	mount.SetDebug(debugs["fuse"] || debugs["fs"])
	log.Printf("starting FUSE.")
	mount.Serve()
	fs.OnUnmount()
}