Codemod: use #include angle brackets in folly and thrift
[folly.git] / folly / experimental / io / FsUtil.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
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 #include <folly/experimental/io/FsUtil.h>
18
19 namespace bsys = ::boost::system;
20
21 namespace folly {
22 namespace fs {
23
24 namespace {
25 bool skipPrefix(const path& pth, const path& prefix, path::const_iterator& it) {
26   it = pth.begin();
27   for (auto& p : prefix) {
28     if (it == pth.end()) {
29       return false;
30     }
31     if (p == ".") {
32       // Should only occur at the end, if prefix ends with a slash
33       continue;
34     }
35     if (*it++ != p) {
36       return false;
37     }
38   }
39   return true;
40 }
41 }  // namespace
42
43 bool starts_with(const path& pth, const path& prefix) {
44   path::const_iterator it;
45   return skipPrefix(pth, prefix, it);
46 }
47
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",
53         pth, prefix,
54         bsys::errc::make_error_code(bsys::errc::invalid_argument));
55   }
56
57   path p;
58   for (; it != pth.end(); ++it) {
59     p /= *it;
60   }
61
62   return p;
63 }
64
65 path canonical_parent(const path& pth, const path& base) {
66   return canonical(pth.parent_path(), base) / pth.filename();
67 }
68
69 }  // namespace fs
70 }  // namespace folly
71