summaryrefslogtreecommitdiff
path: root/src/luaentity_common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/luaentity_common.h')
0 files changed, 0 insertions, 0 deletions
first return value is returned by load_atomic -- Important: the callback must close the file in all cases! function serialize_lib.save_atomic(data, filename, callback, config) if serialize_lib.save_lock then serialize_lib.log_warn("Instructed to save '"..filename.."', but save lock is active!") return nil end local cbfunc = callback or ser.write_to_fd local file, ret = io.open(filename..".new", "w") if file then -- save the file using the callback local success success, ret = pcall(cbfunc, data, file) if success then return save_atomic_move_file(filename) end end serialize_lib.log_error("Unable to save data to '"..filename..".new':") serialize_lib.log_error(ret) return nil, ret end -- Saves multiple files synchronously. First writes all data to all .new files, -- then moves all files in quick succession to avoid inconsistent backups. -- parts_table is a table where the keys are used as part of the filename and the values -- are the respective data written to it. -- e.g. if parts_table={foo={...}, bar={...}}, then foo and bar are written out. -- if 'callbacks_table' is defined, it is consulted for callbacks the same way save_atomic does. -- example: if callbacks_table = {foo = func()...}, then the callback is used during writing of file 'foo' (but not for 'bar') -- Note however that you must at least insert a "true" in the parts_table if you don't use the data argument. -- Important: the callback must close the file in all cases! function serialize_lib.save_atomic_multiple(parts_table, filename_prefix, callbacks_table, config) if serialize_lib.save_lock then serialize_lib.log_warn("Instructed to save '"..filename_prefix.."' (multiple), but save lock is active!") return nil end for subfile, data in pairs(parts_table) do local filename = filename_prefix..subfile local cbfunc = ser.write_to_fd if callbacks_table and callbacks_table[subfile] then cbfunc = callbacks_table[subfile] end local success = false local file, ret = io.open(filename..".new", "w") if file then -- save the file using the callback success, ret = pcall(cbfunc, data, file, config) end if not success then serialize_lib.log_error("Unable to save data to '"..filename..".new':") serialize_lib.log_error(ret) return nil, ret end end local first_error for file, _ in pairs(parts_table) do local filename = filename_prefix..file local succ, err = save_atomic_move_file(filename) if not succ and not first_error then first_error = err end end return not first_error, first_error -- either true,nil or nil,error end