Songchun Fan | 59eee56 | 2019-10-24 16:46:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Zubrytskyi | d668885 | 2019-11-26 17:39:08 -0800 | [diff] [blame] | 19 | #include <iterator> |
| 20 | #include <optional> |
Songchun Fan | 59eee56 | 2019-10-24 16:46:06 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <string_view> |
| 23 | |
Yurii Zubrytskyi | d668885 | 2019-11-26 17:39:08 -0800 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | |
Songchun Fan | 59eee56 | 2019-10-24 16:46:06 -0700 | [diff] [blame] | 26 | namespace android::incfs::path { |
| 27 | |
Yurii Zubrytskyi | d668885 | 2019-11-26 17:39:08 -0800 | [diff] [blame] | 28 | namespace details { |
| 29 | void appendNextPath(std::string& res, std::string_view c); |
| 30 | } |
| 31 | |
Songchun Fan | 59eee56 | 2019-10-24 16:46:06 -0700 | [diff] [blame] | 32 | std::string fromFd(int fd); |
Yurii Zubrytskyi | d668885 | 2019-11-26 17:39:08 -0800 | [diff] [blame] | 33 | bool isAbsolute(std::string_view path); |
| 34 | std::string normalize(std::string_view path); |
| 35 | |
| 36 | std::string_view relativize(std::string_view parent, std::string_view nested); |
| 37 | inline std::string_view relativize(const char* parent, const char* nested) { |
| 38 | return relativize(std::string_view(parent), std::string_view(nested)); |
| 39 | } |
| 40 | inline std::string_view relativize(std::string_view parent, const char* nested) { |
| 41 | return relativize(parent, std::string_view(nested)); |
| 42 | } |
| 43 | inline std::string_view relativize(const char* parent, std::string_view nested) { |
| 44 | return relativize(std::string_view(parent), nested); |
| 45 | } |
| 46 | |
| 47 | std::string_view relativize(std::string&& parent, std::string_view nested) = delete; |
| 48 | std::string_view relativize(std::string_view parent, std::string&& nested) = delete; |
| 49 | |
| 50 | // Note: some system headers #define 'dirname' and 'basename' as macros |
| 51 | std::string_view dirName(std::string_view path); |
| 52 | std::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 |
| 56 | std::pair<std::string_view, std::string_view> splitDirBase(std::string& full); |
| 57 | |
| 58 | int isEmptyDir(std::string_view dir); |
| 59 | bool startsWith(std::string_view path, std::string_view prefix); |
Yurii Zubrytskyi | 003751d | 2020-04-16 12:45:39 -0700 | [diff] [blame] | 60 | bool endsWith(std::string_view path, std::string_view prefix); |
Yurii Zubrytskyi | d668885 | 2019-11-26 17:39:08 -0800 | [diff] [blame] | 61 | |
| 62 | inline auto openDir(const char* path) { |
| 63 | auto dir = std::unique_ptr<DIR, decltype(&closedir)>(::opendir(path), &::closedir); |
| 64 | return dir; |
| 65 | } |
| 66 | |
| 67 | template <class... Paths> |
| 68 | std::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 Fan | 59eee56 | 2019-10-24 16:46:06 -0700 | [diff] [blame] | 78 | |
| 79 | } // namespace android::incfs::path |