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-ownership-preserved-p . ignore
)
439 (file-readable-p . tramp-gvfs-handle-file-readable-p
)
440 (file-regular-p . tramp-handle-file-regular-p
)
441 (file-remote-p . tramp-handle-file-remote-p
)
442 (file-selinux-context . ignore
)
443 (file-symlink-p . tramp-handle-file-symlink-p
)
444 ;; `file-truename' performed by default handler.
445 (file-writable-p . tramp-gvfs-handle-file-writable-p
)
446 (find-backup-file-name . tramp-handle-find-backup-file-name
)
447 ;; `find-file-noselect' performed by default handler.
448 ;; `get-file-buffer' performed by default handler.
449 (insert-directory . tramp-gvfs-handle-insert-directory
)
450 (insert-file-contents . tramp-gvfs-handle-insert-file-contents
)
451 (load . tramp-handle-load
)
452 (make-directory . tramp-gvfs-handle-make-directory
)
453 (make-directory-internal . ignore
)
454 (make-symbolic-link . ignore
)
455 (process-file . ignore
)
456 (rename-file . tramp-gvfs-handle-rename-file
)
457 (set-file-acl . ignore
)
458 (set-file-modes . ignore
)
459 (set-file-selinux-context . ignore
)
460 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime
)
461 (shell-command . ignore
)
462 (start-file-process . ignore
)
463 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
464 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
465 (vc-registered . ignore
)
466 ;; `verify-visited-file-modtime' performed by default handler.
467 (write-region . tramp-gvfs-handle-write-region
)
469 "Alist of handler functions for Tramp GVFS method.
470 Operations not mentioned here will be handled by the default Emacs primitives.")
472 ;; It must be a `defsubst' in order to push the whole code into
473 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
475 (defsubst tramp-gvfs-file-name-p
(filename)
476 "Check if it's a filename handled by the GVFS daemon."
477 (and (tramp-tramp-file-p filename
)
479 (tramp-file-name-method (tramp-dissect-file-name filename
))))
480 (and (stringp method
) (member method tramp-gvfs-methods
)))))
483 (defun tramp-gvfs-file-name-handler (operation &rest args
)
484 "Invoke the GVFS related OPERATION.
485 First arg specifies the OPERATION, second arg is a list of arguments to
486 pass to the OPERATION."
487 (unless tramp-gvfs-enabled
488 (tramp-compat-user-error "Package `tramp-gvfs' not supported"))
489 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist
)))
491 (save-match-data (apply (cdr fn
) args
))
492 (tramp-run-real-handler operation args
))))
494 ;; This might be moved to tramp.el. It shall be the first file name
497 (when (featurep 'dbusbind
)
498 (add-to-list 'tramp-foreign-file-name-handler-alist
499 (cons 'tramp-gvfs-file-name-p
'tramp-gvfs-file-name-handler
)))
502 ;; D-Bus helper function.
504 (defun tramp-gvfs-dbus-string-to-byte-array (string)
505 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
506 (dbus-string-to-byte-array
507 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature
)
508 (concat string
(string 0)) string
)))
510 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
511 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
512 ;; The byte array could be a variant. Take care.
514 (if (and (consp byte-array
) (atom (car byte-array
)))
515 byte-array
(car byte-array
))))
516 (dbus-byte-array-to-string
517 (if (and (consp byte-array
) (zerop (car (last byte-array
))))
518 (butlast byte-array
) byte-array
))))
520 (defun tramp-gvfs-stringify-dbus-message (message)
521 "Convert a D-Bus message into readable UTF8 strings, used for traces."
523 ((and (consp message
) (characterp (car message
)))
524 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message
)))
526 (mapcar 'tramp-gvfs-stringify-dbus-message message
))
528 (format "%S" message
))
531 (defmacro with-tramp-dbus-call-method
532 (vec synchronous bus service path interface method
&rest args
)
533 "Apply a D-Bus call on bus BUS.
535 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
536 it is an asynchronous call, with `ignore' as callback function.
538 The other arguments have the same meaning as with `dbus-call-method'
539 or `dbus-call-method-asynchronously'. Additionally, the call
540 will be traced by Tramp with trace level 6."
541 `(let ((func (if ,synchronous
542 'dbus-call-method
'dbus-call-method-asynchronously
))
543 (args (append (list ,bus
,service
,path
,interface
,method
)
544 (if ,synchronous
(list ,@args
) (list 'ignore
,@args
))))
546 (tramp-message ,vec
6 "%s %s" func args
)
547 (setq result
(apply func args
))
548 (tramp-message ,vec
6 "%s" (tramp-gvfs-stringify-dbus-message result
))
551 (put 'with-tramp-dbus-call-method
'lisp-indent-function
2)
552 (put 'with-tramp-dbus-call-method
'edebug-form-spec
'(form symbolp body
))
553 (tramp-compat-font-lock-add-keywords
554 'emacs-lisp-mode
'("\\<with-tramp-dbus-call-method\\>"))
556 (defmacro with-tramp-gvfs-error-message
(filename handler
&rest args
)
557 "Apply a Tramp GVFS `handler'.
558 In case of an error, modify the error message by replacing
559 `filename' with its GVFS mounted name."
560 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename
)))
563 (tramp-compat-funcall ,handler
,@args
)
567 (when (and (stringp (car elt
))
568 (string-match fuse-file-name
(car elt
)))
569 (setcar elt
(replace-match ,filename t t
(car elt
))))
570 (setq elt
(cdr elt
)))
571 (signal (car err
) (cdr err
))))))
573 (put 'with-tramp-gvfs-error-message
'lisp-indent-function
2)
574 (put 'with-tramp-gvfs-error-message
'edebug-form-spec
'(form symbolp body
))
575 (tramp-compat-font-lock-add-keywords
576 'emacs-lisp-mode
'("\\<with-tramp-gvfs-error-message\\>"))
578 (defvar tramp-gvfs-dbus-event-vector nil
579 "Current Tramp file name to be used, as vector.
580 It is needed when D-Bus signals or errors arrive, because there
581 is no information where to trace the message.")
583 (defun tramp-gvfs-dbus-event-error (event err
)
584 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
585 (when tramp-gvfs-dbus-event-vector
586 (tramp-message tramp-gvfs-dbus-event-vector
10 "%S" event
)
587 (tramp-error tramp-gvfs-dbus-event-vector
'file-error
"%s" (cadr err
))))
589 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
591 (if (boundp 'dbus-event-error-functions
)
592 'dbus-event-error-functions
'dbus-event-error-hooks
)
593 'tramp-gvfs-dbus-event-error
)
596 ;; File name primitives.
598 (defun tramp-gvfs-handle-copy-file
599 (filename newname
&optional ok-if-already-exists keep-date
600 preserve-uid-gid preserve-extended-attributes
)
601 "Like `copy-file' for Tramp files."
602 (with-parsed-tramp-file-name
603 (if (tramp-tramp-file-p filename
) filename newname
) nil
605 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
607 v
'file-already-exists
"File %s already exists" newname
))
609 (if (or (and (tramp-tramp-file-p filename
)
610 (not (tramp-gvfs-file-name-p filename
)))
611 (and (tramp-tramp-file-p newname
)
612 (not (tramp-gvfs-file-name-p newname
))))
614 ;; We cannot copy directly.
615 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
617 (preserve-extended-attributes
619 filename tmpfile t keep-date preserve-uid-gid
620 preserve-extended-attributes
))
622 (copy-file filename tmpfile t keep-date preserve-uid-gid
))
624 (copy-file filename tmpfile t keep-date
)))
625 (rename-file tmpfile newname ok-if-already-exists
))
628 (with-tramp-progress-reporter
629 v
0 (format "Copying %s to %s" filename newname
)
632 (append (if (or keep-date preserve-uid-gid
)
636 (tramp-gvfs-url-file-name filename
)
637 (tramp-gvfs-url-file-name newname
)))))
638 (apply 'tramp-gvfs-send-command v
"gvfs-copy" args
))
639 ;; Propagate the error.
640 (with-current-buffer (tramp-get-connection-buffer v
)
641 (goto-char (point-min))
642 (tramp-error-with-buffer
644 "Copying failed, see buffer `%s' for details." (buffer-name)))))
646 (when (file-remote-p newname
)
647 (with-parsed-tramp-file-name newname nil
648 (tramp-flush-file-property v
(file-name-directory localname
))
649 (tramp-flush-file-property v localname
))))))
651 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash
)
652 "Like `delete-directory' for Tramp files."
653 (when (and recursive
(not (file-symlink-p directory
)))
655 (if (eq t
(car (file-attributes file
)))
656 (tramp-compat-delete-directory file recursive trash
)
657 (tramp-compat-delete-file file trash
)))
659 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
660 (with-parsed-tramp-file-name directory nil
661 (tramp-flush-file-property v
(file-name-directory localname
))
662 (tramp-flush-directory-property v localname
)
664 (tramp-gvfs-send-command
665 v
(if (and trash delete-by-moving-to-trash
) "gvfs-trash" "gvfs-rm")
666 (tramp-gvfs-url-file-name directory
))
667 ;; Propagate the error.
668 (with-current-buffer (tramp-get-connection-buffer v
)
669 (goto-char (point-min))
670 (tramp-error-with-buffer
671 nil v
'file-error
"Couldn't delete %s" directory
)))))
673 (defun tramp-gvfs-handle-delete-file (filename &optional trash
)
674 "Like `delete-file' for Tramp files."
675 (with-parsed-tramp-file-name filename nil
676 (tramp-flush-file-property v
(file-name-directory localname
))
677 (tramp-flush-directory-property v localname
)
679 (tramp-gvfs-send-command
680 v
(if (and trash delete-by-moving-to-trash
) "gvfs-trash" "gvfs-rm")
681 (tramp-gvfs-url-file-name filename
))
682 ;; Propagate the error.
683 (with-current-buffer (tramp-get-connection-buffer v
)
684 (goto-char (point-min))
685 (tramp-error-with-buffer
686 nil v
'file-error
"Couldn't delete %s" filename
)))))
688 (defun tramp-gvfs-handle-expand-file-name (name &optional dir
)
689 "Like `expand-file-name' for Tramp files."
690 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
691 (setq dir
(or dir default-directory
"/"))
692 ;; Unless NAME is absolute, concat DIR and NAME.
693 (unless (file-name-absolute-p name
)
694 (setq name
(concat (file-name-as-directory dir
) name
)))
695 ;; If NAME is not a Tramp file, run the real handler.
696 (if (not (tramp-tramp-file-p name
))
697 (tramp-run-real-handler 'expand-file-name
(list name nil
))
699 (with-parsed-tramp-file-name name nil
700 ;; If there is a default location, expand tilde.
701 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname
)
703 (tramp-gvfs-maybe-open-connection (vector method user host
"/" hop
)))
706 (tramp-get-file-property v
"/" "default-location" "~")
708 ;; Tilde expansion is not possible.
709 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
712 "Cannot expand tilde in file `%s'" name
))
713 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
714 (setq localname
(concat "/" localname
)))
715 ;; We do not pass "/..".
716 (if (string-equal "smb" method
)
717 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname
)
718 (setq localname
(replace-match "/" t t localname
1)))
719 (when (string-match "^/\\.\\./?" localname
)
720 (setq localname
(replace-match "/" t t localname
))))
721 ;; There might be a double slash. Remove this.
722 (while (string-match "//" localname
)
723 (setq localname
(replace-match "/" t t localname
)))
724 ;; No tilde characters in file name, do normal
725 ;; `expand-file-name' (this does "/./" and "/../").
726 (tramp-make-tramp-file-name
728 (tramp-run-real-handler
729 'expand-file-name
(list localname
))))))
731 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format
)
732 "Like `file-attributes' for Tramp files."
733 (unless id-format
(setq id-format
'integer
))
734 ;; Don't modify `last-coding-system-used' by accident.
735 (let ((last-coding-system-used last-coding-system-used
)
736 dirp res-symlink-target res-numlinks res-uid res-gid res-access
737 res-mod res-change res-size res-filemodes res-inode res-device
)
738 (with-parsed-tramp-file-name filename nil
739 (with-tramp-file-property
740 v localname
(format "file-attributes-%s" id-format
)
741 (tramp-message v
5 "file attributes: %s" localname
)
742 (tramp-gvfs-send-command
743 v
"gvfs-info" (tramp-gvfs-url-file-name filename
))
745 (with-current-buffer (tramp-get-connection-buffer v
)
746 (goto-char (point-min))
747 (when (re-search-forward "attributes:" nil t
)
748 ;; ... directory or symlink
749 (goto-char (point-min))
750 (setq dirp
(if (re-search-forward "type:\\s-+directory" nil t
) t
))
751 (goto-char (point-min))
752 (setq res-symlink-target
753 (if (re-search-forward
754 "standard::symlink-target:\\s-+\\(\\S-+\\)" nil t
)
757 (goto-char (point-min))
759 (if (re-search-forward "unix::nlink:\\s-+\\([0-9]+\\)" nil t
)
760 (string-to-number (match-string 1)) 0))
762 (goto-char (point-min))
764 (or (if (eq id-format
'integer
)
765 (if (re-search-forward
766 "unix::uid:\\s-+\\([0-9]+\\)" nil t
)
767 (string-to-number (match-string 1)))
768 (if (re-search-forward
769 "owner::user:\\s-+\\(\\S-+\\)" nil t
)
771 (tramp-get-local-uid id-format
)))
773 (or (if (eq id-format
'integer
)
774 (if (re-search-forward
775 "unix::gid:\\s-+\\([0-9]+\\)" nil t
)
776 (string-to-number (match-string 1)))
777 (if (re-search-forward
778 "owner::group:\\s-+\\(\\S-+\\)" nil t
)
780 (tramp-get-local-gid id-format
)))
781 ;; ... last access, modification and change time
782 (goto-char (point-min))
784 (if (re-search-forward
785 "time::access:\\s-+\\([0-9]+\\)" nil t
)
786 (seconds-to-time (string-to-number (match-string 1)))
788 (goto-char (point-min))
790 (if (re-search-forward
791 "time::modified:\\s-+\\([0-9]+\\)" nil t
)
792 (seconds-to-time (string-to-number (match-string 1)))
794 (goto-char (point-min))
796 (if (re-search-forward
797 "time::changed:\\s-+\\([0-9]+\\)" nil t
)
798 (seconds-to-time (string-to-number (match-string 1)))
801 (goto-char (point-min))
803 (if (re-search-forward
804 "standard::size:\\s-+\\([0-9]+\\)" nil t
)
805 (string-to-number (match-string 1)) 0))
806 ;; ... file mode flags
807 (goto-char (point-min))
809 (if (re-search-forward "unix::mode:\\s-+\\([0-9]+\\)" nil t
)
810 (tramp-file-mode-from-int (match-string 1))
811 (if dirp
"drwx------" "-rwx------")))
812 ;; ... inode and device
813 (goto-char (point-min))
815 (if (re-search-forward "unix::inode:\\s-+\\([0-9]+\\)" nil t
)
816 (string-to-number (match-string 1))
817 (tramp-get-inode v
)))
818 (goto-char (point-min))
820 (if (re-search-forward "unix::device:\\s-+\\([0-9]+\\)" nil t
)
821 (string-to-number (match-string 1))
822 (tramp-get-device v
)))
824 ;; Return data gathered.
826 ;; 0. t for directory, string (name linked to) for
827 ;; symbolic link, or nil.
828 (or dirp res-symlink-target
)
829 ;; 1. Number of links to file.
835 ;; 4. Last access time, as a list of integers.
836 ;; 5. Last modification time, likewise.
837 ;; 6. Last status change time, likewise.
838 res-access res-mod res-change
839 ;; 7. Size in bytes (-1, if number is out of range).
843 ;; 9. t if file's gid would change if file were deleted
848 ;; 11. Device number.
852 (defun tramp-gvfs-handle-file-directory-p (filename)
853 "Like `file-directory-p' for Tramp files."
854 (eq t
(car (file-attributes filename
))))
856 (defun tramp-gvfs-handle-file-executable-p (filename)
857 "Like `file-executable-p' for Tramp files."
858 (with-parsed-tramp-file-name filename nil
859 (with-tramp-file-property v localname
"file-executable-p"
860 (tramp-check-cached-permissions v ?x
))))
862 (defun tramp-gvfs-handle-file-local-copy (filename)
863 "Like `file-local-copy' for Tramp files."
864 (with-parsed-tramp-file-name filename nil
865 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
866 (unless (file-exists-p filename
)
869 "Cannot make local copy of non-existing file `%s'" filename
))
870 (copy-file filename tmpfile t t
)
873 (defun tramp-gvfs-handle-file-name-all-completions (filename directory
)
874 "Like `file-name-all-completions' for Tramp files."
875 (unless (save-match-data (string-match "/" filename
))
876 (with-parsed-tramp-file-name (expand-file-name directory
) nil
883 ;; Try cache entries for filename, filename with last
884 ;; character removed, filename with last two characters
885 ;; removed, ..., and finally the empty string - all
886 ;; concatenated to the local directory name.
887 (let ((remote-file-name-inhibit-cache
888 (or remote-file-name-inhibit-cache
889 tramp-completion-reread-directory-timeout
)))
891 ;; This is inefficient for very long filenames, pity
892 ;; `reduce' is not available...
899 (tramp-get-file-property
901 (concat localname
(substring filename
0 x
))
902 "file-name-all-completions"
904 (when cache-hit
(list cache-hit
))))
905 ;; We cannot use a length of 0, because file properties
906 ;; for "foo" and "foo/" are identical.
907 (tramp-compat-number-sequence (length filename
) 1 -
1)))))
909 ;; Cache expired or no matching cache entry found so we need
910 ;; to perform a remote operation.
911 (let ((result '("." ".."))
913 ;; Get a list of directories and files.
914 (tramp-gvfs-send-command
915 v
"gvfs-ls" (tramp-gvfs-url-file-name directory
))
917 ;; Now grab the output.
919 (insert-buffer-substring (tramp-get-connection-buffer v
))
920 (goto-char (point-max))
921 (while (zerop (forward-line -
1))
922 (setq entry
(buffer-substring (point) (point-at-eol)))
923 (when (string-match filename entry
)
924 (if (file-directory-p (expand-file-name entry directory
))
925 (push (concat entry
"/") result
)
926 (push entry result
)))))
928 ;; Because the remote op went through OK we know the
929 ;; directory we `cd'-ed to exists.
930 (tramp-set-file-property v localname
"file-exists-p" t
)
932 ;; Because the remote op went through OK we know every
933 ;; file listed by `ls' exists.
934 (mapc (lambda (entry)
935 (tramp-set-file-property
936 v
(concat localname entry
) "file-exists-p" t
))
939 ;; Store result in the cache.
940 (tramp-set-file-property
941 v
(concat localname filename
)
942 "file-name-all-completions" result
))))))))
944 (defun tramp-gvfs-handle-file-readable-p (filename)
945 "Like `file-readable-p' for Tramp files."
946 (with-parsed-tramp-file-name filename nil
947 (with-tramp-file-property v localname
"file-executable-p"
948 (tramp-check-cached-permissions v ?r
))))
950 (defun tramp-gvfs-handle-file-writable-p (filename)
951 "Like `file-writable-p' for Tramp files."
952 (with-parsed-tramp-file-name filename nil
953 (with-tramp-file-property v localname
"file-writable-p"
954 (if (file-exists-p filename
)
955 (tramp-check-cached-permissions v ?w
)
956 ;; If file doesn't exist, check if directory is writable.
957 (and (file-directory-p (file-name-directory filename
))
958 (file-writable-p (file-name-directory filename
)))))))
960 (defun tramp-gvfs-handle-insert-directory
961 (filename switches
&optional wildcard full-directory-p
)
962 "Like `insert-directory' for Tramp files."
963 ;; gvfs-* output is hard to parse. So we let `ls-lisp' do the job.
964 (with-parsed-tramp-file-name (expand-file-name filename
) nil
965 (with-tramp-progress-reporter v
0 (format "Opening directory %s" filename
)
967 (let (ls-lisp-use-insert-directory-program)
968 (tramp-run-real-handler
970 (list filename switches wildcard full-directory-p
))))))
972 (defun tramp-gvfs-handle-insert-file-contents
973 (filename &optional visit beg end replace
)
974 "Like `insert-file-contents' for Tramp files."
975 (barf-if-buffer-read-only)
976 (setq filename
(expand-file-name filename
))
977 (let (tmpfile result
)
979 (if (not (file-exists-p filename
))
980 ;; We don't raise a Tramp error, because it might be
981 ;; suppressed, like in `find-file-noselect-1'.
982 (signal 'file-error
(list "File not found on remote host" filename
))
984 (setq tmpfile
(file-local-copy filename
)
985 result
(insert-file-contents tmpfile visit beg end replace
)))
988 (setq buffer-file-name filename
)
989 (setq buffer-read-only
(not (file-writable-p filename
)))
990 (set-visited-file-modtime)
991 (set-buffer-modified-p nil
))
992 (when (stringp tmpfile
)
993 (delete-file tmpfile
)))
996 (list filename
(cadr result
))))
998 (defun tramp-gvfs-handle-make-directory (dir &optional parents
)
999 "Like `make-directory' for Tramp files."
1000 (with-parsed-tramp-file-name dir nil
1003 'tramp-gvfs-send-command v
"gvfs-mkdir"
1005 (list "-p" (tramp-gvfs-url-file-name dir
))
1006 (list (tramp-gvfs-url-file-name dir
))))
1007 ;; Propagate the error.
1008 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))
1010 (defun tramp-gvfs-handle-rename-file
1011 (filename newname
&optional ok-if-already-exists
)
1012 "Like `rename-file' for Tramp files."
1013 (with-parsed-tramp-file-name
1014 (if (tramp-tramp-file-p filename
) filename newname
) nil
1016 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1018 v
'file-already-exists
"File %s already exists" newname
))
1020 (if (or (and (tramp-tramp-file-p filename
)
1021 (not (tramp-gvfs-file-name-p filename
)))
1022 (and (tramp-tramp-file-p newname
)
1023 (not (tramp-gvfs-file-name-p newname
))))
1025 ;; We cannot move directly.
1026 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
1027 (rename-file filename tmpfile t
)
1028 (rename-file tmpfile newname ok-if-already-exists
))
1031 (with-tramp-progress-reporter
1032 v
0 (format "Renaming %s to %s" filename newname
)
1034 (tramp-gvfs-send-command
1036 (tramp-gvfs-url-file-name filename
)
1037 (tramp-gvfs-url-file-name newname
))
1038 ;; Propagate the error.
1039 (with-current-buffer (tramp-get-buffer v
)
1040 (goto-char (point-min))
1041 (tramp-error-with-buffer
1043 "Renaming failed, see buffer `%s' for details." (buffer-name)))))
1045 (when (file-remote-p filename
)
1046 (with-parsed-tramp-file-name filename nil
1047 (tramp-flush-file-property v
(file-name-directory localname
))
1048 (tramp-flush-file-property v localname
)))
1050 (when (file-remote-p newname
)
1051 (with-parsed-tramp-file-name newname nil
1052 (tramp-flush-file-property v
(file-name-directory localname
))
1053 (tramp-flush-file-property v localname
))))))
1055 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list
)
1056 "Like `set-visited-file-modtime' for Tramp files."
1057 (unless (buffer-file-name)
1058 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1061 (let ((f (buffer-file-name)))
1062 (with-parsed-tramp-file-name f nil
1063 (let ((remote-file-name-inhibit-cache t
)
1064 (attr (file-attributes f
)))
1065 ;; '(-1 65535) means file doesn't exists yet.
1066 (setq time-list
(or (nth 5 attr
) '(-1 65535)))))))
1067 ;; We use '(0 0) as a don't-know value.
1068 (unless (not (equal time-list
'(0 0)))
1069 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))))
1071 (defun tramp-gvfs-handle-write-region
1072 (start end filename
&optional append visit lockname confirm
)
1073 "Like `write-region' for Tramp files."
1074 (with-parsed-tramp-file-name filename nil
1075 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1076 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
1077 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
1078 (tramp-error v
'file-error
"File not overwritten")))
1080 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
1081 (write-region start end tmpfile
)
1083 (rename-file tmpfile filename
)
1085 (delete-file tmpfile
)
1087 v
'file-error
"Couldn't write region to `%s'" filename
))))
1089 (tramp-flush-file-property v
(file-name-directory localname
))
1090 (tramp-flush-file-property v localname
)
1092 ;; Set file modification time.
1093 (when (or (eq visit t
) (stringp visit
))
1094 (set-visited-file-modtime (nth 5 (file-attributes filename
))))
1097 (when (or (eq visit t
) (null visit
) (stringp visit
))
1098 (tramp-message v
0 "Wrote %s" filename
))
1099 (run-hooks 'tramp-handle-write-region-hook
)))
1102 ;; File name conversions.
1104 (defun tramp-gvfs-url-file-name (filename)
1105 "Return FILENAME in URL syntax."
1106 ;; "/" must NOT be hexlified.
1107 (let ((url-unreserved-chars (append '(?
/) url-unreserved-chars
))
1112 (if (tramp-tramp-file-p filename
)
1113 (with-parsed-tramp-file-name filename nil
1114 (when (and user
(string-match tramp-user-with-domain-regexp user
))
1116 (concat (match-string 2 user
) ";" (match-string 1 user
))))
1117 (url-parse-make-urlobj
1118 method
(url-hexify-string user
) nil
1119 (tramp-file-name-real-host v
) (tramp-file-name-port v
)
1120 (url-hexify-string localname
) nil nil t
))
1121 (url-parse-make-urlobj
1122 "file" nil nil nil nil
1123 (url-hexify-string (file-truename filename
)) nil nil t
))))
1124 (when (tramp-tramp-file-p filename
)
1125 (with-parsed-tramp-file-name filename nil
1126 (tramp-message v
10 "remote file `%s' is URL `%s'" filename result
)))
1129 (defun tramp-gvfs-object-path (filename)
1130 "Create a D-Bus object path from FILENAME."
1131 (expand-file-name (dbus-escape-as-identifier filename
) tramp-gvfs-path-tramp
))
1133 (defun tramp-gvfs-file-name (object-path)
1134 "Retrieve file name from D-Bus OBJECT-PATH."
1135 (dbus-unescape-from-identifier
1136 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path
)))
1138 (defun tramp-gvfs-fuse-file-name (filename)
1139 "Return FUSE file name, which is directly accessible."
1140 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1141 (tramp-gvfs-maybe-open-connection v
)
1142 (let ((prefix (tramp-get-file-property v
"/" "prefix" ""))
1144 (tramp-get-file-property v
"/" "fuse-mountpoint" nil
)))
1145 (unless fuse-mountpoint
1147 v
'file-error
"There is no FUSE mount point for `%s'" filename
))
1148 ;; We must hide the prefix, if any.
1149 (when (string-match (concat "^" (regexp-quote prefix
)) localname
)
1150 (setq localname
(replace-match "" t t localname
)))
1152 v
10 "remote file `%s' is local file `%s'"
1153 filename
(concat fuse-mountpoint localname
))
1154 (concat fuse-mountpoint localname
))))
1156 (defun tramp-bluez-address (device)
1157 "Return bluetooth device address from a given bluetooth DEVICE name."
1158 (when (stringp device
)
1159 (if (string-match tramp-ipv6-regexp device
)
1160 (match-string 0 device
)
1161 (cadr (assoc device
(tramp-bluez-list-devices))))))
1163 (defun tramp-bluez-device (address)
1164 "Return bluetooth device name from a given bluetooth device ADDRESS.
1165 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1166 (when (stringp address
)
1167 (while (string-match "[][]" address
)
1168 (setq address
(replace-match "" t t address
)))
1170 (dolist (item (tramp-bluez-list-devices) result
)
1171 (when (string-match address
(cadr item
))
1172 (setq result
(car item
)))))))
1175 ;; D-Bus GVFS functions.
1177 (defun tramp-gvfs-handler-askpassword (message user domain flags
)
1178 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1180 (tramp-gvfs-file-name (dbus-event-path-name last-input-event
)))
1184 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message
)
1185 (capitalize (match-string 1 message
))
1191 (with-parsed-tramp-file-name filename l
1192 (when (and (zerop (length user
))
1194 (zerop (logand flags tramp-gvfs-password-need-username
))))
1195 (setq user
(read-string "User name: ")))
1196 (when (and (zerop (length domain
))
1197 (not (zerop (logand flags tramp-gvfs-password-need-domain
))))
1198 (setq domain
(read-string "Domain name: ")))
1200 (tramp-message l
6 "%S %S %S %d" message user domain flags
)
1201 (setq tramp-current-method l-method
1202 tramp-current-user user
1203 tramp-current-host l-host
1204 password
(tramp-read-passwd
1205 (tramp-get-connection-process l
) pw-prompt
))
1208 (if (stringp password
)
1210 t
;; password handled.
1211 nil
;; no abort of D-Bus.
1213 (tramp-file-name-real-user l
)
1215 nil
;; not anonymous.
1216 0) ;; no password save.
1217 ;; No password provided.
1218 (list nil t
"" (tramp-file-name-real-user l
) domain nil
0)))
1220 ;; When QUIT is raised, we shall return this information to D-Bus.
1221 (quit (list nil t
"" "" "" nil
0)))))
1223 (defun tramp-gvfs-handler-askquestion (message choices
)
1224 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1225 (save-window-excursion
1226 (let ((enable-recursive-minibuffers t
)
1230 (with-parsed-tramp-file-name
1231 (tramp-gvfs-file-name (dbus-event-path-name last-input-event
)) nil
1232 (tramp-message v
6 "%S %S" message choices
)
1234 ;; In theory, there can be several choices. Until now,
1235 ;; there is only the question whether to accept an unknown
1238 ;; Preserve message for `progress-reporter'.
1239 (tramp-compat-with-temp-message ""
1241 (pop-to-buffer (current-buffer))
1242 (setq choice
(if (yes-or-no-p (concat (car choices
) " ")) 0 1))
1243 (tramp-message v
6 "%d" choice
)))
1245 ;; When the choice is "no", we set a dummy fuse-mountpoint
1246 ;; in order to leave the timeout.
1247 (unless (zerop choice
)
1248 (tramp-set-file-property v
"/" "fuse-mountpoint" "/"))
1252 nil
;; no abort of D-Bus.
1255 ;; When QUIT is raised, we shall return this information to D-Bus.
1256 (quit (list nil t
0))))))
1258 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1259 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1260 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1262 (let ((signal-name (dbus-event-member-name last-input-event
))
1264 ;; Jump over the first elements of the mount info. Since there
1265 ;; were changes in the entries, we cannot access dedicated
1267 (while (stringp (car elt
)) (setq elt
(cdr elt
)))
1268 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt
)))
1269 (mount-spec (caddr elt
))
1270 (default-location (tramp-gvfs-dbus-byte-array-to-string
1272 (method (tramp-gvfs-dbus-byte-array-to-string
1273 (cadr (assoc "type" (cadr mount-spec
)))))
1274 (user (tramp-gvfs-dbus-byte-array-to-string
1275 (cadr (assoc "user" (cadr mount-spec
)))))
1276 (domain (tramp-gvfs-dbus-byte-array-to-string
1277 (cadr (assoc "domain" (cadr mount-spec
)))))
1278 (host (tramp-gvfs-dbus-byte-array-to-string
1279 (cadr (or (assoc "host" (cadr mount-spec
))
1280 (assoc "server" (cadr mount-spec
))))))
1281 (port (tramp-gvfs-dbus-byte-array-to-string
1282 (cadr (assoc "port" (cadr mount-spec
)))))
1283 (ssl (tramp-gvfs-dbus-byte-array-to-string
1284 (cadr (assoc "ssl" (cadr mount-spec
)))))
1285 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1287 (tramp-gvfs-dbus-byte-array-to-string
1288 (cadr (assoc "share" (cadr mount-spec
)))))))
1289 (when (string-match "^smb" method
)
1290 (setq method
"smb"))
1291 (when (string-equal "obex" method
)
1292 (setq host
(tramp-bluez-device host
)))
1293 (when (and (string-equal "dav" method
) (string-equal "true" ssl
))
1294 (setq method
"davs"))
1295 (unless (zerop (length domain
))
1296 (setq user
(concat user tramp-prefix-domain-format domain
)))
1297 (unless (zerop (length port
))
1298 (setq host
(concat host tramp-prefix-port-format port
)))
1299 (with-parsed-tramp-file-name
1300 (tramp-make-tramp-file-name method user host
"") nil
1303 signal-name
(tramp-gvfs-stringify-dbus-message mount-info
))
1304 (tramp-set-file-property v
"/" "list-mounts" 'undef
)
1305 (if (string-equal (downcase signal-name
) "unmounted")
1306 (tramp-set-file-property v
"/" "fuse-mountpoint" nil
)
1307 ;; Set prefix, mountpoint and location.
1308 (unless (string-equal prefix
"/")
1309 (tramp-set-file-property v
"/" "prefix" prefix
))
1310 (tramp-set-file-property v
"/" "fuse-mountpoint" fuse-mountpoint
)
1311 (tramp-set-file-property
1312 v
"/" "default-location" default-location
)))))))
1314 (when tramp-gvfs-enabled
1315 (dbus-register-signal
1316 :session nil tramp-gvfs-path-mounttracker
1317 tramp-gvfs-interface-mounttracker
"mounted"
1318 'tramp-gvfs-handler-mounted-unmounted
)
1319 (dbus-register-signal
1320 :session nil tramp-gvfs-path-mounttracker
1321 tramp-gvfs-interface-mounttracker
"Mounted"
1322 'tramp-gvfs-handler-mounted-unmounted
)
1324 (dbus-register-signal
1325 :session nil tramp-gvfs-path-mounttracker
1326 tramp-gvfs-interface-mounttracker
"unmounted"
1327 'tramp-gvfs-handler-mounted-unmounted
)
1328 (dbus-register-signal
1329 :session nil tramp-gvfs-path-mounttracker
1330 tramp-gvfs-interface-mounttracker
"Unmounted"
1331 'tramp-gvfs-handler-mounted-unmounted
))
1333 (defun tramp-gvfs-connection-mounted-p (vec)
1334 "Check, whether the location is already mounted."
1336 (tramp-get-file-property vec
"/" "fuse-mountpoint" nil
)
1340 (with-tramp-file-property vec
"/" "list-mounts"
1341 (with-tramp-dbus-call-method vec t
1342 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1343 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts
))
1345 ;; Jump over the first elements of the mount info. Since there
1346 ;; were changes in the entries, we cannot access dedicated
1348 (while (stringp (car elt
)) (setq elt
(cdr elt
)))
1349 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1351 (mount-spec (caddr elt
))
1352 (default-location (tramp-gvfs-dbus-byte-array-to-string
1354 (method (tramp-gvfs-dbus-byte-array-to-string
1355 (cadr (assoc "type" (cadr mount-spec
)))))
1356 (user (tramp-gvfs-dbus-byte-array-to-string
1357 (cadr (assoc "user" (cadr mount-spec
)))))
1358 (domain (tramp-gvfs-dbus-byte-array-to-string
1359 (cadr (assoc "domain" (cadr mount-spec
)))))
1360 (host (tramp-gvfs-dbus-byte-array-to-string
1361 (cadr (or (assoc "host" (cadr mount-spec
))
1362 (assoc "server" (cadr mount-spec
))))))
1363 (port (tramp-gvfs-dbus-byte-array-to-string
1364 (cadr (assoc "port" (cadr mount-spec
)))))
1365 (ssl (tramp-gvfs-dbus-byte-array-to-string
1366 (cadr (assoc "ssl" (cadr mount-spec
)))))
1367 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1369 (tramp-gvfs-dbus-byte-array-to-string
1370 (cadr (assoc "share" (cadr mount-spec
)))))))
1371 (when (string-match "^smb" method
)
1372 (setq method
"smb"))
1373 (when (string-equal "obex" method
)
1374 (setq host
(tramp-bluez-device host
)))
1375 (when (and (string-equal "dav" method
) (string-equal "true" ssl
))
1376 (setq method
"davs"))
1377 (when (and (string-equal "synce" method
) (zerop (length user
)))
1378 (setq user
(or (tramp-file-name-user vec
) "")))
1379 (unless (zerop (length domain
))
1380 (setq user
(concat user tramp-prefix-domain-format domain
)))
1381 (unless (zerop (length port
))
1382 (setq host
(concat host tramp-prefix-port-format port
)))
1384 (string-equal method
(tramp-file-name-method vec
))
1385 (string-equal user
(or (tramp-file-name-user vec
) ""))
1386 (string-equal host
(tramp-file-name-host vec
))
1387 (string-match (concat "^" (regexp-quote prefix
))
1388 (tramp-file-name-localname vec
)))
1389 ;; Set prefix, mountpoint and location.
1390 (unless (string-equal prefix
"/")
1391 (tramp-set-file-property vec
"/" "prefix" prefix
))
1392 (tramp-set-file-property vec
"/" "fuse-mountpoint" fuse-mountpoint
)
1393 (tramp-set-file-property vec
"/" "default-location" default-location
)
1394 (throw 'mounted t
)))))))
1396 (defun tramp-gvfs-mount-spec-entry (key value
)
1397 "Construct a mount-spec entry to be used in a mount_spec.
1398 It was \"a(say)\", but has changed to \"a{sv})\"."
1399 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature
)
1400 (list :dict-entry key
1401 (list :variant
(tramp-gvfs-dbus-string-to-byte-array value
)))
1402 (list :struct key
(tramp-gvfs-dbus-string-to-byte-array value
))))
1404 (defun tramp-gvfs-mount-spec (vec)
1405 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1406 (let* ((method (tramp-file-name-method vec
))
1407 (user (tramp-file-name-real-user vec
))
1408 (domain (tramp-file-name-domain vec
))
1409 (host (tramp-file-name-real-host vec
))
1410 (port (tramp-file-name-port vec
))
1411 (localname (tramp-file-name-localname vec
))
1412 (ssl (if (string-match "^davs" method
) "true" "false"))
1413 (mount-spec '(:array
))
1421 ((string-equal "smb" method
)
1422 (string-match "^/?\\([^/]+\\)" localname
)
1423 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1424 (tramp-gvfs-mount-spec-entry "server" host
)
1425 (tramp-gvfs-mount-spec-entry "share" (match-string 1 localname
))))
1426 ((string-equal "obex" method
)
1427 (list (tramp-gvfs-mount-spec-entry "type" method
)
1428 (tramp-gvfs-mount-spec-entry
1429 "host" (concat "[" (tramp-bluez-address host
) "]"))))
1430 ((string-match "^dav" method
)
1431 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1432 (tramp-gvfs-mount-spec-entry "host" host
)
1433 (tramp-gvfs-mount-spec-entry "ssl" ssl
)))
1435 (list (tramp-gvfs-mount-spec-entry "type" method
)
1436 (tramp-gvfs-mount-spec-entry "host" host
))))))
1440 'mount-spec
(tramp-gvfs-mount-spec-entry "user" user
) 'append
))
1444 'mount-spec
(tramp-gvfs-mount-spec-entry "domain" domain
) 'append
))
1448 'mount-spec
(tramp-gvfs-mount-spec-entry "port" (number-to-string port
))
1451 (when (and (string-match "^dav" method
)
1452 (string-match "^/?[^/]+" localname
))
1453 (setq mount-pref
(match-string 0 localname
)))
1456 `(:struct
,(tramp-gvfs-dbus-string-to-byte-array mount-pref
) ,mount-spec
)))
1459 ;; Connection functions.
1461 (defun tramp-gvfs-maybe-open-connection (vec)
1462 "Maybe open a connection VEC.
1463 Does not do anything if a connection is already open, but re-opens the
1464 connection if a previous connection has died for some reason."
1466 ;; We set the file name, in case there are incoming D-Bus signals or
1468 (setq tramp-gvfs-dbus-event-vector vec
)
1470 ;; For password handling, we need a process bound to the connection
1471 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1473 (unless (get-buffer-process (tramp-get-connection-buffer vec
))
1474 (let ((p (make-network-process
1475 :name
(tramp-buffer-name vec
)
1476 :buffer
(tramp-get-connection-buffer vec
)
1477 :server t
:host
'local
:service t
)))
1478 (tramp-compat-set-process-query-on-exit-flag p nil
)))
1480 (unless (tramp-gvfs-connection-mounted-p vec
)
1481 (let* ((method (tramp-file-name-method vec
))
1482 (user (tramp-file-name-user vec
))
1483 (host (tramp-file-name-host vec
))
1484 (localname (tramp-file-name-localname vec
))
1486 (tramp-gvfs-object-path
1487 (tramp-make-tramp-file-name method user host
""))))
1489 (when (and (string-equal method
"smb")
1490 (string-equal localname
"/"))
1491 (tramp-error vec
'file-error
"Filename must contain a Windows share"))
1493 (with-tramp-progress-reporter
1495 (if (zerop (length user
))
1496 (format "Opening connection for %s using %s" host method
)
1497 (format "Opening connection for %s@%s using %s" user host method
))
1499 ;; Enable auth-source and password-cache.
1500 (tramp-set-connection-property vec
"first-password-request" t
)
1502 ;; There will be a callback of "askPassword" when a password is
1504 (dbus-register-method
1505 :session dbus-service-emacs object-path
1506 tramp-gvfs-interface-mountoperation
"askPassword"
1507 'tramp-gvfs-handler-askpassword
)
1508 (dbus-register-method
1509 :session dbus-service-emacs object-path
1510 tramp-gvfs-interface-mountoperation
"AskPassword"
1511 'tramp-gvfs-handler-askpassword
)
1513 ;; There could be a callback of "askQuestion" when adding fingerprint.
1514 (dbus-register-method
1515 :session dbus-service-emacs object-path
1516 tramp-gvfs-interface-mountoperation
"askQuestion"
1517 'tramp-gvfs-handler-askquestion
)
1518 (dbus-register-method
1519 :session dbus-service-emacs object-path
1520 tramp-gvfs-interface-mountoperation
"AskQuestion"
1521 'tramp-gvfs-handler-askquestion
)
1523 ;; The call must be asynchronously, because of the "askPassword"
1524 ;; or "askQuestion"callbacks.
1525 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature
)
1526 (with-tramp-dbus-call-method vec nil
1527 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1528 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1529 (tramp-gvfs-mount-spec vec
)
1530 `(:struct
:string
,(dbus-get-unique-name :session
)
1531 :object-path
,object-path
))
1532 (with-tramp-dbus-call-method vec nil
1533 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1534 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1535 (tramp-gvfs-mount-spec vec
)
1536 :string
(dbus-get-unique-name :session
) :object-path object-path
))
1538 ;; We must wait, until the mount is applied. This will be
1539 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1543 (if (zerop (length (tramp-file-name-user vec
)))
1546 "Timeout reached mounting %s using %s" host method
)
1549 "Timeout reached mounting %s@%s using %s" user host method
)))
1550 (while (not (tramp-get-file-property vec
"/" "fuse-mountpoint" nil
))
1551 (read-event nil nil
0.1)))
1553 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1554 ;; is marked with the fuse-mountpoint "/". We shall react.
1556 (tramp-get-file-property vec
"/" "fuse-mountpoint" "") "/")
1557 (tramp-error vec
'file-error
"FUSE mount denied"))
1559 ;; In `tramp-check-cached-permissions', the connection
1560 ;; properties {uig,gid}-{integer,string} are used. We set
1561 ;; them to their local counterparts.
1562 (tramp-set-connection-property
1563 vec
"uid-integer" (tramp-get-local-uid 'integer
))
1564 (tramp-set-connection-property
1565 vec
"gid-integer" (tramp-get-local-gid 'integer
))
1566 (tramp-set-connection-property
1567 vec
"uid-string" (tramp-get-local-uid 'string
))
1568 (tramp-set-connection-property
1569 vec
"gid-string" (tramp-get-local-gid 'string
))))))
1571 (defun tramp-gvfs-send-command (vec command
&rest args
)
1572 "Send the COMMAND with its ARGS to connection VEC.
1573 COMMAND is usually a command from the gvfs-* utilities.
1574 `call-process' is applied, and it returns `t' if the return code is zero."
1576 (with-current-buffer (tramp-get-connection-buffer vec
)
1577 (tramp-gvfs-maybe-open-connection vec
)
1579 (tramp-message vec
6 "%s %s" command
(mapconcat 'identity args
" "))
1580 (setq result
(apply 'tramp-call-process command nil t nil args
))
1581 (tramp-message vec
6 "\n%s" (buffer-string))
1585 ;; D-Bus BLUEZ functions.
1587 (defun tramp-bluez-list-devices ()
1588 "Return all discovered bluetooth devices as list.
1589 Every entry is a list (NAME ADDRESS).
1591 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1592 discovery happened more time before indicated there, a rescan will be
1593 started, which lasts some ten seconds. Otherwise, cached results will
1595 ;; Reset the scanned devices list if time has passed.
1596 (and (integerp tramp-bluez-discover-devices-timeout
)
1597 (integerp tramp-bluez-discovery
)
1598 (> (tramp-time-diff (current-time) tramp-bluez-discovery
)
1599 tramp-bluez-discover-devices-timeout
)
1600 (setq tramp-bluez-devices nil
))
1602 ;; Rescan if needed.
1603 (unless tramp-bluez-devices
1605 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1606 :system tramp-bluez-service
"/"
1607 tramp-bluez-interface-manager
"DefaultAdapter")))
1608 (setq tramp-bluez-devices nil
1609 tramp-bluez-discovery t
)
1610 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1611 :system tramp-bluez-service object-path
1612 tramp-bluez-interface-adapter
"StartDiscovery")
1613 (while tramp-bluez-discovery
1614 (read-event nil nil
0.1))))
1615 (setq tramp-bluez-discovery
(current-time))
1616 (tramp-message tramp-gvfs-dbus-event-vector
10 "%s" tramp-bluez-devices
)
1617 tramp-bluez-devices
)
1619 (defun tramp-bluez-property-changed (property value
)
1620 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1621 (tramp-message tramp-gvfs-dbus-event-vector
6 "%s %s" property value
)
1623 ((string-equal property
"Discovering")
1625 ;; "Discovering" FALSE means discovery run has been completed.
1626 ;; We stop it, because we don't need another run.
1627 (setq tramp-bluez-discovery nil
)
1628 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1629 :system tramp-bluez-service
(dbus-event-path-name last-input-event
)
1630 tramp-bluez-interface-adapter
"StopDiscovery")))))
1632 (dbus-register-signal
1633 :system nil nil tramp-bluez-interface-adapter
"PropertyChanged"
1634 'tramp-bluez-property-changed
)
1636 (defun tramp-bluez-device-found (device args
)
1637 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1638 (tramp-message tramp-gvfs-dbus-event-vector
6 "%s %s" device args
)
1639 (let ((alias (car (cadr (assoc "Alias" args
))))
1640 (address (car (cadr (assoc "Address" args
)))))
1641 ;; Maybe we shall check the device class for being a proper
1642 ;; device, and call also SDP in order to find the obex service.
1643 (add-to-list 'tramp-bluez-devices
(list alias address
))))
1645 (dbus-register-signal
1646 :system nil nil tramp-bluez-interface-adapter
"DeviceFound"
1647 'tramp-bluez-device-found
)
1649 (defun tramp-bluez-parse-device-names (ignore)
1650 "Return a list of (nil host) tuples allowed to access."
1652 (lambda (x) (list nil
(car x
)))
1653 (tramp-bluez-list-devices)))
1655 ;; Add completion function for OBEX method.
1656 (when (member tramp-bluez-service
(dbus-list-known-names :system
))
1657 (tramp-set-completion-function
1658 "obex" '((tramp-bluez-parse-device-names ""))))
1661 ;; D-Bus zeroconf functions.
1663 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1664 "Return a list of (user host) tuples allowed to access."
1667 (list nil
(zeroconf-service-host x
)))
1668 (zeroconf-list-services "_workstation._tcp")))
1670 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1671 "Return a list of (user host) tuples allowed to access."
1674 (let ((host (zeroconf-service-host x
))
1675 (port (zeroconf-service-port x
))
1676 (text (zeroconf-service-txt x
))
1679 (setq host
(format "%s%s%d" host tramp-prefix-port-regexp port
)))
1680 ;; A user is marked in a TXT field like "u=guest".
1682 (when (string-match "u=\\(.+\\)$" (car text
))
1683 (setq user
(match-string 1 (car text
))))
1684 (setq text
(cdr text
)))
1686 (zeroconf-list-services "_webdav._tcp")))
1688 ;; Add completion function for DAV and DAVS methods.
1689 (when (member zeroconf-service-avahi
(dbus-list-known-names :system
))
1690 (zeroconf-init tramp-gvfs-zeroconf-domain
)
1691 (tramp-set-completion-function
1692 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1693 (tramp-set-completion-function
1694 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1695 (tramp-set-completion-function
1696 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1699 ;; D-Bus SYNCE functions.
1701 (defun tramp-synce-list-devices ()
1702 "Return all discovered synce devices as list.
1703 They are retrieved from the hal daemon."
1704 (let (tramp-synce-devices)
1706 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1707 :system tramp-hal-service tramp-hal-path-manager
1708 tramp-hal-interface-manager
"GetAllDevices"))
1709 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1710 :system tramp-hal-service device tramp-hal-interface-device
1711 "PropertyExists" "sync.plugin")
1713 'tramp-synce-devices
1714 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1715 :system tramp-hal-service device tramp-hal-interface-device
1716 "GetPropertyString" "pda.pocketpc.name"))))
1717 (tramp-message tramp-gvfs-dbus-event-vector
10 "%s" tramp-synce-devices
)
1718 tramp-synce-devices
))
1720 (defun tramp-synce-parse-device-names (ignore)
1721 "Return a list of (nil host) tuples allowed to access."
1723 (lambda (x) (list nil x
))
1724 (tramp-synce-list-devices)))
1726 ;; Add completion function for SYNCE method.
1727 (tramp-set-completion-function
1728 "synce" '((tramp-synce-parse-device-names "")))
1730 (add-hook 'tramp-unload-hook
1732 (unload-feature 'tramp-gvfs
'force
)))
1734 (provide 'tramp-gvfs
)
1738 ;; * Host name completion via smb-server or smb-network.
1739 ;; * Check how two shares of the same SMB server can be mounted in
1741 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1743 ;; * Implement obex for other serial communication but bluetooth.
1745 ;;; tramp-gvfs.el ends here