From c8d06d0176fb38229094cb75793c14869a6a5ba0 Mon Sep 17 00:00:00 2001
From: mahach666 <79989386+mahach666@users.noreply.github.com>
Date: Sat, 28 Nov 2026 17:21:02 +0300
Subject: [PATCH 1/2] Write the output file name into the header of IFC files
from tdbio
WriteIfc puts the name of the path it is given into FILE_NAME, and tdbio
gave it a temporary path. The temporary file now has the name of the output
and is in a temporary folder next to it. A missing output folder is still an
input error: it is checked first, so that creating the temporary folder does
not create it.
---
src/TdbIO.Cli/Cli.cs | 15 ++++++++++-----
tests/TdbIO.Tests/CliTests.cs | 27 +++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/src/TdbIO.Cli/Cli.cs b/src/TdbIO.Cli/Cli.cs
index 76bce10..e2481e1 100644
--- a/src/TdbIO.Cli/Cli.cs
+++ b/src/TdbIO.Cli/Cli.cs
@@ -535,7 +535,15 @@ private static void WriteFileAtomically(string inputPath, string outputPath, Act
{
dir = ".";
}
- string tempPath = Path.Combine(dir, $".{Path.GetFileName(fullPath)}.{Guid.NewGuid():N}.tmp");
+ // Checked here, because creating the temporary folder would also create the missing ones above it.
+ if (!Directory.Exists(dir))
+ {
+ throw new DirectoryNotFoundException($"The output folder '{dir}' does not exist.");
+ }
+ // The temporary file has the output's own name in a folder of its own: IFC writes that name into its header.
+ string tempFolder = Path.Combine(dir, $".{Guid.NewGuid():N}.tmp");
+ string tempPath = Path.Combine(tempFolder, Path.GetFileName(fullPath));
+ Directory.CreateDirectory(tempFolder);
try
{
writeAction(tempPath);
@@ -547,10 +555,7 @@ private static void WriteFileAtomically(string inputPath, string outputPath, Act
}
finally
{
- if (File.Exists(tempPath))
- {
- try { File.Delete(tempPath); } catch { /* ignore temp cleanup error */ }
- }
+ try { Directory.Delete(tempFolder, recursive: true); } catch { /* ignore temp cleanup error */ }
}
}
diff --git a/tests/TdbIO.Tests/CliTests.cs b/tests/TdbIO.Tests/CliTests.cs
index 98a7e4f..bbc8649 100644
--- a/tests/TdbIO.Tests/CliTests.cs
+++ b/tests/TdbIO.Tests/CliTests.cs
@@ -468,6 +468,33 @@ public void Export_ToIfcHtmlAndSceneJson_Succeeds()
Assert.StartsWith("ISO-10303-21;", stdoutIfc);
}
+ [Fact]
+ public void Export_IfcHeaderNamesTheOutputFile()
+ {
+ string input = File("header.db1", Gzip(Db1TablesPayload(Db1Section(null, 4, [1], (0x04, Counting(4), 1, 2)))));
+ string folder = Directory.CreateDirectory(Path.Combine(_folder, "out")).FullName;
+ string output = Path.Combine(folder, "building.ifc");
+
+ var (exit, _, err) = Run("export", input, output);
+ Assert.Equal(Cli.ExitOk, exit);
+ Assert.Empty(err);
+ Assert.Contains("FILE_NAME('building.ifc',", System.IO.File.ReadAllText(output));
+ // Nothing of the temporary write stays next to the output.
+ Assert.Equal(output, Assert.Single(Directory.GetFileSystemEntries(folder)));
+ }
+
+ [Fact]
+ public void Export_IntoAMissingFolderIsAnInputErrorAndCreatesNothing()
+ {
+ string input = File("header.db1", Gzip(Db1TablesPayload(Db1Section(null, 4, [1], (0x04, Counting(4), 1, 2)))));
+ string folder = Path.Combine(_folder, "missing");
+
+ var (exit, _, err) = Run("export", input, Path.Combine(folder, "building.ifc"));
+ Assert.Equal(Cli.ExitInput, exit);
+ Assert.Contains(folder, err);
+ Assert.False(Directory.Exists(folder));
+ }
+
[Fact]
public void Export_RejectsOutputPathIdenticalToInputFile()
{
From 9236d7f954e6fcb47e58c079e7c1e5663fc777c7 Mon Sep 17 00:00:00 2001
From: mahach666 <79989386+mahach666@users.noreply.github.com>
Date: Sat, 28 Nov 2026 18:29:49 +0300
Subject: [PATCH 2/2] Keep the owner's rights on the temporary folder of an
export
The new folder follows the umask, which can leave its owner unable to create
a file in it, as umask 177 does on Linux and macOS, although a file next to
the output can still be created. When the owner lacks the read, write or
execute right on the folder, tdbio now adds all three.
Add a test of an export under that umask, which does nothing on Windows, and
a test that an export which cannot replace its output leaves nothing behind.
---
src/TdbIO.Cli/Cli.cs | 7 ++++
tests/TdbIO.Tests/CliTests.cs | 15 +++++++++
tests/TdbIO.Tests/CliUmaskTests.cs | 53 ++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+)
create mode 100644 tests/TdbIO.Tests/CliUmaskTests.cs
diff --git a/src/TdbIO.Cli/Cli.cs b/src/TdbIO.Cli/Cli.cs
index e2481e1..04a9f1d 100644
--- a/src/TdbIO.Cli/Cli.cs
+++ b/src/TdbIO.Cli/Cli.cs
@@ -546,6 +546,13 @@ private static void WriteFileAtomically(string inputPath, string outputPath, Act
Directory.CreateDirectory(tempFolder);
try
{
+ // The umask applies to the new folder and can leave even its owner unable to create a file in it.
+ if (!OperatingSystem.IsWindows())
+ {
+ const UnixFileMode ownerRights = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute;
+ UnixFileMode mode = File.GetUnixFileMode(tempFolder);
+ if ((mode & ownerRights) != ownerRights) File.SetUnixFileMode(tempFolder, mode | ownerRights);
+ }
writeAction(tempPath);
if (AreSameFile(inputPath, outputPath))
{
diff --git a/tests/TdbIO.Tests/CliTests.cs b/tests/TdbIO.Tests/CliTests.cs
index bbc8649..7e23af7 100644
--- a/tests/TdbIO.Tests/CliTests.cs
+++ b/tests/TdbIO.Tests/CliTests.cs
@@ -495,6 +495,21 @@ public void Export_IntoAMissingFolderIsAnInputErrorAndCreatesNothing()
Assert.False(Directory.Exists(folder));
}
+ [Fact]
+ public void Export_ThatCannotReplaceItsOutputLeavesNothingBehind()
+ {
+ string input = File("header.db1", Gzip(Db1TablesPayload(Db1Section(null, 4, [1], (0x04, Counting(4), 1, 2)))));
+ string folder = Directory.CreateDirectory(Path.Combine(_folder, "out")).FullName;
+ // A folder stands where the output goes: the temporary file is written and then cannot be moved there.
+ string output = Directory.CreateDirectory(Path.Combine(folder, "taken.ifc")).FullName;
+
+ var (exit, _, err) = Run("export", input, output);
+ Assert.Equal(Cli.ExitInput, exit);
+ Assert.NotEmpty(err);
+ Assert.Equal(output, Assert.Single(Directory.GetFileSystemEntries(folder)));
+ Assert.Empty(Directory.GetFileSystemEntries(output));
+ }
+
[Fact]
public void Export_RejectsOutputPathIdenticalToInputFile()
{
diff --git a/tests/TdbIO.Tests/CliUmaskTests.cs b/tests/TdbIO.Tests/CliUmaskTests.cs
new file mode 100644
index 0000000..1262ee3
--- /dev/null
+++ b/tests/TdbIO.Tests/CliUmaskTests.cs
@@ -0,0 +1,53 @@
+using System.Runtime.InteropServices;
+using TdbIO.CommandLine;
+using Xunit;
+using static TdbIO.Tests.Synthetic;
+
+namespace TdbIO.Tests;
+
+/// For tests that change the umask. It belongs to the whole process, so no other test runs beside them.
+[CollectionDefinition(nameof(ProcessUmask), DisableParallelization = true)]
+public sealed class ProcessUmask
+{
+}
+
+/// The tdbio command line under a umask that restricts what it creates.
+[Collection(nameof(ProcessUmask))]
+public sealed class CliUmaskTests
+{
+ [DllImport("libc", EntryPoint = "umask")]
+ private static extern int SetUmask(int mask);
+
+ [Fact]
+ public void Export_WritesTheFileUnderAUmaskThatClosesNewFoldersToTheirOwner()
+ {
+ if (OperatingSystem.IsWindows()) return; // Windows has no umask.
+ string root = Directory.CreateTempSubdirectory("tdbio-umask-").FullName;
+ try
+ {
+ string input = Path.Combine(root, "header.db1");
+ File.WriteAllBytes(input, Gzip(Db1TablesPayload(Db1Section(null, 4, [1], (0x04, Counting(4), 1, 2)))));
+ string folder = Directory.CreateDirectory(Path.Combine(root, "out")).FullName;
+ string output = Path.Combine(folder, "building.ifc");
+ var stderr = new StringWriter();
+
+ int previous = SetUmask(Convert.ToInt32("177", 8));
+ int exit;
+ try
+ {
+ exit = Cli.Run(["export", input, output], new StringWriter(), stderr);
+ }
+ finally
+ {
+ SetUmask(previous);
+ }
+
+ Assert.True(exit == Cli.ExitOk, stderr.ToString());
+ Assert.Equal(output, Assert.Single(Directory.GetFileSystemEntries(folder)));
+ }
+ finally
+ {
+ Directory.Delete(root, recursive: true);
+ }
+ }
+}