[Hosts] Backup Settings (#37778)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request

Add backup settings for the Hosts File Editor to allow users to
customize the existing hardcoded logic.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] **Closes:** #37666
- [ ] **Communication:** I've discussed this with core contributors
already. If work hasn't been agreed, this work might be rejected
- [x] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end user facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [x] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here:
https://github.com/MicrosoftDocs/windows-dev-docs/pull/5342

<!-- Provide a more detailed description of the PR, other things fixed
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<img width="707" alt="image"
src="https://github.com/user-attachments/assets/e114431e-60e0-4b8c-bba7-df23f7af0182"
/>

<img width="707" alt="image"
src="https://github.com/user-attachments/assets/a02b591e-eb46-4964-bee7-548ec175b3aa"
/>

<img width="707" alt="image"
src="https://github.com/user-attachments/assets/6eb0ff21-74fa-4229-8832-df2df877b5cd"
/>

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

- Backup on: verified that backup isn't executed
- Backups off: Verified that only one backup is executed
- Verified that backup is located in the expected path
- Auto delete is set to "never": verified that no backups are deleted
- Auto delete is set to "based on count": verified that backups are
deleted according to count value
- Auto delete is set to "based on age and count": verified that backups
are deleted according to days and count values
- Verified that files without the backup pattern aren't deleted
- There is also adequate test coverage for these scenarios 🚀

---------

Co-authored-by: Gordon Lam (SH) <yeelam@microsoft.com>
This commit is contained in:
Davide Giacometti
2025-11-05 09:42:31 +01:00
committed by GitHub
parent 31a0deee35
commit 3176eb94a9
19 changed files with 686 additions and 84 deletions

View File

