@@ -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
0 commit comments