From 95b2a1cd2b87ae44da7bb5310509aa1c0242b644 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 6 May 2013 10:51:25 +0200 Subject: [PATCH] Fix another data loss issue in PropertyFileIncluded --- src/App/PropertyFile.cpp | 47 +++++++++++++++++++++++++--------------- src/Mod/Test/Document.py | 7 ++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/App/PropertyFile.cpp b/src/App/PropertyFile.cpp index d2168498e..77f5953ba 100644 --- a/src/App/PropertyFile.cpp +++ b/src/App/PropertyFile.cpp @@ -123,24 +123,28 @@ void PropertyFileIncluded::setValue(const char* sFile, const char* sName) // if a special name given, use this instead if (sName) { - Base::FileInfo ExtraName(path + "/" + sName); - if (ExtraName.exists() ) { + Base::FileInfo fi(pathTrans + "/" + sName); + if (fi.exists()) { // if a file with this name already exists search for a new one + std::string dir = pathTrans; + std::string fnp = fi.fileNamePure(); + std::string ext = fi.extension(false); int i=0; - do { i++; std::stringstream str; - str << path << "/" << sName << i; - ExtraName.setFile(str.str()); + str << dir << "/" << fnp << i; + if (!ext.empty()) + str << "." << ext; + fi.setFile(str.str()); } - while (ExtraName.exists()); - _cValue = ExtraName.filePath(); - _BaseFileName = ExtraName.fileName(); + while (fi.exists()); + _cValue = fi.filePath(); + _BaseFileName = fi.fileName(); } else { - _cValue = path + "/" + sName; + _cValue = pathTrans + "/" + sName; _BaseFileName = sName; } } @@ -163,15 +167,23 @@ void PropertyFileIncluded::setValue(const char* sFile, const char* sName) // if file already exists in transient dir make a new unique name Base::FileInfo fi(_cValue); if (fi.exists()) { - Base::FileInfo fi2(Base::FileInfo::getTempFileName()); - std::stringstream str; - str << fi.dirPath() << "/" << fi2.fileNamePure(); + // if a file with this name already exists search for a new one + std::string dir = fi.dirPath(); + std::string fnp = fi.fileNamePure(); std::string ext = fi.extension(false); - if (!ext.empty()) - str << "." << ext; - Base::FileInfo fi3(str.str()); - _cValue = fi3.filePath(); - _BaseFileName = fi3.fileName(); + int i=0; + do { + i++; + std::stringstream str; + str << dir << "/" << fnp << i; + if (!ext.empty()) + str << "." << ext; + fi.setFile(str.str()); + } + while (fi.exists()); + + _cValue = fi.filePath(); + _BaseFileName = fi.fileName(); } bool done = file.copyTo(_cValue.c_str()); @@ -256,7 +268,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value) setValue(fileStr.c_str(),nameStr.c_str()); return; - } else { std::string error = std::string("Type must be string or file"); diff --git a/src/Mod/Test/Document.py b/src/Mod/Test/Document.py index 914433f82..9618f5c96 100644 --- a/src/Mod/Test/Document.py +++ b/src/Mod/Test/Document.py @@ -717,10 +717,17 @@ class DocumentFileIncludeCases(unittest.TestCase): # the test is about to put the file to the correct transient dir doc2 = FreeCAD.newDocument("Doc2") L4 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject") + L5 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject") + L6 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject") L4.File = (L3.File,"Test.txt") + L5.File = L3.File + L6.File = L3.File FreeCAD.closeDocument("FileIncludeTests") self.Doc = FreeCAD.open(self.TempPath+"/FileIncludeTests.fcstd") self.failUnless(os.path.exists(L4.File)) + self.failUnless(os.path.exists(L5.File)) + self.failUnless(os.path.exists(L6.File)) + self.failUnless(L5.File != L6.File) FreeCAD.closeDocument("Doc2")