X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2FFileName.cpp;h=929c4165bebd541bead6ce6e7a6a61774e12f89d;hb=8d640dc77608bedddb5b00982c23665584f52d21;hp=f0489309d549ae44a61df42c888225eb23eadfe5;hpb=762c4eca5734b4d504ab28308a10d725a4790d5c;p=lyx.git diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index f0489309d5..929c4165be 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -17,7 +17,7 @@ #include "support/filetools.h" #include "support/lassert.h" #include "support/lstrings.h" -#include "support/qstring_helpers.h" +#include "support/mutex.h" #include "support/os.h" #include "support/Package.h" #include "support/qstring_helpers.h" @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -217,9 +216,26 @@ void FileName::erase() } -bool FileName::copyTo(FileName const & name) const +bool FileName::copyTo(FileName const & name, bool keepsymlink) const { - LYXERR(Debug::FILES, "Copying " << name); + FileNameSet visited; + return copyTo(name, keepsymlink, visited); +} + + +bool FileName::copyTo(FileName const & name, bool keepsymlink, + FileName::FileNameSet & visited) const +{ + LYXERR(Debug::FILES, "Copying " << name << " keep symlink: " << keepsymlink); + if (keepsymlink && name.isSymLink()) { + visited.insert(*this); + FileName const target(fromqstr(name.d->fi.symLinkTarget())); + if (visited.find(target) != visited.end()) { + LYXERR(Debug::FILES, "Found circular symlink: " << target); + return false; + } + return copyTo(target, true); + } QFile::remove(name.d->fi.absoluteFilePath()); bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath()); if (!success) @@ -231,6 +247,7 @@ bool FileName::copyTo(FileName const & name) const bool FileName::renameTo(FileName const & name) const { + LYXERR(Debug::FILES, "Renaming " << name << " as " << *this); bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath()); if (!success) LYXERR0("Could not rename file " << *this << " to " << name); @@ -240,6 +257,7 @@ bool FileName::renameTo(FileName const & name) const bool FileName::moveTo(FileName const & name) const { + LYXERR(Debug::FILES, "Moving " << name << " to " << *this); QFile::remove(name.d->fi.absoluteFilePath()); bool success = QFile::rename(d->fi.absoluteFilePath(), @@ -300,6 +318,9 @@ bool FileName::isSymLink() const } +//QFileInfo caching info might fool this test if file was changed meanwhile. +//refresh() helps, but we don't want to put it blindly here, because it might +//trigger slowdown on networked file systems. bool FileName::isFileEmpty() const { LASSERT(!empty(), return true); @@ -435,40 +456,6 @@ FileNameList FileName::dirList(string const & ext) const } -static string createTempFile(QString const & mask) -{ - // FIXME: This is not safe. QTemporaryFile creates a file in open(), - // but the file is deleted when qt_tmp goes out of scope. - // Therefore the next call to createTempFile() may create the - // same file again. To make this safe the QTemporaryFile object - // needs to be kept for the whole life time of the temp file name. - // This can be achieved by using the TempFile class. - QTemporaryFile qt_tmp(mask + ".XXXXXXXXXXXX"); - if (qt_tmp.open()) { - string const temp_file = fromqstr(qt_tmp.fileName()); - LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created."); - return temp_file; - } - LYXERR(Debug::FILES, "Unable to create temporary file with following template: " - << qt_tmp.fileTemplate()); - return string(); -} - - -FileName FileName::tempName(FileName const & temp_dir, string const & mask) -{ - QFileInfo tmp_fi(QDir(temp_dir.d->fi.absoluteFilePath()), toqstr(mask)); - LYXERR(Debug::FILES, "Temporary file in " << tmp_fi.absoluteFilePath()); - return FileName(createTempFile(tmp_fi.absoluteFilePath())); -} - - -FileName FileName::tempName(string const & mask) -{ - return tempName(package().temp_dir(), mask); -} - - FileName FileName::getcwd() { // return makeAbsPath("."); would create an infinite loop @@ -505,6 +492,12 @@ bool FileName::chdir() const } +bool FileName::link(FileName const & name) const +{ + return QFile::link(toqstr(absFileName()), toqstr(name.absFileName())); +} + + unsigned long checksum_ifstream_fallback(char const * file) { unsigned long result = 0; @@ -925,10 +918,15 @@ string DocFileName::outputFileName(string const & path) const string DocFileName::mangledFileName(string const & dir) const { + // Concurrent access to these variables is possible. + // We need to make sure that every DocFileName instance for a given // filename returns the same mangled name. typedef map MangledMap; static MangledMap mangledNames; + static Mutex mangledMutex; + // this locks both access to mangledNames and counter below + Mutex::Locker lock(&mangledMutex); MangledMap::const_iterator const it = mangledNames.find(absFileName()); if (it != mangledNames.end()) return (*it).second;