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 f01df5e commit acb945f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 52 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
25 changes: 0 additions & 25 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package file

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"io/fs"
Expand Down Expand Up @@ -59,26 +57,3 @@ func probablyMatches(filename string, data []byte) (bool, error) {

return bytes.Equal(data[offset:], buf[:n]), nil
}

// CheckHash checks if a file has the given SHA256 hash.
func CheckHash(filename, hash string) (bool, error) {
fh, err := sha256File(filename)
if err != nil {
return false, err
}
return fh == hash, nil
}

// sha256File calculates the SHA256 hash of a file.
func sha256File(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
45 changes: 19 additions & 26 deletions internal/legacy/php_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +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 string

//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")
hashPath := filepath.Join(destDir, "hash")
hashOK, err := file.CheckHash(hashPath, phpCLIHash)
if err != nil && !os.IsNotExist(err) {
return err
}
if hashOK {
return nil
}
br := bytes.NewReader(phpCLI)
r, err := zip.NewReader(br, int64(len(phpCLI)))

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

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

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

return file.CopyIfChanged(hashPath, []byte(phpCLIHash), 0o644)
return g.Wait()
}

// phpPath returns the path to the temporary PHP-CLI binary
Expand All @@ -65,13 +59,13 @@ func copyZipFile(f *zip.File, destDir string) error {

if f.FileInfo().IsDir() {
if err := os.MkdirAll(absPath, 0755); err != nil {
return fmt.Errorf("could create extracted directory %s: %w", absPath, err)
return fmt.Errorf("could not create extracted directory %s: %w", absPath, err)
}
return nil
}

if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
return fmt.Errorf("could create parent directory for extracted file %s: %w", absPath, err)
return fmt.Errorf("could not create parent directory for extracted file %s: %w", absPath, err)
}

rc, err := f.Open()
Expand All @@ -80,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 acb945f

Please sign in to comment.