Skip to content

Commit

Permalink
Apply CopyIfChanged to Windows files (concurrently)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 10, 2025
1 parent 71f5892 commit 55e0305
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
touch internal/legacy/archives/php_darwin_amd64
touch internal/legacy/archives/php_darwin_arm64
touch internal/config/embedded-config.yaml
touch internal/legacy/archives/php_windows.zip.sha256
- name: Run linter
uses: golangci/golangci-lint-action@v6
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/wk8/go-ordered-map/v2 v2.1.8
golang.org/x/crypto v0.31.0
golang.org/x/sync v0.10.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
46 changes: 17 additions & 29 deletions internal/legacy/php_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,42 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"golang.org/x/sync/errgroup"

"github.com/platformsh/cli/internal/file"
)

//go:embed archives/php_windows.zip
var phpCLI []byte

//go:embed archives/php_windows.zip.sha256
var phpCLIHash []byte

//go:embed archives/cacert.pem
var caCert []byte

// copyPHP to destination, if it does not exist
func (c *CLIWrapper) copyPHP(cacheDir string) error {
destDir := filepath.Join(cacheDir, "php")

// Check if the hash is the same as expected.
hashFilename := "sha256sum.txt"
hashContents, err := os.ReadFile(filepath.Join(destDir, hashFilename))
if err != nil && !os.IsNotExist(err) {
return err
}
if bytes.Equal(hashContents, phpCLIHash) {
return nil
}

r, err := zip.NewReader(bytes.NewReader(phpCLI), int64(len(phpCLI)))
if err != nil {
return fmt.Errorf("could not open zip reader: %w", err)
}

tmpDir := destDir + "-tmp"
g := errgroup.Group{}
g.SetLimit(runtime.GOMAXPROCS(0) * 2)
for _, f := range r.File {
if err := copyZipFile(f, tmpDir); err != nil {
return err
}
g.Go(func() error {
return copyZipFile(f, destDir)
})
}

if err := os.WriteFile(filepath.Join(tmpDir, "extras", "cacert.pem"), caCert, 0o644); err != nil {
return err
}

if err := os.WriteFile(filepath.Join(tmpDir, hashFilename), phpCLIHash, 0o644); err != nil {
return err
}
g.Go(func() error {
return file.CopyIfChanged(filepath.Join(destDir, "extras", "cacert.pem"), caCert, 0o644)
})

return os.Rename(tmpDir, destDir)
return g.Wait()
}

// phpPath returns the path to the temporary PHP-CLI binary
Expand Down Expand Up @@ -85,13 +74,12 @@ func copyZipFile(f *zip.File, destDir string) error {
}
defer rc.Close()

destFile, err := os.OpenFile(absPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, f.Mode())
b, err := io.ReadAll(rc)
if err != nil {
return fmt.Errorf("could not open destination for extracted file %s: %w", absPath, err)
return fmt.Errorf("could not extract zipped file %s: %w", f.Name, err)
}
defer destFile.Close()

if _, err := io.Copy(destFile, rc); err != nil {
if err := file.CopyIfChanged(absPath, b, f.Mode()); err != nil {
return fmt.Errorf("could not write extracted file %s: %w", absPath, err)
}

Expand Down

0 comments on commit 55e0305

Please sign in to comment.