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.

content_store.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "errors"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. var (
  16. errHashMismatch = errors.New("Content hash does not match OID")
  17. errSizeMismatch = errors.New("Content size does not match")
  18. )
  19. // ContentStore provides a simple file system based storage.
  20. type ContentStore struct {
  21. BasePath string
  22. }
  23. // Get takes a Meta object and retrieves the content from the store, returning
  24. // it as an io.Reader. If fromByte > 0, the reader starts from that byte
  25. func (s *ContentStore) Get(meta *models.LFSMetaObject, fromByte int64) (io.ReadCloser, error) {
  26. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  27. f, err := os.Open(path)
  28. if err != nil {
  29. log.Error("Whilst trying to read LFS OID[%s]: Unable to open %s Error: %v", meta.Oid, path, err)
  30. return nil, err
  31. }
  32. if fromByte > 0 {
  33. _, err = f.Seek(fromByte, os.SEEK_CUR)
  34. if err != nil {
  35. log.Error("Whilst trying to read LFS OID[%s]: Unable to seek to %d Error: %v", meta.Oid, fromByte, err)
  36. }
  37. }
  38. return f, err
  39. }
  40. // Put takes a Meta object and an io.Reader and writes the content to the store.
  41. func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
  42. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  43. tmpPath := path + ".tmp"
  44. dir := filepath.Dir(path)
  45. if err := os.MkdirAll(dir, 0750); err != nil {
  46. log.Error("Whilst putting LFS OID[%s]: Unable to create the LFS directory: %s Error: %v", meta.Oid, dir, err)
  47. return err
  48. }
  49. file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0640)
  50. if err != nil {
  51. log.Error("Whilst putting LFS OID[%s]: Unable to open temporary file for writing: %s Error: %v", tmpPath, err)
  52. return err
  53. }
  54. defer os.Remove(tmpPath)
  55. hash := sha256.New()
  56. hw := io.MultiWriter(hash, file)
  57. written, err := io.Copy(hw, r)
  58. if err != nil {
  59. log.Error("Whilst putting LFS OID[%s]: Failed to copy to tmpPath: %s Error: %v", meta.Oid, tmpPath, err)
  60. file.Close()
  61. return err
  62. }
  63. file.Close()
  64. if written != meta.Size {
  65. return errSizeMismatch
  66. }
  67. shaStr := hex.EncodeToString(hash.Sum(nil))
  68. if shaStr != meta.Oid {
  69. return errHashMismatch
  70. }
  71. if err := os.Rename(tmpPath, path); err != nil {
  72. log.Error("Whilst putting LFS OID[%s]: Unable to move tmp file to final destination: %s Error: %v", meta.Oid, path, err)
  73. return err
  74. }
  75. return nil
  76. }
  77. // Exists returns true if the object exists in the content store.
  78. func (s *ContentStore) Exists(meta *models.LFSMetaObject) bool {
  79. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  80. if _, err := os.Stat(path); os.IsNotExist(err) {
  81. return false
  82. }
  83. return true
  84. }
  85. // Verify returns true if the object exists in the content store and size is correct.
  86. func (s *ContentStore) Verify(meta *models.LFSMetaObject) (bool, error) {
  87. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  88. fi, err := os.Stat(path)
  89. if os.IsNotExist(err) || err == nil && fi.Size() != meta.Size {
  90. return false, nil
  91. } else if err != nil {
  92. log.Error("Unable stat file: %s for LFS OID[%s] Error: %v", path, meta.Oid, err)
  93. return false, err
  94. }
  95. return true, nil
  96. }
  97. func transformKey(key string) string {
  98. if len(key) < 5 {
  99. return key
  100. }
  101. return filepath.Join(key[0:2], key[2:4], key[4:])
  102. }