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"
64 // =============================================================================
67 static GSList* g_unzFiles;
68 static GSList* g_pakFiles;
69 static char g_strDirs[VFS_MAXDIRS][PATH_MAX + 1];
71 char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX + 1];
72 int g_numForbiddenDirs = 0;
73 static gboolean g_bUsePak = TRUE;
75 // =============================================================================
78 static void vfsAddSlash( char *str ){
79 int n = strlen( str );
81 if ( str[n - 1] != '\\' && str[n - 1] != '/' ) {
87 static void vfsFixDOSName( char *src ){
101 //!\todo Define globally or use heap-allocated string.
104 static void vfsInitPakFile( const char *filename ){
110 uf = unzOpen( filename );
115 g_unzFiles = g_slist_append( g_unzFiles, uf );
117 err = unzGetGlobalInfo( uf,&gi );
118 if ( err != UNZ_OK ) {
121 unzGoToFirstFile( uf );
123 for ( i = 0; i < gi.number_entry; i++ )
125 char filename_inzip[NAME_MAX];
126 char *filename_lower;
127 unz_file_info file_info;
130 err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof( filename_inzip ), NULL, 0, NULL, 0 );
131 if ( err != UNZ_OK ) {
135 err = unzGetFilePos( uf, &pos );
136 if ( err != UNZ_OK ) {
140 file = (VFS_PAKFILE*)safe_malloc( sizeof( VFS_PAKFILE ) );
141 g_pakFiles = g_slist_append( g_pakFiles, file );
143 vfsFixDOSName( filename_inzip );
144 //-1 null terminated string
145 filename_lower = g_ascii_strdown( filename_inzip, -1 );
147 file->name = strdup( filename_lower );
148 file->size = file_info.uncompressed_size;
152 if ( ( i + 1 ) < gi.number_entry ) {
153 err = unzGoToNextFile( uf );
154 if ( err != UNZ_OK ) {
158 g_free( filename_lower );
162 // =============================================================================
165 // reads all pak files from a dir
166 void vfsInitDirectory( const char *path ){
167 char filename[PATH_MAX];
172 for ( j = 0; j < g_numForbiddenDirs; ++j )
174 char* dbuf = g_strdup( path );
175 if ( *dbuf && dbuf[strlen( dbuf ) - 1] == '/' ) {
176 dbuf[strlen( dbuf ) - 1] = 0;
178 const char *p = strrchr( dbuf, '/' );
179 p = ( p ? ( p + 1 ) : dbuf );
180 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
186 if ( j < g_numForbiddenDirs ) {
190 if ( g_numDirs == VFS_MAXDIRS ) {
194 Sys_Printf( "VFS Init: %s\n", path );
196 strncpy( g_strDirs[g_numDirs], path, PATH_MAX );
197 g_strDirs[g_numDirs][PATH_MAX] = 0;
198 vfsFixDOSName( g_strDirs[g_numDirs] );
199 vfsAddSlash( g_strDirs[g_numDirs] );
203 dir = g_dir_open( path, 0, NULL );
208 const char* name = g_dir_read_name( dir );
209 if ( name == NULL ) {
213 for ( j = 0; j < g_numForbiddenDirs; ++j )
215 const char *p = strrchr( name, '/' );
216 p = ( p ? ( p + 1 ) : name );
217 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
221 if ( j < g_numForbiddenDirs ) {
225 dirlist = g_strdup( name );
228 char *ext = strrchr( dirlist, '.' );
230 if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
231 if ( g_numDirs == VFS_MAXDIRS ) {
235 snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
236 g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
237 vfsFixDOSName( g_strDirs[g_numDirs] );
238 vfsAddSlash( g_strDirs[g_numDirs] );
242 if ( ext == NULL || ( Q_stricmp( ext, ".pk3" ) != 0 && Q_stricmp( ext, ".dpk" ) != 0 ) ) {
248 sprintf( filename, "%s/%s", path, dirlist );
249 vfsInitPakFile( filename );
258 // frees all memory that we allocated
262 unzClose( (unzFile)g_unzFiles->data );
263 g_unzFiles = g_slist_remove( g_unzFiles, g_unzFiles->data );
268 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
271 g_pakFiles = g_slist_remove( g_pakFiles, file );
275 // return the number of files that match
276 int vfsGetFileCount( const char *filename ){
278 char fixed[NAME_MAX], tmp[NAME_MAX];
282 strcpy( fixed, filename );
283 vfsFixDOSName( fixed );
284 lower = g_ascii_strdown( fixed, -1 );
286 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
288 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
290 if ( strcmp( file->name, lower ) == 0 ) {
295 for ( i = 0; i < g_numDirs; i++ )
297 strcpy( tmp, g_strDirs[i] );
298 strcat( tmp, lower );
299 if ( access( tmp, R_OK ) == 0 ) {
307 static qboolean isSymlink(const unz_file_info64 *fileInfo) {
308 // see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/stat.h
309 // redefine so it works outside of Unices
310 const unsigned long Q3MAP_S_IFMT = 00170000;
311 const unsigned long Q3MAP_S_IFLNK = 0120000;
312 // see https://trac.edgewall.org/attachment/ticket/8919/ZipDownload.patch
313 const unsigned long PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT = 16;
315 unsigned long attr = fileInfo->external_fa >> PKZIP_EXTERNAL_ATTR_FILE_TYPE_SHIFT;
316 return (attr & Q3MAP_S_IFMT) == Q3MAP_S_IFLNK;
319 // The zip format has a maximum filename size of 64K
320 static const int MAX_FILENAME_BUF = 65537;
322 /* 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.
326 - https://github.com/DaemonEngine/Daemon/blob/master/src/common/FileSystem.cpp
327 - https://gitlab.com/xonotic/darkplaces/-/blob/div0-stable/fs.c
329 Some words by slipher:
331 > Symlinks are a bad feature which you should not use. Therefore, the implementation is as
332 > slow as possible with a full iteration of the archive performed for each symlink.
334 > The symlink path `relative` must be relative to the symlink's location.
335 > Only supports paths consisting of "../" 0 or more times, followed by non-magical path components.
337 void resolveSymlinkPath( const char* base, const char* relative, char* resolved ){
339 base = g_path_get_dirname( base );
341 while( g_str_has_prefix( relative, "../" ) )
343 if ( base[0] == '\0' )
345 Sys_FPrintf( SYS_WRN, "Error while reading symbolic link: \"%s\": no such directory\n", base );
350 base = g_path_get_dirname( base );
354 snprintf( resolved, MAX_FILENAME_BUF, "%s/%s", base, relative);
357 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
358 int vfsLoadFile( const char *filename, void **bufferptr, int index ){
360 char tmp[NAME_MAX], fixed[NAME_MAX];
364 // filename is a full path
369 f = fopen( filename, "rb" );
374 fseek( f, 0, SEEK_END );
378 *bufferptr = safe_malloc( len + 1 );
379 if ( *bufferptr == NULL ) {
384 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
390 // we need to end the buffer with a 0
391 ( (char*) ( *bufferptr ) )[len] = 0;
397 strncpy( fixed, filename, sizeof( fixed ) );
398 vfsFixDOSName( fixed );
399 lower = g_ascii_strdown( fixed, -1 );
401 for ( i = 0; i < g_numDirs; i++ )
403 strcpy( tmp, g_strDirs[i] );
404 strcat( tmp, filename );
405 if ( access( tmp, R_OK ) == 0 ) {
406 if ( count == index ) {
410 f = fopen( tmp, "rb" );
415 fseek( f, 0, SEEK_END );
419 *bufferptr = safe_malloc( len + 1 );
420 if ( *bufferptr == NULL ) {
425 if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
431 // we need to end the buffer with a 0
432 ( (char*) ( *bufferptr ) )[len] = 0;
441 // Do not resolve more than 5 recursive symbolic links to
442 // prevent circular symbolic links.
443 int max_symlink_depth = 5;
446 for ( lst = g_pakFiles; lst != NULL; lst = g_slist_next( lst ) )
448 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
450 if ( strcmp( file->name, lower ) != 0 ) {
454 if ( count == index ) {
456 if ( unzGoToFilePos( file->zipfile, &file->zippos ) != UNZ_OK ) {
459 if ( unzOpenCurrentFile( file->zipfile ) != UNZ_OK ) {
463 unz_file_info64 fileInfo;
464 if ( unzGetCurrentFileInfo64( file->zipfile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0 ) != UNZ_OK ) {
468 *bufferptr = safe_malloc( file->size + 1 );
469 // we need to end the buffer with a 0
470 ( (char*) ( *bufferptr ) )[file->size] = 0;
472 i = unzReadCurrentFile( file->zipfile, *bufferptr, file->size );
473 unzCloseCurrentFile( file->zipfile );
475 if ( isSymlink( &fileInfo ) ) {
476 Sys_FPrintf( SYS_VRB, "Found symbolic link: \"%s\"\n", filename );
478 if ( max_symlink_depth == 0 ) {
479 Sys_FPrintf( SYS_WRN, "Maximum symbolic link depth reached\n" );
486 const char* relative = (const char*) *bufferptr;
487 char resolved[MAX_FILENAME_BUF];
489 resolveSymlinkPath( file->name, relative, resolved );
491 Sys_FPrintf( SYS_VRB, "Resolved symbolic link: \"%s\"\n", resolved );
494 strncpy( fixed, resolved, sizeof( fixed ) );
495 vfsFixDOSName( fixed );
496 lower = g_ascii_strdown( fixed, -1 );
498 // slow as possible full iteration of the archive
499 goto openSymlinkTarget;