]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Initialize bool member of Package in default constructor
[lyx.git] / src / support / FileName.cpp
index 8a1176d60f17af7e9e3a10067223bcabf04a1920..929c4165bebd541bead6ce6e7a6a61774e12f89d 100644 (file)
@@ -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 <QTime>
 
 #include <boost/crc.hpp>
-#include <boost/scoped_array.hpp>
 
 #include <algorithm>
 #include <iterator>
@@ -98,13 +97,7 @@ struct FileName::Private
        ///
        inline void refresh() 
        {
-// There seems to be a bug in Qt >= 4.2.0 and < 4.5.0, that causes problems with
-// QFileInfo::refresh() on *nix. So we recreate the object in that case.
-#if defined(_WIN32) || (QT_VERSION >= 0x040500)
                fi.refresh();
-#else
-               fi = QFileInfo(fi.absoluteFilePath());
-#endif
        }
 
 
@@ -137,7 +130,7 @@ FileName::FileName(string const & abs_filename)
        : d(abs_filename.empty() ? new Private : new Private(abs_filename))
 {
        //LYXERR(Debug::FILES, "FileName(" << abs_filename << ')');
-       LASSERT(empty() || isAbsolute(d->name), /**/);
+       LATTEST(empty() || isAbsolute(d->name));
 }
 
 
@@ -200,7 +193,7 @@ void FileName::set(string const & name)
        d->fi.setFile(toqstr(name));
        d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
-       LASSERT(empty() || isAbsolute(d->name), /**/);
+       LATTEST(empty() || isAbsolute(d->name));
 }
 
 
@@ -212,7 +205,7 @@ void FileName::set(FileName const & rhs, string const & suffix)
                d->fi.setFile(QDir(rhs.d->fi.absoluteFilePath()), toqstr(suffix));
        d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << d->name << ')');
-       LASSERT(empty() || isAbsolute(d->name), /**/);
+       LATTEST(empty() || isAbsolute(d->name));
 }
 
 
@@ -223,9 +216,26 @@ void FileName::erase()
 }
 
 
-bool FileName::copyTo(FileName const & name) const
+bool FileName::copyTo(FileName const & name, bool keepsymlink) const
+{
+       FileNameSet visited;
+       return copyTo(name, keepsymlink, visited);
+}
+
+
+bool FileName::copyTo(FileName const & name, bool keepsymlink,
+                      FileName::FileNameSet & visited) const
 {
-       LYXERR(Debug::FILES, "Copying " << name);
+       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)
@@ -237,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);
@@ -246,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(),
@@ -306,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);
@@ -441,34 +456,6 @@ FileNameList FileName::dirList(string const & ext) const
 }
 
 
-static string createTempFile(QString const & mask)
-{
-       QTemporaryFile qt_tmp(mask);
-       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;
@@ -752,7 +745,11 @@ docstring FileName::fileContents(string const & encoding) const
        if (encoding.empty() || encoding == "UTF-8")
                s = QString::fromUtf8(contents.data());
        else if (encoding == "ascii")
+#if (QT_VERSION < 0x050000)
                s = QString::fromAscii(contents.data());
+#else
+               s = QString::fromLatin1(contents.data());
+#endif
        else if (encoding == "local8bit")
                s = QString::fromLocal8Bit(contents.data());
        else if (encoding == "latin1")
@@ -921,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<string, string> 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;