Skip to content

Commit 8ede78d

Browse files
Merge pull request #753 from leotsarev/log-file-copying-errors
File copying errors
2 parents 419abd9 + ff78e14 commit 8ede78d

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

src/LibraryManager.Build/Contracts/HostInteraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
8383
throw new UnauthorizedAccessException();
8484
}
8585

86-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
86+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
8787
if (result)
8888
{
8989
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);

src/LibraryManager.Contracts/FileHelpers.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,28 @@ public static Task<Stream> ReadFileAsStreamAsync(string fileName, CancellationTo
136136
/// </summary>
137137
/// <param name="sourceFile">Full path to the source file</param>
138138
/// <param name="destinationFile">Full path to the destination file</param>
139+
/// <param name="logger">Pass logger to log errors.</param>
139140
/// <param name="cancellationToken">Cancellation token</param>
140141
/// <returns>A boolean indicating whether the file was copied successfully</returns>
141-
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, CancellationToken cancellationToken)
142+
public static async Task<bool> CopyFileAsync(string sourceFile, string destinationFile, ILogger logger, CancellationToken cancellationToken)
142143
{
143-
try
144+
//Sometimes it flaky. Let's retry
145+
for (int i=0; i<2;i++)
144146
{
145-
using (FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
147+
try
146148
{
149+
using FileStream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read);
150+
147151
await WriteToFileAsync(destinationFile, sourceStream, cancellationToken);
148-
}
149152

150-
return true;
151-
}
152-
catch (Exception)
153-
{
154-
return false;
153+
return true;
154+
}
155+
catch (Exception exception)
156+
{
157+
logger.Log($"Error during copying file {exception}", LogLevel.Error);
158+
}
155159
}
160+
return false;
156161
}
157162

158163
/// <summary>

src/LibraryManager.Vsix/Contracts/HostInteraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
121121
}
122122

123123
await VsHelpers.CheckFileOutOfSourceControlAsync(absoluteDestinationPath);
124-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
124+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
125125

126126
if (result)
127127
{

src/libman/Contracts/HostInteraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
108108
throw new UnauthorizedAccessException();
109109
}
110110

111-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
111+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
112112
if(result)
113113
{
114114
Logger.Log(string.Format(Resources.Text.FileWrittenToDisk, destinationPath.Replace('\\', '/')), LogLevel.Operation);

test/LibraryManager.Mocks/HostInteraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public async Task<bool> CopyFileAsync(string sourcePath, string destinationPath,
152152
throw new UnauthorizedAccessException();
153153
}
154154

155-
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);
155+
bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, Logger, cancellationToken);
156156

157157
return result;
158158
}

0 commit comments

Comments
 (0)