Skip to content

Commit

Permalink
Various refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
levitation committed Jan 17, 2022
1 parent 3077837 commit c0996be
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 207 deletions.
4 changes: 4 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
18 changes: 12 additions & 6 deletions AsyncToSyncCodeRoundtripSynchroniserMonitorNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
<WarningsAsErrors>NU1605</WarningsAsErrors>
<NoWarn>CS1998;SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
<WarningsAsErrors>CS4014;NU1605</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -34,8 +34,8 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
<WarningsAsErrors>NU1605</WarningsAsErrors>
<NoWarn>CS1998;SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
<WarningsAsErrors>CS4014;NU1605</WarningsAsErrors>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -45,6 +45,9 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="AsyncEnumerable, Version=4.0.2.0, Culture=neutral, PublicKeyToken=0426b068161bd1d1, processorArchitecture=MSIL">
<HintPath>..\..\FolderSync\FolderSyncNet\packages\AsyncEnumerator.4.0.2\lib\net45\AsyncEnumerable.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Configuration, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Extensions.Configuration.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -133,8 +136,8 @@
<Reference Include="System.Net.Sockets, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\FolderSync\FolderSyncNet\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
Expand All @@ -151,6 +154,9 @@
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\FolderSync\FolderSyncNet\packages\System.Threading.Tasks.Extensions.4.5.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
Expand Down
11 changes: 6 additions & 5 deletions AsyncToSyncConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define ASYNC
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -120,15 +121,15 @@ static class AsyncToSyncConverter



