Get "real" path under Flatpak

Hello, I am coding a Gnome application with python, and I open a Gtk.FileDialog. I get the result, but it’s not a real path (e.g: /run/user/…), due to Flatpak sandboxing. So my question is: how can I get the real path? Libportal, Gio, libflatpak?

Thank you for response!

You can query the file using the xattr::document-portal.host-path file attribute.

3 Likes

Thank you! But can I have an example of how to do it? I created this:

def get_real_path(self, sandboxed_path: str) → str:
folder = Gio.File.new_for_path(sandboxed_path)

    try:
        info = folder.query_info(
            "xattr::user.document-portal.host-path",
            Gio.FileQueryInfoFlags.NONE,
            None
        )
        real_path = info.get_attribute_string("xattr::user.document-portal.host-path")
        return real_path if real_path else sandboxed_path
    except GLib.Error:
        return sandboxed_path

But the path is always a temp file.

This is probably because this attribute is fairly new. It was introduced in version 1.19.0 of the XDG desktop portals, which is on GitHub also marked as an prerelease (I assume xdg-desktop-portal uses the versioning schema where odd numbers are previews and even numbers stable releases?).

With that being said, you probably need to wait a bit until XDG Desktop Portal is updated on your system for the information to be provided.

1 Like

Also, in XDG desktop portals version 1.19, when you open a directory with portals then only the directory itself gets this xattr, but not the individual files inside.

This was fixed recently, and will be available in upcoming version 1.20.

1 Like

Note this from the gio docs:

So to handle this attribute correctly, you need to hex-unescape it, and then it is no longer guaranteed to be utf-8, so you may want to use g_filename_display_name to show it.

2 Likes

Thanks to everyone. But how to do this without having the most recent version? Other apps do it.

You’ll have to ask the authors of those “other apps”.

The “other” apps, like GNOME Text Editor, often use large filesystem permissions, like --filesystem=home or even --filesystem=host. With these permissions, files are visible under their real path, rather then provided over the proxy of the document portal.

Just note that in many cases, this should not be recommended for Flatpak apps, unless you have a good reason, which text editors and IDE’s have, but most other programs not.

Ok, but I put “–filesystem=host” to my flatpak manifest.

For anyone who has the same problem as me, I found the solution. You have to use “–filesystem=host” as previously mentioned AND install the application (do not run it from GNOME Builder).