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_scanner_nogogit.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //go:build !gogit
  5. // +build !gogit
  6. package lfs
  7. import (
  8. "bufio"
  9. "context"
  10. "io"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/git/pipeline"
  16. )
  17. // SearchPointerBlobs scans the whole repository for LFS pointer files
  18. func SearchPointerBlobs(ctx context.Context, repo *git.Repository, pointerChan chan<- PointerBlob, errChan chan<- error) {
  19. basePath := repo.Path
  20. catFileCheckReader, catFileCheckWriter := io.Pipe()
  21. shasToBatchReader, shasToBatchWriter := io.Pipe()
  22. catFileBatchReader, catFileBatchWriter := io.Pipe()
  23. wg := sync.WaitGroup{}
  24. wg.Add(4)
  25. // Create the go-routines in reverse order.
  26. // 4. Take the output of cat-file --batch and check if each file in turn
  27. // to see if they're pointers to files in the LFS store
  28. go createPointerResultsFromCatFileBatch(ctx, catFileBatchReader, &wg, pointerChan)
  29. // 3. Take the shas of the blobs and batch read them
  30. go pipeline.CatFileBatch(shasToBatchReader, catFileBatchWriter, &wg, basePath)
  31. // 2. From the provided objects restrict to blobs <=1k
  32. go pipeline.BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader, shasToBatchWriter, &wg)
  33. // 1. Run batch-check on all objects in the repository
  34. if git.CheckGitVersionAtLeast("2.6.0") != nil {
  35. revListReader, revListWriter := io.Pipe()
  36. shasToCheckReader, shasToCheckWriter := io.Pipe()
  37. wg.Add(2)
  38. go pipeline.CatFileBatchCheck(shasToCheckReader, catFileCheckWriter, &wg, basePath)
  39. go pipeline.BlobsFromRevListObjects(revListReader, shasToCheckWriter, &wg)
  40. go pipeline.RevListAllObjects(revListWriter, &wg, basePath, errChan)
  41. } else {
  42. go pipeline.CatFileBatchCheckAllObjects(catFileCheckWriter, &wg, basePath, errChan)
  43. }
  44. wg.Wait()
  45. close(pointerChan)
  46. close(errChan)
  47. }
  48. func createPointerResultsFromCatFileBatch(ctx context.Context, catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pointerChan chan<- PointerBlob) {
  49. defer wg.Done()
  50. defer catFileBatchReader.Close()
  51. bufferedReader := bufio.NewReader(catFileBatchReader)
  52. buf := make([]byte, 1025)
  53. loop:
  54. for {
  55. select {
  56. case <-ctx.Done():
  57. break loop
  58. default:
  59. }
  60. // File descriptor line: sha
  61. sha, err := bufferedReader.ReadString(' ')
  62. if err != nil {
  63. _ = catFileBatchReader.CloseWithError(err)
  64. break
  65. }
  66. sha = strings.TrimSpace(sha)
  67. // Throw away the blob
  68. if _, err := bufferedReader.ReadString(' '); err != nil {
  69. _ = catFileBatchReader.CloseWithError(err)
  70. break
  71. }
  72. sizeStr, err := bufferedReader.ReadString('\n')
  73. if err != nil {
  74. _ = catFileBatchReader.CloseWithError(err)
  75. break
  76. }
  77. size, err := strconv.Atoi(sizeStr[:len(sizeStr)-1])
  78. if err != nil {
  79. _ = catFileBatchReader.CloseWithError(err)
  80. break
  81. }
  82. pointerBuf := buf[:size+1]
  83. if _, err := io.ReadFull(bufferedReader, pointerBuf); err != nil {
  84. _ = catFileBatchReader.CloseWithError(err)
  85. break
  86. }
  87. pointerBuf = pointerBuf[:size]
  88. // Now we need to check if the pointerBuf is an LFS pointer
  89. pointer, _ := ReadPointerFromBuffer(pointerBuf)
  90. if !pointer.IsValid() {
  91. continue
  92. }
  93. pointerChan <- PointerBlob{Hash: sha, Pointer: pointer}
  94. }
  95. }