From 886074dc1ecd15de1817c23caddfb64019b7ccb2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 29 May 2023 13:16:47 -0400
Subject: [PATCH] qt: openfile portal: do not use O_PATH fds

This allow to use xdg-open inside a snap

Change-Id: I5ad7e067228b260174abf5c78d0a0e7c2cf8c31d
---
 extras/packaging/gnu-linux/Makefile           |  2 +-
 ...penFile-portal-do-not-use-O_PATH-fds.patch | 52 +++++++++++++++++++
 .../gnu-linux/rules/debian-qt/patches/series  |  3 +-
 .../gnu-linux/rules/rpm/jami-libqt.spec       |  2 +
 ...penFile-portal-do-not-use-O_PATH-fds.patch | 52 +++++++++++++++++++
 .../gnu-linux/scripts/build-package-rpm.sh    |  2 +-
 6 files changed, 110 insertions(+), 3 deletions(-)
 create mode 100644 extras/packaging/gnu-linux/rules/debian-qt/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
 create mode 100644 extras/packaging/gnu-linux/rules/rpm/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch

diff --git a/extras/packaging/gnu-linux/Makefile b/extras/packaging/gnu-linux/Makefile
index 524bbb381..5ed9b6441 100644
--- a/extras/packaging/gnu-linux/Makefile
+++ b/extras/packaging/gnu-linux/Makefile
@@ -49,7 +49,7 @@ QT_MAJOR := 6
 QT_MINOR := 4
 QT_PATCH := 3
 QT_TARBALL_CHECKSUM := 29a7eebdbba0ea57978dea6083709c93593a60f0f3133a3de08b9571ee8eaab4
-DEBIAN_QT_VERSION := $(QT_MAJOR).$(QT_MINOR).$(QT_PATCH)-1
+DEBIAN_QT_VERSION := $(QT_MAJOR).$(QT_MINOR).$(QT_PATCH)-2
 DEBIAN_QT_DSC_FILENAME := libqt-jami_$(DEBIAN_QT_VERSION).dsc
 QT_JAMI_PREFIX := /usr/lib/libqt-jami
 
diff --git a/extras/packaging/gnu-linux/rules/debian-qt/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch b/extras/packaging/gnu-linux/rules/debian-qt/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
new file mode 100644
index 000000000..c2ca9dbce
--- /dev/null
+++ b/extras/packaging/gnu-linux/rules/debian-qt/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
@@ -0,0 +1,52 @@
+From f4410fcbb093f259eaff4a20fc4266a535479235 Mon Sep 17 00:00:00 2001
+From: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
+Date: Mon, 29 May 2023 13:09:53 -0400
+Subject: [PATCH] OpenFile portal: do not use O_PATH fds
+
+Using O_PATH requires correctly specifying whether the fd is writable or
+not. Stating that the fd is writable without it actually being writable
+results into rejection on xdg-desktop-portal side. Other implementations
+like xdg-open or gtk have also moved away from O_PATH fds so this will
+make a matching implementation and avoid possible rejections from xdp.
+
+Fixes: QTBUG-113143
+Original: https://codereview.qt-project.org/c/qt/qtbase/+/475425
+---
+ qtbase/src/gui/platform/unix/qgenericunixservices.cpp | 8 ++------
+ 1 file changed, 2 insertions(+), 6 deletions(-)
+
+diff --git a/qtbase/src/gui/platform/unix/qgenericunixservices.cpp b/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
+index a0e5466c58..fe0fdaa625 100644
+--- a/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
++++ b/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
+@@ -163,8 +163,7 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
+     // handle_token (s) -  A string that will be used as the last element of the @handle.
+     // writable (b) - Whether to allow the chosen application to write to the file.
+ 
+-#ifdef O_PATH
+-    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_PATH);
++    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_RDONLY);
+     if (fd != -1) {
+         QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop"_L1,
+                                                               "/org/freedesktop/portal/desktop"_L1,
+@@ -174,16 +173,13 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
+         QDBusUnixFileDescriptor descriptor;
+         descriptor.giveFileDescriptor(fd);
+ 
+-        const QVariantMap options = {{"writable"_L1, true}};
++        const QVariantMap options = {};
+ 
+         // FIXME parent_window_id
+         message << QString() << QVariant::fromValue(descriptor) << options;
+ 
+         return QDBusConnection::sessionBus().call(message);
+     }
+-#else
+-    Q_UNUSED(url);
+-#endif
+ 
+     return QDBusMessage::createError(QDBusError::InternalError, qt_error_string());
+ }
+-- 
+2.40.1
+
diff --git a/extras/packaging/gnu-linux/rules/debian-qt/patches/series b/extras/packaging/gnu-linux/rules/debian-qt/patches/series
index 55dc20056..98b1f3a1f 100644
--- a/extras/packaging/gnu-linux/rules/debian-qt/patches/series
+++ b/extras/packaging/gnu-linux/rules/debian-qt/patches/series
@@ -1 +1,2 @@
-0001-fix-gcc13.patch
\ No newline at end of file
+0001-fix-gcc13.patch
+0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
\ No newline at end of file
diff --git a/extras/packaging/gnu-linux/rules/rpm/jami-libqt.spec b/extras/packaging/gnu-linux/rules/rpm/jami-libqt.spec
index 4e0a68d41..c4260532a 100644
--- a/extras/packaging/gnu-linux/rules/rpm/jami-libqt.spec
+++ b/extras/packaging/gnu-linux/rules/rpm/jami-libqt.spec
@@ -27,6 +27,7 @@ Vendor:        Savoir-faire Linux Inc.
 URL:           https://jami.net/
 Source:        jami-libqt-%{version}.tar.xz
 Patch0:        0001-fix-gcc13.patch
