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.

273 lines
5.9 KiB

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/fs"
  9. "bazil.org/fuse"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "time"
  13. "path/filepath"
  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. FileMode: uint32(req.Mode),
  84. Uid: req.Uid,
  85. Gid: req.Gid,
  86. },
  87. },
  88. }
  89. glog.V(1).Infof("create: %v", request)
  90. if _, err := client.CreateEntry(ctx, request); err != nil {
  91. return fmt.Errorf("create file: %v", err)
  92. }
  93. return nil
  94. })
  95. if err == nil {
  96. file := dir.newFile(req.Name, nil)
  97. dir.NodeMap[req.Name] = file
  98. return file, &FileHandle{
  99. f: file,
  100. RequestId: req.Header.ID,
  101. NodeId: req.Header.Node,
  102. Uid: req.Uid,
  103. Gid: req.Gid,
  104. }, nil
  105. }
  106. return nil, nil, err
  107. }
  108. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  109. dir.NodeMapLock.Lock()
  110. defer dir.NodeMapLock.Unlock()
  111. err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.CreateEntryRequest{
  113. Directory: dir.Path,
  114. Entry: &filer_pb.Entry{
  115. Name: req.Name,
  116. IsDirectory: true,
  117. Attributes: &filer_pb.FuseAttributes{
  118. Mtime: time.Now().Unix(),
  119. FileMode: uint32(req.Mode),
  120. Uid: req.Uid,
  121. Gid: req.Gid,
  122. },
  123. },
  124. }
  125. glog.V(1).Infof("mkdir: %v", request)
  126. if _, err := client.CreateEntry(ctx, request); err != nil {
  127. glog.V(0).Infof("mkdir %v: %v", request, err)
  128. return fmt.Errorf("make dir: %v", err)
  129. }
  130. return nil
  131. })
  132. if err == nil {
  133. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  134. dir.NodeMap[req.Name] = node
  135. return node, nil
  136. }
  137. return nil, err
  138. }
  139. func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err error) {
  140. dir.NodeMapLock.Lock()
  141. defer dir.NodeMapLock.Unlock()
  142. if dir.NodeMap == nil {
  143. dir.NodeMap = make(map[string]fs.Node)
  144. }
  145. if node, ok := dir.NodeMap[name]; ok {
  146. return node, nil
  147. }
  148. var entry *filer_pb.Entry
  149. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  150. request := &filer_pb.LookupDirectoryEntryRequest{
  151. Directory: dir.Path,
  152. Name: name,
  153. }
  154. glog.V(4).Infof("lookup directory entry: %v", request)
  155. resp, err := client.LookupDirectoryEntry(ctx, request)
  156. if err != nil {
  157. return err
  158. }
  159. entry = resp.Entry
  160. return nil
  161. })
  162. if entry != nil {
  163. if entry.IsDirectory {
  164. node = &Dir{Path: path.Join(dir.Path, name), wfs: dir.wfs}
  165. } else {
  166. node = dir.newFile(name, entry.Chunks)
  167. }
  168. dir.NodeMap[name] = node
  169. return node, nil
  170. }
  171. return nil, fuse.ENOENT
  172. }
  173. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  174. err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  175. request := &filer_pb.ListEntriesRequest{
  176. Directory: dir.Path,
  177. }
  178. glog.V(4).Infof("read directory: %v", request)
  179. resp, err := client.ListEntries(ctx, request)
  180. if err != nil {
  181. return err
  182. }
  183. for _, entry := range resp.Entries {
  184. if entry.IsDirectory {
  185. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  186. ret = append(ret, dirent)
  187. } else {
  188. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  189. ret = append(ret, dirent)
  190. dir.wfs.listDirectoryEntriesCache.Set(dir.Path+"/"+entry.Name, entry, 300*time.Millisecond)
  191. }
  192. }
  193. return nil
  194. })
  195. return ret, err
  196. }
  197. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  198. dir.NodeMapLock.Lock()
  199. defer dir.NodeMapLock.Unlock()
  200. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  201. request := &filer_pb.DeleteEntryRequest{
  202. Directory: dir.Path,
  203. Name: req.Name,
  204. IsDirectory: req.Dir,
  205. }
  206. glog.V(1).Infof("remove directory entry: %v", request)
  207. _, err := client.DeleteEntry(ctx, request)
  208. if err != nil {
  209. return err
  210. }
  211. delete(dir.NodeMap, req.Name)
  212. return nil
  213. })
  214. }