extremetuxracer/src/depot.cpp

56 lines
1.4 KiB
C++

#include "depot.h"
#include "common.h"
#include "absl/strings/strip.h"
#include "absl/strings/str_format.h"
#include "tools/cpp/runfiles/runfiles.h"
using bazel::tools::cpp::runfiles::Runfiles;
static std::unique_ptr<Runfiles> runfiles = nullptr;
void DepotInit(const std::string &argv0) {
if (runfiles != nullptr) {
Message("DepotInit cannot be called twice");
exit(1);
}
std::string err;
auto rf = Runfiles::Create(argv0, &err);
if (rf == nullptr) {
Message("Runfiles::Create", err);
exit(1);
}
runfiles = std::unique_ptr<Runfiles>(rf);
}
absl::StatusOr<std::string> DepotResolveFile(const std::string &depotpath) {
if (runfiles == nullptr) {
return absl::FailedPreconditionError("depot not initialized");
}
absl::string_view sv(depotpath);
if (!absl::ConsumePrefix(&sv, "//")) {
return absl::InvalidArgumentError("path does not start with //");
}
auto loc = runfiles->Rlocation("extremetuxracer/" + std::string(sv));
if (loc.empty()) {
return absl::NotFoundError("depot path not found in runfiles");
}
if (access(loc.c_str(), F_OK) != 0) {
return absl::NotFoundError("depot path does not exit");
}
return loc;
}
std::string DepotMustResolveFile(const std::string &depotpath) {
auto r = DepotResolveFile(depotpath);
if (!r.ok()) {
auto msg = absl::StrFormat("Could not resolve depot path %s: %s", depotpath, r.status().ToString());
Message(msg);
exit(1);
}
return r.value();
}