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