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.

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