FileSize() to identify if no read-access to a file
Posted: Wed Apr 08, 2009 9:30 pm
Currently this function only identifies whether the file exists (and its size), whether it doesn't exist, or whether the path is a directory. If the user has no read access it will return -1, as if the file doesn't exist.
The reason this fails is because obtaining attributes about the file would be a security vulnerability. I believe the reason this fails is because this function wraps GetFileSize from the Win32 API.
Checking for the file existence specifically can be done using the C++ sys/stat.h library (I believe this is part of the STL) and should work even if the user has no access:
The reason this fails is because obtaining attributes about the file would be a security vulnerability. I believe the reason this fails is because this function wraps GetFileSize from the Win32 API.
Checking for the file existence specifically can be done using the C++ sys/stat.h library (I believe this is part of the STL) and should work even if the user has no access:
Code: Select all
bool fileExist(const string fileName)
{
struct stat fileInfo;
int statistics;
// Attempt to get the file attributes
statistics = stat(fileName.c_str(), &fileInfo);
return !statistics; // Returns true/false if file exists
}