mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
[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:
committed by
GitHub
parent
31a0deee35
commit
3176eb94a9
@@ -1,11 +1,7 @@
|
||||
// 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;
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Hosts.Tests.Mocks;
|
||||
using HostsUILib.Helpers;
|
||||
@@ -19,6 +15,7 @@ namespace Hosts.FuzzTests
|
||||
{
|
||||
private static Mock<IUserSettings> _userSettings;
|
||||
private static Mock<IElevationHelper> _elevationHelper;
|
||||
private static Mock<IBackupManager> _backupManager;
|
||||
|
||||
// Case1: Fuzzing method for ValidIPv4
|
||||
public static void FuzzValidIPv4(ReadOnlySpan<byte> input)
|
||||
@@ -73,9 +70,10 @@ namespace Hosts.FuzzTests
|
||||
_userSettings = new Mock<IUserSettings>();
|
||||
_elevationHelper = new Mock<IElevationHelper>();
|
||||
_elevationHelper.Setup(m => m.IsElevated).Returns(true);
|
||||
_backupManager = new Mock<IBackupManager>();
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -30,8 +30,11 @@
|
||||
<Compile Include="..\HostsUILib\Models\Entry.cs" Link="Entry.cs" />
|
||||
<Compile Include="..\HostsUILib\Models\HostsData.cs" Link="HostsData.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\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>
|
||||
|
||||
156
src/modules/Hosts/Hosts.Tests/BackupManagerTest.cs
Normal file
156
src/modules/Hosts/Hosts.Tests/BackupManagerTest.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,10 @@ namespace Hosts.Tests
|
||||
[TestClass]
|
||||
public class HostsServiceTest
|
||||
{
|
||||
private const string BackupPath = @"C:\Backup\hosts";
|
||||
private static Mock<IUserSettings> _userSettings;
|
||||
private static Mock<IElevationHelper> _elevationHelper;
|
||||
private static Mock<IBackupManager> _backupManager;
|
||||
|
||||
[ClassInitialize]
|
||||
public static void ClassInitialize(TestContext context)
|
||||
@@ -29,27 +31,7 @@ namespace Hosts.Tests
|
||||
_userSettings = new Mock<IUserSettings>();
|
||||
_elevationHelper = new Mock<IElevationHelper>();
|
||||
_elevationHelper.Setup(m => m.IsElevated).Returns(true);
|
||||
}
|
||||
|
||||
[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);
|
||||
_backupManager = new Mock<IBackupManager>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -67,7 +49,7 @@ namespace Hosts.Tests
|
||||
";
|
||||
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -92,7 +74,7 @@ namespace Hosts.Tests
|
||||
";
|
||||
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -118,7 +100,7 @@ namespace Hosts.Tests
|
||||
";
|
||||
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -137,7 +119,7 @@ namespace Hosts.Tests
|
||||
public async Task Empty_Hosts()
|
||||
{
|
||||
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));
|
||||
|
||||
await service.WriteAsync(string.Empty, Enumerable.Empty<Entry>());
|
||||
@@ -168,7 +150,7 @@ namespace Hosts.Tests
|
||||
var fileSystem = new CustomMockFileSystem();
|
||||
var userSettings = new Mock<IUserSettings>();
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -200,7 +182,7 @@ namespace Hosts.Tests
|
||||
var fileSystem = new CustomMockFileSystem();
|
||||
var userSettings = new Mock<IUserSettings>();
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -224,7 +206,7 @@ namespace Hosts.Tests
|
||||
";
|
||||
|
||||
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));
|
||||
|
||||
var data = await service.ReadAsync();
|
||||
@@ -241,7 +223,7 @@ namespace Hosts.Tests
|
||||
var elevationHelper = new Mock<IElevationHelper>();
|
||||
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>()));
|
||||
}
|
||||
|
||||
@@ -249,7 +231,7 @@ namespace Hosts.Tests
|
||||
public async Task Save_ReadOnlyHostsException()
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -265,7 +247,7 @@ namespace Hosts.Tests
|
||||
public void Remove_ReadOnly_Attribute()
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -284,7 +266,7 @@ namespace Hosts.Tests
|
||||
public async Task Save_Hidden_Hosts()
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -316,7 +298,7 @@ namespace Hosts.Tests
|
||||
var fs = new CustomMockFileSystem();
|
||||
var settings = new Mock<IUserSettings>();
|
||||
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));
|
||||
|
||||
var data = await svc.ReadAsync();
|
||||
@@ -327,5 +309,57 @@ namespace Hosts.Tests
|
||||
var result = fs.GetFile(svc.HostsFilePath);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ namespace Hosts
|
||||
{
|
||||
// Core Services
|
||||
services.AddSingleton<IFileSystem, FileSystem>();
|
||||
services.AddSingleton<IBackupManager, BackupManager>();
|
||||
services.AddSingleton<IHostsService, HostsService>();
|
||||
services.AddSingleton<IUserSettings, Hosts.Settings.UserSettings>();
|
||||
services.AddSingleton<IElevationHelper, ElevationHelper>();
|
||||
@@ -74,7 +75,7 @@ namespace Hosts
|
||||
}).
|
||||
Build();
|
||||
|
||||
var cleanupBackupThread = new Thread(() =>
|
||||
var deleteBackupThread = new Thread(() =>
|
||||
{
|
||||
// Delete old backups only if running elevated
|
||||
if (!Host.GetService<IElevationHelper>().IsElevated)
|
||||
@@ -84,7 +85,7 @@ namespace Hosts
|
||||
|
||||
try
|
||||
{
|
||||
Host.GetService<IHostsService>().CleanupBackup();
|
||||
Host.GetService<IBackupManager>().Delete();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -92,8 +93,8 @@ namespace Hosts
|
||||
}
|
||||
});
|
||||
|
||||
cleanupBackupThread.IsBackground = true;
|
||||
cleanupBackupThread.Start();
|
||||
deleteBackupThread.IsBackground = true;
|
||||
deleteBackupThread.Start();
|
||||
|
||||
UnhandledException += App_UnhandledException;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// 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.
|
||||
|
||||
@@ -45,17 +45,34 @@ namespace Hosts.Settings
|
||||
public HostsAdditionalLinesPosition AdditionalLinesPosition { get; private set; }
|
||||
|
||||
// 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 UserSettings()
|
||||
{
|
||||
_settingsUtils = new SettingsUtils();
|
||||
ShowStartupWarning = true;
|
||||
LoopbackDuplicates = false;
|
||||
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
|
||||
Encoding = HostsEncoding.Utf8;
|
||||
var defaultSettings = new HostsProperties();
|
||||
ShowStartupWarning = defaultSettings.ShowStartupWarning;
|
||||
LoopbackDuplicates = defaultSettings.LoopbackDuplicates;
|
||||
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();
|
||||
|
||||
@@ -91,6 +108,11 @@ namespace Hosts.Settings
|
||||
Encoding = (HostsEncoding)settings.Properties.Encoding;
|
||||
LoopbackDuplicates = settings.Properties.LoopbackDuplicates;
|
||||
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;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<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')" />
|
||||
<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>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
@@ -46,7 +46,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)src\;$(SolutionDir)src\modules;$(SolutionDir)src\common\Telemetry;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>..\..\..\common\inc;..\..\..\common\Telemetry;..\..\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
|
||||
112
src/modules/Hosts/HostsUILib/Helpers/BackupManager.cs
Normal file
112
src/modules/Hosts/HostsUILib/Helpers/BackupManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
@@ -23,16 +22,15 @@ namespace HostsUILib.Helpers
|
||||
{
|
||||
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 IFileSystem _fileSystem;
|
||||
private readonly IUserSettings _userSettings;
|
||||
private readonly IElevationHelper _elevationHelper;
|
||||
private readonly IFileSystemWatcher _fileSystemWatcher;
|
||||
private readonly IBackupManager _backupManager;
|
||||
private readonly string _hostsFilePath;
|
||||
private bool _backupDone;
|
||||
private bool _disposed;
|
||||
|
||||
public string HostsFilePath => _hostsFilePath;
|
||||
@@ -44,11 +42,13 @@ namespace HostsUILib.Helpers
|
||||
public HostsService(
|
||||
IFileSystem fileSystem,
|
||||
IUserSettings userSettings,
|
||||
IElevationHelper elevationHelper)
|
||||
IElevationHelper elevationHelper,
|
||||
IBackupManager backupManager)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_userSettings = userSettings;
|
||||
_elevationHelper = elevationHelper;
|
||||
_backupManager = backupManager;
|
||||
|
||||
_hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\drivers\etc\hosts");
|
||||
|
||||
@@ -60,18 +60,13 @@ namespace HostsUILib.Helpers
|
||||
_fileSystemWatcher.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
public bool Exists()
|
||||
{
|
||||
return _fileSystem.File.Exists(HostsFilePath);
|
||||
}
|
||||
|
||||
public async Task<HostsData> ReadAsync()
|
||||
{
|
||||
var entries = new List<Entry>();
|
||||
var unparsedBuilder = new StringBuilder();
|
||||
var splittedEntries = false;
|
||||
|
||||
if (!Exists())
|
||||
if (!_fileSystem.File.Exists(HostsFilePath))
|
||||
{
|
||||
return new HostsData(entries, unparsedBuilder.ToString(), false);
|
||||
}
|
||||
@@ -192,15 +187,10 @@ namespace HostsUILib.Helpers
|
||||
{
|
||||
await _asyncLock.WaitAsync();
|
||||
_fileSystemWatcher.EnableRaisingEvents = false;
|
||||
|
||||
if (!_backupDone && Exists())
|
||||
{
|
||||
_fileSystem.File.Copy(HostsFilePath, HostsFilePath + _backupSuffix + DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture));
|
||||
_backupDone = true;
|
||||
}
|
||||
_backupManager.Create(HostsFilePath);
|
||||
|
||||
// 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);
|
||||
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()
|
||||
{
|
||||
var notepadFallback = false;
|
||||
|
||||
13
src/modules/Hosts/HostsUILib/Helpers/IBackupManager.cs
Normal file
13
src/modules/Hosts/HostsUILib/Helpers/IBackupManager.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,6 @@ namespace HostsUILib.Helpers
|
||||
|
||||
Task<bool> PingAsync(string address);
|
||||
|
||||
void CleanupBackup();
|
||||
|
||||
void OpenHostsFile();
|
||||
|
||||
void RemoveReadOnlyAttribute();
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,16 @@ namespace HostsUILib.Settings
|
||||
|
||||
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;
|
||||
|
||||
public delegate void OpenSettingsFunction();
|
||||
|
||||
Reference in New Issue
Block a user