gitea源码

pointer.go 3.4KB

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