fuse_linux.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package main
  11. import (
  12. "fmt"
  13. "os"
  14. "os/signal"
  15. "syscall"
  16. "devt.de/krotik/rufs"
  17. "devt.de/krotik/rufs/export"
  18. "github.com/hanwen/go-fuse/v2/fuse"
  19. "github.com/hanwen/go-fuse/v2/fuse/nodefs"
  20. "github.com/hanwen/go-fuse/v2/fuse/pathfs"
  21. )
  22. /*
  23. setupFuseMount mounts Rufs as a FUSE filesystem.
  24. */
  25. func setupFuseMount(fuseMount *string, tree *rufs.Tree) error {
  26. var err error
  27. var server *fuse.Server
  28. // Create a FUSE mount
  29. fmt.Println(fmt.Sprintf("Mounting: %s", *fuseMount))
  30. // Set up FUSE server
  31. nfs := pathfs.NewPathNodeFs(&export.RufsFuse{
  32. FileSystem: pathfs.NewDefaultFileSystem(),
  33. Tree: tree,
  34. }, nil)
  35. if server, _, err = nodefs.MountRoot(*fuseMount, nfs.Root(), nil); err != nil {
  36. return err
  37. }
  38. // Add an unmount handler
  39. // Attach SIGINT handler - on unix and windows this is send
  40. // when the user presses ^C (Control-C).
  41. sigchan := make(chan os.Signal)
  42. signal.Notify(sigchan, syscall.SIGINT)
  43. go func() {
  44. for true {
  45. signal := <-sigchan
  46. if signal == syscall.SIGINT {
  47. fmt.Println(fmt.Sprintf("Unmounting: %s", *fuseMount))
  48. err := server.Unmount()
  49. if err != nil {
  50. fmt.Println(err)
  51. }
  52. break
  53. }
  54. }
  55. }()
  56. // Run FUSE server
  57. server.Serve()
  58. return err
  59. }