2020-10-14 04:03:04 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "utils.h" // NOLINT(build/include)
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
|
|
|
#include "sandboxed_api/util/temp_file.h"
|
2020-10-14 04:03:04 +08:00
|
|
|
|
|
|
|
namespace gdal::sandbox::utils {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-10-19 23:08:03 +08:00
|
|
|
constexpr char kProjDbEnvVariableName[] = "PROJ_DB_PATH";
|
|
|
|
inline constexpr absl::string_view kDefaultProjDbPath =
|
|
|
|
"/usr/local/share/proj/proj.db";
|
2020-10-14 04:03:04 +08:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TempFile::TempFile(absl::string_view prefix) {
|
|
|
|
auto file_data = sandbox2::CreateNamedTempFile(prefix);
|
|
|
|
|
|
|
|
if (file_data.ok()) {
|
|
|
|
file_data_ = std::move(file_data.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TempFile::~TempFile() {
|
|
|
|
if (HasValue()) {
|
|
|
|
unlink(file_data_.value().first.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:08:03 +08:00
|
|
|
bool TempFile::HasValue() const { return file_data_.has_value(); }
|
2020-10-14 04:03:04 +08:00
|
|
|
|
2020-10-19 23:08:03 +08:00
|
|
|
int TempFile::GetFd() const { return file_data_.value().second; }
|
2020-10-14 04:03:04 +08:00
|
|
|
|
2020-10-19 23:08:03 +08:00
|
|
|
std::string TempFile::GetPath() const { return file_data_.value().first; }
|
2020-10-14 04:03:04 +08:00
|
|
|
|
|
|
|
std::optional<std::string> FindProjDbPath() {
|
2020-10-15 23:13:11 +08:00
|
|
|
std::string proj_db_path(kDefaultProjDbPath);
|
2020-10-19 23:08:03 +08:00
|
|
|
|
|
|
|
if (const char* proj_db_env_var = std::getenv(kProjDbEnvVariableName);
|
2020-10-15 23:13:11 +08:00
|
|
|
proj_db_env_var != nullptr) {
|
|
|
|
proj_db_path = proj_db_env_var;
|
|
|
|
}
|
2020-10-14 04:03:04 +08:00
|
|
|
|
|
|
|
if (!sandbox2::file_util::fileops::Exists(proj_db_path, false)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proj_db_path;
|
|
|
|
}
|
|
|
|
|
2020-10-17 00:15:11 +08:00
|
|
|
std::string GetTestDataPath(absl::string_view testdata_path) {
|
|
|
|
const char* test_srcdir = std::getenv("TEST_SRCDIR");
|
|
|
|
|
2020-10-19 23:08:03 +08:00
|
|
|
return sandbox2::file::JoinPath(test_srcdir == nullptr
|
|
|
|
? sandbox2::file_util::fileops::GetCWD()
|
|
|
|
: test_srcdir,
|
|
|
|
testdata_path);
|
2020-10-17 00:15:11 +08:00
|
|
|
}
|
|
|
|
|
2020-10-14 04:03:04 +08:00
|
|
|
} // namespace gdal::sandbox::utils
|