1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
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/>.
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
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
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
50 ;; The customer option `tramp-gvfs-methods' contains the list of
51 ;; supported connection methods. Per default, these are "dav",
52 ;; "davs", "obex" and "synce". Note that with "obex" it might be
53 ;; necessary to pair with the other bluetooth device, if it hasn't
54 ;; been done already. There might be also some few seconds delay in
55 ;; discovering available bluetooth devices.
57 ;; Other possible connection methods are "ftp", "sftp" and "smb".
58 ;; When one of these methods is added to the list, the remote access
59 ;; for that method is performed via GVFS instead of the native Tramp
62 ;; GVFS offers even more connection methods. The complete list of
63 ;; connection methods of the actual GVFS implementation can be
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
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 "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 customer option
85 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
89 ;; * The current GVFS implementation does not allow to write on the
90 ;; remote bluetooth device via OBEX.
92 ;; * Two shares of the same SMB server cannot be mounted in parallel.
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 ;; Pacify byte-compiler
113 (defcustom tramp-gvfs-methods '("dav" "davs" "obex" "synce")
114 "List of methods for remote files, accessed with GVFS."
117 :type '(repeat (choice (const "dav")
125 ;; Add a default for `tramp-default-user-alist'. Rule: For the SYNCE
126 ;; method, no user is chosen.
128 (add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
130 (defcustom tramp-gvfs-zeroconf-domain "local"
131 "Zeroconf domain to be used for discovering services, like host names."
136 ;; Add the methods to `tramp-methods', in order to allow minibuffer
139 (when (featurep 'dbusbind)
140 (dolist (elt tramp-gvfs-methods)
141 (unless (assoc elt tramp-methods)
142 (add-to-list 'tramp-methods (cons elt nil)))))
144 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
145 "The preceding object path for own objects.")
147 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
148 "The well known name of the GVFS daemon.")
150 ;; D-Bus integration is available since Emacs 23 on some system types.
151 ;; We don't call `dbus-ping', because this would load dbus.el.
152 (defconst tramp-gvfs-enabled
154 (and (featurep 'dbusbind)
155 (tramp-compat-funcall 'dbus-get-unique-name :session)
156 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
157 (tramp-compat-process-running-p "gvfsd-fuse"))))
158 "Non-nil when GVFS is available.")
160 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
161 "The object path of the GVFS daemon.")
163 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
164 "The mount tracking interface in the GVFS daemon.")
166 ;; Introspection data exist since GVFS 1.14. If there are no such
167 ;; data, we expect an earlier interface.
168 (defconst tramp-gvfs-methods-mounttracker
169 (dbus-introspect-get-method-names
170 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
171 tramp-gvfs-interface-mounttracker)
172 "The list of supported methods of the mount tracking interface.")
174 (defconst tramp-gvfs-listmounts
175 (if (member "ListMounts" tramp-gvfs-methods-mounttracker)
178 "The name of the \"listMounts\" method.
179 It has been changed in GVFS 1.14.")
181 (defconst tramp-gvfs-mountlocation
182 (if (member "MountLocation" tramp-gvfs-methods-mounttracker)
185 "The name of the \"mountLocation\" method.
186 It has been changed in GVFS 1.14.")
188 (defconst tramp-gvfs-mountlocation-signature
189 (dbus-introspect-get-signature
190 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
191 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation)
192 "The D-Bus signature of the \"mountLocation\" method.
193 It has been changed in GVFS 1.14.")
195 ;; <interface name='org.gtk.vfs.MountTracker'>
196 ;; <method name='listMounts'>
197 ;; <arg name='mount_info_list'
198 ;; type='a{sosssssbay{aya{say}}ay}'
201 ;; <method name='mountLocation'>
202 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
203 ;; <arg name='dbus_id' type='s' direction='in'/>
204 ;; <arg name='object_path' type='o' direction='in'/>
206 ;; <signal name='mounted'>
207 ;; <arg name='mount_info'
208 ;; type='{sosssssbay{aya{say}}ay}'/>
210 ;; <signal name='unmounted'>
211 ;; <arg name='mount_info'
212 ;; type='{sosssssbay{aya{say}}ay}'/>
218 ;; OBJECT_PATH object_path
219 ;; STRING display_name
220 ;; STRING stable_name
221 ;; STRING x_content_types Since GVFS 1.0 only !!!
223 ;; STRING preferred_filename_encoding
224 ;; BOOLEAN user_visible
225 ;; ARRAY BYTE fuse_mountpoint
227 ;; ARRAY BYTE mount_prefix
229 ;; STRUCT mount_spec_item
230 ;; STRING key (server, share, type, user, host, port)
232 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
234 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
235 "Used by the dbus-proxying implementation of GMountOperation.")
237 ;; <interface name='org.gtk.vfs.MountOperation'>
238 ;; <method name='askPassword'>
239 ;; <arg name='message' type='s' direction='in'/>
240 ;; <arg name='default_user' type='s' direction='in'/>
241 ;; <arg name='default_domain' type='s' direction='in'/>
242 ;; <arg name='flags' type='u' direction='in'/>
243 ;; <arg name='handled' type='b' direction='out'/>
244 ;; <arg name='aborted' type='b' direction='out'/>
245 ;; <arg name='password' type='s' direction='out'/>
246 ;; <arg name='username' type='s' direction='out'/>
247 ;; <arg name='domain' type='s' direction='out'/>
248 ;; <arg name='anonymous' type='b' direction='out'/>
249 ;; <arg name='password_save' type='u' direction='out'/>
251 ;; <method name='askQuestion'>
252 ;; <arg name='message' type='s' direction='in'/>
253 ;; <arg name='choices' type='as' direction='in'/>
254 ;; <arg name='handled' type='b' direction='out'/>
255 ;; <arg name='aborted' type='b' direction='out'/>
256 ;; <arg name='choice' type='u' direction='out'/>
260 ;; The following flags are used in "askPassword". They are defined in
261 ;; /usr/include/glib-2.0/gio/gioenums.h.
263 (defconst tramp-gvfs-password-need-password 1
264 "Operation requires a password.")
266 (defconst tramp-gvfs-password-need-username 2
267 "Operation requires a username.")
269 (defconst tramp-gvfs-password-need-domain 4
270 "Operation requires a domain.")
272 (defconst tramp-gvfs-password-saving-supported 8
273 "Operation supports saving settings.")
275 (defconst tramp-gvfs-password-anonymous-supported 16
276 "Operation supports anonymous users.")
278 (defconst tramp-bluez-service "org.bluez"
279 "The well known name of the BLUEZ service.")
281 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
282 "The manager interface of the BLUEZ daemon.")
284 ;; <interface name='org.bluez.Manager'>
285 ;; <method name='DefaultAdapter'>
286 ;; <arg type='o' direction='out'/>
288 ;; <method name='FindAdapter'>
289 ;; <arg type='s' direction='in'/>
290 ;; <arg type='o' direction='out'/>
292 ;; <method name='ListAdapters'>
293 ;; <arg type='ao' direction='out'/>
295 ;; <signal name='AdapterAdded'>
298 ;; <signal name='AdapterRemoved'>
301 ;; <signal name='DefaultAdapterChanged'>
306 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
307 "The adapter interface of the BLUEZ daemon.")
309 ;; <interface name='org.bluez.Adapter'>
310 ;; <method name='GetProperties'>
311 ;; <arg type='a{sv}' direction='out'/>
313 ;; <method name='SetProperty'>
314 ;; <arg type='s' direction='in'/>
315 ;; <arg type='v' direction='in'/>
317 ;; <method name='RequestMode'>
318 ;; <arg type='s' direction='in'/>
320 ;; <method name='ReleaseMode'/>
321 ;; <method name='RequestSession'/>
322 ;; <method name='ReleaseSession'/>
323 ;; <method name='StartDiscovery'/>
324 ;; <method name='StopDiscovery'/>
325 ;; <method name='ListDevices'>
326 ;; <arg type='ao' direction='out'/>
328 ;; <method name='CreateDevice'>
329 ;; <arg type='s' direction='in'/>
330 ;; <arg type='o' direction='out'/>
332 ;; <method name='CreatePairedDevice'>
333 ;; <arg type='s' direction='in'/>
334 ;; <arg type='o' direction='in'/>
335 ;; <arg type='s' direction='in'/>
336 ;; <arg type='o' direction='out'/>
338 ;; <method name='CancelDeviceCreation'>
339 ;; <arg type='s' direction='in'/>
341 ;; <method name='RemoveDevice'>
342 ;; <arg type='o' direction='in'/>
344 ;; <method name='FindDevice'>
345 ;; <arg type='s' direction='in'/>
346 ;; <arg type='o' direction='out'/>
348 ;; <method name='RegisterAgent'>
349 ;; <arg type='o' direction='in'/>
350 ;; <arg type='s' direction='in'/>
352 ;; <method name='UnregisterAgent'>
353 ;; <arg type='o' direction='in'/>
355 ;; <signal name='DeviceCreated'>
358 ;; <signal name='DeviceRemoved'>
361 ;; <signal name='DeviceFound'>
363 ;; <arg type='a{sv}'/>
365 ;; <signal name='PropertyChanged'>
369 ;; <signal name='DeviceDisappeared'>
374 (defcustom tramp-bluez-discover-devices-timeout 60
375 "Defines seconds since last bluetooth device discovery before rescanning.
376 A value of 0 would require an immediate discovery during hostname
377 completion, nil means to use always cached values for discovered
381 :type '(choice (const nil) integer))
383 (defvar tramp-bluez-discovery nil
384 "Indicator for a running bluetooth device discovery.
385 It keeps the timestamp of last discovery.")
387 (defvar tramp-bluez-devices nil
388 "Alist of detected bluetooth devices.
389 Every entry is a list (NAME ADDRESS).")
391 (defconst tramp-hal-service "org.freedesktop.Hal"
392 "The well known name of the HAL service.")
394 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
395 "The object path of the HAL daemon manager.")
397 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
398 "The manager interface of the HAL daemon.")
400 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
401 "The device interface of the HAL daemon.")
404 ;; New handlers should be added here.
405 (defconst tramp-gvfs-file-name-handler-alist
407 (access-file . ignore)
408 (add-name-to-file . tramp-gvfs-handle-copy-file)
409 ;; `byte-compiler-base-file-name' performed by default handler.
410 (copy-file . tramp-gvfs-handle-copy-file)
411 (delete-directory . tramp-gvfs-handle-delete-directory)
412 (delete-file . tramp-gvfs-handle-delete-file)
413 ;; `diff-latest-backup-file' performed by default handler.
414 (directory-file-name . tramp-handle-directory-file-name)
415 (directory-files . tramp-handle-directory-files)
416 (directory-files-and-attributes
417 . tramp-handle-directory-files-and-attributes)
418 (dired-call-process . ignore)
419 (dired-compress-file . ignore)
420 (dired-uncache . tramp-handle-dired-uncache)
421 ;; `executable-find' is not official yet. performed by default handler.
422 (expand-file-name . tramp-gvfs-handle-expand-file-name)
423 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
425 (file-attributes . tramp-gvfs-handle-file-attributes)
426 (file-directory-p . tramp-gvfs-handle-file-directory-p)
427 (file-executable-p . tramp-gvfs-handle-file-executable-p)
428 (file-exists-p . tramp-handle-file-exists-p)
429 (file-local-copy . tramp-gvfs-handle-file-local-copy)
430 (file-modes . tramp-handle-file-modes)
431 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
432 (file-name-as-directory . tramp-handle-file-name-as-directory)
433 (file-name-completion . tramp-handle-file-name-completion)
434 (file-name-directory . tramp-handle-file-name-directory)
435 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
436 ;; `file-name-sans-versions' performed by default handler.
437 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
438 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
439 (file-notify-rm-watch . ignore)
440 (file-notify-supported-p . ignore)
441 (file-ownership-preserved-p . ignore)
442 (file-readable-p . tramp-gvfs-handle-file-readable-p)
443 (file-regular-p . tramp-handle-file-regular-p)
444 (file-remote-p . tramp-handle-file-remote-p)
445 (file-selinux-context . ignore)
446 (file-symlink-p . tramp-handle-file-symlink-p)
447 ;; `file-truename' performed by default handler.
448 (file-writable-p . tramp-gvfs-handle-file-writable-p)
449 (find-backup-file-name . tramp-handle-find-backup-file-name)
450 ;; `find-file-noselect' performed by default handler.
451 ;; `get-file-buffer' performed by default handler.
452 (insert-directory . tramp-gvfs-handle-insert-directory)
453 (insert-file-contents . tramp-gvfs-handle-insert-file-contents)
454 (load . tramp-handle-load)
455 (make-directory . tramp-gvfs-handle-make-directory)
456 (make-directory-internal . ignore)
457 (make-symbolic-link . ignore)
458 (process-file . ignore)
459 (rename-file . tramp-gvfs-handle-rename-file)
460 (set-file-acl . ignore)
461 (set-file-modes . ignore)
462 (set-file-selinux-context . ignore)
463 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime)
464 (shell-command . ignore)
465 (start-file-process . ignore)
466 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
467 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
468 (vc-registered . ignore)
469 ;; `verify-visited-file-modtime' performed by default handler.
470 (write-region . tramp-gvfs-handle-write-region)
472 "Alist of handler functions for Tramp GVFS method.
473 Operations not mentioned here will be handled by the default Emacs primitives.")
475 ;; It must be a `defsubst' in order to push the whole code into
476 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
478 (defsubst tramp-gvfs-file-name-p (filename)
479 "Check if it's a filename handled by the GVFS daemon."
480 (and (tramp-tramp-file-p filename)
482 (tramp-file-name-method (tramp-dissect-file-name filename))))
483 (and (stringp method) (member method tramp-gvfs-methods)))))
486 (defun tramp-gvfs-file-name-handler (operation &rest args)
487 "Invoke the GVFS related OPERATION.
488 First arg specifies the OPERATION, second arg is a list of arguments to
489 pass to the OPERATION."
490 (unless tramp-gvfs-enabled
491 (tramp-compat-user-error "Package `tramp-gvfs' not supported"))
492 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
494 (save-match-data (apply (cdr fn) args))
495 (tramp-run-real-handler operation args))))
497 ;; This might be moved to tramp.el. It shall be the first file name
500 (when (featurep 'dbusbind)
501 (add-to-list 'tramp-foreign-file-name-handler-alist
502 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
505 ;; D-Bus helper function.
507 (defun tramp-gvfs-dbus-string-to-byte-array (string)
508 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
509 (dbus-string-to-byte-array
510 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
511 (concat string (string 0)) string)))
513 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
514 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
515 ;; The byte array could be a variant. Take care.
517 (if (and (consp byte-array) (atom (car byte-array)))
518 byte-array (car byte-array))))
519 (dbus-byte-array-to-string
520 (if (and (consp byte-array) (zerop (car (last byte-array))))
521 (butlast byte-array) byte-array))))
523 (defun tramp-gvfs-stringify-dbus-message (message)
524 "Convert a D-Bus message into readable UTF8 strings, used for traces."
526 ((and (consp message) (characterp (car message)))
527 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
529 (mapcar 'tramp-gvfs-stringify-dbus-message message))
531 (format "%S" message))
534 (defmacro with-tramp-dbus-call-method
535 (vec synchronous bus service path interface method &rest args)
536 "Apply a D-Bus call on bus BUS.
538 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
539 it is an asynchronous call, with `ignore' as callback function.
541 The other arguments have the same meaning as with `dbus-call-method'
542 or `dbus-call-method-asynchronously'. Additionally, the call
543 will be traced by Tramp with trace level 6."
544 `(let ((func (if ,synchronous
545 'dbus-call-method 'dbus-call-method-asynchronously))
546 (args (append (list ,bus ,service ,path ,interface ,method)
547 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
549 (tramp-message ,vec 6 "%s %s" func args)
550 (setq result (apply func args))
551 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
554 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
555 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
556 (tramp-compat-font-lock-add-keywords
557 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
559 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
560 "Apply a Tramp GVFS `handler'.
561 In case of an error, modify the error message by replacing
562 `filename' with its GVFS mounted name."
563 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
566 (tramp-compat-funcall ,handler ,@args)
570 (when (and (stringp (car elt))
571 (string-match fuse-file-name (car elt)))
572 (setcar elt (replace-match ,filename t t (car elt))))
573 (setq elt (cdr elt)))
574 (signal (car err) (cdr err))))))
576 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
577 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
578 (tramp-compat-font-lock-add-keywords
579 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
581 (defvar tramp-gvfs-dbus-event-vector nil
582 "Current Tramp file name to be used, as vector.
583 It is needed when D-Bus signals or errors arrive, because there
584 is no information where to trace the message.")
586 (defun tramp-gvfs-dbus-event-error (event err)
587 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
588 (when tramp-gvfs-dbus-event-vector
589 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
590 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
592 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
594 (if (boundp 'dbus-event-error-functions)
595 'dbus-event-error-functions 'dbus-event-error-hooks)
596 'tramp-gvfs-dbus-event-error)
599 ;; File name primitives.
601 (defun tramp-gvfs-handle-copy-file
602 (filename newname &optional ok-if-already-exists keep-date
603 preserve-uid-gid preserve-extended-attributes)
604 "Like `copy-file' for Tramp files."
605 (with-parsed-tramp-file-name
606 (if (tramp-tramp-file-p filename) filename newname) nil
608 (when (and (not ok-if-already-exists) (file-exists-p newname))
610 v 'file-already-exists "File %s already exists" newname))
612 (if (or (and (tramp-tramp-file-p filename)
613 (not (tramp-gvfs-file-name-p filename)))
614 (and (tramp-tramp-file-p newname)
615 (not (tramp-gvfs-file-name-p newname))))
617 ;; We cannot copy directly.
618 (let ((tmpfile (tramp-compat-make-temp-file filename)))
620 (preserve-extended-attributes
622 filename tmpfile t keep-date preserve-uid-gid
623 preserve-extended-attributes))
625 (copy-file filename tmpfile t keep-date preserve-uid-gid))
627 (copy-file filename tmpfile t keep-date)))
628 (rename-file tmpfile newname ok-if-already-exists))
631 (with-tramp-progress-reporter
632 v 0 (format "Copying %s to %s" filename newname)
635 (append (if (or keep-date preserve-uid-gid)
639 (tramp-gvfs-url-file-name filename)
640 (tramp-gvfs-url-file-name newname)))))
641 (apply 'tramp-gvfs-send-command v "gvfs-copy" args))
642 ;; Propagate the error.
643 (with-current-buffer (tramp-get-connection-buffer v)
644 (goto-char (point-min))
645 (tramp-error-with-buffer
647 "Copying failed, see buffer `%s' for details." (buffer-name)))))
649 (when (file-remote-p newname)
650 (with-parsed-tramp-file-name newname nil
651 (tramp-flush-file-property v (file-name-directory localname))
652 (tramp-flush-file-property v localname))))))
654 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
655 "Like `delete-directory' for Tramp files."
656 (when (and recursive (not (file-symlink-p directory)))
658 (if (eq t (car (file-attributes file)))
659 (tramp-compat-delete-directory file recursive trash)
660 (tramp-compat-delete-file file trash)))
662 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
663 (with-parsed-tramp-file-name directory nil
664 (tramp-flush-file-property v (file-name-directory localname))
665 (tramp-flush-directory-property v localname)
667 (tramp-gvfs-send-command
668 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
669 (tramp-gvfs-url-file-name directory))
670 ;; Propagate the error.
671 (with-current-buffer (tramp-get-connection-buffer v)
672 (goto-char (point-min))
673 (tramp-error-with-buffer
674 nil v 'file-error "Couldn't delete %s" directory)))))
676 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
677 "Like `delete-file' for Tramp files."
678 (with-parsed-tramp-file-name filename nil
679 (tramp-flush-file-property v (file-name-directory localname))
680 (tramp-flush-directory-property v localname)
682 (tramp-gvfs-send-command
683 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
684 (tramp-gvfs-url-file-name filename))
685 ;; Propagate the error.
686 (with-current-buffer (tramp-get-connection-buffer v)
687 (goto-char (point-min))
688 (tramp-error-with-buffer
689 nil v 'file-error "Couldn't delete %s" filename)))))
691 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
692 "Like `expand-file-name' for Tramp files."
693 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
694 (setq dir (or dir default-directory "/"))
695 ;; Unless NAME is absolute, concat DIR and NAME.
696 (unless (file-name-absolute-p name)
697 (setq name (concat (file-name-as-directory dir) name)))
698 ;; If NAME is not a Tramp file, run the real handler.
699 (if (not (tramp-tramp-file-p name))
700 (tramp-run-real-handler 'expand-file-name (list name nil))
702 (with-parsed-tramp-file-name name nil
703 ;; If there is a default location, expand tilde.
704 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
706 (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
709 (tramp-get-file-property v "/" "default-location" "~")
711 ;; Tilde expansion is not possible.
712 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
715 "Cannot expand tilde in file `%s'" name))
716 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
717 (setq localname (concat "/" localname)))
718 ;; We do not pass "/..".
719 (if (string-equal "smb" method)
720 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
721 (setq localname (replace-match "/" t t localname 1)))
722 (when (string-match "^/\\.\\./?" localname)
723 (setq localname (replace-match "/" t t localname))))
724 ;; There might be a double slash. Remove this.
725 (while (string-match "//" localname)
726 (setq localname (replace-match "/" t t localname)))
727 ;; No tilde characters in file name, do normal
728 ;; `expand-file-name' (this does "/./" and "/../").
729 (tramp-make-tramp-file-name
731 (tramp-run-real-handler
732 'expand-file-name (list localname))))))
734 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
735 "Like `file-attributes' for Tramp files."
736 (unless id-format (setq id-format 'integer))
737 ;; Don't modify `last-coding-system-used' by accident.
738 (let ((last-coding-system-used last-coding-system-used)
739 dirp res-symlink-target res-numlinks res-uid res-gid res-access
740 res-mod res-change res-size res-filemodes res-inode res-device)
741 (with-parsed-tramp-file-name filename nil
742 (with-tramp-file-property
743 v localname (format "file-attributes-%s" id-format)
744 (tramp-message v 5 "file attributes: %s" localname)
745 (tramp-gvfs-send-command
746 v "gvfs-info" (tramp-gvfs-url-file-name filename))
748 (with-current-buffer (tramp-get-connection-buffer v)
749 (goto-char (point-min))
750 (when (re-search-forward "attributes:" nil t)
751 ;; ... directory or symlink
752 (goto-char (point-min))
753 (setq dirp (if (re-search-forward "type:\\s-+directory" nil t) t))
754 (goto-char (point-min))
755 (setq res-symlink-target
756 (if (re-search-forward
757 "standard::symlink-target:\\s-+\\(\\S-+\\)" nil t)
760 (goto-char (point-min))
762 (if (re-search-forward "unix::nlink:\\s-+\\([0-9]+\\)" nil t)
763 (string-to-number (match-string 1)) 0))
765 (goto-char (point-min))
767 (or (if (eq id-format 'integer)
768 (if (re-search-forward
769 "unix::uid:\\s-+\\([0-9]+\\)" nil t)
770 (string-to-number (match-string 1)))
771 (if (re-search-forward
772 "owner::user:\\s-+\\(\\S-+\\)" nil t)
774 (tramp-get-local-uid id-format)))
776 (or (if (eq id-format 'integer)
777 (if (re-search-forward
778 "unix::gid:\\s-+\\([0-9]+\\)" nil t)
779 (string-to-number (match-string 1)))
780 (if (re-search-forward
781 "owner::group:\\s-+\\(\\S-+\\)" nil t)
783 (tramp-get-local-gid id-format)))
784 ;; ... last access, modification and change time
785 (goto-char (point-min))
787 (if (re-search-forward
788 "time::access:\\s-+\\([0-9]+\\)" nil t)
789 (seconds-to-time (string-to-number (match-string 1)))
791 (goto-char (point-min))
793 (if (re-search-forward
794 "time::modified:\\s-+\\([0-9]+\\)" nil t)
795 (seconds-to-time (string-to-number (match-string 1)))
797 (goto-char (point-min))
799 (if (re-search-forward
800 "time::changed:\\s-+\\([0-9]+\\)" nil t)
801 (seconds-to-time (string-to-number (match-string 1)))
804 (goto-char (point-min))
806 (if (re-search-forward
807 "standard::size:\\s-+\\([0-9]+\\)" nil t)
808 (string-to-number (match-string 1)) 0))
809 ;; ... file mode flags
810 (goto-char (point-min))
812 (if (re-search-forward "unix::mode:\\s-+\\([0-9]+\\)" nil t)
813 (tramp-file-mode-from-int (match-string 1))
814 (if dirp "drwx------" "-rwx------")))
815 ;; ... inode and device
816 (goto-char (point-min))
818 (if (re-search-forward "unix::inode:\\s-+\\([0-9]+\\)" nil t)
819 (string-to-number (match-string 1))
820 (tramp-get-inode v)))
821 (goto-char (point-min))
823 (if (re-search-forward "unix::device:\\s-+\\([0-9]+\\)" nil t)
824 (string-to-number (match-string 1))
825 (tramp-get-device v)))
827 ;; Return data gathered.
829 ;; 0. t for directory, string (name linked to) for
830 ;; symbolic link, or nil.
831 (or dirp res-symlink-target)
832 ;; 1. Number of links to file.
838 ;; 4. Last access time, as a list of integers.
839 ;; 5. Last modification time, likewise.
840 ;; 6. Last status change time, likewise.
841 res-access res-mod res-change
842 ;; 7. Size in bytes (-1, if number is out of range).
846 ;; 9. t if file's gid would change if file were deleted
851 ;; 11. Device number.
855 (defun tramp-gvfs-handle-file-directory-p (filename)
856 "Like `file-directory-p' for Tramp files."
857 (eq t (car (file-attributes filename))))
859 (defun tramp-gvfs-handle-file-executable-p (filename)
860 "Like `file-executable-p' for Tramp files."
861 (with-parsed-tramp-file-name filename nil
862 (with-tramp-file-property v localname "file-executable-p"
863 (tramp-check-cached-permissions v ?x))))
865 (defun tramp-gvfs-handle-file-local-copy (filename)
866 "Like `file-local-copy' for Tramp files."
867 (with-parsed-tramp-file-name filename nil
868 (let ((tmpfile (tramp-compat-make-temp-file filename)))
869 (unless (file-exists-p filename)
872 "Cannot make local copy of non-existing file `%s'" filename))
873 (copy-file filename tmpfile t t)
876 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
877 "Like `file-name-all-completions' for Tramp files."
878 (unless (save-match-data (string-match "/" filename))
879 (with-parsed-tramp-file-name (expand-file-name directory) nil
886 ;; Try cache entries for filename, filename with last
887 ;; character removed, filename with last two characters
888 ;; removed, ..., and finally the empty string - all
889 ;; concatenated to the local directory name.
890 (let ((remote-file-name-inhibit-cache
891 (or remote-file-name-inhibit-cache
892 tramp-completion-reread-directory-timeout)))
894 ;; This is inefficient for very long filenames, pity
895 ;; `reduce' is not available...
902 (tramp-get-file-property
904 (concat localname (substring filename 0 x))
905 "file-name-all-completions"
907 (when cache-hit (list cache-hit))))
908 ;; We cannot use a length of 0, because file properties
909 ;; for "foo" and "foo/" are identical.
910 (tramp-compat-number-sequence (length filename) 1 -1)))))
912 ;; Cache expired or no matching cache entry found so we need
913 ;; to perform a remote operation.
914 (let ((result '("." ".."))
916 ;; Get a list of directories and files.
917 (tramp-gvfs-send-command
918 v "gvfs-ls" (tramp-gvfs-url-file-name directory))
920 ;; Now grab the output.
922 (insert-buffer-substring (tramp-get-connection-buffer v))
923 (goto-char (point-max))
924 (while (zerop (forward-line -1))
925 (setq entry (buffer-substring (point) (point-at-eol)))
926 (when (string-match filename entry)
927 (if (file-directory-p (expand-file-name entry directory))
928 (push (concat entry "/") result)
929 (push entry result)))))
931 ;; Because the remote op went through OK we know the
932 ;; directory we `cd'-ed to exists.
933 (tramp-set-file-property v localname "file-exists-p" t)
935 ;; Because the remote op went through OK we know every
936 ;; file listed by `ls' exists.
937 (mapc (lambda (entry)
938 (tramp-set-file-property
939 v (concat localname entry) "file-exists-p" t))
942 ;; Store result in the cache.
943 (tramp-set-file-property
944 v (concat localname filename)
945 "file-name-all-completions" result))))))))
947 (defun tramp-gvfs-handle-file-readable-p (filename)
948 "Like `file-readable-p' for Tramp files."
949 (with-parsed-tramp-file-name filename nil
950 (with-tramp-file-property v localname "file-executable-p"
951 (tramp-check-cached-permissions v ?r))))
953 (defun tramp-gvfs-handle-file-writable-p (filename)
954 "Like `file-writable-p' for Tramp files."
955 (with-parsed-tramp-file-name filename nil
956 (with-tramp-file-property v localname "file-writable-p"
957 (if (file-exists-p filename)
958 (tramp-check-cached-permissions v ?w)
959 ;; If file doesn't exist, check if directory is writable.
960 (and (file-directory-p (file-name-directory filename))
961 (file-writable-p (file-name-directory filename)))))))
963 (defun tramp-gvfs-handle-insert-directory
964 (filename switches &optional wildcard full-directory-p)
965 "Like `insert-directory' for Tramp files."
966 ;; gvfs-* output is hard to parse. So we let `ls-lisp' do the job.
967 (with-parsed-tramp-file-name (expand-file-name filename) nil
968 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
970 (let (ls-lisp-use-insert-directory-program)
971 (tramp-run-real-handler
973 (list filename switches wildcard full-directory-p))))))
975 (defun tramp-gvfs-handle-insert-file-contents
976 (filename &optional visit beg end replace)
977 "Like `insert-file-contents' for Tramp files."
978 (barf-if-buffer-read-only)
979 (setq filename (expand-file-name filename))
980 (let (tmpfile result)
982 (if (not (file-exists-p filename))
983 ;; We don't raise a Tramp error, because it might be
984 ;; suppressed, like in `find-file-noselect-1'.
985 (signal 'file-error (list "File not found on remote host" filename))
987 (setq tmpfile (file-local-copy filename)
988 result (insert-file-contents tmpfile visit beg end replace)))
991 (setq buffer-file-name filename)
992 (setq buffer-read-only (not (file-writable-p filename)))
993 (set-visited-file-modtime)
994 (set-buffer-modified-p nil))
995 (when (stringp tmpfile)
996 (delete-file tmpfile)))
999 (list filename (cadr result))))
1001 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1002 "Like `make-directory' for Tramp files."
1003 (with-parsed-tramp-file-name dir nil
1006 'tramp-gvfs-send-command v "gvfs-mkdir"
1008 (list "-p" (tramp-gvfs-url-file-name dir))
1009 (list (tramp-gvfs-url-file-name dir))))
1010 ;; Propagate the error.
1011 (tramp-error v 'file-error "Couldn't make directory %s" dir))))
1013 (defun tramp-gvfs-handle-rename-file
1014 (filename newname &optional ok-if-already-exists)
1015 "Like `rename-file' for Tramp files."
1016 (with-parsed-tramp-file-name
1017 (if (tramp-tramp-file-p filename) filename newname) nil
1019 (when (and (not ok-if-already-exists) (file-exists-p newname))
1021 v 'file-already-exists "File %s already exists" newname))
1023 (if (or (and (tramp-tramp-file-p filename)
1024 (not (tramp-gvfs-file-name-p filename)))
1025 (and (tramp-tramp-file-p newname)
1026 (not (tramp-gvfs-file-name-p newname))))
1028 ;; We cannot move directly.
1029 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1030 (rename-file filename tmpfile t)
1031 (rename-file tmpfile newname ok-if-already-exists))
1034 (with-tramp-progress-reporter
1035 v 0 (format "Renaming %s to %s" filename newname)
1037 (tramp-gvfs-send-command
1039 (tramp-gvfs-url-file-name filename)
1040 (tramp-gvfs-url-file-name newname))
1041 ;; Propagate the error.
1042 (with-current-buffer (tramp-get-buffer v)
1043 (goto-char (point-min))
1044 (tramp-error-with-buffer
1046 "Renaming failed, see buffer `%s' for details." (buffer-name)))))
1048 (when (file-remote-p filename)
1049 (with-parsed-tramp-file-name filename nil
1050 (tramp-flush-file-property v (file-name-directory localname))
1051 (tramp-flush-file-property v localname)))
1053 (when (file-remote-p newname)
1054 (with-parsed-tramp-file-name newname nil
1055 (tramp-flush-file-property v (file-name-directory localname))
1056 (tramp-flush-file-property v localname))))))
1058 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
1059 "Like `set-visited-file-modtime' for Tramp files."
1060 (unless (buffer-file-name)
1061 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1064 (let ((f (buffer-file-name)))
1065 (with-parsed-tramp-file-name f nil
1066 (let ((remote-file-name-inhibit-cache t)
1067 (attr (file-attributes f)))
1068 ;; '(-1 65535) means file doesn't exists yet.
1069 (setq time-list (or (nth 5 attr) '(-1 65535)))))))
1070 ;; We use '(0 0) as a don't-know value.
1071 (unless (not (equal time-list '(0 0)))
1072 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))))
1074 (defun tramp-gvfs-handle-write-region
1075 (start end filename &optional append visit lockname confirm)
1076 "Like `write-region' for Tramp files."
1077 (with-parsed-tramp-file-name filename nil
1078 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1079 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
1080 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1081 (tramp-error v 'file-error "File not overwritten")))
1083 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1084 (write-region start end tmpfile)
1086 (rename-file tmpfile filename)
1088 (delete-file tmpfile)
1090 v 'file-error "Couldn't write region to `%s'" filename))))
1092 (tramp-flush-file-property v (file-name-directory localname))
1093 (tramp-flush-file-property v localname)
1095 ;; Set file modification time.
1096 (when (or (eq visit t) (stringp visit))
1097 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1100 (when (or (eq visit t) (null visit) (stringp visit))
1101 (tramp-message v 0 "Wrote %s" filename))
1102 (run-hooks 'tramp-handle-write-region-hook)))
1105 ;; File name conversions.
1107 (defun tramp-gvfs-url-file-name (filename)
1108 "Return FILENAME in URL syntax."
1109 ;; "/" must NOT be hexlified.
1110 (let ((url-unreserved-chars (append '(?/) url-unreserved-chars))
1115 (if (tramp-tramp-file-p filename)
1116 (with-parsed-tramp-file-name filename nil
1117 (when (and user (string-match tramp-user-with-domain-regexp user))
1119 (concat (match-string 2 user) ";" (match-string 1 user))))
1120 (url-parse-make-urlobj
1121 method (url-hexify-string user) nil
1122 (tramp-file-name-real-host v) (tramp-file-name-port v)
1123 (url-hexify-string localname) nil nil t))
1124 (url-parse-make-urlobj
1125 "file" nil nil nil nil
1126 (url-hexify-string (file-truename filename)) nil nil t))))
1127 (when (tramp-tramp-file-p filename)
1128 (with-parsed-tramp-file-name filename nil
1129 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1132 (defun tramp-gvfs-object-path (filename)
1133 "Create a D-Bus object path from FILENAME."
1134 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1136 (defun tramp-gvfs-file-name (object-path)
1137 "Retrieve file name from D-Bus OBJECT-PATH."
1138 (dbus-unescape-from-identifier
1139 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1141 (defun tramp-gvfs-fuse-file-name (filename)
1142 "Return FUSE file name, which is directly accessible."
1143 (with-parsed-tramp-file-name (expand-file-name filename) nil
1144 (tramp-gvfs-maybe-open-connection v)
1145 (let ((prefix (tramp-get-file-property v "/" "prefix" ""))
1147 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
1148 (unless fuse-mountpoint
1150 v 'file-error "There is no FUSE mount point for `%s'" filename))
1151 ;; We must hide the prefix, if any.
1152 (when (string-match (concat "^" (regexp-quote prefix)) localname)
1153 (setq localname (replace-match "" t t localname)))
1155 v 10 "remote file `%s' is local file `%s'"
1156 filename (concat fuse-mountpoint localname))
1157 (concat fuse-mountpoint localname))))
1159 (defun tramp-bluez-address (device)
1160 "Return bluetooth device address from a given bluetooth DEVICE name."
1161 (when (stringp device)
1162 (if (string-match tramp-ipv6-regexp device)
1163 (match-string 0 device)
1164 (cadr (assoc device (tramp-bluez-list-devices))))))
1166 (defun tramp-bluez-device (address)
1167 "Return bluetooth device name from a given bluetooth device ADDRESS.
1168 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1169 (when (stringp address)
1170 (while (string-match "[][]" address)
1171 (setq address (replace-match "" t t address)))
1173 (dolist (item (tramp-bluez-list-devices) result)
1174 (when (string-match address (cadr item))
1175 (setq result (car item)))))))
1178 ;; D-Bus GVFS functions.
1180 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1181 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1183 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1187 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1188 (capitalize (match-string 1 message))
1194 (with-parsed-tramp-file-name filename l
1195 (when (and (zerop (length user))
1197 (zerop (logand flags tramp-gvfs-password-need-username))))
1198 (setq user (read-string "User name: ")))
1199 (when (and (zerop (length domain))
1200 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
1201 (setq domain (read-string "Domain name: ")))
1203 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1204 (setq tramp-current-method l-method
1205 tramp-current-user user
1206 tramp-current-host l-host
1207 password (tramp-read-passwd
1208 (tramp-get-connection-process l) pw-prompt))
1211 (if (stringp password)
1213 t ;; password handled.
1214 nil ;; no abort of D-Bus.
1216 (tramp-file-name-real-user l)
1218 nil ;; not anonymous.
1219 0) ;; no password save.
1220 ;; No password provided.
1221 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1223 ;; When QUIT is raised, we shall return this information to D-Bus.
1224 (quit (list nil t "" "" "" nil 0)))))
1226 (defun tramp-gvfs-handler-askquestion (message choices)
1227 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1228 (save-window-excursion
1229 (let ((enable-recursive-minibuffers t)
1233 (with-parsed-tramp-file-name
1234 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1235 (tramp-message v 6 "%S %S" message choices)
1237 ;; In theory, there can be several choices. Until now,
1238 ;; there is only the question whether to accept an unknown
1241 ;; Preserve message for `progress-reporter'.
1242 (tramp-compat-with-temp-message ""
1244 (pop-to-buffer (current-buffer))
1245 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1246 (tramp-message v 6 "%d" choice)))
1248 ;; When the choice is "no", we set a dummy fuse-mountpoint
1249 ;; in order to leave the timeout.
1250 (unless (zerop choice)
1251 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1255 nil ;; no abort of D-Bus.
1258 ;; When QUIT is raised, we shall return this information to D-Bus.
1259 (quit (list nil t 0))))))
1261 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1262 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1263 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1265 (let ((signal-name (dbus-event-member-name last-input-event))
1267 ;; Jump over the first elements of the mount info. Since there
1268 ;; were changes in the entries, we cannot access dedicated
1270 (while (stringp (car elt)) (setq elt (cdr elt)))
1271 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1272 (mount-spec (caddr elt))
1273 (default-location (tramp-gvfs-dbus-byte-array-to-string
1275 (method (tramp-gvfs-dbus-byte-array-to-string
1276 (cadr (assoc "type" (cadr mount-spec)))))
1277 (user (tramp-gvfs-dbus-byte-array-to-string
1278 (cadr (assoc "user" (cadr mount-spec)))))
1279 (domain (tramp-gvfs-dbus-byte-array-to-string
1280 (cadr (assoc "domain" (cadr mount-spec)))))
1281 (host (tramp-gvfs-dbus-byte-array-to-string
1282 (cadr (or (assoc "host" (cadr mount-spec))
1283 (assoc "server" (cadr mount-spec))))))
1284 (port (tramp-gvfs-dbus-byte-array-to-string
1285 (cadr (assoc "port" (cadr mount-spec)))))
1286 (ssl (tramp-gvfs-dbus-byte-array-to-string
1287 (cadr (assoc "ssl" (cadr mount-spec)))))
1288 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1290 (tramp-gvfs-dbus-byte-array-to-string
1291 (cadr (assoc "share" (cadr mount-spec)))))))
1292 (when (string-match "^smb" method)
1293 (setq method "smb"))
1294 (when (string-equal "obex" method)
1295 (setq host (tramp-bluez-device host)))
1296 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1297 (setq method "davs"))
1298 (unless (zerop (length domain))
1299 (setq user (concat user tramp-prefix-domain-format domain)))
1300 (unless (zerop (length port))
1301 (setq host (concat host tramp-prefix-port-format port)))
1302 (with-parsed-tramp-file-name
1303 (tramp-make-tramp-file-name method user host "") nil
1306 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1307 (tramp-set-file-property v "/" "list-mounts" 'undef)
1308 (if (string-equal (downcase signal-name) "unmounted")
1309 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1310 ;; Set prefix, mountpoint and location.
1311 (unless (string-equal prefix "/")
1312 (tramp-set-file-property v "/" "prefix" prefix))
1313 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1314 (tramp-set-file-property
1315 v "/" "default-location" default-location)))))))
1317 (when tramp-gvfs-enabled
1318 (dbus-register-signal
1319 :session nil tramp-gvfs-path-mounttracker
1320 tramp-gvfs-interface-mounttracker "mounted"
1321 'tramp-gvfs-handler-mounted-unmounted)
1322 (dbus-register-signal
1323 :session nil tramp-gvfs-path-mounttracker
1324 tramp-gvfs-interface-mounttracker "Mounted"
1325 'tramp-gvfs-handler-mounted-unmounted)
1327 (dbus-register-signal
1328 :session nil tramp-gvfs-path-mounttracker
1329 tramp-gvfs-interface-mounttracker "unmounted"
1330 'tramp-gvfs-handler-mounted-unmounted)
1331 (dbus-register-signal
1332 :session nil tramp-gvfs-path-mounttracker
1333 tramp-gvfs-interface-mounttracker "Unmounted"
1334 'tramp-gvfs-handler-mounted-unmounted))
1336 (defun tramp-gvfs-connection-mounted-p (vec)
1337 "Check, whether the location is already mounted."
1339 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1343 (with-tramp-file-property vec "/" "list-mounts"
1344 (with-tramp-dbus-call-method vec t
1345 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1346 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1348 ;; Jump over the first elements of the mount info. Since there
1349 ;; were changes in the entries, we cannot access dedicated
1351 (while (stringp (car elt)) (setq elt (cdr elt)))
1352 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1354 (mount-spec (caddr elt))
1355 (default-location (tramp-gvfs-dbus-byte-array-to-string
1357 (method (tramp-gvfs-dbus-byte-array-to-string
1358 (cadr (assoc "type" (cadr mount-spec)))))
1359 (user (tramp-gvfs-dbus-byte-array-to-string
1360 (cadr (assoc "user" (cadr mount-spec)))))
1361 (domain (tramp-gvfs-dbus-byte-array-to-string
1362 (cadr (assoc "domain" (cadr mount-spec)))))
1363 (host (tramp-gvfs-dbus-byte-array-to-string
1364 (cadr (or (assoc "host" (cadr mount-spec))
1365 (assoc "server" (cadr mount-spec))))))
1366 (port (tramp-gvfs-dbus-byte-array-to-string
1367 (cadr (assoc "port" (cadr mount-spec)))))
1368 (ssl (tramp-gvfs-dbus-byte-array-to-string
1369 (cadr (assoc "ssl" (cadr mount-spec)))))
1370 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1372 (tramp-gvfs-dbus-byte-array-to-string
1373 (cadr (assoc "share" (cadr mount-spec)))))))
1374 (when (string-match "^smb" method)
1375 (setq method "smb"))
1376 (when (string-equal "obex" method)
1377 (setq host (tramp-bluez-device host)))
1378 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1379 (setq method "davs"))
1380 (when (and (string-equal "synce" method) (zerop (length user)))
1381 (setq user (or (tramp-file-name-user vec) "")))
1382 (unless (zerop (length domain))
1383 (setq user (concat user tramp-prefix-domain-format domain)))
1384 (unless (zerop (length port))
1385 (setq host (concat host tramp-prefix-port-format port)))
1387 (string-equal method (tramp-file-name-method vec))
1388 (string-equal user (or (tramp-file-name-user vec) ""))
1389 (string-equal host (tramp-file-name-host vec))
1390 (string-match (concat "^" (regexp-quote prefix))
1391 (tramp-file-name-localname vec)))
1392 ;; Set prefix, mountpoint and location.
1393 (unless (string-equal prefix "/")
1394 (tramp-set-file-property vec "/" "prefix" prefix))
1395 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1396 (tramp-set-file-property vec "/" "default-location" default-location)
1397 (throw 'mounted t)))))))
1399 (defun tramp-gvfs-mount-spec-entry (key value)
1400 "Construct a mount-spec entry to be used in a mount_spec.
1401 It was \"a(say)\", but has changed to \"a{sv})\"."
1402 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1403 (list :dict-entry key
1404 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1405 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1407 (defun tramp-gvfs-mount-spec (vec)
1408 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1409 (let* ((method (tramp-file-name-method vec))
1410 (user (tramp-file-name-real-user vec))
1411 (domain (tramp-file-name-domain vec))
1412 (host (tramp-file-name-real-host vec))
1413 (port (tramp-file-name-port vec))
1414 (localname (tramp-file-name-localname vec))
1415 (ssl (if (string-match "^davs" method) "true" "false"))
1416 (mount-spec '(:array))
1424 ((string-equal "smb" method)
1425 (string-match "^/?\\([^/]+\\)" localname)
1426 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1427 (tramp-gvfs-mount-spec-entry "server" host)
1428 (tramp-gvfs-mount-spec-entry "share" (match-string 1 localname))))
1429 ((string-equal "obex" method)
1430 (list (tramp-gvfs-mount-spec-entry "type" method)
1431 (tramp-gvfs-mount-spec-entry
1432 "host" (concat "[" (tramp-bluez-address host) "]"))))
1433 ((string-match "^dav" method)
1434 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1435 (tramp-gvfs-mount-spec-entry "host" host)
1436 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1438 (list (tramp-gvfs-mount-spec-entry "type" method)
1439 (tramp-gvfs-mount-spec-entry "host" host))))))
1443 'mount-spec (tramp-gvfs-mount-spec-entry "user" user) 'append))
1447 'mount-spec (tramp-gvfs-mount-spec-entry "domain" domain) 'append))
1451 'mount-spec (tramp-gvfs-mount-spec-entry "port" (number-to-string port))
1454 (when (and (string-match "^dav" method)
1455 (string-match "^/?[^/]+" localname))
1456 (setq mount-pref (match-string 0 localname)))
1459 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1462 ;; Connection functions.
1464 (defun tramp-gvfs-maybe-open-connection (vec)
1465 "Maybe open a connection VEC.
1466 Does not do anything if a connection is already open, but re-opens the
1467 connection if a previous connection has died for some reason."
1469 ;; We set the file name, in case there are incoming D-Bus signals or
1471 (setq tramp-gvfs-dbus-event-vector vec)
1473 ;; For password handling, we need a process bound to the connection
1474 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1476 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1477 (let ((p (make-network-process
1478 :name (tramp-buffer-name vec)
1479 :buffer (tramp-get-connection-buffer vec)
1480 :server t :host 'local :service t)))
1481 (tramp-compat-set-process-query-on-exit-flag p nil)))
1483 (unless (tramp-gvfs-connection-mounted-p vec)
1484 (let* ((method (tramp-file-name-method vec))
1485 (user (tramp-file-name-user vec))
1486 (host (tramp-file-name-host vec))
1487 (localname (tramp-file-name-localname vec))
1489 (tramp-gvfs-object-path
1490 (tramp-make-tramp-file-name method user host ""))))
1492 (when (and (string-equal method "smb")
1493 (string-equal localname "/"))
1494 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1496 (with-tramp-progress-reporter
1498 (if (zerop (length user))
1499 (format "Opening connection for %s using %s" host method)
1500 (format "Opening connection for %s@%s using %s" user host method))
1502 ;; Enable auth-source and password-cache.
1503 (tramp-set-connection-property vec "first-password-request" t)
1505 ;; There will be a callback of "askPassword" when a password is
1507 (dbus-register-method
1508 :session dbus-service-emacs object-path
1509 tramp-gvfs-interface-mountoperation "askPassword"
1510 'tramp-gvfs-handler-askpassword)
1511 (dbus-register-method
1512 :session dbus-service-emacs object-path
1513 tramp-gvfs-interface-mountoperation "AskPassword"
1514 'tramp-gvfs-handler-askpassword)
1516 ;; There could be a callback of "askQuestion" when adding fingerprint.
1517 (dbus-register-method
1518 :session dbus-service-emacs object-path
1519 tramp-gvfs-interface-mountoperation "askQuestion"
1520 'tramp-gvfs-handler-askquestion)
1521 (dbus-register-method
1522 :session dbus-service-emacs object-path
1523 tramp-gvfs-interface-mountoperation "AskQuestion"
1524 'tramp-gvfs-handler-askquestion)
1526 ;; The call must be asynchronously, because of the "askPassword"
1527 ;; or "askQuestion"callbacks.
1528 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1529 (with-tramp-dbus-call-method vec nil
1530 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1531 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1532 (tramp-gvfs-mount-spec vec)
1533 `(:struct :string ,(dbus-get-unique-name :session)
1534 :object-path ,object-path))
1535 (with-tramp-dbus-call-method vec nil
1536 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1537 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1538 (tramp-gvfs-mount-spec vec)
1539 :string (dbus-get-unique-name :session) :object-path object-path))
1541 ;; We must wait, until the mount is applied. This will be
1542 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1545 ((or (tramp-get-method-parameter method 'tramp-connection-timeout)
1546 tramp-connection-timeout)
1547 (if (zerop (length (tramp-file-name-user vec)))
1550 "Timeout reached mounting %s using %s" host method)
1553 "Timeout reached mounting %s@%s using %s" user host method)))
1554 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1555 (read-event nil nil 0.1)))
1557 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1558 ;; is marked with the fuse-mountpoint "/". We shall react.
1560 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1561 (tramp-error vec 'file-error "FUSE mount denied"))
1563 ;; In `tramp-check-cached-permissions', the connection
1564 ;; properties {uig,gid}-{integer,string} are used. We set
1565 ;; them to their local counterparts.
1566 (tramp-set-connection-property
1567 vec "uid-integer" (tramp-get-local-uid 'integer))
1568 (tramp-set-connection-property
1569 vec "gid-integer" (tramp-get-local-gid 'integer))
1570 (tramp-set-connection-property
1571 vec "uid-string" (tramp-get-local-uid 'string))
1572 (tramp-set-connection-property
1573 vec "gid-string" (tramp-get-local-gid 'string))))))
1575 (defun tramp-gvfs-send-command (vec command &rest args)
1576 "Send the COMMAND with its ARGS to connection VEC.
1577 COMMAND is usually a command from the gvfs-* utilities.
1578 `call-process' is applied, and it returns `t' if the return code is zero."
1580 (with-current-buffer (tramp-get-connection-buffer vec)
1581 (tramp-gvfs-maybe-open-connection vec)
1583 (tramp-message vec 6 "%s %s" command (mapconcat 'identity args " "))
1584 (setq result (apply 'tramp-call-process command nil t nil args))
1585 (tramp-message vec 6 "\n%s" (buffer-string))
1589 ;; D-Bus BLUEZ functions.
1591 (defun tramp-bluez-list-devices ()
1592 "Return all discovered bluetooth devices as list.
1593 Every entry is a list (NAME ADDRESS).
1595 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1596 discovery happened more time before indicated there, a rescan will be
1597 started, which lasts some ten seconds. Otherwise, cached results will
1599 ;; Reset the scanned devices list if time has passed.
1600 (and (integerp tramp-bluez-discover-devices-timeout)
1601 (integerp tramp-bluez-discovery)
1602 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1603 tramp-bluez-discover-devices-timeout)
1604 (setq tramp-bluez-devices nil))
1606 ;; Rescan if needed.
1607 (unless tramp-bluez-devices
1609 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1610 :system tramp-bluez-service "/"
1611 tramp-bluez-interface-manager "DefaultAdapter")))
1612 (setq tramp-bluez-devices nil
1613 tramp-bluez-discovery t)
1614 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1615 :system tramp-bluez-service object-path
1616 tramp-bluez-interface-adapter "StartDiscovery")
1617 (while tramp-bluez-discovery
1618 (read-event nil nil 0.1))))
1619 (setq tramp-bluez-discovery (current-time))
1620 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1621 tramp-bluez-devices)
1623 (defun tramp-bluez-property-changed (property value)
1624 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1625 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1627 ((string-equal property "Discovering")
1629 ;; "Discovering" FALSE means discovery run has been completed.
1630 ;; We stop it, because we don't need another run.
1631 (setq tramp-bluez-discovery nil)
1632 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1633 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1634 tramp-bluez-interface-adapter "StopDiscovery")))))
1636 (dbus-register-signal
1637 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1638 'tramp-bluez-property-changed)
1640 (defun tramp-bluez-device-found (device args)
1641 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1642 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1643 (let ((alias (car (cadr (assoc "Alias" args))))
1644 (address (car (cadr (assoc "Address" args)))))
1645 ;; Maybe we shall check the device class for being a proper
1646 ;; device, and call also SDP in order to find the obex service.
1647 (add-to-list 'tramp-bluez-devices (list alias address))))
1649 (dbus-register-signal
1650 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1651 'tramp-bluez-device-found)
1653 (defun tramp-bluez-parse-device-names (ignore)
1654 "Return a list of (nil host) tuples allowed to access."
1656 (lambda (x) (list nil (car x)))
1657 (tramp-bluez-list-devices)))
1659 ;; Add completion function for OBEX method.
1660 (when (member tramp-bluez-service (dbus-list-known-names :system))
1661 (tramp-set-completion-function
1662 "obex" '((tramp-bluez-parse-device-names ""))))
1665 ;; D-Bus zeroconf functions.
1667 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1668 "Return a list of (user host) tuples allowed to access."
1671 (list nil (zeroconf-service-host x)))
1672 (zeroconf-list-services "_workstation._tcp")))
1674 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1675 "Return a list of (user host) tuples allowed to access."
1678 (let ((host (zeroconf-service-host x))
1679 (port (zeroconf-service-port x))
1680 (text (zeroconf-service-txt x))
1683 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1684 ;; A user is marked in a TXT field like "u=guest".
1686 (when (string-match "u=\\(.+\\)$" (car text))
1687 (setq user (match-string 1 (car text))))
1688 (setq text (cdr text)))
1690 (zeroconf-list-services "_webdav._tcp")))
1692 ;; Add completion function for DAV and DAVS methods.
1693 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1694 (zeroconf-init tramp-gvfs-zeroconf-domain)
1695 (tramp-set-completion-function
1696 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1697 (tramp-set-completion-function
1698 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1699 (tramp-set-completion-function
1700 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1703 ;; D-Bus SYNCE functions.
1705 (defun tramp-synce-list-devices ()
1706 "Return all discovered synce devices as list.
1707 They are retrieved from the hal daemon."
1708 (let (tramp-synce-devices)
1710 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1711 :system tramp-hal-service tramp-hal-path-manager
1712 tramp-hal-interface-manager "GetAllDevices"))
1713 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1714 :system tramp-hal-service device tramp-hal-interface-device
1715 "PropertyExists" "sync.plugin")
1717 'tramp-synce-devices
1718 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1719 :system tramp-hal-service device tramp-hal-interface-device
1720 "GetPropertyString" "pda.pocketpc.name"))))
1721 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1722 tramp-synce-devices))
1724 (defun tramp-synce-parse-device-names (ignore)
1725 "Return a list of (nil host) tuples allowed to access."
1727 (lambda (x) (list nil x))
1728 (tramp-synce-list-devices)))
1730 ;; Add completion function for SYNCE method.
1731 (tramp-set-completion-function
1732 "synce" '((tramp-synce-parse-device-names "")))
1734 (add-hook 'tramp-unload-hook
1736 (unload-feature 'tramp-gvfs 'force)))
1738 (provide 'tramp-gvfs)
1742 ;; * Host name completion via smb-server or smb-network.
1743 ;; * Check how two shares of the same SMB server can be mounted in
1745 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1747 ;; * Implement obex for other serial communication but bluetooth.
1749 ;;; tramp-gvfs.el ends here