Fix another data loss issue in PropertyFileIncluded

This commit is contained in:
wmayer 2013-05-06 10:51:25 +02:00
parent 1aff25a62d
commit 95b2a1cd2b
2 changed files with 36 additions and 18 deletions

View File

@ -123,24 +123,28 @@ void PropertyFileIncluded::setValue(const char* sFile, const char* sName)
// if a special name given, use this instead // if a special name given, use this instead
if (sName) { if (sName) {
Base::FileInfo ExtraName(path + "/" + sName); Base::FileInfo fi(pathTrans + "/" + sName);
if (ExtraName.exists() ) { if (fi.exists()) {
// if a file with this name already exists search for a new one // 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; int i=0;
do { do {
i++; i++;
std::stringstream str; std::stringstream str;
str << path << "/" << sName << i; str << dir << "/" << fnp << i;
ExtraName.setFile(str.str()); if (!ext.empty())
str << "." << ext;
fi.setFile(str.str());
} }
while (ExtraName.exists()); while (fi.exists());
_cValue = ExtraName.filePath();
_BaseFileName = ExtraName.fileName();
_cValue = fi.filePath();
_BaseFileName = fi.fileName();
} }
else { else {
_cValue = path + "/" + sName; _cValue = pathTrans + "/" + sName;
_BaseFileName = 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 // if file already exists in transient dir make a new unique name
Base::FileInfo fi(_cValue); Base::FileInfo fi(_cValue);
if (fi.exists()) { if (fi.exists()) {
Base::FileInfo fi2(Base::FileInfo::getTempFileName()); // if a file with this name already exists search for a new one
std::stringstream str; std::string dir = fi.dirPath();
str << fi.dirPath() << "/" << fi2.fileNamePure(); std::string fnp = fi.fileNamePure();
std::string ext = fi.extension(false); std::string ext = fi.extension(false);
if (!ext.empty()) int i=0;
str << "." << ext; do {
Base::FileInfo fi3(str.str()); i++;
_cValue = fi3.filePath(); std::stringstream str;
_BaseFileName = fi3.fileName(); 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()); bool done = file.copyTo(_cValue.c_str());
@ -256,7 +268,6 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
setValue(fileStr.c_str(),nameStr.c_str()); setValue(fileStr.c_str(),nameStr.c_str());
return; return;
} }
else { else {
std::string error = std::string("Type must be string or file"); std::string error = std::string("Type must be string or file");

View File

@ -717,10 +717,17 @@ class DocumentFileIncludeCases(unittest.TestCase):
# the test is about to put the file to the correct transient dir # the test is about to put the file to the correct transient dir
doc2 = FreeCAD.newDocument("Doc2") doc2 = FreeCAD.newDocument("Doc2")
L4 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject") L4 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject")
L5 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject")
L6 = doc2.addObject("App::DocumentObjectFileIncluded","FileObject")
L4.File = (L3.File,"Test.txt") L4.File = (L3.File,"Test.txt")
L5.File = L3.File
L6.File = L3.File
FreeCAD.closeDocument("FileIncludeTests") FreeCAD.closeDocument("FileIncludeTests")
self.Doc = FreeCAD.open(self.TempPath+"/FileIncludeTests.fcstd") self.Doc = FreeCAD.open(self.TempPath+"/FileIncludeTests.fcstd")
self.failUnless(os.path.exists(L4.File)) 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") FreeCAD.closeDocument("Doc2")