Prefer `customize-set-variable' in tramp.texi
[emacs.git] / lisp / net / tramp-gvfs.el
blob709ea4670a89241a4263a557dfe38b590eb979b3
1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon -*- lexical-binding:t -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Access functions for the GVFS daemon from Tramp. Tested with GVFS
27 ;; 1.0 (Ubuntu 8.10, Gnome 2.24). It has been reported also to run
28 ;; with GVFS 0.2.5 (Ubuntu 8.04, Gnome 2.22), but there is an
29 ;; incompatibility with the mount_info structure, which has been
30 ;; worked around.
32 ;; It has also been tested with GVFS 1.6 (Ubuntu 10.04, Gnome 2.30),
33 ;; where the default_location has been added to mount_info (see
34 ;; <https://bugzilla.gnome.org/show_bug.cgi?id=561998>.
36 ;; With GVFS 1.14 (Ubuntu 12.10, Gnome 3.6) the interfaces have been
37 ;; changed, again. So we must introspect the D-Bus interfaces.
39 ;; All actions to mount a remote location, and to retrieve mount
40 ;; information, are performed by D-Bus messages. File operations
41 ;; themselves are performed via the mounted filesystem in ~/.gvfs.
42 ;; Consequently, GNU Emacs with enabled D-Bus bindings is a
43 ;; precondition.
45 ;; The GVFS D-Bus interface is said to be unstable. There were even
46 ;; no introspection data before GVFS 1.14. The interface, as
47 ;; discovered during development time, is given in respective
48 ;; comments.
50 ;; The custom option `tramp-gvfs-methods' contains the list of
51 ;; supported connection methods. Per default, these are "afp", "dav",
52 ;; "davs", "gdrive", "obex", "sftp" and "synce". Note that with
53 ;; "obex" it might be necessary to pair with the other bluetooth
54 ;; device, if it hasn't been done already. There might be also some
55 ;; few seconds delay in discovering available bluetooth devices.
57 ;; Other possible connection methods are "ftp" and "smb". When one of
58 ;; these methods is added to the list, the remote access for that
59 ;; method is performed via GVFS instead of the native Tramp
60 ;; implementation.
62 ;; GVFS offers even more connection methods. The complete list of
63 ;; connection methods of the actual GVFS implementation can be
64 ;; retrieved by:
66 ;; (message
67 ;; "%s"
68 ;; (mapcar
69 ;; 'car
70 ;; (dbus-call-method
71 ;; :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
72 ;; tramp-gvfs-interface-mounttracker "listMountableInfo")))
74 ;; Note that all other connection methods are not tested, beside the
75 ;; ones offered for customization in `tramp-gvfs-methods'. If you
76 ;; request an additional connection method to be supported, please
77 ;; drop me a note.
79 ;; For hostname completion, information is retrieved either from the
80 ;; bluez daemon (for the "obex" method), the hal daemon (for the
81 ;; "synce" method), or from the zeroconf daemon (for the "afp", "dav",
82 ;; "davs", and "sftp" methods). The zeroconf daemon is pre-configured
83 ;; to discover services in the "local" domain. If another domain
84 ;; shall be used for discovering services, the custom option
85 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
87 ;; Restrictions:
89 ;; * The current GVFS implementation does not allow writing on the
90 ;; remote bluetooth device via OBEX.
92 ;; * Two shares of the same SMB server cannot be mounted in parallel.
94 ;;; Code:
96 ;; D-Bus support in the Emacs core can be disabled with configuration
97 ;; option "--without-dbus". Declare used subroutines and variables.
98 (declare-function dbus-get-unique-name "dbusbind.c")
100 (require 'tramp)
102 (require 'dbus)
103 (require 'url-parse)
104 (require 'url-util)
105 (require 'zeroconf)
107 ;; Pacify byte-compiler.
108 (eval-when-compile
109 (require 'custom))
111 ;;;###tramp-autoload
112 (defcustom tramp-gvfs-methods
113 '("afp" "dav" "davs" "gdrive" "obex" "sftp" "synce")
114 "List of methods for remote files, accessed with GVFS."
115 :group 'tramp
116 :version "26.1"
117 :type '(repeat (choice (const "afp")
118 (const "dav")
119 (const "davs")
120 (const "ftp")
121 (const "gdrive")
122 (const "obex")
123 (const "sftp")
124 (const "smb")
125 (const "synce")))
126 :require 'tramp)
128 ;; Add defaults for `tramp-default-user-alist' and `tramp-default-host-alist'.
129 ;;;###tramp-autoload
130 (when (string-match "\\(.+\\)@\\(\\(?:gmail\\|googlemail\\)\\.com\\)"
131 user-mail-address)
132 (add-to-list 'tramp-default-user-alist
133 `("\\`gdrive\\'" nil ,(match-string 1 user-mail-address)))
134 (add-to-list 'tramp-default-host-alist
135 '("\\`gdrive\\'" nil ,(match-string 2 user-mail-address))))
136 ;;;###tramp-autoload
137 (add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
139 ;;;###tramp-autoload
140 (defcustom tramp-gvfs-zeroconf-domain "local"
141 "Zeroconf domain to be used for discovering services, like host names."
142 :group 'tramp
143 :version "23.2"
144 :type 'string
145 :require 'tramp)
147 ;; Add the methods to `tramp-methods', in order to allow minibuffer
148 ;; completion.
149 ;;;###tramp-autoload
150 (when (featurep 'dbusbind)
151 (dolist (elt tramp-gvfs-methods)
152 (unless (assoc elt tramp-methods)
153 (add-to-list 'tramp-methods (cons elt nil)))))
155 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
156 "The preceding object path for own objects.")
158 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
159 "The well known name of the GVFS daemon.")
161 ;; We don't call `dbus-ping', because this would load dbus.el.
162 (defconst tramp-gvfs-enabled
163 (ignore-errors
164 (and (featurep 'dbusbind)
165 (tramp-compat-funcall 'dbus-get-unique-name :system)
166 (tramp-compat-funcall 'dbus-get-unique-name :session)
167 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
168 (tramp-compat-process-running-p "gvfsd-fuse"))))
169 "Non-nil when GVFS is available.")
171 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
172 "The object path of the GVFS daemon.")
174 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
175 "The mount tracking interface in the GVFS daemon.")
177 ;; Introspection data exist since GVFS 1.14. If there are no such
178 ;; data, we expect an earlier interface.
179 (defconst tramp-gvfs-methods-mounttracker
180 (and tramp-gvfs-enabled
181 (dbus-introspect-get-method-names
182 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
183 tramp-gvfs-interface-mounttracker))
184 "The list of supported methods of the mount tracking interface.")
186 (defconst tramp-gvfs-listmounts
187 (if (member "ListMounts" tramp-gvfs-methods-mounttracker)
188 "ListMounts"
189 "listMounts")
190 "The name of the \"listMounts\" method.
191 It has been changed in GVFS 1.14.")
193 (defconst tramp-gvfs-mountlocation
194 (if (member "MountLocation" tramp-gvfs-methods-mounttracker)
195 "MountLocation"
196 "mountLocation")
197 "The name of the \"mountLocation\" method.
198 It has been changed in GVFS 1.14.")
200 (defconst tramp-gvfs-mountlocation-signature
201 (and tramp-gvfs-enabled
202 (dbus-introspect-get-signature
203 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
204 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation))
205 "The D-Bus signature of the \"mountLocation\" method.
206 It has been changed in GVFS 1.14.")
208 ;; <interface name='org.gtk.vfs.MountTracker'>
209 ;; <method name='listMounts'>
210 ;; <arg name='mount_info_list'
211 ;; type='a{sosssssbay{aya{say}}ay}'
212 ;; direction='out'/>
213 ;; </method>
214 ;; <method name='mountLocation'>
215 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
216 ;; <arg name='dbus_id' type='s' direction='in'/>
217 ;; <arg name='object_path' type='o' direction='in'/>
218 ;; </method>
219 ;; <signal name='mounted'>
220 ;; <arg name='mount_info'
221 ;; type='{sosssssbay{aya{say}}ay}'/>
222 ;; </signal>
223 ;; <signal name='unmounted'>
224 ;; <arg name='mount_info'
225 ;; type='{sosssssbay{aya{say}}ay}'/>
226 ;; </signal>
227 ;; </interface>
229 ;; STRUCT mount_info
230 ;; STRING dbus_id
231 ;; OBJECT_PATH object_path
232 ;; STRING display_name
233 ;; STRING stable_name
234 ;; STRING x_content_types Since GVFS 1.0 only !!!
235 ;; STRING icon
236 ;; STRING preferred_filename_encoding
237 ;; BOOLEAN user_visible
238 ;; ARRAY BYTE fuse_mountpoint
239 ;; STRUCT mount_spec
240 ;; ARRAY BYTE mount_prefix
241 ;; ARRAY
242 ;; STRUCT mount_spec_item
243 ;; STRING key (type, user, domain, host, server,
244 ;; share, volume, port, ssl)
245 ;; ARRAY BYTE value
246 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
248 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
249 "Used by the dbus-proxying implementation of GMountOperation.")
251 ;; <interface name='org.gtk.vfs.MountOperation'>
252 ;; <method name='askPassword'>
253 ;; <arg name='message' type='s' direction='in'/>
254 ;; <arg name='default_user' type='s' direction='in'/>
255 ;; <arg name='default_domain' type='s' direction='in'/>
256 ;; <arg name='flags' type='u' direction='in'/>
257 ;; <arg name='handled' type='b' direction='out'/>
258 ;; <arg name='aborted' type='b' direction='out'/>
259 ;; <arg name='password' type='s' direction='out'/>
260 ;; <arg name='username' type='s' direction='out'/>
261 ;; <arg name='domain' type='s' direction='out'/>
262 ;; <arg name='anonymous' type='b' direction='out'/>
263 ;; <arg name='password_save' type='u' direction='out'/>
264 ;; </method>
265 ;; <method name='askQuestion'>
266 ;; <arg name='message' type='s' direction='in'/>
267 ;; <arg name='choices' type='as' direction='in'/>
268 ;; <arg name='handled' type='b' direction='out'/>
269 ;; <arg name='aborted' type='b' direction='out'/>
270 ;; <arg name='choice' type='u' direction='out'/>
271 ;; </method>
272 ;; </interface>
274 ;; The following flags are used in "askPassword". They are defined in
275 ;; /usr/include/glib-2.0/gio/gioenums.h.
277 (defconst tramp-gvfs-password-need-password 1
278 "Operation requires a password.")
280 (defconst tramp-gvfs-password-need-username 2
281 "Operation requires a username.")
283 (defconst tramp-gvfs-password-need-domain 4
284 "Operation requires a domain.")
286 (defconst tramp-gvfs-password-saving-supported 8
287 "Operation supports saving settings.")
289 (defconst tramp-gvfs-password-anonymous-supported 16
290 "Operation supports anonymous users.")
292 (defconst tramp-bluez-service "org.bluez"
293 "The well known name of the BLUEZ service.")
295 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
296 "The manager interface of the BLUEZ daemon.")
298 ;; <interface name='org.bluez.Manager'>
299 ;; <method name='DefaultAdapter'>
300 ;; <arg type='o' direction='out'/>
301 ;; </method>
302 ;; <method name='FindAdapter'>
303 ;; <arg type='s' direction='in'/>
304 ;; <arg type='o' direction='out'/>
305 ;; </method>
306 ;; <method name='ListAdapters'>
307 ;; <arg type='ao' direction='out'/>
308 ;; </method>
309 ;; <signal name='AdapterAdded'>
310 ;; <arg type='o'/>
311 ;; </signal>
312 ;; <signal name='AdapterRemoved'>
313 ;; <arg type='o'/>
314 ;; </signal>
315 ;; <signal name='DefaultAdapterChanged'>
316 ;; <arg type='o'/>
317 ;; </signal>
318 ;; </interface>
320 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
321 "The adapter interface of the BLUEZ daemon.")
323 ;; <interface name='org.bluez.Adapter'>
324 ;; <method name='GetProperties'>
325 ;; <arg type='a{sv}' direction='out'/>
326 ;; </method>
327 ;; <method name='SetProperty'>
328 ;; <arg type='s' direction='in'/>
329 ;; <arg type='v' direction='in'/>
330 ;; </method>
331 ;; <method name='RequestMode'>
332 ;; <arg type='s' direction='in'/>
333 ;; </method>
334 ;; <method name='ReleaseMode'/>
335 ;; <method name='RequestSession'/>
336 ;; <method name='ReleaseSession'/>
337 ;; <method name='StartDiscovery'/>
338 ;; <method name='StopDiscovery'/>
339 ;; <method name='ListDevices'>
340 ;; <arg type='ao' direction='out'/>
341 ;; </method>
342 ;; <method name='CreateDevice'>
343 ;; <arg type='s' direction='in'/>
344 ;; <arg type='o' direction='out'/>
345 ;; </method>
346 ;; <method name='CreatePairedDevice'>
347 ;; <arg type='s' direction='in'/>
348 ;; <arg type='o' direction='in'/>
349 ;; <arg type='s' direction='in'/>
350 ;; <arg type='o' direction='out'/>
351 ;; </method>
352 ;; <method name='CancelDeviceCreation'>
353 ;; <arg type='s' direction='in'/>
354 ;; </method>
355 ;; <method name='RemoveDevice'>
356 ;; <arg type='o' direction='in'/>
357 ;; </method>
358 ;; <method name='FindDevice'>
359 ;; <arg type='s' direction='in'/>
360 ;; <arg type='o' direction='out'/>
361 ;; </method>
362 ;; <method name='RegisterAgent'>
363 ;; <arg type='o' direction='in'/>
364 ;; <arg type='s' direction='in'/>
365 ;; </method>
366 ;; <method name='UnregisterAgent'>
367 ;; <arg type='o' direction='in'/>
368 ;; </method>
369 ;; <signal name='DeviceCreated'>
370 ;; <arg type='o'/>
371 ;; </signal>
372 ;; <signal name='DeviceRemoved'>
373 ;; <arg type='o'/>
374 ;; </signal>
375 ;; <signal name='DeviceFound'>
376 ;; <arg type='s'/>
377 ;; <arg type='a{sv}'/>
378 ;; </signal>
379 ;; <signal name='PropertyChanged'>
380 ;; <arg type='s'/>
381 ;; <arg type='v'/>
382 ;; </signal>
383 ;; <signal name='DeviceDisappeared'>
384 ;; <arg type='s'/>
385 ;; </signal>
386 ;; </interface>
388 ;;;###tramp-autoload
389 (defcustom tramp-bluez-discover-devices-timeout 60
390 "Defines seconds since last bluetooth device discovery before rescanning.
391 A value of 0 would require an immediate discovery during hostname
392 completion, nil means to use always cached values for discovered
393 devices."
394 :group 'tramp
395 :version "23.2"
396 :type '(choice (const nil) integer)
397 :require 'tramp)
399 (defvar tramp-bluez-discovery nil
400 "Indicator for a running bluetooth device discovery.
401 It keeps the timestamp of last discovery.")
403 (defvar tramp-bluez-devices nil
404 "Alist of detected bluetooth devices.
405 Every entry is a list (NAME ADDRESS).")
407 (defconst tramp-hal-service "org.freedesktop.Hal"
408 "The well known name of the HAL service.")
410 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
411 "The object path of the HAL daemon manager.")
413 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
414 "The manager interface of the HAL daemon.")
416 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
417 "The device interface of the HAL daemon.")
419 ;; "gvfs-<command>" utilities have been deprecated in GVFS 1.31.1. We
420 ;; must use "gio <command>" tool instead.
421 (defconst tramp-gvfs-gio-mapping
422 '(("gvfs-copy" . "copy")
423 ("gvfs-info" . "info")
424 ("gvfs-ls" . "list")
425 ("gvfs-mkdir" . "mkdir")
426 ("gvfs-monitor-file" . "monitor")
427 ("gvfs-move" . "move")
428 ("gvfs-rm" . "remove")
429 ("gvfs-trash" . "trash"))
430 "List of cons cells, mapping \"gvfs-<command>\" to \"gio <command>\".")
432 (defconst tramp-gvfs-file-attributes
433 '("name"
434 "type"
435 "standard::display-name"
436 "standard::symlink-target"
437 "unix::nlink"
438 "unix::uid"
439 "owner::user"
440 "unix::gid"
441 "owner::group"
442 "time::access"
443 "time::modified"
444 "time::changed"
445 "standard::size"
446 "unix::mode"
447 "access::can-read"
448 "access::can-write"
449 "access::can-execute"
450 "unix::inode"
451 "unix::device")
452 "GVFS file attributes.")
454 (defconst tramp-gvfs-file-attributes-with-gvfs-ls-regexp
455 (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t) "=\\(.+?\\)")
456 "Regexp to parse GVFS file attributes with `gvfs-ls'.")
458 (defconst tramp-gvfs-file-attributes-with-gvfs-info-regexp
459 (concat "^[[:blank:]]*"
460 (regexp-opt tramp-gvfs-file-attributes t)
461 ":[[:blank:]]+\\(.*\\)$")
462 "Regexp to parse GVFS file attributes with `gvfs-info'.")
464 (defconst tramp-gvfs-file-system-attributes
465 '("filesystem::free"
466 "filesystem::size"
467 "filesystem::used")
468 "GVFS file system attributes.")
470 (defconst tramp-gvfs-file-system-attributes-regexp
471 (concat "^[[:blank:]]*"
472 (regexp-opt tramp-gvfs-file-system-attributes t)
473 ":[[:blank:]]+\\(.*\\)$")
474 "Regexp to parse GVFS file system attributes with `gvfs-info'.")
477 ;; New handlers should be added here.
478 ;;;###tramp-autoload
479 (defconst tramp-gvfs-file-name-handler-alist
480 '((access-file . ignore)
481 (add-name-to-file . tramp-handle-add-name-to-file)
482 ;; `byte-compiler-base-file-name' performed by default handler.
483 ;; `copy-directory' performed by default handler.
484 (copy-file . tramp-gvfs-handle-copy-file)
485 (delete-directory . tramp-gvfs-handle-delete-directory)
486 (delete-file . tramp-gvfs-handle-delete-file)
487 ;; `diff-latest-backup-file' performed by default handler.
488 (directory-file-name . tramp-handle-directory-file-name)
489 (directory-files . tramp-handle-directory-files)
490 (directory-files-and-attributes
491 . tramp-handle-directory-files-and-attributes)
492 (dired-compress-file . ignore)
493 (dired-uncache . tramp-handle-dired-uncache)
494 (expand-file-name . tramp-gvfs-handle-expand-file-name)
495 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
496 (file-acl . ignore)
497 (file-attributes . tramp-gvfs-handle-file-attributes)
498 (file-directory-p . tramp-gvfs-handle-file-directory-p)
499 (file-equal-p . tramp-handle-file-equal-p)
500 (file-executable-p . tramp-gvfs-handle-file-executable-p)
501 (file-exists-p . tramp-handle-file-exists-p)
502 (file-in-directory-p . tramp-handle-file-in-directory-p)
503 (file-local-copy . tramp-gvfs-handle-file-local-copy)
504 (file-modes . tramp-handle-file-modes)
505 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
506 (file-name-as-directory . tramp-handle-file-name-as-directory)
507 (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
508 (file-name-completion . tramp-handle-file-name-completion)
509 (file-name-directory . tramp-handle-file-name-directory)
510 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
511 ;; `file-name-sans-versions' performed by default handler.
512 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
513 (file-notify-add-watch . tramp-gvfs-handle-file-notify-add-watch)
514 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
515 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
516 (file-ownership-preserved-p . ignore)
517 (file-readable-p . tramp-gvfs-handle-file-readable-p)
518 (file-regular-p . tramp-handle-file-regular-p)
519 (file-remote-p . tramp-handle-file-remote-p)
520 (file-selinux-context . ignore)
521 (file-symlink-p . tramp-handle-file-symlink-p)
522 (file-system-info . tramp-gvfs-handle-file-system-info)
523 (file-truename . tramp-handle-file-truename)
524 (file-writable-p . tramp-gvfs-handle-file-writable-p)
525 (find-backup-file-name . tramp-handle-find-backup-file-name)
526 ;; `find-file-noselect' performed by default handler.
527 ;; `get-file-buffer' performed by default handler.
528 (insert-directory . tramp-handle-insert-directory)
529 (insert-file-contents . tramp-handle-insert-file-contents)
530 (load . tramp-handle-load)
531 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
532 (make-directory . tramp-gvfs-handle-make-directory)
533 (make-directory-internal . ignore)
534 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
535 (make-symbolic-link . tramp-handle-make-symbolic-link)
536 (process-file . ignore)
537 (rename-file . tramp-gvfs-handle-rename-file)
538 (set-file-acl . ignore)
539 (set-file-modes . ignore)
540 (set-file-selinux-context . ignore)
541 (set-file-times . ignore)
542 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
543 (shell-command . ignore)
544 (start-file-process . ignore)
545 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
546 (temporary-file-directory . tramp-handle-temporary-file-directory)
547 (unhandled-file-name-directory . ignore)
548 (vc-registered . ignore)
549 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
550 (write-region . tramp-gvfs-handle-write-region))
551 "Alist of handler functions for Tramp GVFS method.
552 Operations not mentioned here will be handled by the default Emacs primitives.")
554 ;; It must be a `defsubst' in order to push the whole code into
555 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
556 ;;;###tramp-autoload
557 (defsubst tramp-gvfs-file-name-p (filename)
558 "Check if it's a filename handled by the GVFS daemon."
559 (and (tramp-tramp-file-p filename)
560 (let ((method
561 (tramp-file-name-method (tramp-dissect-file-name filename))))
562 (and (stringp method) (member method tramp-gvfs-methods)))))
564 ;;;###tramp-autoload
565 (defun tramp-gvfs-file-name-handler (operation &rest args)
566 "Invoke the GVFS related OPERATION.
567 First arg specifies the OPERATION, second arg is a list of arguments to
568 pass to the OPERATION."
569 (unless tramp-gvfs-enabled
570 (tramp-compat-user-error nil "Package `tramp-gvfs' not supported"))
571 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
572 (if fn
573 (save-match-data (apply (cdr fn) args))
574 (tramp-run-real-handler operation args))))
576 ;;;###tramp-autoload
577 (when (featurep 'dbusbind)
578 (tramp-register-foreign-file-name-handler
579 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler))
582 ;; D-Bus helper function.
584 (defun tramp-gvfs-dbus-string-to-byte-array (string)
585 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
586 (dbus-string-to-byte-array
587 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
588 (concat string (string 0)) string)))
590 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
591 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists.
592 Return nil for null BYTE-ARRAY."
593 ;; The byte array could be a variant. Take care.
594 (let ((byte-array
595 (if (and (consp byte-array) (atom (car byte-array)))
596 byte-array (car byte-array))))
597 (and byte-array
598 (dbus-byte-array-to-string
599 (if (and (consp byte-array) (zerop (car (last byte-array))))
600 (butlast byte-array) byte-array)))))
602 (defun tramp-gvfs-stringify-dbus-message (message)
603 "Convert a D-Bus message into readable UTF8 strings, used for traces."
604 (cond
605 ((and (consp message) (characterp (car message)))
606 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
607 ((consp message)
608 (mapcar 'tramp-gvfs-stringify-dbus-message message))
609 ((stringp message)
610 (format "%S" message))
611 (t message)))
613 (defmacro with-tramp-dbus-call-method
614 (vec synchronous bus service path interface method &rest args)
615 "Apply a D-Bus call on bus BUS.
617 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
618 it is an asynchronous call, with `ignore' as callback function.
620 The other arguments have the same meaning as with `dbus-call-method'
621 or `dbus-call-method-asynchronously'. Additionally, the call
622 will be traced by Tramp with trace level 6."
623 `(let ((func (if ,synchronous
624 'dbus-call-method 'dbus-call-method-asynchronously))
625 (args (append (list ,bus ,service ,path ,interface ,method)
626 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
627 result)
628 (tramp-message ,vec 6 "%s %s" func args)
629 (setq result (apply func args))
630 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
631 result))
633 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
634 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
635 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
637 (defvar tramp-gvfs-dbus-event-vector nil
638 "Current Tramp file name to be used, as vector.
639 It is needed when D-Bus signals or errors arrive, because there
640 is no information where to trace the message.")
642 (defun tramp-gvfs-dbus-event-error (event err)
643 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
644 (when tramp-gvfs-dbus-event-vector
645 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
646 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
648 ;; `dbus-event-error-hooks' has been renamed to
649 ;; `dbus-event-error-functions' in Emacs 24.3.
650 (add-hook
651 (if (boundp 'dbus-event-error-functions)
652 'dbus-event-error-functions 'dbus-event-error-hooks)
653 'tramp-gvfs-dbus-event-error)
656 ;; File name primitives.
658 (defun tramp-gvfs-do-copy-or-rename-file
659 (op filename newname &optional ok-if-already-exists keep-date
660 preserve-uid-gid preserve-extended-attributes)
661 "Copy or rename a remote file.
662 OP must be `copy' or `rename' and indicates the operation to perform.
663 FILENAME specifies the file to copy or rename, NEWNAME is the name of
664 the new file (for copy) or the new name of the file (for rename).
665 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
666 KEEP-DATE means to make sure that NEWNAME has the same timestamp
667 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
668 the uid and gid if both files are on the same host.
669 PRESERVE-EXTENDED-ATTRIBUTES is ignored.
671 This function is invoked by `tramp-gvfs-handle-copy-file' and
672 `tramp-gvfs-handle-rename-file'. It is an error if OP is neither
673 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
674 file names."
675 (unless (memq op '(copy rename))
676 (error "Unknown operation `%s', must be `copy' or `rename'" op))
678 (if (file-directory-p filename)
679 (progn
680 (copy-directory filename newname keep-date t)
681 (when (eq op 'rename) (delete-directory filename 'recursive)))
683 (let ((t1 (tramp-tramp-file-p filename))
684 (t2 (tramp-tramp-file-p newname))
685 (equal-remote (tramp-equal-remote filename newname))
686 (file-operation (intern (format "%s-file" op)))
687 (gvfs-operation (if (eq op 'copy) "gvfs-copy" "gvfs-move"))
688 (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
690 (with-parsed-tramp-file-name (if t1 filename newname) nil
691 (when (and (not ok-if-already-exists) (file-exists-p newname))
692 (tramp-error v 'file-already-exists newname))
694 (if (or (and equal-remote
695 (tramp-get-connection-property v "direct-copy-failed" nil))
696 (and t1 (not (tramp-gvfs-file-name-p filename)))
697 (and t2 (not (tramp-gvfs-file-name-p newname))))
699 ;; We cannot copy or rename directly.
700 (let ((tmpfile (tramp-compat-make-temp-file filename)))
701 (funcall
702 file-operation filename tmpfile t keep-date preserve-uid-gid
703 preserve-extended-attributes)
704 (rename-file tmpfile newname ok-if-already-exists))
706 ;; Direct action.
707 (with-tramp-progress-reporter
708 v 0 (format "%s %s to %s" msg-operation filename newname)
709 (unless
710 (apply
711 'tramp-gvfs-send-command v gvfs-operation
712 (append
713 (and (eq op 'copy) (or keep-date preserve-uid-gid)
714 '("--preserve"))
715 (list
716 (tramp-gvfs-url-file-name filename)
717 (tramp-gvfs-url-file-name newname))))
719 (if (or (not equal-remote)
720 (and equal-remote
721 (tramp-get-connection-property
722 v "direct-copy-failed" nil)))
723 ;; Propagate the error.
724 (with-current-buffer (tramp-get-connection-buffer v)
725 (goto-char (point-min))
726 (tramp-error-with-buffer
727 nil v 'file-error
728 "%s failed, see buffer `%s' for details."
729 msg-operation (buffer-name)))
731 ;; Some WebDAV server, like the one from QNAP, do not
732 ;; support direct copy/move. Try a fallback.
733 (tramp-set-connection-property v "direct-copy-failed" t)
734 (tramp-gvfs-do-copy-or-rename-file
735 op filename newname ok-if-already-exists keep-date
736 preserve-uid-gid preserve-extended-attributes))))
738 (when (and t1 (eq op 'rename))
739 (with-parsed-tramp-file-name filename nil
740 (tramp-flush-file-property v (file-name-directory localname))
741 (tramp-flush-file-property v localname)))
743 (when t2
744 (with-parsed-tramp-file-name newname nil
745 (tramp-flush-file-property v (file-name-directory localname))
746 (tramp-flush-file-property v localname))))))))
748 (defun tramp-gvfs-handle-copy-file
749 (filename newname &optional ok-if-already-exists keep-date
750 preserve-uid-gid preserve-extended-attributes)
751 "Like `copy-file' for Tramp files."
752 (setq filename (expand-file-name filename))
753 (setq newname (expand-file-name newname))
754 ;; At least one file a Tramp file?
755 (if (or (tramp-tramp-file-p filename)
756 (tramp-tramp-file-p newname))
757 (tramp-gvfs-do-copy-or-rename-file
758 'copy filename newname ok-if-already-exists keep-date
759 preserve-uid-gid preserve-extended-attributes)
760 (tramp-run-real-handler
761 'copy-file
762 (list filename newname ok-if-already-exists keep-date
763 preserve-uid-gid preserve-extended-attributes))))
765 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
766 "Like `delete-directory' for Tramp files."
767 (with-parsed-tramp-file-name directory nil
768 (if (and recursive (not (file-symlink-p directory)))
769 (mapc (lambda (file)
770 (if (eq t (tramp-compat-file-attribute-type
771 (file-attributes file)))
772 (delete-directory file recursive trash)
773 (delete-file file trash)))
774 (directory-files
775 directory 'full directory-files-no-dot-files-regexp))
776 (when (directory-files directory nil directory-files-no-dot-files-regexp)
777 (tramp-error
778 v 'file-error "Couldn't delete non-empty %s" directory)))
780 (tramp-flush-file-property v (file-name-directory localname))
781 (tramp-flush-directory-property v localname)
782 (unless
783 (tramp-gvfs-send-command
784 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
785 (tramp-gvfs-url-file-name directory))
786 ;; Propagate the error.
787 (with-current-buffer (tramp-get-connection-buffer v)
788 (goto-char (point-min))
789 (tramp-error-with-buffer
790 nil v 'file-error "Couldn't delete %s" directory)))))
792 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
793 "Like `delete-file' for Tramp files."
794 (with-parsed-tramp-file-name filename nil
795 (tramp-flush-file-property v (file-name-directory localname))
796 (tramp-flush-file-property v localname)
797 (unless
798 (tramp-gvfs-send-command
799 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
800 (tramp-gvfs-url-file-name filename))
801 ;; Propagate the error.
802 (with-current-buffer (tramp-get-connection-buffer v)
803 (goto-char (point-min))
804 (tramp-error-with-buffer
805 nil v 'file-error "Couldn't delete %s" filename)))))
807 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
808 "Like `expand-file-name' for Tramp files."
809 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
810 (setq dir (or dir default-directory "/"))
811 ;; Unless NAME is absolute, concat DIR and NAME.
812 (unless (file-name-absolute-p name)
813 (setq name (concat (file-name-as-directory dir) name)))
814 ;; If NAME is not a Tramp file, run the real handler.
815 (if (not (tramp-tramp-file-p name))
816 (tramp-run-real-handler 'expand-file-name (list name nil))
817 ;; Dissect NAME.
818 (with-parsed-tramp-file-name name nil
819 ;; If there is a default location, expand tilde.
820 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
821 (save-match-data
822 (tramp-gvfs-maybe-open-connection
823 (make-tramp-file-name
824 :method method :user user :domain domain
825 :host host :port port :localname "/" :hop hop)))
826 (setq localname
827 (replace-match
828 (tramp-get-connection-property v "default-location" "~")
829 nil t localname 1)))
830 ;; Tilde expansion is not possible.
831 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
832 (tramp-error
833 v 'file-error
834 "Cannot expand tilde in file `%s'" name))
835 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
836 (setq localname (concat "/" localname)))
837 ;; We do not pass "/..".
838 (if (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
839 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
840 (setq localname (replace-match "/" t t localname 1)))
841 (when (string-match "^/\\.\\./?" localname)
842 (setq localname (replace-match "/" t t localname))))
843 ;; There might be a double slash. Remove this.
844 (while (string-match "//" localname)
845 (setq localname (replace-match "/" t t localname)))
846 ;; No tilde characters in file name, do normal
847 ;; `expand-file-name' (this does "/./" and "/../").
848 (tramp-make-tramp-file-name
849 method user domain host port
850 (tramp-run-real-handler 'expand-file-name (list localname))))))
852 (defun tramp-gvfs-get-directory-attributes (directory)
853 "Return GVFS attributes association list of all files in DIRECTORY."
854 (ignore-errors
855 ;; Don't modify `last-coding-system-used' by accident.
856 (let ((last-coding-system-used last-coding-system-used)
857 result)
858 (with-parsed-tramp-file-name directory nil
859 (with-tramp-file-property v localname "directory-attributes"
860 (tramp-message v 5 "directory gvfs attributes: %s" localname)
861 ;; Send command.
862 (tramp-gvfs-send-command
863 v "gvfs-ls" "-h" "-n" "-a"
864 (mapconcat 'identity tramp-gvfs-file-attributes ",")
865 (tramp-gvfs-url-file-name directory))
866 ;; Parse output.
867 (with-current-buffer (tramp-get-connection-buffer v)
868 (goto-char (point-min))
869 (while (looking-at
870 (concat "^\\(.+\\)[[:blank:]]"
871 "\\([[:digit:]]+\\)[[:blank:]]"
872 "(\\(.+?\\))"
873 tramp-gvfs-file-attributes-with-gvfs-ls-regexp))
874 (let ((item (list (cons "type" (match-string 3))
875 (cons "standard::size" (match-string 2))
876 (cons "name" (match-string 1)))))
877 (goto-char (1+ (match-end 3)))
878 (while (looking-at
879 (concat
880 tramp-gvfs-file-attributes-with-gvfs-ls-regexp
881 "\\(" tramp-gvfs-file-attributes-with-gvfs-ls-regexp
882 "\\|" "$" "\\)"))
883 (push (cons (match-string 1) (match-string 2)) item)
884 (goto-char (match-end 2)))
885 ;; Add display name as head.
886 (push
887 (cons (cdr (or (assoc "standard::display-name" item)
888 (assoc "name" item)))
889 (nreverse item))
890 result))
891 (forward-line)))
892 result)))))
894 (defun tramp-gvfs-get-root-attributes (filename &optional file-system)
895 "Return GVFS attributes association list of FILENAME.
896 If FILE-SYSTEM is non-nil, return file system attributes."
897 (ignore-errors
898 ;; Don't modify `last-coding-system-used' by accident.
899 (let ((last-coding-system-used last-coding-system-used)
900 result)
901 (with-parsed-tramp-file-name filename nil
902 (with-tramp-file-property
903 v localname
904 (if file-system "file-system-attributes" "file-attributes")
905 (tramp-message
906 v 5 "file%s gvfs attributes: %s"
907 (if file-system " system" "") localname)
908 ;; Send command.
909 (if file-system
910 (tramp-gvfs-send-command
911 v "gvfs-info" "--filesystem" (tramp-gvfs-url-file-name filename))
912 (tramp-gvfs-send-command
913 v "gvfs-info" (tramp-gvfs-url-file-name filename)))
914 ;; Parse output.
915 (with-current-buffer (tramp-get-connection-buffer v)
916 (goto-char (point-min))
917 (while (re-search-forward
918 (if file-system
919 tramp-gvfs-file-system-attributes-regexp
920 tramp-gvfs-file-attributes-with-gvfs-info-regexp)
921 nil t)
922 (push (cons (match-string 1) (match-string 2)) result))
923 result))))))
925 (defun tramp-gvfs-get-file-attributes (filename)
926 "Return GVFS attributes association list of FILENAME."
927 (setq filename (directory-file-name (expand-file-name filename)))
928 (with-parsed-tramp-file-name filename nil
929 (setq localname (tramp-compat-file-name-unquote localname))
930 (if (or (and (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
931 (string-match "^/?\\([^/]+\\)$" localname))
932 (string-equal localname "/"))
933 (tramp-gvfs-get-root-attributes filename)
934 (assoc
935 (file-name-nondirectory filename)
936 (tramp-gvfs-get-directory-attributes (file-name-directory filename))))))
938 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
939 "Like `file-attributes' for Tramp files."
940 (unless id-format (setq id-format 'integer))
941 (ignore-errors
942 (let ((attributes (tramp-gvfs-get-file-attributes filename))
943 dirp res-symlink-target res-numlinks res-uid res-gid res-access
944 res-mod res-change res-size res-filemodes res-inode res-device)
945 (when attributes
946 ;; ... directory or symlink
947 (setq dirp (if (equal "directory" (cdr (assoc "type" attributes))) t))
948 (setq res-symlink-target
949 (cdr (assoc "standard::symlink-target" attributes)))
950 ;; ... number links
951 (setq res-numlinks
952 (string-to-number
953 (or (cdr (assoc "unix::nlink" attributes)) "0")))
954 ;; ... uid and gid
955 (setq res-uid
956 (if (eq id-format 'integer)
957 (string-to-number
958 (or (cdr (assoc "unix::uid" attributes))
959 (format "%s" tramp-unknown-id-integer)))
960 (or (cdr (assoc "owner::user" attributes))
961 (cdr (assoc "unix::uid" attributes))
962 tramp-unknown-id-string)))
963 (setq res-gid
964 (if (eq id-format 'integer)
965 (string-to-number
966 (or (cdr (assoc "unix::gid" attributes))
967 (format "%s" tramp-unknown-id-integer)))
968 (or (cdr (assoc "owner::group" attributes))
969 (cdr (assoc "unix::gid" attributes))
970 tramp-unknown-id-string)))
971 ;; ... last access, modification and change time
972 (setq res-access
973 (seconds-to-time
974 (string-to-number
975 (or (cdr (assoc "time::access" attributes)) "0"))))
976 (setq res-mod
977 (seconds-to-time
978 (string-to-number
979 (or (cdr (assoc "time::modified" attributes)) "0"))))
980 (setq res-change
981 (seconds-to-time
982 (string-to-number
983 (or (cdr (assoc "time::changed" attributes)) "0"))))
984 ;; ... size
985 (setq res-size
986 (string-to-number
987 (or (cdr (assoc "standard::size" attributes)) "0")))
988 ;; ... file mode flags
989 (setq res-filemodes
990 (let ((n (cdr (assoc "unix::mode" attributes))))
991 (if n
992 (tramp-file-mode-from-int (string-to-number n))
993 (format
994 "%s%s%s%s------"
995 (if dirp "d" "-")
996 (if (equal (cdr (assoc "access::can-read" attributes))
997 "FALSE")
998 "-" "r")
999 (if (equal (cdr (assoc "access::can-write" attributes))
1000 "FALSE")
1001 "-" "w")
1002 (if (equal (cdr (assoc "access::can-execute" attributes))
1003 "FALSE")
1004 "-" "x")))))
1005 ;; ... inode and device
1006 (setq res-inode
1007 (let ((n (cdr (assoc "unix::inode" attributes))))
1008 (if n
1009 (string-to-number n)
1010 (tramp-get-inode (tramp-dissect-file-name filename)))))
1011 (setq res-device
1012 (let ((n (cdr (assoc "unix::device" attributes))))
1013 (if n
1014 (string-to-number n)
1015 (tramp-get-device (tramp-dissect-file-name filename)))))
1017 ;; Return data gathered.
1018 (list
1019 ;; 0. t for directory, string (name linked to) for
1020 ;; symbolic link, or nil.
1021 (or dirp res-symlink-target)
1022 ;; 1. Number of links to file.
1023 res-numlinks
1024 ;; 2. File uid.
1025 res-uid
1026 ;; 3. File gid.
1027 res-gid
1028 ;; 4. Last access time, as a list of integers.
1029 ;; 5. Last modification time, likewise.
1030 ;; 6. Last status change time, likewise.
1031 res-access res-mod res-change
1032 ;; 7. Size in bytes (-1, if number is out of range).
1033 res-size
1034 ;; 8. File modes.
1035 res-filemodes
1036 ;; 9. t if file's gid would change if file were deleted
1037 ;; and recreated.
1039 ;; 10. Inode number.
1040 res-inode
1041 ;; 11. Device number.
1042 res-device
1043 )))))
1045 (defun tramp-gvfs-handle-file-directory-p (filename)
1046 "Like `file-directory-p' for Tramp files."
1047 (eq t (tramp-compat-file-attribute-type
1048 (file-attributes (file-truename filename)))))
1050 (defun tramp-gvfs-handle-file-executable-p (filename)
1051 "Like `file-executable-p' for Tramp files."
1052 (with-parsed-tramp-file-name filename nil
1053 (with-tramp-file-property v localname "file-executable-p"
1054 (tramp-check-cached-permissions v ?x))))
1056 (defun tramp-gvfs-handle-file-local-copy (filename)
1057 "Like `file-local-copy' for Tramp files."
1058 (with-parsed-tramp-file-name filename nil
1059 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1060 (unless (file-exists-p filename)
1061 (tramp-error
1062 v tramp-file-missing
1063 "Cannot make local copy of non-existing file `%s'" filename))
1064 (copy-file filename tmpfile 'ok-if-already-exists 'keep-time)
1065 tmpfile)))
1067 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
1068 "Like `file-name-all-completions' for Tramp files."
1069 (unless (save-match-data (string-match "/" filename))
1070 (all-completions
1071 filename
1072 (with-parsed-tramp-file-name (expand-file-name directory) nil
1073 (with-tramp-file-property v localname "file-name-all-completions"
1074 (let ((result '("./" "../")))
1075 ;; Get a list of directories and files.
1076 (dolist (item (tramp-gvfs-get-directory-attributes directory) result)
1077 (if (string-equal (cdr (assoc "type" item)) "directory")
1078 (push (file-name-as-directory (car item)) result)
1079 (push (car item) result)))))))))
1081 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
1082 "Like `file-notify-add-watch' for Tramp files."
1083 (setq file-name (expand-file-name file-name))
1084 (with-parsed-tramp-file-name file-name nil
1085 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1086 ;; supported for gvfs-mounted directories.
1087 (when (file-directory-p file-name)
1088 (tramp-error
1089 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1090 (let* ((default-directory (file-name-directory file-name))
1091 (events
1092 (cond
1093 ((and (memq 'change flags) (memq 'attribute-change flags))
1094 '(created changed changes-done-hint moved deleted
1095 attribute-changed))
1096 ((memq 'change flags)
1097 '(created changed changes-done-hint moved deleted))
1098 ((memq 'attribute-change flags) '(attribute-changed))))
1099 (p (apply
1100 'start-process
1101 "gvfs-monitor" (generate-new-buffer " *gvfs-monitor*")
1102 (if (tramp-gvfs-gio-tool-p v)
1103 `("gio" "monitor" ,(tramp-gvfs-url-file-name file-name)))
1104 `("gvfs-monitor-file" (tramp-gvfs-url-file-name file-name)))))
1105 (if (not (processp p))
1106 (tramp-error
1107 v 'file-notify-error "Monitoring not supported for `%s'" file-name)
1108 (tramp-message
1109 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
1110 (tramp-set-connection-property p "vector" v)
1111 (process-put p 'events events)
1112 (process-put p 'watch-name localname)
1113 (process-put p 'adjust-window-size-function 'ignore)
1114 (set-process-query-on-exit-flag p nil)
1115 (set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
1116 ;; There might be an error if the monitor is not supported.
1117 ;; Give the filter a chance to read the output.
1118 (tramp-accept-process-output p 1)
1119 (unless (process-live-p p)
1120 (tramp-error
1121 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1122 p))))
1124 (defun tramp-gvfs-monitor-file-process-filter (proc string)
1125 "Read output from \"gvfs-monitor-file\" and add corresponding \
1126 file-notify events."
1127 (let* ((rest-string (process-get proc 'rest-string))
1128 (dd (with-current-buffer (process-buffer proc) default-directory))
1129 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
1130 (when rest-string
1131 (tramp-message proc 10 "Previous string:\n%s" rest-string))
1132 (tramp-message proc 6 "%S\n%s" proc string)
1133 (setq string (concat rest-string string)
1134 ;; Attribute change is returned in unused wording.
1135 string (replace-regexp-in-string
1136 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1137 (when (string-match "Monitoring not supported" string)
1138 (delete-process proc))
1140 (while (string-match
1141 (concat "^[\n\r]*"
1142 "File Monitor Event:[\n\r]+"
1143 "File = \\([^\n\r]+\\)[\n\r]+"
1144 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1145 string)
1146 (let ((file (match-string 1 string))
1147 (action (intern-soft
1148 (replace-regexp-in-string
1149 "_" "-" (downcase (match-string 2 string))))))
1150 (setq string (replace-match "" nil nil string))
1151 ;; File names are returned as URL paths. We must convert them.
1152 (when (string-match ddu file)
1153 (setq file (replace-match dd nil nil file)))
1154 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
1155 (setq file
1156 (replace-match
1157 (char-to-string (string-to-number (match-string 1 file) 16))
1158 nil nil file)))
1159 ;; Usually, we would add an Emacs event now. Unfortunately,
1160 ;; `unread-command-events' does not accept several events at
1161 ;; once. Therefore, we apply the callback directly.
1162 (tramp-compat-funcall 'file-notify-callback (list proc action file))))
1164 ;; Save rest of the string.
1165 (when (zerop (length string)) (setq string nil))
1166 (when string (tramp-message proc 10 "Rest string:\n%s" string))
1167 (process-put proc 'rest-string string)))
1169 (defun tramp-gvfs-handle-file-readable-p (filename)
1170 "Like `file-readable-p' for Tramp files."
1171 (with-parsed-tramp-file-name filename nil
1172 (with-tramp-file-property v localname "file-readable-p"
1173 (tramp-check-cached-permissions v ?r))))
1175 (defun tramp-gvfs-handle-file-system-info (filename)
1176 "Like `file-system-info' for Tramp files."
1177 (setq filename (directory-file-name (expand-file-name filename)))
1178 (with-parsed-tramp-file-name filename nil
1179 ;; We don't use cached values.
1180 (tramp-set-file-property v localname "file-system-attributes" 'undef)
1181 (let* ((attr (tramp-gvfs-get-root-attributes filename 'file-system))
1182 (size (cdr (assoc "filesystem::size" attr)))
1183 (used (cdr (assoc "filesystem::used" attr)))
1184 (free (cdr (assoc "filesystem::free" attr))))
1185 (when (and (stringp size) (stringp used) (stringp free))
1186 (list (string-to-number (concat size "e0"))
1187 (- (string-to-number (concat size "e0"))
1188 (string-to-number (concat used "e0")))
1189 (string-to-number (concat free "e0")))))))
1191 (defun tramp-gvfs-handle-file-writable-p (filename)
1192 "Like `file-writable-p' for Tramp files."
1193 (with-parsed-tramp-file-name filename nil
1194 (with-tramp-file-property v localname "file-writable-p"
1195 (if (file-exists-p filename)
1196 (tramp-check-cached-permissions v ?w)
1197 ;; If file doesn't exist, check if directory is writable.
1198 (and (file-directory-p (file-name-directory filename))
1199 (file-writable-p (file-name-directory filename)))))))
1201 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1202 "Like `make-directory' for Tramp files."
1203 (setq dir (directory-file-name (expand-file-name dir)))
1204 (with-parsed-tramp-file-name dir nil
1205 (tramp-flush-file-property v (file-name-directory localname))
1206 (tramp-flush-directory-property v localname)
1207 (save-match-data
1208 (let ((ldir (file-name-directory dir)))
1209 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1210 ;; work robust.
1211 (when (and parents (not (file-directory-p ldir)))
1212 (make-directory ldir parents))
1213 ;; Just do it.
1214 (unless (or (tramp-gvfs-send-command
1215 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1216 (and parents (file-directory-p dir)))
1217 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1219 (defun tramp-gvfs-handle-rename-file
1220 (filename newname &optional ok-if-already-exists)
1221 "Like `rename-file' for Tramp files."
1222 ;; Check if both files are local -- invoke normal rename-file.
1223 ;; Otherwise, use Tramp from local system.
1224 (setq filename (expand-file-name filename))
1225 (setq newname (expand-file-name newname))
1226 ;; At least one file a Tramp file?
1227 (if (or (tramp-tramp-file-p filename)
1228 (tramp-tramp-file-p newname))
1229 (tramp-gvfs-do-copy-or-rename-file
1230 'rename filename newname ok-if-already-exists
1231 'keep-date 'preserve-uid-gid)
1232 (tramp-run-real-handler
1233 'rename-file (list filename newname ok-if-already-exists))))
1235 (defun tramp-gvfs-handle-write-region
1236 (start end filename &optional append visit lockname mustbenew)
1237 "Like `write-region' for Tramp files."
1238 (setq filename (expand-file-name filename))
1239 (with-parsed-tramp-file-name filename nil
1240 (when (and mustbenew (file-exists-p filename)
1241 (or (eq mustbenew 'excl)
1242 (not
1243 (y-or-n-p
1244 (format "File %s exists; overwrite anyway? " filename)))))
1245 (tramp-error v 'file-already-exists filename))
1247 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1248 (when (and append (file-exists-p filename))
1249 (copy-file filename tmpfile 'ok))
1250 ;; We say `no-message' here because we don't want the visited file
1251 ;; modtime data to be clobbered from the temp file. We call
1252 ;; `set-visited-file-modtime' ourselves later on.
1253 (tramp-run-real-handler
1254 'write-region (list start end tmpfile append 'no-message lockname))
1255 (condition-case nil
1256 (rename-file tmpfile filename 'ok-if-already-exists)
1257 (error
1258 (delete-file tmpfile)
1259 (tramp-error
1260 v 'file-error "Couldn't write region to `%s'" filename))))
1262 (tramp-flush-file-property v (file-name-directory localname))
1263 (tramp-flush-file-property v localname)
1265 ;; Set file modification time.
1266 (when (or (eq visit t) (stringp visit))
1267 (set-visited-file-modtime
1268 (tramp-compat-file-attribute-modification-time
1269 (file-attributes filename))))
1271 ;; The end.
1272 (when (or (eq visit t) (null visit) (stringp visit))
1273 (tramp-message v 0 "Wrote %s" filename))
1274 (run-hooks 'tramp-handle-write-region-hook)))
1277 ;; File name conversions.
1279 (defun tramp-gvfs-url-file-name (filename)
1280 "Return FILENAME in URL syntax."
1281 ;; "/" must NOT be hexlified.
1282 (setq filename (tramp-compat-file-name-unquote filename))
1283 (let ((url-unreserved-chars (cons ?/ url-unreserved-chars))
1284 result)
1285 (setq
1286 result
1287 (url-recreate-url
1288 (if (tramp-tramp-file-p filename)
1289 (with-parsed-tramp-file-name filename nil
1290 (when (string-equal "gdrive" method)
1291 (setq method "google-drive"))
1292 (when (and user domain)
1293 (setq user (concat domain ";" user)))
1294 (url-parse-make-urlobj
1295 method (and user (url-hexify-string user))
1296 nil (and host (url-hexify-string host))
1297 (if (stringp port) (string-to-number port) port)
1298 (and localname (url-hexify-string localname)) nil nil t))
1299 (url-parse-make-urlobj
1300 "file" nil nil nil nil
1301 (url-hexify-string (file-truename filename)) nil nil t))))
1302 (when (tramp-tramp-file-p filename)
1303 (with-parsed-tramp-file-name filename nil
1304 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1305 result))
1307 (defun tramp-gvfs-object-path (filename)
1308 "Create a D-Bus object path from FILENAME."
1309 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1311 (defun tramp-gvfs-file-name (object-path)
1312 "Retrieve file name from D-Bus OBJECT-PATH."
1313 (dbus-unescape-from-identifier
1314 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1316 (defun tramp-bluez-address (device)
1317 "Return bluetooth device address from a given bluetooth DEVICE name."
1318 (when (stringp device)
1319 (if (string-match tramp-ipv6-regexp device)
1320 (match-string 0 device)
1321 (cadr (assoc device (tramp-bluez-list-devices))))))
1323 (defun tramp-bluez-device (address)
1324 "Return bluetooth device name from a given bluetooth device ADDRESS.
1325 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1326 (when (stringp address)
1327 (while (string-match "[][]" address)
1328 (setq address (replace-match "" t t address)))
1329 (let (result)
1330 (dolist (item (tramp-bluez-list-devices) result)
1331 (when (string-match address (cadr item))
1332 (setq result (car item)))))))
1335 ;; D-Bus GVFS functions.
1337 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1338 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1339 (let* ((filename
1340 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1341 (pw-prompt
1342 (format
1343 "%s for %s "
1344 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1345 (capitalize (match-string 1 message))
1346 "Password")
1347 filename))
1348 password)
1350 (condition-case nil
1351 (with-parsed-tramp-file-name filename l
1352 (when (and (zerop (length user))
1353 (not
1354 (zerop (logand flags tramp-gvfs-password-need-username))))
1355 (setq user (read-string "User name: ")))
1356 (when (and (zerop (length domain))
1357 (not
1358 (zerop (logand flags tramp-gvfs-password-need-domain))))
1359 (setq domain (read-string "Domain name: ")))
1361 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1362 (unless (tramp-get-connection-property l "first-password-request" nil)
1363 (tramp-clear-passwd l))
1365 ;; Set variables for computing the prompt for reading password.
1366 (setq tramp-current-method l-method
1367 tramp-current-user user
1368 tramp-current-domain l-domain
1369 tramp-current-host l-host
1370 tramp-current-port l-port
1371 password (tramp-read-passwd
1372 (tramp-get-connection-process l) pw-prompt))
1374 ;; Return result.
1375 (if (stringp password)
1376 (list
1377 t ;; password handled.
1378 nil ;; no abort of D-Bus.
1379 password
1380 (tramp-file-name-user l)
1381 domain
1382 nil ;; not anonymous.
1383 0) ;; no password save.
1384 ;; No password provided.
1385 (list nil t "" (tramp-file-name-user l) domain nil 0)))
1387 ;; When QUIT is raised, we shall return this information to D-Bus.
1388 (quit (list nil t "" "" "" nil 0)))))
1390 (defun tramp-gvfs-handler-askquestion (message choices)
1391 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1392 (save-window-excursion
1393 (let ((enable-recursive-minibuffers t)
1394 (use-dialog-box (and use-dialog-box (null noninteractive)))
1395 result)
1397 (with-parsed-tramp-file-name
1398 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1399 (tramp-message v 6 "%S %S" message choices)
1401 (setq result
1402 (condition-case nil
1403 (list
1404 t ;; handled.
1405 nil ;; no abort of D-Bus.
1406 (with-tramp-connection-property
1407 (tramp-get-connection-process v) message
1408 ;; In theory, there can be several choices.
1409 ;; Until now, there is only the question whether
1410 ;; to accept an unknown host signature.
1411 (with-temp-buffer
1412 ;; Preserve message for `progress-reporter'.
1413 (with-temp-message ""
1414 (insert message)
1415 (goto-char (point-max))
1416 (if noninteractive
1417 (message "%s" message)
1418 (pop-to-buffer (current-buffer)))
1419 (if (yes-or-no-p
1420 (concat
1421 (buffer-substring
1422 (line-beginning-position) (point))
1423 " "))
1424 0 1)))))
1426 ;; When QUIT is raised, we shall return this
1427 ;; information to D-Bus.
1428 (quit (list nil t 1))))
1430 (tramp-message v 6 "%s" result)
1432 ;; When the choice is "no", we set a dummy fuse-mountpoint in
1433 ;; order to leave the timeout.
1434 (unless (zerop (cl-caddr result))
1435 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1437 result))))
1439 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1440 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1441 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1442 (ignore-errors
1443 (let ((signal-name (dbus-event-member-name last-input-event))
1444 (elt mount-info))
1445 ;; Jump over the first elements of the mount info. Since there
1446 ;; were changes in the entries, we cannot access dedicated
1447 ;; elements.
1448 (while (stringp (car elt)) (setq elt (cdr elt)))
1449 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1450 (mount-spec (cl-caddr elt))
1451 (default-location (tramp-gvfs-dbus-byte-array-to-string
1452 (cl-cadddr elt)))
1453 (method (tramp-gvfs-dbus-byte-array-to-string
1454 (cadr (assoc "type" (cadr mount-spec)))))
1455 (user (tramp-gvfs-dbus-byte-array-to-string
1456 (cadr (assoc "user" (cadr mount-spec)))))
1457 (domain (tramp-gvfs-dbus-byte-array-to-string
1458 (cadr (assoc "domain" (cadr mount-spec)))))
1459 (host (tramp-gvfs-dbus-byte-array-to-string
1460 (cadr (or (assoc "host" (cadr mount-spec))
1461 (assoc "server" (cadr mount-spec))))))
1462 (port (tramp-gvfs-dbus-byte-array-to-string
1463 (cadr (assoc "port" (cadr mount-spec)))))
1464 (ssl (tramp-gvfs-dbus-byte-array-to-string
1465 (cadr (assoc "ssl" (cadr mount-spec)))))
1466 (prefix (concat
1467 (tramp-gvfs-dbus-byte-array-to-string
1468 (car mount-spec))
1469 (tramp-gvfs-dbus-byte-array-to-string
1470 (or (cadr (assoc "share" (cadr mount-spec)))
1471 (cadr (assoc "volume" (cadr mount-spec))))))))
1472 (when (string-match "^\\(afp\\|smb\\)" method)
1473 (setq method (match-string 1 method)))
1474 (when (string-equal "obex" method)
1475 (setq host (tramp-bluez-device host)))
1476 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1477 (setq method "davs"))
1478 (when (string-equal "google-drive" method)
1479 (setq method "gdrive"))
1480 (with-parsed-tramp-file-name
1481 (tramp-make-tramp-file-name method user domain host port "") nil
1482 (tramp-message
1483 v 6 "%s %s"
1484 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1485 (tramp-set-file-property v "/" "list-mounts" 'undef)
1486 (if (string-equal (downcase signal-name) "unmounted")
1487 (tramp-flush-file-property v "/")
1488 ;; Set prefix, mountpoint and location.
1489 (unless (string-equal prefix "/")
1490 (tramp-set-file-property v "/" "prefix" prefix))
1491 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1492 (tramp-set-connection-property
1493 v "default-location" default-location)))))))
1495 (when tramp-gvfs-enabled
1496 (dbus-register-signal
1497 :session nil tramp-gvfs-path-mounttracker
1498 tramp-gvfs-interface-mounttracker "mounted"
1499 'tramp-gvfs-handler-mounted-unmounted)
1500 (dbus-register-signal
1501 :session nil tramp-gvfs-path-mounttracker
1502 tramp-gvfs-interface-mounttracker "Mounted"
1503 'tramp-gvfs-handler-mounted-unmounted)
1505 (dbus-register-signal
1506 :session nil tramp-gvfs-path-mounttracker
1507 tramp-gvfs-interface-mounttracker "unmounted"
1508 'tramp-gvfs-handler-mounted-unmounted)
1509 (dbus-register-signal
1510 :session nil tramp-gvfs-path-mounttracker
1511 tramp-gvfs-interface-mounttracker "Unmounted"
1512 'tramp-gvfs-handler-mounted-unmounted))
1514 (defun tramp-gvfs-connection-mounted-p (vec)
1515 "Check, whether the location is already mounted."
1517 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1518 (catch 'mounted
1519 (dolist
1520 (elt
1521 (with-tramp-file-property vec "/" "list-mounts"
1522 (with-tramp-dbus-call-method vec t
1523 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1524 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1525 nil)
1526 ;; Jump over the first elements of the mount info. Since there
1527 ;; were changes in the entries, we cannot access dedicated
1528 ;; elements.
1529 (while (stringp (car elt)) (setq elt (cdr elt)))
1530 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1531 (cadr elt)))
1532 (mount-spec (cl-caddr elt))
1533 (default-location (tramp-gvfs-dbus-byte-array-to-string
1534 (cl-cadddr elt)))
1535 (method (tramp-gvfs-dbus-byte-array-to-string
1536 (cadr (assoc "type" (cadr mount-spec)))))
1537 (user (tramp-gvfs-dbus-byte-array-to-string
1538 (cadr (assoc "user" (cadr mount-spec)))))
1539 (domain (tramp-gvfs-dbus-byte-array-to-string
1540 (cadr (assoc "domain" (cadr mount-spec)))))
1541 (host (tramp-gvfs-dbus-byte-array-to-string
1542 (cadr (or (assoc "host" (cadr mount-spec))
1543 (assoc "server" (cadr mount-spec))))))
1544 (port (tramp-gvfs-dbus-byte-array-to-string
1545 (cadr (assoc "port" (cadr mount-spec)))))
1546 (ssl (tramp-gvfs-dbus-byte-array-to-string
1547 (cadr (assoc "ssl" (cadr mount-spec)))))
1548 (prefix (concat
1549 (tramp-gvfs-dbus-byte-array-to-string
1550 (car mount-spec))
1551 (tramp-gvfs-dbus-byte-array-to-string
1553 (cadr (assoc "share" (cadr mount-spec)))
1554 (cadr (assoc "volume" (cadr mount-spec))))))))
1555 (when (string-match "^\\(afp\\|smb\\)" method)
1556 (setq method (match-string 1 method)))
1557 (when (string-equal "obex" method)
1558 (setq host (tramp-bluez-device host)))
1559 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1560 (setq method "davs"))
1561 (when (string-equal "google-drive" method)
1562 (setq method "gdrive"))
1563 (when (and (string-equal "synce" method) (zerop (length user)))
1564 (setq user (or (tramp-file-name-user vec) "")))
1565 (when (and
1566 (string-equal method (tramp-file-name-method vec))
1567 (string-equal user (tramp-file-name-user vec))
1568 (string-equal domain (tramp-file-name-domain vec))
1569 (string-equal host (tramp-file-name-host vec))
1570 (string-equal port (tramp-file-name-port vec))
1571 (string-match (concat "^" (regexp-quote prefix))
1572 (tramp-file-name-unquote-localname vec)))
1573 ;; Set prefix, mountpoint and location.
1574 (unless (string-equal prefix "/")
1575 (tramp-set-file-property vec "/" "prefix" prefix))
1576 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1577 (tramp-set-connection-property
1578 vec "default-location" default-location)
1579 (throw 'mounted t)))))))
1581 (defun tramp-gvfs-mount-spec-entry (key value)
1582 "Construct a mount-spec entry to be used in a mount_spec.
1583 It was \"a(say)\", but has changed to \"a{sv})\"."
1584 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1585 (list :dict-entry key
1586 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1587 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1589 (defun tramp-gvfs-mount-spec (vec)
1590 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1591 (let* ((method (tramp-file-name-method vec))
1592 (user (tramp-file-name-user vec))
1593 (domain (tramp-file-name-domain vec))
1594 (host (tramp-file-name-host vec))
1595 (port (tramp-file-name-port vec))
1596 (localname (tramp-file-name-unquote-localname vec))
1597 (share (when (string-match "^/?\\([^/]+\\)" localname)
1598 (match-string 1 localname)))
1599 (ssl (if (string-match "^davs" method) "true" "false"))
1600 (mount-spec
1601 `(:array
1602 ,@(cond
1603 ((string-equal "smb" method)
1604 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1605 (tramp-gvfs-mount-spec-entry "server" host)
1606 (tramp-gvfs-mount-spec-entry "share" share)))
1607 ((string-equal "obex" method)
1608 (list (tramp-gvfs-mount-spec-entry "type" method)
1609 (tramp-gvfs-mount-spec-entry
1610 "host" (concat "[" (tramp-bluez-address host) "]"))))
1611 ((string-match "\\`dav" method)
1612 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1613 (tramp-gvfs-mount-spec-entry "host" host)
1614 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1615 ((string-equal "afp" method)
1616 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1617 (tramp-gvfs-mount-spec-entry "host" host)
1618 (tramp-gvfs-mount-spec-entry "volume" share)))
1619 ((string-equal "gdrive" method)
1620 (list (tramp-gvfs-mount-spec-entry "type" "google-drive")
1621 (tramp-gvfs-mount-spec-entry "host" host)))
1623 (list (tramp-gvfs-mount-spec-entry "type" method)
1624 (tramp-gvfs-mount-spec-entry "host" host))))
1625 ,@(when user
1626 (list (tramp-gvfs-mount-spec-entry "user" user)))
1627 ,@(when domain
1628 (list (tramp-gvfs-mount-spec-entry "domain" domain)))
1629 ,@(when port
1630 (list (tramp-gvfs-mount-spec-entry "port" port)))))
1631 (mount-pref
1632 (if (and (string-match "\\`dav" method)
1633 (string-match "^/?[^/]+" localname))
1634 (match-string 0 localname)
1635 "/")))
1637 ;; Return.
1638 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1641 ;; Connection functions.
1643 (defun tramp-gvfs-get-remote-uid (vec id-format)
1644 "The uid of the remote connection VEC, in ID-FORMAT.
1645 ID-FORMAT valid values are `string' and `integer'."
1646 (with-tramp-connection-property vec (format "uid-%s" id-format)
1647 (let ((method (tramp-file-name-method vec))
1648 (user (tramp-file-name-user vec))
1649 (domain (tramp-file-name-domain vec))
1650 (host (tramp-file-name-host vec))
1651 (port (tramp-file-name-port vec))
1652 (localname
1653 (tramp-get-connection-property vec "default-location" nil)))
1654 (cond
1655 ((and user (equal id-format 'string)) user)
1656 (localname
1657 (tramp-compat-file-attribute-user-id
1658 (file-attributes
1659 (tramp-make-tramp-file-name method user domain host port localname)
1660 id-format)))
1661 ((equal id-format 'integer) tramp-unknown-id-integer)
1662 ((equal id-format 'string) tramp-unknown-id-string)))))
1664 (defun tramp-gvfs-get-remote-gid (vec id-format)
1665 "The gid of the remote connection VEC, in ID-FORMAT.
1666 ID-FORMAT valid values are `string' and `integer'."
1667 (with-tramp-connection-property vec (format "gid-%s" id-format)
1668 (let ((method (tramp-file-name-method vec))
1669 (user (tramp-file-name-user vec))
1670 (domain (tramp-file-name-domain vec))
1671 (host (tramp-file-name-host vec))
1672 (port (tramp-file-name-port vec))
1673 (localname
1674 (tramp-get-connection-property vec "default-location" nil)))
1675 (cond
1676 (localname
1677 (tramp-compat-file-attribute-group-id
1678 (file-attributes
1679 (tramp-make-tramp-file-name method user domain host port localname)
1680 id-format)))
1681 ((equal id-format 'integer) tramp-unknown-id-integer)
1682 ((equal id-format 'string) tramp-unknown-id-string)))))
1684 (defvar tramp-gvfs-get-remote-uid-gid-in-progress nil
1685 "Indication, that remote uid and gid determination is in progress.")
1687 (defun tramp-gvfs-maybe-open-connection (vec)
1688 "Maybe open a connection VEC.
1689 Does not do anything if a connection is already open, but re-opens the
1690 connection if a previous connection has died for some reason."
1691 ;; We set the file name, in case there are incoming D-Bus signals or
1692 ;; D-Bus errors.
1693 (setq tramp-gvfs-dbus-event-vector vec)
1695 ;; For password handling, we need a process bound to the connection
1696 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1697 ;; better solution?
1698 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1699 (let ((p (make-network-process
1700 :name (tramp-buffer-name vec)
1701 :buffer (tramp-get-connection-buffer vec)
1702 :server t :host 'local :service t :noquery t)))
1703 (set-process-query-on-exit-flag p nil)))
1705 (unless (tramp-gvfs-connection-mounted-p vec)
1706 (let* ((method (tramp-file-name-method vec))
1707 (user (tramp-file-name-user vec))
1708 (domain (tramp-file-name-domain vec))
1709 (host (tramp-file-name-host vec))
1710 (port (tramp-file-name-port vec))
1711 (localname (tramp-file-name-unquote-localname vec))
1712 (object-path
1713 (tramp-gvfs-object-path
1714 (tramp-make-tramp-file-name method user domain host port ""))))
1716 (when (and (string-equal method "afp")
1717 (string-equal localname "/"))
1718 (tramp-error vec 'file-error "Filename must contain an AFP volume"))
1720 (when (and (string-match method "davs?")
1721 (string-equal localname "/"))
1722 (tramp-error vec 'file-error "Filename must contain a WebDAV share"))
1724 (when (and (string-equal method "smb")
1725 (string-equal localname "/"))
1726 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1728 (with-tramp-progress-reporter
1729 vec 3
1730 (if (zerop (length user))
1731 (format "Opening connection for %s using %s" host method)
1732 (format "Opening connection for %s@%s using %s" user host method))
1734 ;; Enable `auth-source'.
1735 (tramp-set-connection-property
1736 vec "first-password-request" tramp-cache-read-persistent-data)
1738 ;; There will be a callback of "askPassword" when a password is needed.
1739 (dbus-register-method
1740 :session dbus-service-emacs object-path
1741 tramp-gvfs-interface-mountoperation "askPassword"
1742 'tramp-gvfs-handler-askpassword)
1743 (dbus-register-method
1744 :session dbus-service-emacs object-path
1745 tramp-gvfs-interface-mountoperation "AskPassword"
1746 'tramp-gvfs-handler-askpassword)
1748 ;; There could be a callback of "askQuestion" when adding fingerprint.
1749 (dbus-register-method
1750 :session dbus-service-emacs object-path
1751 tramp-gvfs-interface-mountoperation "askQuestion"
1752 'tramp-gvfs-handler-askquestion)
1753 (dbus-register-method
1754 :session dbus-service-emacs object-path
1755 tramp-gvfs-interface-mountoperation "AskQuestion"
1756 'tramp-gvfs-handler-askquestion)
1758 ;; The call must be asynchronously, because of the "askPassword"
1759 ;; or "askQuestion" callbacks.
1760 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1761 (with-tramp-dbus-call-method vec nil
1762 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1763 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1764 (tramp-gvfs-mount-spec vec)
1765 `(:struct :string ,(dbus-get-unique-name :session)
1766 :object-path ,object-path))
1767 (with-tramp-dbus-call-method vec nil
1768 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1769 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1770 (tramp-gvfs-mount-spec vec)
1771 :string (dbus-get-unique-name :session) :object-path object-path))
1773 ;; We must wait, until the mount is applied. This will be
1774 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1775 ;; file property.
1776 (with-timeout
1777 ((or (tramp-get-method-parameter vec 'tramp-connection-timeout)
1778 tramp-connection-timeout)
1779 (if (zerop (length (tramp-file-name-user vec)))
1780 (tramp-error
1781 vec 'file-error
1782 "Timeout reached mounting %s using %s" host method)
1783 (tramp-error
1784 vec 'file-error
1785 "Timeout reached mounting %s@%s using %s" user host method)))
1786 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1787 (read-event nil nil 0.1)))
1789 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1790 ;; is marked with the fuse-mountpoint "/". We shall react.
1791 (when (string-equal
1792 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1793 (tramp-error vec 'file-error "FUSE mount denied"))
1795 ;; Set connection-local variables.
1796 (tramp-set-connection-local-variables vec)
1798 ;; Mark it as connected.
1799 (tramp-set-connection-property
1800 (tramp-get-connection-process vec) "connected" t))))
1802 ;; In `tramp-check-cached-permissions', the connection properties
1803 ;; {uig,gid}-{integer,string} are used. We set them to proper values.
1804 (unless tramp-gvfs-get-remote-uid-gid-in-progress
1805 (let ((tramp-gvfs-get-remote-uid-gid-in-progress t))
1806 (tramp-gvfs-get-remote-uid vec 'integer)
1807 (tramp-gvfs-get-remote-gid vec 'integer)
1808 (tramp-gvfs-get-remote-uid vec 'string)
1809 (tramp-gvfs-get-remote-gid vec 'string))))
1811 (defun tramp-gvfs-gio-tool-p (vec)
1812 "Check, whether the gio tool is available."
1813 (with-tramp-connection-property vec "gio-tool"
1814 (zerop (tramp-call-process vec "gio" nil nil nil "version"))))
1816 (defun tramp-gvfs-send-command (vec command &rest args)
1817 "Send the COMMAND with its ARGS to connection VEC.
1818 COMMAND is a command from the gvfs-* utilities. It is replaced
1819 by the corresponding gio tool call if available. `call-process'
1820 is applied, and it returns t if the return code is zero."
1821 (let* ((locale (tramp-get-local-locale vec))
1822 (process-environment
1823 (append
1824 `(,(format "LANG=%s" locale)
1825 ,(format "LANGUAGE=%s" locale)
1826 ,(format "LC_ALL=%s" locale))
1827 process-environment)))
1828 (when (tramp-gvfs-gio-tool-p vec)
1829 ;; Use gio tool.
1830 (setq args (cons (cdr (assoc command tramp-gvfs-gio-mapping)) args)
1831 command "gio"))
1833 (with-current-buffer (tramp-get-connection-buffer vec)
1834 (tramp-gvfs-maybe-open-connection vec)
1835 (erase-buffer)
1836 (or (zerop (apply 'tramp-call-process vec command nil t nil args))
1837 ;; Remove information about mounted connection.
1838 (and (tramp-flush-file-property vec "/") nil)))))
1841 ;; D-Bus BLUEZ functions.
1843 (defun tramp-bluez-list-devices ()
1844 "Return all discovered bluetooth devices as list.
1845 Every entry is a list (NAME ADDRESS).
1847 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1848 discovery happened more time before indicated there, a rescan will be
1849 started, which lasts some ten seconds. Otherwise, cached results will
1850 be used."
1851 ;; Reset the scanned devices list if time has passed.
1852 (and (integerp tramp-bluez-discover-devices-timeout)
1853 (integerp tramp-bluez-discovery)
1854 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1855 tramp-bluez-discover-devices-timeout)
1856 (setq tramp-bluez-devices nil))
1858 ;; Rescan if needed.
1859 (unless tramp-bluez-devices
1860 (let ((object-path
1861 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1862 :system tramp-bluez-service "/"
1863 tramp-bluez-interface-manager "DefaultAdapter")))
1864 (setq tramp-bluez-devices nil
1865 tramp-bluez-discovery t)
1866 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1867 :system tramp-bluez-service object-path
1868 tramp-bluez-interface-adapter "StartDiscovery")
1869 (while tramp-bluez-discovery
1870 (read-event nil nil 0.1))))
1871 (setq tramp-bluez-discovery (current-time))
1872 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1873 tramp-bluez-devices)
1875 (defun tramp-bluez-property-changed (property value)
1876 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1877 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1878 (cond
1879 ((string-equal property "Discovering")
1880 (unless (car value)
1881 ;; "Discovering" FALSE means discovery run has been completed.
1882 ;; We stop it, because we don't need another run.
1883 (setq tramp-bluez-discovery nil)
1884 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1885 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1886 tramp-bluez-interface-adapter "StopDiscovery")))))
1888 (when tramp-gvfs-enabled
1889 (dbus-register-signal
1890 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1891 'tramp-bluez-property-changed))
1893 (defun tramp-bluez-device-found (device args)
1894 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1895 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1896 (let ((alias (car (cadr (assoc "Alias" args))))
1897 (address (car (cadr (assoc "Address" args)))))
1898 ;; Maybe we shall check the device class for being a proper
1899 ;; device, and call also SDP in order to find the obex service.
1900 (add-to-list 'tramp-bluez-devices (list alias address))))
1902 (when tramp-gvfs-enabled
1903 (dbus-register-signal
1904 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1905 'tramp-bluez-device-found))
1907 (defun tramp-bluez-parse-device-names (_ignore)
1908 "Return a list of (nil host) tuples allowed to access."
1909 (mapcar
1910 (lambda (x) (list nil (car x)))
1911 (tramp-bluez-list-devices)))
1913 ;; Add completion function for OBEX method.
1914 (when (and tramp-gvfs-enabled
1915 (member tramp-bluez-service (dbus-list-known-names :system)))
1916 (tramp-set-completion-function
1917 "obex" '((tramp-bluez-parse-device-names ""))))
1920 ;; D-Bus zeroconf functions.
1922 (defun tramp-zeroconf-parse-device-names (service)
1923 "Return a list of (user host) tuples allowed to access."
1924 (mapcar
1925 (lambda (x)
1926 (let ((host (zeroconf-service-host x))
1927 (port (zeroconf-service-port x))
1928 (text (zeroconf-service-txt x))
1929 user)
1930 (when port
1931 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1932 ;; A user is marked in a TXT field like "u=guest".
1933 (while text
1934 (when (string-match "u=\\(.+\\)$" (car text))
1935 (setq user (match-string 1 (car text))))
1936 (setq text (cdr text)))
1937 (list user host)))
1938 (zeroconf-list-services service)))
1940 ;; We use the TRIM argument of `split-string', which exist since Emacs
1941 ;; 24.4. I mask this for older Emacs versions, there is no harm.
1942 (defun tramp-gvfs-parse-device-names (service)
1943 "Return a list of (user host) tuples allowed to access.
1944 This uses \"avahi-browse\" in case D-Bus is not enabled in Avahi."
1945 (let ((result
1946 (ignore-errors
1947 (tramp-compat-funcall
1948 'split-string
1949 (shell-command-to-string (format "avahi-browse -trkp %s" service))
1950 "[\n\r]+" 'omit "^\\+;.*$"))))
1951 (delete-dups
1952 (mapcar
1953 (lambda (x)
1954 (let* ((list (split-string x ";"))
1955 (host (nth 6 list))
1956 (text (tramp-compat-funcall
1957 'split-string (nth 9 list) "\" \"" 'omit "\""))
1958 user)
1959 ;; A user is marked in a TXT field like "u=guest".
1960 (while text
1961 (when (string-match "u=\\(.+\\)$" (car text))
1962 (setq user (match-string 1 (car text))))
1963 (setq text (cdr text)))
1964 (list user host)))
1965 result))))
1967 ;; Add completion functions for AFP, DAV, DAVS, SFTP and SMB methods.
1968 (when tramp-gvfs-enabled
1969 ;; Suppress D-Bus error messages.
1970 (let (tramp-gvfs-dbus-event-vector)
1971 (zeroconf-init tramp-gvfs-zeroconf-domain)
1972 (if (zeroconf-list-service-types)
1973 (progn
1974 (tramp-set-completion-function
1975 "afp" '((tramp-zeroconf-parse-device-names "_afpovertcp._tcp")))
1976 (tramp-set-completion-function
1977 "dav" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1978 (tramp-set-completion-function
1979 "davs" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1980 (tramp-set-completion-function
1981 "sftp" '((tramp-zeroconf-parse-device-names "_ssh._tcp")
1982 (tramp-zeroconf-parse-device-names "_workstation._tcp")))
1983 (when (member "smb" tramp-gvfs-methods)
1984 (tramp-set-completion-function
1985 "smb" '((tramp-zeroconf-parse-device-names "_smb._tcp")))))
1987 (when (executable-find "avahi-browse")
1988 (tramp-set-completion-function
1989 "afp" '((tramp-gvfs-parse-device-names "_afpovertcp._tcp")))
1990 (tramp-set-completion-function
1991 "dav" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1992 (tramp-set-completion-function
1993 "davs" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1994 (tramp-set-completion-function
1995 "sftp" '((tramp-gvfs-parse-device-names "_ssh._tcp")
1996 (tramp-gvfs-parse-device-names "_workstation._tcp")))
1997 (when (member "smb" tramp-gvfs-methods)
1998 (tramp-set-completion-function
1999 "smb" '((tramp-gvfs-parse-device-names "_smb._tcp"))))))))
2002 ;; D-Bus SYNCE functions.
2004 (defun tramp-synce-list-devices ()
2005 "Return all discovered synce devices as list.
2006 They are retrieved from the hal daemon."
2007 (let (tramp-synce-devices)
2008 (dolist (device
2009 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
2010 :system tramp-hal-service tramp-hal-path-manager
2011 tramp-hal-interface-manager "GetAllDevices"))
2012 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
2013 :system tramp-hal-service device tramp-hal-interface-device
2014 "PropertyExists" "sync.plugin")
2015 (let ((prop
2016 (with-tramp-dbus-call-method
2017 tramp-gvfs-dbus-event-vector t
2018 :system tramp-hal-service device tramp-hal-interface-device
2019 "GetPropertyString" "pda.pocketpc.name")))
2020 (unless (member prop tramp-synce-devices)
2021 (push prop tramp-synce-devices)))))
2022 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
2023 tramp-synce-devices))
2025 (defun tramp-synce-parse-device-names (_ignore)
2026 "Return a list of (nil host) tuples allowed to access."
2027 (mapcar
2028 (lambda (x) (list nil x))
2029 (tramp-synce-list-devices)))
2031 ;; Add completion function for SYNCE method.
2032 (when tramp-gvfs-enabled
2033 (tramp-set-completion-function
2034 "synce" '((tramp-synce-parse-device-names ""))))
2036 (add-hook 'tramp-unload-hook
2037 (lambda ()
2038 (unload-feature 'tramp-gvfs 'force)))
2040 (provide 'tramp-gvfs)
2042 ;;; TODO:
2044 ;; * Host name completion for existing mount points (afp-server,
2045 ;; smb-server) or via smb-network.
2047 ;; * Check, how two shares of the same SMB server can be mounted in
2048 ;; parallel.
2050 ;; * Apply SDP on bluetooth devices, in order to filter out obex
2051 ;; capability.
2053 ;; * Implement obex for other serial communication but bluetooth.
2055 ;;; tramp-gvfs.el ends here