Cache file sizes.
This commit is contained in:
@@ -21,9 +21,10 @@
|
|||||||
struct File {
|
struct File {
|
||||||
istring name;
|
istring name;
|
||||||
bool dir;
|
bool dir;
|
||||||
|
int size;
|
||||||
|
|
||||||
File() { dir=false; }
|
File() { dir=false; size=-1; }
|
||||||
File(istring fn, bool dir_=false): name(fn), dir(dir) { }
|
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 name==rhs.name; }
|
||||||
bool operator< (const File &rhs) const { return name<rhs.name; }
|
bool operator< (const File &rhs) const { return name<rhs.name; }
|
||||||
@@ -47,6 +48,7 @@ struct FileSet
|
|||||||
bool DoesFileExist(const CString &path) const;
|
bool DoesFileExist(const CString &path) const;
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
void FileSet::LoadFromDir(const CString &dir)
|
void FileSet::LoadFromDir(const CString &dir)
|
||||||
@@ -68,6 +70,7 @@ void FileSet::LoadFromDir(const CString &dir)
|
|||||||
File f;
|
File f;
|
||||||
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;
|
||||||
|
|
||||||
files.insert(f);
|
files.insert(f);
|
||||||
} while( FindNextFile( hFind, &fd ) );
|
} while( FindNextFile( hFind, &fd ) );
|
||||||
@@ -95,9 +98,6 @@ void FileSet::LoadFromDir(const CString &dir)
|
|||||||
File f;
|
File f;
|
||||||
f.name=ent->d_name;
|
f.name=ent->d_name;
|
||||||
|
|
||||||
if( ent->d_type == DT_UNKNOWN )
|
|
||||||
{
|
|
||||||
/* d_type isn't implemented; we need to stat. */
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if( stat(ent->d_name, &st) == -1 )
|
if( stat(ent->d_name, &st) == -1 )
|
||||||
{
|
{
|
||||||
@@ -106,10 +106,10 @@ void FileSet::LoadFromDir(const CString &dir)
|
|||||||
LOG->Warn("Got file '%s' in '%s' from list, but can't stat? (%s)",
|
LOG->Warn("Got file '%s' in '%s' from list, but can't stat? (%s)",
|
||||||
ent->d_name, dir.c_str(), strerror(errno));
|
ent->d_name, dir.c_str(), strerror(errno));
|
||||||
f.dir = false;
|
f.dir = false;
|
||||||
} else
|
} else {
|
||||||
f.dir = (st.st_mode & S_IFDIR);
|
f.dir = (st.st_mode & S_IFDIR);
|
||||||
} else
|
f.size = st.st_size;
|
||||||
f.dir = ent->d_type == DT_DIR;
|
}
|
||||||
|
|
||||||
files.insert(f);
|
files.insert(f);
|
||||||
}
|
}
|
||||||
@@ -186,6 +186,14 @@ bool FileSet::IsAFile(const CString &path) const
|
|||||||
return !i->dir;
|
return !i->dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int FileSet::GetFileSize(const CString &path) const
|
||||||
|
{
|
||||||
|
set<File>::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". */
|
/* 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 )
|
||||||
{
|
{
|
||||||
@@ -232,6 +240,7 @@ public:
|
|||||||
bool DoesFileExist(const CString &path);
|
bool DoesFileExist(const CString &path);
|
||||||
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);
|
||||||
|
|
||||||
void FlushDirCache();
|
void FlushDirCache();
|
||||||
};
|
};
|
||||||
@@ -260,6 +269,14 @@ bool FilenameDB::IsADirectory( const CString &sPath )
|
|||||||
return fs.IsADirectory(Name);
|
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 */
|
/* XXX: this won't work right for URIs, eg \\foo\bar */
|
||||||
bool FilenameDB::ResolvePath(CString &path)
|
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); }
|
bool ResolvePath(CString &path) { return FDB.ResolvePath(path); }
|
||||||
|
|
||||||
|
void FlushDirCache()
|
||||||
|
{
|
||||||
|
FDB.FlushDirCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
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); }
|
||||||
|
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
|
#else
|
||||||
bool DoesFileExist( const CString &sPath )
|
bool DoesFileExist( const CString &sPath )
|
||||||
{
|
{
|
||||||
@@ -462,14 +492,6 @@ bool IsADirectory( const CString &sPath )
|
|||||||
|
|
||||||
return !!(st.st_mode & S_IFDIR);
|
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 )
|
unsigned GetFileSizeInBytes( const CString &sFilePath )
|
||||||
{
|
{
|
||||||
@@ -479,8 +501,11 @@ unsigned GetFileSizeInBytes( const CString &sFilePath )
|
|||||||
|
|
||||||
return st.st_size;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user