2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "filelister.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "path.h"
|
2015-07-23 14:01:33 +02:00
|
|
|
#include "pathmatch.h"
|
2018-03-30 21:04:32 +02:00
|
|
|
#include "utils.h"
|
2010-03-11 20:58:59 +01:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2021-01-30 14:45:03 +01:00
|
|
|
// fix NAME_MAX not found on macOS GCC8.1
|
|
|
|
#include <climits>
|
2023-08-23 11:22:41 +02:00
|
|
|
#include <memory>
|
2011-03-20 14:25:11 +01:00
|
|
|
|
|
|
|
#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
|
2012-07-16 11:37:29 +02:00
|
|
|
// of called functions. Thus, we explicitly call *A versions of the functions.
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2021-04-18 21:52:14 +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)
|
2018-02-17 06:31:12 +01:00
|
|
|
{
|
2021-04-18 21:52:14 +02:00
|
|
|
return addFiles(files, path, extra, true, ignored);
|
2018-02-17 06:31:12 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 21:52:14 +02:00
|
|
|
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)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
2023-08-23 11:22:41 +02:00
|
|
|
if (path.empty())
|
|
|
|
return "no path specified";
|
|
|
|
|
2013-03-01 18:04:10 +01:00
|
|
|
const std::string cleanedPath = Path::toNativeSeparators(path);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2013-03-01 18:04:10 +01:00
|
|
|
// basedir is the base directory which is used to form pathnames.
|
|
|
|
// It always has a trailing backslash available for concatenation.
|
|
|
|
std::string basedir;
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2013-03-01 18:04:10 +01:00
|
|
|
// searchPattern is the search string passed into FindFirst and FindNext.
|
|
|
|
std::string searchPattern = cleanedPath;
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2013-03-03 07:32:14 +01:00
|
|
|
// The user wants to check all files in a dir
|
2023-08-23 11:22:41 +02:00
|
|
|
const bool checkAllFilesInDir = Path::isDirectory(cleanedPath);
|
2013-03-03 07:32:14 +01:00
|
|
|
|
|
|
|
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) {
|
2011-03-20 14:25:11 +01:00
|
|
|
case '\\':
|
2013-03-01 18:04:10 +01:00
|
|
|
searchPattern += '*';
|
|
|
|
basedir = cleanedPath;
|
2011-03-20 14:25:11 +01:00
|
|
|
break;
|
|
|
|
case '*':
|
2013-03-01 18:04:10 +01:00
|
|
|
basedir = cleanedPath.substr(0, cleanedPath.length() - 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
break;
|
|
|
|
default:
|
2013-03-01 18:04:10 +01:00
|
|
|
searchPattern += "\\*";
|
2011-03-20 14:25:11 +01:00
|
|
|
if (cleanedPath != ".")
|
2013-03-01 18:04:10 +01:00
|
|
|
basedir = cleanedPath + '\\';
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
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) {
|
2013-03-01 18:04:10 +01:00
|
|
|
basedir = cleanedPath.substr(0, pos + 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 11:37:29 +02:00
|
|
|
WIN32_FIND_DATAA ffd;
|
2023-08-23 11:22:41 +02:00
|
|
|
HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &ffd);
|
|
|
|
if (INVALID_HANDLE_VALUE == hFind) {
|
|
|
|
const DWORD err = GetLastError();
|
|
|
|
if (err == ERROR_FILE_NOT_FOUND) {
|
|
|
|
// no files matched
|
|
|
|
return "";
|
|
|
|
}
|
2023-09-19 15:36:34 +02:00
|
|
|
return "finding files failed. Search pattern: '" + searchPattern + "'. (error: " + std::to_string(err) + ")";
|
2023-08-23 11:22:41 +02:00
|
|
|
}
|
|
|
|
std::unique_ptr<void, decltype(&FindClose)> hFind_deleter(hFind, FindClose);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
do {
|
2023-08-23 11:22:41 +02:00
|
|
|
if (ffd.cFileName[0] != '.' && ffd.cFileName[0] != '\0')
|
|
|
|
{
|
|
|
|
const char* ansiFfd = ffd.cFileName;
|
|
|
|
if (std::strchr(ansiFfd,'?')) {
|
|
|
|
ansiFfd = ffd.cAlternateFileName;
|
|
|
|
}
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
const std::string fname(basedir + ansiFfd);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2023-08-23 11:22:41 +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);
|
2015-11-29 10:49:10 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
// Limitation: file sizes are assumed to fit in a 'size_t'
|
2012-04-08 11:50:54 +02:00
|
|
|
#ifdef _WIN64
|
2023-08-23 11:22:41 +02:00
|
|
|
files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
|
2012-04-08 11:50:54 +02:00
|
|
|
#else
|
2023-08-23 11:22:41 +02:00
|
|
|
files[nativename] = ffd.nFileSizeLow;
|
2012-04-08 11:50:54 +02:00
|
|
|
#endif
|
2023-08-23 11:22:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Directory
|
|
|
|
if (recursive) {
|
|
|
|
if (!ignored.match(fname)) {
|
|
|
|
std::string err = FileLister::recursiveAddFiles(files, fname, extra, ignored);
|
|
|
|
if (!err.empty())
|
|
|
|
return err;
|
|
|
|
}
|
2021-04-18 21:52:14 +02:00
|
|
|
}
|
2018-02-17 06:31:12 +01:00
|
|
|
}
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
if (!FindNextFileA(hFind, &ffd)) {
|
|
|
|
const DWORD err = GetLastError();
|
|
|
|
// no more files matched
|
|
|
|
if (err != ERROR_NO_MORE_FILES)
|
|
|
|
return "failed to get next file (error: " + std::to_string(err) + ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
return "";
|
2011-08-06 15:47:57 +02:00
|
|
|
}
|
2011-03-20 14:25:11 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
////// This code is POSIX-style systems ///////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-19 21:20:30 +01:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
#undef __STRICT_ANSI__
|
|
|
|
#endif
|
|
|
|
|
2015-12-12 00:35:38 +01:00
|
|
|
#include <dirent.h>
|
2011-04-19 13:52:32 +02:00
|
|
|
#include <sys/stat.h>
|
2021-05-03 09:39:33 +02:00
|
|
|
#include <cerrno>
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2021-07-14 20:50:14 +02:00
|
|
|
#ifndef NAME_MAX
|
|
|
|
#ifdef MAXNAMLEN
|
|
|
|
#define NAME_MAX MAXNAMLEN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-12-12 00:35:38 +01:00
|
|
|
|
2021-04-18 21:52:14 +02:00
|
|
|
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
|
|
|
)
|
2011-10-29 19:30:33 +02:00
|
|
|
{
|
2022-11-24 13:45:57 +01:00
|
|
|
if (ignored.match(path))
|
|
|
|
return "";
|
|
|
|
|
2015-12-12 00:35:38 +01:00
|
|
|
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());
|
2023-08-23 11:22:41 +02:00
|
|
|
if (!dir) {
|
|
|
|
const int err = errno;
|
|
|
|
return "could not open directory '" + path + "' (errno: " + std::to_string(err) + ")";
|
|
|
|
}
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir_deleter(dir, closedir);
|
2011-10-29 19:30:33 +02:00
|
|
|
|
2023-08-16 10:20:53 +02:00
|
|
|
std::string new_path = path;
|
2022-12-09 19:34:51 +01:00
|
|
|
new_path += '/';
|
2011-10-29 19:57:12 +02:00
|
|
|
|
2023-10-08 09:10:17 +02:00
|
|
|
while (const dirent* dir_result = readdir(dir)) {
|
2015-12-12 00:35:38 +01:00
|
|
|
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
|
|
|
|
(std::strcmp(dir_result->d_name, "..") == 0))
|
|
|
|
continue;
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2022-12-09 19:34:51 +01:00
|
|
|
new_path.erase(path.length() + 1);
|
|
|
|
new_path += dir_result->d_name;
|
2011-10-29 19:57:12 +02:00
|
|
|
|
2019-01-09 19:47:46 +01:00
|
|
|
#if defined(_DIRENT_HAVE_D_TYPE) || defined(_BSD_SOURCE)
|
2023-08-23 11:22:41 +02:00
|
|
|
const bool path_is_directory = (dir_result->d_type == DT_DIR || (dir_result->d_type == DT_UNKNOWN && Path::isDirectory(new_path)));
|
2019-01-08 21:17:04 +01:00
|
|
|
#else
|
2023-08-23 11:22:41 +02:00
|
|
|
const bool path_is_directory = Path::isDirectory(new_path);
|
2019-01-08 21:17:04 +01:00
|
|
|
#endif
|
2019-01-09 19:47:46 +01:00
|
|
|
if (path_is_directory) {
|
2017-07-28 11:27:04 +02:00
|
|
|
if (recursive && !ignored.match(new_path)) {
|
2021-04-18 21:52:14 +02:00
|
|
|
std::string err = addFiles2(files, new_path, extra, recursive, ignored);
|
2023-05-03 16:23:07 +02:00
|
|
|
if (!err.empty()) {
|
2021-04-18 21:52:14 +02:00
|
|
|
return err;
|
2023-05-03 16:23:07 +02:00
|
|
|
}
|
2015-12-12 00:35:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-28 11:27:04 +02:00
|
|
|
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
|
2021-04-18 21:52:14 +02:00
|
|
|
if (stat(new_path.c_str(), &file_stat) != -1)
|
|
|
|
files[new_path] = file_stat.st_size;
|
2023-05-03 16:23:07 +02:00
|
|
|
else {
|
2023-08-23 11:22:41 +02:00
|
|
|
const int err = errno;
|
|
|
|
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
|
2023-05-03 16:23:07 +02:00
|
|
|
}
|
2015-12-12 00:35:38 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-23 14:01:33 +02:00
|
|
|
}
|
2015-12-12 00:35:38 +01:00
|
|
|
} else
|
|
|
|
files[path] = file_stat.st_size;
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
2021-04-18 21:52:14 +02:00
|
|
|
return "";
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 21:52:14 +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)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
2021-04-18 21:52:14 +02:00
|
|
|
return addFiles(files, path, extra, true, ignored);
|
2015-03-05 06:53:11 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 21:52:14 +02:00
|
|
|
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)
|
2015-03-05 06:53:11 +01:00
|
|
|
{
|
2023-08-23 11:22:41 +02:00
|
|
|
if (path.empty())
|
|
|
|
return "no path specified";
|
2015-12-12 00:35:38 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
std::string corrected_path = path;
|
|
|
|
if (endsWith(corrected_path, '/'))
|
|
|
|
corrected_path.erase(corrected_path.end() - 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
return addFiles2(files, corrected_path, extra, recursive, ignored);
|
2011-08-06 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2011-03-20 14:25:11 +01:00
|
|
|
#endif
|