Skip to content

Commit

Permalink
Merge pull request #753 from leotsarev/log-file-copying-errors
Browse files Browse the repository at this point in the history
File copying errors
  • Loading branch information
phil-allen-msft authored May 24, 2024
2 parents 419abd9 + ff78e14 commit 8ede78d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
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

0 comments on commit 8ede78d

Please sign in to comment.