EnvUtil.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2017-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 <map>
  18. #include <string>
  19. #include <unordered_map>
  20. #include <vector>
  21. #include <folly/CPortability.h>
  22. #include <folly/Memory.h>
  23. namespace folly {
  24. namespace experimental {
  25. // Class to model the process environment in idiomatic C++
  26. //
  27. // Changes to the modeled environment do not change the process environment
  28. // unless `setAsCurrentEnvironment()` is called.
  29. struct EnvironmentState {
  30. using EnvType = std::unordered_map<std::string, std::string>;
  31. // Returns an EnvironmentState containing a copy of the current process
  32. // environment. Subsequent changes to the process environment do not
  33. // alter the stored model. If the process environment is altered during the
  34. // execution of this method the results are not defined.
  35. //
  36. // Throws MalformedEnvironment if the process environment cannot be modeled.
  37. static EnvironmentState fromCurrentEnvironment();
  38. // Returns an empty EnvironmentState
  39. static EnvironmentState empty() {
  40. return {};
  41. }
  42. explicit EnvironmentState(EnvType const& env) : env_(env) {}
  43. explicit EnvironmentState(EnvType&& env) : env_(std::move(env)) {}
  44. // Get the model environment for querying.
  45. EnvType const& operator*() const {
  46. return env_;
  47. }
  48. EnvType const* operator->() const {
  49. return &env_;
  50. }
  51. // Get the model environment for mutation or querying.
  52. EnvType& operator*() {
  53. return env_;
  54. }
  55. EnvType* operator->() {
  56. return &env_;
  57. }
  58. // Update the process environment with the one in the stored model.
  59. // Subsequent changes to the model do not alter the process environment. The
  60. // state of the process environment during execution of this method is not
  61. // defined. If the process environment is altered by another thread during the
  62. // execution of this method the results are not defined.
  63. void setAsCurrentEnvironment();
  64. // Get a copy of the model environment in the form used by `folly::Subprocess`
  65. std::vector<std::string> toVector() const;
  66. // Get a copy of the model environment in the form commonly used by C routines
  67. // such as execve, execle, etc. Example usage:
  68. //
  69. // EnvironmentState forChild{};
  70. // ... manipulate `forChild` as needed ...
  71. // execve("/bin/program",pArgs,forChild.toPointerArray().get());
  72. std::unique_ptr<char*, void (*)(char**)> toPointerArray() const;
  73. private:
  74. EnvironmentState() {}
  75. EnvType env_;
  76. };
  77. struct FOLLY_EXPORT MalformedEnvironment : std::runtime_error {
  78. using std::runtime_error::runtime_error;
  79. };
  80. } // namespace experimental
  81. namespace test {
  82. // RAII class allowing scoped changes to the process environment. The
  83. // environment state at the time of its construction is restored at the time
  84. // of its destruction.
  85. struct EnvVarSaver {
  86. EnvVarSaver()
  87. : state_(std::make_unique<experimental::EnvironmentState>(
  88. experimental::EnvironmentState::fromCurrentEnvironment())) {}
  89. EnvVarSaver(EnvVarSaver&& other) noexcept : state_(std::move(other.state_)) {}
  90. EnvVarSaver& operator=(EnvVarSaver&& other) noexcept {
  91. state_ = std::move(other.state_);
  92. return *this;
  93. }
  94. ~EnvVarSaver() {
  95. if (state_) {
  96. state_->setAsCurrentEnvironment();
  97. }
  98. }
  99. private:
  100. std::unique_ptr<experimental::EnvironmentState> state_;
  101. };
  102. } // namespace test
  103. } // namespace folly