2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // Leonardo Zide (leo@lokigames.com)
38 #include "qsysprintf.h"
40 #if defined ( __linux__ ) || defined ( __APPLE__ )
45 #include <sys/types.h>
50 bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ){
55 char realsrc[PATH_MAX], realdest[PATH_MAX];
57 realpath( lpExistingFileName, realsrc );
58 realpath( lpNewFileName, realdest );
60 src = fopen( realsrc, "rb" );
64 dst = fopen( realdest, "wb" );
70 fseek( src, 0, SEEK_END );
76 if ( fread( buf, l, 1, src ) == 1 ) {
77 if ( fwrite( buf, l, 1, dst ) == 1 ) {
90 bool radCreateDirectory( const char *directory ) {
91 if ( mkdir( directory, 0777 ) == -1 ) {
92 Sys_Printf( "mkdir %s failed\n", directory );
98 int GetFullPathName( const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart ){
99 if ( lpFileName[0] == '/' ) {
100 strcpy( lpBuffer, lpFileName );
101 *lpFilePart = strrchr( lpBuffer, '/' );
102 return strlen( lpBuffer );
105 if ( getcwd( lpBuffer, nBufferLength ) == NULL ) {
109 strcat( lpBuffer, "/" );
110 *lpFilePart = lpBuffer + strlen( lpBuffer );
111 strcat( lpBuffer, lpFileName );
113 char *scr = lpBuffer, *dst = lpBuffer;
114 for ( int i = 0; ( i < nBufferLength ) && ( *scr != 0 ); i++ )
116 if ( *scr == '/' && *( scr + 1 ) == '.' && *( scr + 2 ) == '.' ) {
118 while ( dst != lpBuffer && *dst != '/' )
131 return strlen( lpBuffer );
134 EPathCheck CheckFile( const char *path ) {
136 if ( stat( path, &sbuf ) == -1 ) {
139 if ( S_ISDIR( sbuf.st_mode ) ) {
140 return PATH_DIRECTORY;
145 FindFiles::FindFiles( const char *_directory ) {
146 findHandle = opendir( _directory );
149 FindFiles::~FindFiles() {
150 if ( findHandle != NULL ) {
151 closedir( findHandle );
156 const char* FindFiles::NextFile() {
159 if ( findHandle == NULL ) {
163 d = readdir( findHandle );
172 FindFiles::FindFiles( const char *_directory ) {
174 directory = _directory;
175 if ( directory.GetLength() > 0 ) {
176 endChar = directory.GetAt( directory.GetLength() - 1 );
177 if ( !( endChar == '/' || endChar == '\\' ) ) {
178 // We're only using '/' as the path separator throughout this code, not '\'.
179 // However, I'd hate to see the code silently fail due to a trailing '\', so
180 // I added the check for it.
185 findHandle = INVALID_HANDLE_VALUE;
188 FindFiles::~FindFiles() {
189 if ( findHandle != INVALID_HANDLE_VALUE ) {
190 FindClose( findHandle );
191 findHandle = INVALID_HANDLE_VALUE;
195 const char* FindFiles::NextFile() {
196 if ( findHandle == INVALID_HANDLE_VALUE ) {
197 findHandle = FindFirstFile( directory.GetBuffer(), &findFileData );
198 if ( findHandle == INVALID_HANDLE_VALUE ) {
201 return findFileData.cFileName;
203 if ( FindNextFile( findHandle, &findFileData ) == 0 ) {
204 FindClose( findHandle );
205 findHandle = INVALID_HANDLE_VALUE;
208 return findFileData.cFileName;
211 EPathCheck CheckFile( const char *path ) {
213 if ( _stat( path, &sbuf ) == -1 ) {
216 if ( ( sbuf.st_mode & _S_IFDIR ) != 0 ) {
217 return PATH_DIRECTORY;
222 bool radCreateDirectory( const char *directory ) {
223 return ( CreateDirectory( directory, NULL ) != false );
226 bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ) {
227 return ( CopyFile( lpExistingFileName, lpNewFileName, FALSE ) != false );
232 bool CopyTree( const char *source, const char *dest ) {
236 FindFiles fileScan( source );
238 while ( ( dirname = fileScan.NextFile() ) != NULL ) {
239 if ( strcmp( dirname, "." ) == 0 || strcmp( dirname, ".." ) == 0 ) {
242 if ( strcmp( dirname, ".svn" ) == 0 ) {
251 switch ( CheckFile( srcEntry.GetBuffer() ) ) {
252 case PATH_DIRECTORY: {
253 if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) {
254 if ( !radCreateDirectory( dstEntry.GetBuffer() ) ) {
255 Sys_Printf( "create directory %s failed\n", dstEntry.GetBuffer() );
260 ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
267 Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
268 bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );