1

I have a self updating standalone application in windows. The current update process goes as:

  1. Main app downloads file from server and save it to disk with a temporary name.
  2. Main app executes a secondary app with parameters.
  3. Main app exits.
  4. Secondary app renames temporary file to overwrite main app.exe.
  5. Secondary app executes main app.
  6. Secondary app exits.

The above works. But I'm wondering if its possible to do this without using a secondary app? Particularly with Golang.

majidarif
  • 18,694
  • 16
  • 88
  • 133
  • Yes, there are some nice 3rd party packages such as https://github.com/inconshreveable/go-update or https://github.com/rhysd/go-github-selfupdate. – icza Mar 19 '19 at 18:00
  • 1
    @icza that library almost certainly does exactly what OP laid out in the question, using a supervisor process to perform the update. There's no way around it, because Windows locks the executable while it's running. – Adrian Mar 19 '19 at 18:02
  • @Adrian Yes, it could be. I haven't looked into how it is implemented. – icza Mar 19 '19 at 19:31
  • This one does the job https://github.com/mouuff/go-rocket-update – Arnaud Aliès Jan 17 '21 at 13:31

2 Answers2

4

The executable file is locked while running. However you can rename the file even though you can't delete it. So download the new app to a temporary file, make your app rename the app.exe to app-old.exe and then rename the temp file to app.exe. Also delete app-old.exe before downloading beginning the rename craze.

Sami Sallinen
  • 3,203
  • 12
  • 16
  • This will work great if you don't need the application to switch to using the new version immediately ( `5. Secondary app executes main app.` ) – Dave S Mar 19 '19 at 18:52
  • This approach actually works for me but my question is, does it work for all versions of windows? I mean, including Win7 and above? Specially when I distribute my app. – majidarif Mar 19 '19 at 21:09
  • Yes, you can rename the EXE file for a running process in Windows Pro version 10.0.17134 Build 17134, I tried it earlier. – Dave S Mar 19 '19 at 21:22
  • I have been using this approach in a an installer (renames old version if in use etc) but not really doing a self-update for ~10 years and haven't seen a problem with it so far. So I guess it should work across different windows versions quite well. – Sami Sallinen Mar 20 '19 at 10:53
  • Already started distributing it and can confirm it works on all platform I've distributed it to. The only thing is I cannot delete the old app. So I just delete it on start of the new app instead of deleting it while downloading. – majidarif Mar 21 '19 at 07:46
2

No. Windows locks writing to or deleting an executable file or DLL while there are any processes running it.

See for example this SO question - Locking Executing Files: Windows does, Linux doesn't. Why?

If it's OK to run an out-of-date version of the application and you only need it updated for the next time that it is run (skipping the step Secondary app executes main app. of restarting the application immediately) then see Sami Sallinen's answer for renaming the running EXE and DLLs.

Dave S
  • 1,427
  • 1
  • 16
  • 18
  • I can actually execute the new version and exit the currently running exe to restart the app. Which meant I didn't have to skip that step. – majidarif Mar 21 '19 at 07:43