You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.4 KiB

  1. // +build linux darwin
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "bazil.org/fuse"
  8. "bazil.org/fuse/fs"
  9. "github.com/chrislusf/weed-fs/go/filer"
  10. "github.com/chrislusf/weed-fs/go/glog"
  11. "github.com/chrislusf/weed-fs/go/storage"
  12. "github.com/chrislusf/weed-fs/go/util"
  13. )
  14. func runMount(cmd *Command, args []string) bool {
  15. fmt.Printf("This is Seaweed File System version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
  16. if *mountOptions.dir == "" {
  17. fmt.Printf("Please specify the mount directory via \"-dir\"")
  18. return false
  19. }
  20. c, err := fuse.Mount(*mountOptions.dir)
  21. if err != nil {
  22. glog.Fatal(err)
  23. return false
  24. }
  25. OnInterrupt(func() {
  26. fuse.Unmount(*mountOptions.dir)
  27. c.Close()
  28. })
  29. err = fs.Serve(c, WFS{})
  30. if err != nil {
  31. fuse.Unmount(*mountOptions.dir)
  32. }
  33. // check if the mount process has an error to report
  34. <-c.Ready
  35. if err := c.MountError; err != nil {
  36. glog.Fatal(err)
  37. }
  38. return true
  39. }
  40. type File struct {
  41. FileId filer.FileId
  42. Name string
  43. }
  44. func (File) Attr() fuse.Attr {
  45. return fuse.Attr{Mode: 0444}
  46. }
  47. func (File) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
  48. return []byte("hello, world\n"), nil
  49. }
  50. type Dir struct {
  51. Path string
  52. Id uint64
  53. }
  54. func (dir Dir) Attr() fuse.Attr {
  55. return fuse.Attr{Inode: dir.Id, Mode: os.ModeDir | 0555}
  56. }
  57. func (dir Dir) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
  58. files_result, e := filer.ListFiles(*mountOptions.filer, dir.Path, name)
  59. if e != nil {
  60. return nil, fuse.ENOENT
  61. }
  62. if len(files_result.Files) > 0 {
  63. return File{files_result.Files[0].Id, files_result.Files[0].Name}, nil
  64. }
  65. return nil, fmt.Errorf("File Not Found for %s", name)
  66. }
  67. type WFS struct{}
  68. func (WFS) Root() (fs.Node, fuse.Error) {
  69. return Dir{}, nil
  70. }
  71. func (dir *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
  72. ret := make([]fuse.Dirent, 0)
  73. if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
  74. for _, d := range dirs.Directories {
  75. dirId := uint64(d.Id)
  76. ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
  77. }
  78. }
  79. if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
  80. for _, f := range files.Files {
  81. if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
  82. fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
  83. ret = append(ret, fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File})
  84. }
  85. }
  86. }
  87. return ret, nil
  88. }