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.

363 lines
9.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 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. var entry *filer_pb.Entry
  36. if item != nil && !item.Expired() {
  37. entry = item.Value().(*filer_pb.Entry)
  38. }
  39. if entry != nil {
  40. entry := item.Value().(*filer_pb.Entry)
  41. attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  42. attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  43. attr.Mode = os.FileMode(entry.Attributes.FileMode)
  44. attr.Gid = entry.Attributes.Gid
  45. attr.Uid = entry.Attributes.Uid
  46. return nil
  47. }
  48. glog.V(3).Infof("dir Attr cache miss %s", dir.Path)
  49. entry, err := filer2.GetEntry(ctx, dir.wfs, dir.Path)
  50. if err != nil {
  51. glog.V(2).Infof("read dir %s attr: %v, error: %v", dir.Path, dir.attributes, err)
  52. return err
  53. }
  54. if entry == nil {
  55. return fuse.ENOENT
  56. }
  57. dir.attributes = entry.Attributes
  58. glog.V(2).Infof("dir %s: %v perm: %v", dir.Path, dir.attributes, os.FileMode(dir.attributes.FileMode))
  59. attr.Mode = os.FileMode(dir.attributes.FileMode) | os.ModeDir
  60. attr.Mtime = time.Unix(dir.attributes.Mtime, 0)
  61. attr.Ctime = time.Unix(dir.attributes.Crtime, 0)
  62. attr.Gid = dir.attributes.Gid
  63. attr.Uid = dir.attributes.Uid
  64. return nil
  65. }
  66. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  67. attr.Uid = dir.wfs.option.MountUid
  68. attr.Gid = dir.wfs.option.MountGid
  69. attr.Mode = dir.wfs.option.MountMode
  70. attr.Crtime = dir.wfs.option.MountCtime
  71. attr.Ctime = dir.wfs.option.MountCtime
  72. attr.Mtime = dir.wfs.option.MountMtime
  73. attr.Atime = dir.wfs.option.MountMtime
  74. }
  75. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) *File {
  76. return &File{
  77. Name: name,
  78. dir: dir,
  79. wfs: dir.wfs,
  80. entry: entry,
  81. entryViewCache: nil,
  82. }
  83. }
  84. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  85. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  86. request := &filer_pb.CreateEntryRequest{
  87. Directory: dir.Path,
  88. Entry: &filer_pb.Entry{
  89. Name: req.Name,
  90. IsDirectory: req.Mode&os.ModeDir > 0,
  91. Attributes: &filer_pb.FuseAttributes{
  92. Mtime: time.Now().Unix(),
  93. Crtime: time.Now().Unix(),
  94. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  95. Uid: req.Uid,
  96. Gid: req.Gid,
  97. Collection: dir.wfs.option.Collection,
  98. Replication: dir.wfs.option.Replication,
  99. TtlSec: dir.wfs.option.TtlSec,
  100. },
  101. },
  102. }
  103. glog.V(1).Infof("create: %v", request)
  104. if request.Entry.IsDirectory {
  105. if err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  106. if _, err := client.CreateEntry(ctx, request); err != nil {
  107. glog.V(0).Infof("create %s/%s: %v", dir.Path, req.Name, err)
  108. return fuse.EIO
  109. }
  110. return nil
  111. }); err != nil {
  112. return nil, nil, err
  113. }
  114. }
  115. file := dir.newFile(req.Name, request.Entry)
  116. if !request.Entry.IsDirectory {
  117. file.isOpen = true
  118. }
  119. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  120. fh.dirtyMetadata = true
  121. return file, fh, nil
  122. }
  123. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  124. err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  125. request := &filer_pb.CreateEntryRequest{
  126. Directory: dir.Path,
  127. Entry: &filer_pb.Entry{
  128. Name: req.Name,
  129. IsDirectory: true,
  130. Attributes: &filer_pb.FuseAttributes{
  131. Mtime: time.Now().Unix(),
  132. Crtime: time.Now().Unix(),
  133. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  134. Uid: req.Uid,
  135. Gid: req.Gid,
  136. },
  137. },
  138. }
  139. glog.V(1).Infof("mkdir: %v", request)
  140. if _, err := client.CreateEntry(ctx, request); err != nil {
  141. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  142. return fuse.EIO
  143. }
  144. return nil
  145. })
  146. if err == nil {
  147. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  148. return node, nil
  149. }
  150. return nil, err
  151. }
  152. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  153. glog.V(4).Infof("dir Lookup %s: %s", dir.Path, req.Name)
  154. var entry *filer_pb.Entry
  155. fullFilePath := path.Join(dir.Path, req.Name)
  156. item := dir.wfs.listDirectoryEntriesCache.Get(fullFilePath)
  157. if item != nil && !item.Expired() {
  158. entry = item.Value().(*filer_pb.Entry)
  159. }
  160. if entry == nil {
  161. glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  162. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  163. if err != nil {
  164. return nil, err
  165. }
  166. dir.wfs.listDirectoryEntriesCache.Set(fullFilePath, entry, 5*time.Minute)
  167. } else {
  168. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  169. }
  170. if entry != nil {
  171. if entry.IsDirectory {
  172. node = &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs, attributes: entry.Attributes}
  173. } else {
  174. node = dir.newFile(req.Name, entry)
  175. }
  176. resp.EntryValid = time.Duration(0)
  177. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  178. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  179. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  180. resp.Attr.Gid = entry.Attributes.Gid
  181. resp.Attr.Uid = entry.Attributes.Uid
  182. return node, nil
  183. }
  184. return nil, fuse.ENOENT
  185. }
  186. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  187. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  188. cacheTtl := 5 * time.Minute
  189. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, dir.Path, "", func(entry *filer_pb.Entry, isLast bool) {
  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(path.Join(dir.Path, entry.Name), entry, cacheTtl)
  198. })
  199. if readErr != nil {
  200. glog.V(0).Infof("list %s: %v", dir.Path, err)
  201. return ret, fuse.EIO
  202. }
  203. return ret, err
  204. }
  205. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  206. if !req.Dir {
  207. return dir.removeOneFile(ctx, req)
  208. }
  209. return dir.removeFolder(ctx, req)
  210. }
  211. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  212. entry, err := filer2.GetEntry(ctx, dir.wfs, path.Join(dir.Path, req.Name))
  213. if err != nil {
  214. return err
  215. }
  216. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  217. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  218. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  219. request := &filer_pb.DeleteEntryRequest{
  220. Directory: dir.Path,
  221. Name: req.Name,
  222. IsDeleteData: false,
  223. }
  224. glog.V(3).Infof("remove file: %v", request)
  225. _, err := client.DeleteEntry(ctx, request)
  226. if err != nil {
  227. glog.V(3).Infof("remove file %s/%s: %v", dir.Path, req.Name, err)
  228. return fuse.ENOENT
  229. }
  230. return nil
  231. })
  232. }
  233. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  234. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  235. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  236. request := &filer_pb.DeleteEntryRequest{
  237. Directory: dir.Path,
  238. Name: req.Name,
  239. IsDeleteData: true,
  240. }
  241. glog.V(3).Infof("remove directory entry: %v", request)
  242. _, err := client.DeleteEntry(ctx, request)
  243. if err != nil {
  244. glog.V(3).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  245. return fuse.ENOENT
  246. }
  247. return nil
  248. })
  249. }
  250. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  251. if dir.attributes == nil {
  252. return nil
  253. }
  254. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  255. if req.Valid.Mode() {
  256. dir.attributes.FileMode = uint32(req.Mode)
  257. }
  258. if req.Valid.Uid() {
  259. dir.attributes.Uid = req.Uid
  260. }
  261. if req.Valid.Gid() {
  262. dir.attributes.Gid = req.Gid
  263. }
  264. if req.Valid.Mtime() {
  265. dir.attributes.Mtime = req.Mtime.Unix()
  266. }
  267. dir.wfs.listDirectoryEntriesCache.Delete(dir.Path)
  268. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  269. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  270. request := &filer_pb.UpdateEntryRequest{
  271. Directory: parentDir,
  272. Entry: &filer_pb.Entry{
  273. Name: name,
  274. Attributes: dir.attributes,
  275. },
  276. }
  277. glog.V(1).Infof("set attr directory entry: %v", request)
  278. _, err := client.UpdateEntry(ctx, request)
  279. if err != nil {
  280. glog.V(0).Infof("UpdateEntry %s: %v", dir.Path, err)
  281. return fuse.EIO
  282. }
  283. return nil
  284. })
  285. }