Skip to content

Commit d5e8d32

Browse files
committed
WinGet will attemp to correct issues with temp folder permissions (fix #3357, related to microsoft/winget-cli#5276)
1 parent 8b90226 commit d5e8d32

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
3+
using System.Security.AccessControl;
4+
using System.Security.Principal;
35
using System.Text;
46
using UniGetUI.Core.Data;
57
using UniGetUI.Core.Logging;
@@ -182,6 +184,8 @@ protected override ManagerStatus LoadManager()
182184
return status;
183185
}
184186

187+
TryRepairTempFolderPermissions();
188+
185189
Process process = new()
186190
{
187191
StartInfo = new ProcessStartInfo
@@ -250,21 +254,77 @@ public override void AttemptFastRepair()
250254
{
251255
if (WinGetHelper.Instance is NativeWinGetHelper)
252256
{
253-
Logger.ImportantInfo("Attempting to reconnec to WinGet COM Server...");
257+
Logger.ImportantInfo("Attempting to reconnect to WinGet COM Server...");
254258
ReRegisterCOMServer();
259+
TryRepairTempFolderPermissions();
255260
NO_PACKAGES_HAVE_BEEN_LOADED = false;
261+
256262
}
257263
else
258264
{
259265
Logger.Warn("Attempted to reconnect to COM Server but Bundled WinGet is being used.");
260266
}
261-
} catch (Exception ex)
267+
}
268+
catch (Exception ex)
262269
{
263270
Logger.Error("An error ocurred while attempting to reconnect to COM Server");
264271
Logger.Error(ex);
265272
}
266273
}
267274

275+
private static void TryRepairTempFolderPermissions()
276+
{
277+
if (!Settings.Get("EnableNewWinGetTroubleshooter")) return;
278+
279+
try
280+
{
281+
string tempPath = Path.GetTempPath();
282+
string winGetTempPath = Path.Combine(tempPath, "WinGet");
283+
284+
if (!Directory.Exists(winGetTempPath))
285+
{
286+
Logger.Warn("WinGet temp folder does not exist, creating it...");
287+
Directory.CreateDirectory(winGetTempPath);
288+
}
289+
290+
var directoryInfo = new DirectoryInfo(winGetTempPath);
291+
var accessControl = directoryInfo.GetAccessControl();
292+
var rules = accessControl.GetAccessRules(true, true, typeof(NTAccount));
293+
294+
bool userHasAccess = false;
295+
string currentUser = WindowsIdentity.GetCurrent().Name;
296+
297+
foreach (FileSystemAccessRule rule in rules)
298+
{
299+
if (rule.IdentityReference.Value.Equals(currentUser, StringComparison.CurrentCultureIgnoreCase))
300+
{
301+
userHasAccess = true;
302+
break;
303+
}
304+
}
305+
306+
if (!userHasAccess)
307+
{
308+
Logger.Warn("WinGet temp folder does not have correct permissions set, adding the current user...");
309+
var rule = new FileSystemAccessRule(
310+
currentUser,
311+
FileSystemRights.FullControl,
312+
InheritanceFlags.ContainerInherit |
313+
InheritanceFlags.ObjectInherit,
314+
PropagationFlags.None,
315+
AccessControlType.Allow);
316+
317+
accessControl.AddAccessRule(rule);
318+
directoryInfo.SetAccessControl(accessControl);
319+
}
320+
}
321+
catch (Exception ex)
322+
{
323+
Logger.Error("An error occurred while attempting to properly configure WinGet's temp folder permissions.");
324+
Logger.Error(ex);
325+
}
326+
}
327+
268328
public override void RefreshPackageIndexes()
269329
{
270330
Process p = new()

src/UniGetUI/Pages/SettingsPage.xaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,20 @@ public SettingsPage()
184184
_ = PEInterface.InstalledPackagesLoader.ReloadPackages();
185185
};
186186

187+
188+
CheckboxCard WinGet_EnableTroubleshooter_v2 = new()
189+
{
190+
Text = CoreTools.Translate("Enable an [experimental] improved WinGet troubleshooter"),
191+
SettingName = "EnableNewWinGetTroubleshooter"
192+
};
193+
WinGet_EnableTroubleshooter_v2.StateChanged += (_, _) =>
194+
{
195+
MainApp.Instance.MainWindow.WinGetWarningBanner.IsOpen = false;
196+
_ = PEInterface.InstalledPackagesLoader.ReloadPackages();
197+
};
198+
187199
ExtraSettingsCards[PEInterface.WinGet].Add(WinGet_EnableTroubleshooter);
200+
ExtraSettingsCards[PEInterface.WinGet].Add(WinGet_EnableTroubleshooter_v2);
188201
ExtraSettingsCards[PEInterface.WinGet].Add(WinGet_ResetWindowsIPackageManager);
189202
ExtraSettingsCards[PEInterface.WinGet].Add(WinGet_UseBundled);
190203

0 commit comments

Comments
 (0)