fuse_file.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 linux darwin freebsd
  17. package fuse
  18. import (
  19. "errors"
  20. "io"
  21. "os"
  22. "sync"
  23. "bazil.org/fuse"
  24. "bazil.org/fuse/fs"
  25. "github.com/ethereum/go-ethereum/log"
  26. "github.com/ethereum/go-ethereum/swarm/storage"
  27. "golang.org/x/net/context"
  28. )
  29. const (
  30. MaxAppendFileSize = 10485760 // 10Mb
  31. )
  32. var (
  33. errInvalidOffset = errors.New("Invalid offset during write")
  34. errFileSizeMaxLimixReached = errors.New("File size exceeded max limit")
  35. )
  36. var (
  37. _ fs.Node = (*SwarmFile)(nil)
  38. _ fs.HandleReader = (*SwarmFile)(nil)
  39. _ fs.HandleWriter = (*SwarmFile)(nil)
  40. )
  41. type SwarmFile struct {
  42. inode uint64
  43. name string
  44. path string
  45. key storage.Key
  46. fileSize int64
  47. reader storage.LazySectionReader
  48. mountInfo *MountInfo
  49. lock *sync.RWMutex
  50. }
  51. func NewSwarmFile(path, fname string, minfo *MountInfo) *SwarmFile {
  52. newFile := &SwarmFile{
  53. inode: NewInode(),
  54. name: fname,
  55. path: path,
  56. key: nil,
  57. fileSize: -1, // -1 means , file already exists in swarm and you need to just get the size from swarm
  58. reader: nil,
  59. mountInfo: minfo,
  60. lock: &sync.RWMutex{},
  61. }
  62. return newFile
  63. }
  64. func (file *SwarmFile) Attr(ctx context.Context, a *fuse.Attr) error {
  65. a.Inode = file.inode
  66. //TODO: need to get permission as argument
  67. a.Mode = 0700
  68. a.Uid = uint32(os.Getuid())
  69. a.Gid = uint32(os.Getegid())
  70. if file.fileSize == -1 {
  71. reader := file.mountInfo.swarmApi.Retrieve(file.key)
  72. quitC := make(chan bool)
  73. size, err := reader.Size(quitC)
  74. if err != nil {
  75. log.Warn("Couldnt get size of file %s : %v", file.path, err)
  76. }
  77. file.fileSize = size
  78. }
  79. a.Size = uint64(file.fileSize)
  80. return nil
  81. }
  82. func (sf *SwarmFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  83. sf.lock.RLock()
  84. defer sf.lock.RUnlock()
  85. if sf.reader == nil {
  86. sf.reader = sf.mountInfo.swarmApi.Retrieve(sf.key)
  87. }
  88. buf := make([]byte, req.Size)
  89. n, err := sf.reader.ReadAt(buf, req.Offset)
  90. if err == io.ErrUnexpectedEOF || err == io.EOF {
  91. err = nil
  92. }
  93. resp.Data = buf[:n]
  94. sf.reader = nil
  95. return err
  96. }
  97. func (sf *SwarmFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  98. if sf.fileSize == 0 && req.Offset == 0 {
  99. // A new file is created
  100. err := addFileToSwarm(sf, req.Data, len(req.Data))
  101. if err != nil {
  102. return err
  103. }
  104. resp.Size = len(req.Data)
  105. } else if req.Offset <= sf.fileSize {
  106. totalSize := sf.fileSize + int64(len(req.Data))
  107. if totalSize > MaxAppendFileSize {
  108. log.Warn("Append file size reached (%v) : (%v)", sf.fileSize, len(req.Data))
  109. return errFileSizeMaxLimixReached
  110. }
  111. err := appendToExistingFileInSwarm(sf, req.Data, req.Offset, int64(len(req.Data)))
  112. if err != nil {
  113. return err
  114. }
  115. resp.Size = len(req.Data)
  116. } else {
  117. log.Warn("Invalid write request size(%v) : off(%v)", sf.fileSize, req.Offset)
  118. return errInvalidOffset
  119. }
  120. return nil
  121. }