@@ -1,11 +1,7 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Hosts.Tests.Mocks; using Hosts.Tests.Mocks;
using HostsUILib.Helpers; using HostsUILib.Helpers;
@@ -19,6 +15,7 @@ namespace Hosts.FuzzTests
{ {
private static Mock<IUserSettings> _userSettings; private static Mock<IUserSettings> _userSettings;
private static Mock<IElevationHelper> _elevationHelper; private static Mock<IElevationHelper> _elevationHelper;
private static Mock<IBackupManager> _backupManager;
// Case1 Fuzzing method for ValidIPv4 // Case1 Fuzzing method for ValidIPv4
public static void FuzzValidIPv4(ReadOnlySpan<byte> input) public static void FuzzValidIPv4(ReadOnlySpan<byte> input)
@@ -73,9 +70,10 @@ namespace Hosts.FuzzTests
_userSettings = new Mock<IUserSettings>(); _userSettings = new Mock<IUserSettings>();
_elevationHelper = new Mock<IElevationHelper>(); _elevationHelper = new Mock<IElevationHelper>();
_elevationHelper.Setup(m => m.IsElevated).Returns(true); _elevationHelper.Setup(m => m.IsElevated).Returns(true);
_backupManager = new Mock<IBackupManager>();
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
string input = System.Text.Encoding.UTF8.GetString(data); string input = System.Text.Encoding.UTF8.GetString(data);

View File

@@ -30,8 +30,11 @@
<Compile Include="..\HostsUILib\Models\Entry.cs" Link="Entry.cs" /> <Compile Include="..\HostsUILib\Models\Entry.cs" Link="Entry.cs" />
<Compile Include="..\HostsUILib\Models\HostsData.cs" Link="HostsData.cs" /> <Compile Include="..\HostsUILib\Models\HostsData.cs" Link="HostsData.cs" />
<Compile Include="..\HostsUILib\Settings\HostsAdditionalLinesPosition.cs" Link="HostsAdditionalLinesPosition.cs" /> <Compile Include="..\HostsUILib\Settings\HostsAdditionalLinesPosition.cs" Link="HostsAdditionalLinesPosition.cs" />
<Compile Include="..\HostsUILib\Settings\HostsDeleteBackupMode.cs" Link="HostsDeleteBackupMode.cs" />
<Compile Include="..\HostsUILib\Settings\HostsEncoding.cs" Link="HostsEncoding.cs" /> <Compile Include="..\HostsUILib\Settings\HostsEncoding.cs" Link="HostsEncoding.cs" />
<Compile Include="..\HostsUILib\Settings\IUserSettings.cs" Link="IUserSettings.cs" /> <Compile Include="..\HostsUILib\Settings\IUserSettings.cs" Link="IUserSettings.cs" />
<Compile Include="..\HostsUILib\Helpers\IBackupManager.cs" Link="IBackupManager.cs" />
<Compile Include="..\HostsUILib\Helpers\BackupManager.cs" Link="BackupManager.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,156 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO.Abstractions.TestingHelpers;
using HostsUILib.Helpers;
using HostsUILib.Settings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Hosts.Tests
{
[TestClass]
public class BackupManagerTest
{
private const string HostsPath = @"C:\Windows\System32\Drivers\etc\hosts";
private const string BackupPath = @"C:\Backup\hosts";
private const string BackupSearchPattern = $"*_PowerToysBackup_*";
[TestMethod]
public void Hosts_Backup_Not_Executed()
{
var fileSystem = new MockFileSystem();
SetupFiles(fileSystem, true);
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.BackupHosts).Returns(false);
userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
var backupManager = new BackupManager(fileSystem, userSettings.Object);
backupManager.Create(HostsPath);
Assert.AreEqual(0, fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern).Length);
}
[TestMethod]
public void Hosts_Backup_Executed_Once()
{
var fileSystem = new MockFileSystem();
SetupFiles(fileSystem, true);
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.BackupHosts).Returns(true);
userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
var backupManager = new BackupManager(fileSystem, userSettings.Object);
backupManager.Create(HostsPath);
backupManager.Create(HostsPath);
Assert.AreEqual(1, fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern).Length);
var hostsContent = fileSystem.File.ReadAllText(HostsPath);
var backupContent = fileSystem.File.ReadAllText(fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern)[0]);
Assert.AreEqual(hostsContent, backupContent);
}
[DataTestMethod]
[DataRow(-10, -10)]
[DataRow(-10, 0)]
[DataRow(-10, 10)]
[DataRow(0, -10)]
[DataRow(0, 0)]
[DataRow(0, 10)]
[DataRow(10, -10)]
[DataRow(10, 0)]
[DataRow(10, 10)]
public void Hosts_Backups_Delete_Never(int count, int days)
{
var fileSystem = new MockFileSystem();
SetupFiles(fileSystem, false);
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
userSettings.Setup(m => m.DeleteBackupsMode).Returns(HostsDeleteBackupMode.Never);
var backupManager = new BackupManager(fileSystem, userSettings.Object);
backupManager.Delete();
Assert.AreEqual(30, fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern).Length);
Assert.AreEqual(31, fileSystem.Directory.GetFiles(BackupPath).Length);
}
[DataTestMethod]
[DataRow(-10, 30)]
[DataRow(0, 30)]
[DataRow(10, 10)]
public void Hosts_Backups_Delete_ByCount(int count, int expectedBackups)
{
var fileSystem = new MockFileSystem();
SetupFiles(fileSystem, false);
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
userSettings.Setup(m => m.DeleteBackupsMode).Returns(HostsDeleteBackupMode.Count);
userSettings.Setup(m => m.DeleteBackupsCount).Returns(count);
var backupManager = new BackupManager(fileSystem, userSettings.Object);
backupManager.Delete();
Assert.AreEqual(expectedBackups, fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern).Length);
Assert.AreEqual(expectedBackups + 1, fileSystem.Directory.GetFiles(BackupPath).Length);
}
[DataTestMethod]
[DataRow(-10, -10, 30)]
[DataRow(-10, 0, 30)]
[DataRow(-10, 10, 5)]
[DataRow(0, -10, 30)]
[DataRow(0, 0, 30)]
[DataRow(0, 10, 5)]
[DataRow(10, -10, 30)]
[DataRow(10, 0, 30)]
[DataRow(5, 1, 5)]
[DataRow(1, 15, 10)]
[DataRow(2, 2, 2)]
public void Hosts_Backups_Delete_ByAge(int count, int days, int expectedBackups)
{
var fileSystem = new MockFileSystem();
SetupFiles(fileSystem, false);
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
userSettings.Setup(m => m.DeleteBackupsMode).Returns(HostsDeleteBackupMode.Age);
userSettings.Setup(m => m.DeleteBackupsCount).Returns(count);
userSettings.Setup(m => m.DeleteBackupsDays).Returns(days);
var backupManager = new BackupManager(fileSystem, userSettings.Object);
backupManager.Delete();
Assert.AreEqual(expectedBackups, fileSystem.Directory.GetFiles(BackupPath, BackupSearchPattern).Length);
Assert.AreEqual(expectedBackups + 1, fileSystem.Directory.GetFiles(BackupPath).Length);
}
private void SetupFiles(MockFileSystem fileSystem, bool hostsOnly)
{
fileSystem.AddDirectory(BackupPath);
fileSystem.AddFile(HostsPath, new MockFileData("HOSTS FILE CONTENT"));
if (hostsOnly)
{
return;
}
var today = new DateTimeOffset(DateTime.Today);
var notBackupData = new MockFileData("NOT A BACKUP")
{
CreationTime = today.AddDays(-100),
};
fileSystem.AddFile(fileSystem.Path.Combine(BackupPath, "hosts_not_a_backup"), notBackupData);
// The first backup is from 5 days ago. There are 30 backups, one for each day.
var offset = 5;
for (var i = 0; i < 30; i++)
{
var backupData = new MockFileData("THIS IS A BACKUP")
{
CreationTime = today.AddDays(-i - offset),
};
fileSystem.AddFile(fileSystem.Path.Combine(BackupPath, $"hosts_PowerToysBackup_{i}"), backupData);
}
}
}
}

View File

