aboutsummaryrefslogtreecommitdiff
path: root/deps/uv/src/win/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'deps/uv/src/win/util.c')
-rw-r--r--deps/uv/src/win/util.c125
1 files changed, 64 insertions, 61 deletions
diff --git a/deps/uv/src/win/util.c b/deps/uv/src/win/util.c
index ed7f115718..f19d30a056 100644
--- a/deps/uv/src/win/util.c
+++ b/deps/uv/src/win/util.c
@@ -102,9 +102,10 @@ int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
int uv_exepath(char* buffer, size_t* size_ptr) {
int utf8_len, utf16_buffer_len, utf16_len;
WCHAR* utf16_buffer;
+ int err;
if (buffer == NULL || size_ptr == NULL || *size_ptr == 0) {
- return -1;
+ return UV_EINVAL;
}
if (*size_ptr > 32768) {
@@ -116,12 +117,13 @@ int uv_exepath(char* buffer, size_t* size_ptr) {
utf16_buffer = (WCHAR*) malloc(sizeof(WCHAR) * utf16_buffer_len);
if (!utf16_buffer) {
- return -1;
+ return UV_ENOMEM;
}
/* Get the path as UTF-16. */
utf16_len = GetModuleFileNameW(NULL, utf16_buffer, utf16_buffer_len);
if (utf16_len <= 0) {
+ err = GetLastError();
goto error;
}
@@ -138,6 +140,7 @@ int uv_exepath(char* buffer, size_t* size_ptr) {
NULL,
NULL);
if (utf8_len == 0) {
+ err = GetLastError();
goto error;
}
@@ -150,26 +153,26 @@ int uv_exepath(char* buffer, size_t* size_ptr) {
error:
free(utf16_buffer);
- return -1;
+ return uv_translate_sys_error(err);
}
-uv_err_t uv_cwd(char* buffer, size_t size) {
+int uv_cwd(char* buffer, size_t size) {
DWORD utf16_len;
WCHAR utf16_buffer[MAX_PATH];
int r;
if (buffer == NULL || size == 0) {
- return uv__new_artificial_error(UV_EINVAL);
+ return UV_EINVAL;
}
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
if (utf16_len == 0) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
} else if (utf16_len > MAX_PATH) {
/* This should be impossible; however the CRT has a code path to deal */
/* with this scenario, so I added a check anyway. */
- return uv__new_artificial_error(UV_EIO);
+ return UV_EIO;
}
/* utf16_len contains the length, *not* including the terminating null. */
@@ -193,20 +196,20 @@ uv_err_t uv_cwd(char* buffer, size_t size) {
NULL,
NULL);
if (r == 0) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
- return uv_ok_;
+ return 0;
}
-uv_err_t uv_chdir(const char* dir) {
+int uv_chdir(const char* dir) {
WCHAR utf16_buffer[MAX_PATH];
size_t utf16_len;
WCHAR drive_letter, env_var[4];
if (dir == NULL) {
- return uv__new_artificial_error(UV_EINVAL);
+ return UV_EINVAL;
}
if (MultiByteToWideChar(CP_UTF8,
@@ -220,14 +223,14 @@ uv_err_t uv_chdir(const char* dir) {
/* including terminating null. If it doesn't fit, the path name must be */
/* too long. */
if (error == ERROR_INSUFFICIENT_BUFFER) {
- return uv__new_artificial_error(UV_ENAMETOOLONG);
+ return UV_ENAMETOOLONG;
} else {
- return uv__new_sys_error(error);
+ return uv_translate_sys_error(error);
}
}
if (!SetCurrentDirectoryW(utf16_buffer)) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
/* Windows stores the drive-local path in an "hidden" environment variable, */
@@ -235,9 +238,9 @@ uv_err_t uv_chdir(const char* dir) {
/* update this, so we'll have to do it. */
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
if (utf16_len == 0) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
} else if (utf16_len > MAX_PATH) {
- return uv__new_artificial_error(UV_EIO);
+ return UV_EIO;
}
/* The returned directory should not have a trailing slash, unless it */
@@ -270,11 +273,11 @@ uv_err_t uv_chdir(const char* dir) {
env_var[3] = L'\0';
if (!SetEnvironmentVariableW(env_var, utf16_buffer)) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
}
- return uv_ok_;
+ return 0;
}
@@ -338,8 +341,8 @@ char** uv_setup_args(int argc, char** argv) {
}
-uv_err_t uv_set_process_title(const char* title) {
- uv_err_t err;
+int uv_set_process_title(const char* title) {
+ int err;
int length;
WCHAR* title_w = NULL;
@@ -348,7 +351,7 @@ uv_err_t uv_set_process_title(const char* title) {
/* Find out how big the buffer for the wide-char title must be */
length = uv_utf8_to_utf16(title, NULL, 0);
if (!length) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto done;
}
@@ -360,7 +363,7 @@ uv_err_t uv_set_process_title(const char* title) {
length = uv_utf8_to_utf16(title, title_w, length);
if (!length) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto done;
};
@@ -370,7 +373,7 @@ uv_err_t uv_set_process_title(const char* title) {
}
if (!SetConsoleTitleW(title_w)) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto done;
}
@@ -379,11 +382,11 @@ uv_err_t uv_set_process_title(const char* title) {
process_title = strdup(title);
LeaveCriticalSection(&process_title_lock);
- err = uv_ok_;
+ err = 0;
done:
free(title_w);
- return err;
+ return uv_translate_sys_error(err);
}
@@ -417,7 +420,7 @@ static int uv__get_process_title() {
}
-uv_err_t uv_get_process_title(char* buffer, size_t size) {
+int uv_get_process_title(char* buffer, size_t size) {
uv__once_init();
EnterCriticalSection(&process_title_lock);
@@ -426,14 +429,14 @@ uv_err_t uv_get_process_title(char* buffer, size_t size) {
* we must query it with getConsoleTitleW
*/
if (!process_title && uv__get_process_title() == -1) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
assert(process_title);
strncpy(buffer, process_title, size);
LeaveCriticalSection(&process_title_lock);
- return uv_ok_;
+ return 0;
}
@@ -462,23 +465,23 @@ uint64_t uv_hrtime(void) {
}
-uv_err_t uv_resident_set_memory(size_t* rss) {
+int uv_resident_set_memory(size_t* rss) {
HANDLE current_process;
PROCESS_MEMORY_COUNTERS pmc;
current_process = GetCurrentProcess();
if (!GetProcessMemoryInfo(current_process, &pmc, sizeof(pmc))) {
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
*rss = pmc.WorkingSetSize;
- return uv_ok_;
+ return 0;
}
-uv_err_t uv_uptime(double* uptime) {
+int uv_uptime(double* uptime) {
BYTE stack_buffer[4096];
BYTE* malloced_buffer = NULL;
BYTE* buffer = (BYTE*) stack_buffer;
@@ -505,7 +508,7 @@ uv_err_t uv_uptime(double* uptime) {
break;
} else if (result != ERROR_MORE_DATA) {
*uptime = 0;
- return uv__new_sys_error(result);
+ return uv_translate_sys_error(result);
}
free(malloced_buffer);
@@ -519,7 +522,7 @@ uv_err_t uv_uptime(double* uptime) {
buffer = malloced_buffer = (BYTE*) malloc(buffer_size);
if (malloced_buffer == NULL) {
*uptime = 0;
- return uv__new_artificial_error(UV_ENOMEM);
+ return UV_ENOMEM;
}
}
@@ -559,7 +562,7 @@ uv_err_t uv_uptime(double* uptime) {
*uptime = (double) (object_type->PerfTime.QuadPart - value) /
(double) object_type->PerfFreq.QuadPart;
free(malloced_buffer);
- return uv_ok_;
+ return 0;
}
}
@@ -570,16 +573,16 @@ uv_err_t uv_uptime(double* uptime) {
/* If we get here, the uptime value was not found. */
free(malloced_buffer);
*uptime = 0;
- return uv__new_artificial_error(UV_ENOSYS);
+ return UV_ENOSYS;
internalError:
free(malloced_buffer);
*uptime = 0;
- return uv__new_artificial_error(UV_EIO);
+ return UV_EIO;
}
-uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
+int uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
uv_cpu_info_t* cpu_infos;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
DWORD sppi_size;
@@ -587,7 +590,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
DWORD cpu_count, r, i;
NTSTATUS status;
ULONG result_size;
- uv_err_t err;
+ int err;
uv_cpu_info_t* cpu_info;
cpu_infos = NULL;
@@ -601,14 +604,14 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
cpu_infos = calloc(cpu_count, sizeof *cpu_infos);
if (cpu_infos == NULL) {
- err = uv__new_artificial_error(UV_ENOMEM);
+ err = ERROR_OUTOFMEMORY;
goto error;
}
sppi_size = cpu_count * sizeof(*sppi);
sppi = malloc(sppi_size);
if (sppi == NULL) {
- err = uv__new_artificial_error(UV_ENOMEM);
+ err = ERROR_OUTOFMEMORY;
goto error;
}
@@ -617,7 +620,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
sppi_size,
&result_size);
if (!NT_SUCCESS(status)) {
- err = uv__new_sys_error(pRtlNtStatusToDosError(status));
+ err = pRtlNtStatusToDosError(status);
goto error;
}
@@ -645,7 +648,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
KEY_QUERY_VALUE,
&processor_key);
if (r != ERROR_SUCCESS) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto error;
}
@@ -655,7 +658,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
NULL,
(BYTE*) &cpu_speed,
&cpu_speed_size) != ERROR_SUCCESS) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
@@ -666,7 +669,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
NULL,
(BYTE*) &cpu_brand,
&cpu_brand_size) != ERROR_SUCCESS) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
@@ -692,7 +695,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
NULL,
NULL);
if (len == 0) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto error;
}
@@ -701,7 +704,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
/* Allocate 1 extra byte for the null terminator. */
cpu_info->model = malloc(len + 1);
if (cpu_info->model == NULL) {
- err = uv__new_artificial_error(UV_ENOMEM);
+ err = ERROR_OUTOFMEMORY;
goto error;
}
@@ -713,7 +716,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
len,
NULL,
NULL) == 0) {
- err = uv__new_sys_error(GetLastError());
+ err = GetLastError();
goto error;
}
@@ -726,7 +729,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
*cpu_count_ptr = cpu_count;
*cpu_infos_ptr = cpu_infos;
- return uv_ok_;
+ return 0;
error:
/* This is safe because the cpu_infos array is zeroed on allocation. */
@@ -736,7 +739,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
free(cpu_infos);
free(sppi);
- return err;
+ return uv_translate_sys_error(err);
}
@@ -751,7 +754,7 @@ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
}
-uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
+int uv_interface_addresses(uv_interface_address_t** addresses_ptr,
int* count_ptr) {
IP_ADAPTER_ADDRESSES* win_address_buf;
ULONG win_address_buf_size;
@@ -792,7 +795,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
/* all adapters. */
win_address_buf = malloc(win_address_buf_size);
if (win_address_buf == NULL)
- return uv__new_artificial_error(UV_ENOMEM);
+ return UV_ENOMEM;
continue;
@@ -800,16 +803,16 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
/* No adapters were found. */
uv_address_buf = malloc(1);
if (uv_address_buf == NULL)
- return uv__new_artificial_error(UV_ENOMEM);
+ return UV_ENOMEM;
*count_ptr = 0;
*addresses_ptr = uv_address_buf;
- return uv_ok_;
+ return 0;
}
case ERROR_ADDRESS_NOT_ASSOCIATED:
- return uv__new_artificial_error(UV_EAGAIN);
+ return UV_EAGAIN;
case ERROR_INVALID_PARAMETER:
/* MSDN says:
@@ -820,13 +823,13 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
* Since the first two conditions are not met, it must be that the
* adapter data is too big.
*/
- return uv__new_artificial_error(UV_ENOBUFS);
+ return UV_ENOBUFS;
default:
/* Other (unspecified) errors can happen, but we don't have any */
/* special meaning for them. */
assert(r != ERROR_SUCCESS);
- return uv__new_sys_error(r);
+ return uv_translate_sys_error(r);
}
}
@@ -861,7 +864,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
FALSE);
if (name_size <= 0) {
free(win_address_buf);
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
uv_address_buf_size += name_size;
@@ -880,7 +883,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
uv_address_buf = malloc(uv_address_buf_size);
if (uv_address_buf == NULL) {
free(win_address_buf);
- return uv__new_artificial_error(UV_ENOMEM);
+ return UV_ENOMEM;
}
/* Compute the start of the uv_interface_address_t array, and the place in */
@@ -916,7 +919,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
if (name_size <= 0) {
free(win_address_buf);
free(uv_address_buf);
- return uv__new_sys_error(GetLastError());
+ return uv_translate_sys_error(GetLastError());
}
prefix = win_address->FirstPrefix;
@@ -966,7 +969,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses_ptr,
*addresses_ptr = uv_address_buf;
*count_ptr = count;
- return uv_ok_;
+ return 0;
}