2 * Copyright 2014 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 namespace bsys = ::boost::system;
25 bool skipPrefix(const path& pth, const path& prefix, path::const_iterator& it) {
27 for (auto& p : prefix) {
28 if (it == pth.end()) {
32 // Should only occur at the end, if prefix ends with a slash
43 bool starts_with(const path& pth, const path& prefix) {
44 path::const_iterator it;
45 return skipPrefix(pth, prefix, it);
48 path remove_prefix(const path& pth, const path& prefix) {
49 path::const_iterator it;
50 if (!skipPrefix(pth, prefix, it)) {
51 throw filesystem_error(
52 "Path does not start with prefix",
54 bsys::errc::make_error_code(bsys::errc::invalid_argument));
58 for (; it != pth.end(); ++it) {
65 path canonical_parent(const path& pth, const path& base) {
66 return canonical(pth.parent_path(), base) / pth.filename();