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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(dataPath, 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.GetCacheDataPath(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. cache: c,
  46. pathComps: pathComps,
  47. name: stat.Name(),
  48. modTime: stat.ModTime(),
  49. perm: stat.Mode().Perm(),
  50. }, nil
  51. }
  52. func makeCacheDirFromOption(c *Cache, pathComps []string, opt CreateDirOption) (*CacheDir, error) {
  53. metaPath := c.GetCacheMetaPath(pathComps...)
  54. dataPath := c.GetCacheDataPath(pathComps...)
  55. err := os.MkdirAll(metaPath, 0755)
  56. if err != nil {
  57. return nil, err
  58. }
  59. err = os.MkdirAll(dataPath, 0755)
  60. if err != nil {
  61. return nil, err
  62. }
  63. os.Chtimes(dataPath, opt.ModTime, opt.ModTime)
  64. return &CacheDir{
  65. cache: c,
  66. pathComps: pathComps,
  67. name: pathComps[len(pathComps)-1],
  68. modTime: opt.ModTime,
  69. perm: 0755,
  70. }, nil
  71. }
  72. func loadCacheDirInfo(c *Cache, pathComps []string, dataFileInfo os.FileInfo) (*CacheEntryInfo, error) {
  73. return &CacheEntryInfo{
  74. PathComps: pathComps,
  75. Size: 0,
  76. Mode: dataFileInfo.Mode(),
  77. ModTime: dataFileInfo.ModTime(),
  78. IsDir: true,
  79. }, nil
  80. }
  81. func (f *CacheDir) PathComps() []string {
  82. return f.pathComps
  83. }
  84. func (f *CacheDir) Name() string {
  85. return f.name
  86. }
  87. func (f *CacheDir) Size() int64 {
  88. return 0
  89. }
  90. func (f *CacheDir) Mode() os.FileMode {
  91. return f.perm | os.ModeDir
  92. }
  93. func (f *CacheDir) ModTime() time.Time {
  94. return f.modTime
  95. }
  96. func (f *CacheDir) IsDir() bool {
  97. return true
  98. }
  99. func (f *CacheDir) Info() CacheEntryInfo {
  100. return CacheEntryInfo{
  101. PathComps: f.pathComps,
  102. Size: 0,
  103. Mode: f.perm,
  104. ModTime: f.modTime,
  105. IsDir: true,
  106. }
  107. }
  108. func (f *CacheDir) SetModTime(modTime time.Time) error {
  109. dataPath := f.cache.GetCacheDataPath(f.pathComps...)
  110. return os.Chtimes(dataPath, modTime, modTime)
  111. }

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