From 5dfa02f5312a512d096fb0faef3e651682baab96 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 8 Jan 2025 22:08:29 +0000 Subject: [PATCH] Use a -tmp and rename pattern for Windows too --- internal/legacy/php_windows.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/legacy/php_windows.go b/internal/legacy/php_windows.go index e5593fe..9950cf0 100644 --- a/internal/legacy/php_windows.go +++ b/internal/legacy/php_windows.go @@ -9,8 +9,6 @@ import ( "os" "path/filepath" "strings" - - "github.com/platformsh/cli/internal/file" ) //go:embed archives/php_windows.zip @@ -27,8 +25,8 @@ 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 } @@ -36,23 +34,27 @@ func (c *CLIWrapper) copyPHP(cacheDir string) error { 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 @@ -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()