Make file link absolute, add fs_is_relative_path

This fixes links not opening for relative paths, as links like `file://temp/skins` cannot be resolved by the shell.
This commit is contained in:
Robert Müller 2022-08-20 20:54:25 +02:00
parent 6a5d99daf1
commit 21fe945ca7
3 changed files with 33 additions and 2 deletions

View file

@ -721,7 +721,7 @@ endif()
if(TARGET_OS STREQUAL "windows")
set(PLATFORM_CLIENT)
set(PLATFORM_CLIENT_LIBS opengl32 winmm)
set(PLATFORM_LIBS version ws2_32) # Windows sockets
set(PLATFORM_LIBS shlwapi version ws2_32) # Windows sockets
elseif(TARGET_OS STREQUAL "mac")
find_library(CARBON Carbon)
find_library(COCOA Cocoa)

View file

@ -68,6 +68,7 @@
#include <process.h>
#include <share.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <wincrypt.h>
#else
#error NOT IMPLEMENTED
@ -2309,6 +2310,17 @@ int fs_is_dir(const char *path)
#endif
}
int fs_is_relative_path(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wPath[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, std::size(wPath));
return PathIsRelativeW(wPath) ? 1 : 0;
#else
return path[0] == '/' ? 0 : 1; // yes, it's that simple
#endif
}
int fs_chdir(const char *path)
{
if(fs_is_dir(path))
@ -3866,8 +3878,18 @@ int open_file(const char *path)
#if defined(CONF_PLATFORM_MACOS)
return open_link(path);
#else
// Create a file link so the path can contain forward and
// backward slashes. But the file link must be absolute.
char buf[512];
str_format(buf, sizeof(buf), "file://%s", path);
char workingDir[IO_MAX_PATH_LENGTH];
if(fs_is_relative_path(path))
{
fs_getcwd(workingDir, sizeof(workingDir));
str_append(workingDir, "/", sizeof(workingDir));
}
else
workingDir[0] = '\0';
str_format(buf, sizeof(buf), "file://%s%s", workingDir, path);
return open_link(buf);
#endif
}

View file

@ -1821,6 +1821,15 @@ int fs_storage_path(const char *appname, char *path, int max);
*/
int fs_is_dir(const char *path);
/*
Function: fs_is_relative_path
Checks whether a given path is relative or absolute.
Returns:
Returns 1 if relative, 0 if absolute.
*/
int fs_is_relative_path(const char *path);
/*
Function: fs_chdir
Changes current working directory