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. "runtime"
  6. "bazil.org/fuse"
  7. "bazil.org/fuse/fs"
  8. "github.com/chrislusf/seaweedfs/go/filer"
  9. "github.com/chrislusf/seaweedfs/go/glog"
  10. "github.com/chrislusf/seaweedfs/go/storage"
  11. "github.com/chrislusf/seaweedfs/go/util"
  12. "golang.org/x/net/context"
  13. )
  14. func runMount(cmd *Command, args []string) bool {
  15. fmt.Printf("This is SeaweedFS 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(context context.Context, attr *fuse.Attr) error {
  45. return nil
  46. }
  47. func (File) ReadAll(ctx context.Context) ([]byte, 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(context context.Context, attr *fuse.Attr) error {
  55. return nil
  56. }
  57. func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, 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, error) {
  69. return Dir{}, nil
  70. }
  71. func (dir *Dir) ReadDir(ctx context.Context) ([]fuse.Dirent, error) {
  72. var ret []fuse.Dirent
  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. }