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 // - Directories should be searched in the following order: ~/.q3a/baseq3,
35 // install dir (/usr/local/games/quake3/baseq3) and cd_path (/mnt/cdrom/baseq3).
37 // - Pak files are searched first inside the directories.
38 // - Case insensitive.
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)
41 // Leonardo Zide (leo@lokigames.com)
49 #include "filematch.h"
55 #define GARUX_DISABLE_BAD_MINIZ
56 #ifndef GARUX_DISABLE_BAD_MINIZ
69 // =============================================================================
72 static GSList* g_unzFiles;
73 static GSList* g_pakFiles;
74 static char g_strDirs[VFS_MAXDIRS][PATH_MAX + 1];
76 char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX + 1];
77 int g_numForbiddenDirs = 0;
78 static gboolean g_bUsePak = TRUE;
79 char g_strLoadedFileLocation[1024];
81 // =============================================================================
84 static void vfsAddSlash( char *str ){
85 int n = strlen( str );
87 if ( str[n - 1] != '\\' && str[n - 1] != '/' ) {
93 static void vfsFixDOSName( char *src ){
100 if ( *src == '\\' ) {
107 //!\todo Define globally or use heap-allocated string.
110 static void vfsInitPakFile( const char *filename ){
116 uf = unzOpen( filename );
121 g_unzFiles = g_slist_append( g_unzFiles, uf );
123 err = unzGetGlobalInfo( uf,&gi );
124 if ( err != UNZ_OK ) {
127 unzGoToFirstFile( uf );
129 char* unzFilePath = strdup( filename );
131 for ( i = 0; i < gi.number_entry; i++ )
133 char filename_inzip[NAME_MAX];
134 char *filename_lower;
135 unz_file_info file_info;
138 err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof( filename_inzip ), NULL, 0, NULL, 0 );
139 if ( err != UNZ_OK ) {
143 err = unzGetFilePos( uf, &pos );
144 if ( err != UNZ_OK ) {
148 file = (VFS_PAKFILE*)safe_malloc( sizeof( VFS_PAKFILE ) );
149 g_pakFiles = g_slist_append( g_pakFiles, file );
151 vfsFixDOSName( filename_inzip );
152 //-1 null terminated string
153 filename_lower = g_ascii_strdown( filename_inzip, -1 );
155 file->name = strdup( filename_lower );
156 file->size = file_info.uncompressed_size;
158 file->unzFilePath = unzFilePath;
161 if ( ( i + 1 ) < gi.number_entry ) {
162 err = unzGoToNextFile( uf );
163 if ( err != UNZ_OK ) {
167 g_free( filename_lower );
171 // =============================================================================
174 // reads all pak files from a dir
175 void vfsInitDirectory( const char *path ){
176 char filename[PATH_MAX];
181 for ( j = 0; j < g_numForbiddenDirs; ++j )
183 char* dbuf = g_strdup( path );
184 if ( *dbuf && dbuf[strlen( dbuf ) - 1] == '/' ) {
185 dbuf[strlen( dbuf ) - 1] = 0;
187 const char *p = strrchr( dbuf, '/' );
188 p = ( p ? ( p + 1 ) : dbuf );
189 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
195 if ( j < g_numForbiddenDirs ) {
199 if ( g_numDirs == VFS_MAXDIRS ) {
200 Sys_FPrintf( SYS_WRN, "WARNING: too many VFS directories, can't init %s\n", path );
204 Sys_Printf( "VFS Init: %s\n", path );
206 strncpy( g_strDirs[g_numDirs], path, PATH_MAX );
207 g_strDirs[g_numDirs][PATH_MAX] = 0;
208 vfsFixDOSName( g_strDirs[g_numDirs] );
209 vfsAddSlash( g_strDirs[g_numDirs] );
213 dir = g_dir_open( path, 0, NULL );
218 const char* name = g_dir_read_name( dir );
219 if ( name == NULL ) {
223 for ( j = 0; j < g_numForbiddenDirs; ++j )
225 const char *p = strrchr( name, '/' );
226 p = ( p ? ( p + 1 ) : name );
227 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
231 if ( j < g_numForbiddenDirs ) {
235 dirlist = g_strdup( name );
238 char *ext = strrchr( dirlist, '.' );
240 if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
241 if ( g_numDirs == VFS_MAXDIRS ) {
245 snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
246 g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
247 vfsFixDOSName( g_strDirs[g_numDirs] );
248 vfsAddSlash( g_strDirs[g_numDirs] );
252 if ( ext == NULL || ( Q_stricmp( ext, ".pk3" ) != 0 && Q_stricmp( ext, ".dpk" ) != 0 ) ) {
258 sprintf( filename, "%s/%s", path, dirlist );
259 vfsInitPakFile( filename );
269 // lists all .shader files
270 void vfsListShaderFiles( char* list, int *num ){
271 //char filename[PATH_MAX];
277 for ( i = 0; i < g_numDirs; i++ ){
278 strncpy( path, g_strDirs[ i ], NAME_MAX );
279 strcat( path, "scripts/" );
281 dir = g_dir_open( path, 0, NULL );
286 const char* name = g_dir_read_name( dir );
287 if ( name == NULL ) {
290 dirlist = g_strdup( name );
291 char *ext = strrchr( dirlist, '.' );
293 if ( ( ext == NULL ) || ( Q_stricmp( ext, ".shader" ) != 0 ) ) {
297 for ( k = 0; k < *num; k++ ){
298 if ( !Q_stricmp( list + k*65, dirlist ) ) goto shISdouplicate;
300 strcpy( list + (*num)*65, dirlist );
308 /* search in packs */
311 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
313 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
315 char *ext = strrchr( file->name, '.' );
317 if ( ( ext == NULL ) || ( Q_stricmp( ext, ".shader" ) != 0 ) ) {
320 //name + ext this time
321 ext = strrchr( file->name, '/' );
324 for ( k = 0; k < *num; k++ ){
325 if ( !Q_stricmp( list + k*65, ext ) ) goto shISdouplicate2;
327 strcpy( list + (*num)*65, ext );
334 // frees all memory that we allocated
338 unzClose( (unzFile)g_unzFiles->data );
339 g_unzFiles = g_slist_remove( g_unzFiles, g_unzFiles->data );
344 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
345 free( file->unzFilePath );
348 g_pakFiles = g_slist_remove( g_pakFiles, file );
352 // return the number of files that match
353 int vfsGetFileCount( const char *filename ){
355 char fixed[NAME_MAX], tmp[NAME_MAX];
359 strcpy( fixed, filename );
360 vfsFixDOSName( fixed );
361 lower = g_ascii_strdown( fixed, -1 );
363 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
365 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
367 if ( strcmp( file->name, lower ) == 0 ) {
372 for ( i = 0; i < g_numDirs; i++ )
374 strcpy( tmp, g_strDirs[i] );
375 strcat( tmp, lower );
376 if ( access( tmp, R_OK ) == 0 ) {
384 static qboolean isSymlink(const unz_file_info64 *fileInfo) {
385 // see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/stat.h
386 // redefine so it works outside of Unices
387 const unsigned long Q3MAP_S_IFMT = 00170000;
388 const unsigned long Q3MAP_S_IFLNK = 0120000;
389 // see https://trac.edgewall.org/attachment/ticket/8919/ZipDownload.patch
390 const unsigned long PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT = 16;
392 unsigned long attr = fileInfo->external_fa >> PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT;
393 return (attr & Q3MAP_S_IFMT) == Q3MAP_S_IFLNK;
396 // The zip format has a maximum filename size of 64K
397 static const int MAX_FILENAME_BUF = 65537;
399 /* The symlink implementation is ported from Dæmon engine implementation by slipher which was a complete rewrite of one illwieckz did on Dæmon by taking inspiration from Darkplaces engine.
403 - https://github.com/DaemonEngine/Daemon/blob/master/src/common/FileSystem.cpp
404 - https://gitlab.com/xonotic/darkplaces/-/blob/div0-stable/fs.c
406 Some words by slipher:
408 > Symlinks are a bad feature which you should not use. Therefore, the implementation is as
409 > slow as possible with a full iteration of the archive performed for each symlink.
411 > The symlink path `relative` must be relative to the symlink's location.
412 > Only supports paths consisting of "../" 0 or more times, followed by non-magical path components.
414 void resolveSymlinkPath( const char* base, const char* relative, char* resolved ){
416 base = g_path_get_dirname( base );
418 while( g_str_has_prefix( relative, "../" ) )
420 if ( base[0] == '\0' )
422 Sys_FPrintf( SYS_WRN, "Error while reading symbolic link: \"%s\": no such directory\n", base );
427 base = g_path_get_dirname( base );
431 snprintf( resolved, MAX_FILENAME_BUF, "%s/%s", base, relative);
434 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
435 int vfsLoadFile( const char *filename, void **bufferptr, int index ){
437 char tmp[NAME_MAX], fixed[NAME_MAX];
441 // filename is a full path
443 strcpy( g_strLoadedFileLocation, filename );
447 f = fopen( filename, "rb" );
452 fseek( f, 0, SEEK_END );
456 *bufferptr = safe_malloc( len + 1 );
457 if ( *bufferptr == NULL ) {
462 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
468 // we need to end the buffer with a 0
469 ( (char*) ( *bufferptr ) )[len] = 0;
475 strncpy( fixed, filename, sizeof( fixed ) );
476 vfsFixDOSName( fixed );
477 lower = g_ascii_strdown( fixed, -1 );
479 for ( i = 0; i < g_numDirs; i++ )
481 strcpy( tmp, g_strDirs[i] );
482 strcat( tmp, filename );
483 if ( access( tmp, R_OK ) == 0 ) {
484 if ( count == index ) {
485 strcpy( g_strLoadedFileLocation, tmp );
490 f = fopen( tmp, "rb" );
495 fseek( f, 0, SEEK_END );
499 *bufferptr = safe_malloc( len + 1 );
500 if ( *bufferptr == NULL ) {
505 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
511 // we need to end the buffer with a 0
512 ( (char*) ( *bufferptr ) )[len] = 0;
521 // Do not resolve more than 5 recursive symbolic links to
522 // prevent circular symbolic links.
523 int max_symlink_depth = 5;
526 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
528 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
530 if ( strcmp( file->name, lower ) != 0 ) {
534 if ( count == index ) {
535 strcpy( g_strLoadedFileLocation, file->unzFilePath );
536 strcat( g_strLoadedFileLocation, " :: " );
537 strcat( g_strLoadedFileLocation, filename );
540 if ( unzGoToFilePos( file->zipfile, &file->zippos ) != UNZ_OK ) {
543 if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
547 unz_file_info64 fileInfo;
548 if ( unzGetCurrentFileInfo64( file->zipfile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0 ) != UNZ_OK ) {
552 *bufferptr = safe_malloc( file->size + 1 );
553 // we need to end the buffer with a 0
554 ( (char*) ( *bufferptr ) )[file->size] = 0;
556 i = unzReadCurrentFile( file->zipfile, *bufferptr, file->size );
557 unzCloseCurrentFile( file->zipfile );
559 if ( isSymlink( &fileInfo ) ) {
560 Sys_FPrintf( SYS_VRB, "Found symbolic link: \"%s\"\n", filename );
562 if ( max_symlink_depth == 0 ) {
563 Sys_FPrintf( SYS_WRN, "Maximum symbolic link depth reached\n" );
570 const char* relative = (const char*) *bufferptr;
571 char resolved[MAX_FILENAME_BUF];
573 resolveSymlinkPath( file->name, relative, resolved );
575 Sys_FPrintf( SYS_VRB, "Resolved symbolic link: \"%s\"\n", resolved );
578 strncpy( fixed, resolved, sizeof( fixed ) );
579 vfsFixDOSName( fixed );
580 lower = g_ascii_strdown( fixed, -1 );
582 // slow as possible full iteration of the archive
583 goto openSymlinkTarget;
604 qboolean vfsPackFile( const char *filename, const char *packname, const int compLevel ){
605 #ifndef GARUX_DISABLE_BAD_MINIZ
607 char tmp[NAME_MAX], fixed[NAME_MAX];
610 byte *bufferptr = NULL;
611 strcpy( fixed, filename );
612 vfsFixDOSName( fixed );
615 for ( i = 0; i < g_numDirs; i++ )
617 strcpy( tmp, g_strDirs[i] );
618 strcat( tmp, filename );
619 if ( access( tmp, R_OK ) == 0 ) {
620 if ( access( packname, R_OK ) == 0 ) {
622 memset( &zip, 0, sizeof(zip) );
623 mz_zip_reader_init_file( &zip, packname, 0 );
624 mz_zip_writer_init_from_reader( &zip, packname );
626 mz_bool success = MZ_TRUE;
627 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
628 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
629 Error( "Failed creating zip archive \"%s\"!\n", packname );
631 mz_zip_reader_end( &zip);
632 mz_zip_writer_end( &zip );
636 memset( &zip, 0, sizeof(zip) );
637 if( !mz_zip_writer_init_file( &zip, packname, 0 ) ){
638 Error( "Failed creating zip archive \"%s\"!\n", packname );
640 mz_bool success = MZ_TRUE;
641 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
642 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
643 Error( "Failed creating zip archive \"%s\"!\n", packname );
645 mz_zip_writer_end( &zip );
652 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
654 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
656 if ( strcmp( file->name, fixed ) != 0 ) {
660 memcpy( file->zipfile, &file->zipinfo, sizeof( unz_s ) );
662 if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
666 bufferptr = safe_malloc( file->size + 1 );
667 // we need to end the buffer with a 0
668 ( (char*) ( bufferptr ) )[file->size] = 0;
670 mz_uint16 DOS_time = (mz_uint16)(((file->zipinfo.cur_file_info.tmu_date.tm_hour) << 11) + ((file->zipinfo.cur_file_info.tmu_date.tm_min) << 5) + ((file->zipinfo.cur_file_info.tmu_date.tm_sec) >> 1));
671 mz_uint16 DOS_date = (mz_uint16)(((file->zipinfo.cur_file_info.tmu_date.tm_year - 1980) << 9) + ((file->zipinfo.cur_file_info.tmu_date.tm_mon + 1) << 5) + file->zipinfo.cur_file_info.tmu_date.tm_mday);
673 i = unzReadCurrentFile( file->zipfile, bufferptr, file->size );
674 unzCloseCurrentFile( file->zipfile );
679 mz_bool success = MZ_TRUE;
680 success &= mz_zip_add_mem_to_archive_file_in_place_with_time( packname, filename, bufferptr, i, 0, 0, compLevel, DOS_time, DOS_date );
682 Error( "Failed creating zip archive \"%s\"!\n", packname );
691 Error( "Disabled because of miniz issue" );
695 qboolean vfsPackFile_Absolute_Path( const char *filepath, const char *filename, const char *packname, const int compLevel ){
696 #ifndef GARUX_DISABLE_BAD_MINIZ
698 strcpy( tmp, filepath );
699 if ( access( tmp, R_OK ) == 0 ) {
700 if ( access( packname, R_OK ) == 0 ) {
702 memset( &zip, 0, sizeof(zip) );
703 mz_zip_reader_init_file( &zip, packname, 0 );
704 mz_zip_writer_init_from_reader( &zip, packname );
706 mz_bool success = MZ_TRUE;
707 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
708 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
709 Error( "Failed creating zip archive \"%s\"!\n", packname );
711 mz_zip_reader_end( &zip);
712 mz_zip_writer_end( &zip );
716 memset( &zip, 0, sizeof(zip) );
717 if( !mz_zip_writer_init_file( &zip, packname, 0 ) ){
718 Error( "Failed creating zip archive \"%s\"!\n", packname );
720 mz_bool success = MZ_TRUE;
721 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
722 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
723 Error( "Failed creating zip archive \"%s\"!\n", packname );
725 mz_zip_writer_end( &zip );
733 Error( "Disabled because of miniz issue" );