fiorix/freegeoip — pull request #100 by GUI (2014-12-30 18:28:41+00:00)
Title: Prevent auto update temp files from accumulating on errors

This solves two semi-related issues with the handling of the temporary
files during the auto update process:

- The /tmp/freegeoip directory was being created when the process first
started, but if that directory went away for any reason, then the auto
update process would begin failing, since it couldn't rename the
temporary downloaded file into a non-existent /tmp/freegeoip
directory. This fixes that issue by ensuring the directory exists (and
creates it if not) prior to each rename.
- If renaming the temporary data file failed to be renamed for any
reason (for example, due to the missing directory issue above), then
the temp files would begin to accumulate indefinitely. This could lead
to your /tmp partition filling up. This fixes that issues by ensuring
the temporary files are always destroyed if errors during the rename
are encountered.

For reference, how I encountered these issues was on a couple of RHEL6 boxes where we had the freegeoip services installed, but it wasn't actively being used. RedHat uses tmpwatch and it's setup by default to automatically delete /tmp/* files if they haven't been accessed in 10 days. So after 10 days of inactivity on the `/tmp/freegeoip/db.gz` file, tmpwatch deleted both the file and the directory. This led to a bunch of files like `/tmp/_freegeoip.1419860568582031321.db.gz` piling up until the /tmp partition ran out of space (since the rename into the `/tmp/freegeoip` directory was failing since the directory no longer existed). So while this might have been an odd corner case of usage (having freegeoip running, but not using it), I think these couple of small changes should hopefully help make things more resilient to unexpected things happening with the temp directory and prevent unexpected file growth even if things start failing badly.