From f13c28528706aee2b8a86ef58f8d32b3d934d502 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 26 May 2023 22:29:34 +0300 Subject: [PATCH] filesystem: verbose error printing in FS_SetCurrentDirectory, move error reporting from engine --- engine/common/host.c | 7 +++---- filesystem/filesystem.c | 25 ++++++++++++++++++++++--- filesystem/tests/no-init.c | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/engine/common/host.c b/engine/common/host.c index 5650d950..53b295b0 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -1077,10 +1077,9 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha FS_LoadProgs(); - if( FS_SetCurrentDirectory( host.rootdir ) != 0 ) - Con_Reportf( "%s is working directory now\n", host.rootdir ); - else - Sys_Error( "Changing working directory to %s failed.\n", host.rootdir ); + // TODO: this function will cause engine to stop in case of fail + // when it will have an option to return string error, restore Sys_Error + FS_SetCurrentDirectory( host.rootdir ); FS_Init(); diff --git a/filesystem/filesystem.c b/filesystem/filesystem.c index 990e5ba7..e6bfa254 100644 --- a/filesystem/filesystem.c +++ b/filesystem/filesystem.c @@ -1703,17 +1703,36 @@ qboolean FS_SysFileOrFolderExists( const char *path ) FS_SetCurrentDirectory Sets current directory, path should be in UTF-8 encoding +TODO: make this non-fatal ================== */ int FS_SetCurrentDirectory( const char *path ) { #if XASH_WIN32 - return SetCurrentDirectoryW( FS_PathToWideChar( path )); + if( !SetCurrentDirectoryW( FS_PathToWideChar( path ))) + { + char buf[1024]; + FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, GetLastError(), MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ), + buf, sizeof( buf ), NULL ); + + Sys_Error( "Changing directory to %s failed: %s\n", path, buf ) + return false; + } #elif XASH_POSIX - return !chdir( path ); + if( chdir( path ) < 0 ) + { + Sys_Error( "Changing directory to %s failed: %s\n", path, strerror( errno )); + return false; + } #else -#error + // it may be fine for some systems to skip chdir + Con_Printf( "FS_SetCurrentDirectory: not implemented, ignoring...\n" ); + return true; #endif + + Con_Printf( "%s is working directory now\n", path ); + return true; } /* diff --git a/filesystem/tests/no-init.c b/filesystem/tests/no-init.c index 568f735e..3ea2935e 100644 --- a/filesystem/tests/no-init.c +++ b/filesystem/tests/no-init.c @@ -48,7 +48,7 @@ static int TestNoInit( void ) g_fs.AllowDirectPaths( false ); if( g_fs.Search( "asfjkajk", 0, 0 ) != 0 ) return 0; - g_fs.SetCurrentDirectory( "asdasdasdasd" ); + g_fs.SetCurrentDirectory( "." ); // must succeed! g_fs.FindLibrary( "kek", true, &dllinfo ); if( g_fs.Open( "afbvasvwerf", "w+", true ) != 0 ) return 0;