MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cli_ensure_windows_user_path

Function cli_ensure_windows_user_path

src/cli/cli.c:5920–6012  ·  view source on GitHub ↗

Persist the current-user PATH while the activation lease is held. The * installer script may update only its process-local PATH after this returns; * it must not perform a second persistent mutation outside coordination. */

Source from the content-addressed store, hash-verified

5918 * installer script may update only its process-local PATH after this returns;
5919 * it must not perform a second persistent mutation outside coordination. */
5920static int cli_ensure_windows_user_path(const char *bin_dir, bool dry_run) {
5921 wchar_t *wide_dir = cli_windows_utf8_to_wide(bin_dir);
5922 HKEY environment = NULL;
5923 if (!wide_dir || cli_windows_open_user_path_key(&environment) != CLI_OK) {
5924 free(wide_dir);
5925 return CLI_ERR;
5926 }
5927
5928 DWORD type = REG_EXPAND_SZ;
5929 DWORD bytes = 0;
5930 LONG queried = RegQueryValueExW(environment, L"Path", NULL, &type, NULL, &bytes);
5931 bool missing = queried == ERROR_FILE_NOT_FOUND;
5932 if ((!missing && queried != ERROR_SUCCESS) ||
5933 (!missing && type != REG_SZ && type != REG_EXPAND_SZ)) {
5934 RegCloseKey(environment);
5935 free(wide_dir);
5936 return CLI_ERR;
5937 }
5938 size_t existing_capacity = missing ? 1U : (size_t)bytes / sizeof(wchar_t) + 1U;
5939 wchar_t *existing = calloc(existing_capacity, sizeof(*existing));
5940 if (!existing) {
5941 RegCloseKey(environment);
5942 free(wide_dir);
5943 return CLI_ERR;
5944 }
5945 if (!missing) {
5946 DWORD read_bytes = bytes;
5947 if (RegQueryValueExW(environment, L"Path", NULL, &type, (BYTE *)existing, &read_bytes) !=
5948 ERROR_SUCCESS) {
5949 RegCloseKey(environment);
5950 free(existing);
5951 free(wide_dir);
5952 return CLI_ERR;
5953 }
5954 existing[existing_capacity - 1U] = L'\0';
5955 }
5956
5957 bool present = false;
5958 const wchar_t *cursor = existing;
5959 while (!present && *cursor) {
5960 const wchar_t *separator = wcschr(cursor, L';');
5961 size_t length = separator ? (size_t)(separator - cursor) : wcslen(cursor);
5962 present = cli_windows_path_segment_equal(cursor, length, wide_dir);
5963 cursor = separator ? separator + 1 : cursor + length;
5964 }
5965 if (present || dry_run) {
5966 RegCloseKey(environment);
5967 free(existing);
5968 free(wide_dir);
5969 return present ? CLI_TRUE : CLI_OK;
5970 }
5971
5972 size_t existing_length = wcslen(existing);
5973 size_t directory_length = wcslen(wide_dir);
5974 bool separator_needed = existing_length > 0 && existing[existing_length - 1U] != L';';
5975 if (existing_length > SIZE_MAX - directory_length - 2U) {
5976 RegCloseKey(environment);
5977 free(existing);

Callers 1

cli_install_activateFunction · 0.85

Tested by

no test coverage detected