Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[emacs.git] / lisp / net / tramp-gvfs.el
blob82abf542c5e01b54deecb3b25f077cbcf5022afc
1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon
3 ;; Copyright (C) 2009-2016 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 <http://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 23.1 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 'cl)
110 (require 'custom))
112 ;;;###tramp-autoload
113 (defcustom tramp-gvfs-methods
114 '("afp" "dav" "davs" "gdrive" "obex" "sftp" "synce")
115 "List of methods for remote files, accessed with GVFS."
116 :group 'tramp
117 :version "25.2"
118 :type '(repeat (choice (const "afp")
119 (const "dav")
120 (const "davs")
121 (const "ftp")
122 (const "gdrive")
123 (const "obex")
124 (const "sftp")
125 (const "smb")
126 (const "synce")))
127 :require 'tramp)
129 ;; Add defaults for `tramp-default-user-alist' and `tramp-default-host-alist'.
130 ;;;###tramp-autoload
131 (when (string-match "\\(.+\\)@\\(\\(?:gmail\\|googlemail\\)\\.com\\)"
132 user-mail-address)
133 (add-to-list 'tramp-default-user-alist
134 `("\\`gdrive\\'" nil ,(match-string 1 user-mail-address)))
135 (add-to-list 'tramp-default-host-alist
136 '("\\`gdrive\\'" nil ,(match-string 2 user-mail-address))))
137 ;;;###tramp-autoload
138 (add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
140 ;;;###tramp-autoload
141 (defcustom tramp-gvfs-zeroconf-domain "local"
142 "Zeroconf domain to be used for discovering services, like host names."
143 :group 'tramp
144 :version "23.2"
145 :type 'string
146 :require 'tramp)
148 ;; Add the methods to `tramp-methods', in order to allow minibuffer
149 ;; completion.
150 ;;;###tramp-autoload
151 (when (featurep 'dbusbind)
152 (dolist (elt tramp-gvfs-methods)
153 (unless (assoc elt tramp-methods)
154 (add-to-list 'tramp-methods (cons elt nil)))))
156 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
157 "The preceding object path for own objects.")
159 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
160 "The well known name of the GVFS daemon.")
162 ;; D-Bus integration is available since Emacs 23 on some system types.
163 ;; We don't call `dbus-ping', because this would load dbus.el.
164 (defconst tramp-gvfs-enabled
165 (ignore-errors
166 (and (featurep 'dbusbind)
167 (tramp-compat-funcall 'dbus-get-unique-name :system)
168 (tramp-compat-funcall 'dbus-get-unique-name :session)
169 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
170 (tramp-compat-process-running-p "gvfsd-fuse"))))
171 "Non-nil when GVFS is available.")
173 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
174 "The object path of the GVFS daemon.")
176 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
177 "The mount tracking interface in the GVFS daemon.")
179 ;; Introspection data exist since GVFS 1.14. If there are no such
180 ;; data, we expect an earlier interface.
181 (defconst tramp-gvfs-methods-mounttracker
182 (and tramp-gvfs-enabled
183 (dbus-introspect-get-method-names
184 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
185 tramp-gvfs-interface-mounttracker))
186 "The list of supported methods of the mount tracking interface.")
188 (defconst tramp-gvfs-listmounts
189 (if (member "ListMounts" tramp-gvfs-methods-mounttracker)
190 "ListMounts"
191 "listMounts")
192 "The name of the \"listMounts\" method.
193 It has been changed in GVFS 1.14.")
195 (defconst tramp-gvfs-mountlocation
196 (if (member "MountLocation" tramp-gvfs-methods-mounttracker)
197 "MountLocation"
198 "mountLocation")
199 "The name of the \"mountLocation\" method.
200 It has been changed in GVFS 1.14.")
202 (defconst tramp-gvfs-mountlocation-signature
203 (and tramp-gvfs-enabled
204 (dbus-introspect-get-signature
205 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
206 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation))
207 "The D-Bus signature of the \"mountLocation\" method.
208 It has been changed in GVFS 1.14.")
210 ;; <interface name='org.gtk.vfs.MountTracker'>
211 ;; <method name='listMounts'>
212 ;; <arg name='mount_info_list'
213 ;; type='a{sosssssbay{aya{say}}ay}'
214 ;; direction='out'/>
215 ;; </method>
216 ;; <method name='mountLocation'>
217 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
218 ;; <arg name='dbus_id' type='s' direction='in'/>
219 ;; <arg name='object_path' type='o' direction='in'/>
220 ;; </method>
221 ;; <signal name='mounted'>
222 ;; <arg name='mount_info'
223 ;; type='{sosssssbay{aya{say}}ay}'/>
224 ;; </signal>
225 ;; <signal name='unmounted'>
226 ;; <arg name='mount_info'
227 ;; type='{sosssssbay{aya{say}}ay}'/>
228 ;; </signal>
229 ;; </interface>
231 ;; STRUCT mount_info
232 ;; STRING dbus_id
233 ;; OBJECT_PATH object_path
234 ;; STRING display_name
235 ;; STRING stable_name
236 ;; STRING x_content_types Since GVFS 1.0 only !!!
237 ;; STRING icon
238 ;; STRING preferred_filename_encoding
239 ;; BOOLEAN user_visible
240 ;; ARRAY BYTE fuse_mountpoint
241 ;; STRUCT mount_spec
242 ;; ARRAY BYTE mount_prefix
243 ;; ARRAY
244 ;; STRUCT mount_spec_item
245 ;; STRING key (type, user, domain, host, server,
246 ;; share, volume, port, ssl)
247 ;; ARRAY BYTE value
248 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
250 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
251 "Used by the dbus-proxying implementation of GMountOperation.")
253 ;; <interface name='org.gtk.vfs.MountOperation'>
254 ;; <method name='askPassword'>
255 ;; <arg name='message' type='s' direction='in'/>
256 ;; <arg name='default_user' type='s' direction='in'/>
257 ;; <arg name='default_domain' type='s' direction='in'/>
258 ;; <arg name='flags' type='u' direction='in'/>
259 ;; <arg name='handled' type='b' direction='out'/>
260 ;; <arg name='aborted' type='b' direction='out'/>
261 ;; <arg name='password' type='s' direction='out'/>
262 ;; <arg name='username' type='s' direction='out'/>
263 ;; <arg name='domain' type='s' direction='out'/>
264 ;; <arg name='anonymous' type='b' direction='out'/>
265 ;; <arg name='password_save' type='u' direction='out'/>
266 ;; </method>
267 ;; <method name='askQuestion'>
268 ;; <arg name='message' type='s' direction='in'/>
269 ;; <arg name='choices' type='as' direction='in'/>
270 ;; <arg name='handled' type='b' direction='out'/>
271 ;; <arg name='aborted' type='b' direction='out'/>
272 ;; <arg name='choice' type='u' direction='out'/>
273 ;; </method>
274 ;; </interface>
276 ;; The following flags are used in "askPassword". They are defined in
277 ;; /usr/include/glib-2.0/gio/gioenums.h.
279 (defconst tramp-gvfs-password-need-password 1
280 "Operation requires a password.")
282 (defconst tramp-gvfs-password-need-username 2
283 "Operation requires a username.")
285 (defconst tramp-gvfs-password-need-domain 4
286 "Operation requires a domain.")
288 (defconst tramp-gvfs-password-saving-supported 8
289 "Operation supports saving settings.")
291 (defconst tramp-gvfs-password-anonymous-supported 16
292 "Operation supports anonymous users.")
294 (defconst tramp-bluez-service "org.bluez"
295 "The well known name of the BLUEZ service.")
297 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
298 "The manager interface of the BLUEZ daemon.")
300 ;; <interface name='org.bluez.Manager'>
301 ;; <method name='DefaultAdapter'>
302 ;; <arg type='o' direction='out'/>
303 ;; </method>
304 ;; <method name='FindAdapter'>
305 ;; <arg type='s' direction='in'/>
306 ;; <arg type='o' direction='out'/>
307 ;; </method>
308 ;; <method name='ListAdapters'>
309 ;; <arg type='ao' direction='out'/>
310 ;; </method>
311 ;; <signal name='AdapterAdded'>
312 ;; <arg type='o'/>
313 ;; </signal>
314 ;; <signal name='AdapterRemoved'>
315 ;; <arg type='o'/>
316 ;; </signal>
317 ;; <signal name='DefaultAdapterChanged'>
318 ;; <arg type='o'/>
319 ;; </signal>
320 ;; </interface>
322 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
323 "The adapter interface of the BLUEZ daemon.")
325 ;; <interface name='org.bluez.Adapter'>
326 ;; <method name='GetProperties'>
327 ;; <arg type='a{sv}' direction='out'/>
328 ;; </method>
329 ;; <method name='SetProperty'>
330 ;; <arg type='s' direction='in'/>
331 ;; <arg type='v' direction='in'/>
332 ;; </method>
333 ;; <method name='RequestMode'>
334 ;; <arg type='s' direction='in'/>
335 ;; </method>
336 ;; <method name='ReleaseMode'/>
337 ;; <method name='RequestSession'/>
338 ;; <method name='ReleaseSession'/>
339 ;; <method name='StartDiscovery'/>
340 ;; <method name='StopDiscovery'/>
341 ;; <method name='ListDevices'>
342 ;; <arg type='ao' direction='out'/>
343 ;; </method>
344 ;; <method name='CreateDevice'>
345 ;; <arg type='s' direction='in'/>
346 ;; <arg type='o' direction='out'/>
347 ;; </method>
348 ;; <method name='CreatePairedDevice'>
349 ;; <arg type='s' direction='in'/>
350 ;; <arg type='o' direction='in'/>
351 ;; <arg type='s' direction='in'/>
352 ;; <arg type='o' direction='out'/>
353 ;; </method>
354 ;; <method name='CancelDeviceCreation'>
355 ;; <arg type='s' direction='in'/>
356 ;; </method>
357 ;; <method name='RemoveDevice'>
358 ;; <arg type='o' direction='in'/>
359 ;; </method>
360 ;; <method name='FindDevice'>
361 ;; <arg type='s' direction='in'/>
362 ;; <arg type='o' direction='out'/>
363 ;; </method>
364 ;; <method name='RegisterAgent'>
365 ;; <arg type='o' direction='in'/>
366 ;; <arg type='s' direction='in'/>
367 ;; </method>
368 ;; <method name='UnregisterAgent'>
369 ;; <arg type='o' direction='in'/>
370 ;; </method>
371 ;; <signal name='DeviceCreated'>
372 ;; <arg type='o'/>
373 ;; </signal>
374 ;; <signal name='DeviceRemoved'>
375 ;; <arg type='o'/>
376 ;; </signal>
377 ;; <signal name='DeviceFound'>
378 ;; <arg type='s'/>
379 ;; <arg type='a{sv}'/>
380 ;; </signal>
381 ;; <signal name='PropertyChanged'>
382 ;; <arg type='s'/>
383 ;; <arg type='v'/>
384 ;; </signal>
385 ;; <signal name='DeviceDisappeared'>
386 ;; <arg type='s'/>
387 ;; </signal>
388 ;; </interface>
390 ;;;###tramp-autoload
391 (defcustom tramp-bluez-discover-devices-timeout 60
392 "Defines seconds since last bluetooth device discovery before rescanning.
393 A value of 0 would require an immediate discovery during hostname
394 completion, nil means to use always cached values for discovered
395 devices."
396 :group 'tramp
397 :version "23.2"
398 :type '(choice (const nil) integer)
399 :require 'tramp)
401 (defvar tramp-bluez-discovery nil
402 "Indicator for a running bluetooth device discovery.
403 It keeps the timestamp of last discovery.")
405 (defvar tramp-bluez-devices nil
406 "Alist of detected bluetooth devices.
407 Every entry is a list (NAME ADDRESS).")
409 (defconst tramp-hal-service "org.freedesktop.Hal"
410 "The well known name of the HAL service.")
412 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
413 "The object path of the HAL daemon manager.")
415 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
416 "The manager interface of the HAL daemon.")
418 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
419 "The device interface of the HAL daemon.")
421 (defconst tramp-gvfs-file-attributes
422 '("name"
423 "type"
424 "standard::display-name"
425 "standard::symlink-target"
426 "unix::nlink"
427 "unix::uid"
428 "owner::user"
429 "unix::gid"
430 "owner::group"
431 "time::access"
432 "time::modified"
433 "time::changed"
434 "standard::size"
435 "unix::mode"
436 "access::can-read"
437 "access::can-write"
438 "access::can-execute"
439 "unix::inode"
440 "unix::device")
441 "GVFS file attributes.")
443 (defconst tramp-gvfs-file-attributes-with-gvfs-ls-regexp
444 (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t) "=\\(.+?\\)")
445 "Regexp to parse GVFS file attributes with `gvfs-ls'.")
447 (defconst tramp-gvfs-file-attributes-with-gvfs-info-regexp
448 (concat "^[[:blank:]]*"
449 (regexp-opt tramp-gvfs-file-attributes t)
450 ":[[:blank:]]+\\(.*\\)$")
451 "Regexp to parse GVFS file attributes with `gvfs-info'.")
454 ;; New handlers should be added here.
455 (defconst tramp-gvfs-file-name-handler-alist
456 '((access-file . ignore)
457 (add-name-to-file . tramp-gvfs-handle-copy-file)
458 ;; `byte-compiler-base-file-name' performed by default handler.
459 ;; `copy-directory' performed by default handler.
460 (copy-file . tramp-gvfs-handle-copy-file)
461 (delete-directory . tramp-gvfs-handle-delete-directory)
462 (delete-file . tramp-gvfs-handle-delete-file)
463 ;; `diff-latest-backup-file' performed by default handler.
464 (directory-file-name . tramp-handle-directory-file-name)
465 (directory-files . tramp-handle-directory-files)
466 (directory-files-and-attributes
467 . tramp-handle-directory-files-and-attributes)
468 (dired-compress-file . ignore)
469 (dired-uncache . tramp-handle-dired-uncache)
470 (expand-file-name . tramp-gvfs-handle-expand-file-name)
471 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
472 (file-acl . ignore)
473 (file-attributes . tramp-gvfs-handle-file-attributes)
474 (file-directory-p . tramp-gvfs-handle-file-directory-p)
475 (file-equal-p . tramp-handle-file-equal-p)
476 (file-executable-p . tramp-gvfs-handle-file-executable-p)
477 (file-exists-p . tramp-handle-file-exists-p)
478 (file-in-directory-p . tramp-handle-file-in-directory-p)
479 (file-local-copy . tramp-gvfs-handle-file-local-copy)
480 (file-modes . tramp-handle-file-modes)
481 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
482 (file-name-as-directory . tramp-handle-file-name-as-directory)
483 (file-name-completion . tramp-handle-file-name-completion)
484 (file-name-directory . tramp-handle-file-name-directory)
485 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
486 ;; `file-name-sans-versions' performed by default handler.
487 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
488 (file-notify-add-watch . tramp-gvfs-handle-file-notify-add-watch)
489 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
490 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
491 (file-ownership-preserved-p . ignore)
492 (file-readable-p . tramp-gvfs-handle-file-readable-p)
493 (file-regular-p . tramp-handle-file-regular-p)
494 (file-remote-p . tramp-handle-file-remote-p)
495 (file-selinux-context . ignore)
496 (file-symlink-p . tramp-handle-file-symlink-p)
497 ;; `file-truename' performed by default handler.
498 (file-writable-p . tramp-gvfs-handle-file-writable-p)
499 (find-backup-file-name . tramp-handle-find-backup-file-name)
500 ;; `find-file-noselect' performed by default handler.
501 ;; `get-file-buffer' performed by default handler.
502 (insert-directory . tramp-handle-insert-directory)
503 (insert-file-contents . tramp-handle-insert-file-contents)
504 (load . tramp-handle-load)
505 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
506 (make-directory . tramp-gvfs-handle-make-directory)
507 (make-directory-internal . ignore)
508 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
509 (make-symbolic-link . tramp-handle-make-symbolic-link)
510 (process-file . ignore)
511 (rename-file . tramp-gvfs-handle-rename-file)
512 (set-file-acl . ignore)
513 (set-file-modes . ignore)
514 (set-file-selinux-context . ignore)
515 (set-file-times . ignore)
516 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
517 (shell-command . ignore)
518 (start-file-process . ignore)
519 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
520 (temporary-file-directory . tramp-handle-temporary-file-directory)
521 (unhandled-file-name-directory . ignore)
522 (vc-registered . ignore)
523 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
524 (write-region . tramp-gvfs-handle-write-region))
525 "Alist of handler functions for Tramp GVFS method.
526 Operations not mentioned here will be handled by the default Emacs primitives.")
528 ;; It must be a `defsubst' in order to push the whole code into
529 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
530 ;;;###tramp-autoload
531 (defsubst tramp-gvfs-file-name-p (filename)
532 "Check if it's a filename handled by the GVFS daemon."
533 (and (tramp-tramp-file-p filename)
534 (let ((method
535 (tramp-file-name-method (tramp-dissect-file-name filename))))
536 (and (stringp method) (member method tramp-gvfs-methods)))))
538 ;;;###tramp-autoload
539 (defun tramp-gvfs-file-name-handler (operation &rest args)
540 "Invoke the GVFS related OPERATION.
541 First arg specifies the OPERATION, second arg is a list of arguments to
542 pass to the OPERATION."
543 (unless tramp-gvfs-enabled
544 (tramp-user-error nil "Package `tramp-gvfs' not supported"))
545 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
546 (if fn
547 (save-match-data (apply (cdr fn) args))
548 (tramp-run-real-handler operation args))))
550 ;; This might be moved to tramp.el. It shall be the first file name
551 ;; handler.
552 ;;;###tramp-autoload
553 (when (featurep 'dbusbind)
554 (add-to-list 'tramp-foreign-file-name-handler-alist
555 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
558 ;; D-Bus helper function.
560 (defun tramp-gvfs-dbus-string-to-byte-array (string)
561 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
562 (dbus-string-to-byte-array
563 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
564 (concat string (string 0)) string)))
566 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
567 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
568 ;; The byte array could be a variant. Take care.
569 (let ((byte-array
570 (if (and (consp byte-array) (atom (car byte-array)))
571 byte-array (car byte-array))))
572 (dbus-byte-array-to-string
573 (if (and (consp byte-array) (zerop (car (last byte-array))))
574 (butlast byte-array) byte-array))))
576 (defun tramp-gvfs-stringify-dbus-message (message)
577 "Convert a D-Bus message into readable UTF8 strings, used for traces."
578 (cond
579 ((and (consp message) (characterp (car message)))
580 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
581 ((consp message)
582 (mapcar 'tramp-gvfs-stringify-dbus-message message))
583 ((stringp message)
584 (format "%S" message))
585 (t message)))
587 (defmacro with-tramp-dbus-call-method
588 (vec synchronous bus service path interface method &rest args)
589 "Apply a D-Bus call on bus BUS.
591 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
592 it is an asynchronous call, with `ignore' as callback function.
594 The other arguments have the same meaning as with `dbus-call-method'
595 or `dbus-call-method-asynchronously'. Additionally, the call
596 will be traced by Tramp with trace level 6."
597 `(let ((func (if ,synchronous
598 'dbus-call-method 'dbus-call-method-asynchronously))
599 (args (append (list ,bus ,service ,path ,interface ,method)
600 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
601 result)
602 (tramp-message ,vec 6 "%s %s" func args)
603 (setq result (apply func args))
604 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
605 result))
607 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
608 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
609 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
611 (defvar tramp-gvfs-dbus-event-vector nil
612 "Current Tramp file name to be used, as vector.
613 It is needed when D-Bus signals or errors arrive, because there
614 is no information where to trace the message.")
616 (defun tramp-gvfs-dbus-event-error (event err)
617 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
618 (when tramp-gvfs-dbus-event-vector
619 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
620 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
622 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
623 (add-hook
624 (if (boundp 'dbus-event-error-functions)
625 'dbus-event-error-functions 'dbus-event-error-hooks)
626 'tramp-gvfs-dbus-event-error)
629 ;; File name primitives.
631 (defun tramp-gvfs-do-copy-or-rename-file
632 (op filename newname &optional ok-if-already-exists keep-date
633 preserve-uid-gid preserve-extended-attributes)
634 "Copy or rename a remote file.
635 OP must be `copy' or `rename' and indicates the operation to perform.
636 FILENAME specifies the file to copy or rename, NEWNAME is the name of
637 the new file (for copy) or the new name of the file (for rename).
638 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
639 KEEP-DATE means to make sure that NEWNAME has the same timestamp
640 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
641 the uid and gid if both files are on the same host.
642 PRESERVE-EXTENDED-ATTRIBUTES is ignored.
644 This function is invoked by `tramp-gvfs-handle-copy-file' and
645 `tramp-gvfs-handle-rename-file'. It is an error if OP is neither
646 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
647 file names."
648 (unless (memq op '(copy rename))
649 (error "Unknown operation `%s', must be `copy' or `rename'" op))
651 (let ((t1 (tramp-tramp-file-p filename))
652 (t2 (tramp-tramp-file-p newname))
653 (equal-remote (tramp-equal-remote filename newname))
654 (file-operation (intern (format "%s-file" op)))
655 (gvfs-operation (if (eq op 'copy) "gvfs-copy" "gvfs-move"))
656 (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
658 (with-parsed-tramp-file-name (if t1 filename newname) nil
659 (when (and (not ok-if-already-exists) (file-exists-p newname))
660 (tramp-error
661 v 'file-already-exists "File %s already exists" newname))
663 (if (or (and equal-remote
664 (tramp-get-connection-property v "direct-copy-failed" nil))
665 (and t1 (not (tramp-gvfs-file-name-p filename)))
666 (and t2 (not (tramp-gvfs-file-name-p newname))))
668 ;; We cannot copy or rename directly.
669 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with
670 ;; Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and renamed
671 ;; in Emacs 24.3.
672 (let ((tmpfile (tramp-compat-make-temp-file filename)))
673 (cond
674 (preserve-extended-attributes
675 (funcall
676 file-operation
677 filename tmpfile t keep-date preserve-uid-gid
678 preserve-extended-attributes))
680 (funcall
681 file-operation filename tmpfile t keep-date preserve-uid-gid)))
682 (rename-file tmpfile newname ok-if-already-exists))
684 ;; Direct action.
685 (with-tramp-progress-reporter
686 v 0 (format "%s %s to %s" msg-operation filename newname)
687 (unless
688 (apply
689 'tramp-gvfs-send-command v gvfs-operation
690 (append
691 (and (eq op 'copy) (or keep-date preserve-uid-gid)
692 '("--preserve"))
693 (list
694 (tramp-gvfs-url-file-name filename)
695 (tramp-gvfs-url-file-name newname))))
697 (if (or (not equal-remote)
698 (and equal-remote
699 (tramp-get-connection-property
700 v "direct-copy-failed" nil)))
701 ;; Propagate the error.
702 (with-current-buffer (tramp-get-connection-buffer v)
703 (goto-char (point-min))
704 (tramp-error-with-buffer
705 nil v 'file-error
706 "%s failed, see buffer `%s' for details."
707 msg-operation (buffer-name)))
709 ;; Some WebDAV server, like the one from QNAP, do not
710 ;; support direct copy/move. Try a fallback.
711 (tramp-set-connection-property v "direct-copy-failed" t)
712 (tramp-gvfs-do-copy-or-rename-file
713 op filename newname ok-if-already-exists keep-date
714 preserve-uid-gid preserve-extended-attributes))))
716 (when (and t1 (eq op 'rename))
717 (with-parsed-tramp-file-name filename nil
718 (tramp-flush-file-property v (file-name-directory localname))
719 (tramp-flush-file-property v localname)))
721 (when t2
722 (with-parsed-tramp-file-name newname nil
723 (tramp-flush-file-property v (file-name-directory localname))
724 (tramp-flush-file-property v localname)))))))
726 (defun tramp-gvfs-handle-copy-file
727 (filename newname &optional ok-if-already-exists keep-date
728 preserve-uid-gid preserve-extended-attributes)
729 "Like `copy-file' for Tramp files."
730 (setq filename (expand-file-name filename))
731 (setq newname (expand-file-name newname))
732 (cond
733 ;; At least one file a Tramp file?
734 ((or (tramp-tramp-file-p filename)
735 (tramp-tramp-file-p newname))
736 (tramp-gvfs-do-copy-or-rename-file
737 'copy filename newname ok-if-already-exists keep-date
738 preserve-uid-gid preserve-extended-attributes))
739 ;; Compat section. PRESERVE-EXTENDED-ATTRIBUTES has been
740 ;; introduced with Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and
741 ;; renamed in Emacs 24.3.
742 (preserve-extended-attributes
743 (tramp-run-real-handler
744 'copy-file
745 (list filename newname ok-if-already-exists keep-date
746 preserve-uid-gid preserve-extended-attributes)))
748 (tramp-run-real-handler
749 'copy-file
750 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))))
752 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
753 "Like `delete-directory' for Tramp files."
754 (with-parsed-tramp-file-name directory nil
755 (if (and recursive (not (file-symlink-p directory)))
756 (mapc (lambda (file)
757 (if (eq t (car (file-attributes file)))
758 (tramp-compat-delete-directory file recursive trash)
759 (tramp-compat-delete-file file trash)))
760 (directory-files
761 directory 'full directory-files-no-dot-files-regexp))
762 (when (directory-files directory nil directory-files-no-dot-files-regexp)
763 (tramp-error
764 v 'file-error "Couldn't delete non-empty %s" directory)))
766 (tramp-flush-file-property v (file-name-directory localname))
767 (tramp-flush-directory-property v localname)
768 (unless
769 (tramp-gvfs-send-command
770 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
771 (tramp-gvfs-url-file-name directory))
772 ;; Propagate the error.
773 (with-current-buffer (tramp-get-connection-buffer v)
774 (goto-char (point-min))
775 (tramp-error-with-buffer
776 nil v 'file-error "Couldn't delete %s" directory)))))
778 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
779 "Like `delete-file' for Tramp files."
780 (with-parsed-tramp-file-name filename nil
781 (tramp-flush-file-property v (file-name-directory localname))
782 (tramp-flush-file-property v localname)
783 (unless
784 (tramp-gvfs-send-command
785 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
786 (tramp-gvfs-url-file-name filename))
787 ;; Propagate the error.
788 (with-current-buffer (tramp-get-connection-buffer v)
789 (goto-char (point-min))
790 (tramp-error-with-buffer
791 nil v 'file-error "Couldn't delete %s" filename)))))
793 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
794 "Like `expand-file-name' for Tramp files."
795 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
796 (setq dir (or dir default-directory "/"))
797 ;; Unless NAME is absolute, concat DIR and NAME.
798 (unless (file-name-absolute-p name)
799 (setq name (concat (file-name-as-directory dir) name)))
800 ;; If NAME is not a Tramp file, run the real handler.
801 (if (not (tramp-tramp-file-p name))
802 (tramp-run-real-handler 'expand-file-name (list name nil))
803 ;; Dissect NAME.
804 (with-parsed-tramp-file-name name nil
805 ;; If there is a default location, expand tilde.
806 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
807 (save-match-data
808 (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
809 (setq localname
810 (replace-match
811 (tramp-get-connection-property v "default-location" "~")
812 nil t localname 1)))
813 ;; Tilde expansion is not possible.
814 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
815 (tramp-error
816 v 'file-error
817 "Cannot expand tilde in file `%s'" name))
818 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
819 (setq localname (concat "/" localname)))
820 ;; We do not pass "/..".
821 (if (string-match "^\\(afp\\|smb\\)$" method)
822 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
823 (setq localname (replace-match "/" t t localname 1)))
824 (when (string-match "^/\\.\\./?" localname)
825 (setq localname (replace-match "/" t t localname))))
826 ;; There might be a double slash. Remove this.
827 (while (string-match "//" localname)
828 (setq localname (replace-match "/" t t localname)))
829 ;; No tilde characters in file name, do normal
830 ;; `expand-file-name' (this does "/./" and "/../").
831 (tramp-make-tramp-file-name
832 method user host
833 (tramp-run-real-handler
834 'expand-file-name (list localname))))))
836 (defun tramp-gvfs-get-directory-attributes (directory)
837 "Return GVFS attributes association list of all files in DIRECTORY."
838 (ignore-errors
839 ;; Don't modify `last-coding-system-used' by accident.
840 (let ((last-coding-system-used last-coding-system-used)
841 result)
842 (with-parsed-tramp-file-name directory nil
843 (with-tramp-file-property v localname "directory-gvfs-attributes"
844 (tramp-message v 5 "directory gvfs attributes: %s" localname)
845 ;; Send command.
846 (tramp-gvfs-send-command
847 v "gvfs-ls" "-h" "-n" "-a"
848 (mapconcat 'identity tramp-gvfs-file-attributes ",")
849 (tramp-gvfs-url-file-name directory))
850 ;; Parse output.
851 (with-current-buffer (tramp-get-connection-buffer v)
852 (goto-char (point-min))
853 (while (looking-at
854 (concat "^\\(.+\\)[[:blank:]]"
855 "\\([[:digit:]]+\\)[[:blank:]]"
856 "(\\(.+?\\))"
857 tramp-gvfs-file-attributes-with-gvfs-ls-regexp))
858 (let ((item (list (cons "type" (match-string 3))
859 (cons "standard::size" (match-string 2))
860 (cons "name" (match-string 1)))))
861 (goto-char (1+ (match-end 3)))
862 (while (looking-at
863 (concat
864 tramp-gvfs-file-attributes-with-gvfs-ls-regexp
865 "\\(" tramp-gvfs-file-attributes-with-gvfs-ls-regexp
866 "\\|" "$" "\\)"))
867 (push (cons (match-string 1) (match-string 2)) item)
868 (goto-char (match-end 2)))
869 ;; Add display name as head.
870 (push
871 (cons (cdr (or (assoc "standard::display-name" item)
872 (assoc "name" item)))
873 (nreverse item))
874 result))
875 (forward-line)))
876 result)))))
878 (defun tramp-gvfs-get-root-attributes (filename)
879 "Return GVFS attributes association list of FILENAME."
880 (ignore-errors
881 ;; Don't modify `last-coding-system-used' by accident.
882 (let ((last-coding-system-used last-coding-system-used)
883 result)
884 (with-parsed-tramp-file-name filename nil
885 (with-tramp-file-property v localname "file-gvfs-attributes"
886 (tramp-message v 5 "file gvfs attributes: %s" localname)
887 ;; Send command.
888 (tramp-gvfs-send-command
889 v "gvfs-info" (tramp-gvfs-url-file-name filename))
890 ;; Parse output.
891 (with-current-buffer (tramp-get-connection-buffer v)
892 (goto-char (point-min))
893 (while (re-search-forward
894 tramp-gvfs-file-attributes-with-gvfs-info-regexp nil t)
895 (push (cons (match-string 1) (match-string 2)) result))
896 result))))))
898 (defun tramp-gvfs-get-file-attributes (filename)
899 "Return GVFS attributes association list of FILENAME."
900 (setq filename (directory-file-name (expand-file-name filename)))
901 (with-parsed-tramp-file-name filename nil
902 (if (or
903 (and (string-match "^\\(afp\\|smb\\)$" method)
904 (string-match "^/?\\([^/]+\\)$" localname))
905 (string-equal localname "/"))
906 (tramp-gvfs-get-root-attributes filename)
907 (assoc
908 (file-name-nondirectory filename)
909 (tramp-gvfs-get-directory-attributes (file-name-directory filename))))))
911 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
912 "Like `file-attributes' for Tramp files."
913 (unless id-format (setq id-format 'integer))
914 (ignore-errors
915 (let ((attributes (tramp-gvfs-get-file-attributes filename))
916 dirp res-symlink-target res-numlinks res-uid res-gid res-access
917 res-mod res-change res-size res-filemodes res-inode res-device)
918 (when attributes
919 ;; ... directory or symlink
920 (setq dirp (if (equal "directory" (cdr (assoc "type" attributes))) t))
921 (setq res-symlink-target
922 (cdr (assoc "standard::symlink-target" attributes)))
923 ;; ... number links
924 (setq res-numlinks
925 (string-to-number
926 (or (cdr (assoc "unix::nlink" attributes)) "0")))
927 ;; ... uid and gid
928 (setq res-uid
929 (if (eq id-format 'integer)
930 (string-to-number
931 (or (cdr (assoc "unix::uid" attributes))
932 (format "%s" tramp-unknown-id-integer)))
933 (or (cdr (assoc "owner::user" attributes))
934 (cdr (assoc "unix::uid" attributes))
935 tramp-unknown-id-string)))
936 (setq res-gid
937 (if (eq id-format 'integer)
938 (string-to-number
939 (or (cdr (assoc "unix::gid" attributes))
940 (format "%s" tramp-unknown-id-integer)))
941 (or (cdr (assoc "owner::group" attributes))
942 (cdr (assoc "unix::gid" attributes))
943 tramp-unknown-id-string)))
944 ;; ... last access, modification and change time
945 (setq res-access
946 (seconds-to-time
947 (string-to-number
948 (or (cdr (assoc "time::access" attributes)) "0"))))
949 (setq res-mod
950 (seconds-to-time
951 (string-to-number
952 (or (cdr (assoc "time::modified" attributes)) "0"))))
953 (setq res-change
954 (seconds-to-time
955 (string-to-number
956 (or (cdr (assoc "time::changed" attributes)) "0"))))
957 ;; ... size
958 (setq res-size
959 (string-to-number
960 (or (cdr (assoc "standard::size" attributes)) "0")))
961 ;; ... file mode flags
962 (setq res-filemodes
963 (let ((n (cdr (assoc "unix::mode" attributes))))
964 (if n
965 (tramp-file-mode-from-int (string-to-number n))
966 (format
967 "%s%s%s%s------"
968 (if dirp "d" "-")
969 (if (equal (cdr (assoc "access::can-read" attributes))
970 "FALSE")
971 "-" "r")
972 (if (equal (cdr (assoc "access::can-write" attributes))
973 "FALSE")
974 "-" "w")
975 (if (equal (cdr (assoc "access::can-execute" attributes))
976 "FALSE")
977 "-" "x")))))
978 ;; ... inode and device
979 (setq res-inode
980 (let ((n (cdr (assoc "unix::inode" attributes))))
981 (if n
982 (string-to-number n)
983 (tramp-get-inode (tramp-dissect-file-name filename)))))
984 (setq res-device
985 (let ((n (cdr (assoc "unix::device" attributes))))
986 (if n
987 (string-to-number n)
988 (tramp-get-device (tramp-dissect-file-name filename)))))
990 ;; Return data gathered.
991 (list
992 ;; 0. t for directory, string (name linked to) for
993 ;; symbolic link, or nil.
994 (or dirp res-symlink-target)
995 ;; 1. Number of links to file.
996 res-numlinks
997 ;; 2. File uid.
998 res-uid
999 ;; 3. File gid.
1000 res-gid
1001 ;; 4. Last access time, as a list of integers.
1002 ;; 5. Last modification time, likewise.
1003 ;; 6. Last status change time, likewise.
1004 res-access res-mod res-change
1005 ;; 7. Size in bytes (-1, if number is out of range).
1006 res-size
1007 ;; 8. File modes.
1008 res-filemodes
1009 ;; 9. t if file's gid would change if file were deleted
1010 ;; and recreated.
1012 ;; 10. Inode number.
1013 res-inode
1014 ;; 11. Device number.
1015 res-device
1016 )))))
1018 (defun tramp-gvfs-handle-file-directory-p (filename)
1019 "Like `file-directory-p' for Tramp files."
1020 (eq t (car (file-attributes (file-truename filename)))))
1022 (defun tramp-gvfs-handle-file-executable-p (filename)
1023 "Like `file-executable-p' for Tramp files."
1024 (with-parsed-tramp-file-name filename nil
1025 (with-tramp-file-property v localname "file-executable-p"
1026 (tramp-check-cached-permissions v ?x))))
1028 (defun tramp-gvfs-handle-file-local-copy (filename)
1029 "Like `file-local-copy' for Tramp files."
1030 (with-parsed-tramp-file-name filename nil
1031 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1032 (unless (file-exists-p filename)
1033 (tramp-error
1034 v 'file-error
1035 "Cannot make local copy of non-existing file `%s'" filename))
1036 (copy-file filename tmpfile 'ok-if-already-exists 'keep-time)
1037 tmpfile)))
1039 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
1040 "Like `file-name-all-completions' for Tramp files."
1041 (unless (save-match-data (string-match "/" filename))
1042 (all-completions
1043 filename
1044 (with-parsed-tramp-file-name (expand-file-name directory) nil
1045 (with-tramp-file-property v localname "file-name-all-completions"
1046 (let ((result '("./" "../")))
1047 ;; Get a list of directories and files.
1048 (dolist (item (tramp-gvfs-get-directory-attributes directory) result)
1049 (if (string-equal (cdr (assoc "type" item)) "directory")
1050 (push (file-name-as-directory (car item)) result)
1051 (push (car item) result)))))))))
1053 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
1054 "Like `file-notify-add-watch' for Tramp files."
1055 (setq file-name (expand-file-name file-name))
1056 (with-parsed-tramp-file-name file-name nil
1057 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1058 ;; supported for gvfs-mounted directories.
1059 (when (file-directory-p file-name)
1060 (tramp-error
1061 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1062 (let* ((default-directory (file-name-directory file-name))
1063 (events
1064 (cond
1065 ((and (memq 'change flags) (memq 'attribute-change flags))
1066 '(created changed changes-done-hint moved deleted
1067 attribute-changed))
1068 ((memq 'change flags)
1069 '(created changed changes-done-hint moved deleted))
1070 ((memq 'attribute-change flags) '(attribute-changed))))
1071 (p (start-process
1072 "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
1073 "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name))))
1074 (if (not (processp p))
1075 (tramp-error
1076 v 'file-notify-error "Monitoring not supported for `%s'" file-name)
1077 (tramp-message
1078 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
1079 (tramp-set-connection-property p "vector" v)
1080 (process-put p 'events events)
1081 (process-put p 'watch-name localname)
1082 (set-process-query-on-exit-flag p nil)
1083 (set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
1084 ;; There might be an error if the monitor is not supported.
1085 ;; Give the filter a chance to read the output.
1086 (tramp-accept-process-output p 1)
1087 (unless (memq (process-status p) '(run open))
1088 (tramp-error
1089 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1090 p))))
1092 (defun tramp-gvfs-monitor-file-process-filter (proc string)
1093 "Read output from \"gvfs-monitor-file\" and add corresponding \
1094 file-notify events."
1095 (let* ((rest-string (process-get proc 'rest-string))
1096 (dd (with-current-buffer (process-buffer proc) default-directory))
1097 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
1098 (when rest-string
1099 (tramp-message proc 10 "Previous string:\n%s" rest-string))
1100 (tramp-message proc 6 "%S\n%s" proc string)
1101 (setq string (concat rest-string string)
1102 ;; Attribute change is returned in unused wording.
1103 string (replace-regexp-in-string
1104 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1105 (when (string-match "Monitoring not supported" string)
1106 (delete-process proc))
1108 (while (string-match
1109 (concat "^[\n\r]*"
1110 "File Monitor Event:[\n\r]+"
1111 "File = \\([^\n\r]+\\)[\n\r]+"
1112 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1113 string)
1114 (let ((file (match-string 1 string))
1115 (action (intern-soft
1116 (replace-regexp-in-string
1117 "_" "-" (downcase (match-string 2 string))))))
1118 (setq string (replace-match "" nil nil string))
1119 ;; File names are returned as URL paths. We must convert them.
1120 (when (string-match ddu file)
1121 (setq file (replace-match dd nil nil file)))
1122 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
1123 (setq file
1124 (replace-match
1125 (char-to-string (string-to-number (match-string 1 file) 16))
1126 nil nil file)))
1127 ;; Usually, we would add an Emacs event now. Unfortunately,
1128 ;; `unread-command-events' does not accept several events at
1129 ;; once. Therefore, we apply the callback directly.
1130 (tramp-compat-funcall 'file-notify-callback (list proc action file))))
1132 ;; Save rest of the string.
1133 (when (zerop (length string)) (setq string nil))
1134 (when string (tramp-message proc 10 "Rest string:\n%s" string))
1135 (process-put proc 'rest-string string)))
1137 (defun tramp-gvfs-handle-file-readable-p (filename)
1138 "Like `file-readable-p' for Tramp files."
1139 (with-parsed-tramp-file-name filename nil
1140 (with-tramp-file-property v localname "file-readable-p"
1141 (tramp-check-cached-permissions v ?r))))
1143 (defun tramp-gvfs-handle-file-writable-p (filename)
1144 "Like `file-writable-p' for Tramp files."
1145 (with-parsed-tramp-file-name filename nil
1146 (with-tramp-file-property v localname "file-writable-p"
1147 (if (file-exists-p filename)
1148 (tramp-check-cached-permissions v ?w)
1149 ;; If file doesn't exist, check if directory is writable.
1150 (and (file-directory-p (file-name-directory filename))
1151 (file-writable-p (file-name-directory filename)))))))
1153 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1154 "Like `make-directory' for Tramp files."
1155 (setq dir (directory-file-name (expand-file-name dir)))
1156 (with-parsed-tramp-file-name dir nil
1157 (tramp-flush-file-property v (file-name-directory localname))
1158 (tramp-flush-directory-property v localname)
1159 (save-match-data
1160 (let ((ldir (file-name-directory dir)))
1161 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1162 ;; work robust.
1163 (when (and parents (not (file-directory-p ldir)))
1164 (make-directory ldir parents))
1165 ;; Just do it.
1166 (unless (tramp-gvfs-send-command
1167 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1168 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1170 (defun tramp-gvfs-handle-rename-file
1171 (filename newname &optional ok-if-already-exists)
1172 "Like `rename-file' for Tramp files."
1173 ;; Check if both files are local -- invoke normal rename-file.
1174 ;; Otherwise, use Tramp from local system.
1175 (setq filename (expand-file-name filename))
1176 (setq newname (expand-file-name newname))
1177 ;; At least one file a Tramp file?
1178 (if (or (tramp-tramp-file-p filename)
1179 (tramp-tramp-file-p newname))
1180 (tramp-gvfs-do-copy-or-rename-file
1181 'rename filename newname ok-if-already-exists
1182 'keep-date 'preserve-uid-gid)
1183 (tramp-run-real-handler
1184 'rename-file (list filename newname ok-if-already-exists))))
1186 (defun tramp-gvfs-handle-write-region
1187 (start end filename &optional append visit lockname confirm)
1188 "Like `write-region' for Tramp files."
1189 (with-parsed-tramp-file-name filename nil
1190 (when (and confirm (file-exists-p filename))
1191 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1192 (tramp-error v 'file-error "File not overwritten")))
1194 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1195 (when (and append (file-exists-p filename))
1196 (copy-file filename tmpfile 'ok))
1197 ;; We say `no-message' here because we don't want the visited file
1198 ;; modtime data to be clobbered from the temp file. We call
1199 ;; `set-visited-file-modtime' ourselves later on.
1200 (tramp-run-real-handler
1201 'write-region
1202 (if confirm ; don't pass this arg unless defined for backward compat.
1203 (list start end tmpfile append 'no-message lockname confirm)
1204 (list start end tmpfile append 'no-message lockname)))
1205 (condition-case nil
1206 (rename-file tmpfile filename 'ok-if-already-exists)
1207 (error
1208 (delete-file tmpfile)
1209 (tramp-error
1210 v 'file-error "Couldn't write region to `%s'" filename))))
1212 (tramp-flush-file-property v (file-name-directory localname))
1213 (tramp-flush-file-property v localname)
1215 ;; Set file modification time.
1216 (when (or (eq visit t) (stringp visit))
1217 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1219 ;; The end.
1220 (when (or (eq visit t) (null visit) (stringp visit))
1221 (tramp-message v 0 "Wrote %s" filename))
1222 (run-hooks 'tramp-handle-write-region-hook)))
1225 ;; File name conversions.
1227 (defun tramp-gvfs-url-file-name (filename)
1228 "Return FILENAME in URL syntax."
1229 ;; "/" must NOT be hexlified.
1230 (let ((url-unreserved-chars (cons ?/ url-unreserved-chars))
1231 result)
1232 (setq
1233 result
1234 (url-recreate-url
1235 (if (tramp-tramp-file-p filename)
1236 (with-parsed-tramp-file-name filename nil
1237 (when (string-equal "gdrive" method)
1238 (setq method "google-drive"))
1239 (when (and user (string-match tramp-user-with-domain-regexp user))
1240 (setq user
1241 (concat (match-string 2 user) ";" (match-string 1 user))))
1242 (url-parse-make-urlobj
1243 method (and user (url-hexify-string user)) nil
1244 (tramp-file-name-real-host v) (tramp-file-name-port v)
1245 (and localname (url-hexify-string localname)) nil nil t))
1246 (url-parse-make-urlobj
1247 "file" nil nil nil nil
1248 (url-hexify-string (file-truename filename)) nil nil t))))
1249 (when (tramp-tramp-file-p filename)
1250 (with-parsed-tramp-file-name filename nil
1251 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1252 result))
1254 (defun tramp-gvfs-object-path (filename)
1255 "Create a D-Bus object path from FILENAME."
1256 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1258 (defun tramp-gvfs-file-name (object-path)
1259 "Retrieve file name from D-Bus OBJECT-PATH."
1260 (dbus-unescape-from-identifier
1261 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1263 (defun tramp-bluez-address (device)
1264 "Return bluetooth device address from a given bluetooth DEVICE name."
1265 (when (stringp device)
1266 (if (string-match tramp-ipv6-regexp device)
1267 (match-string 0 device)
1268 (cadr (assoc device (tramp-bluez-list-devices))))))
1270 (defun tramp-bluez-device (address)
1271 "Return bluetooth device name from a given bluetooth device ADDRESS.
1272 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1273 (when (stringp address)
1274 (while (string-match "[][]" address)
1275 (setq address (replace-match "" t t address)))
1276 (let (result)
1277 (dolist (item (tramp-bluez-list-devices) result)
1278 (when (string-match address (cadr item))
1279 (setq result (car item)))))))
1282 ;; D-Bus GVFS functions.
1284 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1285 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1286 (let* ((filename
1287 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1288 (pw-prompt
1289 (format
1290 "%s for %s "
1291 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1292 (capitalize (match-string 1 message))
1293 "Password")
1294 filename))
1295 password)
1297 (condition-case nil
1298 (with-parsed-tramp-file-name filename l
1299 (when (and (zerop (length user))
1300 (not
1301 (zerop (logand flags tramp-gvfs-password-need-username))))
1302 (setq user (read-string "User name: ")))
1303 (when (and (zerop (length domain))
1304 (not
1305 (zerop (logand flags tramp-gvfs-password-need-domain))))
1306 (setq domain (read-string "Domain name: ")))
1308 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1309 (unless (tramp-get-connection-property l "first-password-request" nil)
1310 (tramp-clear-passwd l))
1312 (setq tramp-current-method l-method
1313 tramp-current-user user
1314 tramp-current-host l-host
1315 password (tramp-read-passwd
1316 (tramp-get-connection-process l) pw-prompt))
1318 ;; Return result.
1319 (if (stringp password)
1320 (list
1321 t ;; password handled.
1322 nil ;; no abort of D-Bus.
1323 password
1324 (tramp-file-name-real-user l)
1325 domain
1326 nil ;; not anonymous.
1327 0) ;; no password save.
1328 ;; No password provided.
1329 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1331 ;; When QUIT is raised, we shall return this information to D-Bus.
1332 (quit (list nil t "" "" "" nil 0)))))
1334 (defun tramp-gvfs-handler-askquestion (message choices)
1335 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1336 (save-window-excursion
1337 (let ((enable-recursive-minibuffers t)
1338 choice)
1340 (condition-case nil
1341 (with-parsed-tramp-file-name
1342 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1343 (tramp-message v 6 "%S %S" message choices)
1345 ;; In theory, there can be several choices. Until now,
1346 ;; there is only the question whether to accept an unknown
1347 ;; host signature.
1348 (with-temp-buffer
1349 ;; Preserve message for `progress-reporter'.
1350 (with-temp-message ""
1351 (insert message)
1352 (pop-to-buffer (current-buffer))
1353 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1354 (tramp-message v 6 "%d" choice)))
1356 ;; When the choice is "no", we set a dummy fuse-mountpoint
1357 ;; in order to leave the timeout.
1358 (unless (zerop choice)
1359 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1361 (list
1362 t ;; handled.
1363 nil ;; no abort of D-Bus.
1364 choice))
1366 ;; When QUIT is raised, we shall return this information to D-Bus.
1367 (quit (list nil t 0))))))
1369 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1370 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1371 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1372 (ignore-errors
1373 (let ((signal-name (dbus-event-member-name last-input-event))
1374 (elt mount-info))
1375 ;; Jump over the first elements of the mount info. Since there
1376 ;; were changes in the entries, we cannot access dedicated
1377 ;; elements.
1378 (while (stringp (car elt)) (setq elt (cdr elt)))
1379 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1380 (mount-spec (caddr elt))
1381 (default-location (tramp-gvfs-dbus-byte-array-to-string
1382 (cadddr elt)))
1383 (method (tramp-gvfs-dbus-byte-array-to-string
1384 (cadr (assoc "type" (cadr mount-spec)))))
1385 (user (tramp-gvfs-dbus-byte-array-to-string
1386 (cadr (assoc "user" (cadr mount-spec)))))
1387 (domain (tramp-gvfs-dbus-byte-array-to-string
1388 (cadr (assoc "domain" (cadr mount-spec)))))
1389 (host (tramp-gvfs-dbus-byte-array-to-string
1390 (cadr (or (assoc "host" (cadr mount-spec))
1391 (assoc "server" (cadr mount-spec))))))
1392 (port (tramp-gvfs-dbus-byte-array-to-string
1393 (cadr (assoc "port" (cadr mount-spec)))))
1394 (ssl (tramp-gvfs-dbus-byte-array-to-string
1395 (cadr (assoc "ssl" (cadr mount-spec)))))
1396 (prefix (concat
1397 (tramp-gvfs-dbus-byte-array-to-string
1398 (car mount-spec))
1399 (tramp-gvfs-dbus-byte-array-to-string
1400 (or (cadr (assoc "share" (cadr mount-spec)))
1401 (cadr (assoc "volume" (cadr mount-spec))))))))
1402 (when (string-match "^\\(afp\\|smb\\)" method)
1403 (setq method (match-string 1 method)))
1404 (when (string-equal "obex" method)
1405 (setq host (tramp-bluez-device host)))
1406 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1407 (setq method "davs"))
1408 (when (string-equal "google-drive" method)
1409 (setq method "gdrive"))
1410 (unless (zerop (length domain))
1411 (setq user (concat user tramp-prefix-domain-format domain)))
1412 (unless (zerop (length port))
1413 (setq host (concat host tramp-prefix-port-format port)))
1414 (with-parsed-tramp-file-name
1415 (tramp-make-tramp-file-name method user host "") nil
1416 (tramp-message
1417 v 6 "%s %s"
1418 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1419 (tramp-set-file-property v "/" "list-mounts" 'undef)
1420 (if (string-equal (downcase signal-name) "unmounted")
1421 (tramp-flush-file-property v "/")
1422 ;; Set prefix, mountpoint and location.
1423 (unless (string-equal prefix "/")
1424 (tramp-set-file-property v "/" "prefix" prefix))
1425 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1426 (tramp-set-connection-property
1427 v "default-location" default-location)))))))
1429 (when tramp-gvfs-enabled
1430 (dbus-register-signal
1431 :session nil tramp-gvfs-path-mounttracker
1432 tramp-gvfs-interface-mounttracker "mounted"
1433 'tramp-gvfs-handler-mounted-unmounted)
1434 (dbus-register-signal
1435 :session nil tramp-gvfs-path-mounttracker
1436 tramp-gvfs-interface-mounttracker "Mounted"
1437 'tramp-gvfs-handler-mounted-unmounted)
1439 (dbus-register-signal
1440 :session nil tramp-gvfs-path-mounttracker
1441 tramp-gvfs-interface-mounttracker "unmounted"
1442 'tramp-gvfs-handler-mounted-unmounted)
1443 (dbus-register-signal
1444 :session nil tramp-gvfs-path-mounttracker
1445 tramp-gvfs-interface-mounttracker "Unmounted"
1446 'tramp-gvfs-handler-mounted-unmounted))
1448 (defun tramp-gvfs-connection-mounted-p (vec)
1449 "Check, whether the location is already mounted."
1451 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1452 (catch 'mounted
1453 (dolist
1454 (elt
1455 (with-tramp-file-property vec "/" "list-mounts"
1456 (with-tramp-dbus-call-method vec t
1457 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1458 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1459 nil)
1460 ;; Jump over the first elements of the mount info. Since there
1461 ;; were changes in the entries, we cannot access dedicated
1462 ;; elements.
1463 (while (stringp (car elt)) (setq elt (cdr elt)))
1464 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1465 (cadr elt)))
1466 (mount-spec (caddr elt))
1467 (default-location (tramp-gvfs-dbus-byte-array-to-string
1468 (cadddr elt)))
1469 (method (tramp-gvfs-dbus-byte-array-to-string
1470 (cadr (assoc "type" (cadr mount-spec)))))
1471 (user (tramp-gvfs-dbus-byte-array-to-string
1472 (cadr (assoc "user" (cadr mount-spec)))))
1473 (domain (tramp-gvfs-dbus-byte-array-to-string
1474 (cadr (assoc "domain" (cadr mount-spec)))))
1475 (host (tramp-gvfs-dbus-byte-array-to-string
1476 (cadr (or (assoc "host" (cadr mount-spec))
1477 (assoc "server" (cadr mount-spec))))))
1478 (port (tramp-gvfs-dbus-byte-array-to-string
1479 (cadr (assoc "port" (cadr mount-spec)))))
1480 (ssl (tramp-gvfs-dbus-byte-array-to-string
1481 (cadr (assoc "ssl" (cadr mount-spec)))))
1482 (prefix (concat
1483 (tramp-gvfs-dbus-byte-array-to-string
1484 (car mount-spec))
1485 (tramp-gvfs-dbus-byte-array-to-string
1487 (cadr (assoc "share" (cadr mount-spec)))
1488 (cadr (assoc "volume" (cadr mount-spec))))))))
1489 (when (string-match "^\\(afp\\|smb\\)" method)
1490 (setq method (match-string 1 method)))
1491 (when (string-equal "obex" method)
1492 (setq host (tramp-bluez-device host)))
1493 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1494 (setq method "davs"))
1495 (when (string-equal "google-drive" method)
1496 (setq method "gdrive"))
1497 (when (and (string-equal "synce" method) (zerop (length user)))
1498 (setq user (or (tramp-file-name-user vec) "")))
1499 (unless (zerop (length domain))
1500 (setq user (concat user tramp-prefix-domain-format domain)))
1501 (unless (zerop (length port))
1502 (setq host (concat host tramp-prefix-port-format port)))
1503 (when (and
1504 (string-equal method (tramp-file-name-method vec))
1505 (string-equal user (or (tramp-file-name-user vec) ""))
1506 (string-equal host (tramp-file-name-host vec))
1507 (string-match (concat "^" (regexp-quote prefix))
1508 (tramp-file-name-localname vec)))
1509 ;; Set prefix, mountpoint and location.
1510 (unless (string-equal prefix "/")
1511 (tramp-set-file-property vec "/" "prefix" prefix))
1512 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1513 (tramp-set-connection-property
1514 vec "default-location" default-location)
1515 (throw 'mounted t)))))))
1517 (defun tramp-gvfs-mount-spec-entry (key value)
1518 "Construct a mount-spec entry to be used in a mount_spec.
1519 It was \"a(say)\", but has changed to \"a{sv})\"."
1520 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1521 (list :dict-entry key
1522 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1523 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1525 (defun tramp-gvfs-mount-spec (vec)
1526 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1527 (let* ((method (tramp-file-name-method vec))
1528 (user (tramp-file-name-real-user vec))
1529 (domain (tramp-file-name-domain vec))
1530 (host (tramp-file-name-real-host vec))
1531 (port (tramp-file-name-port vec))
1532 (localname (tramp-file-name-localname vec))
1533 (share (when (string-match "^/?\\([^/]+\\)" localname)
1534 (match-string 1 localname)))
1535 (ssl (if (string-match "^davs" method) "true" "false"))
1536 (mount-spec
1537 `(:array
1538 ,@(cond
1539 ((string-equal "smb" method)
1540 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1541 (tramp-gvfs-mount-spec-entry "server" host)
1542 (tramp-gvfs-mount-spec-entry "share" share)))
1543 ((string-equal "obex" method)
1544 (list (tramp-gvfs-mount-spec-entry "type" method)
1545 (tramp-gvfs-mount-spec-entry
1546 "host" (concat "[" (tramp-bluez-address host) "]"))))
1547 ((string-match "\\`dav" method)
1548 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1549 (tramp-gvfs-mount-spec-entry "host" host)
1550 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1551 ((string-equal "afp" method)
1552 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1553 (tramp-gvfs-mount-spec-entry "host" host)
1554 (tramp-gvfs-mount-spec-entry "volume" share)))
1555 ((string-equal "gdrive" method)
1556 (list (tramp-gvfs-mount-spec-entry "type" "google-drive")
1557 (tramp-gvfs-mount-spec-entry "host" host)))
1559 (list (tramp-gvfs-mount-spec-entry "type" method)
1560 (tramp-gvfs-mount-spec-entry "host" host))))
1561 ,@(when user
1562 (list (tramp-gvfs-mount-spec-entry "user" user)))
1563 ,@(when domain
1564 (list (tramp-gvfs-mount-spec-entry "domain" domain)))
1565 ,@(when port
1566 (list (tramp-gvfs-mount-spec-entry
1567 "port" (number-to-string port))))))
1568 (mount-pref
1569 (if (and (string-match "\\`dav" method)
1570 (string-match "^/?[^/]+" localname))
1571 (match-string 0 localname)
1572 "/")))
1574 ;; Return.
1575 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1578 ;; Connection functions.
1580 (defun tramp-gvfs-get-remote-uid (vec id-format)
1581 "The uid of the remote connection VEC, in ID-FORMAT.
1582 ID-FORMAT valid values are `string' and `integer'."
1583 (with-tramp-connection-property vec (format "uid-%s" id-format)
1584 (let ((method (tramp-file-name-method vec))
1585 (user (tramp-file-name-user vec))
1586 (host (tramp-file-name-host vec))
1587 (localname
1588 (tramp-get-connection-property vec "default-location" nil)))
1589 (cond
1590 ((and user (equal id-format 'string)) user)
1591 (localname
1592 (nth 2 (file-attributes
1593 (tramp-make-tramp-file-name method user host localname)
1594 id-format)))
1595 ((equal id-format 'integer) tramp-unknown-id-integer)
1596 ((equal id-format 'string) tramp-unknown-id-string)))))
1598 (defun tramp-gvfs-get-remote-gid (vec id-format)
1599 "The gid of the remote connection VEC, in ID-FORMAT.
1600 ID-FORMAT valid values are `string' and `integer'."
1601 (with-tramp-connection-property vec (format "gid-%s" id-format)
1602 (let ((method (tramp-file-name-method vec))
1603 (user (tramp-file-name-user vec))
1604 (host (tramp-file-name-host vec))
1605 (localname
1606 (tramp-get-connection-property vec "default-location" nil)))
1607 (cond
1608 (localname
1609 (nth 3 (file-attributes
1610 (tramp-make-tramp-file-name method user host localname)
1611 id-format)))
1612 ((equal id-format 'integer) tramp-unknown-id-integer)
1613 ((equal id-format 'string) tramp-unknown-id-string)))))
1615 (defvar tramp-gvfs-get-remote-uid-gid-in-progress nil
1616 "Indication, that remote uid and gid determination is in progress.")
1618 (defun tramp-gvfs-maybe-open-connection (vec)
1619 "Maybe open a connection VEC.
1620 Does not do anything if a connection is already open, but re-opens the
1621 connection if a previous connection has died for some reason."
1622 (tramp-check-proper-method-and-host vec)
1624 ;; We set the file name, in case there are incoming D-Bus signals or
1625 ;; D-Bus errors.
1626 (setq tramp-gvfs-dbus-event-vector vec)
1628 ;; For password handling, we need a process bound to the connection
1629 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1630 ;; better solution?
1631 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1632 (let ((p (make-network-process
1633 :name (tramp-buffer-name vec)
1634 :buffer (tramp-get-connection-buffer vec)
1635 :server t :host 'local :service t :noquery t)))
1636 (set-process-query-on-exit-flag p nil)))
1638 (unless (tramp-gvfs-connection-mounted-p vec)
1639 (let* ((method (tramp-file-name-method vec))
1640 (user (tramp-file-name-user vec))
1641 (host (tramp-file-name-host vec))
1642 (localname (tramp-file-name-localname vec))
1643 (object-path
1644 (tramp-gvfs-object-path
1645 (tramp-make-tramp-file-name method user host ""))))
1647 (when (and (string-equal method "afp")
1648 (string-equal localname "/"))
1649 (tramp-error vec 'file-error "Filename must contain an AFP volume"))
1651 (when (and (string-equal method "smb")
1652 (string-equal localname "/"))
1653 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1655 (with-tramp-progress-reporter
1656 vec 3
1657 (if (zerop (length user))
1658 (format "Opening connection for %s using %s" host method)
1659 (format "Opening connection for %s@%s using %s" user host method))
1661 ;; Enable `auth-source'.
1662 (tramp-set-connection-property vec "first-password-request" t)
1664 ;; There will be a callback of "askPassword" when a password is
1665 ;; needed.
1666 (dbus-register-method
1667 :session dbus-service-emacs object-path
1668 tramp-gvfs-interface-mountoperation "askPassword"
1669 'tramp-gvfs-handler-askpassword)
1670 (dbus-register-method
1671 :session dbus-service-emacs object-path
1672 tramp-gvfs-interface-mountoperation "AskPassword"
1673 'tramp-gvfs-handler-askpassword)
1675 ;; There could be a callback of "askQuestion" when adding fingerprint.
1676 (dbus-register-method
1677 :session dbus-service-emacs object-path
1678 tramp-gvfs-interface-mountoperation "askQuestion"
1679 'tramp-gvfs-handler-askquestion)
1680 (dbus-register-method
1681 :session dbus-service-emacs object-path
1682 tramp-gvfs-interface-mountoperation "AskQuestion"
1683 'tramp-gvfs-handler-askquestion)
1685 ;; The call must be asynchronously, because of the "askPassword"
1686 ;; or "askQuestion"callbacks.
1687 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1688 (with-tramp-dbus-call-method vec nil
1689 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1690 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1691 (tramp-gvfs-mount-spec vec)
1692 `(:struct :string ,(dbus-get-unique-name :session)
1693 :object-path ,object-path))
1694 (with-tramp-dbus-call-method vec nil
1695 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1696 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1697 (tramp-gvfs-mount-spec vec)
1698 :string (dbus-get-unique-name :session) :object-path object-path))
1700 ;; We must wait, until the mount is applied. This will be
1701 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1702 ;; file property.
1703 (with-timeout
1704 ((or (tramp-get-method-parameter vec 'tramp-connection-timeout)
1705 tramp-connection-timeout)
1706 (if (zerop (length (tramp-file-name-user vec)))
1707 (tramp-error
1708 vec 'file-error
1709 "Timeout reached mounting %s using %s" host method)
1710 (tramp-error
1711 vec 'file-error
1712 "Timeout reached mounting %s@%s using %s" user host method)))
1713 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1714 (read-event nil nil 0.1)))
1716 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1717 ;; is marked with the fuse-mountpoint "/". We shall react.
1718 (when (string-equal
1719 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1720 (tramp-error vec 'file-error "FUSE mount denied"))
1722 ;; Mark it as connected.
1723 (tramp-set-connection-property
1724 (tramp-get-connection-process vec) "connected" t))))
1726 ;; In `tramp-check-cached-permissions', the connection properties
1727 ;; {uig,gid}-{integer,string} are used. We set them to proper values.
1728 (unless tramp-gvfs-get-remote-uid-gid-in-progress
1729 (let ((tramp-gvfs-get-remote-uid-gid-in-progress t))
1730 (tramp-gvfs-get-remote-uid vec 'integer)
1731 (tramp-gvfs-get-remote-gid vec 'integer)
1732 (tramp-gvfs-get-remote-uid vec 'string)
1733 (tramp-gvfs-get-remote-gid vec 'string))))
1735 (defun tramp-gvfs-send-command (vec command &rest args)
1736 "Send the COMMAND with its ARGS to connection VEC.
1737 COMMAND is usually a command from the gvfs-* utilities.
1738 `call-process' is applied, and it returns t if the return code is zero."
1739 (let* ((locale (tramp-get-local-locale vec))
1740 (process-environment
1741 (append
1742 `(,(format "LANG=%s" locale)
1743 ,(format "LANGUAGE=%s" locale)
1744 ,(format "LC_ALL=%s" locale))
1745 process-environment)))
1746 (with-current-buffer (tramp-get-connection-buffer vec)
1747 (tramp-gvfs-maybe-open-connection vec)
1748 (erase-buffer)
1749 (or (zerop (apply 'tramp-call-process vec command nil t nil args))
1750 ;; Remove information about mounted connection.
1751 (and (tramp-flush-file-property vec "/") nil)))))
1754 ;; D-Bus BLUEZ functions.
1756 (defun tramp-bluez-list-devices ()
1757 "Return all discovered bluetooth devices as list.
1758 Every entry is a list (NAME ADDRESS).
1760 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1761 discovery happened more time before indicated there, a rescan will be
1762 started, which lasts some ten seconds. Otherwise, cached results will
1763 be used."
1764 ;; Reset the scanned devices list if time has passed.
1765 (and (integerp tramp-bluez-discover-devices-timeout)
1766 (integerp tramp-bluez-discovery)
1767 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1768 tramp-bluez-discover-devices-timeout)
1769 (setq tramp-bluez-devices nil))
1771 ;; Rescan if needed.
1772 (unless tramp-bluez-devices
1773 (let ((object-path
1774 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1775 :system tramp-bluez-service "/"
1776 tramp-bluez-interface-manager "DefaultAdapter")))
1777 (setq tramp-bluez-devices nil
1778 tramp-bluez-discovery t)
1779 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1780 :system tramp-bluez-service object-path
1781 tramp-bluez-interface-adapter "StartDiscovery")
1782 (while tramp-bluez-discovery
1783 (read-event nil nil 0.1))))
1784 (setq tramp-bluez-discovery (current-time))
1785 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1786 tramp-bluez-devices)
1788 (defun tramp-bluez-property-changed (property value)
1789 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1790 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1791 (cond
1792 ((string-equal property "Discovering")
1793 (unless (car value)
1794 ;; "Discovering" FALSE means discovery run has been completed.
1795 ;; We stop it, because we don't need another run.
1796 (setq tramp-bluez-discovery nil)
1797 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1798 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1799 tramp-bluez-interface-adapter "StopDiscovery")))))
1801 (when tramp-gvfs-enabled
1802 (dbus-register-signal
1803 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1804 'tramp-bluez-property-changed))
1806 (defun tramp-bluez-device-found (device args)
1807 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1808 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1809 (let ((alias (car (cadr (assoc "Alias" args))))
1810 (address (car (cadr (assoc "Address" args)))))
1811 ;; Maybe we shall check the device class for being a proper
1812 ;; device, and call also SDP in order to find the obex service.
1813 (add-to-list 'tramp-bluez-devices (list alias address))))
1815 (when tramp-gvfs-enabled
1816 (dbus-register-signal
1817 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1818 'tramp-bluez-device-found))
1820 (defun tramp-bluez-parse-device-names (_ignore)
1821 "Return a list of (nil host) tuples allowed to access."
1822 (mapcar
1823 (lambda (x) (list nil (car x)))
1824 (tramp-bluez-list-devices)))
1826 ;; Add completion function for OBEX method.
1827 (when (and tramp-gvfs-enabled
1828 (member tramp-bluez-service (dbus-list-known-names :system)))
1829 (tramp-set-completion-function
1830 "obex" '((tramp-bluez-parse-device-names ""))))
1833 ;; D-Bus zeroconf functions.
1835 (defun tramp-zeroconf-parse-device-names (service)
1836 "Return a list of (user host) tuples allowed to access."
1837 (mapcar
1838 (lambda (x)
1839 (let ((host (zeroconf-service-host x))
1840 (port (zeroconf-service-port x))
1841 (text (zeroconf-service-txt x))
1842 user)
1843 (when port
1844 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1845 ;; A user is marked in a TXT field like "u=guest".
1846 (while text
1847 (when (string-match "u=\\(.+\\)$" (car text))
1848 (setq user (match-string 1 (car text))))
1849 (setq text (cdr text)))
1850 (list user host)))
1851 (zeroconf-list-services service)))
1853 ;; We use the TRIM argument of `split-string', which exist since Emacs
1854 ;; 24.4. I mask this for older Emacs versions, there is no harm.
1855 (defun tramp-gvfs-parse-device-names (service)
1856 "Return a list of (user host) tuples allowed to access.
1857 This uses \"avahi-browse\" in case D-Bus is not enabled in Avahi."
1858 (let ((result
1859 (ignore-errors
1860 (tramp-compat-funcall
1861 'split-string
1862 (shell-command-to-string (format "avahi-browse -trkp %s" service))
1863 "[\n\r]+" 'omit "^\\+;.*$"))))
1864 (delete-dups
1865 (mapcar
1866 (lambda (x)
1867 (let* ((list (split-string x ";"))
1868 (host (nth 6 list))
1869 (port (nth 8 list))
1870 (text (tramp-compat-funcall
1871 'split-string (nth 9 list) "\" \"" 'omit "\""))
1872 user)
1873 ; (when (and port (not (string-equal port "0")))
1874 ; (setq host (format "%s%s%s" host tramp-prefix-port-regexp port)))
1875 ;; A user is marked in a TXT field like "u=guest".
1876 (while text
1877 (when (string-match "u=\\(.+\\)$" (car text))
1878 (setq user (match-string 1 (car text))))
1879 (setq text (cdr text)))
1880 (list user host)))
1881 result))))
1883 ;; Add completion functions for AFP, DAV, DAVS, SFTP and SMB methods.
1884 (when tramp-gvfs-enabled
1885 ;; Suppress D-Bus error messages.
1886 (let (tramp-gvfs-dbus-event-vector)
1887 (zeroconf-init tramp-gvfs-zeroconf-domain)
1888 (if (zeroconf-list-service-types)
1889 (progn
1890 (tramp-set-completion-function
1891 "afp" '((tramp-zeroconf-parse-device-names "_afpovertcp._tcp")))
1892 (tramp-set-completion-function
1893 "dav" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1894 (tramp-set-completion-function
1895 "davs" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1896 (tramp-set-completion-function
1897 "sftp" '((tramp-zeroconf-parse-device-names "_ssh._tcp")
1898 (tramp-zeroconf-parse-device-names "_workstation._tcp")))
1899 (when (member "smb" tramp-gvfs-methods)
1900 (tramp-set-completion-function
1901 "smb" '((tramp-zeroconf-parse-device-names "_smb._tcp")))))
1903 (when (executable-find "avahi-browse")
1904 (tramp-set-completion-function
1905 "afp" '((tramp-gvfs-parse-device-names "_afpovertcp._tcp")))
1906 (tramp-set-completion-function
1907 "dav" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1908 (tramp-set-completion-function
1909 "davs" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1910 (tramp-set-completion-function
1911 "sftp" '((tramp-gvfs-parse-device-names "_ssh._tcp")
1912 (tramp-gvfs-parse-device-names "_workstation._tcp")))
1913 (when (member "smb" tramp-gvfs-methods)
1914 (tramp-set-completion-function
1915 "smb" '((tramp-gvfs-parse-device-names "_smb._tcp"))))))))
1918 ;; D-Bus SYNCE functions.
1920 (defun tramp-synce-list-devices ()
1921 "Return all discovered synce devices as list.
1922 They are retrieved from the hal daemon."
1923 (let (tramp-synce-devices)
1924 (dolist (device
1925 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1926 :system tramp-hal-service tramp-hal-path-manager
1927 tramp-hal-interface-manager "GetAllDevices"))
1928 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1929 :system tramp-hal-service device tramp-hal-interface-device
1930 "PropertyExists" "sync.plugin")
1931 (let ((prop
1932 (with-tramp-dbus-call-method
1933 tramp-gvfs-dbus-event-vector t
1934 :system tramp-hal-service device tramp-hal-interface-device
1935 "GetPropertyString" "pda.pocketpc.name")))
1936 (unless (member prop tramp-synce-devices)
1937 (push prop tramp-synce-devices)))))
1938 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1939 tramp-synce-devices))
1941 (defun tramp-synce-parse-device-names (_ignore)
1942 "Return a list of (nil host) tuples allowed to access."
1943 (mapcar
1944 (lambda (x) (list nil x))
1945 (tramp-synce-list-devices)))
1947 ;; Add completion function for SYNCE method.
1948 (when tramp-gvfs-enabled
1949 (tramp-set-completion-function
1950 "synce" '((tramp-synce-parse-device-names ""))))
1952 (add-hook 'tramp-unload-hook
1953 (lambda ()
1954 (unload-feature 'tramp-gvfs 'force)))
1956 (provide 'tramp-gvfs)
1958 ;;; TODO:
1960 ;; * Host name completion for existing mount points (afp-server,
1961 ;; smb-server) or via smb-network.
1962 ;; * Check, how two shares of the same SMB server can be mounted in
1963 ;; parallel.
1964 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1965 ;; capability.
1966 ;; * Implement obex for other serial communication but bluetooth.
1968 ;;; tramp-gvfs.el ends here