From 8c533510ee73e4cc1d4b009e09881a82cb0e8ddd Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 1 May 2024 13:57:41 +0100 Subject: [PATCH] gdocumentportal: Factor out opening_ro_might_succeed() No functional change. Signed-off-by: Simon McVittie --- gio/gdocumentportal.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gio/gdocumentportal.c b/gio/gdocumentportal.c index 382e2aab6..531156660 100644 --- a/gio/gdocumentportal.c +++ b/gio/gdocumentportal.c @@ -90,6 +90,24 @@ enum { XDP_ADD_FLAGS_FLAGS_ALL = ((1 << 3) - 1) }; +/* + * Assume that opening a file read/write failed with @saved_errno, + * and return TRUE if opening the same file read-only might succeed. + */ +static gboolean +opening_ro_might_succeed (int saved_errno) +{ + switch (saved_errno) + { + case EACCES: + case EISDIR: + return TRUE; + + default: + return FALSE; + } +} + GList * g_document_portal_add_documents (GList *uris, const char *app_id, @@ -131,7 +149,7 @@ g_document_portal_add_documents (GList *uris, int fd; fd = g_open (path, O_CLOEXEC | O_RDWR); - if (fd == -1 && (errno == EACCES || errno == EISDIR)) + if (fd == -1 && opening_ro_might_succeed (errno)) { /* If we don't have write access, fall back to read-only, * and stop requesting the write permission */