Fixed !WriteKeyValue with "@\"

This commit is contained in:
Birunthan Mohanathas 2012-05-21 12:07:50 +03:00
parent f2318c4a65
commit 4d18141696
2 changed files with 109 additions and 108 deletions

View File

@ -488,130 +488,131 @@ void CRainmeter::Bang_TrayMenu()
*/ */
void CRainmeter::Bang_WriteKeyValue(std::vector<std::wstring>& args, CMeterWindow* meterWindow) void CRainmeter::Bang_WriteKeyValue(std::vector<std::wstring>& args, CMeterWindow* meterWindow)
{ {
if (args.size() < 4) if (args.size() == 3 && meterWindow)
{ {
if (!meterWindow) return;
// Add the config filepath to the args // Add the config filepath to the args
args.push_back(meterWindow->GetSkinFilePath()); args.push_back(meterWindow->GetSkinFilePath());
} }
else if (args.size() < 4)
if (args.size() > 3)
{ {
const std::wstring& strIniFile = args[3]; Log(LOG_ERROR, L"!WriteKeyValue: Invalid parameters");
const WCHAR* iniFile = strIniFile.c_str(); return;
}
if (strIniFile.find(L"..\\") != std::wstring::npos || strIniFile.find(L"../") != std::wstring::npos) std::wstring& strIniFile = args[3];
{ if (meterWindow)
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Illegal path: %s", iniFile); {
return; meterWindow->MakePathAbsolute(strIniFile);
} }
const std::wstring& skinPath = GetSkinPath(); const WCHAR* iniFile = strIniFile.c_str();
const std::wstring settingsPath = GetSettingsPath();
if (_wcsnicmp(iniFile, skinPath.c_str(), skinPath.size()) != 0 && if (strIniFile.find(L"..\\") != std::wstring::npos || strIniFile.find(L"../") != std::wstring::npos)
_wcsnicmp(iniFile, settingsPath.c_str(), settingsPath.size()) != 0) {
{ LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Illegal path: %s", iniFile);
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Illegal path: %s", iniFile); return;
return; }
}
// Verify whether the file exists const std::wstring& skinPath = GetSkinPath();
if (_waccess(iniFile, 0) == -1) const std::wstring settingsPath = GetSettingsPath();
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: File not found: %s", iniFile);
return;
}
// Verify whether the file is read-only if (_wcsnicmp(iniFile, skinPath.c_str(), skinPath.size()) != 0 &&
DWORD attr = GetFileAttributes(iniFile); _wcsnicmp(iniFile, settingsPath.c_str(), settingsPath.size()) != 0)
if (attr == -1 || (attr & FILE_ATTRIBUTE_READONLY)) {
{ LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Illegal path: %s", iniFile);
LogWithArgs(LOG_WARNING, L"!WriteKeyValue: File is read-only: %s", iniFile); return;
return; }
}
// Avoid "IniFileMapping" // Verify whether the file exists
CSystem::UpdateIniFileMappingList(); if (_waccess(iniFile, 0) == -1)
std::wstring strIniWrite = CSystem::GetTemporaryFile(strIniFile); {
if (strIniWrite.size() == 1 && strIniWrite[0] == L'?') // error occurred LogWithArgs(LOG_ERROR, L"!WriteKeyValue: File not found: %s", iniFile);
{ return;
return; }
}
bool temporary = !strIniWrite.empty(); // Verify whether the file is read-only
DWORD attr = GetFileAttributes(iniFile);
if (attr == -1 || (attr & FILE_ATTRIBUTE_READONLY))
{
LogWithArgs(LOG_WARNING, L"!WriteKeyValue: File is read-only: %s", iniFile);
return;
}
if (temporary) // Avoid "IniFileMapping"
{ CSystem::UpdateIniFileMappingList();
if (GetDebug()) LogWithArgs(LOG_DEBUG, L"!WriteKeyValue: Writing to: %s (Temp: %s)", iniFile, strIniWrite.c_str()); std::wstring strIniWrite = CSystem::GetTemporaryFile(strIniFile);
} if (strIniWrite.size() == 1 && strIniWrite[0] == L'?') // error occurred
else {
{ return;
if (GetDebug()) LogWithArgs(LOG_DEBUG, L"!WriteKeyValue: Writing to: %s", iniFile); }
strIniWrite = strIniFile;
}
const WCHAR* iniWrite = strIniWrite.c_str(); bool temporary = !strIniWrite.empty();
const WCHAR* section = args[0].c_str();
const WCHAR* key = args[1].c_str();
const std::wstring& strValue = args[2];
bool formula = false; if (temporary)
BOOL write = 0; {
if (GetDebug()) LogWithArgs(LOG_DEBUG, L"!WriteKeyValue: Writing to: %s (Temp: %s)", iniFile, strIniWrite.c_str());
if (meterWindow)
{
double value;
formula = meterWindow->GetParser().ParseFormula(strValue, &value);
// Formula read fine
if (formula)
{
WCHAR buffer[256];
int len = _snwprintf_s(buffer, _TRUNCATE, L"%.5f", value);
CMeasure::RemoveTrailingZero(buffer, len);
write = WritePrivateProfileString(section, key, buffer, iniWrite);
}
}
if (!formula)
{
write = WritePrivateProfileString(section, key, strValue.c_str(), iniWrite);
}
if (temporary)
{
if (write != 0)
{
WritePrivateProfileString(NULL, NULL, NULL, iniWrite); // FLUSH
// Copy the file back
if (!CSystem::CopyFiles(strIniWrite, strIniFile))
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to copy temporary file to original filepath: %s (Temp: %s)", iniFile, iniWrite);
}
}
else // failed
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to write to: %s (Temp: %s)", iniFile, iniWrite);
}
// Remove a temporary file
CSystem::RemoveFile(strIniWrite);
}
else
{
if (write == 0) // failed
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to write to: %s", iniFile);
}
}
} }
else else
{ {
Log(LOG_ERROR, L"!WriteKeyValue: Invalid parameters"); if (GetDebug()) LogWithArgs(LOG_DEBUG, L"!WriteKeyValue: Writing to: %s", iniFile);
strIniWrite = strIniFile;
}
const WCHAR* iniWrite = strIniWrite.c_str();
const WCHAR* section = args[0].c_str();
const WCHAR* key = args[1].c_str();
const std::wstring& strValue = args[2];
bool formula = false;
BOOL write = 0;
if (meterWindow)
{
double value;
formula = meterWindow->GetParser().ParseFormula(strValue, &value);
// Formula read fine
if (formula)
{
WCHAR buffer[256];
int len = _snwprintf_s(buffer, _TRUNCATE, L"%.5f", value);
CMeasure::RemoveTrailingZero(buffer, len);
write = WritePrivateProfileString(section, key, buffer, iniWrite);
}
}
if (!formula)
{
write = WritePrivateProfileString(section, key, strValue.c_str(), iniWrite);
}
if (temporary)
{
if (write != 0)
{
WritePrivateProfileString(NULL, NULL, NULL, iniWrite); // FLUSH
// Copy the file back
if (!CSystem::CopyFiles(strIniWrite, strIniFile))
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to copy temporary file to original filepath: %s (Temp: %s)", iniFile, iniWrite);
}
}
else // failed
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to write to: %s (Temp: %s)", iniFile, iniWrite);
}
// Remove a temporary file
CSystem::RemoveFile(strIniWrite);
}
else
{
if (write == 0) // failed
{
LogWithArgs(LOG_ERROR, L"!WriteKeyValue: Failed to write to: %s", iniFile);
}
} }
} }
@ -1953,7 +1954,7 @@ void CRainmeter::ExecuteBang(const WCHAR* bang, std::vector<std::wstring>& args,
} }
else if (_wcsicmp(bang, L"SetWallpaper") == 0) else if (_wcsicmp(bang, L"SetWallpaper") == 0)
{ {
Bang_SetWallpaper(args); Bang_SetWallpaper(args, meterWindow);
} }
else if (_wcsicmp(bang, L"About") == 0) else if (_wcsicmp(bang, L"About") == 0)
{ {

View File

@ -241,7 +241,7 @@ private:
void Bang_ToggleConfig(std::vector<std::wstring>& args); void Bang_ToggleConfig(std::vector<std::wstring>& args);
void Bang_DeactivateConfigGroup(std::vector<std::wstring>& args); void Bang_DeactivateConfigGroup(std::vector<std::wstring>& args);
void Bang_SetClip(std::vector<std::wstring>& args); void Bang_SetClip(std::vector<std::wstring>& args);
void Bang_SetWallpaper(std::vector<std::wstring>& args); void Bang_SetWallpaper(std::vector<std::wstring>& args, CMeterWindow* meterWindow);
void Bang_SkinMenu(std::vector<std::wstring>& args, CMeterWindow* meterWindow); void Bang_SkinMenu(std::vector<std::wstring>& args, CMeterWindow* meterWindow);
void Bang_TrayMenu(); void Bang_TrayMenu();
void Bang_WriteKeyValue(std::vector<std::wstring>& args, CMeterWindow* meterWindow); void Bang_WriteKeyValue(std::vector<std::wstring>& args, CMeterWindow* meterWindow);