Skip to content

Commit 5bfe911

Browse files
fix setting timestamp, prepare bandwidthLimit in FileIoAccessProvider
1 parent 962b7f2 commit 5bfe911

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

FileSyncLibNet/AccessProviders/FileIoAccessProvider.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Threading;
78

89
namespace FileSyncLibNet.AccessProviders
910
{
@@ -12,6 +13,9 @@ internal class FileIoAccessProvider : IAccessProvider
1213
public string AccessPath { get; private set; }
1314
private readonly RemoteState remoteState;
1415
private readonly ILogger logger;
16+
private const int DefaultBandwidthLimit = 100 * 1024 * 1024; // 100 MB/s
17+
private int bandwidthLimit = DefaultBandwidthLimit;
18+
1519
public FileIoAccessProvider(ILogger logger, string stateFilename)
1620
{
1721
this.logger = logger;
@@ -38,7 +42,7 @@ public FileInfo2 GetFileInfo(string path)
3842

3943
return new FileInfo2(path, fi.Exists)
4044
{
41-
LastWriteTime = fi.Exists ? fi.LastWriteTime : DateTime.MinValue,
45+
LastWriteTime = fi.Exists ? new DateTime(fi.LastWriteTime.Ticks) : DateTime.MinValue,
4246
Length = fi.Exists ? fi.Length : 0
4347
};
4448
}
@@ -79,9 +83,28 @@ public void WriteFile(FileInfo2 file, Stream content)
7983
{
8084
var realFilename = Path.Combine(AccessPath, file.Name);
8185
Directory.CreateDirectory(Path.GetDirectoryName(realFilename));
82-
using (var stream = File.Create(realFilename))
86+
if (bandwidthLimit == DefaultBandwidthLimit)
87+
{
88+
using (var stream = File.Create(realFilename))
89+
{
90+
content.CopyTo(stream);
91+
}
92+
}
93+
else
8394
{
84-
content.CopyTo(stream);
95+
using (var stream = new FileStream(realFilename, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.None))
96+
{
97+
int sleepIntervalMs = 50; // Fixed sleep interval
98+
int bufferSize = bandwidthLimit / (1000 / sleepIntervalMs); // Calculate buffer size based on bandwidth limit and interval
99+
byte[] buffer = new byte[bufferSize];
100+
int bytesRead;
101+
102+
while ((bytesRead = content.Read(buffer, 0, buffer.Length)) > 0)
103+
{
104+
stream.Write(buffer, 0, bytesRead);
105+
Thread.Sleep(sleepIntervalMs); // Sleep for the fixed interval
106+
}
107+
}
85108
}
86109
File.SetLastWriteTime(realFilename, file.LastWriteTime);
87110
remoteState?.SetFileInfo(realFilename, file);

0 commit comments

Comments
 (0)