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.

276 lines
6.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path"
  7. "sync"
  8. "bazil.org/fuse"
  9. "bazil.org/fuse/fs"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "path/filepath"
  13. "time"
  14. )
  15. type Dir struct {
  16. Path string
  17. NodeMap map[string]fs.Node
  18. NodeMapLock sync.Mutex
  19. wfs *WFS
  20. }
  21. var _ = fs.Node(&Dir{})
  22. var _ = fs.NodeCreater(&Dir{})
  23. var _ = fs.NodeMkdirer(&Dir{})
  24. var _ = fs.NodeStringLookuper(&Dir{})
  25. var _ = fs.HandleReadDirAller(&Dir{})
  26. var _ = fs.NodeRemover(&Dir{})
  27. func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error {
  28. if dir.Path == "/" {
  29. attr.Valid = time.Second
  30. attr.Mode = os.ModeDir | 0777
  31. return nil
  32. }
  33. parent, name := filepath.Split(dir.Path)
  34. var attributes *filer_pb.FuseAttributes
  35. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.GetEntryAttributesRequest{
  37. Name: name,
  38. ParentDir: parent,
  39. }
  40. glog.V(1).Infof("read dir attr: %v", request)
  41. resp, err := client.GetEntryAttributes(context, request)
  42. if err != nil {
  43. glog.V(0).Infof("read dir attr %v: %v", request, err)
  44. return err
  45. }
  46. attributes = resp.Attributes
  47. return nil
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. // glog.V(1).Infof("dir %s: %v", dir.Path, attributes)
  53. // glog.V(1).Infof("dir %s permission: %v", dir.Path, os.FileMode(attributes.FileMode))
  54. attr.Mode = os.FileMode(attributes.FileMode) | os.ModeDir
  55. if dir.Path == "/" && attributes.FileMode == 0 {
  56. attr.Valid = time.Second
  57. }
  58. attr.Mtime = time.Unix(attributes.Mtime, 0)
  59. attr.Ctime = time.Unix(attributes.Mtime, 0)
  60. attr.Gid = attributes.Gid
  61. attr.Uid = attributes.Uid
  62. return nil
  63. }
  64. func (dir *Dir) newFile(name string, chunks []*filer_pb.FileChunk) *File {
  65. return &File{
  66. Name: name,
  67. dir: dir,
  68. wfs: dir.wfs,
  69. // attributes: &filer_pb.FuseAttributes{},
  70. Chunks: chunks,
  71. }
  72. }
  73. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  74. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  75. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  76. request := &filer_pb.CreateEntryRequest{
  77. Directory: dir.Path,
  78. Entry: &filer_pb.Entry{
  79. Name: req.Name,
  80. IsDirectory: req.Mode&os.ModeDir > 0,
  81. Attributes: &filer_pb.FuseAttributes{
  82. Mtime: time.Now().Unix(),
  83. Crtime: time.Now().Unix(),
  84. FileMode: uint32(req.Mode),
  85. Uid: req.Uid,
  86. Gid: req.Gid,
  87. },
  88. },
  89. }
  90. glog.V(1).Infof("create: %v", request)
  91. if _, err := client.CreateEntry(ctx, request); err != nil {
  92. return fmt.Errorf("create file: %v", err)
  93. }
  94. return nil
  95. })
  96. if err == nil {
  97. file := dir.newFile(req.Name, nil)
  98. dir.NodeMap[req.Name] = file
  99. file.isOpen = true
  100. return file, &FileHandle{
  101. f: file,
  102. RequestId: req.Header.ID,
  103. NodeId: req.Header.Node,
  104. Uid: req.Uid,
  105. Gid: req.Gid,
  106. }, nil
  107. }
  108. return nil, nil, err
  109. }
  110. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  111. dir.NodeMapLock.Lock()
  112. defer dir.NodeMapLock.Unlock()
  113. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  114. request := &filer_pb.CreateEntryRequest{
  115. Directory: dir.Path,
  116. Entry: &filer_pb.Entry{
  117. Name: req.Name,
  118. IsDirectory: true,
  119. Attributes: &filer_pb.FuseAttributes{
  120. Mtime: time.Now().Unix(),
  121. Crtime: time.Now().Unix(),
  122. FileMode: uint32(req.Mode),
  123. Uid: req.Uid,
  124. Gid: req.Gid,
  125. },
  126. },
  127. }
  128. glog.V(1).Infof("mkdir: %v", request)
  129. if _, err := client.CreateEntry(ctx, request); err != nil {
  130. glog.V(0).Infof("mkdir %v: %v", request, err)
  131. return fmt.Errorf("make dir: %v", err)
  132. }
  133. return nil
  134. })
  135. if err == nil {
  136. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  137. dir.NodeMap[req.Name] = node
  138. return node, nil
  139. }
  140. return nil, err
  141. }
  142. func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err error) {
  143. dir.NodeMapLock.Lock()
  144. defer dir.NodeMapLock.Unlock()
  145. if dir.NodeMap == nil {
  146. dir.NodeMap = make(map[string]fs.Node)
  147. }
  148. if node, ok := dir.NodeMap[name]; ok {
  149. return node, nil
  150. }
  151. var entry *filer_pb.Entry
  152. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  153. request := &filer_pb.LookupDirectoryEntryRequest{
  154. Directory: dir.Path,
  155. Name: name,
  156. }
  157. glog.V(4).Infof("lookup directory entry: %v", request)
  158. resp, err := client.LookupDirectoryEntry(ctx, request)
  159. if err != nil {
  160. return err
  161. }
  162. entry = resp.Entry
  163. return nil
  164. })
  165. if entry != nil {
  166. if entry.IsDirectory {
  167. node = &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}
  168. } else {
  169. node = dir.newFile(name, entry.Chunks)
  170. }
  171. dir.NodeMap[name] = node
  172. return node, nil
  173. }
  174. return nil, fuse.ENOENT
  175. }
  176. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  177. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  178. request := &filer_pb.ListEntriesRequest{
  179. Directory: dir.Path,
  180. }
  181. glog.V(4).Infof("read directory: %v", request)
  182. resp, err := client.ListEntries(ctx, request)
  183. if err != nil {
  184. return err
  185. }
  186. for _, entry := range resp.Entries {
  187. if entry.IsDirectory {
  188. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  189. ret = append(ret, dirent)
  190. } else {
  191. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  192. ret = append(ret, dirent)
  193. dir.wfs.listDirectoryEntriesCache.Set(dir.Path+"/"+entry.Name, entry, 300*time.Millisecond)
  194. }
  195. }
  196. return nil
  197. })
  198. return ret, err
  199. }
  200. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  201. dir.NodeMapLock.Lock()
  202. defer dir.NodeMapLock.Unlock()
  203. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  204. request := &filer_pb.DeleteEntryRequest{
  205. Directory: dir.Path,
  206. Name: req.Name,
  207. IsDirectory: req.Dir,
  208. }
  209. glog.V(1).Infof("remove directory entry: %v", request)
  210. _, err := client.DeleteEntry(ctx, request)
  211. if err != nil {
  212. return err
  213. }
  214. delete(dir.NodeMap, req.Name)
  215. return nil
  216. })
  217. }