85606d1c53cc06346f1c764599deef30033779b2
[folly.git] / folly / experimental / io / FsUtil.h
1 /*
2  * Copyright 2016 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 #pragma once
18
19 #include <boost/filesystem.hpp>
20
21 namespace folly {
22 namespace fs {
23
24 // Functions defined in this file are meant to extend the
25 // boost::filesystem library; functions will be named according to boost's
26 // naming conventions instead of ours.  For convenience, import the
27 // boost::filesystem namespace into folly::fs.
28 using namespace ::boost::filesystem;
29
30 /**
31  * Check whether "path" starts with "prefix".
32  * That is, if prefix has n path elements, then the first n elements of
33  * path must be the same as prefix.
34  *
35  * There is a special case if prefix ends with a slash:
36  * /foo/bar/ is not a prefix of /foo/bar, but both /foo/bar and /foo/bar/
37  * are prefixes of /foo/bar/baz.
38  */
39 bool starts_with(const path& p, const path& prefix);
40
41 /**
42  * If "path" starts with "prefix", return "path" with "prefix" removed.
43  * Otherwise, throw filesystem_error.
44  */
45 path remove_prefix(const path& p, const path& prefix);
46
47 /**
48  * Canonicalize the parent path, leaving the filename (last component)
49  * unchanged.  You may use this before creating a file instead of
50  * boost::filesystem::canonical, which requires that the entire path exists.
51  */
52 path canonical_parent(const path& p, const path& basePath = current_path());
53
54 /**
55  * Get the path to the current executable.
56  *
57  * Note that this is not reliable and not recommended in general; it may not be
58  * implemented on your platform (in which case it will throw), the executable
59  * might have been moved or replaced while running, and applications comprising
60  * of multiple executables should use some form of configuration system to
61  * find the other executables rather than relying on relative paths from one
62  * to another.
63  *
64  * So this should only be used for tests, logging, or other innocuous purposes.
65  */
66 path executable_path();
67
68 }  // namespace fs
69 }  // namespace folly