Commit 1b464d30 authored by Harald Sitter's avatar Harald Sitter
Browse files

stop mtime duplication

applying the mtime was duplicated across multiple functions when indeed
it is largely the same code with only the actual setting being slightly
variable. use a template function with callback instead to share the
sharable bits.

also, since smbc_utime is used in two functions move that to its own
helper built on top to remove even the callback duplication
parent 82cf40ac
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2000 Caldera Systems Inc.
SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
SPDX-FileContributor: Matthew Peterson <mpeterson@caldera.com>
*/
......@@ -35,6 +36,7 @@
// Qt includes
//-----------------------------
#include <QDateTime>
#include <QLoggingCategory>
#include <QObject>
#include <QUrl>
......@@ -248,6 +250,40 @@ private:
const bool m_enableEEXISTWorkaround; /* Enables a workaround for some broken libsmbclient versions */
// Close without calling finish(). Use this to close after error.
void closeWithoutFinish();
// Apply mtime if modified metadata is set. This callsback with a utbuf
// with modtime accordingly set. The callback should implement the actual apply.
template<typename UTimeFunction>
void applyMTime(UTimeFunction &&callback)
{
const QString mtimeStr = metaData("modified");
if (mtimeStr.isEmpty()) {
return;
}
qCDebug(KIO_SMB_LOG) << "modified:" << mtimeStr;
const QDateTime dateTime = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dateTime.isValid()) {
struct utimbuf utbuf {
};
utbuf.modtime = dateTime.toSecsSinceEpoch(); // modification time
callback(utbuf);
}
}
void applyMTimeSMBC(const SMBUrl &url)
{
#ifdef HAVE_UTIME_H // smbc_utime is conditional inside the libsmb headers
applyMTime([url](struct utimbuf utbuf) {
struct stat st {
};
if (cache_stat(url, &st) == 0) {
utbuf.actime = st.st_atime; // access time, unchanged
smbc_utime(url.toSmbcUrl(), &utbuf);
}
});
#endif
}
};
//===========================================================================
......
......@@ -8,7 +8,6 @@
#include "kio_smb.h"
#include "smburl.h"
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
......@@ -367,19 +366,11 @@ void SMBSlave::smbCopyGet(const QUrl &ksrc, const QUrl &kdst, int permissions, K
}
}
// Restore the mtime on the file.
const QString mtimeStr = metaData("modified");
qCDebug(KIO_SMB_LOG) << "modified:" << mtimeStr;
if (!mtimeStr.isEmpty()) {
QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dt.isValid()) {
struct utimbuf utbuf {
};
utbuf.actime = QFileInfo(dstFile).lastRead().toSecsSinceEpoch(); // access time, unchanged
utbuf.modtime = dt.toSecsSinceEpoch(); // modification time
utime(QFile::encodeName(dstFile).constData(), &utbuf);
}
}
// set modification time (if applicable)
applyMTime([dstFile](struct utimbuf utbuf) {
utbuf.actime = QFileInfo(dstFile).lastRead().toSecsSinceEpoch(); // access time, unchanged
utime(QFile::encodeName(dstFile).constData(), &utbuf);
});
finished();
}
......@@ -550,20 +541,7 @@ void SMBSlave::smbCopyPut(const QUrl &ksrc, const QUrl &kdst, int permissions, K
}
}
#ifdef HAVE_UTIME_H
// set modification time
const QString mtimeStr = metaData("modified");
if (!mtimeStr.isEmpty()) {
QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dt.isValid()) {
struct utimbuf utbuf {
};
utbuf.actime = st.st_atime; // access time, unchanged
utbuf.modtime = dt.toSecsSinceEpoch(); // modification time
smbc_utime(dstOrigUrl.toSmbcUrl(), &utbuf);
}
}
#endif
applyMTimeSMBC(dstOrigUrl);
// We have done our job => finish
finished();
......
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2000 Caldera Systems Inc.
SPDX-FileCopyrightText: 2018-2020 Harald Sitter <sitter@kde.org>
SPDX-FileCopyrightText: 2018-2021 Harald Sitter <sitter@kde.org>
SPDX-FileContributor: Matthew Peterson <mpeterson@caldera.com>
*/
#include "kio_smb.h"
#include "smburl.h"
#include <QDateTime>
#include <QMimeDatabase>
#include <QMimeType>
#include <QVarLengthArray>
......@@ -404,22 +403,8 @@ void SMBSlave::put(const QUrl &kurl, int permissions, KIO::JobFlags flags)
// TODO: put in call to chmod when it is working!
// smbc_chmod(url.toSmbcUrl(),permissions);
}
#ifdef HAVE_UTIME_H
// set modification time
const QString mtimeStr = metaData("modified");
if (!mtimeStr.isEmpty()) {
QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dt.isValid()) {
if (cache_stat(m_current_url, &st) == 0) {
struct utimbuf utbuf {
};
utbuf.actime = st.st_atime; // access time, unchanged
utbuf.modtime = dt.toSecsSinceEpoch(); // modification time
smbc_utime(m_current_url.toSmbcUrl(), &utbuf);
}
}
}
#endif
applyMTimeSMBC(m_current_url);
// We have done our job => finish
finished();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment