Monday, September 10, 2012

Use Passive FTP

Stonefield Query has a built-in automatic update feature. It basically works like this:

  • Once every X days (where X is configurable by the user), download an XML file from our web server. The file contains the update version number, a list of files to download and install, and HTML describing the changes.
  • If the update version number is newer than the current version number, display the changes to the user and ask if they want to download it.
  • If they do, download and install the files listed in the XML file.

The last step is a little complicated. First, you obviously can’t overwrite the running EXE. Second, in Windows Vista and later, you can’t even write to the program folder without elevating privileges. We take care of that as follows:

  • Download the files to the user’s temporary files folder (which is writable).
  • Using ShellExecute with the “RunAs” parameter, launch an updater EXE and display the User Account Control (UAC) dialog requesting permission to elevate.
  • Quit so the main EXE is no longer running.
  • Have the updater EXE copy the downloaded files to the program folder, unzipping or running a setup program as necessary.
  • Have the updater EXE run the main EXE and then quit itself.

This has normally worked just fine. However, recently we had reports that Stonefield Query would take a long time to start for some users. We have great diagnostics built into Stonefield Query, so we quickly determined that the update checking, which runs at startup, was taking five minutes to execute. That sounded a lot like a timeout, which we confirmed by adding additional diagnostics to the FTP process. Another clue was that the problem only seemed to happen on Windows Server 2008 systems. Once again proving that Google is my friend, I found several possible solutions. The one that seemed most promising was to use passive mode for FTP transfers. Since I use Rick Strahl’s West Wind Client Tools, implementing this was easy: set the wwFTP object’s lPassiveFTP property to .T. Since we ourselves have a Windows Server 2008 server, it was easy to reproduce the problem and confirm that this fixed it.

Once again, Google and Rick save my bacon.

3 comments:

Unknown said...

Doug, I'm just starting to educate myself about app updating approaches, and yours strikes me as something less than "automatic." Automatic, to me, is what Google Chrome does -- there's no quit, install, restart. It's just done. Do you have an understanding of what approach is being used there?
-Mark Winston

Doug Hennig said...

Hi Mark.

I'm not sure how Chrome does it, but I can't see any other way to update the running application other than quitting and restarting it. And by automatic I mean the user doesn't have to manually download and run an installer -- the software does it by itself.

Doug

Unknown said...

Google use the Task Scheduler to run the updater with elevated privileges everytime the machine starts and on a daily basis. I'd expect that the updated code doesn't run until you restart Chrome.