Skip to content

Commit 5dbf76a

Browse files
TUN-7335: Fix cloudflared update not working in windows
This PR fixes some long standing bugs in the windows update paths. We previously did not surface the errors at all leading to this function failing silently. This PR: 1. Now returns the ExitError if the bat run for update fails. 2. Fixes the errors surfaced by that return: a. The batch file doesnt play well with spaces. This is fixed by using PROGRA~1/2 which are aliases windows uses. b. The existing script also seemed to be irregular about where batch files were put and looked for. This is also fixed in this script.
1 parent 8d87d4f commit 5dbf76a

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

cmd/cloudflared/updater/update.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88
"runtime"
9+
"strings"
910
"time"
1011

1112
"github.com/facebookgo/grace/gracenet"
@@ -95,11 +96,24 @@ func CheckForUpdate(options updateOptions) (CheckResult, error) {
9596
url = StagingUpdateURL
9697
}
9798

99+
if runtime.GOOS == "windows" {
100+
cfdPath = encodeWindowsPath(cfdPath)
101+
}
102+
98103
s := NewWorkersService(version, url, cfdPath, Options{IsBeta: options.isBeta,
99104
IsForced: options.isForced, RequestedVersion: options.intendedVersion})
100105

101106
return s.Check()
102107
}
108+
func encodeWindowsPath(path string) string {
109+
// We do this because Windows allows spaces in directories such as
110+
// Program Files but does not allow these directories to be spaced in batch files.
111+
targetPath := strings.Replace(path, "Program Files (x86)", "PROGRA~2", -1)
112+
// This is to do the same in 32 bit systems. We do this second so that the first
113+
// replace is for x86 dirs.
114+
targetPath = strings.Replace(targetPath, "Program Files", "PROGRA~1", -1)
115+
return targetPath
116+
}
103117

104118
func applyUpdate(options updateOptions, update CheckResult) UpdateOutcome {
105119
if update.Version() == "" || options.updateDisabled {

cmd/cloudflared/updater/workers_update.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ const (
2525
// rename cloudflared.exe.new to cloudflared.exe
2626
// delete cloudflared.exe.old
2727
// start the service
28-
// delete the batch file
29-
windowsUpdateCommandTemplate = `@echo off
30-
sc stop cloudflared >nul 2>&1
28+
// exit with code 0 if we've reached this point indicating success.
29+
windowsUpdateCommandTemplate = `sc stop cloudflared >nul 2>&1
3130
rename "{{.TargetPath}}" {{.OldName}}
3231
rename "{{.NewPath}}" {{.BinaryName}}
3332
del "{{.OldPath}}"
3433
sc start cloudflared >nul 2>&1
35-
del {{.BatchName}}`
34+
exit /b 0`
3635
batchFileName = "cfd_update.bat"
3736
)
3837

@@ -214,8 +213,9 @@ func isValidChecksum(checksum, filePath string) error {
214213
// writeBatchFile writes a batch file out to disk
215214
// see the dicussion on why it has to be done this way
216215
func writeBatchFile(targetPath string, newPath string, oldPath string) error {
217-
os.Remove(batchFileName) //remove any failed updates before download
218-
f, err := os.Create(batchFileName)
216+
batchFilePath := filepath.Join(filepath.Dir(targetPath), batchFileName)
217+
os.Remove(batchFilePath) //remove any failed updates before download
218+
f, err := os.Create(batchFilePath)
219219
if err != nil {
220220
return err
221221
}
@@ -241,6 +241,16 @@ func writeBatchFile(targetPath string, newPath string, oldPath string) error {
241241

242242
// run each OS command for windows
243243
func runWindowsBatch(batchFile string) error {
244-
cmd := exec.Command("cmd", "/c", batchFile)
245-
return cmd.Start()
244+
defer os.Remove(batchFile)
245+
cmd := exec.Command("cmd", "/C", batchFile)
246+
_, err := cmd.Output()
247+
// Remove the batch file we created. Don't let this interfere with the error
248+
// we report.
249+
if err != nil {
250+
if exitError, ok := err.(*exec.ExitError); ok {
251+
return fmt.Errorf("Error during update : %s;", string(exitError.Stderr))
252+
}
253+
254+
}
255+
return err
246256
}

0 commit comments

Comments
 (0)