Skip to content

Commit

Permalink
Use a -tmp and rename pattern for Windows too
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 9, 2025
1 parent a621df0 commit 5dfa02f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions internal/legacy/php_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"os"
"path/filepath"
"strings"

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

//go:embed archives/php_windows.zip
Expand All @@ -27,32 +25,36 @@ func (c *CLIWrapper) copyPHP(cacheDir string) error {
destDir := filepath.Join(cacheDir, "php")

// Check if the hash is the same as expected.
hashPath := filepath.Join(destDir, "sha256sum.txt")
hashContents, err := os.ReadFile(hashPath)
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
}

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)
}

tmpDir := destDir + "-tmp"
for _, f := range r.File {
if err := copyZipFile(f, destDir); err != nil {
if err := copyZipFile(f, tmpDir); err != nil {
return err
}
}

if err := os.WriteFile(filepath.Join(destDir, "extras", "cacert.pem"), caCert, 0o644); err != nil {
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
}

return file.CopyIfChanged(hashPath, phpCLIHash, 0o644)
return os.Rename(tmpDir, destDir)
}

// phpPath returns the path to the temporary PHP-CLI binary
Expand All @@ -68,13 +70,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 Down

0 comments on commit 5dfa02f

Please sign in to comment.