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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package lfs
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "errors"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "code.gitea.io/gitea/models"
  10. )
  11. var (
  12. errHashMismatch = errors.New("Content hash does not match OID")
  13. errSizeMismatch = errors.New("Content size does not match")
  14. )
  15. // ContentStore provides a simple file system based storage.
  16. type ContentStore struct {
  17. BasePath string
  18. }
  19. // Get takes a Meta object and retrieves the content from the store, returning
  20. // it as an io.Reader. If fromByte > 0, the reader starts from that byte
  21. func (s *ContentStore) Get(meta *models.LFSMetaObject, fromByte int64) (io.ReadCloser, error) {
  22. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  23. f, err := os.Open(path)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if fromByte > 0 {
  28. _, err = f.Seek(fromByte, os.SEEK_CUR)
  29. }
  30. return f, err
  31. }
  32. // Put takes a Meta object and an io.Reader and writes the content to the store.
  33. func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
  34. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  35. tmpPath := path + ".tmp"
  36. dir := filepath.Dir(path)
  37. if err := os.MkdirAll(dir, 0750); err != nil {
  38. return err
  39. }
  40. file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0640)
  41. if err != nil {
  42. return err
  43. }
  44. defer os.Remove(tmpPath)
  45. hash := sha256.New()
  46. hw := io.MultiWriter(hash, file)
  47. written, err := io.Copy(hw, r)
  48. if err != nil {
  49. file.Close()
  50. return err
  51. }
  52. file.Close()
  53. if written != meta.Size {
  54. return errSizeMismatch
  55. }
  56. shaStr := hex.EncodeToString(hash.Sum(nil))
  57. if shaStr != meta.Oid {
  58. return errHashMismatch
  59. }
  60. return os.Rename(tmpPath, path)
  61. }
  62. // Exists returns true if the object exists in the content store.
  63. func (s *ContentStore) Exists(meta *models.LFSMetaObject) bool {
  64. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  65. if _, err := os.Stat(path); os.IsNotExist(err) {
  66. return false
  67. }
  68. return true
  69. }
  70. // Verify returns true if the object exists in the content store and size is correct.
  71. func (s *ContentStore) Verify(meta *models.LFSMetaObject) (bool, error) {
  72. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  73. fi, err := os.Stat(path)
  74. if os.IsNotExist(err) || err == nil && fi.Size() != meta.Size {
  75. return false, nil
  76. } else if err != nil {
  77. return false, err
  78. }
  79. return true, nil
  80. }
  81. func transformKey(key string) string {
  82. if len(key) < 5 {
  83. return key
  84. }
  85. return filepath.Join(key[0:2], key[2:4], key[4:])
  86. }