file: return errors for use in assertions

master
Lars Mueller 2022-01-08 13:32:12 +01:00
parent 9bfefb9283
commit 170b0393e1
1 changed files with 12 additions and 13 deletions

View File

@ -25,16 +25,16 @@ end
-- concat_path is set by init.lua to avoid code duplication
function read(filename)
local file = io.open(filename, "r")
if file == nil then return nil end
local file, err = io.open(filename, "r")
if file == nil then return nil, err end
local content = file:read"*a"
file:close()
return content
end
function write_unsafe(filename, new_content)
local file = io.open(filename, "w")
if file == nil then return false end
local file, err = io.open(filename, "w")
if file == nil then return false, err end
file:write(new_content)
file:close()
return true
@ -42,9 +42,9 @@ end
write = minetest and minetest.safe_file_write or write_unsafe
function write_binary(filename, new_content)
local file = io.open(filename, "wb")
if file == nil then return false end
function write_binary_unsafe(filename, new_content)
local file, err = io.open(filename, "wb")
if file == nil then return false, err end
file:write(new_content)
file:close()
return true
@ -61,24 +61,23 @@ function ensure_content(filename, ensured_content)
end
function append(filename, new_content)
local file = io.open(filename, "a")
if file == nil then return false end
local file, err = io.open(filename, "a")
if file == nil then return false, err end
file:write(new_content)
file:close()
return true
end
function exists(filename)
local file = io.open(filename, "r")
if file == nil then return false end
local file, err = io.open(filename, "r")
if file == nil then return false, err end
file:close()
return true
end
function create_if_not_exists(filename, content)
if not exists(filename) then
write(filename, content or "")
return true
return write(filename, content or "")
end
return false
end