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.

361 lines
9.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "path"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/fuse"
  11. "github.com/seaweedfs/fuse/fs"
  12. )
  13. type Dir struct {
  14. Path string
  15. wfs *WFS
  16. attributes *filer_pb.FuseAttributes
  17. }
  18. var _ = fs.Node(&Dir{})
  19. var _ = fs.NodeCreater(&Dir{})
  20. var _ = fs.NodeMkdirer(&Dir{})
  21. var _ = fs.NodeRequestLookuper(&Dir{})
  22. var _ = fs.HandleReadDirAller(&Dir{})
  23. var _ = fs.NodeRemover(&Dir{})
  24. var _ = fs.NodeRenamer(&Dir{})
  25. var _ = fs.NodeSetattrer(&Dir{})
  26. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  27. glog.V(3).Infof("dir Attr %s", dir.Path)
  28. // https://github.com/bazil/fuse/issues/196
  29. attr.Valid = time.Second
  30. if dir.Path == dir.wfs.option.FilerMountRootPath {
  31. dir.setRootDirAttributes(attr)
  32. return nil
  33. }
  34. item := dir.wfs.listDirectoryEntriesCache.Get(dir.Path)
  35. if item != nil && !item.Expired() {
  36. glog.V(4).Infof("dir Attr cache hit %s", dir.Path)
  37. entry := item.Value().(*filer_pb.Entry)
  38. attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  39. attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  40. attr.Mode = os.FileMode(entry.Attributes.FileMode)
  41. attr.Gid = entry.Attributes.Gid
  42. attr.Uid = entry.Attributes.Uid
  43. return nil
  44. }
  45. glog.V(3).Infof("dir Attr cache miss %s", dir.Path)
  46. entry, err := filer2.GetEntry(ctx, dir.wfs, dir.Path)
  47. if err != nil {
  48. glog.V(2).Infof("read dir %s attr: %v, error: %v", dir.Path, dir.attributes, err)
  49. return err
  50. }
  51. if entry == nil {
  52. return fuse.ENOENT
  53. }
  54. dir.attributes = entry.Attributes
  55. glog.V(2).Infof("dir %s: %v perm: %v", dir.Path, dir.attributes, os.FileMode(dir.attributes.FileMode))
  56. attr.Mode = os.FileMode(dir.attributes.FileMode) | os.ModeDir
  57. attr.Mtime = time.Unix(dir.attributes.Mtime, 0)
  58. attr.Ctime = time.Unix(dir.attributes.Crtime, 0)
  59. attr.Gid = dir.attributes.Gid
  60. attr.Uid = dir.attributes.Uid
  61. return nil
  62. }
  63. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  64. attr.Uid = dir.wfs.option.MountUid
  65. attr.Gid = dir.wfs.option.MountGid
  66. attr.Mode = dir.wfs.option.MountMode
  67. attr.Crtime = dir.wfs.option.MountCtime
  68. attr.Ctime = dir.wfs.option.MountCtime
  69. attr.Mtime = dir.wfs.option.MountMtime
  70. attr.Atime = dir.wfs.option.MountMtime
  71. }
  72. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) *File {
  73. return &File{
  74. Name: name,
  75. dir: dir,
  76. wfs: dir.wfs,
  77. entry: entry,
  78. entryViewCache: nil,
  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 &^ dir.wfs.option.Umask),
  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(ctx, 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(ctx, 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 &^ dir.wfs.option.Umask),
  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. glog.V(4).Infof("dir Lookup %s: %s", dir.Path, req.Name)
  151. var entry *filer_pb.Entry
  152. fullFilePath := path.Join(dir.Path, req.Name)
  153. item := dir.wfs.listDirectoryEntriesCache.Get(fullFilePath)
  154. if item != nil && !item.Expired() {
  155. entry = item.Value().(*filer_pb.Entry)
  156. }
  157. if entry == nil {
  158. glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  159. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  160. if err != nil {
  161. return nil, err
  162. }
  163. dir.wfs.listDirectoryEntriesCache.Set(fullFilePath, entry, 5*time.Second)
  164. } else {
  165. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  166. }
  167. if entry != nil {
  168. if entry.IsDirectory {
  169. node = &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs, attributes: entry.Attributes}
  170. } else {
  171. node = dir.newFile(req.Name, entry)
  172. }
  173. resp.EntryValid = time.Duration(0)
  174. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  175. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  176. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  177. resp.Attr.Gid = entry.Attributes.Gid
  178. resp.Attr.Uid = entry.Attributes.Uid
  179. return node, nil
  180. }
  181. return nil, fuse.ENOENT
  182. }
  183. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  184. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  185. cacheTtl := 5 * time.Minute
  186. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, dir.Path, "", func(entry *filer_pb.Entry, isLast bool) {
  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(path.Join(dir.Path, entry.Name), entry, cacheTtl)
  195. })
  196. if readErr != nil {
  197. glog.V(0).Infof("list %s: %v", dir.Path, err)
  198. return ret, fuse.EIO
  199. }
  200. return ret, err
  201. }
  202. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  203. if !req.Dir {
  204. return dir.removeOneFile(ctx, req)
  205. }
  206. return dir.removeFolder(ctx, req)
  207. }
  208. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  209. entry, err := filer2.GetEntry(ctx, dir.wfs, path.Join(dir.Path, req.Name))
  210. if err != nil {
  211. return err
  212. }
  213. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  214. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  215. request := &filer_pb.DeleteEntryRequest{
  216. Directory: dir.Path,
  217. Name: req.Name,
  218. IsDeleteData: false,
  219. }
  220. glog.V(3).Infof("remove file: %v", request)
  221. _, err := client.DeleteEntry(ctx, request)
  222. if err != nil {
  223. glog.V(3).Infof("remove file %s/%s: %v", dir.Path, req.Name, err)
  224. return fuse.ENOENT
  225. }
  226. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  227. return nil
  228. })
  229. }
  230. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  231. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  232. request := &filer_pb.DeleteEntryRequest{
  233. Directory: dir.Path,
  234. Name: req.Name,
  235. IsDeleteData: true,
  236. }
  237. glog.V(3).Infof("remove directory entry: %v", request)
  238. _, err := client.DeleteEntry(ctx, request)
  239. if err != nil {
  240. glog.V(3).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  241. return fuse.ENOENT
  242. }
  243. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  244. return nil
  245. })
  246. }
  247. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  248. if dir.attributes == nil {
  249. return nil
  250. }
  251. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  252. if req.Valid.Mode() {
  253. dir.attributes.FileMode = uint32(req.Mode)
  254. }
  255. if req.Valid.Uid() {
  256. dir.attributes.Uid = req.Uid
  257. }
  258. if req.Valid.Gid() {
  259. dir.attributes.Gid = req.Gid
  260. }
  261. if req.Valid.Mtime() {
  262. dir.attributes.Mtime = req.Mtime.Unix()
  263. }
  264. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  265. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  266. request := &filer_pb.UpdateEntryRequest{
  267. Directory: parentDir,
  268. Entry: &filer_pb.Entry{
  269. Name: name,
  270. Attributes: dir.attributes,
  271. },
  272. }
  273. glog.V(1).Infof("set attr directory entry: %v", request)
  274. _, err := client.UpdateEntry(ctx, request)
  275. if err != nil {
  276. glog.V(0).Infof("UpdateEntry %s: %v", dir.Path, err)
  277. return fuse.EIO
  278. }
  279. dir.wfs.listDirectoryEntriesCache.Delete(dir.Path)
  280. return nil
  281. })
  282. }