@@ -20,8 +20,10 @@ namespace Hosts.Tests
[TestClass] [TestClass]
public class HostsServiceTest public class HostsServiceTest
{ {
private const string BackupPath = @"C:\Backup\hosts";
private static Mock<IUserSettings> _userSettings; private static Mock<IUserSettings> _userSettings;
private static Mock<IElevationHelper> _elevationHelper; private static Mock<IElevationHelper> _elevationHelper;
private static Mock<IBackupManager> _backupManager;
[ClassInitialize] [ClassInitialize]
public static void ClassInitialize(TestContext context) public static void ClassInitialize(TestContext context)
@@ -29,27 +31,7 @@ namespace Hosts.Tests
_userSettings = new Mock<IUserSettings>(); _userSettings = new Mock<IUserSettings>();
_elevationHelper = new Mock<IElevationHelper>(); _elevationHelper = new Mock<IElevationHelper>();
_elevationHelper.Setup(m => m.IsElevated).Returns(true); _elevationHelper.Setup(m => m.IsElevated).Returns(true);
} _backupManager = new Mock<IBackupManager>();
[TestMethod]
public void Hosts_Exists()
{
var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(string.Empty));
var result = service.Exists();
Assert.IsTrue(result);
}
[TestMethod]
public void Hosts_Not_Exists()
{
var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object);
var result = service.Exists();
Assert.IsFalse(result);
} }
[TestMethod] [TestMethod]
@@ -67,7 +49,7 @@ namespace Hosts.Tests
"; ";
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -92,7 +74,7 @@ namespace Hosts.Tests
"; ";
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -118,7 +100,7 @@ namespace Hosts.Tests
"; ";
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -137,7 +119,7 @@ namespace Hosts.Tests
public async Task Empty_Hosts() public async Task Empty_Hosts()
{ {
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(string.Empty)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(string.Empty));
await service.WriteAsync(string.Empty, Enumerable.Empty<Entry>()); await service.WriteAsync(string.Empty, Enumerable.Empty<Entry>());
@@ -168,7 +150,7 @@ namespace Hosts.Tests
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>(); var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Top); userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Top);
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -200,7 +182,7 @@ namespace Hosts.Tests
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>(); var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Bottom); userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Bottom);
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -224,7 +206,7 @@ namespace Hosts.Tests
"; ";
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync(); var data = await service.ReadAsync();
@@ -241,7 +223,7 @@ namespace Hosts.Tests
var elevationHelper = new Mock<IElevationHelper>(); var elevationHelper = new Mock<IElevationHelper>();
elevationHelper.Setup(m => m.IsElevated).Returns(false); elevationHelper.Setup(m => m.IsElevated).Returns(false);
var service = new HostsService(fileSystem, _userSettings.Object, elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, elevationHelper.Object, _backupManager.Object);
await Assert.ThrowsExceptionAsync<NotRunningElevatedException>(async () => await service.WriteAsync("# Empty hosts file", Enumerable.Empty<Entry>())); await Assert.ThrowsExceptionAsync<NotRunningElevatedException>(async () => await service.WriteAsync("# Empty hosts file", Enumerable.Empty<Entry>()));
} }
@@ -249,7 +231,7 @@ namespace Hosts.Tests
public async Task Save_ReadOnlyHostsException() public async Task Save_ReadOnlyHostsException()
{ {
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
var hostsFile = new MockFileData(string.Empty) var hostsFile = new MockFileData(string.Empty)
{ {
@@ -265,7 +247,7 @@ namespace Hosts.Tests
public void Remove_ReadOnly_Attribute() public void Remove_ReadOnly_Attribute()
{ {
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
var hostsFile = new MockFileData(string.Empty) var hostsFile = new MockFileData(string.Empty)
{ {
@@ -284,7 +266,7 @@ namespace Hosts.Tests
public async Task Save_Hidden_Hosts() public async Task Save_Hidden_Hosts()
{ {
var fileSystem = new CustomMockFileSystem(); var fileSystem = new CustomMockFileSystem();
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object); var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, _backupManager.Object);
var hostsFile = new MockFileData(string.Empty) var hostsFile = new MockFileData(string.Empty)
{ {
@@ -316,7 +298,7 @@ namespace Hosts.Tests
var fs = new CustomMockFileSystem(); var fs = new CustomMockFileSystem();
var settings = new Mock<IUserSettings>(); var settings = new Mock<IUserSettings>();
settings.Setup(s => s.NoLeadingSpaces).Returns(true); settings.Setup(s => s.NoLeadingSpaces).Returns(true);
var svc = new HostsService(fs, settings.Object, _elevationHelper.Object); var svc = new HostsService(fs, settings.Object, _elevationHelper.Object, _backupManager.Object);
fs.AddFile(svc.HostsFilePath, new MockFileData(content)); fs.AddFile(svc.HostsFilePath, new MockFileData(content));
var data = await svc.ReadAsync(); var data = await svc.ReadAsync();
@@ -327,5 +309,57 @@ namespace Hosts.Tests
var result = fs.GetFile(svc.HostsFilePath); var result = fs.GetFile(svc.HostsFilePath);
Assert.AreEqual(expected, result.TextContents); Assert.AreEqual(expected, result.TextContents);
} }
[TestMethod]
public async Task Hosts_Backup_Not_Executed()
{
var content =
@"10.1.1.1 host host.local # comment
10.1.1.2 host2 host2.local # another comment
";
var fileSystem = new CustomMockFileSystem();
fileSystem.AddDirectory(BackupPath);
_userSettings.Setup(m => m.BackupHosts).Returns(false);
_userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
var backupManager = new BackupManager(fileSystem, _userSettings.Object);
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, backupManager);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync();
var entries = data.Entries.ToList();
entries.Add(new Entry(0, "10.1.1.30", "host30 host30.local", "new entry", false));
await service.WriteAsync(data.AdditionalLines, data.Entries);
Assert.AreEqual(0, fileSystem.Directory.GetFiles(BackupPath).Length);
}
[TestMethod]
public async Task Hosts_Backup_Executed_Once()
{
var content =
@"10.1.1.1 host host.local # comment
10.1.1.2 host2 host2.local # another comment
";
var fileSystem = new CustomMockFileSystem();
_userSettings.Setup(m => m.BackupHosts).Returns(true);
_userSettings.Setup(m => m.BackupPath).Returns(BackupPath);
var backupManager = new BackupManager(fileSystem, _userSettings.Object);
var service = new HostsService(fileSystem, _userSettings.Object, _elevationHelper.Object, backupManager);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
var data = await service.ReadAsync();
var entries = data.Entries.ToList();
entries.Add(new Entry(0, "10.1.1.30", "host30 host30.local", "new entry", false));
await service.WriteAsync(data.AdditionalLines, data.Entries);
await service.WriteAsync(data.AdditionalLines, data.Entries);
Assert.AreEqual(1, fileSystem.Directory.GetFiles(BackupPath).Length);
var backupContent = fileSystem.File.ReadAllText(fileSystem.Directory.GetFiles(BackupPath)[0]);
Assert.AreEqual(content, backupContent);
}
} }
} }

View File

@@ -56,6 +56,7 @@ namespace Hosts
{ {
// Core Services // Core Services
services.AddSingleton<IFileSystem, FileSystem>(); services.AddSingleton<IFileSystem, FileSystem>();
services.AddSingleton<IBackupManager, BackupManager>();
services.AddSingleton<IHostsService, HostsService>(); services.AddSingleton<IHostsService, HostsService>();
services.AddSingleton<IUserSettings, Hosts.Settings.UserSettings>(); services.AddSingleton<IUserSettings, Hosts.Settings.UserSettings>();
services.AddSingleton<IElevationHelper, ElevationHelper>(); services.AddSingleton<IElevationHelper, ElevationHelper>();
@@ -74,7 +75,7 @@ namespace Hosts
}). }).
Build(); Build();
var cleanupBackupThread = new Thread(() => var deleteBackupThread = new Thread(() =>
{ {
// Delete old backups only if running elevated // Delete old backups only if running elevated
if (!Host.GetService<IElevationHelper>().IsElevated) if (!Host.GetService<IElevationHelper>().IsElevated)
@@ -84,7 +85,7 @@ namespace Hosts
try try
{ {
Host.GetService<IHostsService>().CleanupBackup(); Host.GetService<IBackupManager>().Delete();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -92,8 +93,8 @@ namespace Hosts
} }
}); });
cleanupBackupThread.IsBackground = true; deleteBackupThread.IsBackground = true;
cleanupBackupThread.Start(); deleteBackupThread.Start();
UnhandledException += App_UnhandledException; UnhandledException += App_UnhandledException;

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -45,17 +45,34 @@ namespace Hosts.Settings
public HostsAdditionalLinesPosition AdditionalLinesPosition { get; private set; } public HostsAdditionalLinesPosition AdditionalLinesPosition { get; private set; }
// Moved from Settings.UI.Library // Moved from Settings.UI.Library
public HostsEncoding Encoding { get; set; } public HostsEncoding Encoding { get; private set; }
public bool BackupHosts { get; private set; }
public string BackupPath { get; private set; }
// Moved from Settings.UI.Library
public HostsDeleteBackupMode DeleteBackupsMode { get; private set; }
public int DeleteBackupsDays { get; private set; }
public int DeleteBackupsCount { get; private set; }
public event EventHandler LoopbackDuplicatesChanged; public event EventHandler LoopbackDuplicatesChanged;
public UserSettings() public UserSettings()
{ {
_settingsUtils = new SettingsUtils(); _settingsUtils = new SettingsUtils();
ShowStartupWarning = true; var defaultSettings = new HostsProperties();
LoopbackDuplicates = false; ShowStartupWarning = defaultSettings.ShowStartupWarning;
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top; LoopbackDuplicates = defaultSettings.LoopbackDuplicates;
Encoding = HostsEncoding.Utf8; AdditionalLinesPosition = (HostsAdditionalLinesPosition)defaultSettings.AdditionalLinesPosition;
Encoding = (HostsEncoding)defaultSettings.Encoding;
BackupHosts = defaultSettings.BackupHosts;
BackupPath = defaultSettings.BackupPath;
DeleteBackupsMode = (HostsDeleteBackupMode)defaultSettings.DeleteBackupsMode;
DeleteBackupsDays = defaultSettings.DeleteBackupsDays;
DeleteBackupsCount = defaultSettings.DeleteBackupsCount;
LoadSettingsFromJson(); LoadSettingsFromJson();
@@ -91,6 +108,11 @@ namespace Hosts.Settings
Encoding = (HostsEncoding)settings.Properties.Encoding; Encoding = (HostsEncoding)settings.Properties.Encoding;
LoopbackDuplicates = settings.Properties.LoopbackDuplicates; LoopbackDuplicates = settings.Properties.LoopbackDuplicates;
NoLeadingSpaces = settings.Properties.NoLeadingSpaces; NoLeadingSpaces = settings.Properties.NoLeadingSpaces;
BackupHosts = settings.Properties.BackupHosts;
BackupPath = settings.Properties.BackupPath;
DeleteBackupsMode = (HostsDeleteBackupMode)settings.Properties.DeleteBackupsMode;
DeleteBackupsDays = settings.Properties.DeleteBackupsDays;
DeleteBackupsCount = settings.Properties.DeleteBackupsCount;
} }
retry = false; retry = false;

View File

@@ -2,7 +2,7 @@
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" /> <Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" />
<Target Name="GenerateResourceFiles" BeforeTargets="PrepareForBuild"> <Target Name="GenerateResourceFiles" BeforeTargets="PrepareForBuild">
<Exec Command="powershell -NonInteractive -executionpolicy Unrestricted $(SolutionDir)tools\build\convert-resx-to-rc.ps1 $(MSBuildThisFileDirectory) resource.base.h resource.h HostsModuleInterface.base.rc HostsModuleInterface.rc" /> <Exec Command="powershell -NonInteractive -executionpolicy Unrestricted ..\..\..\..\tools\build\convert-resx-to-rc.ps1 $(MSBuildThisFileDirectory) resource.base.h resource.h HostsModuleInterface.base.rc HostsModuleInterface.rc" />
</Target> </Target>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion> <VCProjectVersion>16.0</VCProjectVersion>
@@ -46,7 +46,7 @@
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>$(SolutionDir)src\;$(SolutionDir)src\modules;$(SolutionDir)src\common\Telemetry;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\..\..\common\inc;..\..\..\common\Telemetry;..\..\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,112 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Abstractions;
using System.Linq;
using HostsUILib.Settings;
namespace HostsUILib.Helpers
{
public class BackupManager : IBackupManager
{
private const string BackupSuffix = "_PowerToysBackup_";
private readonly IFileSystem _fileSystem;
private readonly IUserSettings _userSettings;
private bool _backupDone;
public BackupManager(IFileSystem fileSystem, IUserSettings userSettings)
{
_fileSystem = fileSystem;
_userSettings = userSettings;
}
public void Create(string hostsFilePath)
{
if (_backupDone || !_userSettings.BackupHosts || !_fileSystem.File.Exists(hostsFilePath))
{
return;
}
try
{
if (!_fileSystem.Directory.Exists(_userSettings.BackupPath))
{
_fileSystem.Directory.CreateDirectory(_userSettings.BackupPath);
}
var backupPath = _fileSystem.Path.Combine(_userSettings.BackupPath, $"hosts{BackupSuffix}{DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture)}");
_fileSystem.File.Copy(hostsFilePath, backupPath);
_backupDone = true;
}
catch (Exception ex)
{
LoggerInstance.Logger.LogError("Backup failed", ex);
}
}
public void Delete()
{
switch (_userSettings.DeleteBackupsMode)
{
case HostsDeleteBackupMode.Count:
DeleteByCount(_userSettings.DeleteBackupsCount);
break;
case HostsDeleteBackupMode.Age:
DeleteByAge(_userSettings.DeleteBackupsDays, _userSettings.DeleteBackupsCount);
break;
}
}
public void DeleteByCount(int count)
{
if (count < 1)
{
return;
}
var backups = GetAll().OrderByDescending(f => f.CreationTime).Skip(count).ToArray();
DeleteAll(backups);
}
public void DeleteByAge(int days, int count)
{
if (days < 1)
{
return;
}
var backupsEnumerable = GetAll();
if (count > 0)
{
backupsEnumerable = backupsEnumerable.OrderByDescending(f => f.CreationTime).Skip(count);
}
var backups = backupsEnumerable.Where(f => f.CreationTime < DateTime.Now.AddDays(-days)).ToArray();
DeleteAll(backups);
}
private IEnumerable<IFileInfo> GetAll()
{
if (!_fileSystem.Directory.Exists(_userSettings.BackupPath))
{
return [];
}
return _fileSystem.Directory.GetFiles(_userSettings.BackupPath, $"*{BackupSuffix}*").Select(_fileSystem.FileInfo.New);
}
private void DeleteAll(IFileInfo[] files)
{
foreach (var f in files)
{
_fileSystem.File.Delete(f.FullName);
}
}
}
}

View File

@@ -5,7 +5,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
using System.Linq; using System.Linq;
@@ -23,16 +22,15 @@ namespace HostsUILib.Helpers
{ {
public partial class HostsService : IHostsService, IDisposable public partial class HostsService : IHostsService, IDisposable
{ {
private const string _backupSuffix = $"_PowerToysBackup_"; private const int DefaultBufferSize = 4096; // From System.IO.File source code
private const int _defaultBufferSize = 4096; // From System.IO.File source code
private readonly SemaphoreSlim _asyncLock = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _asyncLock = new SemaphoreSlim(1, 1);
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly IUserSettings _userSettings; private readonly IUserSettings _userSettings;
private readonly IElevationHelper _elevationHelper; private readonly IElevationHelper _elevationHelper;
private readonly IFileSystemWatcher _fileSystemWatcher; private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly IBackupManager _backupManager;
private readonly string _hostsFilePath; private readonly string _hostsFilePath;
private bool _backupDone;
private bool _disposed; private bool _disposed;
public string HostsFilePath => _hostsFilePath; public string HostsFilePath => _hostsFilePath;
@@ -44,11 +42,13 @@ namespace HostsUILib.Helpers
public HostsService( public HostsService(
IFileSystem fileSystem, IFileSystem fileSystem,
IUserSettings userSettings, IUserSettings userSettings,
IElevationHelper elevationHelper) IElevationHelper elevationHelper,
IBackupManager backupManager)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
_userSettings = userSettings; _userSettings = userSettings;
_elevationHelper = elevationHelper; _elevationHelper = elevationHelper;
_backupManager = backupManager;
_hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\drivers\etc\hosts"); _hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\drivers\etc\hosts");
@@ -60,18 +60,13 @@ namespace HostsUILib.Helpers
_fileSystemWatcher.EnableRaisingEvents = true; _fileSystemWatcher.EnableRaisingEvents = true;
} }
public bool Exists()
{
return _fileSystem.File.Exists(HostsFilePath);
}
public async Task<HostsData> ReadAsync() public async Task<HostsData> ReadAsync()
{ {
var entries = new List<Entry>(); var entries = new List<Entry>();
var unparsedBuilder = new StringBuilder(); var unparsedBuilder = new StringBuilder();
var splittedEntries = false; var splittedEntries = false;
if (!Exists()) if (!_fileSystem.File.Exists(HostsFilePath))
{ {
return new HostsData(entries, unparsedBuilder.ToString(), false); return new HostsData(entries, unparsedBuilder.ToString(), false);
} }
@@ -192,15 +187,10 @@ namespace HostsUILib.Helpers
{ {
await _asyncLock.WaitAsync(); await _asyncLock.WaitAsync();
_fileSystemWatcher.EnableRaisingEvents = false; _fileSystemWatcher.EnableRaisingEvents = false;
_backupManager.Create(HostsFilePath);
if (!_backupDone && Exists())
{
_fileSystem.File.Copy(HostsFilePath, HostsFilePath + _backupSuffix + DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture));
_backupDone = true;
}
// FileMode.OpenOrCreate is necessary to prevent UnauthorizedAccessException when the hosts file is hidden // FileMode.OpenOrCreate is necessary to prevent UnauthorizedAccessException when the hosts file is hidden
using var stream = _fileSystem.FileStream.New(HostsFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, _defaultBufferSize, FileOptions.Asynchronous); using var stream = _fileSystem.FileStream.New(HostsFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, DefaultBufferSize, FileOptions.Asynchronous);
using var writer = new StreamWriter(stream, Encoding); using var writer = new StreamWriter(stream, Encoding);
foreach (var line in lines) foreach (var line in lines)
{ {
@@ -231,15 +221,6 @@ namespace HostsUILib.Helpers
} }
} }
public void CleanupBackup()
{
Directory.GetFiles(Path.GetDirectoryName(HostsFilePath), $"*{_backupSuffix}*")
.Select(f => new FileInfo(f))
.Where(f => f.CreationTime < DateTime.Now.AddDays(-15))
.ToList()
.ForEach(f => f.Delete());
}
public void OpenHostsFile() public void OpenHostsFile()
{ {
var notepadFallback = false; var notepadFallback = false;

View File

@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace HostsUILib.Helpers
{
public interface IBackupManager
{
void Create(string hostsFilePath);
void Delete();
}
}

View File

@@ -22,8 +22,6 @@ namespace HostsUILib.Helpers
Task<bool> PingAsync(string address); Task<bool> PingAsync(string address);
void CleanupBackup();
void OpenHostsFile(); void OpenHostsFile();
void RemoveReadOnlyAttribute(); void RemoveReadOnlyAttribute();

View File

@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace HostsUILib.Settings
{
public enum HostsDeleteBackupMode
{
Never = 0,
Count = 1,
Age = 2,
}
}

View File

@@ -16,6 +16,16 @@ namespace HostsUILib.Settings
public HostsEncoding Encoding { get; } public HostsEncoding Encoding { get; }
public bool BackupHosts { get; }
public string BackupPath { get; }
public HostsDeleteBackupMode DeleteBackupsMode { get; }
public int DeleteBackupsDays { get; }
public int DeleteBackupsCount { get; }
event EventHandler LoopbackDuplicatesChanged; event EventHandler LoopbackDuplicatesChanged;
public delegate void OpenSettingsFunction(); public delegate void OpenSettingsFunction();

View File

@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Settings.UI.Library.Enumerations
{
public enum HostsDeleteBackupMode
{
Never = 0,
Count = 1,
Age = 2,
}
}

View File

@@ -1,10 +1,10 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Settings.UI.Library.Attributes;
using Settings.UI.Library.Enumerations; using Settings.UI.Library.Enumerations;
namespace Microsoft.PowerToys.Settings.UI.Library namespace Microsoft.PowerToys.Settings.UI.Library
@@ -27,6 +27,17 @@ namespace Microsoft.PowerToys.Settings.UI.Library
[JsonConverter(typeof(BoolPropertyJsonConverter))] [JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool NoLeadingSpaces { get; set; } public bool NoLeadingSpaces { get; set; }
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool BackupHosts { get; set; }
public string BackupPath { get; set; }
public HostsDeleteBackupMode DeleteBackupsMode { get; set; }
public int DeleteBackupsDays { get; set; }
public int DeleteBackupsCount { get; set; }
public HostsProperties() public HostsProperties()
{ {
ShowStartupWarning = true; ShowStartupWarning = true;
@@ -35,6 +46,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top; AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
Encoding = HostsEncoding.Utf8; Encoding = HostsEncoding.Utf8;
NoLeadingSpaces = false; NoLeadingSpaces = false;
BackupHosts = true;
BackupPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\drivers\etc");
DeleteBackupsMode = HostsDeleteBackupMode.Age;
DeleteBackupsDays = 15;
DeleteBackupsCount = 5;
} }
} }
} }

View File

@@ -7,9 +7,24 @@
xmlns:local="using:Microsoft.PowerToys.Settings.UI.Helpers" xmlns:local="using:Microsoft.PowerToys.Settings.UI.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls" xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
xmlns:tkconverters="using:CommunityToolkit.WinUI.Converters"
xmlns:ui="using:CommunityToolkit.WinUI" xmlns:ui="using:CommunityToolkit.WinUI"
mc:Ignorable="d"> mc:Ignorable="d">
<Page.Resources>
<tkconverters:ResourceNameToResourceStringConverter x:Key="ResourceNameToResourceStringConverter" />
<tkconverters:DoubleToVisibilityConverter
x:Key="CountBasedVisibilityConverter"
FalseValue="Collapsed"
GreaterThan="0"
LessThan="2"
TrueValue="Visible" />
<tkconverters:DoubleToVisibilityConverter
x:Key="AgeBasedVisibilityConverter"
FalseValue="Collapsed"
GreaterThan="1"
LessThan="3"
TrueValue="Visible" />
</Page.Resources>
<controls:SettingsPageControl x:Uid="Hosts" ModuleImageSource="ms-appx:///Assets/Settings/Modules/HostsFileEditor.png"> <controls:SettingsPageControl x:Uid="Hosts" ModuleImageSource="ms-appx:///Assets/Settings/Modules/HostsFileEditor.png">
<controls:SettingsPageControl.ModuleContent> <controls:SettingsPageControl.ModuleContent>
<StackPanel ChildrenTransitions="{StaticResource SettingsCardsAnimations}" Orientation="Vertical"> <StackPanel ChildrenTransitions="{StaticResource SettingsCardsAnimations}" Orientation="Vertical">
@@ -70,6 +85,92 @@
</ComboBox> </ComboBox>
</tkcontrols:SettingsCard> </tkcontrols:SettingsCard>
</controls:SettingsGroup> </controls:SettingsGroup>
<controls:SettingsGroup x:Uid="Hosts_Backup_GroupSettings" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<tkcontrols:SettingsExpander
x:Uid="Hosts_Backup"
HeaderIcon="{ui:FontIcon Glyph=&#xEA35;}"
IsExpanded="True">
<ToggleSwitch IsOn="{x:Bind ViewModel.BackupHosts, Mode=TwoWay}" />
<tkcontrols:SettingsExpander.Items>
<tkcontrols:SettingsCard x:Uid="Hosts_Backup_Location" IsEnabled="{x:Bind ViewModel.BackupHosts, Mode=OneWay}">
<Grid ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
x:Name="pathTextBlock"
Grid.Column="0"
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
IsTextSelectionEnabled="True"
Text="{x:Bind ViewModel.BackupPath, Mode=TwoWay}"
TextWrapping="Wrap">
<ToolTipService.ToolTip>
<ToolTip IsEnabled="{Binding IsTextTrimmed, ElementName=pathTextBlock, Mode=OneWay}">
<TextBlock Text="{x:Bind ViewModel.BackupPath, Mode=TwoWay}" />
</ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
<Button
Grid.Column="1"
Command="{x:Bind ViewModel.SelectBackupPathEventHandler}"
Content="&#xe8da;"
FontFamily="{ThemeResource SymbolThemeFontFamily}">
<ToolTipService.ToolTip>
<ToolTip>
<TextBlock x:Uid="Hosts_ButtonSelectLocation" />
</ToolTip>
</ToolTipService.ToolTip>
</Button>
</Grid>
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="Hosts_Delete_Backup" IsEnabled="{x:Bind ViewModel.BackupHosts, Mode=OneWay}">
<ComboBox MinWidth="{StaticResource SettingActionControlMinWidth}" SelectedIndex="{x:Bind Path=ViewModel.DeleteBackupsMode, Mode=TwoWay}">
<ComboBoxItem x:Uid="Hosts_DeleteBackupMode_Never" />
<ComboBoxItem x:Uid="Hosts_DeleteBackupMode_CountBased" />
<ComboBoxItem x:Uid="Hosts_DeleteBackupMode_AgeAndCountBased" />
</ComboBox>
</tkcontrols:SettingsCard>
<!-- Count -->
<tkcontrols:SettingsCard
x:Name="BackupsCountInputSettingsCard"
IsEnabled="{x:Bind ViewModel.BackupHosts, Mode=OneWay}"
Visibility="{x:Bind ViewModel.DeleteBackupsMode, Converter={StaticResource CountBasedVisibilityConverter}, Mode=OneWay}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
Minimum="{x:Bind ViewModel.MinimumBackupsCount, Mode=OneWay}"
SpinButtonPlacementMode="Compact"
Value="{x:Bind ViewModel.DeleteBackupsCount, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<!-- Age and count -->
<tkcontrols:SettingsCard
x:Uid="Hosts_Backup_DaysInput"
IsEnabled="{x:Bind ViewModel.BackupHosts, Mode=OneWay}"
Visibility="{x:Bind ViewModel.DeleteBackupsMode, Converter={StaticResource AgeBasedVisibilityConverter}, Mode=OneWay}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
Minimum="1"
SpinButtonPlacementMode="Compact"
Value="{x:Bind ViewModel.DeleteBackupsDays, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard
x:Name="BackupsCountInputAgeSettingsCard"
IsEnabled="{x:Bind ViewModel.BackupHosts, Mode=OneWay}"
Visibility="{x:Bind ViewModel.DeleteBackupsMode, Converter={StaticResource AgeBasedVisibilityConverter}, Mode=OneWay}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
Minimum="{x:Bind ViewModel.MinimumBackupsCount, Mode=OneWay}"
SpinButtonPlacementMode="Compact"
Value="{x:Bind ViewModel.DeleteBackupsCount, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander>
</controls:SettingsGroup>
</StackPanel> </StackPanel>
</controls:SettingsPageControl.ModuleContent> </controls:SettingsPageControl.ModuleContent>

View File

@@ -18,6 +18,10 @@ namespace Microsoft.PowerToys.Settings.UI.Views
InitializeComponent(); InitializeComponent();
var settingsUtils = new SettingsUtils(); var settingsUtils = new SettingsUtils();
ViewModel = new HostsViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), SettingsRepository<HostsSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, App.IsElevated); ViewModel = new HostsViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), SettingsRepository<HostsSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, App.IsElevated);
BackupsCountInputSettingsCard.Header = ResourceLoaderInstance.ResourceLoader.GetString("Hosts_Backup_CountInput_Header");
BackupsCountInputSettingsCard.Description = ResourceLoaderInstance.ResourceLoader.GetString("Hosts_Backup_CountInput_Description");
BackupsCountInputAgeSettingsCard.Header = ResourceLoaderInstance.ResourceLoader.GetString("Hosts_Backup_CountInput_Header");
BackupsCountInputAgeSettingsCard.Description = ResourceLoaderInstance.ResourceLoader.GetString("Hosts_Backup_CountInput_Age_Description");
} }
public void RefreshEnabledState() public void RefreshEnabledState()

