|
1 | 1 | using System.Diagnostics; |
2 | 2 | using System.Runtime.InteropServices; |
| 3 | +using System.Security.AccessControl; |
| 4 | +using System.Security.Principal; |
3 | 5 | using System.Text; |
4 | 6 | using UniGetUI.Core.Data; |
5 | 7 | using UniGetUI.Core.Logging; |
@@ -182,6 +184,8 @@ protected override ManagerStatus LoadManager() |
182 | 184 | return status; |
183 | 185 | } |
184 | 186 |
|
| 187 | + TryRepairTempFolderPermissions(); |
| 188 | + |
185 | 189 | Process process = new() |
186 | 190 | { |
187 | 191 | StartInfo = new ProcessStartInfo |
@@ -250,21 +254,77 @@ public override void AttemptFastRepair() |
250 | 254 | { |
251 | 255 | if (WinGetHelper.Instance is NativeWinGetHelper) |
252 | 256 | { |
253 | | - Logger.ImportantInfo("Attempting to reconnec to WinGet COM Server..."); |
| 257 | + Logger.ImportantInfo("Attempting to reconnect to WinGet COM Server..."); |
254 | 258 | ReRegisterCOMServer(); |
| 259 | + TryRepairTempFolderPermissions(); |
255 | 260 | NO_PACKAGES_HAVE_BEEN_LOADED = false; |
| 261 | + |
256 | 262 | } |
257 | 263 | else |
258 | 264 | { |
259 | 265 | Logger.Warn("Attempted to reconnect to COM Server but Bundled WinGet is being used."); |
260 | 266 | } |
261 | | - } catch (Exception ex) |
| 267 | + } |
| 268 | + catch (Exception ex) |
262 | 269 | { |
263 | 270 | Logger.Error("An error ocurred while attempting to reconnect to COM Server"); |
264 | 271 | Logger.Error(ex); |
265 | 272 | } |
266 | 273 | } |
267 | 274 |
|
| 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 | + |
268 | 328 | public override void RefreshPackageIndexes() |
269 | 329 | { |
270 | 330 | Process p = new() |
|
0 commit comments