From fa5bb2ea07089be737481f3b5348a8523eae3c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 29 Dec 2021 12:33:47 +0000 Subject: [PATCH 1/2] link: filelink: Add isWritable function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/link/filelink.cpp | 12 ++++++++++++ src/link/filelink.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/link/filelink.cpp b/src/link/filelink.cpp index f915bd07f..2325b953d 100644 --- a/src/link/filelink.cpp +++ b/src/link/filelink.cpp @@ -188,6 +188,18 @@ bool FileLink::isOpen() || _file.isReadable(); // If file is readable it's already opened and working }; +bool FileLink::isWritable() +{ + // We should use the absoluteBasePath to make sure that the path is writable + // Using the complete path with the file name may result in problems (E.g: Mac) + // If the file does not exist yet the check may result in isWritable as false + // Checking for the base path is a working and equivalent check + // This also avoids the creation of empty files + const auto absoluteBasePath = QFileInfo(_file).absolutePath(); + const bool isWritable = QFileInfo(absoluteBasePath).isWritable(); + return isWritable; +} + bool FileLink::finishConnection() { // Stop reading log process if it's running diff --git a/src/link/filelink.h b/src/link/filelink.h index d1a7cf943..c274f353b 100644 --- a/src/link/filelink.h +++ b/src/link/filelink.h @@ -70,7 +70,7 @@ class FileLink : public AbstractLink { * @return true * @return false */ - bool isWritable() final { return false; }; + bool isWritable() final; /** * @brief Return package index From b4ed4cd7c69a1986177950a8f23a5ccb3e48bd2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 29 Dec 2021 12:29:52 +0000 Subject: [PATCH 2/2] link: filelink: Use new isWritable function implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/link/filelink.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/link/filelink.cpp b/src/link/filelink.cpp index 2325b953d..15548401c 100644 --- a/src/link/filelink.cpp +++ b/src/link/filelink.cpp @@ -38,14 +38,14 @@ void FileLink::writeData(const QByteArray& data) } // This save the data as a structure to deal with the timestamp - if (_openModeFlag == QIODevice::WriteOnly && _file.isWritable()) { + if (_openModeFlag == QIODevice::WriteOnly && isWritable()) { QString time = QTime::fromMSecsSinceStartOfDay(_timer.elapsed()).toString(_timeFormat); Pack pack {time, data}; _inout << pack.time << pack.data; } else { qCWarning(PING_PROTOCOL_FILELINK) << "Something is wrong!"; qCDebug(PING_PROTOCOL_FILELINK) << "File is opened as write only:" << (_openModeFlag == QIODevice::WriteOnly); - qCDebug(PING_PROTOCOL_FILELINK) << "File can be writable:" << _file.isWritable(); + qCDebug(PING_PROTOCOL_FILELINK) << "File can be writable:" << isWritable(); } } @@ -67,10 +67,7 @@ bool FileLink::setConfiguration(const LinkConfiguration& linkConfiguration) _file.setFileName(linkConfiguration.args()->at(0)); if (_openModeFlag == QIODevice::WriteOnly) { - // The file will be created when something is received - // Avoiding empty files - // Check if path is writable - return QFileInfo(QFileInfo(_file).canonicalPath()).isWritable(); + return isWritable(); } // Everything after this point is to deal with reading data @@ -99,10 +96,8 @@ bool FileLink::setConfiguration(const LinkConfiguration& linkConfiguration) bool FileLink::startConnection() { - // The file will be created when something is received - // Avoiding empty files if (_openModeFlag == QIODevice::WriteOnly) { - return QFileInfo(QFileInfo(_file).canonicalPath()).isWritable(); + return isWritable(); } if (!isOpen()) { @@ -184,7 +179,7 @@ bool FileLink::isOpen() { // If filelink exist to create a log, the file will be only created after receiving the first data // To return at least a good answer, we do check the path to see if it's writable - return (QFileInfo(QFileInfo(_file).canonicalPath()).isWritable() && _openModeFlag == QIODevice::WriteOnly) + return (isWritable() && _openModeFlag == QIODevice::WriteOnly) || _file.isReadable(); // If file is readable it's already opened and working };