+Patch1:        0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
 
 %global gst 0.10
 %if 0%{?fedora} || 0%{?rhel} > 7
@@ -62,6 +63,7 @@ This package contains Qt libraries for Jami.
 %prep
 %setup -n qt-everywhere-src-%{version}
 %patch0 -p1
+%patch1 -p1
 
 %build
 echo "Building Qt using %{job_count} parallel jobs"
diff --git a/extras/packaging/gnu-linux/rules/rpm/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch b/extras/packaging/gnu-linux/rules/rpm/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
new file mode 100644
index 000000000..c2ca9dbce
--- /dev/null
+++ b/extras/packaging/gnu-linux/rules/rpm/patches/0002-OpenFile-portal-do-not-use-O_PATH-fds.patch
@@ -0,0 +1,52 @@
+From f4410fcbb093f259eaff4a20fc4266a535479235 Mon Sep 17 00:00:00 2001
+From: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
+Date: Mon, 29 May 2023 13:09:53 -0400
+Subject: [PATCH] OpenFile portal: do not use O_PATH fds
+
+Using O_PATH requires correctly specifying whether the fd is writable or
+not. Stating that the fd is writable without it actually being writable
+results into rejection on xdg-desktop-portal side. Other implementations
+like xdg-open or gtk have also moved away from O_PATH fds so this will
+make a matching implementation and avoid possible rejections from xdp.
+
+Fixes: QTBUG-113143
+Original: https://codereview.qt-project.org/c/qt/qtbase/+/475425
+---
+ qtbase/src/gui/platform/unix/qgenericunixservices.cpp | 8 ++------
+ 1 file changed, 2 insertions(+), 6 deletions(-)
+
+diff --git a/qtbase/src/gui/platform/unix/qgenericunixservices.cpp b/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
+index a0e5466c58..fe0fdaa625 100644
+--- a/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
++++ b/qtbase/src/gui/platform/unix/qgenericunixservices.cpp
+@@ -163,8 +163,7 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
+     // handle_token (s) -  A string that will be used as the last element of the @handle.
+     // writable (b) - Whether to allow the chosen application to write to the file.
+ 
+-#ifdef O_PATH
+-    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_PATH);
++    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_RDONLY);
+     if (fd != -1) {
+         QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop"_L1,
+                                                               "/org/freedesktop/portal/desktop"_L1,
+@@ -174,16 +173,13 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
+         QDBusUnixFileDescriptor descriptor;
+         descriptor.giveFileDescriptor(fd);
+ 
+-        const QVariantMap options = {{"writable"_L1, true}};
++        const QVariantMap options = {};
+ 
+         // FIXME parent_window_id
+         message << QString() << QVariant::fromValue(descriptor) << options;
+ 
+         return QDBusConnection::sessionBus().call(message);
+     }
+-#else
+-    Q_UNUSED(url);
+-#endif
+ 
+     return QDBusMessage::createError(QDBusError::InternalError, qt_error_string());
+ }
+-- 
+2.40.1
+
diff --git a/extras/packaging/gnu-linux/scripts/build-package-rpm.sh b/extras/packaging/gnu-linux/scripts/build-package-rpm.sh
index 2ae016a97..a7019e6c2 100755
--- a/extras/packaging/gnu-linux/scripts/build-package-rpm.sh
+++ b/extras/packaging/gnu-linux/scripts/build-package-rpm.sh
@@ -46,7 +46,7 @@ CMAKE_PREFIX_PATH="${QT_JAMI_PREFIX}/lib/cmake:${CMAKE_PREFIX_PATH}"
 QT_MAJOR=6
 QT_MINOR=4
 QT_PATCH=3
-QT_RELEASE_PATCH=1
+QT_RELEASE_PATCH=2
 
 QT_MAJOR_MINOR=${QT_MAJOR}.${QT_MINOR}
 QT_MAJOR_MINOR_PATCH=${QT_MAJOR}.${QT_MINOR}.${QT_PATCH}
-- 
GitLab