From: Timothee 'TTimo' Besset Date: Sun, 6 May 2012 21:59:07 +0000 (-0500) Subject: error check and bail if permission denied during gamepack install X-Git-Tag: xonotic-v0.7.0~16^2^2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=9b5ba1fe10d7eba4db1588c2a4926829c465dc76;p=xonotic%2Fnetradiant.git error check and bail if permission denied during gamepack install --- diff --git a/libs/missing.h b/libs/missing.h index 2ba03d14..9a1b121f 100644 --- a/libs/missing.h +++ b/libs/missing.h @@ -209,7 +209,7 @@ DIR * findHandle; #endif }; -bool CopyTree( const char* source, const char* dest ); +bool radCopyTree( const char* source, const char* dest, bool fatal_on_error = true ); typedef enum { PATH_FAIL, // stat call failed (does not exist is likely) @@ -220,7 +220,7 @@ typedef enum { // check a path for existence, return directory / file EPathCheck CheckFile( const char *path ); -bool radCreateDirectory( const char *directory ); -bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ); +bool radCreateDirectory( const char *directory, bool fatal_on_error = true ); +bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error = true ); #endif // _MISSING_H_ diff --git a/radiant/missing.cpp b/radiant/missing.cpp index 6c7e4a7f..a60b2944 100644 --- a/radiant/missing.cpp +++ b/radiant/missing.cpp @@ -36,6 +36,7 @@ #include "missing.h" #include "qsysprintf.h" +#include "qe3.h" #if defined ( __linux__ ) || defined ( __APPLE__ ) @@ -47,7 +48,7 @@ #include #include -bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){ +bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error ){ FILE *src, *dst; void* buf; int l; @@ -59,12 +60,20 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){ src = fopen( realsrc, "rb" ); if ( !src ) { - return false; + if ( fatal_on_error ) { + Error( "Failed to open source for copy: %s\n", realsrc ); + } + Sys_Printf( "Failed to open source for copy: %s\n", realsrc ); + return false; } dst = fopen( realdest, "wb" ); if ( !dst ) { - fclose( src ); - return false; + if ( fatal_on_error ) { + Error( "Failed to open destination for copy: %s\n", realdest ); + } + Sys_Printf( "Failed to open destination for copy: %s\n", realdest ); + fclose( src ); + return false; } fseek( src, 0, SEEK_END ); @@ -79,6 +88,12 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){ } } } + if ( !ret ) { + if ( fatal_on_error ) { + Error( "short read or short write while copying %s to %s\n", realsrc, realdest ); + } + Sys_Printf( "short read or short write while copying %s to %s\n", realsrc, realdest ); + } g_free( buf ); fclose( src ); @@ -87,12 +102,15 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){ return ret; } -bool radCreateDirectory( const char *directory ) { - if ( mkdir( directory, 0777 ) == -1 ) { - Sys_Printf( "mkdir %s failed\n", directory ); - return false; - } - return true; +bool radCreateDirectory( const char *directory, bool fatal_on_error ) { + if ( mkdir( directory, 0777 ) >= 0 ) { + return true; + } + if ( fatal_on_error ) { + Error( "mkdir %s failed - check your permissions", directory ); + } + Sys_Printf( "mkdir %s failed - check your permissions\n", directory ); + return false; } int GetFullPathName( const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart ){ @@ -219,17 +237,31 @@ EPathCheck CheckFile( const char *path ) { return PATH_FILE; } -bool radCreateDirectory( const char *directory ) { - return ( CreateDirectory( directory, NULL ) != false ); +bool radCreateDirectory( const char *directory, bool fatal_on_error ) { + if ( CreateDirectory( directory, NULL ) ) { + return true; + } + if ( fatal_on_error ) { + Error( "mkdir %s failed - check your permissions", directory ); + } + Sys_Printf( "mkdir %s failed - check your permissions\n", directory ); + return false; } -bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ) { - return ( CopyFile( lpExistingFileName, lpNewFileName, FALSE ) != false ); +bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error ) { + if ( CopyFile( lpExistingFileName, lpNewFileName, FALSE ) ) { + return true; + } + if ( fatal_on_error ) { + Error( "copy %s %s failed - check your permissions", lpExistingFileName, lpNewFileName ); + } + Sys_Printf( "copy %s %s failed - check your permissions\n", lpExistingFileName, lpNewFileName ); + return false; } #endif -bool CopyTree( const char *source, const char *dest ) { +bool radCopyTree( const char *source, const char *dest, bool fatal_on_error ) { Str srcEntry; Str dstEntry; const char *dirname; @@ -249,23 +281,28 @@ bool CopyTree( const char *source, const char *dest ) { dstEntry += "/"; dstEntry += dirname; switch ( CheckFile( srcEntry.GetBuffer() ) ) { + case PATH_FAIL: { + if ( fatal_on_error ) { + Error( "%s does not exist. check your permissions", srcEntry.GetBuffer() ); + } + } case PATH_DIRECTORY: { if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) { - if ( !radCreateDirectory( dstEntry.GetBuffer() ) ) { - Sys_Printf( "create directory %s failed\n", dstEntry.GetBuffer() ); + Sys_Printf( "Create directory %s\n", dstEntry.GetBuffer() ); + if ( !radCreateDirectory( dstEntry.GetBuffer(), fatal_on_error ) ) { return false; } } bool ret; - ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() ); + ret = radCopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error ); if ( !ret ) { return false; } break; } case PATH_FILE: { - Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() ); - bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() ); + Sys_Printf( "Copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() ); + bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error ); if ( !ret ) { return false; } diff --git a/radiant/preferences.cpp b/radiant/preferences.cpp index b8910ee5..08ff05c4 100644 --- a/radiant/preferences.cpp +++ b/radiant/preferences.cpp @@ -3411,8 +3411,8 @@ void CGameInstall::Run() { // write out the game file Str gameFilePath = g_strAppPath.GetBuffer(); gameFilePath += "games/"; - if(CheckFile(gameFilePath) != PATH_DIRECTORY) { - radCreateDirectory(gameFilePath); + if ( CheckFile( gameFilePath ) != PATH_DIRECTORY ) { + radCreateDirectory( gameFilePath ); } switch ( m_availGames[ m_nComboSelect ] ) { @@ -3469,7 +3469,7 @@ void CGameInstall::Run() { source += Q2_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"baseq2\"\n" ); break; } @@ -3481,7 +3481,7 @@ void CGameInstall::Run() { source += Q3_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); // Hardcoded fix for "missing" shaderlist in gamepack dest += "/baseq3/scripts/shaderlist.txt"; if(CheckFile(dest.GetBuffer()) != PATH_FILE) { @@ -3499,7 +3499,7 @@ void CGameInstall::Run() { source += URT_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"q3ut4\"\n" ); break; } @@ -3511,7 +3511,7 @@ void CGameInstall::Run() { source += UFOAI_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"base\"\n" ); break; } @@ -3523,7 +3523,7 @@ void CGameInstall::Run() { source += Q2W_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"default\"\n" ); break; } @@ -3535,7 +3535,7 @@ void CGameInstall::Run() { source += WARSOW_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"basewsw\"\n" ); break; } @@ -3547,7 +3547,7 @@ void CGameInstall::Run() { source += NEXUIZ_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"data\"\n" ); break; } @@ -3559,7 +3559,7 @@ void CGameInstall::Run() { source += TREMULOUS_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"base\"\n" ); break; } @@ -3571,7 +3571,7 @@ void CGameInstall::Run() { source += JA_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"base\"\n" ); break; } @@ -3583,7 +3583,7 @@ void CGameInstall::Run() { source += REACTION_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); fprintf( fg, " basegame=\"Boomstick\"\n" ); fprintf( fg, " default_scale=\"0.5\"\n" ); // Superfluous because the default is already 0.5, // but demonstrates how to set the default texture scale @@ -3603,7 +3603,7 @@ void CGameInstall::Run() { source += ET_PACK; source += "/install/"; Str dest = m_strEngine.GetBuffer(); - CopyTree( source.GetBuffer(), dest.GetBuffer() ); + radCopyTree( source.GetBuffer(), dest.GetBuffer() ); // Hardcoded fix for "missing" shaderlist in gamepack dest += "/etmain/scripts/shaderlist.txt"; if(CheckFile(dest.GetBuffer()) != PATH_FILE) {