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.

112 lines
2.6 KiB

  1. // +build linux darwin
  2. package main
  3. import (
  4. "bazil.org/fuse"
  5. "bazil.org/fuse/fs"
  6. "code.google.com/p/weed-fs/go/filer"
  7. "code.google.com/p/weed-fs/go/glog"
  8. "code.google.com/p/weed-fs/go/storage"
  9. "code.google.com/p/weed-fs/go/util"
  10. "fmt"
  11. "os"
  12. "os/signal"
  13. "runtime"
  14. )
  15. func runMount(cmd *Command, args []string) bool {
  16. fmt.Printf("This is Weed 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. signalChan := make(chan os.Signal, 1)
  27. signal.Notify(signalChan, os.Interrupt)
  28. go func() {
  29. for _ = range signalChan {
  30. // sig is a ^C, handle it
  31. fuse.Unmount(*mountOptions.dir)
  32. c.Close()
  33. os.Exit(0)
  34. }
  35. }()
  36. err = fs.Serve(c, WFS{})
  37. if err != nil {
  38. fuse.Unmount(*mountOptions.dir)
  39. }
  40. // check if the mount process has an error to report
  41. <-c.Ready
  42. if err := c.MountError; err != nil {
  43. glog.Fatal(err)
  44. }
  45. return true
  46. }
  47. type File struct {
  48. FileId filer.FileId
  49. Name string
  50. }
  51. func (File) Attr() fuse.Attr {
  52. return fuse.Attr{Mode: 0444}
  53. }
  54. func (File) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
  55. return []byte("hello, world\n"), nil
  56. }
  57. type Dir struct {
  58. DirectoryId filer.DirectoryId
  59. Name string
  60. }
  61. func (dir Dir) Attr() fuse.Attr {
  62. return fuse.Attr{Inode: 1, Mode: os.ModeDir | 0555}
  63. }
  64. func (dir Dir) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
  65. files_result, e := filer.ListFiles(*mountOptions.filer, dir.DirectoryId, name)
  66. if e != nil {
  67. return nil, fuse.ENOENT
  68. }
  69. if len(files_result.Files) > 0 {
  70. return File{files_result.Files[0].Id, files_result.Files[0].Name}, nil
  71. }
  72. return nil, fmt.Errorf("File Not Found for %s", name)
  73. }
  74. type WFS struct{}
  75. func (WFS) Root() (fs.Node, fuse.Error) {
  76. return Dir{}, nil
  77. }
  78. func (dir *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
  79. ret := make([]fuse.Dirent, 0)
  80. if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.DirectoryId); e == nil {
  81. for _, d := range dirs.Directories {
  82. dirId := uint64(d.Id)
  83. ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
  84. }
  85. }
  86. if files, e := filer.ListFiles(*mountOptions.filer, dir.DirectoryId, ""); e == nil {
  87. for _, f := range files.Files {
  88. if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
  89. fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key
  90. ret = append(ret, fuse.Dirent{Inode: fileInode, Name: f.Name, Type: fuse.DT_File})
  91. }
  92. }
  93. }
  94. return ret, nil
  95. }