]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/cpp/src/arrow/filesystem/path_util.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / filesystem / path_util.h
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#pragma once
19
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "arrow/type_fwd.h"
25#include "arrow/util/optional.h"
26#include "arrow/util/string_view.h"
27
28namespace arrow {
29namespace fs {
30namespace internal {
31
32constexpr char kSep = '/';
33
34// Computations on abstract paths (not local paths with system-dependent behaviour).
35// Abstract paths are typically used in URIs.
36
37// Split an abstract path into its individual components.
38ARROW_EXPORT
39std::vector<std::string> SplitAbstractPath(const std::string& s);
40
41// Return the extension of the file
42ARROW_EXPORT
43std::string GetAbstractPathExtension(const std::string& s);
44
45// Return the parent directory and basename of an abstract path. Both values may be
46// empty.
47ARROW_EXPORT
48std::pair<std::string, std::string> GetAbstractPathParent(const std::string& s);
49
50// Validate the components of an abstract path.
51ARROW_EXPORT
52Status ValidateAbstractPathParts(const std::vector<std::string>& parts);
53
54// Append a non-empty stem to an abstract path.
55ARROW_EXPORT
56std::string ConcatAbstractPath(const std::string& base, const std::string& stem);
57
58// Make path relative to base, if it starts with base. Otherwise error out.
59ARROW_EXPORT
60Result<std::string> MakeAbstractPathRelative(const std::string& base,
61 const std::string& path);
62
63ARROW_EXPORT
64std::string EnsureLeadingSlash(util::string_view s);
65
66ARROW_EXPORT
67util::string_view RemoveLeadingSlash(util::string_view s);
68
69ARROW_EXPORT
70std::string EnsureTrailingSlash(util::string_view s);
71
72ARROW_EXPORT
73util::string_view RemoveTrailingSlash(util::string_view s);
74
75ARROW_EXPORT
76bool IsAncestorOf(util::string_view ancestor, util::string_view descendant);
77
78ARROW_EXPORT
79util::optional<util::string_view> RemoveAncestor(util::string_view ancestor,
80 util::string_view descendant);
81
82/// Return a vector of ancestors between a base path and a descendant.
83/// For example,
84///
85/// AncestorsFromBasePath("a/b", "a/b/c/d/e") -> ["a/b/c", "a/b/c/d"]
86ARROW_EXPORT
87std::vector<std::string> AncestorsFromBasePath(util::string_view base_path,
88 util::string_view descendant);
89
90/// Given a vector of paths of directories which must be created, produce a the minimal
91/// subset for passing to CreateDir(recursive=true) by removing redundant parent
92/// directories
93ARROW_EXPORT
94std::vector<std::string> MinimalCreateDirSet(std::vector<std::string> dirs);
95
96// Join the components of an abstract path.
97template <class StringIt>
98std::string JoinAbstractPath(StringIt it, StringIt end) {
99 std::string path;
100 for (; it != end; ++it) {
101 if (it->empty()) continue;
102
103 if (!path.empty()) {
104 path += kSep;
105 }
106 path += *it;
107 }
108 return path;
109}
110
111template <class StringRange>
112std::string JoinAbstractPath(const StringRange& range) {
113 return JoinAbstractPath(range.begin(), range.end());
114}
115
116/// Convert slashes to backslashes, on all platforms. Mostly useful for testing.
117ARROW_EXPORT
118std::string ToBackslashes(util::string_view s);
119
120/// Ensure a local path is abstract, by converting backslashes to regular slashes
121/// on Windows. Return the path unchanged on other systems.
122ARROW_EXPORT
123std::string ToSlashes(util::string_view s);
124
125ARROW_EXPORT
126bool IsEmptyPath(util::string_view s);
127
128} // namespace internal
129} // namespace fs
130} // namespace arrow