Example #1
0
func intmain() int {
	bind := flag.String("bind", "", "Address to bind to, e.g. localhost:5956")
	path := flag.String("path", "", "Path to expose, e.g. /mnt/files")

	flag.Parse()

	fmt.Printf("The bind is %s and the path is %s\n", *bind, *path)

	if *bind == "" || *path == "" {
		flag.PrintDefaults()
		return 1
	}

	fs, err := server.NewRealFileSystem(*path)
	if err != nil {
		fmt.Println("Failed to use specified directory: " + err.Error())
		return 1
	}

	if err := server.ServeTCP(*bind, &fs); err != nil {
		fmt.Println("Failed to start server: " + err.Error())
		return 1
	}
	return 0
}
Example #2
0
func TestSimpleMount(t *testing.T) {
	log.Print("Starting test yay")
	mountdir, err := ioutil.TempDir(os.TempDir(), "snf")
	if err != nil {
		t.Error(err.Error())
		return
	}
	defer func() {
		if err := os.Remove(mountdir); err != nil {
			log.Printf("Failed to remove temp dir \"%s\": %s", mountdir, err.Error())
		}
	}()
	log.Printf("Created mount dir at %s", mountdir)
	fs, err := server.NewRealFileSystem(mountdir)
	if err != nil {
		t.Error(err.Error())
		return
	}

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Error(err.Error())
		return
	}
	log.Printf("Listening at %s", ln.Addr())

	serverResultChan := make(chan error, 1)
	go func() {
		conn, err := ln.Accept()
		if err != nil {
			serverResultChan <- err
			return
		}
		ln.Close()
		log.Print("Accepted a client :)")
		s := server.New(conn, conn, &fs)
		serverResultChan <- s.Serve()
	}()

	log.Print("About to mount...")
	mountResultChan := make(chan error, 1)
	go func() {
		mountResultChan <- Mount(ln.Addr().String(), mountdir)
	}()

	mounted := false
	for started := time.Now(); (time.Now().Sub(started) <= (3 * time.Second)) && !mounted; {
		mounted, err = isMounted(mountdir)
		if err != nil {
			t.Errorf("Failed attempting to figure out if dir was mounted or not: %s", err.Error())
			return
		}
		time.Sleep(100 * time.Millisecond)
	}
	if !mounted {
		t.Error("Given up waiting for directory to mount")
		return
	}

	log.Print("Mounted. Unmounting")

	if err != nil {
		t.Errorf("Expecting no error from Mount(), received: %s", err.Error())
		return
	}

	cmd := exec.Command("umount", mountdir)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &out
	if err = cmd.Run(); err != nil {
		t.Errorf("Expected no error when unmounting, received: %s", err.Error())
		return
	}
	if err = <-serverResultChan; err != nil {
		t.Errorf("Received error from server.ServeConnection(): %s", err.Error())
		return
	}
	log.Print("Done")
}