Skip to content

Commit

Permalink
savefile.lua does not use os.tmpname() since it's unreliable on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Dec 5, 2016
1 parent 79095e1 commit f1b4840
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions ludobits/m/savefile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
-- file.save({ foo = "bar" })
--

local M = {}
local M = {
tmpname = "___lbtmp"
}


local function get_path(filename)
function M.get_path(filename)
local path = sys.get_save_file(sys.get_config("project.title"):gsub(" ", "_"), filename)
return path
end
Expand All @@ -22,7 +23,7 @@ end
-- @param filename
-- @return file instance
function M.open(filename)
local path = get_path(filename)
local path = M.get_path(filename)
local instance = {}

--- Load the contents of the file
Expand All @@ -37,7 +38,14 @@ function M.open(filename)
-- @return error_message
function instance.save(t)
assert(t and type(t) == "table", "You must provide a table to save")
local tmpname = os.tmpname()
--local tmpname = ((sys.get_sys_info().system_name == "Windows") and os.getenv("TMP") or "") .. os.tmpname()
-- os.tmpname() is not very reliable on windows (it doesn't return a valid path to a temp file)
-- it's better to return the path to an application local file, and in this case we use the same filename
-- every time so that we do not pollute the filesystem with many temporary files in case of problems
-- when writing to the file
-- it is reasonable to assume that a filename such as "___lbtmp" isn't used by any other part of the
-- system, and if that is actually the case it is possible to change it
local tmpname = M.get_path(M.tmpname)
local success = sys.save(tmpname, t)
if not success then
return false, "Unable to save file"
Expand Down

0 comments on commit f1b4840

Please sign in to comment.