View File

@@ -5599,4 +5599,48 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
<data name="Shortcut_Conflict_LearnMore.Content" xml:space="preserve"> <data name="Shortcut_Conflict_LearnMore.Content" xml:space="preserve">
<value>Learn more</value> <value>Learn more</value>
</data> </data>
<data name="Hosts_Backup_GroupSettings.Header" xml:space="preserve">
<value>Backup</value>
</data>
<data name="Hosts_Backup.Header" xml:space="preserve">
<value>Backup hosts file</value>
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="Hosts_Backup.Description" xml:space="preserve">
<value>Automatically create a backup of the hosts file when you save for the first time in a session</value>
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="Hosts_Backup_Location.Header" xml:space="preserve">
<value>Location</value>
</data>
<data name="Hosts_ButtonSelectLocation.Text" xml:space="preserve">
<value>Select location</value>
</data>
<data name="Hosts_Delete_Backup.Header" xml:space="preserve">
<value>Automatically delete backups</value>
</data>
<data name="Hosts_Backup_DaysInput.Header" xml:space="preserve">
<value>Days</value>
</data>
<data name="Hosts_Backup_CountInput_Description" xml:space="preserve">
<value>Set the number of backups to keep. Older backups will be deleted once the limit is reached.</value>
</data>
<data name="Hosts_Backup_DaysInput.Description" xml:space="preserve">
<value>Set the number of days to keep backups. Older backups will be deleted once the limit is reached.</value>
</data>
<data name="Hosts_DeleteBackupMode_Never.Content" xml:space="preserve">
<value>Never</value>
</data>
<data name="Hosts_DeleteBackupMode_CountBased.Content" xml:space="preserve">
<value>Based on count</value>
</data>
<data name="Hosts_DeleteBackupMode_AgeAndCountBased.Content" xml:space="preserve">
<value>Based on age and count</value>
</data>
<data name="Hosts_Backup_CountInput_Age_Description" xml:space="preserve">
<value>Set an optional number of backups to always keep despite their age</value>
</data>
<data name="Hosts_Backup_CountInput_Header" xml:space="preserve">
<value>Backup count</value>
</data>
</root> </root>

