blob: f312d17874d62e986841b25c8ad4d9c33ff8a508 [file] [log] [blame]
Songchun Fan59eee562019-10-24 16:46:06 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Yurii Zubrytskyid6688852019-11-26 17:39:08 -080019#include <iterator>
20#include <optional>
Songchun Fan59eee562019-10-24 16:46:06 -070021#include <string>
22#include <string_view>
23
Yurii Zubrytskyid6688852019-11-26 17:39:08 -080024#include <dirent.h>
25
Songchun Fan59eee562019-10-24 16:46:06 -070026namespace android::incfs::path {
27
Yurii Zubrytskyid6688852019-11-26 17:39:08 -080028namespace details {
29void appendNextPath(std::string& res, std::string_view c);
30}
31
Songchun Fan59eee562019-10-24 16:46:06 -070032std::string fromFd(int fd);
Yurii Zubrytskyid6688852019-11-26 17:39:08 -080033bool isAbsolute(std::string_view path);
34std::string normalize(std::string_view path);
35
36std::string_view relativize(std::string_view parent, std::string_view nested);
37inline std::string_view relativize(const char* parent, const char* nested) {
38 return relativize(std::string_view(parent), std::string_view(nested));
39}
40inline std::string_view relativize(std::string_view parent, const char* nested) {
41 return relativize(parent, std::string_view(nested));
42}
43inline std::string_view relativize(const char* parent, std::string_view nested) {
44 return relativize(std::string_view(parent), nested);
45}
46
47std::string_view relativize(std::string&& parent, std::string_view nested) = delete;
48std::string_view relativize(std::string_view parent, std::string&& nested) = delete;
49
50// Note: some system headers #define 'dirname' and 'basename' as macros
51std::string_view dirName(std::string_view path);
52std::string_view baseName(std::string_view path);
53
54// Split the |full| path into its directory and basename components.
55// This modifies the input string to null-terminate the output directory
56std::pair<std::string_view, std::string_view> splitDirBase(std::string& full);
57
58int isEmptyDir(std::string_view dir);
59bool startsWith(std::string_view path, std::string_view prefix);
Yurii Zubrytskyi003751d2020-04-16 12:45:39 -070060bool endsWith(std::string_view path, std::string_view prefix);
Yurii Zubrytskyid6688852019-11-26 17:39:08 -080061
62inline auto openDir(const char* path) {
63 auto dir = std::unique_ptr<DIR, decltype(&closedir)>(::opendir(path), &::closedir);
64 return dir;
65}
66
67template <class... Paths>
68std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
69 std::string result;
70 {
71 using std::size;
72 result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
73 }
74 result.assign(first);
75 (details::appendNextPath(result, second), ..., details::appendNextPath(result, paths));
76 return result;
77}
Songchun Fan59eee562019-10-24 16:46:06 -070078
79} // namespace android::incfs::path