2022-11-18 14:35:21 +01:00
|
|
|
/*
|
2022-12-19 15:22:30 +01:00
|
|
|
dir.c - caseinsensitive directory operations
|
2022-11-18 14:35:21 +01:00
|
|
|
Copyright (C) 2022 Alibek Omarov, Velaron
|
2023-04-15 01:28:04 +02:00
|
|
|
Copyright (C) 2023 Xash3D FWGS contributors
|
2022-11-18 14:35:21 +01: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-12-26 19:27:04 +01:00
|
|
|
#include "build.h"
|
2022-11-18 14:35:21 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2022-12-26 19:27:04 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
2022-11-18 14:35:21 +01:00
|
|
|
#if XASH_POSIX
|
|
|
|
#include <unistd.h>
|
2023-02-13 20:53:17 +01:00
|
|
|
#if !XASH_PSVITA
|
2022-12-26 19:27:04 +01:00
|
|
|
#include <sys/ioctl.h>
|
2022-11-18 14:35:21 +01:00
|
|
|
#endif
|
2023-02-13 20:53:17 +01:00
|
|
|
#endif
|
2022-12-26 19:27:04 +01:00
|
|
|
#if XASH_LINUX
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#ifndef FS_CASEFOLD_FL // for compatibility with older distros
|
|
|
|
#define FS_CASEFOLD_FL 0x40000000
|
|
|
|
#endif // FS_CASEFOLD_FL
|
|
|
|
#endif // XASH_LINUX
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
#include "port.h"
|
|
|
|
#include "filesystem_internal.h"
|
|
|
|
#include "crtlib.h"
|
|
|
|
#include "xash3d_mathlib.h"
|
|
|
|
#include "common/com_strings.h"
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DIRENTRY_EMPTY_DIRECTORY = 0, // don't care if it's not directory or it's empty
|
|
|
|
DIRENTRY_NOT_SCANNED = -1,
|
|
|
|
DIRENTRY_CASEINSENSITIVE = -2, // directory is already caseinsensitive, just copy whatever is left
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct dir_s
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
int numentries;
|
|
|
|
struct dir_s *entries; // sorted
|
|
|
|
} dir_t;
|
|
|
|
|
2022-12-26 19:27:04 +01:00
|
|
|
static qboolean Platform_GetDirectoryCaseSensitivity( const char *dir )
|
|
|
|
{
|
2023-02-18 03:25:11 +01:00
|
|
|
#if XASH_WIN32 || XASH_PSVITA || XASH_NSWITCH
|
2022-12-26 19:27:04 +01:00
|
|
|
return false;
|
|
|
|
#elif XASH_LINUX && defined( FS_IOC_GETFLAGS )
|
|
|
|
int flags = 0;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open( dir, O_RDONLY | O_NONBLOCK );
|
|
|
|
if( fd < 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( ioctl( fd, FS_IOC_GETFLAGS, &flags ) < 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
close( fd );
|
|
|
|
|
|
|
|
return !FBitSet( flags, FS_CASEFOLD_FL );
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
static int FS_SortDirEntries( const void *_a, const void *_b )
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
|
|
|
const dir_t *a = _a;
|
|
|
|
const dir_t *b = _b;
|
|
|
|
return Q_stricmp( a->name, b->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FS_FreeDirEntries( dir_t *dir )
|
|
|
|
{
|
|
|
|
if( dir->entries )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < dir->numentries; i++ )
|
|
|
|
FS_FreeDirEntries( &dir->entries[i] );
|
|
|
|
dir->entries = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->numentries = DIRENTRY_NOT_SCANNED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FS_InitDirEntries( dir_t *dir, const stringlist_t *list )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dir->numentries = list->numstrings;
|
|
|
|
dir->entries = Mem_Malloc( fs_mempool, sizeof( dir_t ) * dir->numentries );
|
|
|
|
|
|
|
|
for( i = 0; i < list->numstrings; i++ )
|
|
|
|
{
|
|
|
|
dir_t *entry = &dir->entries[i];
|
2022-12-26 12:39:51 +01:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
Q_strncpy( entry->name, list->strings[i], sizeof( entry->name ));
|
|
|
|
entry->numentries = DIRENTRY_NOT_SCANNED;
|
|
|
|
entry->entries = NULL;
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
qsort( dir->entries, dir->numentries, sizeof( dir->entries[0] ), FS_SortDirEntries );
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void FS_PopulateDirEntries( dir_t *dir, const char *path )
|
2022-12-14 22:52:09 +01:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
stringlist_t list;
|
2022-12-14 22:52:09 +01:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
if( !FS_SysFolderExists( path ))
|
|
|
|
{
|
|
|
|
dir->numentries = DIRENTRY_EMPTY_DIRECTORY;
|
|
|
|
dir->entries = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-26 19:27:04 +01:00
|
|
|
if( !Platform_GetDirectoryCaseSensitivity( path ))
|
|
|
|
{
|
|
|
|
dir->numentries = DIRENTRY_CASEINSENSITIVE;
|
|
|
|
dir->entries = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
stringlistinit( &list );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &list, path );
|
|
|
|
if( !list.numstrings )
|
|
|
|
{
|
|
|
|
dir->numentries = DIRENTRY_EMPTY_DIRECTORY;
|
|
|
|
dir->entries = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FS_InitDirEntries( dir, &list );
|
|
|
|
}
|
2022-12-19 15:22:30 +01:00
|
|
|
stringlistfreecontents( &list );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FS_FindDirEntry( dir_t *dir, const char *name )
|
|
|
|
{
|
|
|
|
int left, right;
|
|
|
|
|
|
|
|
// look for the file (binary search)
|
|
|
|
left = 0;
|
|
|
|
right = dir->numentries - 1;
|
2022-12-26 12:39:51 +01:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
while( left <= right )
|
|
|
|
{
|
|
|
|
int middle = (left + right) / 2;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
diff = Q_stricmp( dir->entries[middle].name, name );
|
|
|
|
|
|
|
|
// found it
|
|
|
|
if( !diff )
|
|
|
|
return middle;
|
|
|
|
|
|
|
|
// if we're too far in the list
|
|
|
|
if( diff > 0 )
|
|
|
|
right = middle - 1;
|
|
|
|
else left = middle + 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FS_MergeDirEntries( dir_t *dir, const stringlist_t *list )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
dir_t temp;
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
// glorified realloc for sorted dir entries
|
|
|
|
// make new array and copy old entries with same name and subentries
|
|
|
|
// everything else get freed
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
FS_InitDirEntries( &temp, list );
|
|
|
|
|
|
|
|
for( i = 0; i < dir->numentries; i++ )
|
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
dir_t *oldentry = &dir->entries[i];
|
|
|
|
dir_t *newentry;
|
2022-12-19 15:22:30 +01:00
|
|
|
int j;
|
|
|
|
|
|
|
|
// don't care about directories without subentries
|
2022-12-26 12:39:51 +01:00
|
|
|
if( oldentry->entries == NULL )
|
2022-12-19 15:22:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// try to find this directory in new tree
|
2022-12-26 12:39:51 +01:00
|
|
|
j = FS_FindDirEntry( &temp, oldentry->name );
|
2022-12-19 15:22:30 +01:00
|
|
|
|
|
|
|
// not found, free memory
|
|
|
|
if( j < 0 )
|
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
FS_FreeDirEntries( oldentry );
|
2022-12-19 15:22:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found directory, move all entries
|
2022-12-26 12:39:51 +01:00
|
|
|
newentry = &temp.entries[j];
|
|
|
|
|
|
|
|
newentry->numentries = oldentry->numentries;
|
|
|
|
newentry->entries = oldentry->entries;
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we can free old tree and replace it with temporary
|
2023-01-02 22:24:57 +01:00
|
|
|
// do not add null check there! If we hit it, it's probably a logic error!
|
2022-12-19 15:22:30 +01:00
|
|
|
Mem_Free( dir->entries );
|
|
|
|
dir->numentries = temp.numentries;
|
|
|
|
dir->entries = temp.entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FS_MaybeUpdateDirEntries( dir_t *dir, const char *path, const char *entryname )
|
|
|
|
{
|
|
|
|
stringlist_t list;
|
2022-12-26 12:39:51 +01:00
|
|
|
int ret;
|
2022-12-19 15:22:30 +01:00
|
|
|
|
|
|
|
stringlistinit( &list );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &list, path );
|
2022-12-19 15:22:30 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( list.numstrings == 0 ) // empty directory
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
FS_FreeDirEntries( dir );
|
|
|
|
dir->numentries = DIRENTRY_EMPTY_DIRECTORY;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2023-01-02 22:24:57 +01:00
|
|
|
else if( dir->numentries <= DIRENTRY_EMPTY_DIRECTORY ) // not initialized or was empty
|
2022-12-26 12:39:51 +01:00
|
|
|
{
|
|
|
|
FS_InitDirEntries( dir, &list );
|
|
|
|
ret = FS_FindDirEntry( dir, entryname );
|
|
|
|
}
|
|
|
|
else if( list.numstrings != dir->numentries ) // quick update
|
|
|
|
{
|
|
|
|
FS_MergeDirEntries( dir, &list );
|
|
|
|
ret = FS_FindDirEntry( dir, entryname );
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
// do heavy compare if directory now have an entry we need
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < list.numstrings; i++ )
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !Q_stricmp( list.strings[i], entryname ))
|
2022-12-19 15:22:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( i != list.numstrings )
|
|
|
|
{
|
|
|
|
FS_MergeDirEntries( dir, &list );
|
|
|
|
ret = FS_FindDirEntry( dir, entryname );
|
|
|
|
}
|
|
|
|
else ret = -1;
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stringlistfreecontents( &list );
|
2022-12-26 12:39:51 +01:00
|
|
|
return ret;
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
static inline qboolean FS_AppendToPath( char *dst, size_t *pi, const size_t len, const char *src, const char *path, const char *err )
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
size_t i = *pi;
|
2022-12-19 15:22:30 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
i += Q_strncpy( &dst[i], src, len - i );
|
|
|
|
*pi = i;
|
|
|
|
|
|
|
|
if( i >= len )
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2024-06-19 05:46:08 +02:00
|
|
|
Con_Printf( S_ERROR "%s: overflow while appending %s (%s)\n", __func__, path, err );
|
2022-12-26 12:39:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean FS_FixFileCase( dir_t *dir, const char *path, char *dst, const size_t len, qboolean createpath )
|
|
|
|
{
|
2023-01-04 16:09:00 +01:00
|
|
|
const char *prev;
|
|
|
|
const char *next;
|
2022-12-26 12:39:51 +01:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if( !FS_AppendToPath( dst, &i, len, dir->name, path, "init" ))
|
|
|
|
return false;
|
|
|
|
|
2023-06-08 21:58:12 +02:00
|
|
|
// nothing to fix
|
|
|
|
if( !COM_CheckStringEmpty( path ))
|
|
|
|
return true;
|
|
|
|
|
2023-01-04 20:34:45 +01:00
|
|
|
for( prev = path, next = Q_strchrnul( prev, '/' );
|
2022-12-26 12:39:51 +01:00
|
|
|
;
|
2023-01-04 20:34:45 +01:00
|
|
|
prev = next + 1, next = Q_strchrnul( prev, '/' ))
|
2022-12-26 12:39:51 +01:00
|
|
|
{
|
|
|
|
qboolean uptodate = false; // do not run second scan if we're just updated our directory list
|
|
|
|
size_t temp;
|
2022-12-19 15:22:30 +01:00
|
|
|
char entryname[MAX_SYSPATH];
|
|
|
|
int ret;
|
|
|
|
|
2023-01-02 23:15:24 +01:00
|
|
|
if( dir->numentries == DIRENTRY_NOT_SCANNED )
|
|
|
|
{
|
|
|
|
// read directory first time
|
|
|
|
FS_PopulateDirEntries( dir, dst );
|
|
|
|
uptodate = true;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
// this subdirectory is case insensitive, just slam everything that's left
|
|
|
|
if( dir->numentries == DIRENTRY_CASEINSENSITIVE )
|
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !FS_AppendToPath( dst, &i, len, prev, path, "caseinsensitive entry" ))
|
2022-12-19 15:22:30 +01:00
|
|
|
return false;
|
2023-01-04 16:09:00 +01:00
|
|
|
|
2023-01-04 20:34:45 +01:00
|
|
|
// check file existense
|
|
|
|
return createpath ? true : FS_SysFileOrFolderExists( dst );
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get our entry name
|
|
|
|
Q_strncpy( entryname, prev, next - prev + 1 );
|
|
|
|
|
|
|
|
// didn't found, but does it exists in FS?
|
2022-12-26 12:39:51 +01:00
|
|
|
if(( ret = FS_FindDirEntry( dir, entryname )) < 0 )
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
// if we're creating files or folders, we don't care if path doesn't exist
|
|
|
|
// so copy everything that's left and exit without an error
|
|
|
|
if( uptodate || ( ret = FS_MaybeUpdateDirEntries( dir, dst, entryname )) < 0 )
|
|
|
|
return createpath ? FS_AppendToPath( dst, &i, len, prev, path, "create path" ) : false;
|
2022-12-19 15:22:30 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
uptodate = true;
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dir = &dir->entries[ret];
|
2022-12-26 12:39:51 +01:00
|
|
|
temp = i;
|
|
|
|
if( !FS_AppendToPath( dst, &temp, len, dir->name, path, "case fix" ))
|
2022-12-19 15:22:30 +01:00
|
|
|
return false;
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !uptodate && !FS_SysFileOrFolderExists( dst )) // file not found, rescan...
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
dst[i] = 0; // strip failed part
|
2022-12-21 05:34:31 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
// if we're creating files or folders, we don't care if path doesn't exist
|
|
|
|
// so copy everything that's left and exit without an error
|
|
|
|
if(( ret = FS_MaybeUpdateDirEntries( dir, dst, entryname )) < 0 )
|
|
|
|
return createpath ? FS_AppendToPath( dst, &i, len, prev, path, "create path rescan" ) : false;
|
2022-12-21 05:34:31 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
dir = &dir->entries[ret];
|
|
|
|
if( !FS_AppendToPath( dst, &temp, len, dir->name, path, "case fix rescan" ))
|
2022-12-21 05:34:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-26 12:39:51 +01:00
|
|
|
i = temp;
|
2022-12-21 05:34:31 +01:00
|
|
|
|
|
|
|
// end of string, found file, return
|
2023-01-04 20:34:45 +01:00
|
|
|
if( next[0] == '\0' || ( next[0] == '/' && next[1] == '\0' ))
|
2022-12-21 05:34:31 +01:00
|
|
|
break;
|
|
|
|
|
2023-01-04 20:34:45 +01:00
|
|
|
if( !FS_AppendToPath( dst, &i, len, "/", path, "path separator" ))
|
2022-12-21 05:34:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-19 15:22:30 +01:00
|
|
|
|
|
|
|
static void FS_Close_DIR( searchpath_t *search )
|
|
|
|
{
|
|
|
|
FS_FreeDirEntries( search->dir );
|
|
|
|
Mem_Free( search->dir );
|
2022-12-14 22:52:09 +01:00
|
|
|
}
|
2022-11-18 14:35:21 +01:00
|
|
|
|
2022-12-14 22:52:09 +01:00
|
|
|
static void FS_PrintInfo_DIR( searchpath_t *search, char *dst, size_t size )
|
2022-11-18 14:35:21 +01:00
|
|
|
{
|
|
|
|
Q_strncpy( dst, search->filename, size );
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
static int FS_FindFile_DIR( searchpath_t *search, const char *path, char *fixedname, size_t len )
|
2022-11-18 14:35:21 +01:00
|
|
|
{
|
|
|
|
char netpath[MAX_SYSPATH];
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
if( !FS_FixFileCase( search->dir, path, netpath, sizeof( netpath ), false ))
|
|
|
|
return -1;
|
2022-11-18 14:35:21 +01:00
|
|
|
|
2023-01-04 16:09:00 +01:00
|
|
|
if( FS_SysFileExists( netpath ))
|
2022-12-19 15:22:30 +01:00
|
|
|
{
|
|
|
|
// return fixed case file name only local for that searchpath
|
|
|
|
if( fixedname )
|
|
|
|
Q_strncpy( fixedname, netpath + Q_strlen( search->filename ), len );
|
2022-11-18 14:35:21 +01:00
|
|
|
return 0;
|
2022-12-19 15:22:30 +01:00
|
|
|
}
|
2022-11-18 14:35:21 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:52:09 +01:00
|
|
|
static void FS_Search_DIR( searchpath_t *search, stringlist_t *list, const char *pattern, int caseinsensitive )
|
2022-11-18 14:35:21 +01:00
|
|
|
{
|
|
|
|
string netpath, temp;
|
|
|
|
stringlist_t dirlist;
|
|
|
|
const char *slash, *backslash, *colon, *separator;
|
|
|
|
int basepathlength, dirlistindex, resultlistindex;
|
|
|
|
char *basepath;
|
|
|
|
|
|
|
|
slash = Q_strrchr( pattern, '/' );
|
|
|
|
backslash = Q_strrchr( pattern, '\\' );
|
|
|
|
colon = Q_strrchr( pattern, ':' );
|
2022-11-21 11:38:10 +01:00
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
separator = Q_max( slash, backslash );
|
|
|
|
separator = Q_max( separator, colon );
|
2022-12-19 15:22:30 +01:00
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
basepathlength = separator ? (separator + 1 - pattern) : 0;
|
|
|
|
basepath = Mem_Calloc( fs_mempool, basepathlength + 1 );
|
|
|
|
if( basepathlength ) memcpy( basepath, pattern, basepathlength );
|
2022-11-21 11:38:10 +01:00
|
|
|
basepath[basepathlength] = '\0';
|
2022-11-18 14:35:21 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !FS_FixFileCase( search->dir, basepath, netpath, sizeof( netpath ), false ))
|
|
|
|
{
|
|
|
|
Mem_Free( basepath );
|
|
|
|
return;
|
|
|
|
}
|
2022-11-18 14:35:21 +01:00
|
|
|
|
|
|
|
stringlistinit( &dirlist );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &dirlist, netpath );
|
2022-11-18 14:35:21 +01:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
Q_strncpy( temp, basepath, sizeof( temp ));
|
2022-11-21 11:38:10 +01:00
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
for( dirlistindex = 0; dirlistindex < dirlist.numstrings; dirlistindex++ )
|
|
|
|
{
|
2022-11-21 13:11:55 +01:00
|
|
|
Q_strncpy( &temp[basepathlength], dirlist.strings[dirlistindex], sizeof( temp ) - basepathlength );
|
2022-11-18 14:35:21 +01:00
|
|
|
|
|
|
|
if( matchpattern( temp, (char *)pattern, true ) )
|
|
|
|
{
|
|
|
|
for( resultlistindex = 0; resultlistindex < list->numstrings; resultlistindex++ )
|
|
|
|
{
|
|
|
|
if( !Q_strcmp( list->strings[resultlistindex], temp ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( resultlistindex == list->numstrings )
|
|
|
|
stringlistappend( list, temp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stringlistfreecontents( &dirlist );
|
|
|
|
|
|
|
|
Mem_Free( basepath );
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:52:09 +01:00
|
|
|
static int FS_FileTime_DIR( searchpath_t *search, const char *filename )
|
2022-11-18 14:35:21 +01:00
|
|
|
{
|
|
|
|
char path[MAX_SYSPATH];
|
|
|
|
|
2022-11-21 13:11:55 +01:00
|
|
|
Q_snprintf( path, sizeof( path ), "%s%s", search->filename, filename );
|
2022-11-18 14:35:21 +01:00
|
|
|
return FS_SysFileTime( path );
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:52:09 +01:00
|
|
|
static file_t *FS_OpenFile_DIR( searchpath_t *search, const char *filename, const char *mode, int pack_ind )
|
2022-11-18 14:35:21 +01:00
|
|
|
{
|
2024-06-27 06:31:09 +02:00
|
|
|
file_t *f;
|
2022-11-18 14:35:21 +01:00
|
|
|
char path[MAX_SYSPATH];
|
|
|
|
|
2022-11-21 13:11:55 +01:00
|
|
|
Q_snprintf( path, sizeof( path ), "%s%s", search->filename, filename );
|
2024-06-27 06:31:09 +02:00
|
|
|
f = FS_SysOpen( path, mode );
|
|
|
|
f->searchpath = search;
|
|
|
|
|
|
|
|
return f;
|
2022-12-14 22:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FS_InitDirectorySearchpath( searchpath_t *search, const char *path, int flags )
|
|
|
|
{
|
|
|
|
memset( search, 0, sizeof( searchpath_t ));
|
|
|
|
|
2023-06-08 21:58:12 +02:00
|
|
|
Q_strncpy( search->filename, path, sizeof( search->filename ) - 1 );
|
|
|
|
COM_PathSlashFix( search->filename );
|
|
|
|
|
|
|
|
if( !Q_stricmp( COM_FileExtension( path ), "pk3dir" ))
|
|
|
|
search->type = SEARCHPATH_PK3DIR;
|
|
|
|
else search->type = SEARCHPATH_PLAIN;
|
2022-12-14 22:52:09 +01:00
|
|
|
search->flags = flags;
|
2022-12-14 23:06:20 +01:00
|
|
|
search->pfnPrintInfo = FS_PrintInfo_DIR;
|
|
|
|
search->pfnClose = FS_Close_DIR;
|
|
|
|
search->pfnOpenFile = FS_OpenFile_DIR;
|
|
|
|
search->pfnFileTime = FS_FileTime_DIR;
|
|
|
|
search->pfnFindFile = FS_FindFile_DIR;
|
|
|
|
search->pfnSearch = FS_Search_DIR;
|
2022-12-19 15:22:30 +01:00
|
|
|
|
|
|
|
// create cache root
|
|
|
|
search->dir = Mem_Malloc( fs_mempool, sizeof( dir_t ));
|
2022-12-26 12:39:51 +01:00
|
|
|
Q_strncpy( search->dir->name, search->filename, sizeof( search->dir->name ));
|
2022-12-19 15:22:30 +01:00
|
|
|
FS_PopulateDirEntries( search->dir, path );
|
2022-12-14 22:52:09 +01:00
|
|
|
}
|
|
|
|
|
2023-06-08 21:14:12 +02:00
|
|
|
searchpath_t *FS_AddDir_Fullpath( const char *path, int flags )
|
2022-12-14 22:52:09 +01:00
|
|
|
{
|
|
|
|
searchpath_t *search;
|
|
|
|
|
|
|
|
search = (searchpath_t *)Mem_Calloc( fs_mempool, sizeof( searchpath_t ));
|
|
|
|
FS_InitDirectorySearchpath( search, path, flags );
|
|
|
|
Con_Printf( "Adding directory: %s\n", path );
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
return search;
|
2022-12-14 22:52:09 +01:00
|
|
|
}
|