FsUtil.h 2.4 KB

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