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.

321 lines
7.7 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, entry *filer_pb.Entry) *File {
  74. return &File{
  75. Name: name,
  76. dir: dir,
  77. wfs: dir.wfs,
  78. entry: entry,
  79. }
  80. }
  81. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  82. resp *fuse.CreateResponse) (fs.Node, fs.Handle, 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.option.Collection,
  95. Replication: dir.wfs.option.Replication,
  96. TtlSec: dir.wfs.option.TtlSec,
  97. },
  98. },
  99. }
  100. glog.V(1).Infof("create: %v", request)
  101. if request.Entry.IsDirectory {
  102. if err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  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. }); err != nil {
  109. return nil, nil, err
  110. }
  111. }
  112. file := dir.newFile(req.Name, request.Entry)
  113. if !request.Entry.IsDirectory {
  114. file.isOpen = true
  115. }
  116. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  117. fh.dirtyMetadata = true
  118. return file, fh, nil
  119. }
  120. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  121. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  122. request := &filer_pb.CreateEntryRequest{
  123. Directory: dir.Path,
  124. Entry: &filer_pb.Entry{
  125. Name: req.Name,
  126. IsDirectory: true,
  127. Attributes: &filer_pb.FuseAttributes{
  128. Mtime: time.Now().Unix(),
  129. Crtime: time.Now().Unix(),
  130. FileMode: uint32(req.Mode),
  131. Uid: req.Uid,
  132. Gid: req.Gid,
  133. },
  134. },
  135. }
  136. glog.V(1).Infof("mkdir: %v", request)
  137. if _, err := client.CreateEntry(ctx, request); err != nil {
  138. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  139. return fuse.EIO
  140. }
  141. return nil
  142. })
  143. if err == nil {
  144. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  145. return node, nil
  146. }
  147. return nil, err
  148. }
  149. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  150. var entry *filer_pb.Entry
  151. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  152. request := &filer_pb.LookupDirectoryEntryRequest{
  153. Directory: dir.Path,
  154. Name: req.Name,
  155. }
  156. glog.V(4).Infof("lookup directory entry: %v", request)
  157. resp, err := client.LookupDirectoryEntry(ctx, request)
  158. if err != nil {
  159. // glog.V(0).Infof("lookup %s/%s: %v", dir.Path, name, err)
  160. return fuse.ENOENT
  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, req.Name), wfs: dir.wfs, attributes: entry.Attributes}
  168. } else {
  169. node = dir.newFile(req.Name, entry)
  170. }
  171. resp.EntryValid = time.Duration(0)
  172. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  173. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  174. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  175. resp.Attr.Gid = entry.Attributes.Gid
  176. resp.Attr.Uid = entry.Attributes.Uid
  177. return node, nil
  178. }
  179. return nil, fuse.ENOENT
  180. }
  181. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  182. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  183. request := &filer_pb.ListEntriesRequest{
  184. Directory: dir.Path,
  185. Limit: uint32(dir.wfs.option.DirListingLimit),
  186. }
  187. glog.V(4).Infof("read directory: %v", request)
  188. resp, err := client.ListEntries(ctx, request)
  189. if err != nil {
  190. glog.V(0).Infof("list %s: %v", dir.Path, err)
  191. return fuse.EIO
  192. }
  193. for _, entry := range resp.Entries {
  194. if entry.IsDirectory {
  195. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  196. ret = append(ret, dirent)
  197. } else {
  198. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  199. ret = append(ret, dirent)
  200. }
  201. dir.wfs.listDirectoryEntriesCache.Set(dir.Path+"/"+entry.Name, entry, 300*time.Millisecond)
  202. }
  203. return nil
  204. })
  205. return ret, err
  206. }
  207. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  208. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  209. request := &filer_pb.DeleteEntryRequest{
  210. Directory: dir.Path,
  211. Name: req.Name,
  212. IsDirectory: req.Dir,
  213. IsDeleteData: true,
  214. }
  215. glog.V(1).Infof("remove directory entry: %v", request)
  216. _, err := client.DeleteEntry(ctx, request)
  217. if err != nil {
  218. glog.V(0).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  219. return fuse.EIO
  220. }
  221. return nil
  222. })
  223. }
  224. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  225. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  226. if req.Valid.Mode() {
  227. dir.attributes.FileMode = uint32(req.Mode)
  228. }
  229. if req.Valid.Uid() {
  230. dir.attributes.Uid = req.Uid
  231. }
  232. if req.Valid.Gid() {
  233. dir.attributes.Gid = req.Gid
  234. }
  235. if req.Valid.Mtime() {
  236. dir.attributes.Mtime = req.Mtime.Unix()
  237. }
  238. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  239. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  240. request := &filer_pb.UpdateEntryRequest{
  241. Directory: parentDir,
  242. Entry: &filer_pb.Entry{
  243. Name: name,
  244. Attributes: dir.attributes,
  245. },
  246. }
  247. glog.V(1).Infof("set attr directory entry: %v", request)
  248. _, err := client.UpdateEntry(ctx, request)
  249. if err != nil {
  250. glog.V(0).Infof("UpdateEntry %s: %v", dir.Path, err)
  251. return fuse.EIO
  252. }
  253. return nil
  254. })
  255. }