Cache mtimes.

This commit is contained in:
Glenn Maynard
2003-09-23 00:09:10 +00:00
parent adafc1114f
commit 18a45487ab
3 changed files with 40 additions and 14 deletions
+1 -5
View File
@@ -437,12 +437,8 @@ unsigned int GetHashForFile( CString sPath )
unsigned int hash = 0; unsigned int hash = 0;
hash += GetHashForString( sPath ); hash += GetHashForString( sPath );
hash += GetFileSizeInBytes( sPath ); hash += GetFileSizeInBytes( sPath );
hash += GetFileModTime( sPath );
struct stat st;
if( DoStat(sPath, &st))
hash += st.st_mtime;
return hash; return hash;
} }
+38 -8
View File
@@ -22,9 +22,12 @@ struct File {
istring name; istring name;
bool dir; bool dir;
int size; 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; } File() { dir=false; size=-1; mtime=-1; }
File( istring fn ): name(fn) { dir=false; size=-1; } File( istring fn ): name(fn) { dir=false; size=-1; mtime=-1; }
bool operator== (const File &rhs) const { return name==rhs.name; } bool operator== (const File &rhs) const { return name==rhs.name; }
bool operator< (const File &rhs) const { return name<rhs.name; } bool operator< (const File &rhs) const { return name<rhs.name; }
@@ -49,6 +52,7 @@ struct FileSet
bool IsADirectory(const CString &path) const; bool IsADirectory(const CString &path) const;
bool IsAFile(const CString &path) const; bool IsAFile(const CString &path) const;
int GetFileSize(const CString &path) const; int GetFileSize(const CString &path) const;
int GetFileModTime(const CString &path) const;
}; };
void FileSet::LoadFromDir(const CString &dir) void FileSet::LoadFromDir(const CString &dir)
@@ -71,6 +75,7 @@ void FileSet::LoadFromDir(const CString &dir)
f.name=fd.cFileName; f.name=fd.cFileName;
f.dir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); f.dir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
f.size = fd.nFileSizeLow; f.size = fd.nFileSizeLow;
f.mtime = fd.ftLastWriteTime.dwLowDateTime;
files.insert(f); files.insert(f);
} while( FindNextFile( hFind, &fd ) ); } while( FindNextFile( hFind, &fd ) );
@@ -109,6 +114,7 @@ void FileSet::LoadFromDir(const CString &dir)
} else { } else {
f.dir = (st.st_mode & S_IFDIR); f.dir = (st.st_mode & S_IFDIR);
f.size = st.st_size; f.size = st.st_size;
f.mtime = st.st_mtime;
} }
files.insert(f); files.insert(f);
@@ -194,6 +200,14 @@ int FileSet::GetFileSize(const CString &path) const
return i->size; return i->size;
} }
int FileSet::GetFileModTime(const CString &path) const
{
set<File>::const_iterator i = files.find(File(path.c_str()));
if(i == files.end())
return -1;
return i->mtime;
}
/* Given "foo/bar/baz/" or "foo/bar/baz", return "foo/bar/" and "baz". */ /* Given "foo/bar/baz/" or "foo/bar/baz", return "foo/bar/" and "baz". */
static void SplitPath( CString Path, CString &Dir, CString &Name ) static void SplitPath( CString Path, CString &Dir, CString &Name )
{ {
@@ -241,6 +255,7 @@ public:
bool IsAFile(const CString &path); bool IsAFile(const CString &path);
bool IsADirectory(const CString &path); bool IsADirectory(const CString &path);
int GetFileSize(const CString &path); int GetFileSize(const CString &path);
int GetFileModTime( const CString &sFilePath );
void FlushDirCache(); void FlushDirCache();
}; };
@@ -277,6 +292,13 @@ int FilenameDB::GetFileSize( const CString &sPath )
return fs.GetFileSize(Name); return fs.GetFileSize(Name);
} }
int FilenameDB::GetFileModTime( const CString &sPath )
{
CString Dir, Name;
SplitPath(sPath, Dir, Name);
FileSet &fs = GetFileSet(Dir.c_str());
return fs.GetFileModTime(Name);
}
/* XXX: this won't work right for URIs, eg \\foo\bar */ /* XXX: this won't work right for URIs, eg \\foo\bar */
bool FilenameDB::ResolvePath(CString &path) bool FilenameDB::ResolvePath(CString &path)
@@ -462,15 +484,21 @@ void FlushDirCache()
bool DoesFileExist( const CString &sPath ) { return FDB.DoesFileExist(sPath); } bool DoesFileExist( const CString &sPath ) { return FDB.DoesFileExist(sPath); }
bool IsAFile( const CString &sPath ) { return FDB.IsAFile(sPath); } bool IsAFile( const CString &sPath ) { return FDB.IsAFile(sPath); }
bool IsADirectory( const CString &sPath ) { return FDB.IsADirectory(sPath); } bool IsADirectory( const CString &sPath ) { return FDB.IsADirectory(sPath); }
int GetFileModTime( const CString &sPath ) { return FDB.GetFileModTime(sPath); }
unsigned GetFileSizeInBytes( const CString &sPath ) unsigned GetFileSizeInBytes( const CString &sPath )
{ {
int ret = FDB.GetFileSize(sPath); int ret = FDB.GetFileSize(sPath);
LOG->Trace("file '%s' size %i", sPath.c_str(), ret); // LOG->Trace("file '%s' size %i", sPath.c_str(), ret);
if( ret == -1 ) if( ret == -1 )
return 0; return 0;
return ret; return ret;
} }
#else #else
static bool DoStat(CString sPath, struct stat *st)
{
TrimRight(sPath, "/\\");
return stat(sPath.c_str(), st) != -1;
}
bool DoesFileExist( const CString &sPath ) bool DoesFileExist( const CString &sPath )
{ {
if(sPath.empty()) return false; if(sPath.empty()) return false;
@@ -501,11 +529,13 @@ unsigned GetFileSizeInBytes( const CString &sFilePath )
return st.st_size; return st.st_size;
} }
#endif
/* XXX */ int GetFileModTime( const CString &sPath )
bool DoStat(CString sPath, struct stat *st)
{ {
TrimRight(sPath, "/\\"); struct stat st;
return stat(sPath.c_str(), st) != -1; if(!DoStat(sFilePath, &st))
return -1;
return st.st_mtime;
} }
#endif
+1 -1
View File
@@ -6,7 +6,7 @@ bool IsAFile( const CString &sPath );
bool IsADirectory( const CString &sPath ); bool IsADirectory( const CString &sPath );
bool ResolvePath(CString &path); bool ResolvePath(CString &path);
unsigned GetFileSizeInBytes( const CString &sFilePath ); unsigned GetFileSizeInBytes( const CString &sFilePath );
bool DoStat(CString sPath, struct stat *st); int GetFileModTime( const CString &sPath );
void FlushDirCache(); void FlushDirCache();