2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/io/FsUtil.h>
19 #include <folly/Exception.h>
21 namespace bsys = ::boost::system;
27 bool skipPrefix(const path& pth, const path& prefix, path::const_iterator& it) {
29 for (auto& p : prefix) {
30 if (it == pth.end()) {
34 // Should only occur at the end, if prefix ends with a slash
45 bool starts_with(const path& pth, const path& prefix) {
46 path::const_iterator it;
47 return skipPrefix(pth, prefix, it);
50 path remove_prefix(const path& pth, const path& prefix) {
51 path::const_iterator it;
52 if (!skipPrefix(pth, prefix, it)) {
53 throw filesystem_error(
54 "Path does not start with prefix",
56 bsys::errc::make_error_code(bsys::errc::invalid_argument));
60 for (; it != pth.end(); ++it) {
67 path canonical_parent(const path& pth, const path& base) {
68 return canonical(pth.parent_path(), base) / pth.filename();
71 path executable_path() {
72 return read_symlink("/proc/self/exe");