Fix build broken after #1634 (#1642)

This commit is contained in:
Mihai Maruseac
2025-10-17 06:01:17 +02:00
committed by GitHub
parent 9a39d20213
commit c706e8e673
3 changed files with 15 additions and 21 deletions
-1
View File
@@ -10,7 +10,6 @@ require (
github.com/go-chi/render v1.0.3
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a
github.com/google/renameio v1.0.1
github.com/spf13/cobra v1.10.1
github.com/stretchr/testify v1.11.1
)
-2
View File
@@ -17,8 +17,6 @@ github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9v
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a h1:l7A0loSszR5zHd/qK53ZIHMO8b3bBSmENnQ6eKnUT0A=
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+15 -18
View File
@@ -2,15 +2,16 @@ package cmd
import (
"archive/zip"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/google/renameio"
"github.com/spf13/cobra"
)
@@ -95,7 +96,7 @@ func upgrade(urlPrefix string) error {
fmt.Printf("Now going to replace the existing silverbullet binary in %s\n", installDir)
// Extract the zip file
err = extractZip(tmpDir, zipPath, installDir)
err = extractZip(zipPath, installDir)
if err != nil {
return fmt.Errorf("failed to extract zip: %w", err)
}
@@ -114,7 +115,7 @@ func upgrade(urlPrefix string) error {
return nil
}
func extractZip(tmpDir, src, dest string) error {
func extractZip(src, dest string) error {
reader, err := zip.OpenReader(src)
if err != nil {
return err
@@ -143,28 +144,24 @@ func extractZip(tmpDir, src, dest string) error {
return err
}
// Create the file as a temporary file first
tempFile, err := renameio.TempFile(tmpDir, path)
// First, attempt to remove the file to prevent "text file busy" error
err = os.Remove(path)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
// Create the file. It will create a new inode, so we can write to it while the executable still runs
outFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.FileInfo().Mode())
if err != nil {
return err
}
defer tempFile.Cleanup()
defer outFile.Close()
// Extract to the temporary file
_, err = io.Copy(tempFile, rc)
// Extract to the file
_, err = io.Copy(outFile, rc)
if err != nil {
return err
}
// Properly set the mode of the target file
if err := tempFile.Chmod(file.FileInfo().Mode()); err != nil {
return err
}
// Atomically create the target file
if err := tempFile.CloseAtomicallyReplace(); err != nil {
return err
}
}
return nil