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