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.

146 lines
4.2 KiB

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