2023-09-20 09:36:01 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2023-09-20 09:36:01 +02:00
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
#undef min
|
|
|
|
#undef max
|
2023-09-20 09:36:01 +02:00
|
|
|
#undef wcsncpy
|
2023-11-07 20:56:28 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <cwchar>
|
|
|
|
#include "json/json.h"
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2023-10-02 23:13:55 +02:00
|
|
|
#include "unix_private.h"
|
2023-11-07 20:56:28 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2023-10-02 23:13:55 +02:00
|
|
|
#include <linux/limits.h>
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(vrclient);
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2023-10-02 23:13:55 +02:00
|
|
|
#define IS_ABSOLUTE( x ) (*x == '/' || *x == '\\' || (*x && *(x + 1) == ':'))
|
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
static char *get_unix_file_name( const WCHAR *path )
|
|
|
|
{
|
|
|
|
UNICODE_STRING nt_name;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
ULONG size = 256;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
nt_name.Buffer = (WCHAR *)path;
|
|
|
|
nt_name.MaximumLength = nt_name.Length = lstrlenW( path ) * sizeof(WCHAR);
|
|
|
|
InitializeObjectAttributes( &attr, &nt_name, 0, 0, NULL );
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!(buffer = (char *)malloc( size ))) return NULL;
|
|
|
|
status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN_IF );
|
|
|
|
if (status != STATUS_BUFFER_TOO_SMALL) break;
|
|
|
|
free( buffer );
|
|
|
|
}
|
|
|
|
if (status && status != STATUS_NO_SUCH_FILE)
|
|
|
|
{
|
|
|
|
free( buffer );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2023-10-02 23:13:55 +02:00
|
|
|
char *vrclient_dos_to_unix_path( const char *src )
|
|
|
|
{
|
|
|
|
char buffer[4096], *dst = buffer;
|
|
|
|
uint32_t len;
|
|
|
|
|
2023-10-05 14:06:38 +02:00
|
|
|
TRACE( "src %s\n", debugstr_a(src) );
|
|
|
|
|
2023-10-02 23:13:55 +02:00
|
|
|
if (!src) return NULL;
|
|
|
|
|
|
|
|
*dst = 0;
|
|
|
|
if (!*src) goto done;
|
|
|
|
|
|
|
|
if (IS_ABSOLUTE( src ))
|
|
|
|
{
|
|
|
|
/* absolute path, use wine conversion */
|
2023-11-07 20:56:28 +01:00
|
|
|
WCHAR srcW[PATH_MAX] = {'\\', '?', '?', '\\', 0}, *tmp;
|
2023-10-02 23:13:55 +02:00
|
|
|
char *unix_path;
|
|
|
|
uint32_t r;
|
|
|
|
|
2024-06-27 03:24:41 +02:00
|
|
|
r = ntdll_umbstowcs( src, strlen( src ) + 1, srcW + 4, PATH_MAX - 4 );
|
2023-11-07 20:56:28 +01:00
|
|
|
if (r == 0) unix_path = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (tmp = srcW; *tmp; ++tmp) if (*tmp == '/') *tmp = '\\';
|
|
|
|
unix_path = get_unix_file_name( srcW );
|
|
|
|
}
|
2023-10-02 23:13:55 +02:00
|
|
|
|
|
|
|
if (!unix_path)
|
|
|
|
{
|
|
|
|
WARN( "Unable to convert DOS filename to unix: %s\n", src );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!realpath( unix_path, dst ))
|
|
|
|
{
|
|
|
|
ERR( "Could not get real path for %s.\n", unix_path );
|
|
|
|
lstrcpynA( dst, unix_path, PATH_MAX );
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
free( unix_path );
|
2023-10-02 23:13:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* relative path, just fix up backslashes */
|
|
|
|
const char *s;
|
|
|
|
char *d;
|
|
|
|
|
|
|
|
for (s = src, d = dst; *src; ++s, ++d)
|
|
|
|
{
|
|
|
|
if (*s == '\\') *d = '/';
|
|
|
|
else *d = *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
len = strlen( buffer ) + 1;
|
2023-11-07 20:56:28 +01:00
|
|
|
if (!(dst = (char *)malloc( len ))) return NULL;
|
2023-10-02 23:13:55 +02:00
|
|
|
memcpy( dst, buffer, len );
|
|
|
|
|
2023-10-05 14:06:38 +02:00
|
|
|
TRACE( "-> %s\n", debugstr_a(dst) );
|
2023-10-02 23:13:55 +02:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vrclient_free_path( char *path )
|
|
|
|
{
|
2023-11-07 20:56:28 +01:00
|
|
|
free( path );
|
2023-10-02 23:13:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-02 23:53:50 +02:00
|
|
|
/* returns the number of bytes written to dst, not including the NUL terminator */
|
|
|
|
unsigned int vrclient_unix_path_to_dos_path( bool api_result, const char *src, char *dst, uint32_t dst_bytes )
|
|
|
|
{
|
2023-11-07 20:56:28 +01:00
|
|
|
NTSTATUS status;
|
|
|
|
uint32_t r = 0;
|
|
|
|
ULONG size = 0;
|
2023-10-02 23:53:50 +02:00
|
|
|
WCHAR *dosW;
|
|
|
|
|
2023-10-05 14:06:38 +02:00
|
|
|
TRACE( "api_result %u, src %s, dst %p, dst_bytes %u\n", api_result, debugstr_a(src), dst, dst_bytes );
|
|
|
|
|
2023-10-02 23:53:50 +02:00
|
|
|
if (!src || !*src || !api_result || !dst || !dst_bytes)
|
|
|
|
{
|
|
|
|
if (dst && dst_bytes) *dst = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
status = wine_unix_to_nt_file_name( src, NULL, &size );
|
|
|
|
if (status != STATUS_BUFFER_TOO_SMALL)
|
2023-10-02 23:53:50 +02:00
|
|
|
{
|
2023-11-07 20:56:28 +01:00
|
|
|
WARN( "Unable to convert unix filename to DOS: %s, status %#x.\n", debugstr_a(src), status );
|
2023-10-02 23:53:50 +02:00
|
|
|
*dst = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
dosW = (WCHAR *)malloc( size * sizeof(WCHAR) );
|
|
|
|
status = wine_unix_to_nt_file_name( src, dosW, &size );
|
|
|
|
if (!status) r = ntdll_wcstoumbs( dosW, size, dst, dst_bytes, FALSE );
|
|
|
|
else *dst = 0;
|
|
|
|
free( dosW );
|
|
|
|
|
|
|
|
if (!strncmp( dst, "\\??\\", 4 ))
|
|
|
|
{
|
|
|
|
memmove( dst, dst + 4, r - 4 );
|
|
|
|
r -= 4;
|
|
|
|
}
|
2023-10-02 23:53:50 +02:00
|
|
|
|
2023-10-05 14:06:38 +02:00
|
|
|
TRACE( "-> dst %s, r %u\n", debugstr_a(dst), r );
|
2023-10-02 23:53:50 +02:00
|
|
|
return r == 0 ? 0 : r - 1;
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:50:30 +01:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
static bool ends_with(const std::string &s, char c)
|
|
|
|
{
|
|
|
|
auto e = s.end();
|
|
|
|
if(e != s.begin()){
|
|
|
|
--e;
|
|
|
|
return *e == c;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool convert_path_to_win(std::string &s)
|
|
|
|
{
|
|
|
|
bool need_slash = ends_with(s, '\\') || ends_with(s, '/');
|
2023-11-07 20:56:28 +01:00
|
|
|
WCHAR srcW[PATH_MAX] = {'\\', '?', '?', '\\', 0}, *tmp;
|
|
|
|
char *unix_path;
|
|
|
|
uint32_t sz;
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2024-06-27 03:24:41 +02:00
|
|
|
sz = ntdll_umbstowcs( s.c_str(), strlen( s.c_str() ) + 1, srcW + 4, PATH_MAX - 4 );
|
2023-11-07 20:56:28 +01:00
|
|
|
if (!sz) return false;
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
for (tmp = srcW; *tmp; ++tmp) if (*tmp == '/') *tmp = '\\';
|
|
|
|
if (!(unix_path = get_unix_file_name( srcW ))) return false;
|
2020-03-18 19:50:30 +01:00
|
|
|
|
|
|
|
/* XXX assuming the system encoding is UTF-8 */
|
|
|
|
s = unix_path;
|
2023-11-07 20:56:28 +01:00
|
|
|
free( unix_path );
|
2020-03-18 19:50:30 +01:00
|
|
|
|
2023-11-07 20:56:28 +01:00
|
|
|
if (need_slash) s += '/';
|
2020-03-18 19:50:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *json_convert_paths(const char *input)
|
|
|
|
{
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::Value root;
|
|
|
|
|
|
|
|
if(!reader.parse(input, root))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(root.isMember("type") &&
|
|
|
|
root["type"].isString() &&
|
|
|
|
root["type"] == "set_paths")
|
|
|
|
{
|
|
|
|
if(root.isMember("webroot") &&
|
|
|
|
root["webroot"].isString())
|
|
|
|
{
|
|
|
|
std::string path(root["webroot"].asString());
|
|
|
|
if(convert_path_to_win(path))
|
|
|
|
root["webroot"] = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(root.isMember("models") &&
|
|
|
|
root["models"].isString())
|
|
|
|
{
|
|
|
|
std::string path(root["models"].asString());
|
|
|
|
if(convert_path_to_win(path))
|
|
|
|
root["models"] = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::FastWriter writer;
|
|
|
|
|
|
|
|
std::string contents(writer.write(root));
|
|
|
|
|
|
|
|
char *ret = (char *)malloc(contents.length() + 1);
|
|
|
|
if(!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size_t len = contents.copy(ret, contents.length());
|
|
|
|
ret[len] = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-04 02:09:03 +01:00
|
|
|
char *json_convert_startup_info(const char *startup_info)
|
|
|
|
{
|
|
|
|
std::string src_path;
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::Value root;
|
2023-09-24 15:30:59 +02:00
|
|
|
char *dst_path;
|
2021-02-04 02:09:03 +01:00
|
|
|
size_t len;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if(!startup_info || !reader.parse(startup_info, root))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!root.isMember("action_manifest_path") || !root["action_manifest_path"].isString())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
src_path = root["action_manifest_path"].asString();
|
|
|
|
WINE_TRACE("action_manifest_path %s.\n", src_path.c_str());
|
|
|
|
|
2023-09-24 15:30:59 +02:00
|
|
|
if (!(dst_path = vrclient_dos_to_unix_path(src_path.c_str())))
|
2021-02-04 02:09:03 +01:00
|
|
|
{
|
|
|
|
WINE_ERR("error converting path %s.\n", src_path.c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
WINE_TRACE("converted path %s.\n", dst_path);
|
|
|
|
|
|
|
|
root["action_manifest_path"] = dst_path;
|
|
|
|
|
|
|
|
Json::FastWriter writer;
|
|
|
|
|
|
|
|
std::string contents(writer.write(root));
|
|
|
|
ret = (char *)malloc(contents.length() + 1);
|
|
|
|
len = contents.copy(ret, contents.length());
|
|
|
|
ret[len] = 0;
|
|
|
|
|
2023-09-24 15:30:59 +02:00
|
|
|
vrclient_free_path(dst_path);
|
|
|
|
|
2021-02-04 02:09:03 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:50:30 +01:00
|
|
|
}
|