]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Ceph - scalable distributed file system | |
3 | * | |
4 | * Copyright (C) 2020 SUSE LINUX GmbH | |
5 | * | |
6 | * This is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License version 2.1, as published by the Free Software | |
9 | * Foundation. See file COPYING. | |
10 | * | |
11 | */ | |
12 | ||
13 | #define dout_context cct | |
14 | #define dout_subsys ceph_subsys_ | |
15 | ||
16 | #include "common/debug.h" | |
17 | #include "common/errno.h" | |
18 | #include "common/win32/registry.h" | |
19 | ||
20 | RegistryKey::RegistryKey(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey, | |
21 | bool create_value): cct(cct_) | |
22 | { | |
23 | DWORD status = RegOpenKeyEx(hRootKey, strKey, 0, KEY_ALL_ACCESS, &hKey); | |
24 | ||
25 | if (status == ERROR_FILE_NOT_FOUND && create_value) | |
26 | { | |
27 | ldout(cct_, 10) << "Creating registry key: " << strKey << dendl; | |
28 | status = RegCreateKeyEx( | |
29 | hRootKey, strKey, 0, NULL, REG_OPTION_NON_VOLATILE, | |
30 | KEY_ALL_ACCESS, NULL, &hKey, NULL); | |
31 | } | |
32 | ||
33 | if (ERROR_SUCCESS != status) { | |
34 | if (ERROR_FILE_NOT_FOUND == status) { | |
35 | missingKey = true; | |
36 | } else { | |
37 | lderr(cct_) << "Error: " << win32_strerror(status) | |
38 | << ". Could not open registry key: " | |
39 | << strKey << dendl; | |
40 | } | |
41 | } | |
42 | } | |
43 | ||
44 | RegistryKey::~RegistryKey() { | |
45 | if (!hKey) | |
46 | return; | |
47 | ||
48 | DWORD status = RegCloseKey(hKey); | |
49 | if (ERROR_SUCCESS != status) { | |
50 | derr << "Error: " << win32_strerror(status) | |
51 | << ". Could not close registry key." << dendl; | |
52 | } else { | |
53 | hKey = NULL; | |
54 | } | |
55 | } | |
56 | ||
57 | int RegistryKey::remove(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey) | |
58 | { | |
59 | DWORD status = RegDeleteKeyEx(hRootKey, strKey, KEY_WOW64_64KEY, 0); | |
60 | ||
61 | if (status == ERROR_FILE_NOT_FOUND) | |
62 | { | |
63 | ldout(cct_, 20) << "Registry key : " << strKey | |
64 | << " does not exist." << dendl; | |
65 | return 0; | |
66 | } | |
67 | ||
68 | if (ERROR_SUCCESS != status) { | |
69 | lderr(cct_) << "Error: " << win32_strerror(status) | |
70 | << ". Could not delete registry key: " | |
71 | << strKey << dendl; | |
72 | return -EINVAL; | |
73 | } | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | int RegistryKey::flush() { | |
79 | DWORD status = RegFlushKey(hKey); | |
80 | if (ERROR_SUCCESS != status) { | |
81 | derr << "Error: " << win32_strerror(status) | |
82 | << ". Could not flush registry key." << dendl; | |
83 | return -EINVAL; | |
84 | } | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | int RegistryKey::set(LPCTSTR lpValue, DWORD data) | |
90 | { | |
91 | DWORD status = RegSetValueEx(hKey, lpValue, 0, REG_DWORD, | |
92 | (LPBYTE)&data, sizeof(DWORD)); | |
93 | if (ERROR_SUCCESS != status) { | |
94 | derr << "Error: " << win32_strerror(status) | |
95 | << ". Could not set registry value: " << (char*)lpValue << dendl; | |
96 | return -EINVAL; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | int RegistryKey::set(LPCTSTR lpValue, std::string data) | |
103 | { | |
104 | DWORD status = RegSetValueEx(hKey, lpValue, 0, REG_SZ, | |
105 | (LPBYTE)data.c_str(), data.length()); | |
106 | if (ERROR_SUCCESS != status) { | |
107 | derr << "Error: " << win32_strerror(status) | |
108 | << ". Could not set registry value: " | |
109 | << (char*)lpValue << dendl; | |
110 | return -EINVAL; | |
111 | } | |
112 | return 0; | |
113 | } | |
114 | ||
115 | int RegistryKey::get(LPCTSTR lpValue, bool& value) | |
116 | { | |
117 | DWORD value_dw = 0; | |
118 | int r = get(lpValue, value_dw); | |
119 | if (!r) { | |
120 | value = !!value_dw; | |
121 | } | |
122 | return r; | |
123 | } | |
124 | ||
125 | int RegistryKey::get(LPCTSTR lpValue, DWORD& value) | |
126 | { | |
127 | DWORD data; | |
128 | DWORD size = sizeof(data); | |
129 | DWORD type = REG_DWORD; | |
130 | DWORD status = RegQueryValueEx(hKey, lpValue, NULL, | |
131 | &type, (LPBYTE)&data, &size); | |
132 | if (ERROR_SUCCESS != status) { | |
133 | derr << "Error: " << win32_strerror(status) | |
134 | << ". Could not get registry value: " | |
135 | << (char*)lpValue << dendl; | |
136 | return -EINVAL; | |
137 | } | |
138 | value = data; | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | int RegistryKey::get(LPCTSTR lpValue, std::string& value) | |
144 | { | |
145 | std::string data{""}; | |
146 | DWORD size = 0; | |
147 | DWORD type = REG_SZ; | |
148 | DWORD status = RegQueryValueEx(hKey, lpValue, NULL, &type, | |
149 | (LPBYTE)data.c_str(), &size); | |
150 | if (ERROR_MORE_DATA == status) { | |
151 | data.resize(size); | |
152 | status = RegQueryValueEx(hKey, lpValue, NULL, &type, | |
153 | (LPBYTE)data.c_str(), &size); | |
154 | } | |
155 | ||
156 | if (ERROR_SUCCESS != status) { | |
157 | derr << "Error: " << win32_strerror(status) | |
158 | << ". Could not get registry value: " | |
159 | << (char*)lpValue << dendl; | |
160 | return -EINVAL; | |
161 | } | |
162 | value.assign(data.c_str()); | |
163 | ||
164 | return 0; | |
165 | } |