Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion gcp/indexer/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package shared
import (
"archive/tar"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"cloud.google.com/go/storage"
)
Expand All @@ -43,27 +45,48 @@ func CopyFromBucket(ctx context.Context, bucketHdl *storage.BucketHandle, name s
obj := bucketHdl.Object(name + TarExt)
r, err := obj.NewReader(ctx)
if err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}
defer r.Close()
cleanDest := filepath.Clean(tmpDir)
tarRdr := tar.NewReader(r)
for {
hdr, err := tarRdr.Next()
if err == io.EOF {
break
}
if err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}

if hdr.Typeflag == tar.TypeDir {
continue
}
if hdr.Typeflag != tar.TypeReg && hdr.Typeflag != tar.TypeRegA {
_ = os.RemoveAll(tmpDir)
return "", fmt.Errorf("unsupported tar entry type %q for %q", hdr.Typeflag, hdr.Name)
}

path := filepath.Join(cleanDest, hdr.Name)
rel, err := filepath.Rel(cleanDest, path)
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
_ = os.RemoveAll(tmpDir)
return "", fmt.Errorf("invalid tar entry path: %q", hdr.Name)
}

buf, err := io.ReadAll(tarRdr)
if err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}
path := filepath.Clean(filepath.Join(tmpDir, hdr.Name))
if err := os.MkdirAll(filepath.Dir(path), 0760); err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}
if err := os.WriteFile(path, buf, 0660); err != nil {
_ = os.RemoveAll(tmpDir)
return "", err
}
}
Expand Down
15 changes: 11 additions & 4 deletions gcp/indexer/stages/preparation/preparation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"

"cloud.google.com/go/pubsub/v2"
Expand Down Expand Up @@ -257,7 +256,8 @@ func (s *Stage) cloneGitRepo(ctx context.Context, name, address string) (*git.Re
}

repo, err := git.PlainClone(tmpDir, false, &git.CloneOptions{
URL: address,
URL: address,
NoCheckout: true,
})
if err != nil {
return nil, tmpDir, fmt.Errorf("failed to clone repository for %s: %v", name, err)
Expand Down Expand Up @@ -290,7 +290,10 @@ func (s *Stage) updateGitRepo(ctx context.Context, name string) (*git.Repository
func (r *Stage) copyToBucket(ctx context.Context, dir, name string) error {
var filePaths []string
if err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
filePaths = append(filePaths, path)
Expand All @@ -306,12 +309,16 @@ func (r *Stage) copyToBucket(ctx context.Context, dir, name string) error {
defer tarW.Close()

for _, p := range filePaths {
relPath, err := filepath.Rel(dir, p)
if err != nil {
return fmt.Errorf("failed to compute relative path for %s in %s: %v", p, name, err)
}
buf, err := os.ReadFile(p)
if err != nil {
return fmt.Errorf("failed to read file %s for %s: %v", p, name, err)
}
if err := tarW.WriteHeader(&tar.Header{
Name: strings.ReplaceAll(p, dir, ""),
Name: filepath.ToSlash(relPath),
Mode: 0660,
Size: int64(len(buf)),
}); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions gcp/indexer/stages/processing/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (s *Stage) processGit(ctx context.Context, repoInfo *preparation.Result) er

var fileResults []*FileResult
if err := filepath.Walk(repoDir, func(p string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if _, ok := vendoredLibNames[strings.ToLower(info.Name())]; ok {
// Ignore vendored libraries, as they can cause bad matches.
Expand All @@ -151,6 +154,9 @@ func (s *Stage) processGit(ctx context.Context, repoInfo *preparation.Result) er

return nil
}
if !info.Mode().IsRegular() {
return nil
}

for _, ext := range repoInfo.FileExts {
if filepath.Ext(p) == ext {
Expand Down
Loading