Something went wrong. Try again.
A small D-Bus client transport for Zig: session bus, method calls, signals, replies and file descriptor passing.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Reveals a local file in the file manager through the desktop portal://! https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html//!//! `OpenDirectory` opens the directory containing a file, and takes that//! file as an open descriptor rather than a path, which is what makes it//! work from inside a sandbox: the portal receives the descriptor over the//! socket and never needs the caller's filesystem view. The descriptor is//! encoded as an `h` index into the `fds` slice handed to `call`.//!//! The outcome arrives as a `Response` signal on the returned Request path,//! awaited here exactly the way the other portal examples await theirs.
const std = @import("std");const Io = std.Io;
const mdbus = @import("mdbus");const Connection = mdbus.Connection;
/// How long to wait for the portal to answer. Revealing a file is normally/// instant, but a portal may still put a prompt in front of the user.const response_timeout_ns: i96 = 60 * std.time.ns_per_s;
fn nowNs(io: Io) i96 { return Io.Clock.Timestamp.now(io, .awake).raw.nanoseconds;}
pub fn main(init: std.process.Init) !u8 { const gpa = init.gpa; const io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator());
var stderr_buffer: [512]u8 = undefined; var stderr_writer: Io.File.Writer = .init(.stderr(), io, &stderr_buffer); const stderr = &stderr_writer.interface;
if (args.len != 2) { try stderr.print("usage: {s} <file>\n", .{if (args.len != 0) args[0] else "open-directory"}); try stderr.flush(); return 1; }
var file = Io.Dir.cwd().openFile(io, args[1], .{}) catch |err| { try stderr.print("cannot open {s}: {t}\n", .{ args[1], err }); try stderr.flush(); return 1; }; defer file.close(io);
var connection = try Connection.connectSession(io, gpa, init.minimal.environ); defer connection.deinit();
// OpenDirectory takes a parent window identifier -- empty for a program // with no window -- the file as an `h` index into the descriptors sent // with the message, and a vardict of options this example leaves empty. var body: Connection.Encoder = .init(gpa); defer body.deinit(); try body.string(""); try body.unixFd(0); const options = try body.beginArray('{'); try body.endArray(options);
var reply = try connection.call(.{ .destination = "org.freedesktop.portal.Desktop", .path = "/org/freedesktop/portal/desktop", .interface = "org.freedesktop.portal.OpenURI", .member = "OpenDirectory", }, "sha{sv}", body.bytes(), &.{file.handle}, 5000); defer reply.deinit();
if (reply.messageType() == .error_reply) { try stderr.print("portal error: {s}\n", .{reply.header.error_name.?}); var decoder = reply.bodyDecoder(); if (decoder.string()) |detail| { try stderr.print(" {s}\n", .{detail}); } else |_| {} try stderr.flush(); return 1; }
var decoder = reply.bodyDecoder(); const handle = try decoder.objectPath(); try decoder.end();
// Wait for the Response signal on the request handle, polling the // connection's descriptor the way an event loop would. const deadline = nowNs(io) + response_timeout_ns; while (true) { while (try connection.nextMessage()) |received| { var message = received; defer message.deinit(); if (message.messageType() != .signal) continue; const path = message.header.path orelse continue; const member = message.header.member orelse continue; if (!std.mem.eql(u8, path, handle) or !std.mem.eql(u8, member, "Response")) continue;
// Response carries a result code and a vardict of results, // which OpenDirectory leaves empty. var response = message.bodyDecoder(); switch (try response.uint32()) { 0 => return 0, 1 => { try stderr.print("cancelled\n", .{}); try stderr.flush(); return 1; }, else => { try stderr.print("the portal could not reveal {s}\n", .{args[1]}); try stderr.flush(); return 1; }, } }
const remaining_ns = deadline - nowNs(io); if (remaining_ns <= 0) { try stderr.print("no answer from the portal\n", .{}); try stderr.flush(); return 1; } var poll_fds = [_]std.posix.pollfd{.{ .fd = connection.getFd(), .events = std.posix.POLL.IN, .revents = 0, }}; _ = try std.posix.poll(&poll_fds, @intCast(@divTrunc(remaining_ns, std.time.ns_per_ms) + 1)); }}