You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dir.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package cache
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. type CacheDir struct {
  8. pathComps []string
  9. name string
  10. modTime time.Time
  11. perm os.FileMode
  12. }
  13. func createNewCacheDir(c *Cache, pathComps []string) (*CacheDir, error) {
  14. metaPath := c.GetCacheMetaPath(pathComps...)
  15. dataPath := c.GetCacheDataPath(pathComps...)
  16. err := os.MkdirAll(metaPath, 0755)
  17. if err != nil {
  18. return nil, err
  19. }
  20. err = os.MkdirAll(dataPath, 0755)
  21. if err != nil {
  22. return nil, err
  23. }
  24. // 修改文件夹的修改时间
  25. modTime := time.Now()
  26. os.Chtimes(metaPath, modTime, modTime)
  27. return &CacheDir{
  28. pathComps: pathComps,
  29. name: pathComps[len(pathComps)-1],
  30. modTime: modTime,
  31. perm: 0755,
  32. }, nil
  33. }
  34. func loadCacheDir(c *Cache, pathComps []string) (*CacheDir, error) {
  35. stat, err := os.Stat(c.GetCacheMetaPath(pathComps...))
  36. if err != nil {
  37. return nil, err
  38. }
  39. if !stat.IsDir() {
  40. return nil, fmt.Errorf("not a directory")
  41. }
  42. return &CacheDir{
  43. pathComps: pathComps,
  44. name: stat.Name(),
  45. modTime: stat.ModTime(),
  46. perm: stat.Mode().Perm(),
  47. }, nil
  48. }
  49. func makeCacheDirFromOption(c *Cache, pathComps []string, opt CreateDirOption) (*CacheDir, error) {
  50. metaPath := c.GetCacheMetaPath(pathComps...)
  51. dataPath := c.GetCacheDataPath(pathComps...)
  52. err := os.MkdirAll(metaPath, 0755)
  53. if err != nil {
  54. return nil, err
  55. }
  56. err = os.MkdirAll(dataPath, 0755)
  57. if err != nil {
  58. return nil, err
  59. }
  60. os.Chtimes(metaPath, opt.ModTime, opt.ModTime)
  61. return &CacheDir{
  62. pathComps: pathComps,
  63. name: pathComps[len(pathComps)-1],
  64. modTime: opt.ModTime,
  65. perm: 0755,
  66. }, nil
  67. }
  68. func loadCacheDirInfo(c *Cache, pathComps []string) (*CacheEntryInfo, error) {
  69. stat, err := os.Stat(c.GetCacheMetaPath(pathComps...))
  70. if err != nil {
  71. return nil, err
  72. }
  73. if !stat.IsDir() {
  74. return nil, fmt.Errorf("not a directory")
  75. }
  76. return &CacheEntryInfo{
  77. PathComps: pathComps,
  78. Size: 0,
  79. Mode: stat.Mode(),
  80. ModTime: stat.ModTime(),
  81. IsDir: true,
  82. }, nil
  83. }
  84. func (f *CacheDir) PathComps() []string {
  85. return f.pathComps
  86. }
  87. func (f *CacheDir) Name() string {
  88. return f.name
  89. }
  90. func (f *CacheDir) Size() int64 {
  91. return 0
  92. }
  93. func (f *CacheDir) Mode() os.FileMode {
  94. return f.perm | os.ModeDir
  95. }
  96. func (f *CacheDir) ModTime() time.Time {
  97. return f.modTime
  98. }
  99. func (f *CacheDir) IsDir() bool {
  100. return true
  101. }
  102. func (f *CacheDir) Info() CacheEntryInfo {
  103. return CacheEntryInfo{
  104. PathComps: f.pathComps,
  105. Size: 0,
  106. Mode: f.perm,
  107. ModTime: f.modTime,
  108. IsDir: true,
  109. }
  110. }
  111. func (f *CacheDir) SetModTime(modTime time.Time) error {
  112. // TODO 修改文件夹的修改时间
  113. return nil
  114. }
  115. // func (f *CacheDir) Release() {
  116. // }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。