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.

274 lines
6.5 KiB

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