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.

104 lines
2.3 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 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(attr *fuse.Attr) {
  45. }
  46. func (File) ReadAll(ctx context.Context) ([]byte, error) {
  47. return []byte("hello, world\n"), nil
  48. }
  49. type Dir struct {
  50. Path string
  51. Id uint64
  52. }
  53. func (dir Dir) Attr(attr *fuse.Attr) {
  54. }
  55. func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
  56. files_result, e := filer.ListFiles(*mountOptions.filer, dir.Path, name)
  57. if e != nil {
  58. return nil, fuse.ENOENT
  59. }
  60. if len(files_result.Files) > 0 {
  61. return File{files_result.Files[0].Id, files_result.Files[0].Name}, nil
  62. }
  63. return nil, fmt.Errorf("File Not Found for %s", name)
  64. }
  65. type WFS struct{}
  66. func (WFS) Root() (fs.Node, error) {
  67. return Dir{}, nil
  68. }
  69. func (dir *Dir) ReadDir(ctx context.Context) ([]fuse.Dirent, error) {
  70. var ret []fuse.Dirent
  71. if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
  72. for _, d := range dirs.Directories {
  73. dirId := uint64(d.Id)
  74. ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
  75. }
  76. }
  77. if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
  78. for _, f := range files.Files {
  79. if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
  80. fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
  81. ret = append(ret, fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File})
  82. }
  83. }
  84. }
  85. return ret, nil
  86. }