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.

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