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

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

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