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"
68 // =============================================================================
71 static GSList* g_unzFiles;
72 static GSList* g_pakFiles;
73 static char g_strDirs[VFS_MAXDIRS][PATH_MAX + 1];
75 char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX + 1];
76 int g_numForbiddenDirs = 0;
77 static gboolean g_bUsePak = TRUE;
79 // =============================================================================
82 static void vfsAddSlash( char *str ){
83 int n = strlen( str );
85 if ( str[n - 1] != '\\' && str[n - 1] != '/' ) {
91 static void vfsFixDOSName( char *src ){
105 //!\todo Define globally or use heap-allocated string.
108 static void vfsInitPakFile( const char *filename ){
114 uf = unzOpen( filename );
119 g_unzFiles = g_slist_append( g_unzFiles, uf );
121 err = unzGetGlobalInfo( uf,&gi );
122 if ( err != UNZ_OK ) {
125 unzGoToFirstFile( uf );
127 for ( i = 0; i < gi.number_entry; i++ )
129 char filename_inzip[NAME_MAX];
130 char *filename_lower;
131 unz_file_info file_info;
134 err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof( filename_inzip ), NULL, 0, NULL, 0 );
135 if ( err != UNZ_OK ) {
139 err = unzGetFilePos( uf, &pos );
140 if ( err != UNZ_OK ) {
144 file = (VFS_PAKFILE*)safe_malloc( sizeof( VFS_PAKFILE ) );
145 g_pakFiles = g_slist_append( g_pakFiles, file );
147 vfsFixDOSName( filename_inzip );
148 //-1 null terminated string
149 filename_lower = g_ascii_strdown( filename_inzip, -1 );
151 file->name = strdup( filename_lower );
152 file->size = file_info.uncompressed_size;
156 if ( ( i + 1 ) < gi.number_entry ) {
157 err = unzGoToNextFile( uf );
158 if ( err != UNZ_OK ) {
162 g_free( filename_lower );
166 // =============================================================================
169 // reads all pak files from a dir
170 void vfsInitDirectory( const char *path ){
171 char filename[PATH_MAX];
176 for ( j = 0; j < g_numForbiddenDirs; ++j )
178 char* dbuf = g_strdup( path );
179 if ( *dbuf && dbuf[strlen( dbuf ) - 1] == '/' ) {
180 dbuf[strlen( dbuf ) - 1] = 0;
182 const char *p = strrchr( dbuf, '/' );
183 p = ( p ? ( p + 1 ) : dbuf );
184 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
190 if ( j < g_numForbiddenDirs ) {
194 if ( g_numDirs == VFS_MAXDIRS ) {
195 Sys_FPrintf( SYS_WRN, "WARNING: too many VFS directories, can't init %s\n", path );
199 Sys_Printf( "VFS Init: %s\n", path );
201 strncpy( g_strDirs[g_numDirs], path, PATH_MAX );
202 g_strDirs[g_numDirs][PATH_MAX] = 0;
203 vfsFixDOSName( g_strDirs[g_numDirs] );
204 vfsAddSlash( g_strDirs[g_numDirs] );
208 dir = g_dir_open( path, 0, NULL );
213 const char* name = g_dir_read_name( dir );
214 if ( name == NULL ) {
218 for ( j = 0; j < g_numForbiddenDirs; ++j )
220 const char *p = strrchr( name, '/' );
221 p = ( p ? ( p + 1 ) : name );
222 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
226 if ( j < g_numForbiddenDirs ) {
230 dirlist = g_strdup( name );
233 char *ext = strrchr( dirlist, '.' );
235 if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
236 if ( g_numDirs == VFS_MAXDIRS ) {
240 snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
241 g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
242 vfsFixDOSName( g_strDirs[g_numDirs] );
243 vfsAddSlash( g_strDirs[g_numDirs] );
247 if ( ext == NULL || ( Q_stricmp( ext, ".pk3" ) != 0 && Q_stricmp( ext, ".dpk" ) != 0 ) ) {
253 sprintf( filename, "%s/%s", path, dirlist );
254 vfsInitPakFile( filename );
264 // lists all .shader files
265 void vfsListShaderFiles( char* list, int *num ){
266 //char filename[PATH_MAX];
272 for ( i = 0; i < g_numDirs; i++ ){
273 strncpy( path, g_strDirs[ i ], NAME_MAX );
274 strcat( path, "scripts/" );
276 dir = g_dir_open( path, 0, NULL );
281 const char* name = g_dir_read_name( dir );
282 if ( name == NULL ) {
285 dirlist = g_strdup( name );
286 char *ext = strrchr( dirlist, '.' );
288 if ( ( ext == NULL ) || ( Q_stricmp( ext, ".shader" ) != 0 ) ) {
292 for ( k = 0; k < *num; k++ ){
293 if ( !Q_stricmp( list + k*65, dirlist ) ) goto shISdouplicate;
295 strcpy( list + (*num)*65, dirlist );
303 /* search in packs */
306 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
308 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
310 char *ext = strrchr( file->name, '.' );
312 if ( ( ext == NULL ) || ( Q_stricmp( ext, ".shader" ) != 0 ) ) {
315 //name + ext this time
316 ext = strrchr( file->name, '/' );
319 for ( k = 0; k < *num; k++ ){
320 if ( !Q_stricmp( list + k*65, ext ) ) goto shISdouplicate2;
322 strcpy( list + (*num)*65, ext );
329 // frees all memory that we allocated
333 unzClose( (unzFile)g_unzFiles->data );
334 g_unzFiles = g_slist_remove( g_unzFiles, g_unzFiles->data );
339 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
342 g_pakFiles = g_slist_remove( g_pakFiles, file );
346 // return the number of files that match
347 int vfsGetFileCount( const char *filename ){
349 char fixed[NAME_MAX], tmp[NAME_MAX];
353 strcpy( fixed, filename );
354 vfsFixDOSName( fixed );
355 lower = g_ascii_strdown( fixed, -1 );
357 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
359 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
361 if ( strcmp( file->name, lower ) == 0 ) {
366 for ( i = 0; i < g_numDirs; i++ )
368 strcpy( tmp, g_strDirs[i] );
369 strcat( tmp, lower );
370 if ( access( tmp, R_OK ) == 0 ) {
378 static qboolean isSymlink(const unz_file_info64 *fileInfo) {
379 // see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/stat.h
380 // redefine so it works outside of Unices
381 const unsigned long Q3MAP_S_IFMT = 00170000;
382 const unsigned long Q3MAP_S_IFLNK = 0120000;
383 // see https://trac.edgewall.org/attachment/ticket/8919/ZipDownload.patch
384 const unsigned long PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT = 16;
386 unsigned long attr = fileInfo->external_fa >> PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT;
387 return (attr & Q3MAP_S_IFMT) == Q3MAP_S_IFLNK;
390 // The zip format has a maximum filename size of 64K
391 static const int MAX_FILENAME_BUF = 65537;
393 /* 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.
397 - https://github.com/DaemonEngine/Daemon/blob/master/src/common/FileSystem.cpp
398 - https://gitlab.com/xonotic/darkplaces/-/blob/div0-stable/fs.c
400 Some words by slipher:
402 > Symlinks are a bad feature which you should not use. Therefore, the implementation is as
403 > slow as possible with a full iteration of the archive performed for each symlink.
405 > The symlink path `relative` must be relative to the symlink's location.
406 > Only supports paths consisting of "../" 0 or more times, followed by non-magical path components.
408 void resolveSymlinkPath( const char* base, const char* relative, char* resolved ){
410 base = g_path_get_dirname( base );
412 while( g_str_has_prefix( relative, "../" ) )
414 if ( base[0] == '\0' )
416 Sys_FPrintf( SYS_WRN, "Error while reading symbolic link: \"%s\": no such directory\n", base );
421 base = g_path_get_dirname( base );
425 snprintf( resolved, MAX_FILENAME_BUF, "%s/%s", base, relative);
428 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
429 int vfsLoadFile( const char *filename, void **bufferptr, int index ){
431 char tmp[NAME_MAX], fixed[NAME_MAX];
435 // filename is a full path
440 f = fopen( filename, "rb" );
445 fseek( f, 0, SEEK_END );
449 *bufferptr = safe_malloc( len + 1 );
450 if ( *bufferptr == NULL ) {
455 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
461 // we need to end the buffer with a 0
462 ( (char*) ( *bufferptr ) )[len] = 0;
468 strncpy( fixed, filename, sizeof( fixed ) );
469 vfsFixDOSName( fixed );
470 lower = g_ascii_strdown( fixed, -1 );
472 for ( i = 0; i < g_numDirs; i++ )
474 strcpy( tmp, g_strDirs[i] );
475 strcat( tmp, filename );
476 if ( access( tmp, R_OK ) == 0 ) {
477 if ( count == index ) {
481 f = fopen( tmp, "rb" );
486 fseek( f, 0, SEEK_END );
490 *bufferptr = safe_malloc( len + 1 );
491 if ( *bufferptr == NULL ) {
496 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
502 // we need to end the buffer with a 0
503 ( (char*) ( *bufferptr ) )[len] = 0;
512 // Do not resolve more than 5 recursive symbolic links to
513 // prevent circular symbolic links.
514 int max_symlink_depth = 5;
517 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
519 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
521 if ( strcmp( file->name, lower ) != 0 ) {
525 if ( count == index ) {
527 if ( unzGoToFilePos( file->zipfile, &file->zippos ) != UNZ_OK ) {
530 if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
534 unz_file_info64 fileInfo;
535 if ( unzGetCurrentFileInfo64( file->zipfile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0 ) != UNZ_OK ) {
539 *bufferptr = safe_malloc( file->size + 1 );
540 // we need to end the buffer with a 0
541 ( (char*) ( *bufferptr ) )[file->size] = 0;
543 i = unzReadCurrentFile( file->zipfile, *bufferptr, file->size );
544 unzCloseCurrentFile( file->zipfile );
546 if ( isSymlink( &fileInfo ) ) {
547 Sys_FPrintf( SYS_VRB, "Found symbolic link: \"%s\"\n", filename );
549 if ( max_symlink_depth == 0 ) {
550 Sys_FPrintf( SYS_WRN, "Maximum symbolic link depth reached\n" );
557 const char* relative = (const char*) *bufferptr;
558 char resolved[MAX_FILENAME_BUF];
560 resolveSymlinkPath( file->name, relative, resolved );
562 Sys_FPrintf( SYS_VRB, "Resolved symbolic link: \"%s\"\n", resolved );
565 strncpy( fixed, resolved, sizeof( fixed ) );
566 vfsFixDOSName( fixed );
567 lower = g_ascii_strdown( fixed, -1 );
569 // slow as possible full iteration of the archive
570 goto openSymlinkTarget;
591 qboolean vfsPackFile( const char *filename, const char *packname, const int compLevel ){
594 char tmp[NAME_MAX], fixed[NAME_MAX];
597 byte *bufferptr = NULL;
598 strcpy( fixed, filename );
599 vfsFixDOSName( fixed );
602 for ( i = 0; i < g_numDirs; i++ )
604 strcpy( tmp, g_strDirs[i] );
605 strcat( tmp, filename );
606 if ( access( tmp, R_OK ) == 0 ) {
607 if ( access( packname, R_OK ) == 0 ) {
609 memset( &zip, 0, sizeof(zip) );
610 mz_zip_reader_init_file( &zip, packname, 0 );
611 mz_zip_writer_init_from_reader( &zip, packname );
613 mz_bool success = MZ_TRUE;
614 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
615 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
616 Error( "Failed creating zip archive \"%s\"!\n", packname );
618 mz_zip_reader_end( &zip);
619 mz_zip_writer_end( &zip );
623 memset( &zip, 0, sizeof(zip) );
624 if( !mz_zip_writer_init_file( &zip, packname, 0 ) ){
625 Error( "Failed creating zip archive \"%s\"!\n", packname );
627 mz_bool success = MZ_TRUE;
628 success &= mz_zip_writer_add_file( &zip, filename, tmp, 0, 0, compLevel );
629 if ( !success || !mz_zip_writer_finalize_archive( &zip ) ){
630 Error( "Failed creating zip archive \"%s\"!\n", packname );
632 mz_zip_writer_end( &zip );
639 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
641 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
643 if ( strcmp( file->name, fixed ) != 0 ) {
647 memcpy( file->zipfile, &file->zipinfo, sizeof( unz_s ) );
649 if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
653 bufferptr = safe_malloc( file->size + 1 );
654 // we need to end the buffer with a 0
655 ( (char*) ( bufferptr ) )[file->size] = 0;
657 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));
658 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);
660 i = unzReadCurrentFile( file->zipfile, bufferptr, file->size );
661 unzCloseCurrentFile( file->zipfile );
666 mz_bool success = MZ_TRUE;
667 success &= mz_zip_add_mem_to_archive_file_in_place_with_time( packname, filename, bufferptr, i, 0, 0, compLevel, DOS_time, DOS_date );
669 Error( "Failed creating zip archive \"%s\"!\n", packname );
678 Error( "Disabled because of miniz issue" );