From 318b00b6a2146a3739b2f17a159366efb53e5eca Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Fri, 25 Oct 2019 01:29:04 +0200 Subject: Windows: Avoid unnecessarily update of system encryption SetupConfig related files if there content didn't change --- src/Common/BootEncryption.cpp | 57 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) (limited to 'src/Common') diff --git a/src/Common/BootEncryption.cpp b/src/Common/BootEncryption.cpp index 6131ede5..9a04bb79 100644 --- a/src/Common/BootEncryption.cpp +++ b/src/Common/BootEncryption.cpp @@ -3151,20 +3151,69 @@ namespace VeraCrypt if (bForInstall) { + /* Before updating files, we check first if they already have the expected content. If yes, then we don't perform + * any write operation to avoid modifying file timestamps Unnecessarily. + */ + bool bSkipWrite = false; + wchar_t wszBuffer [2 * TC_MAX_PATH] = {0}; wstring szPathParam = L"\""; szPathParam += szInstallPath; szPathParam += L"\""; - WritePrivateProfileStringW (L"SetupConfig", L"ReflectDrivers", szPathParam.c_str(), szSetupconfigLocation); + if ( (0 < GetPrivateProfileStringW (L"SetupConfig", L"ReflectDrivers", L"", wszBuffer, ARRAYSIZE (wszBuffer), szSetupconfigLocation)) + && (_wcsicmp (wszBuffer, szPathParam.c_str()) == 0) + ) + { + bSkipWrite = true; + } + + if (!bSkipWrite) + WritePrivateProfileStringW (L"SetupConfig", L"ReflectDrivers", szPathParam.c_str(), szSetupconfigLocation); + + bSkipWrite = false; szPathParam = GetProgramConfigPath (L"SetupComplete.cmd"); - FILE* scriptFile = _wfopen (szPathParam.c_str(), L"w"); + + wstring wszExpectedValue = L"\""; + wszExpectedValue += szInstallPath; + wszExpectedValue += L"\\VeraCrypt.exe\" /PostOOBE"; + + FILE* scriptFile = _wfopen (szPathParam.c_str(), L"r"); if (scriptFile) { - fwprintf (scriptFile, L"\"%s\\VeraCrypt.exe\" /PostOOBE\n", szInstallPath); + long fileSize = _filelength (_fileno (scriptFile)); + if (fileSize < (2 * TC_MAX_PATH)) + { + fgetws (wszBuffer, ARRAYSIZE (wszBuffer), scriptFile); + + if (wszBuffer[wcslen (wszBuffer) - 1] == L'\n') + wszBuffer[wcslen (wszBuffer) - 1] = 0; + + bSkipWrite = (0 == _wcsicmp (wszBuffer, wszExpectedValue.c_str())); + } fclose (scriptFile); + } - WritePrivateProfileStringW (L"SetupConfig", L"PostOOBE", szPathParam.c_str(), szSetupconfigLocation); + if (!bSkipWrite) + { + scriptFile = _wfopen (szPathParam.c_str(), L"w"); + if (scriptFile) + { + fwprintf (scriptFile, L"%s\n", wszExpectedValue.c_str()); + fclose (scriptFile); + } } + + bSkipWrite = false; + + if ( (0 < GetPrivateProfileStringW (L"SetupConfig", L"PostOOBE", L"", wszBuffer, ARRAYSIZE (wszBuffer), szSetupconfigLocation)) + && (_wcsicmp (wszBuffer, szPathParam.c_str()) == 0) + ) + { + bSkipWrite = true; + } + + if (!bSkipWrite) + WritePrivateProfileStringW (L"SetupConfig", L"PostOOBE", szPathParam.c_str(), szSetupconfigLocation); } else { -- cgit v1.2.3