From 170b0393e1193865f7666033ac5581dc01ef44ff Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sat, 8 Jan 2022 13:32:12 +0100 Subject: [PATCH] file: return errors for use in assertions --- file.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/file.lua b/file.lua index 35e435a..bd912ea 100644 --- a/file.lua +++ b/file.lua @@ -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