Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix winget after a call to winget settings export #2767

Merged
merged 8 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/AppInstallerCLICore/Commands/RootCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ using namespace AppInstaller::Utility::literals;

namespace AppInstaller::CLI
{
using namespace Settings;

namespace
{
void OutputGroupPolicySourceList(Execution::Context& context, const std::vector<Settings::SourceFromPolicy>& sources, Resource::StringId header)
Expand Down Expand Up @@ -193,7 +195,7 @@ namespace AppInstaller::CLI
};

info << std::endl << Resource::String::Logs << ": "_liv << Runtime::GetPathTo(Runtime::PathName::DefaultLogLocationForDisplay).u8string() << std::endl;
info << std::endl << Resource::String::UserSettings << ": "_liv << Runtime::GetPathTo(Runtime::PathName::UserSettingsFileLocationForDisplay).u8string() << std::endl;
info << std::endl << Resource::String::UserSettings << ": "_liv << UserSettings::SettingsFilePath(true).u8string() << std::endl;

info << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/Workflows/SettingsFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace AppInstaller::CLI::Workflow
{
root["$schema"] = "https://aka.ms/winget-settings-export.schema.json";
root["adminSettings"] = Json::ValueType::objectValue;
root["userSettingsFile"] = Runtime::GetPathTo(Runtime::PathName::UserSettingsFileLocation).u8string();
root["userSettingsFile"] = UserSettings::SettingsFilePath().u8string();
}

void AddAdminSetting(AdminSetting setting)
Expand Down
37 changes: 37 additions & 0 deletions src/AppInstallerCommonCore/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,41 @@ namespace AppInstaller::Filesystem
return path;
}
}

void ReplaceCommonPathPrefix(std::filesystem::path& source, const std::filesystem::path& prefix, std::string_view replacement)
{
auto prefixItr = prefix.begin();
auto sourceItr = source.begin();

while (prefixItr != prefix.end() && sourceItr != source.end())
{
if (*prefixItr != *sourceItr)
{
break;
}

++prefixItr;
++sourceItr;
}

// Only replace source if we found all of prefix
if (prefixItr == prefix.end())
{
std::filesystem::path temp{ replacement };

for (; sourceItr != source.end(); ++sourceItr)
{
temp /= *sourceItr;
}

source = std::move(temp);
}
}

std::filesystem::path GetKnownFolderPath(const KNOWNFOLDERID& id)
{
wil::unique_cotaskmem_string knownFolder = nullptr;
THROW_IF_FAILED(SHGetKnownFolderPath(id, KF_FLAG_NO_ALIAS | KF_FLAG_DONT_VERIFY | KF_FLAG_NO_PACKAGE_REDIRECTION, NULL, &knownFolder));
return knownFolder.get();
}
}
4 changes: 0 additions & 4 deletions src/AppInstallerCommonCore/Public/AppInstallerRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ namespace AppInstaller::Runtime
PortableLinksUserLocation,
// The location where symlinks to portable packages are stored under machine scope.
PortableLinksMachineLocation,
// The location of the user settings json file.
UserSettingsFileLocation,
// The location of the user settings json file, anonymized using environment variables.
UserSettingsFileLocationForDisplay,
};

// The principal that an ACE applies to.
Expand Down
7 changes: 7 additions & 0 deletions src/AppInstallerCommonCore/Public/winget/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
#pragma once
#include <filesystem>
#include <shtypes.h>

namespace AppInstaller::Filesystem
{
Expand Down Expand Up @@ -35,4 +36,10 @@ namespace AppInstaller::Filesystem

// Get expanded file system path.
std::filesystem::path GetExpandedPath(const std::string& path);

// If `source` begins with all of `prefix`, replace that with `replacement`.
void ReplaceCommonPathPrefix(std::filesystem::path& source, const std::filesystem::path& prefix, std::string_view replacement);

// Gets the path of a known folder.
std::filesystem::path GetKnownFolderPath(const KNOWNFOLDERID& id);
}
2 changes: 1 addition & 1 deletion src/AppInstallerCommonCore/Public/winget/UserSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ namespace AppInstaller::Settings

static UserSettings const& Instance(const std::optional<std::string>& content = std::nullopt);

static std::filesystem::path SettingsFilePath();
static std::filesystem::path SettingsFilePath(bool forDisplay = false);

