This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathMapping.cpp
More file actions
48 lines (36 loc) · 1.52 KB
/
Copy pathMapping.cpp
File metadata and controls
48 lines (36 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2021 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "SourcePathsMapping/Mapping.h"
#include <chrono>
#include <filesystem>
#include <optional>
#include <system_error>
#include "OrbitBase/Logging.h"
namespace fs = std::filesystem;
namespace orbit_source_paths_mapping {
static bool IsRegularFile(const std::filesystem::path& target_path) {
std::error_code error{};
if (fs::is_regular_file(target_path, error)) return true;
if (error.value() != 0) {
ORBIT_ERROR("Failed to 'stat' the file \"%s\": %s", target_path.string(), error.message());
}
return false;
}
std::optional<fs::path> MapToFirstExistingTarget(absl::Span<const Mapping> mappings,
const fs::path& source_path) {
return MapToFirstMatchingTarget(mappings, source_path, &IsRegularFile);
}
std::optional<Mapping> InferMappingFromExample(const fs::path& source_path,
const fs::path& target_path) {
if (source_path.filename() != target_path.filename()) return std::nullopt;
if (source_path == target_path) return std::nullopt;
fs::path source = source_path;
fs::path target = target_path;
while (source.has_filename() && target.has_filename() && source.filename() == target.filename()) {
source = source.parent_path();
target = target.parent_path();
}
return Mapping{source, target};
}
} // namespace orbit_source_paths_mapping