2 * Copyright 2015 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 #ifndef FOLLY_IO_FSUTIL_H_
18 #define FOLLY_IO_FSUTIL_H_
20 #include <boost/filesystem.hpp>
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;
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.
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.
40 bool starts_with(const path& p, const path& prefix);
43 * If "path" starts with "prefix", return "path" with "prefix" removed.
44 * Otherwise, throw filesystem_error.
46 path remove_prefix(const path& p, const path& prefix);
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.
53 path canonical_parent(const path& p, const path& basePath = current_path());
56 * Get the path to the current executable.
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
65 * So this should only be used for tests, logging, or other innocuous purposes.
67 path executable_path();
72 #endif /* FOLLY_IO_FSUTIL_H_ */