CLI: Strict check if exclude path is file.
Initially I added logic that checked if excluded path was a file we would accept. This works for source files, but when file with "unknown" extension was given it was determined as a directory name and ending slash was added. E.g. -ifile.h would end up having ignored path file.h/. This commit adds per-platform checks if the path points to the file and if the file also exists.
This commit is contained in:
parent
13360c2a66
commit
b976445be7
|
@ -388,10 +388,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
|
|||
path = Path::simplifyPath(path.c_str());
|
||||
path = Path::removeQuotationMarks(path);
|
||||
|
||||
// If not "known" filename extension then assume it is path
|
||||
if (!FileLister::acceptFile(path))
|
||||
if (!FileLister::fileExists(path) && FileLister::isDirectory(path))
|
||||
{
|
||||
// If path doesn't end with / or \, add it
|
||||
// If directory name doesn't end with / or \, add it
|
||||
if (path[path.length()-1] != '/')
|
||||
path += '/';
|
||||
}
|
||||
|
|
|
@ -228,7 +228,13 @@ bool FileLister::isDirectory(const std::string &path)
|
|||
return (MyIsDirectory(path) != FALSE);
|
||||
}
|
||||
|
||||
|
||||
bool FileLister::fileExists(const std::string &path)
|
||||
{
|
||||
if (PathFileExists(path.c_str()) == TRUE)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
@ -335,4 +341,22 @@ bool FileLister::isDirectory(const std::string &path)
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool FileLister::fileExists(const std::string &path)
|
||||
{
|
||||
struct stat statinfo;
|
||||
int result = stat(path.c_str(), &statinfo);
|
||||
|
||||
if (result < 0) // Todo: should check errno == ENOENT?
|
||||
{
|
||||
// File not found
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if file is regular file
|
||||
if ((statinfo.st_mode & S_IFMT) == S_IFREG)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -57,6 +57,12 @@ public:
|
|||
*/
|
||||
static bool isDirectory(const std::string &path);
|
||||
|
||||
/**
|
||||
* @brief Check if the given path is a file and if it exists?
|
||||
* @return true if path points to file and the file exists.
|
||||
*/
|
||||
static bool fileExists(const std::string &path);
|
||||
|
||||
#ifndef _WIN32
|
||||
static void recursiveAddFiles2(std::vector<std::string> &relative,
|
||||
std::vector<std::string> &absolute,
|
||||
|
|
Loading…
Reference in New Issue