Generalize FilenameDB.
Automatically create directories.
This commit is contained in:
@@ -6,8 +6,13 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <RageLog.h>
|
||||||
|
|
||||||
#if defined(WIN32)
|
#if !defined(WIN32)
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#else
|
||||||
|
#include <windows.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -15,10 +20,101 @@
|
|||||||
#define O_BINARY 0
|
#define O_BINARY 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* XXX: Drop FileDB and cache/resolve in here. */
|
|
||||||
#include "RageUtil_FileDB.h"
|
#include "RageUtil_FileDB.h"
|
||||||
|
|
||||||
/* This driver handles direct file access. */
|
/* This driver handles direct file access. */
|
||||||
|
class DirectFilenameDB: public FilenameDB
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
virtual void PopulateFileSet( FileSet &fs, const CString &sPath );
|
||||||
|
public:
|
||||||
|
DirectFilenameDB() { ExpireSeconds = 30; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void DirectFilenameDB::PopulateFileSet( FileSet &fs, const CString &path )
|
||||||
|
{
|
||||||
|
CString sPath = path;
|
||||||
|
|
||||||
|
/* Resolve path cases (path/Path -> PATH/path). */
|
||||||
|
ResolvePath( sPath );
|
||||||
|
|
||||||
|
fs.age.GetDeltaTime(); /* reset */
|
||||||
|
fs.files.clear();
|
||||||
|
|
||||||
|
#if defined(WIN32)
|
||||||
|
WIN32_FIND_DATA fd;
|
||||||
|
|
||||||
|
if ( sPath.size() > 0 && sPath.Right(1) == SLASH )
|
||||||
|
sPath.erase( sPath.size() - 1 );
|
||||||
|
|
||||||
|
HANDLE hFind = FindFirstFile( sPath+SLASH "*", &fd );
|
||||||
|
|
||||||
|
if( hFind == INVALID_HANDLE_VALUE )
|
||||||
|
return;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if(!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
File f;
|
||||||
|
f.SetName( fd.cFileName );
|
||||||
|
f.dir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
||||||
|
f.size = fd.nFileSizeLow;
|
||||||
|
f.mtime = fd.ftLastWriteTime.dwLowDateTime;
|
||||||
|
|
||||||
|
fs.files.insert(f);
|
||||||
|
} while( FindNextFile( hFind, &fd ) );
|
||||||
|
FindClose(hFind);
|
||||||
|
#else
|
||||||
|
int OldDir = open(".", O_RDONLY);
|
||||||
|
if( OldDir == -1 )
|
||||||
|
RageException::Throw( "Couldn't open(.): %s", strerror(errno) );
|
||||||
|
|
||||||
|
if( chdir(sPath) == -1 )
|
||||||
|
{
|
||||||
|
/* Only log once per dir. */
|
||||||
|
if( LOG )
|
||||||
|
LOG->MapLog("chdir " + sPath, "Couldn't chdir(%s): %s", sPath.c_str(), strerror(errno) );
|
||||||
|
close( OldDir );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DIR *d = opendir(".");
|
||||||
|
|
||||||
|
while(struct dirent *ent = readdir(d))
|
||||||
|
{
|
||||||
|
if(!strcmp(ent->d_name, ".")) continue;
|
||||||
|
if(!strcmp(ent->d_name, "..")) continue;
|
||||||
|
|
||||||
|
File f;
|
||||||
|
f.SetName( ent->d_name );
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
if( stat(ent->d_name, &st) == -1 )
|
||||||
|
{
|
||||||
|
/* If it's a broken symlink, ignore it. Otherwise, warn. */
|
||||||
|
if( lstat(ent->d_name, &st) == 0 )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* Huh? */
|
||||||
|
if(LOG)
|
||||||
|
LOG->Warn("Got file '%s' in '%s' from list, but can't stat? (%s)",
|
||||||
|
ent->d_name, sPath.c_str(), strerror(errno));
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
f.dir = (st.st_mode & S_IFDIR);
|
||||||
|
f.size = st.st_size;
|
||||||
|
f.mtime = st.st_mtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.files.insert(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(d);
|
||||||
|
if( fchdir( OldDir ) == -1 )
|
||||||
|
RageException::Throw( "Couldn't fchdir(): %s", strerror(errno) );
|
||||||
|
close( OldDir );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
class RageFileObjDirect: public RageFileObj
|
class RageFileObjDirect: public RageFileObj
|
||||||
{
|
{
|
||||||
@@ -40,16 +136,21 @@ public:
|
|||||||
RageFileDriverDirect::RageFileDriverDirect( CString root_ ):
|
RageFileDriverDirect::RageFileDriverDirect( CString root_ ):
|
||||||
root(root_)
|
root(root_)
|
||||||
{
|
{
|
||||||
|
FDB = new DirectFilenameDB;
|
||||||
|
|
||||||
if( root.Right(1) != "/" )
|
if( root.Right(1) != "/" )
|
||||||
root += '/';
|
root += '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
void FDB_GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
|
RageFileDriverDirect::~RageFileDriverDirect()
|
||||||
|
{
|
||||||
|
delete FDB;
|
||||||
|
}
|
||||||
|
|
||||||
void RageFileDriverDirect::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
void RageFileDriverDirect::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
||||||
{
|
{
|
||||||
const unsigned OldStart = AddTo.size();
|
const unsigned OldStart = AddTo.size();
|
||||||
FDB_GetDirListing( root+sPath, AddTo, bOnlyDirs, bReturnPathToo );
|
FDB->GetDirListing( root+sPath, AddTo, bOnlyDirs, bReturnPathToo );
|
||||||
|
|
||||||
if( bReturnPathToo )
|
if( bReturnPathToo )
|
||||||
{
|
{
|
||||||
@@ -59,15 +160,70 @@ void RageFileDriverDirect::GetDirListing( CString sPath, CStringArray &AddTo, bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* mkdir -p. Doesn't fail if Path already exists and is a directory. */
|
||||||
|
static bool CreateDirectories( CString Path )
|
||||||
|
{
|
||||||
|
CStringArray parts;
|
||||||
|
CString curpath;
|
||||||
|
split(Path, SLASH, parts);
|
||||||
|
|
||||||
|
for(unsigned i = 0; i < parts.size(); ++i)
|
||||||
|
{
|
||||||
|
curpath += parts[i] + SLASH;
|
||||||
|
if( mkdir(curpath, 0755) == 0 )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(errno == EEXIST)
|
||||||
|
continue; // we expect to see this error
|
||||||
|
|
||||||
|
// Log the error, but continue on.
|
||||||
|
/* When creating a directory that already exists over Samba, Windows is
|
||||||
|
* returning ENOENT instead of EEXIST. */
|
||||||
|
/* On Win32 when Path is only a drive letter (e.g. "i:\"), the result is
|
||||||
|
* EINVAL. */
|
||||||
|
if( LOG )
|
||||||
|
LOG->Warn("Couldn't create %s: %s", curpath.c_str(), strerror(errno) );
|
||||||
|
|
||||||
|
/* Make sure it's a directory. */
|
||||||
|
FlushDirCache();
|
||||||
|
if( !IsADirectory(curpath) )
|
||||||
|
{
|
||||||
|
if( LOG )
|
||||||
|
LOG->Warn("Couldn't create %s: path exists and is not a directory", curpath.c_str() );
|
||||||
|
|
||||||
|
// HACK: IsADirectory doesn't work if Path contains a drive letter.
|
||||||
|
// So, ignore IsADirectory's result and continue trying to create
|
||||||
|
// directories anyway. This shouldn't change behavior, but
|
||||||
|
// is inefficient because we don't bail early on an error.
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
RageFileObj *RageFileDriverDirect::Open( CString sPath, RageFile::OpenMode mode, RageFile &p, int &err )
|
RageFileObj *RageFileDriverDirect::Open( CString sPath, RageFile::OpenMode mode, RageFile &p, int &err )
|
||||||
{
|
{
|
||||||
sPath = root + sPath;
|
sPath = root + sPath;
|
||||||
ResolvePath( sPath );
|
/* XXX: make sure this will partially resolve. eg. if "abc/def" exists,
|
||||||
|
* and we're opening "ABC/DEF/GHI/jkl/mno", make sure this will resolve
|
||||||
|
* to "abc/def/GHI/jkl/mno"; we'll create the missing ones later. */
|
||||||
|
FDB->ResolvePath( sPath );
|
||||||
|
|
||||||
int flags = O_BINARY;
|
int flags = O_BINARY;
|
||||||
if( mode == RageFile::READ )
|
if( mode == RageFile::READ )
|
||||||
flags |= O_RDONLY;
|
flags |= O_RDONLY;
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
CString dir = Dirname(sPath);
|
||||||
|
if( this->GetFileType(dir) != RageFileManager::TYPE_DIR )
|
||||||
|
CreateDirectories( dir );
|
||||||
flags |= O_WRONLY|O_CREAT|O_TRUNC;
|
flags |= O_WRONLY|O_CREAT|O_TRUNC;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(XBOX)
|
||||||
|
sPath.Replace( "/", "\\" );
|
||||||
|
#endif
|
||||||
|
|
||||||
int fd = open( sPath, flags, 0644 );
|
int fd = open( sPath, flags, 0644 );
|
||||||
if( fd == -1 )
|
if( fd == -1 )
|
||||||
@@ -79,48 +235,20 @@ RageFileObj *RageFileDriverDirect::Open( CString sPath, RageFile::OpenMode mode,
|
|||||||
return new RageFileObjDirect( fd, p );
|
return new RageFileObjDirect( fd, p );
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool DoStat(CString sPath, struct stat *st)
|
|
||||||
{
|
|
||||||
TrimRight(sPath, "/\\");
|
|
||||||
return stat(sPath.c_str(), st) != -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
RageFileManager::FileType RageFileDriverDirect::GetFileType( CString sPath )
|
RageFileManager::FileType RageFileDriverDirect::GetFileType( CString sPath )
|
||||||
{
|
{
|
||||||
sPath = root + sPath;
|
/* XXX */
|
||||||
|
return (RageFileManager::FileType) FDB->GetFileType( root + sPath );
|
||||||
ResolvePath( sPath );
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if( !DoStat(sPath, &st) )
|
|
||||||
return RageFileManager::TYPE_NONE;
|
|
||||||
|
|
||||||
if( st.st_mode & S_IFDIR )
|
|
||||||
return RageFileManager::TYPE_DIR;
|
|
||||||
|
|
||||||
return RageFileManager::TYPE_FILE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int RageFileDriverDirect::GetFileSizeInBytes( CString sPath )
|
int RageFileDriverDirect::GetFileSizeInBytes( CString sPath )
|
||||||
{
|
{
|
||||||
sPath = root + sPath;
|
return FDB->GetFileSize( root + sPath );
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if( !DoStat(sPath, &st) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return st.st_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int RageFileDriverDirect::GetFileModTime( CString sPath )
|
int RageFileDriverDirect::GetFileModTime( CString sPath )
|
||||||
{
|
{
|
||||||
sPath = root + sPath;
|
return FDB->GetFileModTime( root + sPath );
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if( !DoStat(sPath, &st) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return st.st_mtime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
@@ -173,6 +301,12 @@ bool RageFileDriverDirect::Ready()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RageFileDriverDirect::FlushDirCache( const CString &sPath )
|
||||||
|
{
|
||||||
|
FDB->FlushDirCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
RageFileObjDirect::RageFileObjDirect( int fd_, RageFile &p ):
|
RageFileObjDirect::RageFileObjDirect( int fd_, RageFile &p ):
|
||||||
RageFileObj( p )
|
RageFileObj( p )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,11 +3,12 @@
|
|||||||
|
|
||||||
#include "RageFileDriver.h"
|
#include "RageFileDriver.h"
|
||||||
|
|
||||||
|
class FilenameDB;
|
||||||
class RageFileDriverDirect: public RageFileDriver
|
class RageFileDriverDirect: public RageFileDriver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RageFileDriverDirect( CString root );
|
RageFileDriverDirect( CString root );
|
||||||
virtual ~RageFileDriverDirect() { }
|
virtual ~RageFileDriverDirect();
|
||||||
|
|
||||||
RageFileObj *Open( CString path, RageFile::OpenMode mode, RageFile &p, int &err );
|
RageFileObj *Open( CString path, RageFile::OpenMode mode, RageFile &p, int &err );
|
||||||
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
|
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
|
||||||
@@ -15,9 +16,11 @@ public:
|
|||||||
int GetFileSizeInBytes( CString sFilePath );
|
int GetFileSizeInBytes( CString sFilePath );
|
||||||
int GetFileModTime( CString sPath );
|
int GetFileModTime( CString sPath );
|
||||||
bool Ready();
|
bool Ready();
|
||||||
|
void FlushDirCache( const CString &sPath );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CString root;
|
CString root;
|
||||||
|
FilenameDB *FDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,161 +2,9 @@
|
|||||||
|
|
||||||
#include "RageUtil_FileDB.h"
|
#include "RageUtil_FileDB.h"
|
||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
#include "RageTimer.h"
|
|
||||||
#include "RageLog.h"
|
#include "RageLog.h"
|
||||||
#include <map>
|
|
||||||
#include <set>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include "RageFile.h"
|
|
||||||
#include "arch/arch.h"
|
#include "arch/arch.h"
|
||||||
|
|
||||||
#if !defined(WIN32)
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#else
|
|
||||||
#include "windows.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum FileType { TTYPE_FILE, TTYPE_DIR, TTYPE_NONE };
|
|
||||||
|
|
||||||
struct File
|
|
||||||
{
|
|
||||||
CString name;
|
|
||||||
CString lname;
|
|
||||||
|
|
||||||
void SetName( const CString &fn )
|
|
||||||
{
|
|
||||||
name = fn;
|
|
||||||
lname = name;
|
|
||||||
lname.MakeLower();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool dir;
|
|
||||||
int size;
|
|
||||||
/* Modification time of the file. The contents of this is undefined, except that
|
|
||||||
* when the file has been modified, this value will change. */
|
|
||||||
int mtime;
|
|
||||||
|
|
||||||
File() { dir=false; size=-1; mtime=-1; }
|
|
||||||
File( const CString &fn )
|
|
||||||
{
|
|
||||||
SetName( fn );
|
|
||||||
dir=false; size=-1; mtime=-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator== (const File &rhs) const { return lname==rhs.lname; }
|
|
||||||
bool operator< (const File &rhs) const { return lname<rhs.lname; }
|
|
||||||
|
|
||||||
bool equal(const File &rhs) const { return lname == rhs.lname; }
|
|
||||||
bool equal(const CString &rhs) const
|
|
||||||
{
|
|
||||||
CString l = rhs;
|
|
||||||
l.MakeLower();
|
|
||||||
return lname == l;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* This represents a directory. */
|
|
||||||
struct FileSet
|
|
||||||
{
|
|
||||||
set<File> files;
|
|
||||||
RageTimer age;
|
|
||||||
void LoadFromDir(const CString &dir);
|
|
||||||
void GetFilesMatching(
|
|
||||||
const CString &beginning, const CString &containing, const CString &ending,
|
|
||||||
vector<CString> &out, bool bOnlyDirs) const;
|
|
||||||
void GetFilesEqualTo(const CString &pat, vector<CString> &out, bool bOnlyDirs) const;
|
|
||||||
|
|
||||||
FileType GetFileType( const CString &path ) const;
|
|
||||||
int GetFileSize(const CString &path) const;
|
|
||||||
int GetFileModTime(const CString &path) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
void FileSet::LoadFromDir(const CString &dir)
|
|
||||||
{
|
|
||||||
age.GetDeltaTime(); /* reset */
|
|
||||||
files.clear();
|
|
||||||
|
|
||||||
#if defined(WIN32)
|
|
||||||
WIN32_FIND_DATA fd;
|
|
||||||
CString dirHolder = dir ;
|
|
||||||
|
|
||||||
if ( dirHolder.size() > 0 && dirHolder.Right(1) == SLASH )
|
|
||||||
{
|
|
||||||
dirHolder.erase( dirHolder.size() - 1 ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
HANDLE hFind = FindFirstFile( dirHolder+SLASH "*", &fd );
|
|
||||||
|
|
||||||
if( hFind == INVALID_HANDLE_VALUE )
|
|
||||||
return;
|
|
||||||
|
|
||||||
do {
|
|
||||||
if(!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
File f;
|
|
||||||
f.SetName( fd.cFileName );
|
|
||||||
f.dir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
||||||
f.size = fd.nFileSizeLow;
|
|
||||||
f.mtime = fd.ftLastWriteTime.dwLowDateTime;
|
|
||||||
|
|
||||||
files.insert(f);
|
|
||||||
} while( FindNextFile( hFind, &fd ) );
|
|
||||||
FindClose(hFind);
|
|
||||||
#else
|
|
||||||
int OldDir = open(".", O_RDONLY);
|
|
||||||
if( OldDir == -1 )
|
|
||||||
RageException::Throw( "Couldn't open(.): %s", strerror(errno) );
|
|
||||||
|
|
||||||
if( chdir(dir) == -1 )
|
|
||||||
{
|
|
||||||
/* Only log once per dir. */
|
|
||||||
if( LOG )
|
|
||||||
LOG->MapLog("chdir " + dir, "Couldn't chdir(%s): %s", dir.c_str(), strerror(errno) );
|
|
||||||
close( OldDir );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DIR *d = opendir(".");
|
|
||||||
|
|
||||||
while(struct dirent *ent = readdir(d))
|
|
||||||
{
|
|
||||||
if(!strcmp(ent->d_name, ".")) continue;
|
|
||||||
if(!strcmp(ent->d_name, "..")) continue;
|
|
||||||
|
|
||||||
File f;
|
|
||||||
f.SetName( ent->d_name );
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if( stat(ent->d_name, &st) == -1 )
|
|
||||||
{
|
|
||||||
/* If it's a broken symlink, ignore it. Otherwise, warn. */
|
|
||||||
if( lstat(ent->d_name, &st) == 0 )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* Huh? */
|
|
||||||
if(LOG)
|
|
||||||
LOG->Warn("Got file '%s' in '%s' from list, but can't stat? (%s)",
|
|
||||||
ent->d_name, dir.c_str(), strerror(errno));
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
f.dir = (st.st_mode & S_IFDIR);
|
|
||||||
f.size = st.st_size;
|
|
||||||
f.mtime = st.st_mtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
files.insert(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir(d);
|
|
||||||
if( fchdir( OldDir ) == -1 )
|
|
||||||
RageException::Throw( "Couldn't fchdir(): %s", strerror(errno) );
|
|
||||||
close( OldDir );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Search for "beginning*containing*ending". */
|
/* Search for "beginning*containing*ending". */
|
||||||
void FileSet::GetFilesMatching(const CString &beginning, const CString &containing, const CString &ending, vector<CString> &out, bool bOnlyDirs) const
|
void FileSet::GetFilesMatching(const CString &beginning, const CString &containing, const CString &ending, vector<CString> &out, bool bOnlyDirs) const
|
||||||
{
|
{
|
||||||
@@ -236,11 +84,7 @@ static void SplitPath( CString Path, CString &Dir, CString &Name )
|
|||||||
if( Path.size() > 0 && Path.Right(1) == SLASH )
|
if( Path.size() > 0 && Path.Right(1) == SLASH )
|
||||||
Path.erase( Path.size()-1 );
|
Path.erase( Path.size()-1 );
|
||||||
|
|
||||||
#ifdef _XBOX
|
|
||||||
static Regex split("(.*\\\\)([^\\\\]+)");
|
|
||||||
#else
|
|
||||||
static Regex split("(.*/)([^/]+)");
|
static Regex split("(.*/)([^/]+)");
|
||||||
#endif
|
|
||||||
|
|
||||||
CStringArray match;
|
CStringArray match;
|
||||||
if(split.Compare(Path, match)) {
|
if(split.Compare(Path, match)) {
|
||||||
@@ -249,47 +93,13 @@ static void SplitPath( CString Path, CString &Dir, CString &Name )
|
|||||||
Name = match[1];
|
Name = match[1];
|
||||||
} else {
|
} else {
|
||||||
/* No slash. */
|
/* No slash. */
|
||||||
#ifdef _XBOX
|
|
||||||
Dir = "D:\\" ;
|
|
||||||
Name = "" ;
|
|
||||||
#else
|
|
||||||
Dir = "." SLASH;
|
Dir = "." SLASH;
|
||||||
Name = Path;
|
Name = Path;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FilenameDB
|
|
||||||
{
|
|
||||||
FileSet &GetFileSet( CString dir );
|
|
||||||
|
|
||||||
/* Directories we have cached: */
|
|
||||||
map<CString, FileSet *> dirs;
|
|
||||||
|
|
||||||
void GetFilesEqualTo(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
|
|
||||||
void GetFilesMatching(const CString &dir,
|
|
||||||
const CString &beginning, const CString &containing, const CString &ending,
|
|
||||||
vector<CString> &out, bool bOnlyDirs);
|
|
||||||
|
|
||||||
public:
|
|
||||||
/* This handles at most one * wildcard. If we need anything more complicated,
|
|
||||||
* we'll need to use fnmatch or regex. */
|
|
||||||
void GetFilesSimpleMatch(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
|
|
||||||
|
|
||||||
/* Search for "path" case-insensitively and replace it with the correct
|
|
||||||
* case. If "path" doesn't exist at all, return false and don't change it. */
|
|
||||||
bool ResolvePath(CString &path);
|
|
||||||
|
|
||||||
FileType GetFileType( const CString &path );
|
|
||||||
int GetFileSize(const CString &path);
|
|
||||||
int GetFileModTime( const CString &sFilePath );
|
|
||||||
|
|
||||||
void FlushDirCache();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
FileType FilenameDB::GetFileType( const CString &sPath )
|
FileType FilenameDB::GetFileType( const CString &sPath )
|
||||||
{
|
{
|
||||||
CString Dir, Name;
|
CString Dir, Name;
|
||||||
@@ -399,57 +209,43 @@ FileSet &FilenameDB::GetFileSet( CString dir )
|
|||||||
{
|
{
|
||||||
/* Normalize the path. */
|
/* Normalize the path. */
|
||||||
dir.Replace("\\", SLASH); /* foo\bar -> foo/bar */
|
dir.Replace("\\", SLASH); /* foo\bar -> foo/bar */
|
||||||
dir.Replace("/", SLASH); /* foo//bar -> foo/bar */
|
|
||||||
dir.Replace("//", SLASH); /* foo//bar -> foo/bar */
|
dir.Replace("//", SLASH); /* foo//bar -> foo/bar */
|
||||||
|
|
||||||
CString lower = dir;
|
CString lower = dir;
|
||||||
lower.MakeLower();
|
lower.MakeLower();
|
||||||
|
|
||||||
FileSet *ret;
|
|
||||||
map<CString, FileSet *>::iterator i = dirs.find( lower );
|
map<CString, FileSet *>::iterator i = dirs.find( lower );
|
||||||
bool reload = false;
|
if( ExpireSeconds != -1 && i != dirs.end() && i->second->age.PeekDeltaTime() >= ExpireSeconds )
|
||||||
if(i == dirs.end())
|
|
||||||
{
|
{
|
||||||
ret = new FileSet;
|
delete i->second;
|
||||||
dirs[lower] = ret;
|
i = dirs.end();
|
||||||
reload = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret = i->second;
|
|
||||||
if(ret->age.PeekDeltaTime() > 30)
|
|
||||||
reload = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(reload)
|
if( i != dirs.end() )
|
||||||
{
|
return *i->second;
|
||||||
CString RealDir = dir;
|
|
||||||
|
|
||||||
/* Resolve path cases (path/Path -> PATH/path). */
|
FileSet *ret = new FileSet;
|
||||||
ResolvePath( RealDir );
|
PopulateFileSet( *ret, dir );
|
||||||
|
AddFileSet( dir, ret );
|
||||||
ret->LoadFromDir(RealDir);
|
|
||||||
}
|
|
||||||
return *ret;
|
return *ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FilenameDB::AddFileSet( CString sPath, FileSet *fs )
|
||||||
|
{
|
||||||
|
sPath.MakeLower();
|
||||||
|
dirs[sPath] = fs;
|
||||||
|
}
|
||||||
|
|
||||||
void FilenameDB::FlushDirCache()
|
void FilenameDB::FlushDirCache()
|
||||||
{
|
{
|
||||||
set<FileSet *> freed;
|
|
||||||
for( map<CString, FileSet *>::iterator i = dirs.begin(); i != dirs.end(); ++i )
|
for( map<CString, FileSet *>::iterator i = dirs.begin(); i != dirs.end(); ++i )
|
||||||
{
|
|
||||||
if( freed.find(i->second) != freed.end() )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
delete i->second;
|
delete i->second;
|
||||||
freed.insert( i->second );
|
|
||||||
}
|
|
||||||
dirs.clear();
|
dirs.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FilenameDB FDB;
|
|
||||||
|
|
||||||
void FDB_GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
void FilenameDB::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
||||||
{
|
{
|
||||||
// LOG->Trace( "GetDirListing( %s )", sPath.c_str() );
|
// LOG->Trace( "GetDirListing( %s )", sPath.c_str() );
|
||||||
|
|
||||||
@@ -475,11 +271,11 @@ void FDB_GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool
|
|||||||
fn = "*";
|
fn = "*";
|
||||||
|
|
||||||
unsigned start = AddTo.size();
|
unsigned start = AddTo.size();
|
||||||
FDB.GetFilesSimpleMatch(sPath, fn, AddTo, bOnlyDirs);
|
GetFilesSimpleMatch(sPath, fn, AddTo, bOnlyDirs);
|
||||||
|
|
||||||
if(bReturnPathToo && start < AddTo.size())
|
if(bReturnPathToo && start < AddTo.size())
|
||||||
{
|
{
|
||||||
FDB.ResolvePath(sPath);
|
ResolvePath(sPath);
|
||||||
while(start < AddTo.size())
|
while(start < AddTo.size())
|
||||||
{
|
{
|
||||||
AddTo[start] = sPath + AddTo[start];
|
AddTo[start] = sPath + AddTo[start];
|
||||||
@@ -488,12 +284,7 @@ void FDB_GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ResolvePath(CString &path) { return FDB.ResolvePath(path); }
|
bool ResolvePath(CString &path) { return true; } // XXX
|
||||||
|
|
||||||
void FlushDirCache()
|
|
||||||
{
|
|
||||||
FDB.FlushDirCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
@@ -511,83 +302,6 @@ unsigned GetFileSizeInBytes( const CString &sPath )
|
|||||||
}
|
}
|
||||||
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
||||||
{
|
{
|
||||||
FDB_GetDirListing( sPath, AddTo, bOnlyDirs, bReturnPathToo );
|
FDB.GetDirListing( sPath, AddTo, bOnlyDirs, bReturnPathToo );
|
||||||
}
|
|
||||||
#elif 0
|
|
||||||
static bool DoStat(CString sPath, struct stat *st)
|
|
||||||
{
|
|
||||||
TrimRight(sPath, "/\\");
|
|
||||||
return stat(sPath, st) != -1;
|
|
||||||
}
|
|
||||||
bool DoesFileExist( const CString &sPath )
|
|
||||||
{
|
|
||||||
if(sPath.empty()) return false;
|
|
||||||
struct stat st;
|
|
||||||
return DoStat(sPath, &st);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsAFile( const CString &sPath )
|
|
||||||
{
|
|
||||||
return DoesFileExist(sPath) && !IsADirectory(sPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsADirectory( const CString &sPath )
|
|
||||||
{
|
|
||||||
if(sPath.empty()) return false;
|
|
||||||
struct stat st;
|
|
||||||
if (!DoStat(sPath, &st))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return !!(st.st_mode & S_IFDIR);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned GetFileSizeInBytes( const CString &sFilePath )
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
if(!DoStat(sFilePath, &st))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return st.st_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetFileModTime( const CString &sPath )
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
if(!DoStat(sFilePath, &st))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return st.st_mtime;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#include "RageFileManager.h"
|
|
||||||
|
|
||||||
bool DoesFileExist( const CString &sPath )
|
|
||||||
{
|
|
||||||
return FILEMAN->DoesFileExist( sPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsAFile( const CString &sPath )
|
|
||||||
{
|
|
||||||
return FILEMAN->IsAFile( sPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsADirectory( const CString &sPath )
|
|
||||||
{
|
|
||||||
return FILEMAN->IsADirectory( sPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned GetFileSizeInBytes( const CString &sPath )
|
|
||||||
{
|
|
||||||
return FILEMAN->GetFileSizeInBytes( sPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetFileModTime( const CString &sPath )
|
|
||||||
{
|
|
||||||
return FILEMAN->GetFileModTime( sPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
|
|
||||||
{
|
|
||||||
FILEMAN->GetDirListing( sPath, AddTo, bOnlyDirs, bReturnPathToo );
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,13 +1,104 @@
|
|||||||
#ifndef RAGE_UTIL_FILEDB
|
#ifndef RAGE_UTIL_FILEDB
|
||||||
#define RAGE_UTIL_FILEDB 1
|
#define RAGE_UTIL_FILEDB 1
|
||||||
|
|
||||||
bool DoesFileExist( const CString &sPath );
|
#include <set>
|
||||||
bool IsAFile( const CString &sPath );
|
#include <map>
|
||||||
bool IsADirectory( const CString &sPath );
|
#include "RageTimer.h"
|
||||||
bool ResolvePath(CString &path);
|
|
||||||
unsigned GetFileSizeInBytes( const CString &sFilePath );
|
|
||||||
int GetFileModTime( const CString &sPath );
|
|
||||||
|
|
||||||
void FlushDirCache();
|
enum FileType { TTYPE_FILE, TTYPE_DIR, TTYPE_NONE };
|
||||||
|
|
||||||
|
struct File
|
||||||
|
{
|
||||||
|
CString name;
|
||||||
|
CString lname;
|
||||||
|
|
||||||
|
void SetName( const CString &fn )
|
||||||
|
{
|
||||||
|
name = fn;
|
||||||
|
lname = name;
|
||||||
|
lname.MakeLower();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool dir;
|
||||||
|
int size;
|
||||||
|
/* Modification time of the file. The contents of this is undefined, except that
|
||||||
|
* when the file has been modified, this value will change. */
|
||||||
|
int mtime;
|
||||||
|
|
||||||
|
File() { dir=false; size=-1; mtime=-1; }
|
||||||
|
File( const CString &fn )
|
||||||
|
{
|
||||||
|
SetName( fn );
|
||||||
|
dir=false; size=-1; mtime=-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator== (const File &rhs) const { return lname==rhs.lname; }
|
||||||
|
bool operator< (const File &rhs) const { return lname<rhs.lname; }
|
||||||
|
|
||||||
|
bool equal(const File &rhs) const { return lname == rhs.lname; }
|
||||||
|
bool equal(const CString &rhs) const
|
||||||
|
{
|
||||||
|
CString l = rhs;
|
||||||
|
l.MakeLower();
|
||||||
|
return lname == l;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* This represents a directory. */
|
||||||
|
struct FileSet
|
||||||
|
{
|
||||||
|
set<File> files;
|
||||||
|
RageTimer age;
|
||||||
|
void GetFilesMatching(
|
||||||
|
const CString &beginning, const CString &containing, const CString &ending,
|
||||||
|
vector<CString> &out, bool bOnlyDirs) const;
|
||||||
|
void GetFilesEqualTo(const CString &pat, vector<CString> &out, bool bOnlyDirs) const;
|
||||||
|
|
||||||
|
FileType GetFileType( const CString &path ) const;
|
||||||
|
int GetFileSize(const CString &path) const;
|
||||||
|
int GetFileModTime(const CString &path) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FilenameDB
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
FileSet &GetFileSet( CString dir );
|
||||||
|
|
||||||
|
/* Directories we have cached: */
|
||||||
|
map<CString, FileSet *> dirs;
|
||||||
|
|
||||||
|
int ExpireSeconds;
|
||||||
|
|
||||||
|
void GetFilesEqualTo(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
|
||||||
|
void GetFilesMatching(const CString &dir,
|
||||||
|
const CString &beginning, const CString &containing, const CString &ending,
|
||||||
|
vector<CString> &out, bool bOnlyDirs);
|
||||||
|
void AddFileSet( CString sPath, FileSet *fs );
|
||||||
|
|
||||||
|
/* The given path wasn't cached. Cache it, and return the cached FileSet or
|
||||||
|
* NULL on failure. */
|
||||||
|
virtual void PopulateFileSet( FileSet &fs, const CString &sPath ) = 0;
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
FilenameDB::FilenameDB():
|
||||||
|
ExpireSeconds( -1 ) { }
|
||||||
|
FilenameDB::~FilenameDB() { FlushDirCache(); }
|
||||||
|
|
||||||
|
/* This handles at most two * wildcards. If we need anything more complicated,
|
||||||
|
* we'll need to use fnmatch or regex. */
|
||||||
|
void GetFilesSimpleMatch(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
|
||||||
|
|
||||||
|
/* Search for "path" case-insensitively and replace it with the correct
|
||||||
|
* case. If "path" doesn't exist at all, return false and don't change it. */
|
||||||
|
bool ResolvePath(CString &path);
|
||||||
|
|
||||||
|
FileType GetFileType( const CString &path );
|
||||||
|
int GetFileSize(const CString &path);
|
||||||
|
int GetFileModTime( const CString &sFilePath );
|
||||||
|
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
|
||||||
|
|
||||||
|
void FlushDirCache();
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user