fuse.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build !windows
  17. package api
  18. import (
  19. "io"
  20. "os"
  21. "bazil.org/fuse"
  22. "bazil.org/fuse/fs"
  23. "github.com/ethereum/go-ethereum/log"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. "golang.org/x/net/context"
  26. )
  27. // Data structures used for Fuse filesystem, serving directories and serving files to Fuse driver
  28. type FS struct {
  29. root *Dir
  30. }
  31. type Dir struct {
  32. inode uint64
  33. name string
  34. path string
  35. directories []*Dir
  36. files []*File
  37. }
  38. type File struct {
  39. inode uint64
  40. name string
  41. path string
  42. key storage.Key
  43. swarmApi *Api
  44. fileSize uint64
  45. reader storage.LazySectionReader
  46. }
  47. // Functions which satisfy the Fuse File System requests
  48. func (filesystem *FS) Root() (fs.Node, error) {
  49. return filesystem.root, nil
  50. }
  51. func (directory *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
  52. a.Inode = directory.inode
  53. //TODO: need to get permission as argument
  54. a.Mode = os.ModeDir | 0500
  55. a.Uid = uint32(os.Getuid())
  56. a.Gid = uint32(os.Getegid())
  57. return nil
  58. }
  59. func (directory *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
  60. if directory.files != nil {
  61. for _, n := range directory.files {
  62. if n.name == name {
  63. return n, nil
  64. }
  65. }
  66. }
  67. if directory.directories != nil {
  68. for _, n := range directory.directories {
  69. if n.name == name {
  70. return n, nil
  71. }
  72. }
  73. }
  74. return nil, fuse.ENOENT
  75. }
  76. func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
  77. var children []fuse.Dirent
  78. if d.files != nil {
  79. for _, file := range d.files {
  80. children = append(children, fuse.Dirent{Inode: file.inode, Type: fuse.DT_File, Name: file.name})
  81. }
  82. }
  83. if d.directories != nil {
  84. for _, dir := range d.directories {
  85. children = append(children, fuse.Dirent{Inode: dir.inode, Type: fuse.DT_Dir, Name: dir.name})
  86. }
  87. }
  88. return children, nil
  89. }
  90. func (file *File) Attr(ctx context.Context, a *fuse.Attr) error {
  91. a.Inode = file.inode
  92. //TODO: need to get permission as argument
  93. a.Mode = 0500
  94. a.Uid = uint32(os.Getuid())
  95. a.Gid = uint32(os.Getegid())
  96. reader := file.swarmApi.Retrieve(file.key)
  97. quitC := make(chan bool)
  98. size, err := reader.Size(quitC)
  99. if err != nil {
  100. log.Warn("Couldnt file size of file %s : %v", file.path, err)
  101. a.Size = uint64(0)
  102. }
  103. a.Size = uint64(size)
  104. file.fileSize = a.Size
  105. return nil
  106. }
  107. var _ = fs.HandleReader(&File{})
  108. func (file *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  109. buf := make([]byte, req.Size)
  110. reader := file.swarmApi.Retrieve(file.key)
  111. n, err := reader.ReadAt(buf, req.Offset)
  112. if err == io.ErrUnexpectedEOF || err == io.EOF {
  113. err = nil
  114. }
  115. resp.Data = buf[:n]
  116. return err
  117. }