Skip to content
Merged
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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hathora/cloud-sdk-go/hathoracloud v0.12.0 h1:Fb4uTDn3J1FdZiA6TFuwV2ySwkH3QNHKDr1eGA/p8Qc=
github.com/hathora/cloud-sdk-go/hathoracloud v0.12.0/go.mod h1:16HoEwQ/NvaC4+tG6aHHTbgAUm3UP1cQ7cJtZ+RwUZQ=
github.com/hathora/cloud-sdk-go/hathoracloud v0.13.0 h1:002OLIIPPv43XAgfBl0D+A3l+1GgI7Kfw2nASbGcDYU=
github.com/hathora/cloud-sdk-go/hathoracloud v0.13.0/go.mod h1:16HoEwQ/NvaC4+tG6aHHTbgAUm3UP1cQ7cJtZ+RwUZQ=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
Expand Down
110 changes: 63 additions & 47 deletions internal/archive/targz.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/boyter/gocodewalker"
"github.com/h2non/filetype"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)

type TGZFile struct {
Expand Down Expand Up @@ -45,59 +46,74 @@ func CreateTGZ(srcFolder string, ext string) (string, error) {
return false
})

err = walker.Start()
if err != nil {
return "", fmt.Errorf("error walking directory: %w", err)
}

for path := range paths {

info, err := os.Stat(path.Location)
if err != nil {
return "", fmt.Errorf("error statting file: %w", err)
}

if !info.Mode().IsRegular() {
continue
}

relPath, err := filepath.Rel(srcFolder, path.Location)
if err != nil {
return "", fmt.Errorf("error getting relative path: %w", err)
}

zap.L().Debug("Considering file: " + relPath)

header, err := tar.FileInfoHeader(info, "")
if err != nil {
return "", fmt.Errorf("error creating tar file info header: %w", err)
}
header.Name = filepath.ToSlash(relPath)

if err := tarWriter.WriteHeader(header); err != nil {
return "", err
}
var eg errgroup.Group

if info.IsDir() {
zap.L().Debug("Including directory reference: " + relPath)
continue
}
eg.Go(func() error {
return walker.Start()
})

file, err := os.Open(path.Location)
if err != nil {
return "", fmt.Errorf("error opening file: %w", err)
processFiles := func() error {
for path := range paths {
info, err := os.Stat(path.Location)
if err != nil {
return fmt.Errorf("error statting file: %w", err)
}

if !info.Mode().IsRegular() {
continue
}

relPath, err := filepath.Rel(srcFolder, path.Location)
if err != nil {
return fmt.Errorf("error getting relative path: %w", err)
}

zap.L().Debug("Considering file: " + relPath)

header, err := tar.FileInfoHeader(info, "")
if err != nil {
return fmt.Errorf("error creating tar file info header: %w", err)
}
header.Name = filepath.ToSlash(relPath)

if err := tarWriter.WriteHeader(header); err != nil {
return err
}

if info.IsDir() {
zap.L().Debug("Including directory reference: " + relPath)
continue
}

file, err := os.Open(path.Location)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}

written, err := io.Copy(tarWriter, file)
file.Close()
if err != nil {
return fmt.Errorf("error copying file content for %s: %w", path.Location, err)
}
if written != info.Size() {
return fmt.Errorf("expected to write %d bytes but wrote %d bytes for file %s", info.Size(), written, path.Location)
}

zap.L().Debug("Including file: " + relPath)
}
defer file.Close()
return nil
}

written, err := io.Copy(tarWriter, file)
if err != nil {
return "", fmt.Errorf("error copying file content for %s: %w", path.Location, err)
}
if written != info.Size() {
return "", fmt.Errorf("expected to write %d bytes but wrote %d bytes for file %s", info.Size(), written, path.Location)
if err := processFiles(); err != nil {
walker.Terminate()
for range paths { // drain
}
_ = eg.Wait()
return "", err
}

zap.L().Debug("Including file: " + relPath)
if err := eg.Wait(); err != nil {
return "", err
}

return tarGzFile.Name(), nil
Expand Down
Loading