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.

111 lines
2.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // +build linux darwin
  2. package command
  3. import (
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "bazil.org/fuse"
  8. "bazil.org/fuse/fs"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/storage"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "golang.org/x/net/context"
  14. )
  15. func runMount(cmd *Command, args []string) bool {
  16. fmt.Printf("This is SeaweedFS 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 WFS struct{}
  42. func (WFS) Root() (fs.Node, error) {
  43. return Dir{}, nil
  44. }
  45. type Dir struct {
  46. Path string
  47. Id uint64
  48. }
  49. func (dir Dir) Attr(context context.Context, attr *fuse.Attr) error {
  50. attr.Inode = 1
  51. attr.Mode = os.ModeDir | 0555
  52. return nil
  53. }
  54. func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
  55. files_result, e := filer.ListFiles(*mountOptions.filer, dir.Path, name)
  56. if e != nil {
  57. return nil, fuse.ENOENT
  58. }
  59. if len(files_result.Files) > 0 {
  60. return File{files_result.Files[0].Id, files_result.Files[0].Name}, nil
  61. }
  62. return nil, fmt.Errorf("File Not Found for %s", name)
  63. }
  64. func (dir Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
  65. var ret []fuse.Dirent
  66. if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
  67. for _, d := range dirs.Directories {
  68. dirId := uint64(d.Id)
  69. ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
  70. }
  71. }
  72. if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
  73. for _, f := range files.Files {
  74. if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
  75. fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
  76. ret = append(ret, fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File})
  77. }
  78. }
  79. }
  80. return ret, nil
  81. }
  82. type File struct {
  83. FileId filer.FileId
  84. Name string
  85. }
  86. func (File) Attr(context context.Context, attr *fuse.Attr) error {
  87. attr.Inode = 2
  88. attr.Mode = 0444
  89. return nil
  90. }
  91. func (File) ReadAll(ctx context.Context) ([]byte, error) {
  92. return []byte("hello, world\n"), nil
  93. }