(#1986) Use unique temp file names in RefreshEnv.cmd#1987
(#1986) Use unique temp file names in RefreshEnv.cmd#1987jvuori wants to merge 1 commit intochocolatey:masterfrom
Conversation
| echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..." | ||
|
|
||
| :: Generate unique file names | ||
| for /F "tokens=* usebackq" %%F IN (`powershell -command "[guid]::NewGuid().ToString()"`) do set REFRESHENV_GUID=%%F |
There was a problem hiding this comment.
This would require at least a 2 second start up time for PowerShell. Not that I'm against that, but that is longer than the entire thing takes to run now. So this is more than doubling the time. Are there any other methods?
There was a problem hiding this comment.
I'm afraid pure CMD doesn't provide anything reliable we could really use. Sounds an odd idea but what if we just implemented some tiny win32 application (with C) that would only print its process ID and that would be used as the random part of the temporary file names? The executable would be so small that it could be even put into version control beside RefreshEnv.cmd.
There was a problem hiding this comment.
LOL! 13 824 bytes :)
#include <stdio.h>
#include <processthreadsapi.h>
void main()
{
printf("%d", GetCurrentProcessId());
}gcc pid.c -o pid.exe -s -Os
| for /F "tokens=* usebackq" %%F IN (`powershell -command "[guid]::NewGuid().ToString()"`) do set REFRESHENV_GUID=%%F | ||
| set REFRESHENV_ENVSET_TMP="%TEMP%\refreshenv_envset_%REFRESHENV_GUID%.tmp" | ||
| set REFRESHENV_ENVGET_TMP="%TEMP%\refreshenv_envget_%REFRESHENV_GUID%.tmp" | ||
| set REFRESHENV_ENV_CMD="%TEMP%\refreshenv_env_%REFRESHENV_GUID%.cmd" |
There was a problem hiding this comment.
Let's put this somewhere else besides temp. Somewhere more locked down, like a temp folder under the Chocolatey directories that is locked down.
|
|
Fix the issue with generating a GUID (with Powershell [guid]::NewGuid() function) and using it as a part of temporary file names.
Tried first %RANDOM% but it's not random enough. It didn't pass the test script which launches two refreshenv terminals almost at the same time. %TIME% worked better but but it's not 100% reliable either. Besides the format is locale specific so in certain countries it includes characters (colon) that can't be used in file names.