Files
codeql/swift/extractor/infra/file/Path.cpp
Paolo Tranquilli 4880ab41a2 Swift: use weakly_canonical instead of canonical
`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.
2023-01-24 16:34:47 +01:00

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