[Pkg-owncloud-commits] [owncloud-client] 54/164: lnk files: Fix exists() calls. #2792

Sandro Knauß hefee-guest at moszumanska.debian.org
Sun Mar 22 11:56:52 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit f88398e776a0234ed86aa8c46dd8d0c9c0d1794a
Author: Christian Kamm <kamm at incasoftware.de>
Date:   Wed Feb 25 10:51:05 2015 +0100

    lnk files: Fix exists() calls. #2792
---
 src/gui/folder.cpp                |  5 ++-
 src/gui/socketapi.cpp             |  2 +-
 src/libsync/filesystem.cpp        | 88 +++++++++++++++++++++++++++++++++------
 src/libsync/filesystem.h          | 15 ++++++-
 src/libsync/propagatedownload.cpp |  4 +-
 src/libsync/propagateupload.cpp   |  8 ++--
 src/libsync/propagatorjobs.cpp    |  3 +-
 7 files changed, 101 insertions(+), 24 deletions(-)

diff --git a/src/gui/folder.cpp b/src/gui/folder.cpp
index 8acb959..25fdeb8 100644
--- a/src/gui/folder.cpp
+++ b/src/gui/folder.cpp
@@ -29,6 +29,7 @@
 #include "syncengine.h"
 #include "syncrunfilelog.h"
 #include "theme.h"
+#include "filesystem.h"
 
 
 #include "creds/abstractcredentials.h"
