Skip to content

Commit

Permalink
Add minetest.safe_write_file() to script API
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Nov 8, 2017
1 parent 9526c68 commit b692454
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -2380,6 +2380,10 @@ Strings that need to be translated can contain several escapes, preceded by `@`.
* nil: return all entries,
* true: return only subdirectory names, or
* false: return only file names.
* `minetest.safe_file_write(path, content)`: returns boolean indicating success
* Replaces contents of file at path with new contents in a safe (atomic) way.
Use this instead of below code when writing e.g. database files:
`local f = io.open(path, "wb"); f:write(content); f:close()`
* `minetest.get_version()`: returns a table containing components of the
engine version. Components:
* `project`: Name of the project, eg, "Minetest"
Expand Down
18 changes: 18 additions & 0 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -357,6 +357,23 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
return 1;
}

// safe_file_write(path, content)
int ModApiUtil::l_safe_file_write(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
const char *path = luaL_checkstring(L, 1);
size_t size;
const char *content = luaL_checklstring(L, 2, &size);

CHECK_SECURE_PATH(L, path, true);

bool ret = fs::safeWriteToFile(path, std::string(content, size));
lua_pushboolean(L, ret);

return 1;
}

// request_insecure_environment()
int ModApiUtil::l_request_insecure_environment(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
Expand Down Expand Up @@ -476,6 +493,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)

API_FCT(mkdir);
API_FCT(get_dir_list);
API_FCT(safe_file_write);

API_FCT(request_insecure_environment);

Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_util.h
Expand Up @@ -80,6 +80,9 @@ class ModApiUtil : public ModApiBase
// get_dir_list(path, is_dir)
static int l_get_dir_list(lua_State *L);

// safe_file_write(path, content)
static int l_safe_file_write(lua_State *L);

// request_insecure_environment()
static int l_request_insecure_environment(lua_State *L);

Expand Down

0 comments on commit b692454

Please sign in to comment.