master
h 117 lines 4.13 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 DistributionRegistration.h
8
9 Abstract:
10
11 This file contains the DistributionRegistration helper class definition.
12
13 --*/
14
15 #pragma once
16
17 namespace wsl::windows::service {
18
19 template <typename T>
20 struct DistributionProperty
21 {
22 LPCWSTR Name;
23 T (*Transform)(T) = nullptr;
24 };
25
26 template <typename T>
27 struct DistributionPropertyWithDefault : public DistributionProperty<T>
28 {
29 DistributionPropertyWithDefault(LPCWSTR Name, T DefaultValue, T (*Transform)(T) = nullptr) :
30 DistributionProperty<T>(Name, Transform), DefaultValue(DefaultValue)
31 {
32 }
33
34 T DefaultValue;
35 };
36
37 template <typename T>
38 struct ExpectedProperty : public DistributionProperty<T>
39 {
40 ExpectedProperty(LPCWSTR Name, T (*Transform)(T) = nullptr) : DistributionProperty<T>(Name, Transform)
41 {
42 }
43 };
44
45 class DistributionRegistration
46 {
47 public:
48 static DistributionRegistration Create(
49 HKEY LxssKey,
50 const std::optional<GUID>& Id,
51 LPCWSTR Name,
52 ULONG Version,
53 LPCWSTR BasePath,
54 ULONG Flags,
55 ULONG DefaultUID,
56 LPCWSTR PackageFamilyName,
57 LPCWSTR VhdFileName,
58 bool EnableOobe);
59
60 static DistributionRegistration Open(HKEY LxssKey, const GUID& Id);
61 static DistributionRegistration OpenOrDefault(HKEY LxssKey, const GUID* Id);
62 static std::optional<DistributionRegistration> OpenDefault(HKEY LxssKey);
63 static void SetDefault(HKEY LxssKey, const DistributionRegistration& Distro);
64 static void DeleteDefault(HKEY LxssKey);
65
66 DistributionRegistration() = default;
67 const GUID& Id() const;
68
69 std::wstring Read(const DistributionPropertyWithDefault<LPCWSTR>& property) const;
70 std::optional<std::wstring> Read(const DistributionProperty<LPCWSTR>& property) const;
71 DWORD Read(const DistributionPropertyWithDefault<DWORD>& property) const;
72 std::vector<std::string> Read(const DistributionPropertyWithDefault<std::vector<std::string>>& Property) const;
73 std::wstring Read(const ExpectedProperty<LPCWSTR>& property) const;
74
75 void Write(const DistributionProperty<LPCWSTR>& property, LPCWSTR Value) const;
76 void Write(const DistributionProperty<DWORD>& property, DWORD Value) const;
77 void Delete(HKEY LxssKey) const;
78
79 std::filesystem::path ReadVhdFilePath() const;
80
81 static DWORD ApplyGlobalFlagsOverride(DWORD Flags);
82
83 private:
84 DistributionRegistration(const GUID& Id, wil::unique_hkey&& key);
85
86 GUID m_id{};
87 wil::unique_hkey m_key;
88 };
89
90 namespace Property {
91 inline DistributionPropertyWithDefault<LPCWSTR> PackageFamilyName{L"PackageFamilyName", L""};
92 inline DistributionPropertyWithDefault<LPCWSTR> KernelCommandLine{L"KernelCommandLine", L""};
93 inline DistributionPropertyWithDefault<LPCWSTR> VhdFileName{L"VhdFileName", LXSS_VM_MODE_VHD_NAME};
94 inline ExpectedProperty<LPCWSTR> Name{L"DistributionName"};
95 inline ExpectedProperty<LPCWSTR> BasePath{L"BasePath"};
96 inline DistributionProperty<LPCWSTR> Flavor{L"Flavor"};
97 inline DistributionProperty<LPCWSTR> OsVersion{L"OsVersion"};
98 inline DistributionProperty<LPCWSTR> ShortcutPath{L"ShortcutPath"};
99 inline DistributionProperty<LPCWSTR> TerminalProfilePath{L"TerminalProfilePath"};
100
101 inline DistributionPropertyWithDefault<DWORD> Version{L"Version", LXSS_DISTRO_VERSION_CURRENT};
102 inline DistributionPropertyWithDefault<DWORD> Flags{L"Flags", LXSS_DISTRO_FLAGS_DEFAULT, &DistributionRegistration::ApplyGlobalFlagsOverride};
103 inline DistributionPropertyWithDefault<DWORD> DefaultUid{L"DefaultUid", LX_UID_ROOT};
104 inline DistributionPropertyWithDefault<DWORD> State{L"State", LxssDistributionStateInvalid};
105 inline DistributionPropertyWithDefault<DWORD> RunOOBE{L"RunOOBE", 0};
106 inline DistributionPropertyWithDefault<DWORD> Modern{L"Modern", 0};
107
108 inline DistributionPropertyWithDefault<std::vector<std::string>> DefaultEnvironment{
109 L"DefaultEnvironment",
110 {"HOSTTYPE=" DISTRO_HOSTTYPE,
111 "LANG=en_US.UTF-8",
112 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
113 "TERM=xterm-256color"}};
114
115 } // namespace Property
116
117 } // namespace wsl::windows::service