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.

277 lines
6.0 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. "fmt"
  5. "os"
  6. "path"
  7. "sync"
  8. "bazil.org/fuse"
  9. "bazil.org/fuse/fs"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "path/filepath"
  13. "time"
  14. )
  15. type Dir struct {
  16. Path string
  17. NodeMap map[string]fs.Node
  18. NodeMapLock sync.Mutex
  19. wfs *WFS
  20. }
  21. var _ = fs.Node(&Dir{})
  22. var _ = fs.NodeCreater(&Dir{})
  23. var _ = fs.NodeMkdirer(&Dir{})
  24. var _ = fs.NodeStringLookuper(&Dir{})
  25. var _ = fs.HandleReadDirAller(&Dir{})
  26. var _ = fs.NodeRemover(&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. parent, name := filepath.Split(dir.Path)
  34. var attributes *filer_pb.FuseAttributes
  35. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.GetEntryAttributesRequest{
  37. Name: name,
  38. ParentDir: parent,
  39. }
  40. glog.V(1).Infof("read dir attr: %v", request)
  41. resp, err := client.GetEntryAttributes(context, request)
  42. if err != nil {
  43. glog.V(0).Infof("read dir attr %v: %v", request, err)
  44. return err
  45. }
  46. attributes = resp.Attributes
  47. return nil
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. // glog.V(1).Infof("dir %s: %v", dir.Path, attributes)
  53. // glog.V(1).Infof("dir %s permission: %v", dir.Path, os.FileMode(attributes.FileMode))
  54. attr.Mode = os.FileMode(attributes.FileMode) | os.ModeDir
  55. if dir.Path == "/" && attributes.FileMode == 0 {
  56. attr.Valid = time.Second
  57. }
  58. attr.Mtime = time.Unix(attributes.Mtime, 0)
  59. attr.Ctime = time.Unix(attributes.Mtime, 0)
  60. attr.Gid = attributes.Gid
  61. attr.Uid = attributes.Uid
  62. return nil
  63. }
  64. func (dir *Dir) newFile(name string, chunks []*filer_pb.FileChunk) *File {
  65. return &File{
  66. Name: name,
  67. dir: dir,
  68. wfs: dir.wfs,
  69. // attributes: &filer_pb.FuseAttributes{},
  70. Chunks: chunks,
  71. }
  72. }
  73. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  74. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  75. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  76. request := &filer_pb.CreateEntryRequest{
  77. Directory: dir.Path,
  78. Entry: &filer_pb.Entry{
  79. Name: req.Name,
  80. IsDirectory: req.Mode&os.ModeDir > 0,
  81. Attributes: &filer_pb.FuseAttributes{
  82. Mtime: time.Now().Unix(),
  83. Crtime: time.Now().Unix(),
  84. FileMode: uint32(req.Mode),
  85. Uid: req.Uid,
  86. Gid: req.Gid,
  87. },
  88. },
  89. }
  90. glog.V(1).Infof("create: %v", request)
  91. if _, err := client.CreateEntry(ctx, request); err != nil {
  92. return fmt.Errorf("create file: %v", err)
  93. }
  94. return nil
  95. })
  96. if err == nil {
  97. file := dir.newFile(req.Name, nil)
  98. dir.NodeMap[req.Name] = file
  99. file.isOpen = true
  100. return file, &FileHandle{
  101. f: file,
  102. dirtyPages: newDirtyPages(file),
  103. RequestId: req.Header.ID,
  104. NodeId: req.Header.Node,
  105. Uid: req.Uid,
  106. Gid: req.Gid,
  107. }, nil
  108. }
  109. return nil, nil, err
  110. }
  111. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  112. dir.NodeMapLock.Lock()
  113. defer dir.NodeMapLock.Unlock()
  114. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  115. request := &filer_pb.CreateEntryRequest{
  116. Directory: dir.Path,
  117. Entry: &filer_pb.Entry{
  118. Name: req.Name,
  119. IsDirectory: true,
  120. Attributes: &filer_pb.FuseAttributes{
  121. Mtime: time.Now().Unix(),
  122. Crtime: time.Now().Unix(),
  123. FileMode: uint32(req.Mode),
  124. Uid: req.Uid,
  125. Gid: req.Gid,
  126. },
  127. },
  128. }
  129. glog.V(1).Infof("mkdir: %v", request)
  130. if _, err := client.CreateEntry(ctx, request); err != nil {
  131. glog.V(0).Infof("mkdir %v: %v", request, err)
  132. return fmt.Errorf("make dir: %v", err)
  133. }
  134. return nil
  135. })
  136. if err == nil {
  137. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  138. dir.NodeMap[req.Name] = node
  139. return node, nil
  140. }
  141. return nil, err
  142. }
  143. func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err error) {
  144. dir.NodeMapLock.Lock()
  145. defer dir.NodeMapLock.Unlock()
  146. if dir.NodeMap == nil {
  147. dir.NodeMap = make(map[string]fs.Node)
  148. }
  149. if node, ok := dir.NodeMap[name]; ok {
  150. return node, nil
  151. }
  152. var entry *filer_pb.Entry
  153. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  154. request := &filer_pb.LookupDirectoryEntryRequest{
  155. Directory: dir.Path,
  156. Name: name,
  157. }
  158. glog.V(4).Infof("lookup directory entry: %v", request)
  159. resp, err := client.LookupDirectoryEntry(ctx, request)
  160. if err != nil {
  161. return err
  162. }
  163. entry = resp.Entry
  164. return nil
  165. })
  166. if entry != nil {
  167. if entry.IsDirectory {
  168. node = &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}
  169. } else {
  170. node = dir.newFile(name, entry.Chunks)
  171. }
  172. dir.NodeMap[name] = node
  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. }
  182. glog.V(4).Infof("read directory: %v", request)
  183. resp, err := client.ListEntries(ctx, request)
  184. if err != nil {
  185. return err
  186. }
  187. for _, entry := range resp.Entries {
  188. if entry.IsDirectory {
  189. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  190. ret = append(ret, dirent)
  191. } else {
  192. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  193. ret = append(ret, dirent)
  194. dir.wfs.listDirectoryEntriesCache.Set(dir.Path+"/"+entry.Name, entry, 300*time.Millisecond)
  195. }
  196. }
  197. return nil
  198. })
  199. return ret, err
  200. }
  201. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  202. dir.NodeMapLock.Lock()
  203. defer dir.NodeMapLock.Unlock()
  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. }
  210. glog.V(1).Infof("remove directory entry: %v", request)
  211. _, err := client.DeleteEntry(ctx, request)
  212. if err != nil {
  213. return err
  214. }
  215. delete(dir.NodeMap, req.Name)
  216. return nil
  217. })
  218. }