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:6813–6905  ·  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

6811 * installer script may update only its process-local PATH after this returns;
6812 * it must not perform a second persistent mutation outside coordination. */
6813static int cli_ensure_windows_user_path(const char *bin_dir, bool dry_run) {
6814 wchar_t *wide_dir = cli_windows_utf8_to_wide(bin_dir);
6815 HKEY environment = NULL;
6816 if (!wide_dir || cli_windows_open_user_path_key(&environment) != CLI_OK) {
6817 free(wide_dir);
6818 return CLI_ERR;
6819 }
6820
6821 DWORD type = REG_EXPAND_SZ;
6822 DWORD bytes = 0;
6823 LONG queried = RegQueryValueExW(environment, L"Path", NULL, &type, NULL, &bytes);
6824 bool missing = queried == ERROR_FILE_NOT_FOUND;
6825 if ((!missing && queried != ERROR_SUCCESS) ||
6826 (!missing && type != REG_SZ && type != REG_EXPAND_SZ)) {
6827 RegCloseKey(environment);
6828 free(wide_dir);
6829 return CLI_ERR;
6830 }
6831 size_t existing_capacity = missing ? 1U : (size_t)bytes / sizeof(wchar_t) + 1U;
6832 wchar_t *existing = calloc(existing_capacity, sizeof(*existing));
6833 if (!existing) {
6834 RegCloseKey(environment);
6835 free(wide_dir);
6836 return CLI_ERR;
6837 }
6838 if (!missing) {
6839 DWORD read_bytes = bytes;
6840 if (RegQueryValueExW(environment, L"Path", NULL, &type, (BYTE *)existing, &read_bytes) !=
6841 ERROR_SUCCESS) {
6842 RegCloseKey(environment);
6843 free(existing);
6844 free(wide_dir);
6845 return CLI_ERR;
6846 }
6847 existing[existing_capacity - 1U] = L'\0';
6848 }
6849
6850 bool present = false;
6851 const wchar_t *cursor = existing;
6852 while (!present && *cursor) {
6853 const wchar_t *separator = wcschr(cursor, L';');
6854 size_t length = separator ? (size_t)(separator - cursor) : wcslen(cursor);
6855 present = cli_windows_path_segment_equal(cursor, length, wide_dir);
6856 cursor = separator ? separator + 1 : cursor + length;
6857 }
6858 if (present || dry_run) {
6859 RegCloseKey(environment);
6860 free(existing);
6861 free(wide_dir);
6862 return present ? CLI_TRUE : CLI_OK;
6863 }
6864
6865 size_t existing_length = wcslen(existing);
6866 size_t directory_length = wcslen(wide_dir);
6867 bool separator_needed = existing_length > 0 && existing[existing_length - 1U] != L';';
6868 if (existing_length > SIZE_MAX - directory_length - 2U) {
6869 RegCloseKey(environment);
6870 free(existing);

Callers 2

cli_install_activateFunction · 0.85

Tested by

no test coverage detected