Skip to content

Commit

Permalink
Use relative paths for archives
Browse files Browse the repository at this point in the history
  • Loading branch information
Naatan committed Nov 7, 2024
1 parent 6c38c9e commit f380d3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
23 changes: 10 additions & 13 deletions cmd/state-svc/internal/hash/file_hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@ func (fh *FileHasher) HashFiles(wd string, globs []string) (_ string, _ []hashed
return "", nil, errs.Wrap(err, "Could not match glob: %s", glob)
}
sort.Strings(files) // ensure consistent ordering
for _, f := range files {
if !filepath.IsAbs(f) {
af, err := filepath.Abs(filepath.Join(wd, f))
if err != nil {
return "", nil, errs.Wrap(err, "Could not get absolute path for file: %s", f)
}
f = af
for _, relativePath := range files {
absolutePath, err := filepath.Abs(filepath.Join(wd, relativePath))
if err != nil {
return "", nil, errs.Wrap(err, "Could not get absolute path for file: %s", relativePath)
}
fileInfo, err := os.Stat(f)
fileInfo, err := os.Stat(absolutePath)
if err != nil {
return "", nil, errs.Wrap(err, "Could not stat file: %s", f)
return "", nil, errs.Wrap(err, "Could not stat file: %s", absolutePath)
}

if fileInfo.IsDir() {
Expand All @@ -72,10 +69,10 @@ func (fh *FileHasher) HashFiles(wd string, globs []string) (_ string, _ []hashed
} else {
fileHasher := xxhash.New()
// include filepath in hash, because moving files should affect the hash
fmt.Fprintf(fileHasher, "%016x", f)
file, err := os.Open(f)
fmt.Fprintf(fileHasher, "%016x", relativePath)
file, err := os.Open(absolutePath)
if err != nil {
return "", nil, errs.Wrap(err, "Could not open file: %s", f)
return "", nil, errs.Wrap(err, "Could not open file: %s", absolutePath)
}
defer file.Close()
if _, err := io.Copy(fileHasher, file); err != nil {
Expand All @@ -90,7 +87,7 @@ func (fh *FileHasher) HashFiles(wd string, globs []string) (_ string, _ []hashed
hashes = append(hashes, hash)
hashedFiles = append(hashedFiles, hashedFile{
Pattern: glob,
Path: f,
Path: relativePath,
Hash: hash,
})
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/state-svc/internal/hash/file_hasher_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package hash

import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -50,6 +52,10 @@ func TestFileHasher_HashFiles(t *testing.T) {
hash2, files2, err := hasher.HashFiles(dir, []string{"./**/*"})
require.NoError(t, err, errs.JoinMessage(err))

for _, f := range files1 {
assert.False(t, strings.HasPrefix(f.Path, dir), fmt.Sprintf("'%s' should not be prefixed with '%s'", f.Path, dir))
}

sort.Slice(files1, func(i, j int) bool { return files1[i].Path < files1[j].Path })
sort.Slice(files2, func(i, j int) bool { return files2[i].Path < files2[j].Path })
require.Len(t, files2, 3)
Expand Down
10 changes: 7 additions & 3 deletions internal/archiver/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type FileMap struct {
Target string
}

func CreateTgz(filepath string, fileMaps []FileMap) error {
f, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0644)
func CreateTgz(archivePath string, workDir string, fileMaps []FileMap) error {
f, err := os.OpenFile(archivePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return errs.Wrap(err, "Could not create temp file")
}
Expand All @@ -28,7 +28,11 @@ func CreateTgz(filepath string, fileMaps []FileMap) error {
defer tgz.Close()

for _, fileMap := range fileMaps {
file, err := os.Open(fileMap.Source)
source := fileMap.Source
if !filepath.IsAbs(source) {
source = filepath.Join(workDir, source)
}
file, err := os.Open(source)
if err != nil {
return errs.Wrap(err, "Could not open file")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/commit/ingredientcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (i *IngredientCall) createIngredient(hash string, hashedFiles []*graph.Glob
}

tmpFile := fileutils.TempFilePath("", fmt.Sprintf("bs-hash-%s.tar.gz", hash))
if err := archiver.CreateTgz(tmpFile, archiver.FilesWithCommonParent(files...)); err != nil {
if err := archiver.CreateTgz(tmpFile, i.prime.Project().Dir(), archiver.FilesWithCommonParent(files...)); err != nil {
return nil, errs.Wrap(err, "Could not create tar.gz")
}
defer os.Remove(tmpFile)
Expand Down

0 comments on commit f380d3d

Please sign in to comment.