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.

141 lines
4.2 KiB

  1. package embedded_filer
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. )
  10. type FilerEmbedded struct {
  11. master string
  12. directories *DirectoryManagerInMap
  13. files *FileListInLevelDb
  14. }
  15. func NewFilerEmbedded(master string, dir string) (filer *FilerEmbedded, err error) {
  16. dm, de := NewDirectoryManagerInMap(filepath.Join(dir, "dir.log"))
  17. if de != nil {
  18. return nil, de
  19. }
  20. fl, fe := NewFileListInLevelDb(dir)
  21. if fe != nil {
  22. return nil, fe
  23. }
  24. filer = &FilerEmbedded{
  25. master: master,
  26. directories: dm,
  27. files: fl,
  28. }
  29. return
  30. }
  31. func (filer *FilerEmbedded) CreateFile(filePath string, fid string) (err error) {
  32. dir, file := filepath.Split(filePath)
  33. dirId, e := filer.directories.MakeDirectory(dir)
  34. if e != nil {
  35. return e
  36. }
  37. return filer.files.CreateFile(dirId, file, fid)
  38. }
  39. func (filer *FilerEmbedded) FindFile(filePath string) (fid string, err error) {
  40. dir, file := filepath.Split(filePath)
  41. dirId, e := filer.directories.FindDirectory(dir)
  42. if e != nil {
  43. return "", e
  44. }
  45. return filer.files.FindFile(dirId, file)
  46. }
  47. func (filer *FilerEmbedded) FindDirectory(dirPath string) (dirId filer.DirectoryId, err error) {
  48. return filer.directories.FindDirectory(dirPath)
  49. }
  50. func (filer *FilerEmbedded) ListDirectories(dirPath string) (dirs []filer.DirectoryEntry, err error) {
  51. return filer.directories.ListDirectories(dirPath)
  52. }
  53. func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit int) (files []filer.FileEntry, err error) {
  54. dirId, e := filer.directories.FindDirectory(dirPath)
  55. if e != nil {
  56. return nil, e
  57. }
  58. return filer.files.ListFiles(dirId, lastFileName, limit), nil
  59. }
  60. func (filer *FilerEmbedded) DeleteDirectory(dirPath string, recursive bool) (err error) {
  61. dirId, e := filer.directories.FindDirectory(dirPath)
  62. if e != nil {
  63. return e
  64. }
  65. if sub_dirs, sub_err := filer.directories.ListDirectories(dirPath); sub_err == nil {
  66. if len(sub_dirs) > 0 && !recursive {
  67. return fmt.Errorf("Fail to delete directory %s: %d sub directories found!", dirPath, len(sub_dirs))
  68. }
  69. for _, sub := range sub_dirs {
  70. if delete_sub_err := filer.DeleteDirectory(filepath.Join(dirPath, sub.Name), recursive); delete_sub_err != nil {
  71. return delete_sub_err
  72. }
  73. }
  74. }
  75. list := filer.files.ListFiles(dirId, "", 100)
  76. if len(list) != 0 && !recursive {
  77. if !recursive {
  78. return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
  79. }
  80. }
  81. for {
  82. if len(list) == 0 {
  83. return filer.directories.DeleteDirectory(dirPath)
  84. }
  85. var fids []string
  86. for _, fileEntry := range list {
  87. fids = append(fids, string(fileEntry.Id))
  88. }
  89. if result_list, delete_file_err := operation.DeleteFiles(filer.master, fids); delete_file_err != nil {
  90. return delete_file_err
  91. } else {
  92. if len(result_list.Errors) > 0 {
  93. return errors.New(strings.Join(result_list.Errors, "\n"))
  94. }
  95. }
  96. lastFile := list[len(list)-1]
  97. list = filer.files.ListFiles(dirId, lastFile.Name, 100)
  98. }
  99. }
  100. func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) {
  101. dir, file := filepath.Split(filePath)
  102. dirId, e := filer.directories.FindDirectory(dir)
  103. if e != nil {
  104. return "", e
  105. }
  106. return filer.files.DeleteFile(dirId, file)
  107. }
  108. /*
  109. Move a folder or a file, with 4 Use cases:
  110. mv fromDir toNewDir
  111. mv fromDir toOldDir
  112. mv fromFile toDir
  113. mv fromFile toFile
  114. */
  115. func (filer *FilerEmbedded) Move(fromPath string, toPath string) error {
  116. if _, dir_err := filer.FindDirectory(fromPath); dir_err == nil {
  117. if _, err := filer.FindDirectory(toPath); err == nil {
  118. // move folder under an existing folder
  119. return filer.directories.MoveUnderDirectory(fromPath, toPath, "")
  120. }
  121. // move folder to a new folder
  122. return filer.directories.MoveUnderDirectory(fromPath, filepath.Dir(toPath), filepath.Base(toPath))
  123. }
  124. if fid, file_err := filer.DeleteFile(fromPath); file_err == nil {
  125. if _, err := filer.FindDirectory(toPath); err == nil {
  126. // move file under an existing folder
  127. return filer.CreateFile(filepath.Join(toPath, filepath.Base(fromPath)), fid)
  128. }
  129. // move to a folder with new name
  130. return filer.CreateFile(toPath, fid)
  131. }
  132. return fmt.Errorf("File %s is not found!", fromPath)
  133. }