error check and bail if permission denied during gamepack install

This commit is contained in:
Timothee 'TTimo' Besset 2012-05-06 16:59:07 -05:00
parent 6476802ae8
commit 9b5ba1fe10
3 changed files with 73 additions and 36 deletions

View File

@ -209,7 +209,7 @@ DIR * findHandle;
#endif #endif
}; };
bool CopyTree( const char* source, const char* dest ); bool radCopyTree( const char* source, const char* dest, bool fatal_on_error = true );
typedef enum { typedef enum {
PATH_FAIL, // stat call failed (does not exist is likely) PATH_FAIL, // stat call failed (does not exist is likely)
@ -220,7 +220,7 @@ typedef enum {
// check a path for existence, return directory / file // check a path for existence, return directory / file
EPathCheck CheckFile( const char *path ); EPathCheck CheckFile( const char *path );
bool radCreateDirectory( const char *directory ); bool radCreateDirectory( const char *directory, bool fatal_on_error = true );
bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ); bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error = true );
#endif // _MISSING_H_ #endif // _MISSING_H_

View File

@ -36,6 +36,7 @@
#include "missing.h" #include "missing.h"
#include "qsysprintf.h" #include "qsysprintf.h"
#include "qe3.h"
#if defined ( __linux__ ) || defined ( __APPLE__ ) #if defined ( __linux__ ) || defined ( __APPLE__ )
@ -47,7 +48,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h> #include <dirent.h>
bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error ){
FILE *src, *dst; FILE *src, *dst;
void* buf; void* buf;
int l; int l;
@ -59,12 +60,20 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){
src = fopen( realsrc, "rb" ); src = fopen( realsrc, "rb" );
if ( !src ) { 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" ); dst = fopen( realdest, "wb" );
if ( !dst ) { if ( !dst ) {
fclose( src ); if ( fatal_on_error ) {
return false; 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 ); 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 ); g_free( buf );
fclose( src ); fclose( src );
@ -87,12 +102,15 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){
return ret; return ret;
} }
bool radCreateDirectory( const char *directory ) { bool radCreateDirectory( const char *directory, bool fatal_on_error ) {
if ( mkdir( directory, 0777 ) == -1 ) { if ( mkdir( directory, 0777 ) >= 0 ) {
Sys_Printf( "mkdir %s failed\n", directory ); return true;
return false; }
} if ( fatal_on_error ) {
return true; 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 ){ int GetFullPathName( const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart ){
@ -219,17 +237,31 @@ EPathCheck CheckFile( const char *path ) {
return PATH_FILE; return PATH_FILE;
} }
bool radCreateDirectory( const char *directory ) { bool radCreateDirectory( const char *directory, bool fatal_on_error ) {
return ( CreateDirectory( directory, NULL ) != false ); 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 ) { bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error ) {
return ( CopyFile( lpExistingFileName, lpNewFileName, FALSE ) != false ); 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 #endif
bool CopyTree( const char *source, const char *dest ) { bool radCopyTree( const char *source, const char *dest, bool fatal_on_error ) {
Str srcEntry; Str srcEntry;
Str dstEntry; Str dstEntry;
const char *dirname; const char *dirname;
@ -249,23 +281,28 @@ bool CopyTree( const char *source, const char *dest ) {
dstEntry += "/"; dstEntry += "/";
dstEntry += dirname; dstEntry += dirname;
switch ( CheckFile( srcEntry.GetBuffer() ) ) { switch ( CheckFile( srcEntry.GetBuffer() ) ) {
case PATH_FAIL: {
if ( fatal_on_error ) {
Error( "%s does not exist. check your permissions", srcEntry.GetBuffer() );
}
}
case PATH_DIRECTORY: { case PATH_DIRECTORY: {
if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) { if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) {
if ( !radCreateDirectory( dstEntry.GetBuffer() ) ) { Sys_Printf( "Create directory %s\n", dstEntry.GetBuffer() );
Sys_Printf( "create directory %s failed\n", dstEntry.GetBuffer() ); if ( !radCreateDirectory( dstEntry.GetBuffer(), fatal_on_error ) ) {
return false; return false;
} }
} }
bool ret; bool ret;
ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() ); ret = radCopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error );
if ( !ret ) { if ( !ret ) {
return false; return false;
} }
break; break;
} }
case PATH_FILE: { case PATH_FILE: {
Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() ); Sys_Printf( "Copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() ); bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error );
if ( !ret ) { if ( !ret ) {
return false; return false;
} }

View File

@ -3411,8 +3411,8 @@ void CGameInstall::Run() {
// write out the game file // write out the game file
Str gameFilePath = g_strAppPath.GetBuffer(); Str gameFilePath = g_strAppPath.GetBuffer();
gameFilePath += "games/"; gameFilePath += "games/";
if(CheckFile(gameFilePath) != PATH_DIRECTORY) { if ( CheckFile( gameFilePath ) != PATH_DIRECTORY ) {
radCreateDirectory(gameFilePath); radCreateDirectory( gameFilePath );
} }
switch ( m_availGames[ m_nComboSelect ] ) { switch ( m_availGames[ m_nComboSelect ] ) {
@ -3469,7 +3469,7 @@ void CGameInstall::Run() {
source += Q2_PACK; source += Q2_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"baseq2\"\n" ); fprintf( fg, " basegame=\"baseq2\"\n" );
break; break;
} }
@ -3481,7 +3481,7 @@ void CGameInstall::Run() {
source += Q3_PACK; source += Q3_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
// Hardcoded fix for "missing" shaderlist in gamepack // Hardcoded fix for "missing" shaderlist in gamepack
dest += "/baseq3/scripts/shaderlist.txt"; dest += "/baseq3/scripts/shaderlist.txt";
if(CheckFile(dest.GetBuffer()) != PATH_FILE) { if(CheckFile(dest.GetBuffer()) != PATH_FILE) {
@ -3499,7 +3499,7 @@ void CGameInstall::Run() {
source += URT_PACK; source += URT_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"q3ut4\"\n" ); fprintf( fg, " basegame=\"q3ut4\"\n" );
break; break;
} }
@ -3511,7 +3511,7 @@ void CGameInstall::Run() {
source += UFOAI_PACK; source += UFOAI_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"base\"\n" ); fprintf( fg, " basegame=\"base\"\n" );
break; break;
} }
@ -3523,7 +3523,7 @@ void CGameInstall::Run() {
source += Q2W_PACK; source += Q2W_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"default\"\n" ); fprintf( fg, " basegame=\"default\"\n" );
break; break;
} }
@ -3535,7 +3535,7 @@ void CGameInstall::Run() {
source += WARSOW_PACK; source += WARSOW_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"basewsw\"\n" ); fprintf( fg, " basegame=\"basewsw\"\n" );
break; break;
} }
@ -3547,7 +3547,7 @@ void CGameInstall::Run() {
source += NEXUIZ_PACK; source += NEXUIZ_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"data\"\n" ); fprintf( fg, " basegame=\"data\"\n" );
break; break;
} }
@ -3559,7 +3559,7 @@ void CGameInstall::Run() {
source += TREMULOUS_PACK; source += TREMULOUS_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"base\"\n" ); fprintf( fg, " basegame=\"base\"\n" );
break; break;
} }
@ -3571,7 +3571,7 @@ void CGameInstall::Run() {
source += JA_PACK; source += JA_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"base\"\n" ); fprintf( fg, " basegame=\"base\"\n" );
break; break;
} }
@ -3583,7 +3583,7 @@ void CGameInstall::Run() {
source += REACTION_PACK; source += REACTION_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
fprintf( fg, " basegame=\"Boomstick\"\n" ); fprintf( fg, " basegame=\"Boomstick\"\n" );
fprintf( fg, " default_scale=\"0.5\"\n" ); // Superfluous because the default is already 0.5, fprintf( fg, " default_scale=\"0.5\"\n" ); // Superfluous because the default is already 0.5,
// but demonstrates how to set the default texture scale // but demonstrates how to set the default texture scale
@ -3603,7 +3603,7 @@ void CGameInstall::Run() {
source += ET_PACK; source += ET_PACK;
source += "/install/"; source += "/install/";
Str dest = m_strEngine.GetBuffer(); Str dest = m_strEngine.GetBuffer();
CopyTree( source.GetBuffer(), dest.GetBuffer() ); radCopyTree( source.GetBuffer(), dest.GetBuffer() );
// Hardcoded fix for "missing" shaderlist in gamepack // Hardcoded fix for "missing" shaderlist in gamepack
dest += "/etmain/scripts/shaderlist.txt"; dest += "/etmain/scripts/shaderlist.txt";
if(CheckFile(dest.GetBuffer()) != PATH_FILE) { if(CheckFile(dest.GetBuffer()) != PATH_FILE) {