Skip to content

Commit 37b004e

Browse files
authored
Merge pull request #684 from SteveL-MSFT/empty-reg-value
Fix `registry` resource to allow setting value with no data
2 parents 5e860e2 + e9397db commit 37b004e

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

build.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ if (!$SkipBuild) {
354354

355355
if ($IsWindows) {
356356
Copy-Item "$path/$binary.exe" $target -ErrorAction Ignore -Verbose
357+
Copy-Item "$path/$binary.pdb" $target -ErrorAction Ignore -Verbose
357358
}
358359
else {
359360
Copy-Item "$path/$binary" $target -ErrorAction Ignore -Verbose

registry/locales/en-us.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ unsupportedValueDataType = "Unsupported registry value data type"
4444
tracingInitError = "Unable to set global default tracing subscriber. Tracing is disabled."
4545
debugAttach = "attach debugger to pid %{pid} and press any key to continue"
4646
debugEventReadError = "Error: Failed to read event: %{err}"
47-
debugEventUnexpectedError = "Unexpected event: %{e:?}"
47+
debugEventUnexpectedError = "Unexpected event: %{e}"
4848

4949
[registry_helper]
50-
whatIfCreateKey = "key: %{subkey} not found, would create it"
50+
whatIfCreateKey = "Key '%{subkey}' not found, would create it"
5151
removeErrorKeyNotExist = "Key already does not exist"
5252
removeDeletingSubKey = "Deleting subkey '%{name}' using %{parent}"

registry/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub enum RegistryValueData {
1212
DWord(u32),
1313
MultiString(Vec<String>),
1414
QWord(u64),
15+
None,
1516
}
1617

1718
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]

registry/src/registry_helper.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl RegistryHelper {
7272
Ok(Registry {
7373
key_path: self.config.key_path.clone(),
7474
value_name: Some(value_name.clone()),
75-
value_data: Some(convert_reg_value(&value)?),
75+
value_data: convert_reg_value(&value)?,
7676
..Default::default()
7777
})
7878
} else {
@@ -100,7 +100,7 @@ impl RegistryHelper {
100100
};
101101

102102
if self.what_if {
103-
what_if_metadata.push(t!("registry_helper.whatIfCreateKey", "subkey" => subkey).to_string());
103+
what_if_metadata.push(t!("registry_helper.whatIfCreateKey", subkey = subkey).to_string());
104104
}
105105
else {
106106
reg_key = reg_key.create(path, Security::CreateSubKey)?;
@@ -116,8 +116,13 @@ impl RegistryHelper {
116116
Err(e) => return self.handle_error_or_what_if(e)
117117
};
118118

119-
if let Some(value_data) = &self.config.value_data {
120-
let Ok(value_name) = U16CString::from_str(self.config.value_name.as_ref().unwrap()) else {
119+
if let Some(value_name) = &self.config.value_name {
120+
let value_data = match &self.config.value_data {
121+
Some(value_data) => value_data,
122+
None => &RegistryValueData::None,
123+
};
124+
125+
let Ok(value_name) = U16CString::from_str(value_name) else {
121126
return self.handle_error_or_what_if(RegistryError::Utf16Conversion("valueName".to_string()));
122127
};
123128

@@ -153,12 +158,15 @@ impl RegistryHelper {
153158
RegistryValueData::QWord(q) => {
154159
Data::U64(*q)
155160
},
161+
RegistryValueData::None => {
162+
Data::None
163+
},
156164
};
157165

158166
if self.what_if {
159167
return Ok(Some(Registry {
160168
key_path: self.config.key_path.clone(),
161-
value_data: Some(convert_reg_value(&data)?),
169+
value_data: convert_reg_value(&data)?,
162170
value_name: self.config.value_name.clone(),
163171
metadata: if what_if_metadata.is_empty() { None } else { Some(Metadata { what_if: Some(what_if_metadata) })},
164172
..Default::default()
@@ -222,8 +230,12 @@ impl RegistryHelper {
222230
let parent_key: RegKey;
223231
let mut subkeys: Vec<&str> = Vec::new();
224232
let parent_key_path = get_parent_key_path(&self.subkey);
225-
let subkey_name = &self.subkey[parent_key_path.len() + 1..];
226-
subkeys.push(subkey_name);
233+
let subkey_name = if parent_key_path.is_empty() { &self.subkey } else {
234+
&self.subkey[parent_key_path.len() + 1..]
235+
};
236+
if !subkey_name.is_empty() {
237+
subkeys.push(subkey_name);
238+
}
227239
let mut current_key_path = parent_key_path;
228240

229241
loop {
@@ -305,17 +317,18 @@ fn get_parent_key_path(key_path: &str) -> &str {
305317
}
306318
}
307319

308-
fn convert_reg_value(value: &Data) -> Result<RegistryValueData, RegistryError> {
320+
fn convert_reg_value(value: &Data) -> Result<Option<RegistryValueData>, RegistryError> {
309321
match value {
310-
Data::String(s) => Ok(RegistryValueData::String(s.to_string_lossy())),
311-
Data::ExpandString(s) => Ok(RegistryValueData::ExpandString(s.to_string_lossy())),
312-
Data::Binary(b) => Ok(RegistryValueData::Binary(b.clone())),
313-
Data::U32(d) => Ok(RegistryValueData::DWord(*d)),
322+
Data::String(s) => Ok(Some(RegistryValueData::String(s.to_string_lossy()))),
323+
Data::ExpandString(s) => Ok(Some(RegistryValueData::ExpandString(s.to_string_lossy()))),
324+
Data::Binary(b) => Ok(Some(RegistryValueData::Binary(b.clone()))),
325+
Data::U32(d) => Ok(Some(RegistryValueData::DWord(*d))),
314326
Data::MultiString(m) => {
315327
let m: Vec<String> = m.iter().map(|s| s.to_string_lossy()).collect();
316-
Ok(RegistryValueData::MultiString(m))
328+
Ok(Some(RegistryValueData::MultiString(m)))
317329
},
318-
Data::U64(q) => Ok(RegistryValueData::QWord(*q)),
330+
Data::U64(q) => Ok(Some(RegistryValueData::QWord(*q))),
331+
Data::None => Ok(None),
319332
_ => Err(RegistryError::UnsupportedValueDataType)
320333
}
321334
}

registry/tests/registry.config.set.tests.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,31 @@ Describe 'registry config set tests' {
7272

7373
Get-Item -Path 'HKCU:\1\2' -ErrorAction Ignore | Should -BeNullOrEmpty
7474
}
75+
76+
It 'Can set value without data' -Skip:(!$IsWindows) {
77+
$configYaml = @'
78+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
79+
resources:
80+
- name: Key
81+
type: Microsoft.Windows/Registry
82+
properties:
83+
keyPath: 'HKCU\1'
84+
valueName: Test
85+
_exist: true
86+
'@
87+
88+
$out = dsc config set -i $configYaml | ConvertFrom-Json
89+
$LASTEXITCODE | Should -Be 0
90+
$out.results[0].result.afterState.keyPath | Should -BeExactly 'HKCU\1'
91+
$out.results[0].result.afterState.valueName | Should -BeExactly 'Test'
92+
$out.results[0].result.afterState.valueData | Should -BeNullOrEmpty
93+
94+
$out = dsc config get -i $configYaml | ConvertFrom-Json
95+
$LASTEXITCODE | Should -Be 0
96+
$out.results[0].result.actualState.keyPath | Should -BeExactly 'HKCU\1'
97+
$out.results[0].result.actualState.valueName | Should -BeExactly 'Test'
98+
$out.results[0].result.actualState.valueData | Should -BeNullOrEmpty
99+
100+
Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
101+
}
75102
}

0 commit comments

Comments
 (0)