FileUtil: Add a class for Exists/IsDirectory/GetSize

Some code was calling more than one of these functions in a row
(in particular, FileUtil.cpp itself did it a lot...), which is
a waste since it's possible to call stat a single time and then
read all three values from the stat struct. This commit adds a
File::FileInfo class that calls stat once on construction and
then lets Exists/IsDirectory/GetSize be executed very quickly.

The performance improvement mostly matters for functions that
can be handling a lot of files, such as File::ScanDirectoryTree.

I've also done some cleanup in code that uses these functions.
For instance, some code had checks like !Exists() || !IsDirectory(),
which is functionally equivalent to !IsDirectory(), and some
code was using File::GetSize even though there was an IOFile
object that the code could call GetSize on.
This commit is contained in:
JosJuice
2017-06-29 11:20:38 +02:00
parent e14a82a87e
commit 5ca3aee00a
21 changed files with 175 additions and 177 deletions

View File

@ -9,6 +9,8 @@
#include <string>
#include <vector>
#include <sys/stat.h>
#include "Common/CommonTypes.h"
#include "Common/NonCopyable.h"
@ -78,14 +80,42 @@ struct FSTEntry
std::vector<FSTEntry> children;
};
// Returns true if file filename exists
bool Exists(const std::string& filename);
// The functions in this class are functionally identical to the standalone functions
// below, but if you are going to be calling more than one of the functions using the
// same path, creating a single FileInfo object and calling its functions multiple
// times is faster than calling standalone functions multiple times.
class FileInfo final
{
public:
explicit FileInfo(const std::string& path);
explicit FileInfo(const char* path);
explicit FileInfo(int fd);
// Returns true if filename is a directory
bool IsDirectory(const std::string& filename);
// Returns true if the path exists
bool Exists() const;
// Returns true if the path exists and is a directory
bool IsDirectory() const;
// Returns true if the path exists and is a file
bool IsFile() const;
// Returns the size of a file (or returns 0 if the path doesn't refer to a file)
u64 GetSize() const;
// Returns the size of filename (64bit)
u64 GetSize(const std::string& filename);
private:
struct stat m_stat;
bool m_exists;
};
// Returns true if the path exists
bool Exists(const std::string& path);
// Returns true if the path exists and is a directory
bool IsDirectory(const std::string& path);
// Returns true if the path exists and is a file
bool IsFile(const std::string& path);
// Returns the size of a file (or returns 0 if the path isn't a file that exists)
u64 GetSize(const std::string& path);
// Overloaded GetSize, accepts file descriptor
u64 GetSize(const int fd);