mirror of
https://github.com/github/codeql.git
synced 2025-12-21 03:06:31 +01:00
`weakly_canonical` will resolve as much as possible in the path, and not return an error if it can't resolve everything (for example due to a non existant file). In any case in case of problems with the file we will see an error when actually using the resolved path. This tunes down some unhelpful log messages.
37 lines
952 B
C++
37 lines
952 B
C++
#include "swift/extractor/infra/file/Path.h"
|
|
#include <iostream>
|
|
#include <unistd.h>
|
|
|
|
namespace codeql {
|
|
|
|
static bool shouldCanonicalize() {
|
|
auto preserve = getenv("CODEQL_PRESERVE_SYMLINKS");
|
|
if (preserve && std::string(preserve) == "true") {
|
|
return false;
|
|
}
|
|
preserve = getenv("SEMMLE_PRESERVE_SYMLINKS");
|
|
if (preserve && std::string(preserve) == "true") {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::filesystem::path resolvePath(const std::filesystem::path& path) {
|
|
std::error_code ec;
|
|
std::filesystem::path ret = {};
|
|
static const auto canonicalize = shouldCanonicalize();
|
|
if (canonicalize) {
|
|
ret = std::filesystem::weakly_canonical(path, ec);
|
|
} else {
|
|
ret = std::filesystem::absolute(path, ec);
|
|
}
|
|
if (ec) {
|
|
std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute") << " path: " << path
|
|
<< ": " << ec.message() << "\n";
|
|
return path;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
} // namespace codeql
|