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.

72 lines
1.9 KiB

  1. package filer
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. )
  6. type FilerEmbedded struct {
  7. directories *DirectoryManagerInMap
  8. files *FileListInLevelDb
  9. }
  10. func NewFilerEmbedded(dir string) (filer *FilerEmbedded, err error) {
  11. dm, de := NewDirectoryManagerInMap(filepath.Join(dir, "dir.log"))
  12. if de != nil {
  13. return nil, de
  14. }
  15. fl, fe := NewFileListInLevelDb(dir)
  16. if fe != nil {
  17. return nil, fe
  18. }
  19. filer = &FilerEmbedded{
  20. directories: dm,
  21. files: fl,
  22. }
  23. return
  24. }
  25. func (filer *FilerEmbedded) CreateFile(filePath string, fid string) (err error) {
  26. dir, file := filepath.Split(filePath)
  27. dirId, e := filer.directories.MakeDirectory(dir)
  28. if e != nil {
  29. return e
  30. }
  31. return filer.files.CreateFile(dirId, file, fid)
  32. }
  33. func (filer *FilerEmbedded) FindFile(filePath string) (fid string, err error) {
  34. dir, file := filepath.Split(filePath)
  35. dirId, e := filer.directories.FindDirectory(dir)
  36. if e != nil {
  37. return "", e
  38. }
  39. return filer.files.FindFile(dirId, file)
  40. }
  41. func (filer *FilerEmbedded) ListDirectories(dirPath string) (dirs []DirectoryEntry, err error) {
  42. return filer.directories.ListDirectories(dirPath)
  43. }
  44. func (filer *FilerEmbedded) ListFiles(dirPath string, lastFileName string, limit int) (files []FileEntry, err error) {
  45. dirId, e := filer.directories.FindDirectory(dirPath)
  46. if e != nil {
  47. return nil, e
  48. }
  49. return filer.files.ListFiles(dirId, lastFileName, limit), nil
  50. }
  51. func (filer *FilerEmbedded) DeleteDirectory(dirPath string) (err error) {
  52. dirId, e := filer.directories.FindDirectory(dirPath)
  53. if e != nil {
  54. return e
  55. }
  56. if len(filer.files.ListFiles(dirId, "", 1)) > 0 {
  57. return fmt.Errorf("Fail to delete non-empty directory %s!", dirPath)
  58. }
  59. return filer.directories.DeleteDirectory(dirPath)
  60. }
  61. func (filer *FilerEmbedded) DeleteFile(filePath string) (fid string, err error) {
  62. dir, file := filepath.Split(filePath)
  63. dirId, e := filer.directories.FindDirectory(dir)
  64. if e != nil {
  65. return "", e
  66. }
  67. return filer.files.DeleteFile(dirId, file)
  68. }