Skip to content

getWorkingDirectory: implemented to return "./" #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions filesystem.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include "filesystem.h"
#include "lutro.h"
#include "lutro_assert.h"

#include <compat/strl.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <vfs/vfs_implementation.h>
#include <string/stdstring.h>

// for getcwd...
#if defined(_WIN32)
# include <direct.h>
#else
# include <unistd.h>
#endif

#if WANT_PHYSFS
#include "physfs.h"
#endif
Expand Down Expand Up @@ -36,6 +45,7 @@ int lutro_filesystem_preload(lua_State *L)
{ "load", fs_load },
{ "setIdentity", fs_setIdentity },
{ "getAppdataDirectory", fs_getAppdataDirectory },
{ "getWorkingDirectory", fs_getWorkingDirectory },
{ "isDirectory", fs_isDirectory },
{ "isFile", fs_isFile },
{ "createDirectory", fs_createDirectory },
Expand Down Expand Up @@ -292,6 +302,24 @@ int fs_getAppdataDirectory(lua_State *L)
return 1;
}


/**
* lutro.filesystem.getWorkingDirectory
*
* Retrieves the process current working directory (cwd or pwd). Lutro always returns the
* relative directory prefix "./" which is generally interpreted by filesystem APIs as the
* cwd when used as the prefix to a function. LOVE returns the CWD as an absolute path.
* Note that the concept of cwd is inherently non-portable and leads to unexpected or surprising
* behavior for users, and should only be used for local development or debug purposes.
*
* @see https://love2d.org/wiki/love.filesystem.getWorkingDirectory
*/
int fs_getWorkingDirectory(lua_State *L)
{
lua_pushstring(L, "./");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me! While Working directory isn't really a thing in libretro, I think this is a worthy workaround.

return 1;
}

int fs_isDirectory(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
Expand Down
10 changes: 10 additions & 0 deletions test/unit/modules/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ function lutro.filesystem.getAppdataDirectoryTest()
unit.assertIsString(appdataDir)
end

function lutro.filesystem.getWorkingDirectoryTest()
local getWorkingDir = lutro.filesystem.getWorkingDirectory()
local getWorkingDirFixed = getWorkingDir
if getWorkingDirFixed:sub(-1) ~= '/' then
getWorkingDirFixed = getWorkingDirFixed .. '/'
end
unit.assertEquals(getWorkingDir, getWorkingDirFixed)
end

function lutro.filesystem.getUserDirectoryTest()
-- UserDirectory should always have a trailing slash. This is easy to verify.
-- Other aspects of UserDirectory are platform specific and non-trivial to calculate and there
Expand Down Expand Up @@ -58,6 +67,7 @@ end
return {
lutro.filesystem.setRequirePathTest,
lutro.filesystem.getRequirePathTest,
lutro.filesystem.getWorkingDirectoryTest,
lutro.filesystem.getUserDirectoryTest,
lutro.filesystem.getAppdataDirectoryTest,
lutro.filesystem.getDirectoryItemsTest,
Expand Down
Loading