Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions 3rdparty/deepin-pdfium/src/dpdfdoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QFile>
#include <QTemporaryDir>
#include <QUuid>
#include <QStorageInfo>
#include <unistd.h>

/**
Expand Down Expand Up @@ -232,6 +233,19 @@ bool DPdfDoc::isLinearized(const QString &fileName)

static QFile saveWriter;

static bool isNetworkFileSystem(const QString &path)
{
if (path.contains(QLatin1String("/gvfs/")))
return true;

QStorageInfo storage(path);
QByteArray fsType = storage.fileSystemType();
if (fsType.contains("cifs") || fsType.contains("smb") || fsType.contains("fuse.gvfs"))
return true;

return false;
}

int writeFile(struct FPDF_FILEWRITE_* pThis, const void *pData, unsigned long size)
{
Q_UNUSED(pThis)
Expand Down Expand Up @@ -309,7 +323,8 @@ bool DPdfDoc::save()
}

newFile.flush(); // Flush user buffer to kernel buffer
fsync(newFile.handle()); // Sync kernel buffer to disk
if (!isNetworkFileSystem(targetPath))
fsync(newFile.handle()); // Sync kernel buffer to disk (skip on network FS to avoid UI freeze)
newFile.close();

// Remove backup file after successful write
Expand All @@ -336,7 +351,8 @@ bool DPdfDoc::saveAs(const QString &filePath)
locker.unlock();

saveWriter.flush(); // Flush user buffer to kernel buffer
fsync(saveWriter.handle()); // Sync kernel buffer to disk
if (!isNetworkFileSystem(filePath))
fsync(saveWriter.handle()); // Sync kernel buffer to disk (skip on network FS to avoid UI freeze)
saveWriter.close();

return result;
Expand Down
57 changes: 57 additions & 0 deletions tests/document/ut_networkfilesystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 5 in tests/document/ut_networkfilesystem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QCoreApplication>

Check warning on line 7 in tests/document/ut_networkfilesystem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QStorageInfo>

Check warning on line 8 in tests/document/ut_networkfilesystem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QStorageInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QString>

Check warning on line 9 in tests/document/ut_networkfilesystem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTemporaryFile>

Check warning on line 10 in tests/document/ut_networkfilesystem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTemporaryFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

// Replicate the static helper from dpdfdoc.cpp for unit testing.
// The original isNetworkFileSystem() is file-local (static) in
// 3rdparty/deepin-pdfium/src/dpdfdoc.cpp and cannot be linked from
// test code; this copy verifies the decision logic.
static bool isNetworkFileSystem(const QString &path)
{
if (path.contains(QLatin1String("/gvfs/")))
return true;

QStorageInfo storage(path);
QByteArray fsType = storage.fileSystemType();
if (fsType.contains("cifs") || fsType.contains("smb") || fsType.contains("fuse.gvfs"))
return true;

return false;
}

TEST(TestNetworkFileSystem, LocalPathIsNotNetwork)
{
QTemporaryFile tmp;
ASSERT_TRUE(tmp.open());
EXPECT_FALSE(isNetworkFileSystem(tmp.fileName()));
}

TEST(TestNetworkFileSystem, GvfsPathIsNetwork)
{
QString gvfsPath = QStringLiteral("/run/user/1000/gvfs/smb-share:server=fileserver,share=public/doc.pdf");
EXPECT_TRUE(isNetworkFileSystem(gvfsPath));
}

TEST(TestNetworkFileSystem, RootIsNotNetwork)
{
EXPECT_FALSE(isNetworkFileSystem(QStringLiteral("/")));
}

TEST(TestNetworkFileSystem, TmpDirIsNotNetwork)
{
EXPECT_FALSE(isNetworkFileSystem(QStringLiteral("/tmp")));
}

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading