是否可以使用 gio 从非 GUI 会话修改 GNOME 桌面文件元数据?

Is it possible to modify GNOME desktop file metadata from non-GUI session using gio?

运行 CentOS 7 和 GNOME 3.22.2,我正在尝试编写一个 Ansible 剧本来将图标从 /usr/share/applications 复制到预定义用户的桌面,然后授予可信权限(使用gio set "metadata::trusted" yes).

当我尝试使用 gio info -w /home/demo/Desktop/google-chrome.desktop 从终端会话中查看桌面图标的可写属性时,我得到了这个结果:

Settable attributes:
 standard::symlink-target (bytestring)
 time::access (uint64, Keep with file when moved)
 time::access-usec (uint32, Keep with file when moved)
 time::modified (uint64, Copy with file, Keep with file when moved)
 time::modified-usec (uint32, Copy with file, Keep with file when moved)
 unix::gid (uint32, Keep with file when moved)
 unix::mode (uint32, Copy with file, Keep with file when moved)
 unix::uid (uint32, Keep with file when moved)
Writable attribute namespaces:
 xattr (string, Copy with file, Keep with file when moved)
 xattr-sys (string, Keep with file when moved)

所以当我尝试 运行 上面的 gio set 命令时,它失败并显示 gio: Setting attribute metadata::trusted not supported 因为无法写入元数据。但是,如果我登录到 GUI 会话并从终端 运行 执行相同的命令,那么我会看到元数据现在是一个可写属性:

Settable attributes:
 standard::symlink-target (bytestring)
 time::access (uint64, Keep with file when moved)
 time::access-usec (uint32, Keep with file when moved)
 time::modified (uint64, Copy with file, Keep with file when moved)
 time::modified-usec (uint32, Copy with file, Keep with file when moved)
 unix::gid (uint32, Keep with file when moved)
 unix::mode (uint32, Copy with file, Keep with file when moved)
 unix::uid (uint32, Keep with file when moved)
Writable attribute namespaces:
 metadata (string, Copy with file, Keep with file when moved)
 xattr (string, Copy with file, Keep with file when moved)
 xattr-sys (string, Keep with file when moved)

这意味着 gio set 命令在 GNOME GUI 会话中有效,但在终端会话中无效。我试图弄清楚是否有办法让终端会话能够修改它(以便我的剧本可以进行此修改)。如果需要的话,我不反对必须使用不同的脚本语言来做到这一点。

有关信息,当前的 Ansible 剧本部分如下所示:

- name: Make Chrome Launcher Trusted
  shell: gio set /home/demo/Desktop/google-chrome.desktop "metadata::trusted" yes

您需要 dbus,如果您的用户尚未登录,则不需要 运行。您可以使用 dbus-launch 启动 dbus 以供 gio 使用:

- name: Make Chrome Launcher Trusted
  shell: dbus-launch gio set /home/demo/Desktop/google-chrome.desktop "metadata::trusted" yes