2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (c) 2001, Loki software, inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
Neither the name of Loki software nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without specific prior
|
|
|
|
written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Missing functions
|
|
|
|
//
|
|
|
|
// Leonardo Zide (leo@lokigames.com)
|
|
|
|
//
|
|
|
|
|
2008-03-18 17:47:38 +00:00
|
|
|
#include "missing.h"
|
|
|
|
#include "qsysprintf.h"
|
2012-05-06 21:59:07 +00:00
|
|
|
#include "qe3.h"
|
2008-03-18 17:47:38 +00:00
|
|
|
|
2023-04-17 16:13:50 +00:00
|
|
|
#if defined( __linux__ ) || defined( __BSD__ ) || defined( __APPLE__ )
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
2008-02-25 04:17:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-11-04 03:51:54 +00:00
|
|
|
#include <stdlib.h>
|
2008-02-25 04:17:23 +00:00
|
|
|
#include <dirent.h>
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-05-06 21:59:07 +00:00
|
|
|
bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName, bool fatal_on_error ){
|
2012-03-17 20:01:54 +00:00
|
|
|
FILE *src, *dst;
|
|
|
|
void* buf;
|
|
|
|
int l;
|
|
|
|
bool ret = false;
|
|
|
|
char realsrc[PATH_MAX], realdest[PATH_MAX];
|
2023-04-24 04:08:28 +00:00
|
|
|
memset( realsrc, 0, sizeof(realsrc) );
|
|
|
|
memset( realdest, 0, sizeof(realdest) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2023-04-24 04:08:28 +00:00
|
|
|
if ( realpath( lpExistingFileName, realsrc ) == nullptr )
|
|
|
|
strncpy( realsrc, lpExistingFileName, sizeof(realsrc) - 1 );
|
|
|
|
if ( realpath( lpNewFileName, realdest ) == nullptr )
|
|
|
|
strncpy( realdest, lpNewFileName, sizeof(realdest) - 1 );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
src = fopen( realsrc, "rb" );
|
|
|
|
if ( !src ) {
|
2023-04-17 16:13:50 +00:00
|
|
|
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;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
dst = fopen( realdest, "wb" );
|
|
|
|
if ( !dst ) {
|
2023-04-17 16:13:50 +00:00
|
|
|
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;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek( src, 0, SEEK_END );
|
|
|
|
l = ftell( src );
|
|
|
|
rewind( src );
|
|
|
|
buf = g_malloc( l );
|
|
|
|
|
|
|
|
if ( buf != NULL ) {
|
|
|
|
if ( fread( buf, l, 1, src ) == 1 ) {
|
|
|
|
if ( fwrite( buf, l, 1, dst ) == 1 ) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-06 21:59:07 +00:00
|
|
|
if ( !ret ) {
|
2023-04-17 16:13:50 +00:00
|
|
|
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 );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
g_free( buf );
|
|
|
|
fclose( src );
|
|
|
|
fclose( dst );
|
|
|
|
|
|
|
|
return ret;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 21:59:07 +00:00
|
|
|
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;
|
2008-02-25 04:17:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int GetFullPathName( const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart ){
|
|
|
|
if ( lpFileName[0] == '/' ) {
|
|
|
|
strcpy( lpBuffer, lpFileName );
|
|
|
|
*lpFilePart = strrchr( lpBuffer, '/' );
|
|
|
|
return strlen( lpBuffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( getcwd( lpBuffer, nBufferLength ) == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat( lpBuffer, "/" );
|
|
|
|
*lpFilePart = lpBuffer + strlen( lpBuffer );
|
|
|
|
strcat( lpBuffer, lpFileName );
|
|
|
|
|
|
|
|
char *scr = lpBuffer, *dst = lpBuffer;
|
|
|
|
for ( int i = 0; ( i < nBufferLength ) && ( *scr != 0 ); i++ )
|
|
|
|
{
|
|
|
|
if ( *scr == '/' && *( scr + 1 ) == '.' && *( scr + 2 ) == '.' ) {
|
|
|
|
scr += 4;
|
|
|
|
while ( dst != lpBuffer && *dst != '/' )
|
|
|
|
{
|
|
|
|
dst--;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = *scr;
|
|
|
|
|
|
|
|
scr++; dst++;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
|
|
|
|
return strlen( lpBuffer );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2008-03-18 17:47:38 +00:00
|
|
|
EPathCheck CheckFile( const char *path ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
struct stat sbuf;
|
2008-03-18 17:47:38 +00:00
|
|
|
if ( stat( path, &sbuf ) == -1 ) {
|
|
|
|
return PATH_FAIL;
|
|
|
|
}
|
|
|
|
if ( S_ISDIR( sbuf.st_mode ) ) {
|
|
|
|
return PATH_DIRECTORY;
|
|
|
|
}
|
|
|
|
return PATH_FILE;
|
|
|
|
}
|
|
|
|
|
2008-03-18 19:31:49 +00:00
|
|
|
FindFiles::FindFiles( const char *_directory ) {
|
|
|
|
findHandle = opendir( _directory );
|
|
|
|
}
|
|
|
|
|
|
|
|
FindFiles::~FindFiles() {
|
|
|
|
if ( findHandle != NULL ) {
|
|
|
|
closedir( findHandle );
|
2008-06-26 15:19:48 +00:00
|
|
|
findHandle = NULL;
|
2008-03-18 19:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* FindFiles::NextFile() {
|
|
|
|
struct dirent *d;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( findHandle == NULL ) {
|
2008-03-18 19:31:49 +00:00
|
|
|
return NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-03-18 19:31:49 +00:00
|
|
|
|
|
|
|
d = readdir( findHandle );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( d ) {
|
2008-03-18 19:31:49 +00:00
|
|
|
return d->d_name;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-03-18 19:31:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-18 17:47:38 +00:00
|
|
|
#else
|
2012-07-07 17:35:16 +00:00
|
|
|
// WIN32
|
2008-03-18 17:47:38 +00:00
|
|
|
|
|
|
|
FindFiles::FindFiles( const char *_directory ) {
|
2010-12-13 04:50:40 +00:00
|
|
|
char endChar;
|
2008-03-18 17:47:38 +00:00
|
|
|
directory = _directory;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( directory.GetLength() > 0 ) {
|
|
|
|
endChar = directory.GetAt( directory.GetLength() - 1 );
|
|
|
|
if ( !( endChar == '/' || endChar == '\\' ) ) {
|
2010-12-13 04:50:40 +00:00
|
|
|
// We're only using '/' as the path separator throughout this code, not '\'.
|
|
|
|
// However, I'd hate to see the code silently fail due to a trailing '\', so
|
|
|
|
// I added the check for it.
|
|
|
|
directory += '/';
|
|
|
|
}
|
|
|
|
}
|
2008-04-07 02:43:47 +00:00
|
|
|
directory += '*';
|
2008-03-18 17:47:38 +00:00
|
|
|
findHandle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FindFiles::~FindFiles() {
|
2008-06-26 15:19:48 +00:00
|
|
|
if ( findHandle != INVALID_HANDLE_VALUE ) {
|
2008-03-18 17:47:38 +00:00
|
|
|
FindClose( findHandle );
|
2008-06-26 15:19:48 +00:00
|
|
|
findHandle = INVALID_HANDLE_VALUE;
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* FindFiles::NextFile() {
|
|
|
|
if ( findHandle == INVALID_HANDLE_VALUE ) {
|
|
|
|
findHandle = FindFirstFile( directory.GetBuffer(), &findFileData );
|
|
|
|
if ( findHandle == INVALID_HANDLE_VALUE ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return findFileData.cFileName;
|
|
|
|
}
|
|
|
|
if ( FindNextFile( findHandle, &findFileData ) == 0 ) {
|
|
|
|
FindClose( findHandle );
|
2008-06-26 15:19:48 +00:00
|
|
|
findHandle = INVALID_HANDLE_VALUE;
|
2008-03-18 17:47:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return findFileData.cFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
EPathCheck CheckFile( const char *path ) {
|
2012-07-15 15:45:23 +00:00
|
|
|
DWORD Attrib = GetFileAttributes( path );
|
|
|
|
if ( Attrib == INVALID_FILE_ATTRIBUTES ) {
|
|
|
|
return PATH_FAIL;
|
|
|
|
}
|
|
|
|
if ( Attrib & FILE_ATTRIBUTE_DIRECTORY ) {
|
|
|
|
return PATH_DIRECTORY;
|
|
|
|
}
|
|
|
|
return PATH_FILE;
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 21:59:07 +00:00
|
|
|
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;
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 21:59:07 +00:00
|
|
|
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;
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
#endif
|
2008-03-18 17:47:38 +00:00
|
|
|
|
2012-05-06 21:59:07 +00:00
|
|
|
bool radCopyTree( const char *source, const char *dest, bool fatal_on_error ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
Str srcEntry;
|
|
|
|
Str dstEntry;
|
|
|
|
const char *dirname;
|
|
|
|
FindFiles fileScan( source );
|
2008-03-18 17:47:38 +00:00
|
|
|
|
|
|
|
while ( ( dirname = fileScan.NextFile() ) != NULL ) {
|
|
|
|
if ( strcmp( dirname, "." ) == 0 || strcmp( dirname, ".." ) == 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( strcmp( dirname, ".svn" ) == 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
srcEntry = source;
|
|
|
|
srcEntry += "/";
|
|
|
|
srcEntry += dirname;
|
|
|
|
dstEntry = dest;
|
|
|
|
dstEntry += "/";
|
|
|
|
dstEntry += dirname;
|
|
|
|
switch ( CheckFile( srcEntry.GetBuffer() ) ) {
|
2012-05-06 21:59:07 +00:00
|
|
|
case PATH_FAIL: {
|
|
|
|
if ( fatal_on_error ) {
|
|
|
|
Error( "%s does not exist. check your permissions", srcEntry.GetBuffer() );
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
case PATH_DIRECTORY: {
|
|
|
|
if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) {
|
2012-05-06 21:59:07 +00:00
|
|
|
Sys_Printf( "Create directory %s\n", dstEntry.GetBuffer() );
|
|
|
|
if ( !radCreateDirectory( dstEntry.GetBuffer(), fatal_on_error ) ) {
|
2008-03-18 17:47:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
bool ret;
|
2012-05-06 21:59:07 +00:00
|
|
|
ret = radCopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !ret ) {
|
|
|
|
return false;
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PATH_FILE: {
|
2012-05-06 21:59:07 +00:00
|
|
|
Sys_Printf( "Copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
|
|
|
|
bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer(), fatal_on_error );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !ret ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-03-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|