| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | ProcessSettings.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the implementation of the WinRT wrapper for the WSLC SDK ProcessSettings class. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "precomp.h" |
| 16 | #include "ProcessSettings.h" |
| 17 | #include "Process.h" |
| 18 | #include "Microsoft.WSL.Containers.ProcessSettings.g.cpp" |
| 19 | |
| 20 | namespace winrt::Microsoft::WSL::Containers::implementation { |
| 21 | |
| 22 | StringArray::StringArray(size_t capacity) |
| 23 | { |
| 24 | m_strings.reserve(capacity); |
| 25 | m_rawStrings.reserve(capacity); |
| 26 | } |
| 27 | |
| 28 | void StringArray::Add(std::string&& s) |
| 29 | { |
| 30 | m_strings.push_back(std::move(s)); |
| 31 | m_rawStrings.push_back(m_strings.back().c_str()); |
| 32 | } |
| 33 | |
| 34 | PCSTR* StringArray::GetRawPointer() |
| 35 | { |
| 36 | return m_rawStrings.data(); |
| 37 | } |
| 38 | |
| 39 | hstring ProcessSettings::WorkingDirectory() |
| 40 | { |
| 41 | return winrt::to_hstring(m_workingDirectory); |
| 42 | } |
| 43 | |
| 44 | void ProcessSettings::WorkingDirectory(hstring const& value) |
| 45 | { |
| 46 | if (m_processSettings) |
| 47 | { |
| 48 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 49 | } |
| 50 | |
| 51 | m_workingDirectory = winrt::to_string(value); |
| 52 | } |
| 53 | |
| 54 | winrt::Windows::Foundation::Collections::IVector<hstring> ProcessSettings::CommandLine() |
| 55 | { |
| 56 | return m_commandLine; |
| 57 | } |
| 58 | |
| 59 | void ProcessSettings::CommandLine(winrt::Windows::Foundation::Collections::IVector<hstring> const& value) |
| 60 | { |
| 61 | if (m_processSettings) |
| 62 | { |
| 63 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 64 | } |
| 65 | |
| 66 | if (!value) |
| 67 | { |
| 68 | throw winrt::hresult_error(E_POINTER, L"CommandLine cannot be null"); |
| 69 | } |
| 70 | |
| 71 | m_commandLine = value; |
| 72 | } |
| 73 | |
| 74 | winrt::Windows::Foundation::Collections::IMap<hstring, hstring> ProcessSettings::EnvironmentVariables() |
| 75 | { |
| 76 | return m_environmentVariables; |
| 77 | } |
| 78 | |
| 79 | void ProcessSettings::EnvironmentVariables(winrt::Windows::Foundation::Collections::IMap<hstring, hstring> const& value) |
| 80 | { |
| 81 | if (m_processSettings) |
| 82 | { |
| 83 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 84 | } |
| 85 | |
| 86 | if (!value) |
| 87 | { |
| 88 | throw winrt::hresult_error(E_POINTER, L"EnvironmentVariables cannot be null"); |
| 89 | } |
| 90 | |
| 91 | m_environmentVariables = value; |
| 92 | } |
| 93 | |
| 94 | winrt::Microsoft::WSL::Containers::ProcessOutputMode ProcessSettings::OutputMode() |
| 95 | { |
| 96 | return m_outputMode; |
| 97 | } |
| 98 | |
| 99 | void ProcessSettings::OutputMode(winrt::Microsoft::WSL::Containers::ProcessOutputMode const& value) |
| 100 | { |
| 101 | if (m_processSettings) |
| 102 | { |
| 103 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 104 | } |
| 105 | |
| 106 | m_outputMode = value; |
| 107 | } |
| 108 | |
| 109 | WslcProcessSettings* ProcessSettings::ToStructPointer() |
| 110 | { |
| 111 | if (m_processSettings) |
| 112 | { |
| 113 | return m_processSettings.get(); |
| 114 | } |
| 115 | |
| 116 | m_processSettings = std::make_unique<WslcProcessSettings>(); |
| 117 | winrt::check_hresult(WslcInitProcessSettings(m_processSettings.get())); |
| 118 | |
| 119 | if (!m_workingDirectory.empty()) |
| 120 | { |
| 121 | winrt::check_hresult(WslcSetProcessSettingsWorkingDirectory(m_processSettings.get(), m_workingDirectory.c_str())); |
| 122 | } |
| 123 | |
| 124 | if (m_commandLine && m_commandLine.Size() > 0) |
| 125 | { |
| 126 | auto argc = m_commandLine.Size(); |
| 127 | m_commandLineStrings = StringArray{argc}; |
| 128 | for (auto const& arg : m_commandLine) |
| 129 | { |
| 130 | m_commandLineStrings.Add(winrt::to_string(arg)); |
| 131 | } |
| 132 | |
| 133 | winrt::check_hresult(WslcSetProcessSettingsCmdLine(m_processSettings.get(), m_commandLineStrings.GetRawPointer(), argc)); |
| 134 | } |
| 135 | |
| 136 | if (m_environmentVariables.Size() > 0) |
| 137 | { |
| 138 | auto size = m_environmentVariables.Size(); |
| 139 | m_envStrings = StringArray{size}; |
| 140 | for (auto const& [key, value] : m_environmentVariables) |
| 141 | { |
| 142 | m_envStrings.Add(winrt::to_string(key) + "=" + winrt::to_string(value)); |
| 143 | } |
| 144 | |
| 145 | winrt::check_hresult(WslcSetProcessSettingsEnvVariables(m_processSettings.get(), m_envStrings.GetRawPointer(), size)); |
| 146 | } |
| 147 | |
| 148 | return m_processSettings.get(); |
| 149 | } |
| 150 | |
| 151 | } // namespace winrt::Microsoft::WSL::Containers::implementation |