From cf3a554b15ff915f02f1d3c0a4a131eab8886e14 Mon Sep 17 00:00:00 2001 From: "Sayan J. Das" Date: Mon, 17 Mar 2025 16:47:42 +0530 Subject: [PATCH] Fix #5809: Add safety check during file.write (#6292) Co-authored-by: theseyan --- src/ifcopenshell-python/ifcopenshell/file.py | 4 ++-- src/ifcwrap/IfcParseWrapper.i | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index 328d98b27f..10193baa76 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -675,6 +675,7 @@ class file: """ path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) + if format == None: format = ifcopenshell.guess_format(path) if format == ".ifcXML": @@ -690,8 +691,7 @@ class file: if format == ".ifcZIP": return self.write(path, ".ifc", zipped=True) self.wrapped_data.write(str(path)) - if not path.exists(): - raise PermissionError(f"Failed to write to '{path}', check folder permissions.") + if zipped: unzipped_path = path.with_suffix(format) path.rename(unzipped_path) diff --git a/src/ifcwrap/IfcParseWrapper.i b/src/ifcwrap/IfcParseWrapper.i index b7af4036bf..2400e12677 100644 --- a/src/ifcwrap/IfcParseWrapper.i +++ b/src/ifcwrap/IfcParseWrapper.i @@ -142,6 +142,9 @@ static IfcUtil::ArgumentType helper_fn_attribute_type(const IfcUtil::IfcBaseClas void write(const std::string& fn) { std::ofstream f(IfcUtil::path::from_utf8(fn).c_str()); + if (!f.good()) { + throw std::runtime_error("Failed to write to path: '" + fn + "', check folder and file permissions."); + } f << (*$self); }