]> de.git.xonotic.org Git - xonotic/netradiant.git/commitdiff
error check and bail if permission denied during gamepack install
authorTimothee 'TTimo' Besset <ttimo@ttimo.net>
Sun, 6 May 2012 21:59:07 +0000 (16:59 -0500)
committerTimothee 'TTimo' Besset <ttimo@ttimo.net>
Sun, 6 May 2012 21:59:07 +0000 (16:59 -0500)
libs/missing.h
radiant/missing.cpp
radiant/preferences.cpp

index 2ba03d1404f8871818c733fef928bde63701722b..9a1b121f9724a886a116ecb6100a43f8863ba3f3 100644 (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 {
        PATH_FAIL,      // stat call failed (does not exist is likely)
 
 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 );
 
 // 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_
 
 #endif // _MISSING_H_
index 6c7e4a7f5734f7dd8c03c86f3590dec985459e54..a60b2944ce03b1a171521ca6a9f02c79dff93be5 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "missing.h"
 #include "qsysprintf.h"
 
 #include "missing.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 <dirent.h>
 
 #include <stdlib.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;
        void* buf;
        int l;
        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 ) {
 
        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 ) {
        }
        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 );
        }
 
        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 );
 
        g_free( buf );
        fclose( src );
@@ -87,12 +102,15 @@ bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){
        return ret;
 }
 
        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 ){
 }
 
 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 ) {
-       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
 
 }
 
 #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;
        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() ) ) {
                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 ) {
                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;
                                        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: {
                        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;
                        }
                        if ( !ret ) {
                                return false;
                        }
index b8910ee56878258a02f0327974a78e4e77781066..08ff05c4e549d185a4ca37de97fcff2fa505ff27 100644 (file)
@@ -3411,8 +3411,8 @@ void CGameInstall::Run() {
        // write out the game file
        Str gameFilePath = g_strAppPath.GetBuffer();
        gameFilePath += "games/";
        // 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 ] ) {
        }
 
        switch ( m_availGames[ m_nComboSelect ] ) {
@@ -3469,7 +3469,7 @@ void CGameInstall::Run() {
                source += Q2_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"baseq2\"\n" );
                break;
        }
@@ -3481,7 +3481,7 @@ void CGameInstall::Run() {
                source += Q3_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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) {
                // 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();
                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;
        }
                fprintf( fg, "  basegame=\"q3ut4\"\n" );
                break;
        }
@@ -3511,7 +3511,7 @@ void CGameInstall::Run() {
                source += UFOAI_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"base\"\n" );
                break;
        }
@@ -3523,7 +3523,7 @@ void CGameInstall::Run() {
                source += Q2W_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"default\"\n" );
                break;
        }
@@ -3535,7 +3535,7 @@ void CGameInstall::Run() {
                source += WARSOW_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"basewsw\"\n" );
                break;
        }
@@ -3547,7 +3547,7 @@ void CGameInstall::Run() {
                source += NEXUIZ_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"data\"\n" );
                break;
        }
@@ -3559,7 +3559,7 @@ void CGameInstall::Run() {
                source += TREMULOUS_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"base\"\n" );
                break;
        }
@@ -3571,7 +3571,7 @@ void CGameInstall::Run() {
                source += JA_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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;
        }
                fprintf( fg, "  basegame=\"base\"\n" );
                break;
        }
@@ -3583,7 +3583,7 @@ void CGameInstall::Run() {
                source += REACTION_PACK;
                source += "/install/";
                Str dest = m_strEngine.GetBuffer();
                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
                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();
                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) {
                // Hardcoded fix for "missing" shaderlist in gamepack
                dest += "/etmain/scripts/shaderlist.txt";
                if(CheckFile(dest.GetBuffer()) != PATH_FILE) {