Skip to content

Commit 8455de1

Browse files
Merge branch 'maximchick-master'
2 parents 55b7cae + 7a87291 commit 8455de1

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

apply.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func Apply(update io.Reader, opts Options) error {
112112
return err
113113
}
114114
defer fp.Close()
115+
115116
_, err = io.Copy(fp, bytes.NewReader(newBytes))
116117
if err != nil {
117118
return err
@@ -122,7 +123,11 @@ func Apply(update io.Reader, opts Options) error {
122123
fp.Close()
123124

124125
// this is where we'll move the executable to so that we can swap in the updated replacement
125-
oldPath := filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
126+
oldPath := opts.OldSavePath
127+
removeOld := opts.OldSavePath == ""
128+
if removeOld {
129+
oldPath = filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
130+
}
126131

127132
// delete any existing old exec file - this is necessary on Windows for two reasons:
128133
// 1. after a successful update, Windows can't remove the .old file because the process is still running
@@ -154,12 +159,14 @@ func Apply(update io.Reader, opts Options) error {
154159
return err
155160
}
156161

157-
// move successful, remove the old binary
158-
errRemove := os.Remove(oldPath)
162+
// move successful, remove the old binary if needed
163+
if removeOld {
164+
errRemove := os.Remove(oldPath)
159165

160-
// windows has trouble with removing old binaries, so hide it instead
161-
if errRemove != nil {
162-
_ = hideFile(oldPath)
166+
// windows has trouble with removing old binaries, so hide it instead
167+
if errRemove != nil {
168+
_ = hideFile(oldPath)
169+
}
163170
}
164171

165172
return nil
@@ -212,6 +219,10 @@ type Options struct {
212219
// If nil, treat the update as a complete replacement for the contents of the file at TargetPath.
213220
// If non-nil, treat the update contents as a patch and use this object to apply the patch.
214221
Patcher Patcher
222+
223+
// Store the old executable file at this path after a successful update.
224+
// The empty string means the old executable file will be removed after the update.
225+
OldSavePath string
215226
}
216227

217228
// CheckPermissions determines whether the process has the correct permissions to

apply_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ func TestApplySimple(t *testing.T) {
6161
validateUpdate(fName, err, t)
6262
}
6363

64+
func TestApplyOldSavePath(t *testing.T) {
65+
t.Parallel()
66+
67+
fName := "TestApplyOldSavePath"
68+
defer cleanup(fName)
69+
writeOldFile(fName, t)
70+
71+
oldfName := "OldSavePath"
72+
73+
err := Apply(bytes.NewReader(newFile), Options{
74+
TargetPath: fName,
75+
OldSavePath: oldfName,
76+
})
77+
validateUpdate(fName, err, t)
78+
79+
if _, err := os.Stat(oldfName); os.IsNotExist(err) {
80+
t.Fatalf("Failed to find the old file: %v", err)
81+
}
82+
83+
cleanup(oldfName)
84+
}
85+
6486
func TestVerifyChecksum(t *testing.T) {
6587
t.Parallel()
6688

0 commit comments

Comments
 (0)