cppcheck/cli/filelister.cpp

268 lines
8.7 KiB
C++
Raw Normal View History

2008-12-18 22:28:57 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2023-01-28 10:16:34 +01:00
* Copyright (C) 2007-2023 Cppcheck team.
2008-12-18 22:28:57 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
#include "filelister.h"
2017-05-27 04:33:47 +02:00
#include "config.h"
#include "path.h"
#include "pathmatch.h"
2018-03-30 21:04:32 +02:00
#include "utils.h"
2017-05-27 04:33:47 +02:00
#include <cstddef>
#include <cstring>
// fix NAME_MAX not found on macOS GCC8.1
#include <climits>
#ifdef _WIN32
///////////////////////////////////////////////////////////////////////////////
////// This code is WIN32 systems /////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <shlwapi.h>
// Here is the catch: cppcheck core is Ansi code (using char type).
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
// of called functions. Thus, we explicitly call *A versions of the functions.
static BOOL myIsDirectory(const std::string& path)
{
// See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
return PathIsDirectoryA(path.c_str());
}
static HANDLE myFindFirstFile(const std::string& path, LPWIN32_FIND_DATAA findData)
{
HANDLE hFind = FindFirstFileA(path.c_str(), findData);
return hFind;
}
static BOOL myFileExists(const std::string& path)
2011-08-06 23:11:53 +02:00
{
return PathFileExistsA(path.c_str()) && !PathIsDirectoryA(path.c_str());
2011-08-06 23:11:53 +02:00
}
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{
return addFiles(files, path, extra, true, ignored);
}
std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
{
const std::string cleanedPath = Path::toNativeSeparators(path);
// basedir is the base directory which is used to form pathnames.
// It always has a trailing backslash available for concatenation.
std::string basedir;
// searchPattern is the search string passed into FindFirst and FindNext.
std::string searchPattern = cleanedPath;
// The user wants to check all files in a dir
const bool checkAllFilesInDir = (myIsDirectory(cleanedPath) != FALSE);
if (checkAllFilesInDir) {
2018-04-04 21:02:13 +02:00
const char c = cleanedPath.back();
2011-10-13 20:53:06 +02:00
switch (c) {
case '\\':
searchPattern += '*';
basedir = cleanedPath;
break;
case '*':
basedir = cleanedPath.substr(0, cleanedPath.length() - 1);
break;
default:
searchPattern += "\\*";
if (cleanedPath != ".")
basedir = cleanedPath + '\\';
}
2011-10-13 20:53:06 +02:00
} else {
2018-04-04 21:02:13 +02:00
const std::string::size_type pos = cleanedPath.find_last_of('\\');
2011-10-13 20:53:06 +02:00
if (std::string::npos != pos) {
basedir = cleanedPath.substr(0, pos + 1);
}
}
WIN32_FIND_DATAA ffd;
HANDLE hFind = myFindFirstFile(searchPattern, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
return "";
2011-10-13 20:53:06 +02:00
do {
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
continue;
const char* ansiFfd = ffd.cFileName;
if (std::strchr(ansiFfd,'?')) {
ansiFfd = ffd.cAlternateFileName;
}
const std::string fname(basedir + ansiFfd);
2011-10-13 20:53:06 +02:00
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
// File
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.match(fname)) {
const std::string nativename = Path::fromNativeSeparators(fname);
// Limitation: file sizes are assumed to fit in a 'size_t'
#ifdef _WIN64
files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
#else
files[nativename] = ffd.nFileSizeLow;
#endif
}
2011-10-13 20:53:06 +02:00
} else {
// Directory
if (recursive) {
if (!ignored.match(fname)) {
std::string err = FileLister::recursiveAddFiles(files, fname, extra, ignored);
if (!err.empty())
return err;
}
}
}
} while (FindNextFileA(hFind, &ffd) != FALSE);
2015-09-09 17:45:22 +02:00
FindClose(hFind);
return "";
}
bool FileLister::isDirectory(const std::string &path)
{
return (myIsDirectory(path) != FALSE);
}
bool FileLister::fileExists(const std::string &path)
{
return (myFileExists(path) != FALSE);
}
#else
///////////////////////////////////////////////////////////////////////////////
////// This code is POSIX-style systems ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if defined(__CYGWIN__)
#undef __STRICT_ANSI__
#endif
#include <dirent.h>
#include <sys/stat.h>
#include <cerrno>
#ifndef NAME_MAX
#ifdef MAXNAMLEN
#define NAME_MAX MAXNAMLEN
#endif
#endif
static std::string addFiles2(std::map<std::string, std::size_t> &files,
const std::string &path,
const std::set<std::string> &extra,
bool recursive,
const PathMatch& ignored
2021-08-07 20:51:18 +02:00
)
{
2022-11-24 13:45:57 +01:00
if (ignored.match(path))
return "";
struct stat file_stat;
if (stat(path.c_str(), &file_stat) != -1) {
if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
DIR * dir = opendir(path.c_str());
if (!dir)
return "";
std::string new_path = path;
new_path += '/';
while (const struct dirent* dir_result = readdir(dir)) {
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
(std::strcmp(dir_result->d_name, "..") == 0))
continue;
new_path.erase(path.length() + 1);
new_path += dir_result->d_name;
2019-01-09 19:47:46 +01:00
#if defined(_DIRENT_HAVE_D_TYPE) || defined(_BSD_SOURCE)
const bool path_is_directory = (dir_result->d_type == DT_DIR || (dir_result->d_type == DT_UNKNOWN && FileLister::isDirectory(new_path)));
#else
const bool path_is_directory = FileLister::isDirectory(new_path);
#endif
2019-01-09 19:47:46 +01:00
if (path_is_directory) {
if (recursive && !ignored.match(new_path)) {
std::string err = addFiles2(files, new_path, extra, recursive, ignored);
if (!err.empty()) {
closedir(dir);
return err;
}
}
} else {
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
if (stat(new_path.c_str(), &file_stat) != -1)
files[new_path] = file_stat.st_size;
else {
closedir(dir);
return "Can't stat " + new_path + " errno: " + std::to_string(errno);
}
}
}
}
closedir(dir);
} else
files[path] = file_stat.st_size;
}
return "";
}
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{
return addFiles(files, path, extra, true, ignored);
}
std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
{
if (!path.empty()) {
std::string corrected_path = path;
2018-03-30 21:04:32 +02:00
if (endsWith(corrected_path, '/'))
corrected_path.erase(corrected_path.end() - 1);
return addFiles2(files, corrected_path, extra, recursive, ignored);
}
return "";
}
bool FileLister::isDirectory(const std::string &path)
{
struct stat file_stat;
2017-06-01 01:02:12 +02:00
return (stat(path.c_str(), &file_stat) != -1 && (file_stat.st_mode & S_IFMT) == S_IFDIR);
}
bool FileLister::fileExists(const std::string &path)
{
struct stat file_stat;
2017-06-01 01:02:12 +02:00
return (stat(path.c_str(), &file_stat) != -1 && (file_stat.st_mode & S_IFMT) == S_IFREG);
}
#endif