2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
pak.c - PAK support for filesystem
|
2023-04-15 01:28:04 +02:00
|
|
|
Copyright (C) 2003-2006 Mathieu Olivier
|
|
|
|
Copyright (C) 2000-2007 DarkPlaces contributors
|
2022-07-01 18:37:21 +02:00
|
|
|
Copyright (C) 2007 Uncle Mike
|
2023-04-15 01:28:04 +02:00
|
|
|
Copyright (C) 2015-2023 Xash3D FWGS contributors
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2022-08-01 02:12:35 +02:00
|
|
|
#include "build.h"
|
2022-07-01 18:37:21 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2022-08-01 02:12:35 +02:00
|
|
|
#if XASH_POSIX
|
2022-07-01 18:37:21 +02:00
|
|
|
#include <unistd.h>
|
2022-08-01 02:12:35 +02:00
|
|
|
#endif
|
2022-07-01 18:37:21 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "port.h"
|
|
|
|
#include "filesystem_internal.h"
|
|
|
|
#include "crtlib.h"
|
|
|
|
#include "common/com_strings.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================================================================
|
|
|
|
PAK FILES
|
|
|
|
|
|
|
|
The .pak files are just a linear collapse of a directory tree
|
|
|
|
========================================================================
|
|
|
|
*/
|
|
|
|
// header
|
|
|
|
#define IDPACKV1HEADER (('K'<<24)+('C'<<16)+('A'<<8)+'P') // little-endian "PACK"
|
|
|
|
|
|
|
|
#define MAX_FILES_IN_PACK 65536 // pak
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int ident;
|
|
|
|
int dirofs;
|
|
|
|
int dirlen;
|
|
|
|
} dpackheader_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char name[56]; // total 64 bytes
|
|
|
|
int filepos;
|
|
|
|
int filelen;
|
|
|
|
} dpackfile_t;
|
|
|
|
|
|
|
|
// PAK errors
|
|
|
|
#define PAK_LOAD_OK 0
|
|
|
|
#define PAK_LOAD_COULDNT_OPEN 1
|
|
|
|
#define PAK_LOAD_BAD_HEADER 2
|
|
|
|
#define PAK_LOAD_BAD_FOLDERS 3
|
|
|
|
#define PAK_LOAD_TOO_MANY_FILES 4
|
|
|
|
#define PAK_LOAD_NO_FILES 5
|
|
|
|
#define PAK_LOAD_CORRUPTED 6
|
|
|
|
|
2022-08-06 19:33:01 +02:00
|
|
|
struct pack_s
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
int handle;
|
|
|
|
int numfiles;
|
|
|
|
time_t filetime; // common for all packed files
|
2022-12-15 02:01:30 +01:00
|
|
|
dpackfile_t files[1]; // flexible
|
2022-08-06 19:33:01 +02:00
|
|
|
};
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
2022-12-15 02:01:30 +01:00
|
|
|
FS_SortPak
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
====================
|
|
|
|
*/
|
2022-12-15 02:01:30 +01:00
|
|
|
static int FS_SortPak( const void *_a, const void *_b )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-12-15 02:01:30 +01:00
|
|
|
const dpackfile_t *a = _a, *b = _b;
|
2022-07-01 18:37:21 +02:00
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
return Q_stricmp( a->name, b->name );
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
FS_LoadPackPAK
|
|
|
|
|
|
|
|
Takes an explicit (not game tree related) path to a pak file.
|
|
|
|
|
|
|
|
Loads the header and directory, adding the files at the beginning
|
|
|
|
of the list so they override previous pack files.
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
static pack_t *FS_LoadPackPAK( const char *packfile, int *error )
|
|
|
|
{
|
|
|
|
dpackheader_t header;
|
|
|
|
int packhandle;
|
2022-12-15 02:01:30 +01:00
|
|
|
int numpackfiles;
|
2022-07-01 18:37:21 +02:00
|
|
|
pack_t *pack;
|
|
|
|
fs_size_t c;
|
|
|
|
|
|
|
|
packhandle = open( packfile, O_RDONLY|O_BINARY );
|
|
|
|
|
|
|
|
if( packhandle < 0 )
|
|
|
|
{
|
|
|
|
Con_Reportf( "%s couldn't open: %s\n", packfile, strerror( errno ));
|
|
|
|
if( error ) *error = PAK_LOAD_COULDNT_OPEN;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = read( packhandle, (void *)&header, sizeof( header ));
|
|
|
|
|
|
|
|
if( c != sizeof( header ) || header.ident != IDPACKV1HEADER )
|
|
|
|
{
|
|
|
|
Con_Reportf( "%s is not a packfile. Ignored.\n", packfile );
|
|
|
|
if( error ) *error = PAK_LOAD_BAD_HEADER;
|
|
|
|
close( packhandle );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( header.dirlen % sizeof( dpackfile_t ))
|
|
|
|
{
|
|
|
|
Con_Reportf( S_ERROR "%s has an invalid directory size. Ignored.\n", packfile );
|
|
|
|
if( error ) *error = PAK_LOAD_BAD_FOLDERS;
|
|
|
|
close( packhandle );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
numpackfiles = header.dirlen / sizeof( dpackfile_t );
|
|
|
|
|
|
|
|
if( numpackfiles > MAX_FILES_IN_PACK )
|
|
|
|
{
|
|
|
|
Con_Reportf( S_ERROR "%s has too many files ( %i ). Ignored.\n", packfile, numpackfiles );
|
|
|
|
if( error ) *error = PAK_LOAD_TOO_MANY_FILES;
|
|
|
|
close( packhandle );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( numpackfiles <= 0 )
|
|
|
|
{
|
|
|
|
Con_Reportf( "%s has no files. Ignored.\n", packfile );
|
|
|
|
if( error ) *error = PAK_LOAD_NO_FILES;
|
|
|
|
close( packhandle );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
pack = (pack_t *)Mem_Calloc( fs_mempool, sizeof( pack_t ) + sizeof( dpackfile_t ) * ( numpackfiles - 1 ));
|
2022-07-01 18:37:21 +02:00
|
|
|
lseek( packhandle, header.dirofs, SEEK_SET );
|
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
if( header.dirlen != read( packhandle, (void *)pack->files, header.dirlen ))
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
Con_Reportf( "%s is an incomplete PAK, not loading\n", packfile );
|
2022-12-15 02:01:30 +01:00
|
|
|
if( error )
|
|
|
|
*error = PAK_LOAD_CORRUPTED;
|
2022-07-01 18:37:21 +02:00
|
|
|
close( packhandle );
|
2022-12-15 02:01:30 +01:00
|
|
|
Mem_Free( pack );
|
2022-07-01 18:37:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
// TODO: validate directory?
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
pack->filetime = FS_SysFileTime( packfile );
|
|
|
|
pack->handle = packhandle;
|
2022-12-15 02:01:30 +01:00
|
|
|
pack->numfiles = numpackfiles;
|
|
|
|
qsort( pack->files, pack->numfiles, sizeof( pack->files[0] ), FS_SortPak );
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
#ifdef XASH_REDUCE_FD
|
|
|
|
// will reopen when needed
|
|
|
|
close( pack->handle );
|
|
|
|
pack->handle = -1;
|
|
|
|
#endif
|
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
if( error )
|
|
|
|
*error = PAK_LOAD_OK;
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_OpenPackedFile
|
|
|
|
|
|
|
|
Open a packed file using its package file descriptor
|
|
|
|
===========
|
|
|
|
*/
|
2022-12-14 22:59:52 +01:00
|
|
|
static file_t *FS_OpenFile_PAK( searchpath_t *search, const char *filename, const char *mode, int pack_ind )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
dpackfile_t *pfile;
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
pfile = &search->pack->files[pack_ind];
|
2022-07-01 18:37:21 +02:00
|
|
|
|
2022-12-15 02:01:30 +01:00
|
|
|
return FS_OpenHandle( search->filename, search->pack->handle, pfile->filepos, pfile->filelen );
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-12-14 22:59:52 +01:00
|
|
|
===========
|
|
|
|
FS_FindFile_PAK
|
2022-07-01 18:37:21 +02:00
|
|
|
|
2022-12-14 22:59:52 +01:00
|
|
|
===========
|
2022-07-01 18:37:21 +02:00
|
|
|
*/
|
2022-12-19 15:22:30 +01:00
|
|
|
static int FS_FindFile_PAK( searchpath_t *search, const char *path, char *fixedname, size_t len )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
int left, right, middle;
|
|
|
|
|
|
|
|
// look for the file (binary search)
|
|
|
|
left = 0;
|
2022-11-18 14:35:21 +01:00
|
|
|
right = search->pack->numfiles - 1;
|
2022-07-01 18:37:21 +02:00
|
|
|
while( left <= right )
|
|
|
|
{
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
middle = (left + right) / 2;
|
2022-11-18 14:35:21 +01:00
|
|
|
diff = Q_stricmp( search->pack->files[middle].name, path );
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
// Found it
|
|
|
|
if( !diff )
|
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
if( fixedname )
|
|
|
|
Q_strncpy( fixedname, search->pack->files[middle].name, len );
|
2022-07-01 18:37:21 +02:00
|
|
|
return middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're too far in the list
|
|
|
|
if( diff > 0 )
|
|
|
|
right = middle - 1;
|
|
|
|
else left = middle + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:59:52 +01:00
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_Search_PAK
|
|
|
|
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
static void FS_Search_PAK( searchpath_t *search, stringlist_t *list, const char *pattern, int caseinsensitive )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
const char *slash, *backslash, *colon, *separator;
|
|
|
|
int j, i;
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
for( i = 0; i < search->pack->numfiles; i++ )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-11-18 14:35:21 +01:00
|
|
|
Q_strncpy( temp, search->pack->files[i].name, sizeof( temp ));
|
2022-07-01 18:37:21 +02:00
|
|
|
while( temp[0] )
|
|
|
|
{
|
|
|
|
if( matchpattern( temp, pattern, true ))
|
|
|
|
{
|
|
|
|
for( j = 0; j < list->numstrings; j++ )
|
|
|
|
{
|
|
|
|
if( !Q_strcmp( list->strings[j], temp ))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( j == list->numstrings )
|
|
|
|
stringlistappend( list, temp );
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip off one path element at a time until empty
|
|
|
|
// this way directories are added to the listing if they match the pattern
|
|
|
|
slash = Q_strrchr( temp, '/' );
|
|
|
|
backslash = Q_strrchr( temp, '\\' );
|
|
|
|
colon = Q_strrchr( temp, ':' );
|
|
|
|
separator = temp;
|
|
|
|
if( separator < slash )
|
|
|
|
separator = slash;
|
|
|
|
if( separator < backslash )
|
|
|
|
separator = backslash;
|
|
|
|
if( separator < colon )
|
|
|
|
separator = colon;
|
|
|
|
*((char *)separator) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:59:52 +01:00
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_FileTime_PAK
|
|
|
|
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
static int FS_FileTime_PAK( searchpath_t *search, const char *filename )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-11-18 14:35:21 +01:00
|
|
|
return search->pack->filetime;
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 22:59:52 +01:00
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_PrintInfo_PAK
|
|
|
|
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
static void FS_PrintInfo_PAK( searchpath_t *search, char *dst, size_t size )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-12-15 02:01:30 +01:00
|
|
|
Q_snprintf( dst, size, "%s (%i files)", search->filename, search->pack->numfiles );
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 22:59:52 +01:00
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_Close_PAK
|
|
|
|
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
static void FS_Close_PAK( searchpath_t *search )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-11-18 14:35:21 +01:00
|
|
|
if( search->pack->handle >= 0 )
|
|
|
|
close( search->pack->handle );
|
|
|
|
Mem_Free( search->pack );
|
2022-12-14 22:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_AddPak_Fullpath
|
|
|
|
|
|
|
|
Adds the given pack to the search path.
|
|
|
|
The pack type is autodetected by the file extension.
|
|
|
|
|
|
|
|
Returns true if the file was successfully added to the
|
|
|
|
search path or if it was already included.
|
|
|
|
|
|
|
|
If keep_plain_dirs is set, the pack will be added AFTER the first sequence of
|
|
|
|
plain directories.
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
qboolean FS_AddPak_Fullpath( const char *pakfile, qboolean *already_loaded, int flags )
|
|
|
|
{
|
|
|
|
searchpath_t *search;
|
|
|
|
pack_t *pak = NULL;
|
|
|
|
const char *ext = COM_FileExtension( pakfile );
|
|
|
|
int i, errorcode = PAK_LOAD_COULDNT_OPEN;
|
|
|
|
|
|
|
|
for( search = fs_searchpaths; search; search = search->next )
|
|
|
|
{
|
2022-12-15 02:01:30 +01:00
|
|
|
if( search->type == SEARCHPATH_PAK && !Q_stricmp( search->filename, pakfile ))
|
2022-12-14 22:59:52 +01:00
|
|
|
{
|
|
|
|
if( already_loaded ) *already_loaded = true;
|
|
|
|
return true; // already loaded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( already_loaded )
|
|
|
|
*already_loaded = false;
|
|
|
|
|
|
|
|
if( !Q_stricmp( ext, "pak" ))
|
|
|
|
pak = FS_LoadPackPAK( pakfile, &errorcode );
|
|
|
|
|
|
|
|
if( pak )
|
|
|
|
{
|
|
|
|
search = (searchpath_t *)Mem_Calloc( fs_mempool, sizeof( searchpath_t ));
|
2022-12-15 02:01:30 +01:00
|
|
|
Q_strncpy( search->filename, pakfile, sizeof( search->filename ));
|
2022-12-14 22:59:52 +01:00
|
|
|
search->pack = pak;
|
|
|
|
search->type = SEARCHPATH_PAK;
|
|
|
|
search->next = fs_searchpaths;
|
2022-12-15 02:01:30 +01:00
|
|
|
search->flags = flags;
|
2022-12-14 22:59:52 +01:00
|
|
|
|
2022-12-14 23:06:20 +01:00
|
|
|
search->pfnPrintInfo = FS_PrintInfo_PAK;
|
|
|
|
search->pfnClose = FS_Close_PAK;
|
|
|
|
search->pfnOpenFile = FS_OpenFile_PAK;
|
|
|
|
search->pfnFileTime = FS_FileTime_PAK;
|
|
|
|
search->pfnFindFile = FS_FindFile_PAK;
|
|
|
|
search->pfnSearch = FS_Search_PAK;
|
2022-12-14 22:59:52 +01:00
|
|
|
|
|
|
|
fs_searchpaths = search;
|
|
|
|
|
|
|
|
Con_Reportf( "Adding pakfile: %s (%i files)\n", pakfile, pak->numfiles );
|
|
|
|
|
|
|
|
// time to add in search list all the wads that contains in current pakfile (if do)
|
|
|
|
for( i = 0; i < pak->numfiles; i++ )
|
|
|
|
{
|
|
|
|
if( !Q_stricmp( COM_FileExtension( pak->files[i].name ), "wad" ))
|
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
char fullpath[MAX_SYSPATH];
|
|
|
|
|
|
|
|
Q_snprintf( fullpath, sizeof( fullpath ), "%s/%s", pakfile, pak->files[i].name );
|
2022-12-14 22:59:52 +01:00
|
|
|
FS_AddWad_Fullpath( fullpath, NULL, flags );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( errorcode != PAK_LOAD_NO_FILES )
|
|
|
|
Con_Reportf( S_ERROR "FS_AddPak_Fullpath: unable to load pak \"%s\"\n", pakfile );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|