public static async Task AsyncFileUpdated(string fullName, Context context)
public static async Task AsyncFileUpdated(Context context)
{
//using (await Global.FileOperationAsyncLock.LockAsync())
{
var fileData = await FileExtensions.ReadAllTextAsync(Extensions.GetLongPath(fullName), context.Token);
var fileData = await FileExtensions.ReadAllTextAsync(Extensions.GetLongPath(context.Event.FullName), context.Token);
var originalData = fileData;


if (fullName.EndsWith(".cs"))
if (context.Event.FullName.EndsWith(".cs"))
{
foreach (var replacement in CS_Replacements)
{
Expand All @@ -140,7 +141,7 @@ public static async Task AsyncFileUpdated(string fullName, Context context)
fileData = CS_TaskReplaceRegex.Replace(fileData, CS_TaskReplaceRegexReplacement);
fileData = CS_TaskDelayReplaceRegex.Replace(fileData, CS_TaskDelayReplaceRegexReplacement);
}
else if (fullName.EndsWith(".py"))
else if (context.Event.FullName.EndsWith(".py"))
{
foreach (var replacement in PY_Replacements)
{
Expand All @@ -156,7 +157,7 @@ public static async Task AsyncFileUpdated(string fullName, Context context)
}


await ConsoleWatch.SaveFileModifications(fullName, fileData, originalData, context);
await ConsoleWatch.SaveFileModifications(fileData, originalData, context);

} //using (await Global.FileOperationAsyncLock.LockAsync())
} //public static async Task AsyncFileUpdated(string fullName, Context context)
Expand Down
46 changes: 36 additions & 10 deletions BinaryFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ namespace AsyncToSyncCodeRoundtripSynchroniserMonitor
{
public static partial class FileExtensions
{
public static int MaxByteArraySize = 0x7FFFFFC7; //https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element?redirectedfrom=MSDN#remarks

//https://stackoverflow.com/questions/18472867/checking-equality-for-two-byte-arrays/
public static bool BinaryEqual(Binary a, Binary b)
{
return a.Equals(b);
}

public static async Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
public static async Task<Tuple<byte[], long>> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default(CancellationToken), long maxFileSize = 0)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
return await Task.FromCanceled<byte[]>(cancellationToken);
return await Task.FromCanceled<Tuple<byte[], long>>(cancellationToken);

try
{
using (FileStream stream = new FileStream(
using (var stream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
Expand All @@ -41,10 +43,17 @@ public static bool BinaryEqual(Binary a, Binary b)
useAsync: true
))
{
var len = (int)stream.Length; //NB! the lenght might change during the code execution, so need to save it into separate variable
long len = stream.Length; //NB! the length might change during the code execution, so need to save it into separate variable

maxFileSize = Math.Min(MaxByteArraySize, maxFileSize);
if (maxFileSize > 0 && len > maxFileSize)
{
return new Tuple<byte[], long>(null, len);
}

byte[] result = new byte[len];
await stream.ReadAsync(result, 0, len, cancellationToken);
return result;
await stream.ReadAsync(result, 0, (int)len, cancellationToken);
return new Tuple<byte[], long>(result, len);
}
}
catch (IOException)
Expand All @@ -61,21 +70,21 @@ public static bool BinaryEqual(Binary a, Binary b)
catch (TaskCanceledException)
{
//do nothing here
return await Task.FromCanceled<byte[]>(cancellationToken);
return await Task.FromCanceled<Tuple<byte[], long>>(cancellationToken);
}
}
}
}

public static async Task WriteAllBytesAsync(string path, byte[] contents, CancellationToken cancellationToken = default(CancellationToken))
public static async Task WriteAllBytesAsync(string path, byte[] contents, CancellationToken cancellationToken = default(CancellationToken), int writeBufferKB = 0, int bufferWriteDelayMs = 0)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
using (FileStream stream = new FileStream(
using (var stream = new FileStream(
path,
FileMode.OpenOrCreate,
FileAccess.Write,
Expand All @@ -84,7 +93,24 @@ public static bool BinaryEqual(Binary a, Binary b)
useAsync: true
))
{
await stream.WriteAsync(contents, 0, contents.Length, cancellationToken);
var writeBufferLength = writeBufferKB * 1024;
if (writeBufferLength <= 0 || bufferWriteDelayMs <= 0) //NB! disable write buffer length limit if delay is 0
writeBufferLength = contents.Length;

for (int i = 0; i < contents.Length; i += writeBufferLength)
{
if (i > 0 && bufferWriteDelayMs > 0)
{
#if !NOASYNC
await Task.Delay(bufferWriteDelayMs, cancellationToken);
#else
cancellationToken.WaitHandle.WaitOne(bufferWriteDelayMs);
#endif
}

await stream.WriteAsync(contents, i, writeBufferLength, cancellationToken);
}

return;
}
}
Expand Down
40 changes: 40 additions & 0 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#define ASYNC
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Nito.AsyncEx;

namespace AsyncToSyncCodeRoundtripSynchroniserMonitor
{
Expand Down Expand Up @@ -46,6 +50,29 @@ public static Dictionary<TValue, TKey> Inverse<TKey, TValue>(this IDictionary<TK
return result;
}

public static long? CheckDiskSpace(string path)
{
long? freeBytes = null;

try //NB! on some drives (for example, RAM drives, GetDiskFreeSpaceEx does not work
{
//NB! DriveInfo works on paths well in Linux //TODO: what about Mac?
var drive = new DriveInfo(path);
freeBytes = drive.AvailableFreeSpace;
}
catch (ArgumentException)
{
if (ConfigParser.IsWindows)
{
long freeBytesOut;
if (WindowsDllImport.GetDiskFreeSpaceEx(path, out freeBytesOut, out var _, out var __))
freeBytes = freeBytesOut;
}
}

return freeBytes;
}

public static string GetLongPath(string path)
{
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7 and https://superuser.com/questions/1617012/support-of-the-unc-server-share-syntax-in-windows
Expand All @@ -59,5 +86,18 @@ public static string GetLongPath(string path)
return @"\\?\" + path;
}
}

public static async Task FSOperation(Action func, CancellationToken token)
{
//await Task.Run(func).WaitAsync(token);
func();
}

public static async Task<T> FSOperation<T>(Func<T> func, CancellationToken token)
{
//var result = await Task.Run(func).WaitAsync(token);
var result = func();
return result;
}
}
}
Loading

0 comments on commit c0996be

Please sign in to comment.