-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from dotnet-campus/t/lindexi/UsingHardLinkToZ…
…ipNtfsDiskSize 添加使用硬连接减少磁盘空间占用工具
- Loading branch information
Showing
19 changed files
with
884 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="UsingHardLinkToZipNtfsDiskSize.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:UsingHardLinkToZipNtfsDiskSize" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
12 changes: 12 additions & 0 deletions
12
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/App.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Windows; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
87 changes: 87 additions & 0 deletions
87
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/ChannelLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Threading.Channels; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public class ChannelLoggerProvider : ILoggerProvider | ||
{ | ||
public ChannelLoggerProvider(params IStringLoggerWriter[] stringLoggerWriterList) | ||
{ | ||
_stringLoggerWriterList = stringLoggerWriterList; | ||
var channel = Channel.CreateUnbounded<string>(new UnboundedChannelOptions() | ||
{ | ||
SingleReader = true | ||
}); | ||
_channel = channel; | ||
|
||
Task.Run(WriteLogAsync); | ||
} | ||
|
||
private readonly IStringLoggerWriter[] _stringLoggerWriterList; | ||
|
||
private async Task? WriteLogAsync() | ||
{ | ||
while (!_channel.Reader.Completion.IsCompleted) | ||
{ | ||
try | ||
{ | ||
var message = await _channel.Reader.ReadAsync(); | ||
foreach (var stringLoggerWriter in _stringLoggerWriterList) | ||
{ | ||
await stringLoggerWriter.WriteAsync(message); | ||
} | ||
} | ||
catch (ChannelClosedException) | ||
{ | ||
// 结束 | ||
} | ||
} | ||
|
||
foreach (var stringLoggerWriter in _stringLoggerWriterList) | ||
{ | ||
await stringLoggerWriter.DisposeAsync(); | ||
} | ||
} | ||
|
||
private readonly Channel<string> _channel; | ||
public void Dispose() | ||
{ | ||
ChannelWriter<string> channelWriter = _channel.Writer; | ||
channelWriter.TryComplete(); | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new ChannelLogger(_channel.Writer); | ||
} | ||
|
||
class ChannelLogger : ILogger, IDisposable | ||
{ | ||
public ChannelLogger(ChannelWriter<string> writer) | ||
{ | ||
_writer = writer; | ||
} | ||
|
||
private readonly ChannelWriter<string> _writer; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
var message = $"{DateTime.Now:yyyy.MM.dd HH:mm:ss,fff} [{logLevel}][{eventId}] {formatter(state, exception)}"; | ||
_ = _writer.WriteAsync(message); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return true; | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull | ||
{ | ||
return this; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/DispatcherStringLoggerWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
class DispatcherStringLoggerWriter : IStringLoggerWriter | ||
{ | ||
public DispatcherStringLoggerWriter(TextBlock logTextBlock) | ||
{ | ||
_logTextBlock = logTextBlock; | ||
} | ||
|
||
private readonly TextBlock _logTextBlock; | ||
|
||
private string _lastMessage = string.Empty; | ||
private bool _isInvalidate = false; | ||
|
||
public ValueTask WriteAsync(string message) | ||
{ | ||
_lastMessage = message; | ||
|
||
if (!_isInvalidate) | ||
{ | ||
_isInvalidate = true; | ||
|
||
_logTextBlock.Dispatcher.InvokeAsync(() => | ||
{ | ||
_logTextBlock.Text = _lastMessage; | ||
_isInvalidate = false; | ||
}); | ||
} | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
return ValueTask.CompletedTask; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/FileRecordModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public class FileRecordModel | ||
{ | ||
[Key] | ||
[Required] | ||
public string FilePath { set; get; } = null!; | ||
|
||
[Required] | ||
public long FileLength { set; get; } | ||
|
||
[Required] | ||
public string FileSha1Hash { set; get; } = null!; | ||
} |
28 changes: 28 additions & 0 deletions
28
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/FileStorageContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public class FileStorageContext : DbContext | ||
{ | ||
public FileStorageContext() | ||
{ | ||
// 用于设计时 | ||
_sqliteFile = "FileManger.db"; | ||
} | ||
|
||
public FileStorageContext(string sqliteFile) | ||
{ | ||
_sqliteFile = sqliteFile; | ||
} | ||
|
||
private readonly string _sqliteFile; | ||
|
||
public DbSet<FileStorageModel> FileStorageModel { set; get; } = null!; | ||
public DbSet<FileRecordModel> FileRecordModel { set; get; } = null!; | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder | ||
.UseSqlite($"Filename={_sqliteFile}"); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/FileStorageModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public class FileStorageModel | ||
{ | ||
[Key] | ||
[Required] | ||
public string FileSha1Hash { set; get; } = null!; | ||
|
||
/// <summary> | ||
/// 原始的文件路径 | ||
/// </summary> | ||
[Required] | ||
public string OriginFilePath { set; get; } = null!; | ||
|
||
/// <summary> | ||
/// 有多少个文件引用了 | ||
/// </summary> | ||
public long ReferenceCount { set; get; } | ||
|
||
/// <summary> | ||
/// 文件大小 | ||
/// </summary> | ||
public long FileLength { set; get; } | ||
} |
6 changes: 6 additions & 0 deletions
6
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/IStringLoggerWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public interface IStringLoggerWriter : IAsyncDisposable | ||
{ | ||
ValueTask WriteAsync(string message); | ||
} |
32 changes: 32 additions & 0 deletions
32
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/LogFileStringLoggerWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace UsingHardLinkToZipNtfsDiskSize; | ||
|
||
public class LogFileStringLoggerWriter : IStringLoggerWriter | ||
{ | ||
public LogFileStringLoggerWriter(DirectoryInfo logFolder) | ||
{ | ||
_logFolder = logFolder; | ||
var logFile = Path.Join(logFolder.FullName, "Log.txt"); | ||
var fileStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read); | ||
_fileStream = fileStream; | ||
var streamWriter = new StreamWriter(fileStream, Encoding.UTF8); | ||
_streamWriter = streamWriter; | ||
} | ||
|
||
private readonly DirectoryInfo _logFolder; | ||
private readonly FileStream _fileStream; | ||
private readonly StreamWriter _streamWriter; | ||
|
||
public async ValueTask WriteAsync(string message) | ||
{ | ||
await _streamWriter.WriteLineAsync(message); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await _streamWriter.DisposeAsync(); | ||
await _fileStream.DisposeAsync(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
CopyAfterCompileTool/UsingHardLinkToZipNtfsDiskSize/MainWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Window x:Class="UsingHardLinkToZipNtfsDiskSize.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:UsingHardLinkToZipNtfsDiskSize" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="450" Width="800"> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition></RowDefinition> | ||
<RowDefinition Height="Auto"></RowDefinition> | ||
</Grid.RowDefinitions> | ||
<Grid Background="Transparent" AllowDrop="true" DragEnter="Grid_OnDragEnter"> | ||
<Rectangle Margin="10,10,10,10" | ||
StrokeDashArray="2 2" StrokeThickness="2" Stroke="Gray" RadiusX="5" RadiusY="5"></Rectangle> | ||
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50">Drag Folder Here</TextBlock> | ||
</Grid> | ||
<TextBlock x:Name="LogTextBlock" Grid.Row="1" Margin="2 2 2 2" TextWrapping="NoWrap"></TextBlock> | ||
</Grid> | ||
</Window> |
Oops, something went wrong.