Skip to content

Commit

Permalink
work with miniksa to make this look right
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Nov 4, 2020
1 parent 31dd278 commit 396c1a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void _LoadDynamicProfiles();

static bool _IsPackaged();
static void _WriteSettings(const std::string_view content, const hstring filepath);
static void _WriteSettings(std::string_view content, const hstring filepath);
static void _WriteBackupFile(std::string_view content, const winrt::hstring settingsPath);
static std::optional<std::string> _ReadUserSettings();
static std::optional<std::string> _ReadFile(HANDLE hFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,17 +1038,14 @@ const Json::Value& CascadiaSettings::_GetDisabledProfileSourcesJsonObject(const
}

// Method Description:
// - Write the current state of CascadiaSettings to our settings file
// - Create a backup file with the current contents, if one does not exist
// - Create a backup file with the current contents, iff one does not exist
// Arguments:
// - <none>
// - content - the content that we're writing to the backup file
// - settingsPath - the path to the settings file that we're going to create a backup for
// Return Value:
// - <none>
void CascadiaSettings::WriteSettingsToDisk() const
void CascadiaSettings::_WriteBackupFile(std::string_view content, const winrt::hstring settingsPath)
{
const auto settingsPath{ CascadiaSettings::SettingsPath() };

// write backup settings file, if one doesn't exist
const auto backupSettingsPath{ settingsPath + L".backup" };
wil::unique_hfile backupFile{ CreateFileW(backupSettingsPath.c_str(),
GENERIC_READ,
Expand All @@ -1057,10 +1054,28 @@ void CascadiaSettings::WriteSettingsToDisk() const
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
nullptr) };
if (GetLastError() != ERROR_FILE_EXISTS)

// throw if backup file already exists
THROW_LAST_ERROR_IF(INVALID_HANDLE_VALUE == backupFile.get());
THROW_LAST_ERROR_IF(!WriteFile(backupFile.get(), content.data(), gsl::narrow<DWORD>(content.size()), nullptr, nullptr));
}

// Method Description:
// - Write the current state of CascadiaSettings to our settings file
// - Create a backup file with the current contents, if one does not exist
// Arguments:
// - <none>
// Return Value:
// - <none>
void CascadiaSettings::WriteSettingsToDisk() const
{
const auto settingsPath{ CascadiaSettings::SettingsPath() };

try
{
_WriteSettings(_userSettingsString, backupSettingsPath);
_WriteBackupFile(_userSettingsString, settingsPath);
}
CATCH_LOG();

// write current settings to current settings file
const auto json{ ToJson() };
Expand Down

0 comments on commit 396c1a1

Please sign in to comment.