Skip to content

Commit

Permalink
#23 Fix for file corruption on Xml export when overwriting a larger f…
Browse files Browse the repository at this point in the history
…ile (#24)

* #23 Fix for file corruption on Xml export when overwriting a larger file.
* Bumped version for release
  • Loading branch information
andyward authored Sep 9, 2024
1 parent 8bb7250 commit aa32aea
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
70 changes: 70 additions & 0 deletions Xbim.InformationSpecifications.NewTests/IoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,76 @@ public void CanRoundTripCardinalityInJson(CardinalityEnum cardinality)
}
}

[Fact]
public void ResavingXmlShouldTruncateFileWhenShorter()
{
var filename = Path.ChangeExtension(Path.GetTempFileName(), "ids");
var logger = GetXunitLogger();
try
{
// Arrange
Xids x = XidsTestHelpers.GetSimpleXids();

var spec = x.AllSpecifications().First();
spec.Description = "Some longish descripion, just to pad the file out a bit...";

x.ExportBuildingSmartIDS(filename, logger);

var initialLength = new FileInfo(filename).Length;

// Act
spec.Description = "Shorter now";
x.ExportBuildingSmartIDS(filename, logger); // resave over original, which _should_ truncate

// Assert
var latest = new FileInfo(filename);
Xids.CanLoad(latest, logger).Should().BeTrue("file should not be corrupt");

var latestLength = new FileInfo(filename).Length;
latestLength.Should().BeLessThan(initialLength, "file expected to have shrunk");

}
finally
{
if (File.Exists(filename)) File.Delete(filename);
}
}

[Fact]
public void ResavingJsonShouldTruncateFileWhenShorter()
{
var filename = Path.ChangeExtension(Path.GetTempFileName(), "ids");
var logger = GetXunitLogger();
try
{
// Arrange
Xids x = XidsTestHelpers.GetSimpleXids();

var spec = x.AllSpecifications().First();
spec.Description = "Some longish descripion, just to pad the file out a bit...";

x.SaveAsJson(filename);

var initialLength = new FileInfo(filename).Length;

// Act
spec.Description = "Shorter now";
x.SaveAsJson(filename); // resave over original, which _should_ truncate

// Assert
var latest = new FileInfo(filename);
Xids.CanLoad(latest, logger).Should().BeTrue("file should not be corrupt");

var latestLength = new FileInfo(filename).Length;
latestLength.Should().BeLessThan(initialLength, "file expected to have shrunk");

}
finally
{
if (File.Exists(filename)) File.Delete(filename);
}
}

private static Xids BuildMultiSpecGroupIDS()
{
var file = new FileInfo(@"bsFiles/bsFilesSelf/TestFile.ids");
Expand Down
2 changes: 1 addition & 1 deletion Xbim.InformationSpecifications/IO/FacetGroupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void SaveAsJson(this FacetGroup groupToSave, string destinationFil
logger?.LogWarning("File is being overwritten: {file}", f.FullName);
File.Delete(destinationFile);
}
using var s = File.OpenWrite(destinationFile);
using var s = File.Create(destinationFile);
groupToSave.SaveAsJson(s, logger);
}

Expand Down
2 changes: 1 addition & 1 deletion Xbim.InformationSpecifications/IO/XIds.Io.json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void SaveAsJson(string destinationFile, ILogger? logger = null)
logger?.LogWarning("File is being overwritten: {file}", f.FullName);
File.Delete(destinationFile);
}
using var s = File.OpenWrite(destinationFile);
using var s = File.Create(destinationFile);
SaveAsJson(s, logger);
}

Expand Down
11 changes: 9 additions & 2 deletions Xbim.InformationSpecifications/IO/Xids.Io.xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Xml;
using System.Xml.Linq;
using Xbim.InformationSpecifications.Cardinality;
Expand Down Expand Up @@ -65,7 +64,13 @@ public enum ExportedFormat
/// <returns>An enum determining if XML or ZIP files were written</returns>
public ExportedFormat ExportBuildingSmartIDS(string destinationFileName, ILogger? logger = null)
{
using FileStream fs = File.OpenWrite(destinationFileName);
if (File.Exists(destinationFileName))
{
var f = new FileInfo(destinationFileName);
logger?.LogInformation("File is being overwritten: {file}", f.FullName);
File.Delete(destinationFileName);
}
using FileStream fs = File.Create(destinationFileName);
return ExportBuildingSmartIDS(fs, logger);
}

Expand All @@ -79,8 +84,10 @@ public ExportedFormat ExportBuildingSmartIDS(Stream destinationStream, ILogger?
{
if (SpecificationsGroups.Count == 1)
{

using XmlWriter writer = XmlWriter.Create(destinationStream, WriteSettings);
ExportBuildingSmartIDS(SpecificationsGroups.First(), writer, logger);
writer.Close();
return ExportedFormat.XML;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<AssemblyName>Xbim.InformationSpecifications</AssemblyName>
<RootNamespace>Xbim.InformationSpecifications</RootNamespace>
<!-- Remember to update the hardcoded AssemblyVersion property in XIDS-->
<AssemblyVersion>1.0.2</AssemblyVersion>
<AssemblyVersion>1.0.3</AssemblyVersion>
<!-- Remember to update the hardcoded AssemblyVersion property in XIDS-->
<FileVersion>$(AssemblyVersion)</FileVersion>
<Version>$(AssemblyVersion)</Version>
Expand Down
2 changes: 1 addition & 1 deletion Xbim.InformationSpecifications/Xids.AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public partial class Xids
/// This is useful for environments that do not allow to load information from the DLL dynamically
/// (e.g. Blazor).
/// </summary>
public static string AssemblyVersion => "1.0.2";
public static string AssemblyVersion => "1.0.3";
}
}

0 comments on commit aa32aea

Please sign in to comment.