Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <string>
#include <mutex>
#include "core/common/common.h"
 
namespace onnxruntime {
 
enum class DeviceIdStatus {
  New,        // Device ID was newly generated
  Existing,   // Device ID was loaded from persistent storage
  Corrupted,  // Stored device ID was invalid and regenerated
  Failed      // Failed to persist device ID (in-memory only)
};
 
/**
 * Manages a persistent device identifier for telemetry purposes.
 * The device ID is stored in a platform-appropriate location:
 * - Windows: HKCU\SOFTWARE\Microsoft\DeveloperTools\.onnxruntime, deviceid value
 * - macOS: ~/Library/Application Support/Microsoft/DeveloperTools/.onnxruntime/deviceid
 * - Linux: $XDG_CACHE_HOME (or ~/.cache) /Microsoft/DeveloperTools/.onnxruntime/deviceid
 *
 * Thread-safe singleton - use DeviceId::Instance() to access.
 */
class DeviceId {
 public:
  static DeviceId& Instance();
 
  // Get the device ID value (generates/loads on first call)
  std::string GetValue();
 
  // Get the status of the device ID
  DeviceIdStatus GetStatus();
 
  // Get human-readable status string
  std::string GetStatusString();
 
  // Get the directory path for device ID / telemetry cache storage
  // (Windows: %LOCALAPPDATA%, %APPDATA%, or the user profile; macOS:
  // ~/Library/Application Support/...; other POSIX: $XDG_CACHE_HOME or ~/.cache/...).
  static std::string GetStorageDirectory();
 
  // Same as GetStorageDirectory(), but also creates the directory tree if it does not exist.
  // POSIX creates it with mode 0700; Windows uses the inherited directory ACL.
  // Returns "" if no suitable location is available. Use before writing into the directory (e.g. the
  // telemetry offline cache, which the 1DS SDK opens during initialization).
  static std::string EnsureStorageDirectory();
 
 private:
  DeviceId() = default;
  ~DeviceId() = default;
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(DeviceId);
 
  void InitializeInternal();
 
  // Create directory tree recursively using platform APIs.
  static bool CreateDirectoryTree(const std::string& path, bool leaf = true);
 
  static constexpr const char* kDeviceIdDir = "Microsoft/DeveloperTools/.onnxruntime";
  static constexpr const char* kFileName = "deviceid";
  static constexpr size_t kMaxFileSize = 256;
 
  std::string device_id_;
  DeviceIdStatus status_ = DeviceIdStatus::New;
  bool initialized_ = false;
  std::mutex mutex_;
};
}  // namespace onnxruntime