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.

pointer.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021 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. "fmt"
  10. "io"
  11. "path"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. )
  16. const (
  17. blobSizeCutoff = 1024
  18. // MetaFileIdentifier is the string appearing at the first line of LFS pointer files.
  19. // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
  20. MetaFileIdentifier = "version https://git-lfs.github.com/spec/v1"
  21. // MetaFileOidPrefix appears in LFS pointer files on a line before the sha256 hash.
  22. MetaFileOidPrefix = "oid sha256:"
  23. )
  24. var (
  25. // ErrMissingPrefix occurs if the content lacks the LFS prefix
  26. ErrMissingPrefix = errors.New("Content lacks the LFS prefix")
  27. // ErrInvalidStructure occurs if the content has an invalid structure
  28. ErrInvalidStructure = errors.New("Content has an invalid structure")
  29. // ErrInvalidOIDFormat occurs if the oid has an invalid format
  30. ErrInvalidOIDFormat = errors.New("OID has an invalid format")
  31. )
  32. // ReadPointer tries to read LFS pointer data from the reader
  33. func ReadPointer(reader io.Reader) (Pointer, error) {
  34. buf := make([]byte, blobSizeCutoff)
  35. n, err := io.ReadFull(reader, buf)
  36. if err != nil && err != io.ErrUnexpectedEOF {
  37. return Pointer{}, err
  38. }
  39. buf = buf[:n]
  40. return ReadPointerFromBuffer(buf)
  41. }
  42. var oidPattern = regexp.MustCompile(`^[a-f\d]{64}$`)
  43. // ReadPointerFromBuffer will return a pointer if the provided byte slice is a pointer file or an error otherwise.
  44. func ReadPointerFromBuffer(buf []byte) (Pointer, error) {
  45. var p Pointer
  46. headString := string(buf)
  47. if !strings.HasPrefix(headString, MetaFileIdentifier) {
  48. return p, ErrMissingPrefix
  49. }
  50. splitLines := strings.Split(headString, "\n")
  51. if len(splitLines) < 3 {
  52. return p, ErrInvalidStructure
  53. }
  54. oid := strings.TrimPrefix(splitLines[1], MetaFileOidPrefix)
  55. if len(oid) != 64 || !oidPattern.MatchString(oid) {
  56. return p, ErrInvalidOIDFormat
  57. }
  58. size, err := strconv.ParseInt(strings.TrimPrefix(splitLines[2], "size "), 10, 64)
  59. if err != nil {
  60. return p, err
  61. }
  62. p.Oid = oid
  63. p.Size = size
  64. return p, nil
  65. }
  66. // IsValid checks if the pointer has a valid structure.
  67. // It doesn't check if the pointed-to-content exists.
  68. func (p Pointer) IsValid() bool {
  69. if len(p.Oid) != 64 {
  70. return false
  71. }
  72. if !oidPattern.MatchString(p.Oid) {
  73. return false
  74. }
  75. if p.Size < 0 {
  76. return false
  77. }
  78. return true
  79. }
  80. // StringContent returns the string representation of the pointer
  81. // https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#the-pointer
  82. func (p Pointer) StringContent() string {
  83. return fmt.Sprintf("%s\n%s%s\nsize %d\n", MetaFileIdentifier, MetaFileOidPrefix, p.Oid, p.Size)
  84. }
  85. // RelativePath returns the relative storage path of the pointer
  86. func (p Pointer) RelativePath() string {
  87. if len(p.Oid) < 5 {
  88. return p.Oid
  89. }
  90. return path.Join(p.Oid[0:2], p.Oid[2:4], p.Oid[4:])
  91. }
  92. // GeneratePointer generates a pointer for arbitrary content
  93. func GeneratePointer(content io.Reader) (Pointer, error) {
  94. h := sha256.New()
  95. c, err := io.Copy(h, content)
  96. if err != nil {
  97. return Pointer{}, err
  98. }
  99. sum := h.Sum(nil)
  100. return Pointer{Oid: hex.EncodeToString(sum), Size: c}, nil
  101. }