GeezerVison is showing github.com — unix,win: add uv_os_homedir() · libuv/libuv@a62c2d5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Makefile.am +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ include_HEADERS += include/uv-win.h include/tree.h | 45 | 45 | AM_CPPFLAGS += -I$(top_srcdir)/src/win \ | 46 | 46 | -DWIN32_LEAN_AND_MEAN \ | 47 | 47 | -D_WIN32_WINNT=0x0600 | 48 | | -LIBS += -lws2_32 -lpsapi -liphlpapi -lshell32 | | 48 | +LIBS += -lws2_32 -lpsapi -liphlpapi -lole32 -lshell32 | 49 | 49 | libuv_la_SOURCES += src/win/async.c \ | 50 | 50 | src/win/atomicops-inl.h \ | 51 | 51 | src/win/core.c \ | @@ -165,6 +165,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \ | 165 | 165 | test/test-getnameinfo.c \ | 166 | 166 | test/test-getsockname.c \ | 167 | 167 | test/test-handle-fileno.c \ | | 168 | + test/test-homedir.c \ | 168 | 169 | test/test-hrtime.c \ | 169 | 170 | test/test-idle.c \ | 170 | 171 | test/test-ip4-addr.c \ | |
Collapse file Copy file name to clipboardExpand all lines: checksparse.sh +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,7 @@ test/test-get-loadavg.c | 103 | 103 | test/test-get-memory.c | 104 | 104 | test/test-getaddrinfo.c | 105 | 105 | test/test-getsockname.c | | 106 | +test/test-homedir.c | 106 | 107 | test/test-hrtime.c | 107 | 108 | test/test-idle.c | 108 | 109 | test/test-ip6-addr.c | |
Collapse file Copy file name to clipboardExpand all lines: docs/src/misc.rst +17Lines changed: 17 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 227 | 227 |
| 228 | 228 | Changes the current working directory. | 229 | 229 |
| | 230 | +.. c:function:: int uv_os_homedir(char* buffer, size_t* size) | | 231 | +
| | 232 | + Gets the current user's home directory. On Windows, `uv_os_homedir()` first | | 233 | + checks the `USERPROFILE` environment variable using | | 234 | + `GetEnvironmentVariableW()`. If `USERPROFILE` is not set, | | 235 | + `SHGetKnownFolderPath()` is called. On all other operating systems, | | 236 | + `uv_os_homedir()` first checks the `HOME` environment variable using | | 237 | + :man:`getenv(3)`. If `HOME` is not set, :man:`getpwuid_r(3)` is called. The | | 238 | + user's home directory is stored in `buffer`. When `uv_os_homedir()` is | | 239 | + called, `size` indicates the maximum size of `buffer`. On success or | | 240 | + `UV_ENOBUFS` failure, `size` is set to the string length of `buffer`. | | 241 | +
| | 242 | + .. warning:: | | 243 | + `uv_os_homedir()` is not thread safe. | | 244 | +
| | 245 | + .. versionadded:: 1.6.0 | | 246 | +
| 230 | 247 | .. uint64_t uv_get_free_memory(void) | 231 | 248 | .. c:function:: uint64_t uv_get_total_memory(void) | 232 | 249 |
| |
Collapse file Copy file name to clipboardExpand all lines: include/uv.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1028,6 +1028,8 @@ typedef struct { | 1028 | 1028 |
| 1029 | 1029 | UV_EXTERN int uv_getrusage(uv_rusage_t* rusage); | 1030 | 1030 |
| | 1031 | +UV_EXTERN int uv_os_homedir(char* buffer, size_t* size); | | 1032 | + | 1031 | 1033 | UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count); | 1032 | 1034 | UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count); | 1033 | 1035 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/unix/core.c +80Lines changed: 80 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 38 | 38 | #include <limits.h> /* INT_MAX, PATH_MAX */ | 39 | 39 | #include <sys/uio.h> /* writev */ | 40 | 40 | #include <sys/resource.h> /* getrusage */ | | 41 | +#include <pwd.h> | 41 | 42 |
| 42 | 43 | #ifdef __linux__ | 43 | 44 | # include <sys/ioctl.h> | @@ -986,3 +987,82 @@ int uv__dup2_cloexec(int oldfd, int newfd) { | 986 | 987 | return r; | 987 | 988 | } | 988 | 989 | } | | 990 | + | | 991 | + | | 992 | +int uv_os_homedir(char* buffer, size_t* size) { | | 993 | + struct passwd pw; | | 994 | + struct passwd* result; | | 995 | + char* buf; | | 996 | + uid_t uid; | | 997 | + size_t bufsize; | | 998 | + size_t len; | | 999 | + int r; | | 1000 | + | | 1001 | + if (buffer == NULL || size == NULL || *size == 0) | | 1002 | + return -EINVAL; | | 1003 | + | | 1004 | + /* Check if the HOME environment variable is set first */ | | 1005 | + buf = getenv("HOME"); | | 1006 | + | | 1007 | + if (buf != NULL) { | | 1008 | + len = strlen(buf); | | 1009 | + | | 1010 | + if (len >= *size) { | | 1011 | + *size = len; | | 1012 | + return -ENOBUFS; | | 1013 | + } | | 1014 | + | | 1015 | + memcpy(buffer, buf, len + 1); | | 1016 | + *size = len; | | 1017 | + | | 1018 | + return 0; | | 1019 | + } | | 1020 | + | | 1021 | + /* HOME is not set, so call getpwuid() */ | | 1022 | + bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | | 1023 | + | | 1024 | + if (bufsize <= 0) | | 1025 | + return -EIO; | | 1026 | + | | 1027 | + uid = getuid(); | | 1028 | + buf = NULL; | | 1029 | + | | 1030 | + for (;;) { | | 1031 | + free(buf); | | 1032 | + buf = malloc(bufsize); | | 1033 | + | | 1034 | + if (buf == NULL) | | 1035 | + return -ENOMEM; | | 1036 | + | | 1037 | + r = getpwuid_r(uid, &pw, buf, bufsize, &result); | | 1038 | + | | 1039 | + if (r != ERANGE) | | 1040 | + break; | | 1041 | + | | 1042 | + bufsize *= 2; | | 1043 | + } | | 1044 | + | | 1045 | + if (r != 0) { | | 1046 | + free(buf); | | 1047 | + return -r; | | 1048 | + } | | 1049 | + | | 1050 | + if (result == NULL) { | | 1051 | + free(buf); | | 1052 | + return -ENOENT; | | 1053 | + } | | 1054 | + | | 1055 | + len = strlen(pw.pw_dir); | | 1056 | + | | 1057 | + if (len >= *size) { | | 1058 | + *size = len; | | 1059 | + free(buf); | | 1060 | + return -ENOBUFS; | | 1061 | + } | | 1062 | + | | 1063 | + memcpy(buffer, pw.pw_dir, len + 1); | | 1064 | + *size = len; | | 1065 | + free(buf); | | 1066 | + | | 1067 | + return 0; | | 1068 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/win/util.c +72-6Lines changed: 72 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | #include <psapi.h> | 37 | 37 | #include <tlhelp32.h> | 38 | 38 | #include <windows.h> | | 39 | +#include <shlobj.h> | | 40 | +#include <objbase.h> | 39 | 41 |
| 40 | 42 |
| 41 | 43 | /* | @@ -72,7 +74,7 @@ void uv__util_init() { | 72 | 74 | InitializeCriticalSection(&process_title_lock); | 73 | 75 |
| 74 | 76 | /* Retrieve high-resolution timer frequency | 75 | | - * and precompute its reciprocal. | | 77 | + * and precompute its reciprocal. | 76 | 78 | */ | 77 | 79 | if (QueryPerformanceFrequency(&perf_frequency)) { | 78 | 80 | hrtime_interval_ = 1.0 / perf_frequency.QuadPart; | @@ -801,8 +803,8 @@ static int is_windows_version_or_greater(DWORD os_major, | 801 | 803 |
| 802 | 804 | /* Perform the test. */ | 803 | 805 | return (int) VerifyVersionInfo( | 804 | | - &osvi, | 805 | | - VER_MAJORVERSION | VER_MINORVERSION | | | 806 | + &osvi, | | 807 | + VER_MAJORVERSION | VER_MINORVERSION | | 806 | 808 | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, | 807 | 809 | condition_mask); | 808 | 810 | } | @@ -870,7 +872,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses_ptr, | 870 | 872 | flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | | 871 | 873 | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX; | 872 | 874 | } | 873 | | - | | 875 | + | 874 | 876 |
| 875 | 877 | /* Fetch the size of the adapters reported by windows, and then get the */ | 876 | 878 | /* list itself. */ | @@ -1053,14 +1055,14 @@ int uv_interface_addresses(uv_interface_address_t** addresses_ptr, | 1053 | 1055 | prefix->PrefixLength <= prefix_len) | 1054 | 1056 | continue; | 1055 | 1057 |
| 1056 | | - if (address_prefix_match(sa->sa_family, sa, | | 1058 | + if (address_prefix_match(sa->sa_family, sa, | 1057 | 1059 | prefix->Address.lpSockaddr, prefix->PrefixLength)) { | 1058 | 1060 | prefix_len = prefix->PrefixLength; | 1059 | 1061 | } | 1060 | 1062 | } | 1061 | 1063 |
| 1062 | 1064 | /* If there is no matching prefix information, return a single-host | 1063 | | - * subnet mask (e.g. 255.255.255.255 for IPv4). | | 1065 | + * subnet mask (e.g. 255.255.255.255 for IPv4). | 1064 | 1066 | */ | 1065 | 1067 | if (!prefix_len) | 1066 | 1068 | prefix_len = (sa->sa_family == AF_INET6) ? 128 : 32; | @@ -1153,3 +1155,67 @@ int uv_getrusage(uv_rusage_t *uv_rusage) { | 1153 | 1155 |
| 1154 | 1156 | return 0; | 1155 | 1157 | } | | 1158 | + | | 1159 | + | | 1160 | +int uv_os_homedir(char* buffer, size_t* size) { | | 1161 | + wchar_t* path; | | 1162 | + size_t bufsize; | | 1163 | + size_t len; | | 1164 | + int r; | | 1165 | + | | 1166 | + if (buffer == NULL || size == NULL || *size == 0) | | 1167 | + return UV_EINVAL; | | 1168 | + | | 1169 | + /* Check if the USERPROFILE environment variable is set first */ | | 1170 | + path = malloc(*size * sizeof(WCHAR)); | | 1171 | + | | 1172 | + if (path == NULL) | | 1173 | + return UV_ENOMEM; | | 1174 | + | | 1175 | + len = GetEnvironmentVariableW(L"USERPROFILE", path, *size); | | 1176 | + | | 1177 | + if (len == 0) { | | 1178 | + r = GetLastError(); | | 1179 | + free(path); | | 1180 | + | | 1181 | + if (r != ERROR_ENVVAR_NOT_FOUND) | | 1182 | + return uv_translate_sys_error(r); | | 1183 | + } else { | | 1184 | + if (len > *size) { | | 1185 | + free(path); | | 1186 | + *size = len - 1; | | 1187 | + return UV_ENOBUFS; | | 1188 | + } | | 1189 | + | | 1190 | + bufsize = uv_utf16_to_utf8(path, -1, buffer, *size); | | 1191 | + assert(len + 1 == bufsize); | | 1192 | + free(path); | | 1193 | + *size = len; | | 1194 | + | | 1195 | + return 0; | | 1196 | + } | | 1197 | + | | 1198 | + /* USERPROFILE is not set, so call SHGetKnownFolderPath() */ | | 1199 | + if (SHGetKnownFolderPath(&FOLDERID_Profile, 0, NULL, &path) != S_OK) | | 1200 | + return uv_translate_sys_error(GetLastError()); | | 1201 | + | | 1202 | + bufsize = uv_utf16_to_utf8(path, -1, buffer, *size); | | 1203 | + | | 1204 | + if (bufsize == 0) { | | 1205 | + r = GetLastError(); | | 1206 | + | | 1207 | + if (r == ERROR_INSUFFICIENT_BUFFER) { | | 1208 | + *size = wcslen(path); | | 1209 | + CoTaskMemFree(path); | | 1210 | + return UV_ENOBUFS; | | 1211 | + } | | 1212 | + | | 1213 | + CoTaskMemFree(path); | | 1214 | + return uv_translate_sys_error(r); | | 1215 | + } | | 1216 | + | | 1217 | + CoTaskMemFree(path); | | 1218 | + *size = bufsize - 1; | | 1219 | + | | 1220 | + return 0; | | 1221 | +} |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#include "uv.h" | | 2 | +#include "task.h" | | 3 | +#include <string.h> | | 4 | + | | 5 | +#define PATHMAX 1024 | | 6 | +#define SMALLPATH 1 | | 7 | + | | 8 | +TEST_IMPL(homedir) { | | 9 | + char homedir[PATHMAX]; | | 10 | + size_t len; | | 11 | + char last; | | 12 | + int r; | | 13 | + | | 14 | + /* Test the normal case */ | | 15 | + len = sizeof homedir; | | 16 | + homedir[0] = '\0'; | | 17 | + ASSERT(strlen(homedir) == 0); | | 18 | + r = uv_os_homedir(homedir, &len); | | 19 | + ASSERT(r == 0); | | 20 | + ASSERT(strlen(homedir) == len); | | 21 | + ASSERT(len > 0); | | 22 | + ASSERT(homedir[len] == '\0'); | | 23 | + | | 24 | + if (len > 1) { | | 25 | + last = homedir[len - 1]; | | 26 | +#ifdef _WIN32 | | 27 | + ASSERT(last != '\\'); | | 28 | +#else | | 29 | + ASSERT(last != '/'); | | 30 | +#endif | | 31 | + } | | 32 | + | | 33 | + /* Test the case where the buffer is too small */ | | 34 | + len = SMALLPATH; | | 35 | + r = uv_os_homedir(homedir, &len); | | 36 | + ASSERT(r == UV_ENOBUFS); | | 37 | + ASSERT(len > SMALLPATH); | | 38 | + | | 39 | + /* Test invalid inputs */ | | 40 | + r = uv_os_homedir(NULL, &len); | | 41 | + ASSERT(r == UV_EINVAL); | | 42 | + r = uv_os_homedir(homedir, NULL); | | 43 | + ASSERT(r == UV_EINVAL); | | 44 | + len = 0; | | 45 | + r = uv_os_homedir(homedir, &len); | | 46 | + ASSERT(r == UV_EINVAL); | | 47 | + | | 48 | + return 0; | | 49 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/test-list.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -183,6 +183,7 @@ TEST_DECLARE (process_title) | 183 | 183 | TEST_DECLARE (cwd_and_chdir) | 184 | 184 | TEST_DECLARE (get_memory) | 185 | 185 | TEST_DECLARE (handle_fileno) | | 186 | +TEST_DECLARE (homedir) | 186 | 187 | TEST_DECLARE (hrtime) | 187 | 188 | TEST_DECLARE (getaddrinfo_fail) | 188 | 189 | TEST_DECLARE (getaddrinfo_fail_sync) | @@ -542,6 +543,8 @@ TASK_LIST_START | 542 | 543 |
| 543 | 544 | TEST_ENTRY (handle_fileno) | 544 | 545 |
| | 546 | + TEST_ENTRY (homedir) | | 547 | + | 545 | 548 | TEST_ENTRY (hrtime) | 546 | 549 |
| 547 | 550 | TEST_ENTRY_CUSTOM (getaddrinfo_fail, 0, 0, 10000) | |
Collapse file Copy file name to clipboardExpand all lines: uv.gyp +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 108 | 108 | 'libraries': [ | 109 | 109 | '-ladvapi32', | 110 | 110 | '-liphlpapi', | | 111 | + '-lole32', | 111 | 112 | '-lpsapi', | 112 | 113 | '-lshell32', | 113 | 114 | '-lws2_32' | | 304 | 305 | 'test/test-getnameinfo.c', | 305 | 306 | 'test/test-getsockname.c', | 306 | 307 | 'test/test-handle-fileno.c', | | 308 | + 'test/test-homedir.c', | 307 | 309 | 'test/test-hrtime.c', | 308 | 310 | 'test/test-idle.c', | 309 | 311 | 'test/test-ip6-addr.c', | |
|