Skip to content

Commit c5c2244

Browse files
authored
Fix for issue 134 (#135)
* Fix for #134 * Fix for legacy 0649 warnings.
1 parent 1492dab commit c5c2244

File tree

4 files changed

+69
-23
lines changed

4 files changed

+69
-23
lines changed

MFiles.VAF.Extensions.Tests/Configuration/MetadataStructureValidatorTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ public void SubConfiguration_HappyPath()
211211
[DataContract]
212212
class ConfigurationWithField
213213
{
214+
215+
// Suppress warnings about default values as that's what we're actually testing.
216+
#pragma warning disable 0649
214217
[DataMember]
215218
[MFObjType(AllowEmpty = true)]
216219
public MFIdentifier ObjectType;
@@ -224,6 +227,7 @@ class ConfigurationWithField
224227
[DataMember]
225228
public ConfigurationWithField SubConfiguration;
226229

230+
#pragma warning restore 0649
227231
}
228232

229233
[TestMethod]

MFiles.VAF.Extensions/Dashboards/Commands/DownloadSelectedLogsDashboardCommand.cs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ClientOperations clientOps
6969
var fileSizes = new List<string>();
7070
long totalSize = 0;
7171
long archiveSize = 0;
72+
int inaccessibleFileCount = 0;
7273
using (var memStream = new MemoryStream())
7374
{
7475
// Build the archive.
@@ -77,31 +78,49 @@ ClientOperations clientOps
7778
// Add each log file to the archive.
7879
foreach (string relPath in files)
7980
{
80-
// Sanity and safety check.
81-
// All paths must be inside the log folder.
82-
var fullPath = Path.GetFullPath(Path.Combine(rootPath, relPath));
83-
if (!fullPath.StartsWith(rootPath) ||
84-
Path.GetExtension(fullPath).ToLower() != ".log" ||
85-
!File.Exists(fullPath))
86-
throw new FileNotFoundException();
87-
88-
// Add the file to the archive.
89-
// NOTE:
90-
// We can't use archive.CreateEntryFromFile, because the log file may be
91-
// open by the logger, and the method doesn't have options for how we open
92-
// the file. To avoid issues, we must open the file with FileShare.ReadWrite.
93-
var fileEntry = archive.CreateEntry(relPath, CompressionLevel.Optimal);
94-
using (FileStream fileStream = File.Open(
95-
fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
96-
using (Stream entryStream = fileEntry.Open())
81+
ZipArchiveEntry fileEntry = null;
82+
string fullPath = null;
83+
try
9784
{
98-
// Copy the file to the archive.
99-
fileStream.CopyTo(entryStream);
85+
// Sanity and safety check.
86+
// All paths must be inside the log folder.
87+
fullPath = Path.GetFullPath(Path.Combine(rootPath, relPath));
88+
if (!fullPath.StartsWith(rootPath) ||
89+
Path.GetExtension(fullPath).ToLower() != ".log" ||
90+
!File.Exists(fullPath))
91+
throw new FileNotFoundException();
10092

101-
// Bookkeeping.
102-
var fileSize = fileStream.Length;
103-
fileSizes.Add($"{relPath}: {fileSize} bytes");
104-
totalSize += fileSize;
93+
// Add the file to the archive.
94+
// NOTE:
95+
// We can't use archive.CreateEntryFromFile, because the log file may be
96+
// open by the logger, and the method doesn't have options for how we open
97+
// the file. To avoid issues, we must open the file with FileShare.ReadWrite.
98+
using (FileStream fileStream = File.Open(
99+
fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
100+
{
101+
fileEntry = archive.CreateEntry(relPath, CompressionLevel.Optimal);
102+
using (Stream entryStream = fileEntry.Open())
103+
{
104+
// Copy the file to the archive.
105+
fileStream.CopyTo(entryStream);
106+
107+
// Bookkeeping.
108+
var fileSize = fileStream.Length;
109+
fileSizes.Add($"{relPath}: {fileSize} bytes");
110+
totalSize += fileSize;
111+
}
112+
}
113+
}
114+
catch (Exception e)
115+
{
116+
// The file was likely inaccessible. This can happen in some situations
117+
// where the container is recycled and the file is locked.
118+
inaccessibleFileCount++;
119+
120+
this.Logger?.Warn(e, $"Cannot add {relPath} to the log download.");
121+
122+
// Ensure we don't have a value in the zip for this file.
123+
try { fileEntry?.Delete(); } catch { }
105124
}
106125
}
107126
}
@@ -120,6 +139,16 @@ ClientOperations clientOps
120139
String.Join( "\n - ", fileSizes ) );
121140
*/
122141

142+
// If some files weren't accessible then inform the user.
143+
if(inaccessibleFileCount > 0)
144+
{
145+
this.Logger?.Error($"There were {inaccessibleFileCount} files that could not be added to the log download.");
146+
clientOps.ShowMessage
147+
(
148+
string.Format(Resources.Exceptions.Dashboard.LogFileDownloadIncomplete, inaccessibleFileCount)
149+
);
150+
}
151+
123152
// Prompt the user to download the archive.
124153
clientOps.Directives.Add(new PromptDownloadFile
125154
{

MFiles.VAF.Extensions/Resources/Exceptions/Dashboard.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MFiles.VAF.Extensions/Resources/Exceptions/Dashboard.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="LogFileDownloadIncomplete" xml:space="preserve">
121+
<value>{0} file(s) could not be added to the log download. This can happen when files are inaccessible due to container recycle or similar. The log data may be incomplete.</value>
122+
<comment>{0} is replaced with the number of files affected.</comment>
123+
</data>
120124
<data name="XmlFragmentNull" xml:space="preserve">
121125
<value>XML fragment for {0} was null so cannot be rendered.</value>
122126
<comment>Thrown if a dashboard component returns a null XML (XHTML) fragment. {0} will be replaced with the name of the component.</comment>

0 commit comments

Comments
 (0)