@@ -150,7 +151,7 @@ void Folder::checkLocalPath()
     if( fi.isDir() && fi.isReadable() ) {
         qDebug() << "Checked local path ok";
     } else {
-        if( !fi.exists() ) {
+        if( !FileSystem::fileExists(fi) ) {
             // try to create the local dir
             QDir d(_path);
             if( d.mkpath(_path) ) {
@@ -158,7 +159,7 @@ void Folder::checkLocalPath()
             }
         }
         // Check directory again
-        if( !fi.exists() ) {
+        if( !FileSystem::fileExists(fi) ) {
             _syncResult.setErrorString(tr("Local folder %1 does not exist.").arg(_path));
             _syncResult.setStatus( SyncResult::SetupError );
         } else if( !fi.isDir() ) {
diff --git a/src/gui/socketapi.cpp b/src/gui/socketapi.cpp
index d1dc3b4..6f577ce 100644
--- a/src/gui/socketapi.cpp
+++ b/src/gui/socketapi.cpp
@@ -530,7 +530,7 @@ SyncFileStatus SocketApi::fileStatus(Folder *folder, const QString& systemFileNa
 
     QFileInfo fi(file);
 
-    if( !fi.exists() ) {
+    if( !FileSystem::fileExists(fi) ) {
         qDebug() << "OO File " << file << " is not existing";
         return SyncFileStatus(SyncFileStatus::STATUS_STAT_ERROR);
     }
diff --git a/src/libsync/filesystem.cpp b/src/libsync/filesystem.cpp
index b59f687..dcc1209 100644
--- a/src/libsync/filesystem.cpp
+++ b/src/libsync/filesystem.cpp
@@ -120,7 +120,7 @@ bool FileSystem::renameReplace(const QString& originFileName, const QString& des
     // Qt 5.1 has QSaveFile::renameOverwrite we cold use.
     // ### FIXME
     success = true;
-    bool destExists = QFileInfo::exists(destinationFileName);
+    bool destExists = fileExists(destinationFileName);
     if( destExists && !QFile::remove(destinationFileName) ) {
         *errorString = orig.errorString();
         qDebug() << Q_FUNC_INFO << "Target file could not be removed.";
@@ -214,26 +214,90 @@ bool FileSystem::openFileSharedRead(QFile* file, QString* error)
     return ok;
 }
 
+#ifdef Q_OS_WIN
+static bool isLnkFile(const QString& filename)
+{
+    return filename.endsWith(".lnk");
+}
+
+static bool isLnkFile(const QFileInfo& fi)
+{
+    return fi.suffix() == "lnk";
+}
+
+static qint64 getSizeWithCsync(const QString& filename)
+{
+    qint64 result = 0;
+    csync_vio_file_stat_t* stat = csync_vio_file_stat_new();
+    if (csync_vio_local_stat(filename.toUtf8().data(), stat) != -1
+            && (stat->fields & CSYNC_VIO_FILE_STAT_FIELDS_SIZE)) {
+        result = stat->size;
+    } else {
+        qDebug() << "Could not get size time for" << filename << "with csync";
+    }
+    csync_vio_file_stat_destroy(stat);
+    return result;
+}
+#endif
+
 qint64 FileSystem::getSize(const QString& filename)
 {
 #ifdef Q_OS_WIN
-    if (filename.endsWith(".lnk")) {
+    if (isLnkFile(filename)) {
         // Use csync to get the file size. Qt seems unable to get at it.
-        qint64 result = 0;
-        csync_vio_file_stat_t* stat = csync_vio_file_stat_new();
-        if (csync_vio_local_stat(filename.toUtf8().data(), stat) != -1
-                && (stat->fields & CSYNC_VIO_FILE_STAT_FIELDS_SIZE)) {
-            result = stat->size;
-        } else {
-            qDebug() << "Could not get size time for" << filename << "with csync";
-        }
-        csync_vio_file_stat_destroy(stat);
-        return result;
+        return getSizeWithCsync(filename);
     }
 #endif
     return QFileInfo(filename).size();
 }
 
+qint64 FileSystem::getSize(const QFileInfo& fi)
+{
+#ifdef Q_OS_WIN
+    if (isLnkFile(fi)) {
+        // Use csync to get the file size. Qt seems unable to get at it.
+        return getSizeWithCsync(fi.absoluteFilePath());
+    }
+#endif
+    return fi.size();
+}
+
+#ifdef Q_OS_WIN
+static bool fileExistsWin(const QString& filename)
+{
+    WIN32_FIND_DATA FindFileData;
+    HANDLE hFind;
+    hFind = FindFirstFileW( (wchar_t*)filename.utf16(), &FindFileData);
+    if (hFind == INVALID_HANDLE_VALUE) {
+        return false;
+    }
+    FindClose(hFind);
+    return true;
+}
+#endif
+
+bool FileSystem::fileExists(const QString& filename)
+{
+#ifdef Q_OS_WIN
+    if (isLnkFile(filename)) {
+        // Use a native check.
+        return fileExistsWin(filename);
+    }
+#endif
+    return QFileInfo::exists(filename);
+}
+
+bool FileSystem::fileExists(const QFileInfo& fi)
+{
+#ifdef Q_OS_WIN
+    if (isLnkFile(fi)) {
+        // Use a native check.
+        return fileExistsWin(fi.absoluteFilePath());
+    }
+#endif
+    return fi.exists();
+}
+
 #ifdef Q_OS_WIN
 QString FileSystem::fileSystemForPath(const QString & path)
 {
diff --git a/src/libsync/filesystem.h b/src/libsync/filesystem.h
index cd89e38..ec72e48 100644
--- a/src/libsync/filesystem.h
+++ b/src/libsync/filesystem.h
@@ -14,11 +14,13 @@
 #pragma once
 
 #include <QString>
-#include <QFile>
 #include <ctime>
 
 #include <owncloudlib.h>
 
+class QFile;
+class QFileInfo;
+
 namespace OCC {
 
 /**
@@ -39,7 +41,7 @@ void OWNCLOUDSYNC_EXPORT setFileHidden(const QString& filename, bool hidden);
  * Use this over QFileInfo::lastModified() to avoid timezone related bugs. See
  * owncloud/core#9781 for details.
  */
-time_t OWNCLOUDSYNC_EXPORT getModTime(const QString &filename);
+time_t OWNCLOUDSYNC_EXPORT getModTime(const QString& filename);
 
 bool setModTime(const QString &filename, time_t modTime);
 
@@ -49,6 +51,15 @@ bool setModTime(const QString &filename, time_t modTime);
  * See https://bugreports.qt.io/browse/QTBUG-24831.
  */
 qint64 OWNCLOUDSYNC_EXPORT getSize(const QString& filename);
+qint64 OWNCLOUDSYNC_EXPORT getSize(const QFileInfo& fi);
+
+/** Checks whether a file exists.
+ *
+ * Use this over QFileInfo::exists() and QFile::exists() to avoid bugs with lnk
+ * files, see above.
+ */
+bool OWNCLOUDSYNC_EXPORT fileExists(const QString& filename);
+bool OWNCLOUDSYNC_EXPORT fileExists(const QFileInfo& fi);
 
 /**
  * Rename the file \a originFileName to \a destinationFileName, and overwrite the destination if it
diff --git a/src/libsync/propagatedownload.cpp b/src/libsync/propagatedownload.cpp
index 6e3f559..82923e0 100644
--- a/src/libsync/propagatedownload.cpp
+++ b/src/libsync/propagatedownload.cpp
@@ -491,7 +491,7 @@ void PropagateDownloadFileQNAM::downloadFinished()
     }
 
     QFileInfo existingFile(fn);
-    if(existingFile.exists() && existingFile.permissions() != _tmpFile.permissions()) {
+    if(FileSystem::fileExists(existingFile) && existingFile.permissions() != _tmpFile.permissions()) {
         _tmpFile.setPermissions(existingFile.permissions());
     }
 
@@ -520,7 +520,7 @@ void PropagateDownloadFileQNAM::downloadFinished()
     // Maybe we downloaded a newer version of the file than we thought we would...
     // Get up to date information for the journal.
     FileSystem::setModTime(fn, _item._modtime);
-    _item._size = FileSystem::getSize(fn);
+    _item._size = FileSystem::getSize(existingFile);
 
     _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));
     _propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());
diff --git a/src/libsync/propagateupload.cpp b/src/libsync/propagateupload.cpp
index 65a9ef2..f3a6c0d 100644
--- a/src/libsync/propagateupload.cpp
+++ b/src/libsync/propagateupload.cpp
@@ -152,14 +152,14 @@ void PropagateUploadFileQNAM::start()
         return;
 
     QFileInfo fi(_propagator->getFilePath(_item._file));
-    if (!fi.exists()) {
+    if (!FileSystem::fileExists(fi)) {
         done(SyncFileItem::SoftError, tr("File Removed"));
         return;
     }
 
     // Update the mtime and size, it might have changed since discovery.
     _item._modtime = FileSystem::getModTime(fi.absoluteFilePath());
-    quint64 fileSize = FileSystem::getSize(fi.absoluteFilePath());
+    quint64 fileSize = FileSystem::getSize(fi);
     _item._size = fileSize;
 
     // But skip the file if the mtime is too close to 'now'!
@@ -509,7 +509,7 @@ void PropagateUploadFileQNAM::slotPutFinished()
     QFileInfo fi(_propagator->getFilePath(_item._file));
 
     // Check if the file still exists
-    if( !fi.exists() ) {
+    if( !FileSystem::fileExists(fi) ) {
         if (!finished) {
             abortWithError(SyncFileItem::SoftError, tr("The local file was removed during sync."));
             return;
@@ -520,7 +520,7 @@ void PropagateUploadFileQNAM::slotPutFinished()
 
     // compare expected and real modification time of the file and size
     const time_t new_mtime = FileSystem::getModTime(fi.absoluteFilePath());
-    const quint64 new_size = static_cast<quint64>(FileSystem::getSize(fi.absoluteFilePath()));
+    const quint64 new_size = static_cast<quint64>(FileSystem::getSize(fi));
     if (new_mtime != _item._modtime || new_size != _item._size) {
         qDebug() << "The local file has changed during upload:"
                  << "mtime: " << _item._modtime << "<->" << new_mtime
diff --git a/src/libsync/propagatorjobs.cpp b/src/libsync/propagatorjobs.cpp
index 2483f1a..38c536a 100644
--- a/src/libsync/propagatorjobs.cpp
+++ b/src/libsync/propagatorjobs.cpp
@@ -19,6 +19,7 @@
 #include "utility.h"
 #include "syncjournaldb.h"
 #include "syncjournalfilerecord.h"
+#include "filesystem.h"
 #include <qfile.h>
 #include <qdir.h>
 #include <qdiriterator.h>
@@ -95,7 +96,7 @@ void PropagateLocalRemove::start()
         }
     } else {
         QFile file(filename);
-        if (file.exists() && !file.remove()) {
+        if (FileSystem::fileExists(file) && !file.remove()) {
             done(SyncFileItem::NormalError, file.errorString());
             return;
         }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list