]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common: Add Windows registry key utils
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Thu, 30 Apr 2020 07:06:38 +0000 (07:06 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 18 Nov 2020 09:23:43 +0000 (09:23 +0000)
We'll add some helpers facilitating Windows registry key operations.

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
Signed-off-by: Alin Gabriel Serdean <aserdean@cloudbasesolutions.com>
src/common/CMakeLists.txt
src/common/registry_win32.cc [new file with mode: 0644]
src/common/registry_win32.h [new file with mode: 0644]

index 368647a607d822b99a23c39cf86cede59fccf834..7fd47c95ec57f8c3021f8034314937e475501e23 100644 (file)
@@ -108,7 +108,8 @@ if(WIN32)
     blkdev_win32.cc
     dns_resolve_win32.cc
     SubProcess_win32.cc
-    ifaddrs_win32.c)
+    ifaddrs_win32.c
+    registry_win32.cc)
 else()
   list(APPEND common_srcs
     blkdev.cc
diff --git a/src/common/registry_win32.cc b/src/common/registry_win32.cc
new file mode 100644 (file)
index 0000000..eb809a4
--- /dev/null
@@ -0,0 +1,155 @@
+/*
+ * 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/registry_win32.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, 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;
+}
diff --git a/src/common/registry_win32.h b/src/common/registry_win32.h
new file mode 100644 (file)
index 0000000..974d662
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2019 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.
+ *
+ */
+
+#include "include/compat.h"
+#include "common/ceph_context.h"
+
+
+class RegistryKey {
+public:
+  RegistryKey(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey, bool create_value);
+  ~RegistryKey();
+
+  static remove(CephContext *cct_, HKEY hRootKey, LPCTSTR strKey);
+
+  int flush();
+
+  int set(LPCTSTR lpValue, DWORD data);
+  int set(LPCTSTR lpValue, std::string data);
+
+  int get(LPCTSTR lpValue, DWORD* value);
+  int get(LPCTSTR lpValue, std::string& value);
+
+  HKEY hKey = NULL;
+  bool missingKey = false;
+
+private:
+  CephContext *cct;
+};