]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / support / FileName.cpp
index 95880a0e79f7cf91e49239f68a9f5aa5ff71fd0d..482bb93e2135dcdeda0d72721ef30d723d5ed9ef 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"
 #include <QFileInfo>
 #include <QList>
 #include <QTemporaryFile>
-#include <QTime>
+#include <QElapsedTimer>
+
+#ifdef _WIN32
+#include <QThread>
+#endif
 
 #include <boost/crc.hpp>
-#include <boost/scoped_array.hpp>
 
 #include <algorithm>
 #include <iterator>
 using namespace std;
 using namespace lyx::support;
 
-// OK, this is ugly, but it is the only workaround I found to compile
-// with gcc (any version) on a system which uses a non-GNU toolchain.
-// The problem is that gcc uses a weak symbol for a particular
-// instantiation and that the system linker usually does not
-// understand those weak symbols (seen on HP-UX, tru64, AIX and
-// others). Thus we force an explicit instanciation of this particular
-// template (JMarc)
-template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
-
 namespace lyx {
 namespace support {
 
@@ -90,18 +84,17 @@ struct FileName::Private
 {
        Private() {}
 
-       Private(string const & abs_filename) : fi(toqstr(abs_filename))
+       Private(string const & abs_filename) : fi(toqstr(handleTildeName(abs_filename)))
        {
                name = fromqstr(fi.absoluteFilePath());
                fi.setCaching(fi.exists() ? true : false);
        }
        ///
-       inline void refresh() 
+       inline void refresh()
        {
                fi.refresh();
        }
 
-
        static
        bool isFilesystemEqual(QString const & lhs, QString const & rhs)
        {
@@ -109,6 +102,14 @@ struct FileName::Private
                        Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
        }
 
+       static
+       string const handleTildeName(string const & name)
+       {
+               return name == "~" ? Package::get_home_dir().absFileName() :
+                       prefixIs(name, "~/") ? Package::get_home_dir().absFileName() + name.substr(1) :
+                       name;
+       }
+
        /// The absolute file name in UTF-8 encoding.
        std::string name;
        ///
@@ -172,7 +173,7 @@ bool FileName::empty() const
 
 bool FileName::isAbsolute(string const & name)
 {
-       QFileInfo fi(toqstr(name));
+       QFileInfo fi(toqstr(Private::handleTildeName(name)));
        return fi.isAbsolute();
 }
 
@@ -191,7 +192,7 @@ string FileName::realPath() const
 
 void FileName::set(string const & name)
 {
-       d->fi.setFile(toqstr(name));
+       d->fi.setFile(toqstr(Private::handleTildeName(name)));
        d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
        LATTEST(empty() || isAbsolute(d->name));
@@ -248,8 +249,9 @@ bool FileName::copyTo(FileName const & name, bool keepsymlink,
 
 bool FileName::renameTo(FileName const & name) const
 {
-       LYXERR(Debug::FILES, "Renaming " << name);
+       LYXERR(Debug::FILES, "Renaming " << name << " as " << *this);
        bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       d->refresh();
        if (!success)
                LYXERR0("Could not rename file " << *this << " to " << name);
        return success;
@@ -258,11 +260,26 @@ bool FileName::renameTo(FileName const & name) const
 
 bool FileName::moveTo(FileName const & name) const
 {
-       LYXERR(Debug::FILES, "Moving " << name);
+       LYXERR(Debug::FILES, "Moving " << *this << " to " << name);
+#ifdef _WIN32
+       // there's a locking problem on Windows sometimes, so
+       // we will keep trying for five seconds, in the hope
+       // that clears.
+       name.refresh();
+       if (name.exists()) {
+               bool removed = name.removeFile();
+               int tries = 1;
+               while (!removed && tries < 6)   {
+                       QThread::sleep(1);
+                       removed = name.removeFile();
+                       tries++;
+               }
+       }
+#else
        QFile::remove(name.d->fi.absoluteFilePath());
+#endif
 
-       bool success = QFile::rename(d->fi.absoluteFilePath(),
-               name.d->fi.absoluteFilePath());
+       bool const success = renameTo(name);
        if (!success)
                LYXERR0("Could not move file " << *this << " to " << name);
        return success;
@@ -281,6 +298,13 @@ bool FileName::changePermission(unsigned long int mode) const
        return true;
 }
 
+bool FileName::clonePermissions(FileName const & source)
+{
+       QFile fin(toqstr(source.absFileName()));
+       QFile f(toqstr(absFileName()));
+
+       return f.setPermissions(fin.permissions());
+}
 
 string FileName::toFilesystemEncoding() const
 {
@@ -319,6 +343,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);
@@ -480,7 +507,11 @@ time_t FileName::lastModified() const
        // been touched between the object creation and now, we refresh the file
        // information.
        d->refresh();
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
+       return d->fi.lastModified().toSecsSinceEpoch();
+#else
        return d->fi.lastModified().toTime_t();
+#endif
 }
 
 
@@ -490,6 +521,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;
@@ -521,7 +558,7 @@ unsigned long FileName::checksum() const
        }
 
        // This is used in the debug output at the end of the method.
-       static QTime t;
+       static QElapsedTimer t;
        if (lyxerr.debugging(Debug::FILES))
                t.restart();
 
@@ -530,7 +567,7 @@ unsigned long FileName::checksum() const
        // FIXME: This code is not ready with Qt4.4.2,
        // see http://www.lyx.org/trac/ticket/5293
        // FIXME: should we check if the MapExtension extension is supported?
-       // see QAbstractFileEngine::supportsExtension() and 
+       // see QAbstractFileEngine::supportsExtension() and
        // QAbstractFileEngine::MapExtension)
        QFile qf(fi.filePath());
        if (!qf.open(QIODevice::ReadOnly))
@@ -587,7 +624,7 @@ unsigned long FileName::checksum() const
 #endif // QT_VERSION
 
        LYXERR(Debug::FILES, "Checksumming \"" << absFileName() << "\" "
-               << result << " lasted " << t.elapsed() << " ms.");
+               << result << " lasted " << QString::number(t.elapsed()) << " ms.");
        return result;
 }
 
@@ -614,12 +651,12 @@ static bool rmdir(QFileInfo const & fi)
                        continue;
                bool removed;
                if (list.at(i).isDir()) {
-                       LYXERR(Debug::FILES, "Removing dir " 
+                       LYXERR(Debug::FILES, "Removing dir "
                                << fromqstr(list.at(i).absoluteFilePath()));
                        removed = rmdir(list.at(i));
                }
                else {
-                       LYXERR(Debug::FILES, "Removing file " 
+                       LYXERR(Debug::FILES, "Removing file "
                                << fromqstr(list.at(i).absoluteFilePath()));
                        removed = dir.remove(list.at(i).fileName());
                }
@@ -628,7 +665,7 @@ static bool rmdir(QFileInfo const & fi)
                        LYXERR0("Could not delete "
                                << fromqstr(list.at(i).absoluteFilePath()));
                }
-       } 
+       }
        QDir parent = fi.absolutePath();
        success &= parent.rmdir(fi.fileName());
        return success;
@@ -910,13 +947,15 @@ string DocFileName::outputFileName(string const & path) const
 
 string DocFileName::mangledFileName(string const & dir) const
 {
-       // FIXME THREAD
        // 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;
@@ -942,7 +981,6 @@ string DocFileName::mangledFileName(string const & dir) const
        // Add the extension back on
        mname = support::changeExtension(mname, getExtension(name));
 
-       // FIXME THREAD
        // Prepend a counter to the filename. This is necessary to make
        // the mangled name unique.
        static int counter = 0;