File: env_var_wrapper.cpp

package info (click to toggle)
vulkan-loader 1.4.335.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,192 kB
  • sloc: cpp: 325,195; ansic: 46,752; xml: 34,360; python: 6,108; asm: 3,534; makefile: 71; sh: 53
file content (184 lines) | stat: -rw-r--r-- 6,962 bytes parent folder | download | duplicates (7)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2025 The Khronos Group Inc.
 * Copyright (c) 2025 Valve Corporation
 * Copyright (c) 2025 LunarG, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and/or associated documentation files (the "Materials"), to
 * deal in the Materials without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Materials, and to permit persons to whom the Materials are
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice(s) and this permission notice shall be included in
 * all copies or substantial portions of the Materials.
 *
 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
 * USE OR OTHER DEALINGS IN THE MATERIALS.
 *
 */

#include "env_var_wrapper.h"

#include <stdlib.h>

#include <iostream>

#include "wide_char_handling.h"

// NOTE: get_env_var() are only intended for test framework code, all test code MUST use
// EnvVarWrapper

#if defined(_WIN32)
#include <Windows.h>
#include <wchar.h>
#include <strsafe.h>

// Windows specific error handling logic
const long ERROR_SETENV_FAILED = 10543;           // chosen at random, attempts to not conflict
const long ERROR_REMOVEDIRECTORY_FAILED = 10544;  // chosen at random, attempts to not conflict
const char* win_api_error_str(LSTATUS status) {
    if (status == ERROR_INVALID_FUNCTION) return "ERROR_INVALID_FUNCTION";
    if (status == ERROR_FILE_NOT_FOUND) return "ERROR_FILE_NOT_FOUND";
    if (status == ERROR_PATH_NOT_FOUND) return "ERROR_PATH_NOT_FOUND";
    if (status == ERROR_TOO_MANY_OPEN_FILES) return "ERROR_TOO_MANY_OPEN_FILES";
    if (status == ERROR_ACCESS_DENIED) return "ERROR_ACCESS_DENIED";
    if (status == ERROR_INVALID_HANDLE) return "ERROR_INVALID_HANDLE";
    if (status == ERROR_ENVVAR_NOT_FOUND) return "ERROR_ENVVAR_NOT_FOUND";
    if (status == ERROR_SETENV_FAILED) return "ERROR_SETENV_FAILED";
    return "UNKNOWN ERROR";
}
std::string error_code_to_string(DWORD error_code) {
    LPSTR lpMsgBuf{};
    size_t MsgBufSize = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                      nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpMsgBuf, 0, nullptr);
    std::string code_str(lpMsgBuf, MsgBufSize);
    LocalFree(lpMsgBuf);
    return code_str;
}

void print_error_message(LSTATUS status, const char* function_name, std::string optional_message = "") {
    DWORD dw = GetLastError();

    std::cerr << function_name << " failed with " << win_api_error_str(status) << ": " << error_code_to_string(dw);
    if (optional_message != "") {
        std::cerr << " | " << optional_message;
    }
    std::cerr << "\n";
}

// get_env_var() - returns a std::string of `name`. if report_failure is true, then it will log to stderr that it didn't find the
//     env-var
std::string get_env_var(std::string const& name, bool report_failure) {
    std::wstring name_utf16 = widen(name);
    DWORD value_size = GetEnvironmentVariableW(name_utf16.c_str(), nullptr, 0);
    if (0 == value_size) {
        if (report_failure) print_error_message(ERROR_ENVVAR_NOT_FOUND, "GetEnvironmentVariableW");
        return {};
    }
    std::wstring value(value_size, L'\0');
    if (GetEnvironmentVariableW(name_utf16.c_str(), &value[0], value_size) != value_size - 1) {
        return {};
    }
    return narrow(value);
}
#elif TESTING_COMMON_UNIX_PLATFORMS

// get_env_var() - returns a std::string of `name`. if report_failure is true, then it will log to stderr that it didn't find the
//     env-var
std::string get_env_var(std::string const& name, bool report_failure) {
    char* ret = getenv(name.c_str());
    if (ret == nullptr) {
        if (report_failure) std::cerr << "Failed to get environment variable:" << name << "\n";
        return std::string();
    }
    return ret;
}
#endif

std::vector<std::string> split_env_var_as_list(std::string const& env_var_contents) {
    std::vector<std::string> items;
    size_t start = 0;
    size_t len = 0;
    for (size_t i = 0; i < env_var_contents.size(); i++) {
        if (env_var_contents[i] == OS_ENV_VAR_LIST_SEPARATOR) {
            if (len != 0) {
                // only push back non empty strings
                items.push_back(env_var_contents.substr(start, len));
            }
            start = i + 1;
            len = 0;
        } else {
            len++;
        }
    }
    items.push_back(env_var_contents.substr(start, len));
    return items;
}

/*
 * Wrapper around Environment Variables with common operations
 * Since Environment Variables leak between tests, there needs to be RAII code to remove them during test cleanup

 */

EnvVarWrapper::EnvVarWrapper(std::string const& name) noexcept : name(name) {
    initial_value = get_env_var(name, false);
    remove_env_var();
}
EnvVarWrapper::EnvVarWrapper(std::string const& name, std::string const& value) noexcept : name(name), cur_value(value) {
    initial_value = get_env_var(name, false);
    set_env_var();
}
EnvVarWrapper::~EnvVarWrapper() noexcept {
    remove_env_var();
    if (!initial_value.empty()) {
        set_new_value(initial_value);
    }
}

void EnvVarWrapper::set_new_value(std::string const& value) {
    cur_value = value;
    set_env_var();
}
void EnvVarWrapper::add_to_list(std::string const& list_item) {
    if (!cur_value.empty()) {
        cur_value += OS_ENV_VAR_LIST_SEPARATOR;
    }
    cur_value += list_item;
    set_env_var();
}
#if defined(_WIN32)
void EnvVarWrapper::add_to_list(std::wstring const& list_item) {
    if (!cur_value.empty()) {
        cur_value += OS_ENV_VAR_LIST_SEPARATOR;
    }
    cur_value += narrow(list_item);
    set_env_var();
}
#endif
void EnvVarWrapper::remove_value() const { remove_env_var(); }
const char* EnvVarWrapper::get() const { return name.c_str(); }
const char* EnvVarWrapper::value() const { return cur_value.c_str(); }

#if defined(_WIN32)

void EnvVarWrapper::set_env_var() const {
    BOOL ret = SetEnvironmentVariableW(widen(name).c_str(), widen(cur_value).c_str());
    if (ret == 0) {
        print_error_message(ERROR_SETENV_FAILED, "SetEnvironmentVariableW");
    }
}
void EnvVarWrapper::remove_env_var() const { SetEnvironmentVariableW(widen(name).c_str(), nullptr); }

#elif TESTING_COMMON_UNIX_PLATFORMS

void EnvVarWrapper::set_env_var() const { setenv(name.c_str(), cur_value.c_str(), 1); }
void EnvVarWrapper::remove_env_var() const { unsetenv(name.c_str()); }
#endif