View File

@@ -5,8 +5,8 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using global::PowerToys.GPOWrapper; using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers; using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces; using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
@@ -33,6 +33,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public ButtonClickCommand LaunchEventHandler => new ButtonClickCommand(Launch); public ButtonClickCommand LaunchEventHandler => new ButtonClickCommand(Launch);
public ButtonClickCommand SelectBackupPathEventHandler => new ButtonClickCommand(SelectBackupPath);
public bool IsEnabled public bool IsEnabled
{ {
get => _isEnabled; get => _isEnabled;
@@ -144,6 +146,74 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
} }
} }
public bool BackupHosts
{
get => Settings.Properties.BackupHosts;
set
{
if (value != Settings.Properties.BackupHosts)
{
Settings.Properties.BackupHosts = value;
NotifyPropertyChanged();
}
}
}
public string BackupPath
{
get => Settings.Properties.BackupPath;
set
{
if (value != Settings.Properties.BackupPath)
{
Settings.Properties.BackupPath = value;
NotifyPropertyChanged();
}
}
}
public int DeleteBackupsMode
{
get => (int)Settings.Properties.DeleteBackupsMode;
set
{
if (value != (int)Settings.Properties.DeleteBackupsMode)
{
Settings.Properties.DeleteBackupsMode = (HostsDeleteBackupMode)value;
NotifyPropertyChanged();
OnPropertyChanged(nameof(MinimumBackupsCount));
}
}
}
public int DeleteBackupsDays
{
get => Settings.Properties.DeleteBackupsDays;
set
{
if (value != Settings.Properties.DeleteBackupsDays)
{
Settings.Properties.DeleteBackupsDays = value;
NotifyPropertyChanged();
}
}
}
public int DeleteBackupsCount
{
get => Settings.Properties.DeleteBackupsCount;
set
{
if (value != Settings.Properties.DeleteBackupsCount)
{
Settings.Properties.DeleteBackupsCount = value;
NotifyPropertyChanged();
}
}
}
public int MinimumBackupsCount => DeleteBackupsMode == 1 ? 1 : 0;
public HostsViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<HostsSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated) public HostsViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<HostsSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated)
{ {
SettingsUtils = settingsUtils; SettingsUtils = settingsUtils;
@@ -192,5 +262,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
InitializeEnabledValue(); InitializeEnabledValue();
OnPropertyChanged(nameof(IsEnabled)); OnPropertyChanged(nameof(IsEnabled));
} }
public void SelectBackupPath()
{
// This function was changed to use the shell32 API to open folder dialog
// as the old one (PickSingleFolderAsync) can't work when the process is elevated
// TODO: go back PickSingleFolderAsync when it's fixed
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.GetSettingsWindow());
var result = ShellGetFolder.GetFolderDialog(hwnd);
if (!string.IsNullOrEmpty(result))
{
BackupPath = result;
}
}
} }
} }