| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | VhdOptions.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the implementation of the WinRT wrapper for the WSLC SDK VhdOptions class. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "precomp.h" |
| 16 | #include "VhdOptions.h" |
| 17 | #include "Microsoft.WSL.Containers.VhdOptions.g.cpp" |
| 18 | |
| 19 | namespace winrt::Microsoft::WSL::Containers::implementation { |
| 20 | |
| 21 | VhdOptions::VhdOptions(hstring const& name, uint64_t size, VhdType const& type) : |
| 22 | m_name(winrt::to_string(name)), m_size(size), m_type(type) |
| 23 | { |
| 24 | if (size == 0) |
| 25 | { |
| 26 | throw hresult_invalid_argument(L"VHD size cannot be zero"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | hstring VhdOptions::Name() |
| 31 | { |
| 32 | return winrt::to_hstring(m_name); |
| 33 | } |
| 34 | |
| 35 | void VhdOptions::Name(hstring const& value) |
| 36 | { |
| 37 | if (m_vhdOptions) |
| 38 | { |
| 39 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 40 | } |
| 41 | |
| 42 | m_name = winrt::to_string(value); |
| 43 | } |
| 44 | |
| 45 | uint64_t VhdOptions::Size() |
| 46 | { |
| 47 | return m_size; |
| 48 | } |
| 49 | |
| 50 | void VhdOptions::Size(uint64_t value) |
| 51 | { |
| 52 | if (m_vhdOptions) |
| 53 | { |
| 54 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 55 | } |
| 56 | |
| 57 | if (value == 0) |
| 58 | { |
| 59 | throw hresult_invalid_argument(L"VHD size cannot be zero"); |
| 60 | } |
| 61 | |
| 62 | m_size = value; |
| 63 | } |
| 64 | |
| 65 | VhdType VhdOptions::Type() |
| 66 | { |
| 67 | return m_type; |
| 68 | } |
| 69 | |
| 70 | void VhdOptions::Type(VhdType const& value) |
| 71 | { |
| 72 | if (m_vhdOptions) |
| 73 | { |
| 74 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 75 | } |
| 76 | |
| 77 | m_type = value; |
| 78 | } |
| 79 | |
| 80 | winrt::Windows::Foundation::IReference<winrt::Microsoft::WSL::Containers::VhdOwner> VhdOptions::Owner() |
| 81 | { |
| 82 | return m_owner; |
| 83 | } |
| 84 | |
| 85 | void VhdOptions::Owner(winrt::Windows::Foundation::IReference<winrt::Microsoft::WSL::Containers::VhdOwner> const& value) |
| 86 | { |
| 87 | if (m_vhdOptions) |
| 88 | { |
| 89 | throw hresult_illegal_state_change(L"Cannot change value after options have been applied"); |
| 90 | } |
| 91 | |
| 92 | m_owner = value; |
| 93 | } |
| 94 | |
| 95 | WslcVhdRequirements* VhdOptions::ToStructPointer() |
| 96 | { |
| 97 | if (!m_vhdOptions) |
| 98 | { |
| 99 | m_vhdOptions = std::make_unique<WslcVhdRequirements>(); |
| 100 | m_vhdOptions->name = m_name.c_str(); |
| 101 | m_vhdOptions->sizeBytes = m_size; |
| 102 | m_vhdOptions->type = static_cast<WslcVhdType>(m_type); |
| 103 | |
| 104 | if (m_owner != nullptr) |
| 105 | { |
| 106 | auto owner = m_owner.Value(); |
| 107 | m_vhdOptions->uid = owner.Uid; |
| 108 | m_vhdOptions->gid = owner.Gid; |
| 109 | WI_SetFlag(m_vhdOptions->flags, WSLC_VHD_REQ_FLAG_OWNER); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return m_vhdOptions.get(); |
| 114 | } |
| 115 | |
| 116 | } // namespace winrt::Microsoft::WSL::Containers::implementation |