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

jhr*_*abi 6 desktop gnome gio ansible

使用 GNOME 3.22.2 运行 CentOS 7,我正在尝试编写一个 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)
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试运行上述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)
Run Code Online (Sandbox Code Playgroud)

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

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

- name: Make Chrome Launcher Trusted
  shell: gio set /home/demo/Desktop/google-chrome.desktop "metadata::trusted" yes
Run Code Online (Sandbox Code Playgroud)

小智 13

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

- name: Make Chrome Launcher Trusted
  shell: dbus-launch gio set /home/demo/Desktop/google-chrome.desktop "metadata::trusted" yes
Run Code Online (Sandbox Code Playgroud)