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::user.document-portal.host-path file attribute.

2 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.

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.