1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon -*- lexical-binding:t -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
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 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 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
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 "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.
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.
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")
107 ;; Pacify byte-compiler.
112 (defcustom tramp-gvfs-methods
113 '("afp" "dav" "davs" "gdrive" "obex" "sftp" "synce")
114 "List of methods for remote files, accessed with GVFS."
117 :type
'(repeat (choice (const "afp")
128 ;; Add defaults for `tramp-default-user-alist' and `tramp-default-host-alist'.
130 (when (string-match "\\(.+\\)@\\(\\(?:gmail\\|googlemail\\)\\.com\\)"
132 (add-to-list 'tramp-default-user-alist
133 `("\\`gdrive\\'" nil
,(match-string 1 user-mail-address
)))
134 (add-to-list 'tramp-default-host-alist
135 '("\\`gdrive\\'" nil
,(match-string 2 user-mail-address
))))
137 (add-to-list 'tramp-default-user-alist
'("\\`synce\\'" nil nil
))
140 (defcustom tramp-gvfs-zeroconf-domain
"local"
141 "Zeroconf domain to be used for discovering services, like host names."
147 ;; Add the methods to `tramp-methods', in order to allow minibuffer
150 (when (featurep 'dbusbind
)
151 (dolist (elt tramp-gvfs-methods
)
152 (unless (assoc elt tramp-methods
)
153 (add-to-list 'tramp-methods
(cons elt nil
)))))
155 (defconst tramp-gvfs-path-tramp
(concat dbus-path-emacs
"/Tramp")
156 "The preceding object path for own objects.")
158 (defconst tramp-gvfs-service-daemon
"org.gtk.vfs.Daemon"
159 "The well known name of the GVFS daemon.")
161 ;; We don't call `dbus-ping', because this would load dbus.el.
162 (defconst tramp-gvfs-enabled
164 (and (featurep 'dbusbind
)
165 (tramp-compat-funcall 'dbus-get-unique-name
:system
)
166 (tramp-compat-funcall 'dbus-get-unique-name
:session
)
167 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
168 (tramp-compat-process-running-p "gvfsd-fuse"))))
169 "Non-nil when GVFS is available.")
171 (defconst tramp-gvfs-path-mounttracker
"/org/gtk/vfs/mounttracker"
172 "The object path of the GVFS daemon.")
174 (defconst tramp-gvfs-interface-mounttracker
"org.gtk.vfs.MountTracker"
175 "The mount tracking interface in the GVFS daemon.")
177 ;; Introspection data exist since GVFS 1.14. If there are no such
178 ;; data, we expect an earlier interface.
179 (defconst tramp-gvfs-methods-mounttracker
180 (and tramp-gvfs-enabled
181 (dbus-introspect-get-method-names
182 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
183 tramp-gvfs-interface-mounttracker
))
184 "The list of supported methods of the mount tracking interface.")
186 (defconst tramp-gvfs-listmounts
187 (if (member "ListMounts" tramp-gvfs-methods-mounttracker
)
190 "The name of the \"listMounts\" method.
191 It has been changed in GVFS 1.14.")
193 (defconst tramp-gvfs-mountlocation
194 (if (member "MountLocation" tramp-gvfs-methods-mounttracker
)
197 "The name of the \"mountLocation\" method.
198 It has been changed in GVFS 1.14.")
200 (defconst tramp-gvfs-mountlocation-signature
201 (and tramp-gvfs-enabled
202 (dbus-introspect-get-signature
203 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
204 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
))
205 "The D-Bus signature of the \"mountLocation\" method.
206 It has been changed in GVFS 1.14.")
208 ;; <interface name='org.gtk.vfs.MountTracker'>
209 ;; <method name='listMounts'>
210 ;; <arg name='mount_info_list'
211 ;; type='a{sosssssbay{aya{say}}ay}'
214 ;; <method name='mountLocation'>
215 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
216 ;; <arg name='dbus_id' type='s' direction='in'/>
217 ;; <arg name='object_path' type='o' direction='in'/>
219 ;; <signal name='mounted'>
220 ;; <arg name='mount_info'
221 ;; type='{sosssssbay{aya{say}}ay}'/>
223 ;; <signal name='unmounted'>
224 ;; <arg name='mount_info'
225 ;; type='{sosssssbay{aya{say}}ay}'/>
231 ;; OBJECT_PATH object_path
232 ;; STRING display_name
233 ;; STRING stable_name
234 ;; STRING x_content_types Since GVFS 1.0 only !!!
236 ;; STRING preferred_filename_encoding
237 ;; BOOLEAN user_visible
238 ;; ARRAY BYTE fuse_mountpoint
240 ;; ARRAY BYTE mount_prefix
242 ;; STRUCT mount_spec_item
243 ;; STRING key (type, user, domain, host, server,
244 ;; share, volume, port, ssl)
246 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
248 (defconst tramp-gvfs-interface-mountoperation
"org.gtk.vfs.MountOperation"
249 "Used by the dbus-proxying implementation of GMountOperation.")
251 ;; <interface name='org.gtk.vfs.MountOperation'>
252 ;; <method name='askPassword'>
253 ;; <arg name='message' type='s' direction='in'/>
254 ;; <arg name='default_user' type='s' direction='in'/>
255 ;; <arg name='default_domain' type='s' direction='in'/>
256 ;; <arg name='flags' type='u' direction='in'/>
257 ;; <arg name='handled' type='b' direction='out'/>
258 ;; <arg name='aborted' type='b' direction='out'/>
259 ;; <arg name='password' type='s' direction='out'/>
260 ;; <arg name='username' type='s' direction='out'/>
261 ;; <arg name='domain' type='s' direction='out'/>
262 ;; <arg name='anonymous' type='b' direction='out'/>
263 ;; <arg name='password_save' type='u' direction='out'/>
265 ;; <method name='askQuestion'>
266 ;; <arg name='message' type='s' direction='in'/>
267 ;; <arg name='choices' type='as' direction='in'/>
268 ;; <arg name='handled' type='b' direction='out'/>
269 ;; <arg name='aborted' type='b' direction='out'/>
270 ;; <arg name='choice' type='u' direction='out'/>
274 ;; The following flags are used in "askPassword". They are defined in
275 ;; /usr/include/glib-2.0/gio/gioenums.h.
277 (defconst tramp-gvfs-password-need-password
1
278 "Operation requires a password.")
280 (defconst tramp-gvfs-password-need-username
2
281 "Operation requires a username.")
283 (defconst tramp-gvfs-password-need-domain
4
284 "Operation requires a domain.")
286 (defconst tramp-gvfs-password-saving-supported
8
287 "Operation supports saving settings.")
289 (defconst tramp-gvfs-password-anonymous-supported
16
290 "Operation supports anonymous users.")
292 (defconst tramp-bluez-service
"org.bluez"
293 "The well known name of the BLUEZ service.")
295 (defconst tramp-bluez-interface-manager
"org.bluez.Manager"
296 "The manager interface of the BLUEZ daemon.")
298 ;; <interface name='org.bluez.Manager'>
299 ;; <method name='DefaultAdapter'>
300 ;; <arg type='o' direction='out'/>
302 ;; <method name='FindAdapter'>
303 ;; <arg type='s' direction='in'/>
304 ;; <arg type='o' direction='out'/>
306 ;; <method name='ListAdapters'>
307 ;; <arg type='ao' direction='out'/>
309 ;; <signal name='AdapterAdded'>
312 ;; <signal name='AdapterRemoved'>
315 ;; <signal name='DefaultAdapterChanged'>
320 (defconst tramp-bluez-interface-adapter
"org.bluez.Adapter"
321 "The adapter interface of the BLUEZ daemon.")
323 ;; <interface name='org.bluez.Adapter'>
324 ;; <method name='GetProperties'>
325 ;; <arg type='a{sv}' direction='out'/>
327 ;; <method name='SetProperty'>
328 ;; <arg type='s' direction='in'/>
329 ;; <arg type='v' direction='in'/>
331 ;; <method name='RequestMode'>
332 ;; <arg type='s' direction='in'/>
334 ;; <method name='ReleaseMode'/>
335 ;; <method name='RequestSession'/>
336 ;; <method name='ReleaseSession'/>
337 ;; <method name='StartDiscovery'/>
338 ;; <method name='StopDiscovery'/>
339 ;; <method name='ListDevices'>
340 ;; <arg type='ao' direction='out'/>
342 ;; <method name='CreateDevice'>
343 ;; <arg type='s' direction='in'/>
344 ;; <arg type='o' direction='out'/>
346 ;; <method name='CreatePairedDevice'>
347 ;; <arg type='s' direction='in'/>
348 ;; <arg type='o' direction='in'/>
349 ;; <arg type='s' direction='in'/>
350 ;; <arg type='o' direction='out'/>
352 ;; <method name='CancelDeviceCreation'>
353 ;; <arg type='s' direction='in'/>
355 ;; <method name='RemoveDevice'>
356 ;; <arg type='o' direction='in'/>
358 ;; <method name='FindDevice'>
359 ;; <arg type='s' direction='in'/>
360 ;; <arg type='o' direction='out'/>
362 ;; <method name='RegisterAgent'>
363 ;; <arg type='o' direction='in'/>
364 ;; <arg type='s' direction='in'/>
366 ;; <method name='UnregisterAgent'>
367 ;; <arg type='o' direction='in'/>
369 ;; <signal name='DeviceCreated'>
372 ;; <signal name='DeviceRemoved'>
375 ;; <signal name='DeviceFound'>
377 ;; <arg type='a{sv}'/>
379 ;; <signal name='PropertyChanged'>
383 ;; <signal name='DeviceDisappeared'>
389 (defcustom tramp-bluez-discover-devices-timeout
60
390 "Defines seconds since last bluetooth device discovery before rescanning.
391 A value of 0 would require an immediate discovery during hostname
392 completion, nil means to use always cached values for discovered
396 :type
'(choice (const nil
) integer
)
399 (defvar tramp-bluez-discovery nil
400 "Indicator for a running bluetooth device discovery.
401 It keeps the timestamp of last discovery.")
403 (defvar tramp-bluez-devices nil
404 "Alist of detected bluetooth devices.
405 Every entry is a list (NAME ADDRESS).")
407 (defconst tramp-hal-service
"org.freedesktop.Hal"
408 "The well known name of the HAL service.")
410 (defconst tramp-hal-path-manager
"/org/freedesktop/Hal/Manager"
411 "The object path of the HAL daemon manager.")
413 (defconst tramp-hal-interface-manager
"org.freedesktop.Hal.Manager"
414 "The manager interface of the HAL daemon.")
416 (defconst tramp-hal-interface-device
"org.freedesktop.Hal.Device"
417 "The device interface of the HAL daemon.")
419 (defconst tramp-gvfs-file-attributes
422 "standard::display-name"
423 "standard::symlink-target"
436 "access::can-execute"
439 "GVFS file attributes.")
441 (defconst tramp-gvfs-file-attributes-with-gvfs-ls-regexp
442 (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t
) "=\\(.+?\\)")
443 "Regexp to parse GVFS file attributes with `gvfs-ls'.")
445 (defconst tramp-gvfs-file-attributes-with-gvfs-info-regexp
446 (concat "^[[:blank:]]*"
447 (regexp-opt tramp-gvfs-file-attributes t
)
448 ":[[:blank:]]+\\(.*\\)$")
449 "Regexp to parse GVFS file attributes with `gvfs-info'.")
452 ;; New handlers should be added here.
454 (defconst tramp-gvfs-file-name-handler-alist
455 '((access-file . ignore
)
456 (add-name-to-file . tramp-gvfs-handle-copy-file
)
457 ;; `byte-compiler-base-file-name' performed by default handler.
458 ;; `copy-directory' performed by default handler.
459 (copy-file . tramp-gvfs-handle-copy-file
)
460 (delete-directory . tramp-gvfs-handle-delete-directory
)
461 (delete-file . tramp-gvfs-handle-delete-file
)
462 ;; `diff-latest-backup-file' performed by default handler.
463 (directory-file-name . tramp-handle-directory-file-name
)
464 (directory-files . tramp-handle-directory-files
)
465 (directory-files-and-attributes
466 . tramp-handle-directory-files-and-attributes
)
467 (dired-compress-file . ignore
)
468 (dired-uncache . tramp-handle-dired-uncache
)
469 (expand-file-name . tramp-gvfs-handle-expand-file-name
)
470 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
472 (file-attributes . tramp-gvfs-handle-file-attributes
)
473 (file-directory-p . tramp-gvfs-handle-file-directory-p
)
474 (file-equal-p . tramp-handle-file-equal-p
)
475 (file-executable-p . tramp-gvfs-handle-file-executable-p
)
476 (file-exists-p . tramp-handle-file-exists-p
)
477 (file-in-directory-p . tramp-handle-file-in-directory-p
)
478 (file-local-copy . tramp-gvfs-handle-file-local-copy
)
479 (file-modes . tramp-handle-file-modes
)
480 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions
)
481 (file-name-as-directory . tramp-handle-file-name-as-directory
)
482 (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p
)
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.
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
)
535 (tramp-file-name-method (tramp-dissect-file-name filename
))))
536 (and (stringp method
) (member method tramp-gvfs-methods
)))))
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-compat-user-error nil
"Package `tramp-gvfs' not supported"))
545 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist
)))
547 (save-match-data (apply (cdr fn
) args
))
548 (tramp-run-real-handler operation args
))))
551 (when (featurep 'dbusbind
)
552 (tramp-register-foreign-file-name-handler
553 'tramp-gvfs-file-name-p
'tramp-gvfs-file-name-handler
))
556 ;; D-Bus helper function.
558 (defun tramp-gvfs-dbus-string-to-byte-array (string)
559 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
560 (dbus-string-to-byte-array
561 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature
)
562 (concat string
(string 0)) string
)))
564 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
565 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
566 ;; The byte array could be a variant. Take care.
568 (if (and (consp byte-array
) (atom (car byte-array
)))
569 byte-array
(car byte-array
))))
570 (dbus-byte-array-to-string
571 (if (and (consp byte-array
) (zerop (car (last byte-array
))))
572 (butlast byte-array
) byte-array
))))
574 (defun tramp-gvfs-stringify-dbus-message (message)
575 "Convert a D-Bus message into readable UTF8 strings, used for traces."
577 ((and (consp message
) (characterp (car message
)))
578 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message
)))
580 (mapcar 'tramp-gvfs-stringify-dbus-message message
))
582 (format "%S" message
))
585 (defmacro with-tramp-dbus-call-method
586 (vec synchronous bus service path interface method
&rest args
)
587 "Apply a D-Bus call on bus BUS.
589 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
590 it is an asynchronous call, with `ignore' as callback function.
592 The other arguments have the same meaning as with `dbus-call-method'
593 or `dbus-call-method-asynchronously'. Additionally, the call
594 will be traced by Tramp with trace level 6."
595 `(let ((func (if ,synchronous
596 'dbus-call-method
'dbus-call-method-asynchronously
))
597 (args (append (list ,bus
,service
,path
,interface
,method
)
598 (if ,synchronous
(list ,@args
) (list 'ignore
,@args
))))
600 (tramp-message ,vec
6 "%s %s" func args
)
601 (setq result
(apply func args
))
602 (tramp-message ,vec
6 "%s" (tramp-gvfs-stringify-dbus-message result
))
605 (put 'with-tramp-dbus-call-method
'lisp-indent-function
2)
606 (put 'with-tramp-dbus-call-method
'edebug-form-spec
'(form symbolp body
))
607 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-tramp-dbus-call-method\\>"))
609 (defvar tramp-gvfs-dbus-event-vector nil
610 "Current Tramp file name to be used, as vector.
611 It is needed when D-Bus signals or errors arrive, because there
612 is no information where to trace the message.")
614 (defun tramp-gvfs-dbus-event-error (event err
)
615 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
616 (when tramp-gvfs-dbus-event-vector
617 (tramp-message tramp-gvfs-dbus-event-vector
10 "%S" event
)
618 (tramp-error tramp-gvfs-dbus-event-vector
'file-error
"%s" (cadr err
))))
620 ;; `dbus-event-error-hooks' has been renamed to
621 ;; `dbus-event-error-functions' in Emacs 24.3.
623 (if (boundp 'dbus-event-error-functions
)
624 'dbus-event-error-functions
'dbus-event-error-hooks
)
625 'tramp-gvfs-dbus-event-error
)
628 ;; File name primitives.
630 (defun tramp-gvfs-do-copy-or-rename-file
631 (op filename newname
&optional ok-if-already-exists keep-date
632 preserve-uid-gid preserve-extended-attributes
)
633 "Copy or rename a remote file.
634 OP must be `copy' or `rename' and indicates the operation to perform.
635 FILENAME specifies the file to copy or rename, NEWNAME is the name of
636 the new file (for copy) or the new name of the file (for rename).
637 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
638 KEEP-DATE means to make sure that NEWNAME has the same timestamp
639 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
640 the uid and gid if both files are on the same host.
641 PRESERVE-EXTENDED-ATTRIBUTES is ignored.
643 This function is invoked by `tramp-gvfs-handle-copy-file' and
644 `tramp-gvfs-handle-rename-file'. It is an error if OP is neither
645 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
647 (unless (memq op
'(copy rename
))
648 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
650 (let ((t1 (tramp-tramp-file-p filename
))
651 (t2 (tramp-tramp-file-p newname
))
652 (equal-remote (tramp-equal-remote filename newname
))
653 (file-operation (intern (format "%s-file" op
)))
654 (gvfs-operation (if (eq op
'copy
) "gvfs-copy" "gvfs-move"))
655 (msg-operation (if (eq op
'copy
) "Copying" "Renaming")))
657 (with-parsed-tramp-file-name (if t1 filename newname
) nil
658 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
660 v
'file-already-exists
"File %s already exists" newname
))
662 (if (or (and equal-remote
663 (tramp-get-connection-property v
"direct-copy-failed" nil
))
664 (and t1
(not (tramp-gvfs-file-name-p filename
)))
665 (and t2
(not (tramp-gvfs-file-name-p newname
))))
667 ;; We cannot copy or rename directly.
668 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
670 file-operation filename tmpfile t keep-date preserve-uid-gid
671 preserve-extended-attributes
)
672 (rename-file tmpfile newname ok-if-already-exists
))
675 (with-tramp-progress-reporter
676 v
0 (format "%s %s to %s" msg-operation filename newname
)
679 'tramp-gvfs-send-command v gvfs-operation
681 (and (eq op
'copy
) (or keep-date preserve-uid-gid
)
684 (tramp-gvfs-url-file-name filename
)
685 (tramp-gvfs-url-file-name newname
))))
687 (if (or (not equal-remote
)
689 (tramp-get-connection-property
690 v
"direct-copy-failed" nil
)))
691 ;; Propagate the error.
692 (with-current-buffer (tramp-get-connection-buffer v
)
693 (goto-char (point-min))
694 (tramp-error-with-buffer
696 "%s failed, see buffer `%s' for details."
697 msg-operation
(buffer-name)))
699 ;; Some WebDAV server, like the one from QNAP, do not
700 ;; support direct copy/move. Try a fallback.
701 (tramp-set-connection-property v
"direct-copy-failed" t
)
702 (tramp-gvfs-do-copy-or-rename-file
703 op filename newname ok-if-already-exists keep-date
704 preserve-uid-gid preserve-extended-attributes
))))
706 (when (and t1
(eq op
'rename
))
707 (with-parsed-tramp-file-name filename nil
708 (tramp-flush-file-property v
(file-name-directory localname
))
709 (tramp-flush-file-property v localname
)))
712 (with-parsed-tramp-file-name newname nil
713 (tramp-flush-file-property v
(file-name-directory localname
))
714 (tramp-flush-file-property v localname
)))))))
716 (defun tramp-gvfs-handle-copy-file
717 (filename newname
&optional ok-if-already-exists keep-date
718 preserve-uid-gid preserve-extended-attributes
)
719 "Like `copy-file' for Tramp files."
720 (setq filename
(expand-file-name filename
))
721 (setq newname
(expand-file-name newname
))
722 ;; At least one file a Tramp file?
723 (if (or (tramp-tramp-file-p filename
)
724 (tramp-tramp-file-p newname
))
725 (tramp-gvfs-do-copy-or-rename-file
726 'copy filename newname ok-if-already-exists keep-date
727 preserve-uid-gid preserve-extended-attributes
)
728 (tramp-run-real-handler
730 (list filename newname ok-if-already-exists keep-date
731 preserve-uid-gid preserve-extended-attributes
))))
733 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash
)
734 "Like `delete-directory' for Tramp files."
735 (with-parsed-tramp-file-name directory nil
736 (if (and recursive
(not (file-symlink-p directory
)))
738 (if (eq t
(tramp-compat-file-attribute-type
739 (file-attributes file
)))
740 (delete-directory file recursive trash
)
741 (delete-file file trash
)))
743 directory
'full directory-files-no-dot-files-regexp
))
744 (when (directory-files directory nil directory-files-no-dot-files-regexp
)
746 v
'file-error
"Couldn't delete non-empty %s" directory
)))
748 (tramp-flush-file-property v
(file-name-directory localname
))
749 (tramp-flush-directory-property v localname
)
751 (tramp-gvfs-send-command
752 v
(if (and trash delete-by-moving-to-trash
) "gvfs-trash" "gvfs-rm")
753 (tramp-gvfs-url-file-name directory
))
754 ;; Propagate the error.
755 (with-current-buffer (tramp-get-connection-buffer v
)
756 (goto-char (point-min))
757 (tramp-error-with-buffer
758 nil v
'file-error
"Couldn't delete %s" directory
)))))
760 (defun tramp-gvfs-handle-delete-file (filename &optional trash
)
761 "Like `delete-file' for Tramp files."
762 (with-parsed-tramp-file-name filename nil
763 (tramp-flush-file-property v
(file-name-directory localname
))
764 (tramp-flush-file-property v localname
)
766 (tramp-gvfs-send-command
767 v
(if (and trash delete-by-moving-to-trash
) "gvfs-trash" "gvfs-rm")
768 (tramp-gvfs-url-file-name filename
))
769 ;; Propagate the error.
770 (with-current-buffer (tramp-get-connection-buffer v
)
771 (goto-char (point-min))
772 (tramp-error-with-buffer
773 nil v
'file-error
"Couldn't delete %s" filename
)))))
775 (defun tramp-gvfs-handle-expand-file-name (name &optional dir
)
776 "Like `expand-file-name' for Tramp files."
777 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
778 (setq dir
(or dir default-directory
"/"))
779 ;; Unless NAME is absolute, concat DIR and NAME.
780 (unless (file-name-absolute-p name
)
781 (setq name
(concat (file-name-as-directory dir
) name
)))
782 ;; If NAME is not a Tramp file, run the real handler.
783 (if (not (tramp-tramp-file-p name
))
784 (tramp-run-real-handler 'expand-file-name
(list name nil
))
786 (with-parsed-tramp-file-name name nil
787 ;; If there is a default location, expand tilde.
788 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname
)
790 (tramp-gvfs-maybe-open-connection
791 (make-tramp-file-name
792 :method method
:user user
:domain domain
793 :host host
:port port
:localname
"/" :hop hop
)))
796 (tramp-get-connection-property v
"default-location" "~")
798 ;; Tilde expansion is not possible.
799 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
802 "Cannot expand tilde in file `%s'" name
))
803 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
804 (setq localname
(concat "/" localname
)))
805 ;; We do not pass "/..".
806 (if (string-match "^\\(afp\\|smb\\)$" method
)
807 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname
)
808 (setq localname
(replace-match "/" t t localname
1)))
809 (when (string-match "^/\\.\\./?" localname
)
810 (setq localname
(replace-match "/" t t localname
))))
811 ;; There might be a double slash. Remove this.
812 (while (string-match "//" localname
)
813 (setq localname
(replace-match "/" t t localname
)))
814 ;; No tilde characters in file name, do normal
815 ;; `expand-file-name' (this does "/./" and "/../").
816 (tramp-make-tramp-file-name
817 method user domain host port
818 (tramp-run-real-handler
819 'expand-file-name
(list localname
))))))
821 (defun tramp-gvfs-get-directory-attributes (directory)
822 "Return GVFS attributes association list of all files in DIRECTORY."
824 ;; Don't modify `last-coding-system-used' by accident.
825 (let ((last-coding-system-used last-coding-system-used
)
827 (with-parsed-tramp-file-name directory nil
828 (with-tramp-file-property v localname
"directory-gvfs-attributes"
829 (tramp-message v
5 "directory gvfs attributes: %s" localname
)
831 (tramp-gvfs-send-command
832 v
"gvfs-ls" "-h" "-n" "-a"
833 (mapconcat 'identity tramp-gvfs-file-attributes
",")
834 (tramp-gvfs-url-file-name directory
))
836 (with-current-buffer (tramp-get-connection-buffer v
)
837 (goto-char (point-min))
839 (concat "^\\(.+\\)[[:blank:]]"
840 "\\([[:digit:]]+\\)[[:blank:]]"
842 tramp-gvfs-file-attributes-with-gvfs-ls-regexp
))
843 (let ((item (list (cons "type" (match-string 3))
844 (cons "standard::size" (match-string 2))
845 (cons "name" (match-string 1)))))
846 (goto-char (1+ (match-end 3)))
849 tramp-gvfs-file-attributes-with-gvfs-ls-regexp
850 "\\(" tramp-gvfs-file-attributes-with-gvfs-ls-regexp
852 (push (cons (match-string 1) (match-string 2)) item
)
853 (goto-char (match-end 2)))
854 ;; Add display name as head.
856 (cons (cdr (or (assoc "standard::display-name" item
)
857 (assoc "name" item
)))
863 (defun tramp-gvfs-get-root-attributes (filename)
864 "Return GVFS attributes association list of FILENAME."
866 ;; Don't modify `last-coding-system-used' by accident.
867 (let ((last-coding-system-used last-coding-system-used
)
869 (with-parsed-tramp-file-name filename nil
870 (with-tramp-file-property v localname
"file-gvfs-attributes"
871 (tramp-message v
5 "file gvfs attributes: %s" localname
)
873 (tramp-gvfs-send-command
874 v
"gvfs-info" (tramp-gvfs-url-file-name filename
))
876 (with-current-buffer (tramp-get-connection-buffer v
)
877 (goto-char (point-min))
878 (while (re-search-forward
879 tramp-gvfs-file-attributes-with-gvfs-info-regexp nil t
)
880 (push (cons (match-string 1) (match-string 2)) result
))
883 (defun tramp-gvfs-get-file-attributes (filename)
884 "Return GVFS attributes association list of FILENAME."
885 (setq filename
(directory-file-name (expand-file-name filename
)))
886 (with-parsed-tramp-file-name filename nil
887 (setq localname
(tramp-compat-file-name-unquote localname
))
889 (and (string-match "^\\(afp\\|smb\\)$" method
)
890 (string-match "^/?\\([^/]+\\)$" localname
))
891 (string-equal localname
"/"))
892 (tramp-gvfs-get-root-attributes filename
)
894 (file-name-nondirectory filename
)
895 (tramp-gvfs-get-directory-attributes (file-name-directory filename
))))))
897 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format
)
898 "Like `file-attributes' for Tramp files."
899 (unless id-format
(setq id-format
'integer
))
901 (let ((attributes (tramp-gvfs-get-file-attributes filename
))
902 dirp res-symlink-target res-numlinks res-uid res-gid res-access
903 res-mod res-change res-size res-filemodes res-inode res-device
)
905 ;; ... directory or symlink
906 (setq dirp
(if (equal "directory" (cdr (assoc "type" attributes
))) t
))
907 (setq res-symlink-target
908 (cdr (assoc "standard::symlink-target" attributes
)))
912 (or (cdr (assoc "unix::nlink" attributes
)) "0")))
915 (if (eq id-format
'integer
)
917 (or (cdr (assoc "unix::uid" attributes
))
918 (format "%s" tramp-unknown-id-integer
)))
919 (or (cdr (assoc "owner::user" attributes
))
920 (cdr (assoc "unix::uid" attributes
))
921 tramp-unknown-id-string
)))
923 (if (eq id-format
'integer
)
925 (or (cdr (assoc "unix::gid" attributes
))
926 (format "%s" tramp-unknown-id-integer
)))
927 (or (cdr (assoc "owner::group" attributes
))
928 (cdr (assoc "unix::gid" attributes
))
929 tramp-unknown-id-string
)))
930 ;; ... last access, modification and change time
934 (or (cdr (assoc "time::access" attributes
)) "0"))))
938 (or (cdr (assoc "time::modified" attributes
)) "0"))))
942 (or (cdr (assoc "time::changed" attributes
)) "0"))))
946 (or (cdr (assoc "standard::size" attributes
)) "0")))
947 ;; ... file mode flags
949 (let ((n (cdr (assoc "unix::mode" attributes
))))
951 (tramp-file-mode-from-int (string-to-number n
))
955 (if (equal (cdr (assoc "access::can-read" attributes
))
958 (if (equal (cdr (assoc "access::can-write" attributes
))
961 (if (equal (cdr (assoc "access::can-execute" attributes
))
964 ;; ... inode and device
966 (let ((n (cdr (assoc "unix::inode" attributes
))))
969 (tramp-get-inode (tramp-dissect-file-name filename
)))))
971 (let ((n (cdr (assoc "unix::device" attributes
))))
974 (tramp-get-device (tramp-dissect-file-name filename
)))))
976 ;; Return data gathered.
978 ;; 0. t for directory, string (name linked to) for
979 ;; symbolic link, or nil.
980 (or dirp res-symlink-target
)
981 ;; 1. Number of links to file.
987 ;; 4. Last access time, as a list of integers.
988 ;; 5. Last modification time, likewise.
989 ;; 6. Last status change time, likewise.
990 res-access res-mod res-change
991 ;; 7. Size in bytes (-1, if number is out of range).
995 ;; 9. t if file's gid would change if file were deleted
1000 ;; 11. Device number.
1004 (defun tramp-gvfs-handle-file-directory-p (filename)
1005 "Like `file-directory-p' for Tramp files."
1006 (eq t
(tramp-compat-file-attribute-type
1007 (file-attributes (file-truename filename
)))))
1009 (defun tramp-gvfs-handle-file-executable-p (filename)
1010 "Like `file-executable-p' for Tramp files."
1011 (with-parsed-tramp-file-name filename nil
1012 (with-tramp-file-property v localname
"file-executable-p"
1013 (tramp-check-cached-permissions v ?x
))))
1015 (defun tramp-gvfs-handle-file-local-copy (filename)
1016 "Like `file-local-copy' for Tramp files."
1017 (with-parsed-tramp-file-name filename nil
1018 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
1019 (unless (file-exists-p filename
)
1021 v tramp-file-missing
1022 "Cannot make local copy of non-existing file `%s'" filename
))
1023 (copy-file filename tmpfile
'ok-if-already-exists
'keep-time
)
1026 (defun tramp-gvfs-handle-file-name-all-completions (filename directory
)
1027 "Like `file-name-all-completions' for Tramp files."
1028 (unless (save-match-data (string-match "/" filename
))
1031 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1032 (with-tramp-file-property v localname
"file-name-all-completions"
1033 (let ((result '("./" "../")))
1034 ;; Get a list of directories and files.
1035 (dolist (item (tramp-gvfs-get-directory-attributes directory
) result
)
1036 (if (string-equal (cdr (assoc "type" item
)) "directory")
1037 (push (file-name-as-directory (car item
)) result
)
1038 (push (car item
) result
)))))))))
1040 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback
)
1041 "Like `file-notify-add-watch' for Tramp files."
1042 (setq file-name
(expand-file-name file-name
))
1043 (with-parsed-tramp-file-name file-name nil
1044 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1045 ;; supported for gvfs-mounted directories.
1046 (when (file-directory-p file-name
)
1048 v
'file-notify-error
"Monitoring not supported for `%s'" file-name
))
1049 (let* ((default-directory (file-name-directory file-name
))
1052 ((and (memq 'change flags
) (memq 'attribute-change flags
))
1053 '(created changed changes-done-hint moved deleted
1055 ((memq 'change flags
)
1056 '(created changed changes-done-hint moved deleted
))
1057 ((memq 'attribute-change flags
) '(attribute-changed))))
1059 "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
1060 "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name
))))
1061 (if (not (processp p
))
1063 v
'file-notify-error
"Monitoring not supported for `%s'" file-name
)
1065 v
6 "Run `%s', %S" (mapconcat 'identity
(process-command p
) " ") p
)
1066 (tramp-set-connection-property p
"vector" v
)
1067 (process-put p
'events events
)
1068 (process-put p
'watch-name localname
)
1069 (process-put p
'adjust-window-size-function
'ignore
)
1070 (set-process-query-on-exit-flag p nil
)
1071 (set-process-filter p
'tramp-gvfs-monitor-file-process-filter
)
1072 ;; There might be an error if the monitor is not supported.
1073 ;; Give the filter a chance to read the output.
1074 (tramp-accept-process-output p
1)
1075 (unless (process-live-p p
)
1077 v
'file-notify-error
"Monitoring not supported for `%s'" file-name
))
1080 (defun tramp-gvfs-monitor-file-process-filter (proc string
)
1081 "Read output from \"gvfs-monitor-file\" and add corresponding \
1082 file-notify events."
1083 (let* ((rest-string (process-get proc
'rest-string
))
1084 (dd (with-current-buffer (process-buffer proc
) default-directory
))
1085 (ddu (regexp-quote (tramp-gvfs-url-file-name dd
))))
1087 (tramp-message proc
10 "Previous string:\n%s" rest-string
))
1088 (tramp-message proc
6 "%S\n%s" proc string
)
1089 (setq string
(concat rest-string string
)
1090 ;; Attribute change is returned in unused wording.
1091 string
(replace-regexp-in-string
1092 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string
))
1093 (when (string-match "Monitoring not supported" string
)
1094 (delete-process proc
))
1096 (while (string-match
1098 "File Monitor Event:[\n\r]+"
1099 "File = \\([^\n\r]+\\)[\n\r]+"
1100 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1102 (let ((file (match-string 1 string
))
1103 (action (intern-soft
1104 (replace-regexp-in-string
1105 "_" "-" (downcase (match-string 2 string
))))))
1106 (setq string
(replace-match "" nil nil string
))
1107 ;; File names are returned as URL paths. We must convert them.
1108 (when (string-match ddu file
)
1109 (setq file
(replace-match dd nil nil file
)))
1110 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file
)
1113 (char-to-string (string-to-number (match-string 1 file
) 16))
1115 ;; Usually, we would add an Emacs event now. Unfortunately,
1116 ;; `unread-command-events' does not accept several events at
1117 ;; once. Therefore, we apply the callback directly.
1118 (tramp-compat-funcall 'file-notify-callback
(list proc action file
))))
1120 ;; Save rest of the string.
1121 (when (zerop (length string
)) (setq string nil
))
1122 (when string
(tramp-message proc
10 "Rest string:\n%s" string
))
1123 (process-put proc
'rest-string string
)))
1125 (defun tramp-gvfs-handle-file-readable-p (filename)
1126 "Like `file-readable-p' for Tramp files."
1127 (with-parsed-tramp-file-name filename nil
1128 (with-tramp-file-property v localname
"file-readable-p"
1129 (tramp-check-cached-permissions v ?r
))))
1131 (defun tramp-gvfs-handle-file-writable-p (filename)
1132 "Like `file-writable-p' for Tramp files."
1133 (with-parsed-tramp-file-name filename nil
1134 (with-tramp-file-property v localname
"file-writable-p"
1135 (if (file-exists-p filename
)
1136 (tramp-check-cached-permissions v ?w
)
1137 ;; If file doesn't exist, check if directory is writable.
1138 (and (file-directory-p (file-name-directory filename
))
1139 (file-writable-p (file-name-directory filename
)))))))
1141 (defun tramp-gvfs-handle-make-directory (dir &optional parents
)
1142 "Like `make-directory' for Tramp files."
1143 (setq dir
(directory-file-name (expand-file-name dir
)))
1144 (with-parsed-tramp-file-name dir nil
1145 (tramp-flush-file-property v
(file-name-directory localname
))
1146 (tramp-flush-directory-property v localname
)
1148 (let ((ldir (file-name-directory dir
)))
1149 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1151 (when (and parents
(not (file-directory-p ldir
)))
1152 (make-directory ldir parents
))
1154 (unless (tramp-gvfs-send-command
1155 v
"gvfs-mkdir" (tramp-gvfs-url-file-name dir
))
1156 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
1158 (defun tramp-gvfs-handle-rename-file
1159 (filename newname
&optional ok-if-already-exists
)
1160 "Like `rename-file' for Tramp files."
1161 ;; Check if both files are local -- invoke normal rename-file.
1162 ;; Otherwise, use Tramp from local system.
1163 (setq filename
(expand-file-name filename
))
1164 (setq newname
(expand-file-name newname
))
1165 ;; At least one file a Tramp file?
1166 (if (or (tramp-tramp-file-p filename
)
1167 (tramp-tramp-file-p newname
))
1168 (tramp-gvfs-do-copy-or-rename-file
1169 'rename filename newname ok-if-already-exists
1170 'keep-date
'preserve-uid-gid
)
1171 (tramp-run-real-handler
1172 'rename-file
(list filename newname ok-if-already-exists
))))
1174 (defun tramp-gvfs-handle-write-region
1175 (start end filename
&optional append visit lockname confirm
)
1176 "Like `write-region' for Tramp files."
1177 (with-parsed-tramp-file-name filename nil
1178 (when (and confirm
(file-exists-p filename
))
1179 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
1180 (tramp-error v
'file-error
"File not overwritten")))
1182 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
1183 (when (and append
(file-exists-p filename
))
1184 (copy-file filename tmpfile
'ok
))
1185 ;; We say `no-message' here because we don't want the visited file
1186 ;; modtime data to be clobbered from the temp file. We call
1187 ;; `set-visited-file-modtime' ourselves later on.
1188 (tramp-run-real-handler
1190 (if confirm
; don't pass this arg unless defined for backward compat.
1191 (list start end tmpfile append
'no-message lockname confirm
)
1192 (list start end tmpfile append
'no-message lockname
)))
1194 (rename-file tmpfile filename
'ok-if-already-exists
)
1196 (delete-file tmpfile
)
1198 v
'file-error
"Couldn't write region to `%s'" filename
))))
1200 (tramp-flush-file-property v
(file-name-directory localname
))
1201 (tramp-flush-file-property v localname
)
1203 ;; Set file modification time.
1204 (when (or (eq visit t
) (stringp visit
))
1205 (set-visited-file-modtime
1206 (tramp-compat-file-attribute-modification-time
1207 (file-attributes filename
))))
1210 (when (or (eq visit t
) (null visit
) (stringp visit
))
1211 (tramp-message v
0 "Wrote %s" filename
))
1212 (run-hooks 'tramp-handle-write-region-hook
)))
1215 ;; File name conversions.
1217 (defun tramp-gvfs-url-file-name (filename)
1218 "Return FILENAME in URL syntax."
1219 ;; "/" must NOT be hexlified.
1220 (setq filename
(tramp-compat-file-name-unquote filename
))
1221 (let ((url-unreserved-chars (cons ?
/ url-unreserved-chars
))
1226 (if (tramp-tramp-file-p filename
)
1227 (with-parsed-tramp-file-name filename nil
1228 (when (string-equal "gdrive" method
)
1229 (setq method
"google-drive"))
1230 (when (and user
(string-match tramp-user-with-domain-regexp user
))
1232 (concat (match-string 2 user
) ";" (match-string 1 user
))))
1233 (url-parse-make-urlobj
1234 method
(and user
(url-hexify-string user
)) nil
1235 (tramp-file-name-host v
) (tramp-file-name-port v
)
1236 (and localname
(url-hexify-string localname
)) nil nil t
))
1237 (url-parse-make-urlobj
1238 "file" nil nil nil nil
1239 (url-hexify-string (file-truename filename
)) nil nil t
))))
1240 (when (tramp-tramp-file-p filename
)
1241 (with-parsed-tramp-file-name filename nil
1242 (tramp-message v
10 "remote file `%s' is URL `%s'" filename result
)))
1245 (defun tramp-gvfs-object-path (filename)
1246 "Create a D-Bus object path from FILENAME."
1247 (expand-file-name (dbus-escape-as-identifier filename
) tramp-gvfs-path-tramp
))
1249 (defun tramp-gvfs-file-name (object-path)
1250 "Retrieve file name from D-Bus OBJECT-PATH."
1251 (dbus-unescape-from-identifier
1252 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path
)))
1254 (defun tramp-bluez-address (device)
1255 "Return bluetooth device address from a given bluetooth DEVICE name."
1256 (when (stringp device
)
1257 (if (string-match tramp-ipv6-regexp device
)
1258 (match-string 0 device
)
1259 (cadr (assoc device
(tramp-bluez-list-devices))))))
1261 (defun tramp-bluez-device (address)
1262 "Return bluetooth device name from a given bluetooth device ADDRESS.
1263 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1264 (when (stringp address
)
1265 (while (string-match "[][]" address
)
1266 (setq address
(replace-match "" t t address
)))
1268 (dolist (item (tramp-bluez-list-devices) result
)
1269 (when (string-match address
(cadr item
))
1270 (setq result
(car item
)))))))
1273 ;; D-Bus GVFS functions.
1275 (defun tramp-gvfs-handler-askpassword (message user domain flags
)
1276 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1278 (tramp-gvfs-file-name (dbus-event-path-name last-input-event
)))
1282 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message
)
1283 (capitalize (match-string 1 message
))
1289 (with-parsed-tramp-file-name filename l
1290 (when (and (zerop (length user
))
1292 (zerop (logand flags tramp-gvfs-password-need-username
))))
1293 (setq user
(read-string "User name: ")))
1294 (when (and (zerop (length domain
))
1296 (zerop (logand flags tramp-gvfs-password-need-domain
))))
1297 (setq domain
(read-string "Domain name: ")))
1299 (tramp-message l
6 "%S %S %S %d" message user domain flags
)
1300 (unless (tramp-get-connection-property l
"first-password-request" nil
)
1301 (tramp-clear-passwd l
))
1303 (setq tramp-current-method l-method
1304 tramp-current-user user
1305 tramp-current-host l-host
1306 password
(tramp-read-passwd
1307 (tramp-get-connection-process l
) pw-prompt
))
1310 (if (stringp password
)
1312 t
;; password handled.
1313 nil
;; no abort of D-Bus.
1315 (tramp-file-name-user l
)
1317 nil
;; not anonymous.
1318 0) ;; no password save.
1319 ;; No password provided.
1320 (list nil t
"" (tramp-file-name-user l
) domain nil
0)))
1322 ;; When QUIT is raised, we shall return this information to D-Bus.
1323 (quit (list nil t
"" "" "" nil
0)))))
1325 (defun tramp-gvfs-handler-askquestion (message choices
)
1326 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1327 (save-window-excursion
1328 (let ((enable-recursive-minibuffers t
)
1332 (with-parsed-tramp-file-name
1333 (tramp-gvfs-file-name (dbus-event-path-name last-input-event
)) nil
1334 (tramp-message v
6 "%S %S" message choices
)
1336 ;; In theory, there can be several choices. Until now,
1337 ;; there is only the question whether to accept an unknown
1340 ;; Preserve message for `progress-reporter'.
1341 (with-temp-message ""
1343 (pop-to-buffer (current-buffer))
1344 (setq choice
(if (yes-or-no-p (concat (car choices
) " ")) 0 1))
1345 (tramp-message v
6 "%d" choice
)))
1347 ;; When the choice is "no", we set a dummy fuse-mountpoint
1348 ;; in order to leave the timeout.
1349 (unless (zerop choice
)
1350 (tramp-set-file-property v
"/" "fuse-mountpoint" "/"))
1354 nil
;; no abort of D-Bus.
1357 ;; When QUIT is raised, we shall return this information to D-Bus.
1358 (quit (list nil t
0))))))
1360 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1361 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1362 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1364 (let ((signal-name (dbus-event-member-name last-input-event
))
1366 ;; Jump over the first elements of the mount info. Since there
1367 ;; were changes in the entries, we cannot access dedicated
1369 (while (stringp (car elt
)) (setq elt
(cdr elt
)))
1370 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt
)))
1371 (mount-spec (cl-caddr elt
))
1372 (default-location (tramp-gvfs-dbus-byte-array-to-string
1374 (method (tramp-gvfs-dbus-byte-array-to-string
1375 (cadr (assoc "type" (cadr mount-spec
)))))
1376 (user (tramp-gvfs-dbus-byte-array-to-string
1377 (cadr (assoc "user" (cadr mount-spec
)))))
1378 (domain (tramp-gvfs-dbus-byte-array-to-string
1379 (cadr (assoc "domain" (cadr mount-spec
)))))
1380 (host (tramp-gvfs-dbus-byte-array-to-string
1381 (cadr (or (assoc "host" (cadr mount-spec
))
1382 (assoc "server" (cadr mount-spec
))))))
1383 (port (tramp-gvfs-dbus-byte-array-to-string
1384 (cadr (assoc "port" (cadr mount-spec
)))))
1385 (ssl (tramp-gvfs-dbus-byte-array-to-string
1386 (cadr (assoc "ssl" (cadr mount-spec
)))))
1388 (tramp-gvfs-dbus-byte-array-to-string
1390 (tramp-gvfs-dbus-byte-array-to-string
1391 (or (cadr (assoc "share" (cadr mount-spec
)))
1392 (cadr (assoc "volume" (cadr mount-spec
))))))))
1393 (when (string-match "^\\(afp\\|smb\\)" method
)
1394 (setq method
(match-string 1 method
)))
1395 (when (string-equal "obex" method
)
1396 (setq host
(tramp-bluez-device host
)))
1397 (when (and (string-equal "dav" method
) (string-equal "true" ssl
))
1398 (setq method
"davs"))
1399 (when (string-equal "google-drive" method
)
1400 (setq method
"gdrive"))
1401 (unless (zerop (length domain
))
1402 (setq user
(concat user tramp-prefix-domain-format domain
)))
1403 (unless (zerop (length port
))
1404 (setq host
(concat host tramp-prefix-port-format port
)))
1405 (with-parsed-tramp-file-name
1406 (tramp-make-tramp-file-name method user domain host port
"") nil
1409 signal-name
(tramp-gvfs-stringify-dbus-message mount-info
))
1410 (tramp-set-file-property v
"/" "list-mounts" 'undef
)
1411 (if (string-equal (downcase signal-name
) "unmounted")
1412 (tramp-flush-file-property v
"/")
1413 ;; Set prefix, mountpoint and location.
1414 (unless (string-equal prefix
"/")
1415 (tramp-set-file-property v
"/" "prefix" prefix
))
1416 (tramp-set-file-property v
"/" "fuse-mountpoint" fuse-mountpoint
)
1417 (tramp-set-connection-property
1418 v
"default-location" default-location
)))))))
1420 (when tramp-gvfs-enabled
1421 (dbus-register-signal
1422 :session nil tramp-gvfs-path-mounttracker
1423 tramp-gvfs-interface-mounttracker
"mounted"
1424 'tramp-gvfs-handler-mounted-unmounted
)
1425 (dbus-register-signal
1426 :session nil tramp-gvfs-path-mounttracker
1427 tramp-gvfs-interface-mounttracker
"Mounted"
1428 'tramp-gvfs-handler-mounted-unmounted
)
1430 (dbus-register-signal
1431 :session nil tramp-gvfs-path-mounttracker
1432 tramp-gvfs-interface-mounttracker
"unmounted"
1433 'tramp-gvfs-handler-mounted-unmounted
)
1434 (dbus-register-signal
1435 :session nil tramp-gvfs-path-mounttracker
1436 tramp-gvfs-interface-mounttracker
"Unmounted"
1437 'tramp-gvfs-handler-mounted-unmounted
))
1439 (defun tramp-gvfs-connection-mounted-p (vec)
1440 "Check, whether the location is already mounted."
1442 (tramp-get-file-property vec
"/" "fuse-mountpoint" nil
)
1446 (with-tramp-file-property vec
"/" "list-mounts"
1447 (with-tramp-dbus-call-method vec t
1448 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1449 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts
))
1451 ;; Jump over the first elements of the mount info. Since there
1452 ;; were changes in the entries, we cannot access dedicated
1454 (while (stringp (car elt
)) (setq elt
(cdr elt
)))
1455 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1457 (mount-spec (cl-caddr elt
))
1458 (default-location (tramp-gvfs-dbus-byte-array-to-string
1460 (method (tramp-gvfs-dbus-byte-array-to-string
1461 (cadr (assoc "type" (cadr mount-spec
)))))
1462 (user (tramp-gvfs-dbus-byte-array-to-string
1463 (cadr (assoc "user" (cadr mount-spec
)))))
1464 (domain (tramp-gvfs-dbus-byte-array-to-string
1465 (cadr (assoc "domain" (cadr mount-spec
)))))
1466 (host (tramp-gvfs-dbus-byte-array-to-string
1467 (cadr (or (assoc "host" (cadr mount-spec
))
1468 (assoc "server" (cadr mount-spec
))))))
1469 (port (tramp-gvfs-dbus-byte-array-to-string
1470 (cadr (assoc "port" (cadr mount-spec
)))))
1471 (ssl (tramp-gvfs-dbus-byte-array-to-string
1472 (cadr (assoc "ssl" (cadr mount-spec
)))))
1474 (tramp-gvfs-dbus-byte-array-to-string
1476 (tramp-gvfs-dbus-byte-array-to-string
1478 (cadr (assoc "share" (cadr mount-spec
)))
1479 (cadr (assoc "volume" (cadr mount-spec
))))))))
1480 (when (string-match "^\\(afp\\|smb\\)" method
)
1481 (setq method
(match-string 1 method
)))
1482 (when (string-equal "obex" method
)
1483 (setq host
(tramp-bluez-device host
)))
1484 (when (and (string-equal "dav" method
) (string-equal "true" ssl
))
1485 (setq method
"davs"))
1486 (when (string-equal "google-drive" method
)
1487 (setq method
"gdrive"))
1488 (when (and (string-equal "synce" method
) (zerop (length user
)))
1489 (setq user
(or (tramp-file-name-user vec
) "")))
1490 (unless (zerop (length domain
))
1491 (setq user
(concat user tramp-prefix-domain-format domain
)))
1492 (unless (zerop (length port
))
1493 (setq host
(concat host tramp-prefix-port-format port
)))
1495 (string-equal method
(tramp-file-name-method vec
))
1496 (string-equal user
(or (tramp-file-name-user vec
) ""))
1497 (string-equal host
(tramp-file-name-host vec
))
1498 (string-match (concat "^" (regexp-quote prefix
))
1499 (tramp-file-name-unquote-localname vec
)))
1500 ;; Set prefix, mountpoint and location.
1501 (unless (string-equal prefix
"/")
1502 (tramp-set-file-property vec
"/" "prefix" prefix
))
1503 (tramp-set-file-property vec
"/" "fuse-mountpoint" fuse-mountpoint
)
1504 (tramp-set-connection-property
1505 vec
"default-location" default-location
)
1506 (throw 'mounted t
)))))))
1508 (defun tramp-gvfs-mount-spec-entry (key value
)
1509 "Construct a mount-spec entry to be used in a mount_spec.
1510 It was \"a(say)\", but has changed to \"a{sv})\"."
1511 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature
)
1512 (list :dict-entry key
1513 (list :variant
(tramp-gvfs-dbus-string-to-byte-array value
)))
1514 (list :struct key
(tramp-gvfs-dbus-string-to-byte-array value
))))
1516 (defun tramp-gvfs-mount-spec (vec)
1517 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1518 (let* ((method (tramp-file-name-method vec
))
1519 (user (tramp-file-name-user vec
))
1520 (domain (tramp-file-name-domain vec
))
1521 (host (tramp-file-name-host vec
))
1522 (port (tramp-file-name-port vec
))
1523 (localname (tramp-file-name-unquote-localname vec
))
1524 (share (when (string-match "^/?\\([^/]+\\)" localname
)
1525 (match-string 1 localname
)))
1526 (ssl (if (string-match "^davs" method
) "true" "false"))
1530 ((string-equal "smb" method
)
1531 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1532 (tramp-gvfs-mount-spec-entry "server" host
)
1533 (tramp-gvfs-mount-spec-entry "share" share
)))
1534 ((string-equal "obex" method
)
1535 (list (tramp-gvfs-mount-spec-entry "type" method
)
1536 (tramp-gvfs-mount-spec-entry
1537 "host" (concat "[" (tramp-bluez-address host
) "]"))))
1538 ((string-match "\\`dav" method
)
1539 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1540 (tramp-gvfs-mount-spec-entry "host" host
)
1541 (tramp-gvfs-mount-spec-entry "ssl" ssl
)))
1542 ((string-equal "afp" method
)
1543 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1544 (tramp-gvfs-mount-spec-entry "host" host
)
1545 (tramp-gvfs-mount-spec-entry "volume" share
)))
1546 ((string-equal "gdrive" method
)
1547 (list (tramp-gvfs-mount-spec-entry "type" "google-drive")
1548 (tramp-gvfs-mount-spec-entry "host" host
)))
1550 (list (tramp-gvfs-mount-spec-entry "type" method
)
1551 (tramp-gvfs-mount-spec-entry "host" host
))))
1553 (list (tramp-gvfs-mount-spec-entry "user" user
)))
1555 (list (tramp-gvfs-mount-spec-entry "domain" domain
)))
1557 (list (tramp-gvfs-mount-spec-entry
1558 "port" (number-to-string port
))))))
1560 (if (and (string-match "\\`dav" method
)
1561 (string-match "^/?[^/]+" localname
))
1562 (match-string 0 localname
)
1566 `(:struct
,(tramp-gvfs-dbus-string-to-byte-array mount-pref
) ,mount-spec
)))
1569 ;; Connection functions.
1571 (defun tramp-gvfs-get-remote-uid (vec id-format
)
1572 "The uid of the remote connection VEC, in ID-FORMAT.
1573 ID-FORMAT valid values are `string' and `integer'."
1574 (with-tramp-connection-property vec
(format "uid-%s" id-format
)
1575 (let ((method (tramp-file-name-method vec
))
1576 (user (tramp-file-name-user vec
))
1577 (domain (tramp-file-name-domain vec
))
1578 (host (tramp-file-name-host vec
))
1579 (port (tramp-file-name-port vec
))
1581 (tramp-get-connection-property vec
"default-location" nil
)))
1583 ((and user
(equal id-format
'string
)) user
)
1585 (tramp-compat-file-attribute-user-id
1587 (tramp-make-tramp-file-name method user domain host port localname
)
1589 ((equal id-format
'integer
) tramp-unknown-id-integer
)
1590 ((equal id-format
'string
) tramp-unknown-id-string
)))))
1592 (defun tramp-gvfs-get-remote-gid (vec id-format
)
1593 "The gid of the remote connection VEC, in ID-FORMAT.
1594 ID-FORMAT valid values are `string' and `integer'."
1595 (with-tramp-connection-property vec
(format "gid-%s" id-format
)
1596 (let ((method (tramp-file-name-method vec
))
1597 (user (tramp-file-name-user vec
))
1598 (domain (tramp-file-name-domain vec
))
1599 (host (tramp-file-name-host vec
))
1600 (port (tramp-file-name-port vec
))
1602 (tramp-get-connection-property vec
"default-location" nil
)))
1605 (tramp-compat-file-attribute-group-id
1607 (tramp-make-tramp-file-name method user domain host port localname
)
1609 ((equal id-format
'integer
) tramp-unknown-id-integer
)
1610 ((equal id-format
'string
) tramp-unknown-id-string
)))))
1612 (defvar tramp-gvfs-get-remote-uid-gid-in-progress nil
1613 "Indication, that remote uid and gid determination is in progress.")
1615 (defun tramp-gvfs-maybe-open-connection (vec)
1616 "Maybe open a connection VEC.
1617 Does not do anything if a connection is already open, but re-opens the
1618 connection if a previous connection has died for some reason."
1619 ;; We set the file name, in case there are incoming D-Bus signals or
1621 (setq tramp-gvfs-dbus-event-vector vec
)
1623 ;; For password handling, we need a process bound to the connection
1624 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1626 (unless (get-buffer-process (tramp-get-connection-buffer vec
))
1627 (let ((p (make-network-process
1628 :name
(tramp-buffer-name vec
)
1629 :buffer
(tramp-get-connection-buffer vec
)
1630 :server t
:host
'local
:service t
:noquery t
)))
1631 (set-process-query-on-exit-flag p nil
)))
1633 (unless (tramp-gvfs-connection-mounted-p vec
)
1634 (let* ((method (tramp-file-name-method vec
))
1635 (user (tramp-file-name-user vec
))
1636 (domain (tramp-file-name-domain vec
))
1637 (host (tramp-file-name-host vec
))
1638 (port (tramp-file-name-port vec
))
1639 (localname (tramp-file-name-unquote-localname vec
))
1641 (tramp-gvfs-object-path
1642 (tramp-make-tramp-file-name method user domain host port
""))))
1644 (when (and (string-equal method
"afp")
1645 (string-equal localname
"/"))
1646 (tramp-error vec
'file-error
"Filename must contain an AFP volume"))
1648 (when (and (string-equal method
"smb")
1649 (string-equal localname
"/"))
1650 (tramp-error vec
'file-error
"Filename must contain a Windows share"))
1652 (with-tramp-progress-reporter
1654 (if (zerop (length user
))
1655 (format "Opening connection for %s using %s" host method
)
1656 (format "Opening connection for %s@%s using %s" user host method
))
1658 ;; Enable `auth-source'.
1659 (tramp-set-connection-property vec
"first-password-request" t
)
1661 ;; There will be a callback of "askPassword" when a password is
1663 (dbus-register-method
1664 :session dbus-service-emacs object-path
1665 tramp-gvfs-interface-mountoperation
"askPassword"
1666 'tramp-gvfs-handler-askpassword
)
1667 (dbus-register-method
1668 :session dbus-service-emacs object-path
1669 tramp-gvfs-interface-mountoperation
"AskPassword"
1670 'tramp-gvfs-handler-askpassword
)
1672 ;; There could be a callback of "askQuestion" when adding fingerprint.
1673 (dbus-register-method
1674 :session dbus-service-emacs object-path
1675 tramp-gvfs-interface-mountoperation
"askQuestion"
1676 'tramp-gvfs-handler-askquestion
)
1677 (dbus-register-method
1678 :session dbus-service-emacs object-path
1679 tramp-gvfs-interface-mountoperation
"AskQuestion"
1680 'tramp-gvfs-handler-askquestion
)
1682 ;; The call must be asynchronously, because of the "askPassword"
1683 ;; or "askQuestion"callbacks.
1684 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature
)
1685 (with-tramp-dbus-call-method vec nil
1686 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1687 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1688 (tramp-gvfs-mount-spec vec
)
1689 `(:struct
:string
,(dbus-get-unique-name :session
)
1690 :object-path
,object-path
))
1691 (with-tramp-dbus-call-method vec nil
1692 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1693 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1694 (tramp-gvfs-mount-spec vec
)
1695 :string
(dbus-get-unique-name :session
) :object-path object-path
))
1697 ;; We must wait, until the mount is applied. This will be
1698 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1701 ((or (tramp-get-method-parameter vec
'tramp-connection-timeout
)
1702 tramp-connection-timeout
)
1703 (if (zerop (length (tramp-file-name-user vec
)))
1706 "Timeout reached mounting %s using %s" host method
)
1709 "Timeout reached mounting %s@%s using %s" user host method
)))
1710 (while (not (tramp-get-file-property vec
"/" "fuse-mountpoint" nil
))
1711 (read-event nil nil
0.1)))
1713 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1714 ;; is marked with the fuse-mountpoint "/". We shall react.
1716 (tramp-get-file-property vec
"/" "fuse-mountpoint" "") "/")
1717 (tramp-error vec
'file-error
"FUSE mount denied"))
1719 ;; Set connection-local variables.
1720 (tramp-set-connection-local-variables vec
)
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
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
)
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
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
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
)
1792 ((string-equal property
"Discovering")
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."
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."
1839 (let ((host (zeroconf-service-host x
))
1840 (port (zeroconf-service-port x
))
1841 (text (zeroconf-service-txt x
))
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".
1847 (when (string-match "u=\\(.+\\)$" (car text
))
1848 (setq user
(match-string 1 (car text
))))
1849 (setq text
(cdr text
)))
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."
1860 (tramp-compat-funcall
1862 (shell-command-to-string (format "avahi-browse -trkp %s" service
))
1863 "[\n\r]+" 'omit
"^\\+;.*$"))))
1867 (let* ((list (split-string x
";"))
1869 (text (tramp-compat-funcall
1870 'split-string
(nth 9 list
) "\" \"" 'omit
"\""))
1872 ;; A user is marked in a TXT field like "u=guest".
1874 (when (string-match "u=\\(.+\\)$" (car text
))
1875 (setq user
(match-string 1 (car text
))))
1876 (setq text
(cdr text
)))
1880 ;; Add completion functions for AFP, DAV, DAVS, SFTP and SMB methods.
1881 (when tramp-gvfs-enabled
1882 ;; Suppress D-Bus error messages.
1883 (let (tramp-gvfs-dbus-event-vector)
1884 (zeroconf-init tramp-gvfs-zeroconf-domain
)
1885 (if (zeroconf-list-service-types)
1887 (tramp-set-completion-function
1888 "afp" '((tramp-zeroconf-parse-device-names "_afpovertcp._tcp")))
1889 (tramp-set-completion-function
1890 "dav" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1891 (tramp-set-completion-function
1892 "davs" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1893 (tramp-set-completion-function
1894 "sftp" '((tramp-zeroconf-parse-device-names "_ssh._tcp")
1895 (tramp-zeroconf-parse-device-names "_workstation._tcp")))
1896 (when (member "smb" tramp-gvfs-methods
)
1897 (tramp-set-completion-function
1898 "smb" '((tramp-zeroconf-parse-device-names "_smb._tcp")))))
1900 (when (executable-find "avahi-browse")
1901 (tramp-set-completion-function
1902 "afp" '((tramp-gvfs-parse-device-names "_afpovertcp._tcp")))
1903 (tramp-set-completion-function
1904 "dav" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1905 (tramp-set-completion-function
1906 "davs" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1907 (tramp-set-completion-function
1908 "sftp" '((tramp-gvfs-parse-device-names "_ssh._tcp")
1909 (tramp-gvfs-parse-device-names "_workstation._tcp")))
1910 (when (member "smb" tramp-gvfs-methods
)
1911 (tramp-set-completion-function
1912 "smb" '((tramp-gvfs-parse-device-names "_smb._tcp"))))))))
1915 ;; D-Bus SYNCE functions.
1917 (defun tramp-synce-list-devices ()
1918 "Return all discovered synce devices as list.
1919 They are retrieved from the hal daemon."
1920 (let (tramp-synce-devices)
1922 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1923 :system tramp-hal-service tramp-hal-path-manager
1924 tramp-hal-interface-manager
"GetAllDevices"))
1925 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1926 :system tramp-hal-service device tramp-hal-interface-device
1927 "PropertyExists" "sync.plugin")
1929 (with-tramp-dbus-call-method
1930 tramp-gvfs-dbus-event-vector t
1931 :system tramp-hal-service device tramp-hal-interface-device
1932 "GetPropertyString" "pda.pocketpc.name")))
1933 (unless (member prop tramp-synce-devices
)
1934 (push prop tramp-synce-devices
)))))
1935 (tramp-message tramp-gvfs-dbus-event-vector
10 "%s" tramp-synce-devices
)
1936 tramp-synce-devices
))
1938 (defun tramp-synce-parse-device-names (_ignore)
1939 "Return a list of (nil host) tuples allowed to access."
1941 (lambda (x) (list nil x
))
1942 (tramp-synce-list-devices)))
1944 ;; Add completion function for SYNCE method.
1945 (when tramp-gvfs-enabled
1946 (tramp-set-completion-function
1947 "synce" '((tramp-synce-parse-device-names ""))))
1949 (add-hook 'tramp-unload-hook
1951 (unload-feature 'tramp-gvfs
'force
)))
1953 (provide 'tramp-gvfs
)
1957 ;; * Host name completion for existing mount points (afp-server,
1958 ;; smb-server) or via smb-network.
1960 ;; * Check, how two shares of the same SMB server can be mounted in
1963 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1966 ;; * Implement obex for other serial communication but bluetooth.
1968 ;;; tramp-gvfs.el ends here