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.

108 lines
3.0 KiB

  1. package filer
  2. import (
  3. "code.google.com/p/weed-fs/go/operation"
  4. "errors"
  5. "fmt"
  6. "path/filepath"
  7. "strings"
  8. )
  9. type FilerEmbedded struct {
  10. master string
  11. directories *DirectoryManagerInMap
  12. files *FileListInLevelDb
  13. }
  14. func NewFilerEmbedded(master string, dir string) (filer *FilerEmbedded, err error) {
  15. dm, de := NewDirectoryManagerInMap(filepath.Join(dir, "dir.log"))
  16. if de != nil {
  17. return nil, de
  18. }
  19. fl, fe := NewFileListInLevelDb(dir)
  20. if fe != nil {
  21. return nil, fe
  22. }
  23. filer = &FilerEmbedded{
  24. master: master,
  25. directories: dm,
  26. files: fl,
  27. }
  28. return
  29. }
  30. func (filer *FilerEmbedded) CreateFile(filePath string, fid string) (err error) {
  31. dir, file := filepath.Split(filePath)
  32. dirId, e := filer.directories.MakeDirectory(dir)
  33. if e != nil {
  34. return e
  35. }
  36. return filer.files.CreateFile(dirId, file, fid)
  37. }
  38. func (filer *FilerEmbedded) FindFile(filePath string) (fid string, err error) {
  39. dir, file := filepath.Split(filePath)
  40. dirId, e := filer.directories.FindDirectory(dir)
  41. if e != nil {
  42. return "", e
  43. }
  44. return filer.files.FindFile(dirId, file)
  45. }
  46. func (filer *FilerEmbedded) ListDirectories(dirPath string) (dirs []DirectoryEntry, err error) {
  47. return filer.directories.ListDirectories(dirPath)
  48. }
  49. func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit int) (files []FileEntry, err error) {
  50. dirId, e := filer.directories.FindDirectory(dirPath)
  51. if e != nil {
  52. return nil, e
  53. }
  54. return filer.files.ListFiles(dirId, lastFileName, limit), nil
  55. }
  56. func (filer *FilerEmbedded) DeleteDirectory(dirPath string, recursive bool) (err error) {
  57. dirId, e := filer.directories.FindDirectory(dirPath)
  58. if e != nil {
  59. return e
  60. }
  61. if sub_dirs, sub_err := filer.directories.ListDirectories(dirPath); sub_err == nil {
  62. if len(sub_dirs) > 0 && !recursive {
  63. return fmt.Errorf("Fail to delete directory %s: %d sub directories found!", dirPath, len(sub_dirs))
  64. }
  65. for _, sub := range sub_dirs {
  66. if delete_sub_err := filer.DeleteDirectory(filepath.Join(dirPath, sub.Name), recursive); delete_sub_err != nil {
  67. return delete_sub_err
  68. }
  69. }
  70. }
  71. list := filer.files.ListFiles(dirId, "", 100)
  72. if len(list) != 0 && !recursive {
  73. if !recursive {
  74. return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
  75. }
  76. }
  77. for {
  78. if len(list) == 0 {
  79. return filer.directories.DeleteDirectory(dirPath)
  80. }
  81. var fids []string
  82. for _, fileEntry := range list {
  83. fids = append(fids, string(fileEntry.Id))
  84. }
  85. if result_list, delete_file_err := operation.DeleteFiles(filer.master, fids); delete_file_err != nil {
  86. return delete_file_err
  87. } else {
  88. if len(result_list.Errors) > 0 {
  89. return errors.New(strings.Join(result_list.Errors, "\n"))
  90. }
  91. }
  92. lastFile := list[len(list)-1]
  93. list = filer.files.ListFiles(dirId, lastFile.Name, 100)
  94. }
  95. }
  96. func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) {
  97. dir, file := filepath.Split(filePath)
  98. dirId, e := filer.directories.FindDirectory(dir)
  99. if e != nil {
  100. return "", e
  101. }
  102. return filer.files.DeleteFile(dirId, file)
  103. }