Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File copying errors #753

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LibraryManager.Build/Contracts/HostInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
throw new UnauthorizedAccessException();
}

bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
if (result)
{
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);
Expand Down
23 changes: 14 additions & 9 deletions src/LibraryManager.Contracts/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,28 @@ public static Task<Stream> ReadFileAsStreamAsync(string fileName, CancellationTo
/// </summary>
/// <param name="sourceFile">Full path to the source file</param>
/// <param name="destinationFile">Full path to the destination file</param>
/// <param name="logger">Pass logger to log errors.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>A boolean indicating whether the file was copied successfully</returns>
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, CancellationToken cancellationToken)
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, ILogger logger, CancellationToken cancellationToken)
{
try
//Sometimes it flaky. Let's retry
for (int i=0; i<2;i++)
{
using (FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
try
{
using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read);

await WriteToFileAsync(destinationFile, sourceStream, cancellationToken);
}

return true;
}
catch (Exception)
{
return false;
return true;
}
catch (Exception exception)
{
logger.Log($"Error during copying file {exception}", LogLevel.Error);
}
}
return false;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/LibraryManager.Vsix/Contracts/HostInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
}

await VsHelpers.CheckFileOutOfSourceControlAsync(absoluteDestinationPath);
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);

if (result)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libman/Contracts/HostInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
throw new UnauthorizedAccessException();
}

bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
if(result)
{
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);
Expand Down
2 changes: 1 addition & 1 deletion test/LibraryManager.Mocks/HostInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
throw new UnauthorizedAccessException();
}

bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);

return result;
}
Expand Down