Improve StagedFile tempfile handling

Two improvements:

 - Make tempfile creation lazy. Some checks work directly with
   StagedFile#contents and don't need an on-disk file. This should save
   a little time for those checks.

 - Close the tempfile after writing to it. This should resolve the
   reported "Too many open files" error seen when very large numbers of
   files are committed.

Change-Id: I528500b94838858cec055a2b97791662a46e89d8
Reviewed-on: https://gerrit.causes.com/26923
Tested-by: jenkins <jenkins@causes.com>
Reviewed-by: Greg Hurrell <greg@causes.com>