summaryrefslogtreecommitdiffstats
path: root/src/common/win32/registry.cc
blob: 85ab80df97b61c9723ec41dfac85409019e425a4 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2020 SUSE LINUX GmbH
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#define dout_context cct
#define dout_subsys ceph_subsys_

#include "common/debug.h"
#include "common/errno.h"
#include "common/win32/registry.h"

RegistryKey::RegistryKey(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey,
                         bool create_value): cct(cct_)
{
  DWORD status = RegOpenKeyEx(hRootKey, strKey, 0, KEY_ALL_ACCESS, &hKey);

  if (status == ERROR_FILE_NOT_FOUND && create_value)
  {
    ldout(cct_, 10) << "Creating registry key: " << strKey << dendl;
    status = RegCreateKeyEx(
        hRootKey, strKey, 0, NULL, REG_OPTION_NON_VOLATILE,
        KEY_ALL_ACCESS, NULL, &hKey, NULL);
  }

  if (ERROR_SUCCESS != status) {
    if (ERROR_FILE_NOT_FOUND == status) {
      missingKey = true;
    } else {
      lderr(cct_) << "Error: " << win32_strerror(status)
                  << ". Could not open registry key: "
                  << strKey << dendl;
    }
  }
}

RegistryKey::~RegistryKey() {
  if (!hKey)
    return;

  DWORD status = RegCloseKey(hKey);
  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not close registry key." << dendl;
  } else {
    hKey = NULL;
  }
}

int RegistryKey::remove(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey)
{
  DWORD status = RegDeleteKeyEx(hRootKey, strKey, KEY_WOW64_64KEY, 0);

  if (status == ERROR_FILE_NOT_FOUND)
  {
    ldout(cct_, 20) << "Registry key : " << strKey
                    << " does not exist." << dendl;
    return 0;
  }

  if (ERROR_SUCCESS != status) {
    lderr(cct_) << "Error: " << win32_strerror(status)
                << ". Could not delete registry key: "
                << strKey << dendl;
    return -EINVAL;
  }

  return 0;
}

int RegistryKey::flush() {
  DWORD status = RegFlushKey(hKey);
  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not flush registry key." << dendl;
    return -EINVAL;
  }

  return 0;
}

int RegistryKey::set(LPCTSTR lpValue, DWORD data)
{
  DWORD status = RegSetValueEx(hKey, lpValue, 0, REG_DWORD,
                               (LPBYTE)&data, sizeof(DWORD));
  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not set registry value: " << (char*)lpValue << dendl;
    return -EINVAL;
  }

  return 0;
}

int RegistryKey::set(LPCTSTR lpValue, std::string data)
{
  DWORD status = RegSetValueEx(hKey, lpValue, 0, REG_SZ,
                               (LPBYTE)data.c_str(), data.length());
  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not set registry value: "
         << (char*)lpValue << dendl;
    return -EINVAL;
  }
  return 0;
}

int RegistryKey::get(LPCTSTR lpValue, bool& value)
{
  DWORD value_dw = 0;
  int r = get(lpValue, value_dw);
  if (!r) {
    value = !!value_dw;
  }
  return r;
}

int RegistryKey::get(LPCTSTR lpValue, DWORD& value)
{
  DWORD data;
  DWORD size = sizeof(data);
  DWORD type = REG_DWORD;
  DWORD status = RegQueryValueEx(hKey, lpValue, NULL,
                                 &type, (LPBYTE)&data, &size);
  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not get registry value: "
         << (char*)lpValue << dendl;
    return -EINVAL;
  }
  value = data;

  return 0;
}

int RegistryKey::get(LPCTSTR lpValue, std::string& value)
{
  std::string data{""};
  DWORD size = 0;
  DWORD type = REG_SZ;
  DWORD status = RegQueryValueEx(hKey, lpValue, NULL, &type,
                                 (LPBYTE)data.c_str(), &size);
  if (ERROR_MORE_DATA == status) {
    data.resize(size);
    status = RegQueryValueEx(hKey, lpValue, NULL, &type,
                             (LPBYTE)data.c_str(), &size);
  }

  if (ERROR_SUCCESS != status) {
    derr << "Error: " << win32_strerror(status)
         << ". Could not get registry value: "
         << (char*)lpValue << dendl;
    return -EINVAL;
  }
  value.assign(data.c_str());

  return 0;
}