2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-01-24 21:49:00 +08:00
|
|
|
#include <deque>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2022-01-24 21:49:00 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/match.h"
|
|
|
|
#include "absl/strings/str_cat.h"
|
2022-01-24 21:49:00 +08:00
|
|
|
#include "absl/strings/str_join.h"
|
|
|
|
#include "absl/strings/str_split.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/strip.h"
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sapi::file {
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
constexpr char kPathSeparator[] = "/";
|
|
|
|
|
|
|
|
std::string JoinPathImpl(std::initializer_list<absl::string_view> paths) {
|
|
|
|
std::string result;
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
if (path.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (result.empty()) {
|
|
|
|
absl::StrAppend(&result, path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto comp = absl::StripPrefix(path, kPathSeparator);
|
|
|
|
if (absl::EndsWith(result, kPathSeparator)) {
|
|
|
|
absl::StrAppend(&result, comp);
|
|
|
|
} else {
|
|
|
|
absl::StrAppend(&result, kPathSeparator, comp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
bool IsAbsolutePath(absl::string_view path) {
|
|
|
|
return !path.empty() && path[0] == '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<absl::string_view, absl::string_view> SplitPath(
|
|
|
|
absl::string_view path) {
|
|
|
|
const auto pos = path.find_last_of('/');
|
|
|
|
|
|
|
|
// Handle the case with no '/' in 'path'.
|
|
|
|
if (pos == absl::string_view::npos) {
|
|
|
|
return {path.substr(0, 0), path};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the case with a single leading '/' in 'path'.
|
|
|
|
if (pos == 0) {
|
|
|
|
return {path.substr(0, 1), absl::ClippedSubstr(path, 1)};
|
|
|
|
}
|
|
|
|
return {path.substr(0, pos), absl::ClippedSubstr(path, pos + 1)};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CleanPath(const absl::string_view unclean_path) {
|
2022-01-24 21:49:00 +08:00
|
|
|
int dotdot_num = 0;
|
|
|
|
std::deque<absl::string_view> parts;
|
|
|
|
for (absl::string_view part :
|
|
|
|
absl::StrSplit(unclean_path, '/', absl::SkipEmpty())) {
|
|
|
|
if (part == "..") {
|
|
|
|
if (parts.empty()) {
|
|
|
|
++dotdot_num;
|
|
|
|
} else {
|
|
|
|
parts.pop_back();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2022-01-24 21:49:00 +08:00
|
|
|
} else if (part != ".") {
|
|
|
|
parts.push_back(part);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 21:49:00 +08:00
|
|
|
if (absl::StartsWith(unclean_path, "/")) {
|
|
|
|
if (parts.empty()) {
|
|
|
|
return "/";
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2022-01-24 21:49:00 +08:00
|
|
|
parts.push_front("");
|
2019-03-19 00:21:48 +08:00
|
|
|
} else {
|
2022-01-24 21:49:00 +08:00
|
|
|
for (; dotdot_num; --dotdot_num) {
|
|
|
|
parts.push_front("..");
|
|
|
|
}
|
|
|
|
if (parts.empty()) {
|
|
|
|
return ".";
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2022-01-24 21:49:00 +08:00
|
|
|
return absl::StrJoin(parts, "/");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
} // namespace sapi::file
|