lovr.filesystem.write/append returns success instead of size;

We don't have a good way of returning filesystem error messages yet,
but it's still useful to return a boolean instead of a number to
detect failure of zero byte writes.  Exposing the number of bytes
written is kind of weird since it's not very actionable.
This commit is contained in:
bjorn 2022-03-30 22:45:09 -07:00
parent 42e618fa00
commit cd0e458af9
3 changed files with 13 additions and 9 deletions

View File

@ -86,7 +86,8 @@ static int l_lovrFilesystemAppend(lua_State* L) {
} else {
return luax_typeerror(L, 2, "string or Blob");
}
lua_pushinteger(L, lovrFilesystemWrite(path, data, size, true));
bool success = lovrFilesystemWrite(path, data, size, true);
lua_pushboolean(L, success);
return 1;
}
@ -334,7 +335,8 @@ static int l_lovrFilesystemWrite(lua_State* L) {
} else {
return luax_typeerror(L, 2, "string or Blob");
}
lua_pushinteger(L, lovrFilesystemWrite(path, data, size, false));
bool success = lovrFilesystemWrite(path, data, size, false);
lua_pushboolean(L, success);
return 1;
}

View File

@ -376,20 +376,22 @@ bool lovrFilesystemRemove(const char* path) {
return valid(path) && concat(resolved, state.savePath, state.savePathLength, path, strlen(path)) && fs_remove(resolved);
}
size_t lovrFilesystemWrite(const char* path, const char* content, size_t size, bool append) {
bool lovrFilesystemWrite(const char* path, const char* content, size_t size, bool append) {
char resolved[LOVR_PATH_MAX];
if (!valid(path) || !concat(resolved, state.savePath, state.savePathLength, path, strlen(path))) {
return 0;
return false;
}
fs_handle file;
if (!fs_open(resolved, append ? OPEN_APPEND : OPEN_WRITE, &file)) {
return 0;
return false;
}
fs_write(file, content, &size);
fs_close(file);
return size;
if (!fs_write(file, content, &size)) {
return false;
}
return fs_close(file);
}
// Paths

View File

@ -30,7 +30,7 @@ bool lovrFilesystemSetIdentity(const char* identity, bool precedence);
const char* lovrFilesystemGetSaveDirectory(void);
bool lovrFilesystemCreateDirectory(const char* path);
bool lovrFilesystemRemove(const char* path);
size_t lovrFilesystemWrite(const char* path, const char* content, size_t size, bool append);
bool lovrFilesystemWrite(const char* path, const char* content, size_t size, bool append);
size_t lovrFilesystemGetAppdataDirectory(char* buffer, size_t size);
size_t lovrFilesystemGetExecutablePath(char* buffer, size_t size);
size_t lovrFilesystemGetUserDirectory(char* buffer, size_t size);