Skip to content

Commit

Permalink
Fix Windows permission error with self remove (#52316) (#52498)
Browse files Browse the repository at this point in the history
* Fix windows permission error with self replace

* Aggregate errors

* Update comments
  • Loading branch information
vapopov authored Feb 26, 2025
1 parent d756fdf commit e92867e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions integration/autoupdate/tools/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/integration/helpers/archive"
"github.com/gravitational/teleport/lib/modules"
)

const (
Expand All @@ -59,6 +60,7 @@ var (
)

func TestMain(m *testing.M) {
modules.SetModules(&modules.TestModules{TestBuildType: "CLI"})
ctx := context.Background()
tmp, err := os.MkdirTemp(os.TempDir(), testBinaryName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions integration/autoupdate/tools/updater/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func (p *TestModules) BuildType() string {
return "CLI"
}

// IsEnterpriseBuild returns false for [TestModules].
// IsEnterpriseBuild returns true if `UPDATER_TEST_BUILD` env is set `ent` for [TestModules].
func (p *TestModules) IsEnterpriseBuild() bool {
return os.Getenv(TestBuild) == modules.BuildEnterprise
}

// IsOSSBuild returns false for [TestModules].
// IsOSSBuild returns true if `UPDATER_TEST_BUILD` env is set `oss` for [TestModules].
func (p *TestModules) IsOSSBuild() bool {
return os.Getenv(TestBuild) == modules.BuildOSS
}
Expand Down
2 changes: 2 additions & 0 deletions integration/autoupdate/tools/updater_tsh_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,19 @@ func RecursiveChown(dir string, uid, gid int) error {
return nil
}

func CopyFile(src, dest string, perm os.FileMode) error {
func CopyFile(src, dest string, perm os.FileMode) (err error) {
srcFile, err := os.Open(src)
if err != nil {
return trace.ConvertSystemError(err)
}
defer srcFile.Close()
destFile, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return trace.ConvertSystemError(err)
}
defer func() {
err = trace.NewAggregate(err, trace.Wrap(destFile.Close()))
}()
_, err = destFile.ReadFrom(srcFile)
if err != nil {
return trace.ConvertSystemError(err)
Expand Down
10 changes: 4 additions & 6 deletions lib/utils/packaging/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ func replaceZip(toolsDir string, archivePath string, extractDir string, execName
return trace.Wrap(err)
}
appPath := filepath.Join(toolsDir, baseName)
if err := os.Remove(appPath); err != nil && !os.IsNotExist(err) {
return trace.Wrap(err)
}
// For the Windows build we have to copy binary to be able
// to do this without administrative access as it required
// for symlinks.
// For the Windows build, we need to copy the binary to perform updates without requiring
// administrative access, which would otherwise be needed for creating symlinks.
// Since symlinks are not used on the Windows platform, there's no need to remove appPath
// before copying the new binary — it will simply be replaced.
if err := utils.CopyFile(dest, appPath, 0o755); err != nil {
return trace.Wrap(err)
}
Expand Down

0 comments on commit e92867e

Please sign in to comment.