1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/util/delete_reg_value_work_item.h"
#include "base/logging.h"
#include "base/win/registry.h"
#include "chrome/installer/util/logging_installer.h"
using base::win::RegKey;
DeleteRegValueWorkItem::DeleteRegValueWorkItem(HKEY predefined_root,
const std::wstring& key_path,
const std::wstring& value_name,
bool is_str_type)
: predefined_root_(predefined_root),
key_path_(key_path),
value_name_(value_name),
is_str_type_(is_str_type),
status_(DELETE_VALUE),
old_dw_(0) {
}
DeleteRegValueWorkItem::~DeleteRegValueWorkItem() {
}
bool DeleteRegValueWorkItem::Do() {
if (status_ != DELETE_VALUE) {
// we already did something.
LOG(ERROR) << "multiple calls to Do()";
return false;
}
status_ = VALUE_UNCHANGED;
// A big flaw in the RegKey implementation is that all error information
// (besides success/failure) is lost in the translation from LSTATUS to bool.
// So, we resort to direct API calls here. :-/
HKEY raw_key = NULL;
LSTATUS err = RegOpenKeyEx(predefined_root_, key_path_.c_str(), 0,
KEY_READ, &raw_key);
if (err != ERROR_SUCCESS) {
if (err == ERROR_FILE_NOT_FOUND) {
LOG(INFO) << "(delete value) can not open " << key_path_;
status_ = VALUE_NOT_FOUND;
return true;
}
} else {
::RegCloseKey(raw_key);
}
RegKey key;
bool result = false;
if (!key.Open(predefined_root_, key_path_.c_str(), KEY_READ | KEY_WRITE)) {
LOG(ERROR) << "can not open " << key_path_;
} else if (!key.ValueExists(value_name_.c_str())) {
status_ = VALUE_NOT_FOUND;
result = true;
// Read previous value for rollback and delete
} else if (((is_str_type_ && key.ReadValue(value_name_.c_str(),
&old_str_)) ||
(!is_str_type_ && key.ReadValueDW(value_name_.c_str(),
&old_dw_))) &&
(key.DeleteValue(value_name_.c_str()))) {
status_ = VALUE_DELETED;
result = true;
} else {
LOG(ERROR) << "failed to read/delete value " << value_name_;
}
return result;
}
void DeleteRegValueWorkItem::Rollback() {
if (status_ == DELETE_VALUE || status_ == VALUE_ROLLED_BACK)
return;
if (status_ == VALUE_UNCHANGED || status_ == VALUE_NOT_FOUND) {
status_ = VALUE_ROLLED_BACK;
VLOG(1) << "rollback: setting unchanged, nothing to do";
return;
}
// At this point only possible state is VALUE_DELETED.
RegKey key;
if (!key.Open(predefined_root_, key_path_.c_str(), KEY_READ | KEY_WRITE)) {
LOG(ERROR) << "rollback: can not open " << key_path_;
// try to restore the previous value
} else if ((is_str_type_ && key.WriteValue(value_name_.c_str(),
old_str_.c_str())) ||
(!is_str_type_ && key.WriteValue(value_name_.c_str(),
old_dw_))) {
status_ = VALUE_ROLLED_BACK;
VLOG(1) << "rollback: restored " << value_name_;
} else {
LOG(ERROR) << "failed to restore value " << value_name_;
}
key.Close();
return;
}
|