* Plugin LUA Temporary Files

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
tatewise
Megastar
Posts: 27075
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Plugin LUA Temporary Files

Post by tatewise » 27 Feb 2013 21:32

Several Plugins use os.getenv('TEMP')..os.tmpname() to obtain a temporary file names.

The LUA 5.1 Reference Manual says:
os.tmpname ()
Returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed.
When possible, you may prefer to use io.tmpfile, which automatically removes the file when the program ends.
Certainly, os.getenv('TEMP')..os.tmpname() files are not auto-deleted and must be removed using Disk Cleanup on Temporary files.
But io.tmpfile does NOT allow the file extension to be defined.

This has come to light as a result of working on the Ancestors ... Census Checker and Lookup Missing Census Facts Plugins.
Can anyone suggest the best way of using temporary files that auto-delete.

ID:6791
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Jane
Site Admin
Posts: 8440
Joined: 01 Nov 2002 15:00
Family Historian: V7
Location: Somerset, England
Contact:

Plugin LUA Temporary Files

Post by Jane » 28 Feb 2013 13:00

I am not aware of any Auto Delete support other than disk cleanup in windows.

Another option would be to give the web pages meaningful names and clean them up next time the plugin is started.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."

User avatar
tatewise
Megastar
Posts: 27075
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Plugin LUA Temporary Files

Post by tatewise » 28 Feb 2013 15:09

I came to the same conclusion.
So I use file-names such as:

os.getenv('TEMP')..os.tmpname()..string.lower(StrPlugin):gsub(' ','')..'.html'

and similarly for '.css' and '.js' and where StrPlugin is the Plugin Name.

I include os.tmpname() so if the Plugin is run several times, while browser still has old HTML open, each run uses a different file-name.

(I use lower-case because it is more HTML friendly.)

Then to delete old versions of the files:

DeleteOldFiles(os.getenv('TEMP'),string.lower(StrPlugin):gsub(' ',''))

Code: Select all

-- Delete files accessed more than 1 hour ago --
function DeleteOldFiles(strFolder,strFile)
      intTime = os.time() - 3600
      for strEntry in lfs.dir(strFolder) do
            if strEntry ~= '.' and strEntry ~= '..' then
                  strPath = strFolder..'\'..strEntry
                  local tblAttr, strError = lfs.attributes(strPath)
                  if not tblAttr then tblAttr = { mode='attrfail', error=strError } end 
                  if tblAttr.mode == 'file' and strEntry:match(strFile) then
                        if tblAttr.access < intTime then DeleteFile(strPath) end
                  end
            end
      end
end -- function DeleteOldFiles
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

Post Reply