UserSettings(const UserSettings&) = delete;
UserSettings& operator=(const UserSettings&) = delete;
Expand Down
53 changes: 1 addition & 52 deletions src/AppInstallerCommonCore/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace AppInstaller::Runtime
{
using namespace Utility;
using namespace Settings;
using namespace Filesystem;

namespace
{
Expand Down Expand Up @@ -96,13 +97,6 @@ namespace AppInstaller::Runtime
static std::map<PathName, PathDetails> s_Path_TestHook_Overrides;
#endif

std::filesystem::path GetKnownFolderPath(const KNOWNFOLDERID& id)
{
wil::unique_cotaskmem_string knownFolder = nullptr;
THROW_IF_FAILED(SHGetKnownFolderPath(id, KF_FLAG_NO_ALIAS | KF_FLAG_DONT_VERIFY | KF_FLAG_NO_PACKAGE_REDIRECTION, NULL, &knownFolder));
return knownFolder.get();
}

// Gets the user's temp path
std::filesystem::path GetPathToUserTemp()
{
Expand Down Expand Up @@ -166,37 +160,6 @@ namespace AppInstaller::Runtime
return result;
}

// If `source` begins with all of `prefix`, replace that with `replacement`.
void ReplaceCommonPathPrefix(std::filesystem::path& source, const std::filesystem::path& prefix, std::string_view replacement)
{
auto prefixItr = prefix.begin();
auto sourceItr = source.begin();

while (prefixItr != prefix.end() && sourceItr != source.end())
{
if (*prefixItr != *sourceItr)
{
break;
}

++prefixItr;
++sourceItr;
}

// Only replace source if we found all of prefix
if (prefixItr == prefix.end())
{
std::filesystem::path temp{ replacement };

for (; sourceItr != source.end(); ++sourceItr)
{
temp /= *sourceItr;
}

source = std::move(temp);
}
}

DWORD AccessPermissionsFrom(ACEPermissions permissions)
{
DWORD result = 0;
Expand Down Expand Up @@ -547,13 +510,6 @@ namespace AppInstaller::Runtime
case PathName::PortableLinksMachineLocation:
result = GetPathDetailsCommon(path);
break;
case PathName::UserSettingsFileLocation:
result.Path = UserSettings::SettingsFilePath();
break;
case PathName::UserSettingsFileLocationForDisplay:
result.Path = UserSettings::SettingsFilePath();
ReplaceCommonPathPrefix(result.Path, GetKnownFolderPath(FOLDERID_LocalAppData), "%LOCALAPPDATA%");
break;
default:
THROW_HR(E_UNEXPECTED);
}
Expand Down Expand Up @@ -624,13 +580,6 @@ namespace AppInstaller::Runtime
case PathName::PortableLinksMachineLocation:
result = GetPathDetailsCommon(path);
break;
case PathName::UserSettingsFileLocation:
result.Path = UserSettings::SettingsFilePath();
break;
case PathName::UserSettingsFileLocationForDisplay:
result.Path = UserSettings::SettingsFilePath();
ReplaceCommonPathPrefix(result.Path, GetKnownFolderPath(FOLDERID_LocalAppData), "%LOCALAPPDATA%");
break;
default:
THROW_HR(E_UNEXPECTED);
}
Expand Down
36 changes: 30 additions & 6 deletions src/AppInstallerCommonCore/UserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "winget/JsonUtil.h"
#include "winget/Settings.h"
#include "winget/UserSettings.h"
#include "winget/filesystem.h"

#include "AppInstallerArchitecture.h"
#include "winget/Locale.h"
Expand All @@ -18,6 +19,7 @@ namespace AppInstaller::Settings
using namespace Utility;
using namespace Logging;
using namespace JSON;
using namespace Filesystem;

static constexpr std::string_view s_SettingEmpty =
R"({
Expand Down Expand Up @@ -93,11 +95,22 @@ namespace AppInstaller::Settings

std::optional<Json::Value> ParseFile(const StreamDefinition& setting, std::vector<UserSettings::Warning>& warnings)
{
auto stream = Stream{ setting }.Get();
if (stream)
try
{
std::string settingsContentStr = Utility::ReadEntireStream(*stream);
return ParseSettingsContent(settingsContentStr, setting.Name, warnings);
auto stream = Stream{ setting }.Get();
if (stream)
{
std::string settingsContentStr = Utility::ReadEntireStream(*stream);
return ParseSettingsContent(settingsContentStr, setting.Name, warnings);
}
}
catch (const std::exception& e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exposes the scenario where both the normal and backup settings fail, which can result in no output to the user about settings load failure? I think it just means an else for when the backup settings fails to load (~line 520 now) where we put in a different warning about not being able to load any settings file.

{
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << "Reason: " << e.what());
}
catch (...)
{
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << " Reason unknown.");
}

return {};
Expand Down Expand Up @@ -505,6 +518,10 @@ namespace AppInstaller::Settings
m_type = UserSettingsType::Backup;
settingsRoot = settingsBackupJson.value();
}
else
{
AICLI_LOG(Core, Warning, << "Failed loading settings files.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more along the lines of the user visible portion (without opening the log file). So a new:

m_warnings.emplace_back(StringResource::String::SettingsWarningWhoopsSettingsDidntLoad);

That would then at least warn the user "Hey, we couldn't load any settings, so you are getting the default behavior".

}
}
}

Expand Down Expand Up @@ -543,8 +560,15 @@ namespace AppInstaller::Settings
}
}

std::filesystem::path UserSettings::SettingsFilePath()
std::filesystem::path UserSettings::SettingsFilePath(bool forDisplay)
{
return Stream{ Stream::PrimaryUserSettings }.GetPath();
auto path = Stream{ Stream::PrimaryUserSettings }.GetPath();

if (forDisplay)
{
ReplaceCommonPathPrefix(path, GetKnownFolderPath(FOLDERID_LocalAppData), "%LOCALAPPDATA%");
}

return path;
}
}