From a6671a2d0d7e1e0d1fcbadc3274ec1eef0dda382 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Mon, 22 Sep 2003 05:14:00 +0000 Subject: [PATCH] Cache file sizes. --- stepmania/src/RageUtil_FileDB.cpp | 79 ++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/stepmania/src/RageUtil_FileDB.cpp b/stepmania/src/RageUtil_FileDB.cpp index 8dced999c5..29b3b8d5ed 100644 --- a/stepmania/src/RageUtil_FileDB.cpp +++ b/stepmania/src/RageUtil_FileDB.cpp @@ -21,9 +21,10 @@ struct File { istring name; bool dir; - - File() { dir=false; } - File(istring fn, bool dir_=false): name(fn), dir(dir) { } + int size; + + File() { dir=false; size=-1; } + File( istring fn ): name(fn) { dir=false; size=-1; } bool operator== (const File &rhs) const { return name==rhs.name; } bool operator< (const File &rhs) const { return named_name; - if( ent->d_type == DT_UNKNOWN ) + struct stat st; + if( stat(ent->d_name, &st) == -1 ) { - /* d_type isn't implemented; we need to stat. */ - struct stat st; - if( stat(ent->d_name, &st) == -1 ) - { - /* 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)); - f.dir = false; - } else - f.dir = (st.st_mode & S_IFDIR); - } else - f.dir = ent->d_type == DT_DIR; + /* 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)); + f.dir = false; + } else { + f.dir = (st.st_mode & S_IFDIR); + f.size = st.st_size; + } files.insert(f); } @@ -186,6 +186,14 @@ bool FileSet::IsAFile(const CString &path) const return !i->dir; } +int FileSet::GetFileSize(const CString &path) const +{ + set::const_iterator i = files.find(File(path.c_str())); + if(i == files.end()) + return -1; + return i->size; +} + /* Given "foo/bar/baz/" or "foo/bar/baz", return "foo/bar/" and "baz". */ static void SplitPath( CString Path, CString &Dir, CString &Name ) { @@ -232,6 +240,7 @@ public: bool DoesFileExist(const CString &path); bool IsAFile(const CString &path); bool IsADirectory(const CString &path); + int GetFileSize(const CString &path); void FlushDirCache(); }; @@ -260,6 +269,14 @@ bool FilenameDB::IsADirectory( const CString &sPath ) return fs.IsADirectory(Name); } +int FilenameDB::GetFileSize( const CString &sPath ) +{ + CString Dir, Name; + SplitPath(sPath, Dir, Name); + FileSet &fs = GetFileSet(Dir.c_str()); + return fs.GetFileSize(Name); +} + /* XXX: this won't work right for URIs, eg \\foo\bar */ bool FilenameDB::ResolvePath(CString &path) @@ -435,11 +452,24 @@ void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bRe bool ResolvePath(CString &path) { return FDB.ResolvePath(path); } +void FlushDirCache() +{ + FDB.FlushDirCache(); +} + #if 1 bool DoesFileExist( const CString &sPath ) { return FDB.DoesFileExist(sPath); } bool IsAFile( const CString &sPath ) { return FDB.IsAFile(sPath); } bool IsADirectory( const CString &sPath ) { return FDB.IsADirectory(sPath); } +unsigned GetFileSizeInBytes( const CString &sPath ) +{ + int ret = FDB.GetFileSize(sPath); + LOG->Trace("file '%s' size %i", sPath.c_str(), ret); + if( ret == -1 ) + return 0; + return ret; +} #else bool DoesFileExist( const CString &sPath ) { @@ -462,14 +492,6 @@ bool IsADirectory( const CString &sPath ) return !!(st.st_mode & S_IFDIR); } -#endif - -/* XXX */ -bool DoStat(CString sPath, struct stat *st) -{ - TrimRight(sPath, "/\\"); - return stat(sPath.c_str(), st) != -1; -} unsigned GetFileSizeInBytes( const CString &sFilePath ) { @@ -479,8 +501,11 @@ unsigned GetFileSizeInBytes( const CString &sFilePath ) return st.st_size; } +#endif -void FlushDirCache() +/* XXX */ +bool DoStat(CString sPath, struct stat *st) { - FDB.FlushDirCache(); + TrimRight(sPath, "/\\"); + return stat(sPath.c_str(), st) != -1; }