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.

156 lines
4.6 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. return filer.findFileEntry(dir, file)
  44. }
  45. func (filer *FilerEmbedded) findFileEntry(parentPath string, fileName string) (fid string, err error) {
  46. dirId, e := filer.directories.findDirectoryId(parentPath)
  47. if e != nil {
  48. return "", e
  49. }
  50. return filer.files.FindFile(dirId, fileName)
  51. }
  52. func (filer *FilerEmbedded) LookupDirectoryEntry(dirPath string, name string) (found bool, fileId string, err error) {
  53. if _, err = filer.directories.findDirectory(filepath.Join(dirPath, name)); err == nil {
  54. return true, "", nil
  55. }
  56. if fileId, err = filer.findFileEntry(dirPath, name); err == nil {
  57. return true, fileId, nil
  58. }
  59. return false, "", err
  60. }
  61. func (filer *FilerEmbedded) ListDirectories(dirPath string) (dirs []filer.DirectoryName, err error) {
  62. return filer.directories.ListDirectories(dirPath)
  63. }
  64. func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit int) (files []filer.FileEntry, err error) {
  65. dirId, e := filer.directories.findDirectoryId(dirPath)
  66. if e != nil {
  67. return nil, e
  68. }
  69. return filer.files.ListFiles(dirId, lastFileName, limit), nil
  70. }
  71. func (filer *FilerEmbedded) DeleteDirectory(dirPath string, recursive bool) (err error) {
  72. dirId, e := filer.directories.findDirectoryId(dirPath)
  73. if e != nil {
  74. return e
  75. }
  76. if sub_dirs, sub_err := filer.directories.ListDirectories(dirPath); sub_err == nil {
  77. if len(sub_dirs) > 0 && !recursive {
  78. return fmt.Errorf("Fail to delete directory %s: %d sub directories found!", dirPath, len(sub_dirs))
  79. }
  80. for _, sub := range sub_dirs {
  81. if delete_sub_err := filer.DeleteDirectory(filepath.Join(dirPath, string(sub)), recursive); delete_sub_err != nil {
  82. return delete_sub_err
  83. }
  84. }
  85. }
  86. list := filer.files.ListFiles(dirId, "", 100)
  87. if len(list) != 0 && !recursive {
  88. if !recursive {
  89. return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
  90. }
  91. }
  92. for {
  93. if len(list) == 0 {
  94. return filer.directories.DeleteDirectory(dirPath)
  95. }
  96. var fids []string
  97. for _, fileEntry := range list {
  98. fids = append(fids, string(fileEntry.Id))
  99. }
  100. if result_list, delete_file_err := operation.DeleteFiles(filer.master, fids); delete_file_err != nil {
  101. return delete_file_err
  102. } else {
  103. if len(result_list.Errors) > 0 {
  104. return errors.New(strings.Join(result_list.Errors, "\n"))
  105. }
  106. }
  107. lastFile := list[len(list)-1]
  108. list = filer.files.ListFiles(dirId, lastFile.Name, 100)
  109. }
  110. }
  111. func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) {
  112. dir, file := filepath.Split(filePath)
  113. dirId, e := filer.directories.findDirectoryId(dir)
  114. if e != nil {
  115. return "", e
  116. }
  117. return filer.files.DeleteFile(dirId, file)
  118. }
  119. /*
  120. Move a folder or a file, with 4 Use cases:
  121. mv fromDir toNewDir
  122. mv fromDir toOldDir
  123. mv fromFile toDir
  124. mv fromFile toFile
  125. */
  126. func (filer *FilerEmbedded) Move(fromPath string, toPath string) error {
  127. filer.mvMutex.Lock()
  128. defer filer.mvMutex.Unlock()
  129. if _, dir_err := filer.directories.findDirectoryId(fromPath); dir_err == nil {
  130. if _, err := filer.directories.findDirectoryId(toPath); err == nil {
  131. // move folder under an existing folder
  132. return filer.directories.MoveUnderDirectory(fromPath, toPath, "")
  133. }
  134. // move folder to a new folder
  135. return filer.directories.MoveUnderDirectory(fromPath, filepath.Dir(toPath), filepath.Base(toPath))
  136. }
  137. if fid, file_err := filer.DeleteFile(fromPath); file_err == nil {
  138. if _, err := filer.directories.findDirectoryId(toPath); err == nil {
  139. // move file under an existing folder
  140. return filer.CreateFile(filepath.Join(toPath, filepath.Base(fromPath)), fid)
  141. }
  142. // move to a folder with new name
  143. return filer.CreateFile(toPath, fid)
  144. }
  145. return fmt.Errorf("File %s is not found!", fromPath)
  146. }