Merge changes from emacs-23 branch.
[bpt/emacs.git] / lisp / net / tramp-gvfs.el
1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon
2
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; Access functions for the GVFS daemon from Tramp. Tested with GVFS
27 ;; 1.0.2 (Ubuntu 8.10, Gnome 2.24). It has been reported also to run
28 ;; with GVFS 0.2.5 (Ubuntu 8.04, Gnome 2.22), but there is an
29 ;; incompatibility with the mount_info structure, which has been
30 ;; worked around.
31
32 ;; It has also been tested with GVFS 1.6.2 (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>.
35
36 ;; All actions to mount a remote location, and to retrieve mount
37 ;; information, are performed by D-Bus messages. File operations
38 ;; themselves are performed via the mounted filesystem in ~/.gvfs.
39 ;; Consequently, GNU Emacs 23.1 with enabled D-Bus bindings is a
40 ;; precondition.
41
42 ;; The GVFS D-Bus interface is said to be instable. There are even no
43 ;; introspection data. The interface, as discovered during
44 ;; development time, is given in respective comments.
45
46 ;; The customer option `tramp-gvfs-methods' contains the list of
47 ;; supported connection methods. Per default, these are "dav",
48 ;; "davs", "obex" and "synce". Note that with "obex" it might be
49 ;; necessary to pair with the other bluetooth device, if it hasn't
50 ;; been done already. There might be also some few seconds delay in
51 ;; discovering available bluetooth devices.
52
53 ;; Other possible connection methods are "ftp", "sftp" and "smb".
54 ;; When one of these methods is added to the list, the remote access
55 ;; for that method is performed via GVFS instead of the native Tramp
56 ;; implementation.
57
58 ;; GVFS offers even more connection methods. The complete list of
59 ;; connection methods of the actual GVFS implementation can be
60 ;; retrieved by:
61 ;;
62 ;; (message
63 ;; "%s"
64 ;; (mapcar
65 ;; 'car
66 ;; (dbus-call-method
67 ;; :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
68 ;; tramp-gvfs-interface-mounttracker "listMountableInfo")))
69
70 ;; Note that all other connection methods are not tested, beside the
71 ;; ones offered for customization in `tramp-gvfs-methods'. If you
72 ;; request an additional connection method to be supported, please
73 ;; drop me a note.
74
75 ;; For hostname completion, information is retrieved either from the
76 ;; bluez daemon (for the "obex" method), the hal daemon (for the
77 ;; "synce" method), or from the zeroconf daemon (for the "dav",
78 ;; "davs", and "sftp" methods). The zeroconf daemon is pre-configured
79 ;; to discover services in the "local" domain. If another domain
80 ;; shall be used for discovering services, the customer option
81 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
82
83 ;; Restrictions:
84
85 ;; * The current GVFS implementation does not allow to write on the
86 ;; remote bluetooth device via OBEX.
87 ;;
88 ;; * Two shares of the same SMB server cannot be mounted in parallel.
89
90 ;;; Code:
91
92 ;; D-Bus support in the Emacs core can be disabled with configuration
93 ;; option "--without-dbus". Declare used subroutines and variables.
94 (declare-function dbus-call-method "dbusbind.c")
95 (declare-function dbus-call-method-asynchronously "dbusbind.c")
96 (declare-function dbus-get-unique-name "dbusbind.c")
97 (declare-function dbus-register-method "dbusbind.c")
98 (declare-function dbus-register-signal "dbusbind.c")
99
100 ;; Pacify byte-compiler
101 (eval-when-compile
102 (require 'cl)
103 (require 'custom))
104
105 (require 'tramp)
106
107 ;; We call several `tramp-handle-*' functions directly. So we must
108 ;; reqire that package as well.
109 (require 'tramp-sh)
110
111 (require 'dbus)
112 (require 'url-parse)
113 (require 'url-util)
114 (require 'zeroconf)
115
116 ;;;###tramp-autoload
117 (defcustom tramp-gvfs-methods '("dav" "davs" "obex" "synce")
118 "*List of methods for remote files, accessed with GVFS."
119 :group 'tramp
120 :version "23.2"
121 :type '(repeat (choice (const "dav")
122 (const "davs")
123 (const "ftp")
124 (const "obex")
125 (const "sftp")
126 (const "smb")
127 (const "synce"))))
128
129 ;; Add a default for `tramp-default-user-alist'. Rule: For the SYNCE
130 ;; method, no user is chosen.
131 (add-to-list 'tramp-default-user-alist
132 '("synce" nil nil))
133
134 (defcustom tramp-gvfs-zeroconf-domain "local"
135 "*Zeroconf domain to be used for discovering services, like host names."
136 :group 'tramp
137 :version "23.2"
138 :type 'string)
139
140 ;; Add the methods to `tramp-methods', in order to allow minibuffer
141 ;; completion.
142 ;;;###tramp-autoload
143 (when (featurep 'dbusbind)
144 (dolist (elt tramp-gvfs-methods)
145 (unless (assoc elt tramp-methods)
146 (add-to-list 'tramp-methods (cons elt nil)))))
147
148 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
149 "The preceeding object path for own objects.")
150
151 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
152 "The well known name of the GVFS daemon.")
153
154 ;; Check that GVFS is available. D-Bus integration is available since
155 ;; Emacs 23 on some system types. We don't call `dbus-ping', because
156 ;; this would load dbus.el.
157 (unless (and (tramp-compat-funcall 'dbus-get-unique-name :session)
158 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
159 (error "Package `tramp-gvfs' not supported"))
160
161 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
162 "The object path of the GVFS daemon.")
163
164 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
165 "The mount tracking interface in the GVFS daemon.")
166
167 ;; <interface name='org.gtk.vfs.MountTracker'>
168 ;; <method name='listMounts'>
169 ;; <arg name='mount_info_list'
170 ;; type='a{sosssssbay{aya{say}}ay}'
171 ;; direction='out'/>
172 ;; </method>
173 ;; <method name='mountLocation'>
174 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
175 ;; <arg name='dbus_id' type='s' direction='in'/>
176 ;; <arg name='object_path' type='o' direction='in'/>
177 ;; </method>
178 ;; <signal name='mounted'>
179 ;; <arg name='mount_info'
180 ;; type='{sosssssbay{aya{say}}ay}'/>
181 ;; </signal>
182 ;; <signal name='unmounted'>
183 ;; <arg name='mount_info'
184 ;; type='{sosssssbay{aya{say}}ay}'/>
185 ;; </signal>
186 ;; </interface>
187 ;;
188 ;; STRUCT mount_info
189 ;; STRING dbus_id
190 ;; OBJECT_PATH object_path
191 ;; STRING display_name
192 ;; STRING stable_name
193 ;; STRING x_content_types Since GVFS 1.0 only !!!
194 ;; STRING icon
195 ;; STRING prefered_filename_encoding
196 ;; BOOLEAN user_visible
197 ;; ARRAY BYTE fuse_mountpoint
198 ;; STRUCT mount_spec
199 ;; ARRAY BYTE mount_prefix
200 ;; ARRAY
201 ;; STRUCT mount_spec_item
202 ;; STRING key (server, share, type, user, host, port)
203 ;; ARRAY BYTE value
204 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
205
206 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
207 "Used by the dbus-proxying implementation of GMountOperation.")
208
209 ;; <interface name='org.gtk.vfs.MountOperation'>
210 ;; <method name='askPassword'>
211 ;; <arg name='message' type='s' direction='in'/>
212 ;; <arg name='default_user' type='s' direction='in'/>
213 ;; <arg name='default_domain' type='s' direction='in'/>
214 ;; <arg name='flags' type='u' direction='in'/>
215 ;; <arg name='handled' type='b' direction='out'/>
216 ;; <arg name='aborted' type='b' direction='out'/>
217 ;; <arg name='password' type='s' direction='out'/>
218 ;; <arg name='username' type='s' direction='out'/>
219 ;; <arg name='domain' type='s' direction='out'/>
220 ;; <arg name='anonymous' type='b' direction='out'/>
221 ;; <arg name='password_save' type='u' direction='out'/>
222 ;; </method>
223 ;; <method name='askQuestion'>
224 ;; <arg name='message' type='s' direction='in'/>
225 ;; <arg name='choices' type='as' direction='in'/>
226 ;; <arg name='handled' type='b' direction='out'/>
227 ;; <arg name='aborted' type='b' direction='out'/>
228 ;; <arg name='choice' type='u' direction='out'/>
229 ;; </method>
230 ;; </interface>
231
232 ;; The following flags are used in "askPassword". They are defined in
233 ;; /usr/include/glib-2.0/gio/gioenums.h.
234
235 (defconst tramp-gvfs-password-need-password 1
236 "Operation requires a password.")
237
238 (defconst tramp-gvfs-password-need-username 2
239 "Operation requires a username.")
240
241 (defconst tramp-gvfs-password-need-domain 4
242 "Operation requires a domain.")
243
244 (defconst tramp-gvfs-password-saving-supported 8
245 "Operation supports saving settings.")
246
247 (defconst tramp-gvfs-password-anonymous-supported 16
248 "Operation supports anonymous users.")
249
250 (defconst tramp-bluez-service "org.bluez"
251 "The well known name of the BLUEZ service.")
252
253 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
254 "The manager interface of the BLUEZ daemon.")
255
256 ;; <interface name='org.bluez.Manager'>
257 ;; <method name='DefaultAdapter'>
258 ;; <arg type='o' direction='out'/>
259 ;; </method>
260 ;; <method name='FindAdapter'>
261 ;; <arg type='s' direction='in'/>
262 ;; <arg type='o' direction='out'/>
263 ;; </method>
264 ;; <method name='ListAdapters'>
265 ;; <arg type='ao' direction='out'/>
266 ;; </method>
267 ;; <signal name='AdapterAdded'>
268 ;; <arg type='o'/>
269 ;; </signal>
270 ;; <signal name='AdapterRemoved'>
271 ;; <arg type='o'/>
272 ;; </signal>
273 ;; <signal name='DefaultAdapterChanged'>
274 ;; <arg type='o'/>
275 ;; </signal>
276 ;; </interface>
277
278 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
279 "The adapter interface of the BLUEZ daemon.")
280
281 ;; <interface name='org.bluez.Adapter'>
282 ;; <method name='GetProperties'>
283 ;; <arg type='a{sv}' direction='out'/>
284 ;; </method>
285 ;; <method name='SetProperty'>
286 ;; <arg type='s' direction='in'/>
287 ;; <arg type='v' direction='in'/>
288 ;; </method>
289 ;; <method name='RequestMode'>
290 ;; <arg type='s' direction='in'/>
291 ;; </method>
292 ;; <method name='ReleaseMode'/>
293 ;; <method name='RequestSession'/>
294 ;; <method name='ReleaseSession'/>
295 ;; <method name='StartDiscovery'/>
296 ;; <method name='StopDiscovery'/>
297 ;; <method name='ListDevices'>
298 ;; <arg type='ao' direction='out'/>
299 ;; </method>
300 ;; <method name='CreateDevice'>
301 ;; <arg type='s' direction='in'/>
302 ;; <arg type='o' direction='out'/>
303 ;; </method>
304 ;; <method name='CreatePairedDevice'>
305 ;; <arg type='s' direction='in'/>
306 ;; <arg type='o' direction='in'/>
307 ;; <arg type='s' direction='in'/>
308 ;; <arg type='o' direction='out'/>
309 ;; </method>
310 ;; <method name='CancelDeviceCreation'>
311 ;; <arg type='s' direction='in'/>
312 ;; </method>
313 ;; <method name='RemoveDevice'>
314 ;; <arg type='o' direction='in'/>
315 ;; </method>
316 ;; <method name='FindDevice'>
317 ;; <arg type='s' direction='in'/>
318 ;; <arg type='o' direction='out'/>
319 ;; </method>
320 ;; <method name='RegisterAgent'>
321 ;; <arg type='o' direction='in'/>
322 ;; <arg type='s' direction='in'/>
323 ;; </method>
324 ;; <method name='UnregisterAgent'>
325 ;; <arg type='o' direction='in'/>
326 ;; </method>
327 ;; <signal name='DeviceCreated'>
328 ;; <arg type='o'/>
329 ;; </signal>
330 ;; <signal name='DeviceRemoved'>
331 ;; <arg type='o'/>
332 ;; </signal>
333 ;; <signal name='DeviceFound'>
334 ;; <arg type='s'/>
335 ;; <arg type='a{sv}'/>
336 ;; </signal>
337 ;; <signal name='PropertyChanged'>
338 ;; <arg type='s'/>
339 ;; <arg type='v'/>
340 ;; </signal>
341 ;; <signal name='DeviceDisappeared'>
342 ;; <arg type='s'/>
343 ;; </signal>
344 ;; </interface>
345
346 (defcustom tramp-bluez-discover-devices-timeout 60
347 "Defines seconds since last bluetooth device discovery before rescanning.
348 A value of 0 would require an immediate discovery during hostname
349 completion, nil means to use always cached values for discovered
350 devices."
351 :group 'tramp
352 :version "23.2"
353 :type '(choice (const nil) integer))
354
355 (defvar tramp-bluez-discovery nil
356 "Indicator for a running bluetooth device discovery.
357 It keeps the timestamp of last discovery.")
358
359 (defvar tramp-bluez-devices nil
360 "Alist of detected bluetooth devices.
361 Every entry is a list (NAME ADDRESS).")
362
363 (defconst tramp-hal-service "org.freedesktop.Hal"
364 "The well known name of the HAL service.")
365
366 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
367 "The object path of the HAL daemon manager.")
368
369 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
370 "The manager interface of the HAL daemon.")
371
372 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
373 "The device interface of the HAL daemon.")
374
375 \f
376 ;; New handlers should be added here.
377 (defconst tramp-gvfs-file-name-handler-alist
378 '(
379 (access-file . ignore)
380 (add-name-to-file . tramp-gvfs-handle-copy-file)
381 ;; `byte-compiler-base-file-name' performed by default handler.
382 (copy-file . tramp-gvfs-handle-copy-file)
383 (delete-directory . tramp-gvfs-handle-delete-directory)
384 (delete-file . tramp-gvfs-handle-delete-file)
385 ;; `diff-latest-backup-file' performed by default handler.
386 (directory-file-name . tramp-handle-directory-file-name)
387 (directory-files . tramp-gvfs-handle-directory-files)
388 (directory-files-and-attributes
389 . tramp-gvfs-handle-directory-files-and-attributes)
390 (dired-call-process . ignore)
391 (dired-compress-file . ignore)
392 (dired-uncache . tramp-handle-dired-uncache)
393 ;; `executable-find' is not official yet. performed by default handler.
394 (expand-file-name . tramp-gvfs-handle-expand-file-name)
395 ;; `file-accessible-directory-p' performed by default handler.
396 (file-attributes . tramp-gvfs-handle-file-attributes)
397 (file-directory-p . tramp-gvfs-handle-file-directory-p)
398 (file-executable-p . tramp-gvfs-handle-file-executable-p)
399 (file-exists-p . tramp-gvfs-handle-file-exists-p)
400 (file-local-copy . tramp-gvfs-handle-file-local-copy)
401 ;; `file-modes' performed by default handler.
402 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
403 (file-name-as-directory . tramp-handle-file-name-as-directory)
404 (file-name-completion . tramp-handle-file-name-completion)
405 (file-name-directory . tramp-handle-file-name-directory)
406 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
407 ;; `file-name-sans-versions' performed by default handler.
408 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
409 (file-ownership-preserved-p . ignore)
410 (file-readable-p . tramp-gvfs-handle-file-readable-p)
411 (file-regular-p . tramp-handle-file-regular-p)
412 (file-remote-p . tramp-handle-file-remote-p)
413 (file-selinux-context . tramp-gvfs-handle-file-selinux-context)
414 (file-symlink-p . tramp-handle-file-symlink-p)
415 ;; `file-truename' performed by default handler.
416 (file-writable-p . tramp-gvfs-handle-file-writable-p)
417 (find-backup-file-name . tramp-handle-find-backup-file-name)
418 ;; `find-file-noselect' performed by default handler.
419 ;; `get-file-buffer' performed by default handler.
420 (insert-directory . tramp-gvfs-handle-insert-directory)
421 (insert-file-contents . tramp-gvfs-handle-insert-file-contents)
422 (load . tramp-handle-load)
423 (make-directory . tramp-gvfs-handle-make-directory)
424 (make-directory-internal . ignore)
425 (make-symbolic-link . ignore)
426 (process-file . tramp-gvfs-handle-process-file)
427 (rename-file . tramp-gvfs-handle-rename-file)
428 (set-file-modes . tramp-gvfs-handle-set-file-modes)
429 (set-file-selinux-context . tramp-gvfs-handle-set-file-selinux-context)
430 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime)
431 (shell-command . tramp-gvfs-handle-shell-command)
432 (start-file-process . tramp-gvfs-handle-start-file-process)
433 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
434 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
435 (vc-registered . ignore)
436 (verify-visited-file-modtime
437 . tramp-gvfs-handle-verify-visited-file-modtime)
438 (write-region . tramp-gvfs-handle-write-region)
439 )
440 "Alist of handler functions for Tramp GVFS method.
441 Operations not mentioned here will be handled by the default Emacs primitives.")
442
443 ;;;###tramp-autoload
444 (defsubst tramp-gvfs-file-name-p (filename)
445 "Check if it's a filename handled by the GVFS daemon."
446 (and (tramp-tramp-file-p filename)
447 (let ((method
448 (tramp-file-name-method (tramp-dissect-file-name filename))))
449 (and (stringp method) (member method tramp-gvfs-methods)))))
450
451 ;;;###tramp-autoload
452 (defun tramp-gvfs-file-name-handler (operation &rest args)
453 "Invoke the GVFS related OPERATION.
454 First arg specifies the OPERATION, second arg is a list of arguments to
455 pass to the OPERATION."
456 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
457 (if fn
458 (save-match-data (apply (cdr fn) args))
459 (tramp-run-real-handler operation args))))
460
461 ;; This might be moved to tramp.el. It shall be the first file name
462 ;; handler.
463 ;;;###tramp-autoload
464 (when (featurep 'dbusbind)
465 (add-to-list 'tramp-foreign-file-name-handler-alist
466 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
467
468 (defun tramp-gvfs-stringify-dbus-message (message)
469 "Convert a D-Bus message into readable UTF8 strings, used for traces."
470 (cond
471 ((and (consp message) (characterp (car message)))
472 (format "%S" (dbus-byte-array-to-string message)))
473 ((consp message)
474 (mapcar 'tramp-gvfs-stringify-dbus-message message))
475 ((stringp message)
476 (format "%S" message))
477 (t message)))
478
479 (defmacro with-tramp-dbus-call-method
480 (vec synchronous bus service path interface method &rest args)
481 "Apply a D-Bus call on bus BUS.
482
483 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
484 it is an asynchronous call, with `ignore' as callback function.
485
486 The other arguments have the same meaning as with `dbus-call-method'
487 or `dbus-call-method-asynchronously'. Additionally, the call
488 will be traced by Tramp with trace level 6."
489 `(let ((func (if ,synchronous
490 'dbus-call-method 'dbus-call-method-asynchronously))
491 (args (append (list ,bus ,service ,path ,interface ,method)
492 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
493 result)
494 (tramp-message ,vec 6 "%s %s" func args)
495 (setq result (apply func args))
496 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
497 result))
498
499 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
500 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
501 (tramp-compat-font-lock-add-keywords
502 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
503
504 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
505 "Apply a Tramp GVFS `handler'.
506 In case of an error, modify the error message by replacing
507 `filename' with its GVFS mounted name."
508 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
509 elt)
510 (condition-case err
511 (tramp-compat-funcall ,handler ,@args)
512 (error
513 (setq elt (cdr err))
514 (while elt
515 (when (and (stringp (car elt))
516 (string-match fuse-file-name (car elt)))
517 (setcar elt (replace-match ,filename t t (car elt))))
518 (setq elt (cdr elt)))
519 (signal (car err) (cdr err))))))
520
521 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
522 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
523 (tramp-compat-font-lock-add-keywords
524 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
525
526 (defvar tramp-gvfs-dbus-event-vector nil
527 "Current Tramp file name to be used, as vector.
528 It is needed when D-Bus signals or errors arrive, because there
529 is no information where to trace the message.")
530
531 (defun tramp-gvfs-dbus-event-error (event err)
532 "Called when a D-Bus error message arrives, see `dbus-event-error-hooks'."
533 (when tramp-gvfs-dbus-event-vector
534 ;(tramp-cleanup-connection tramp-gvfs-dbus-event-vector)
535 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
536 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
537
538 (add-hook 'dbus-event-error-hooks 'tramp-gvfs-dbus-event-error)
539
540 \f
541 ;; File name primitives.
542
543 (defun tramp-gvfs-handle-copy-file
544 (filename newname &optional ok-if-already-exists keep-date
545 preserve-uid-gid preserve-selinux-context)
546 "Like `copy-file' for Tramp files."
547 (with-parsed-tramp-file-name
548 (if (tramp-tramp-file-p filename) filename newname) nil
549 (with-progress-reporter
550 v 0 (format "Copying %s to %s" filename newname)
551 (condition-case err
552 (let ((args
553 (list
554 (if (tramp-gvfs-file-name-p filename)
555 (tramp-gvfs-fuse-file-name filename)
556 filename)
557 (if (tramp-gvfs-file-name-p newname)
558 (tramp-gvfs-fuse-file-name newname)
559 newname)
560 ok-if-already-exists keep-date preserve-uid-gid)))
561 (when preserve-selinux-context
562 (setq args (append args (list preserve-selinux-context))))
563 (apply 'copy-file args))
564
565 ;; Error case. Let's try it with the GVFS utilities.
566 (error
567 (tramp-message v 4 "`copy-file' failed, trying `gvfs-copy'")
568 (unless
569 (zerop
570 (let ((args
571 (append (if (or keep-date preserve-uid-gid)
572 (list "--preserve")
573 nil)
574 (list
575 (tramp-gvfs-url-file-name filename)
576 (tramp-gvfs-url-file-name newname)))))
577 (apply 'tramp-gvfs-send-command v "gvfs-copy" args)))
578 ;; Propagate the error.
579 (tramp-error v (car err) "%s" (cdr err)))))))
580
581 (when (file-remote-p newname)
582 (with-parsed-tramp-file-name newname nil
583 (tramp-flush-file-property v (file-name-directory localname))
584 (tramp-flush-file-property v localname))))
585
586 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive)
587 "Like `delete-directory' for Tramp files."
588 (tramp-compat-delete-directory
589 (tramp-gvfs-fuse-file-name directory) recursive))
590
591 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
592 "Like `delete-file' for Tramp files."
593 (tramp-compat-delete-file (tramp-gvfs-fuse-file-name filename) trash))
594
595 (defun tramp-gvfs-handle-directory-files
596 (directory &optional full match nosort)
597 "Like `directory-files' for Tramp files."
598 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
599 (mapcar
600 (lambda (x)
601 (if (string-match fuse-file-name x)
602 (replace-match directory t t x)
603 x))
604 (directory-files fuse-file-name full match nosort))))
605
606 (defun tramp-gvfs-handle-directory-files-and-attributes
607 (directory &optional full match nosort id-format)
608 "Like `directory-files-and-attributes' for Tramp files."
609 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
610 (mapcar
611 (lambda (x)
612 (when (string-match fuse-file-name (car x))
613 (setcar x (replace-match directory t t (car x))))
614 x)
615 (directory-files-and-attributes
616 fuse-file-name full match nosort id-format))))
617
618 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
619 "Like `expand-file-name' for Tramp files."
620 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
621 (setq dir (or dir default-directory "/"))
622 ;; Unless NAME is absolute, concat DIR and NAME.
623 (unless (file-name-absolute-p name)
624 (setq name (concat (file-name-as-directory dir) name)))
625 ;; If NAME is not a Tramp file, run the real handler.
626 (if (not (tramp-tramp-file-p name))
627 (tramp-run-real-handler 'expand-file-name (list name nil))
628 ;; Dissect NAME.
629 (with-parsed-tramp-file-name name nil
630 ;; If there is a default location, expand tilde.
631 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
632 (save-match-data
633 (tramp-gvfs-maybe-open-connection (vector method user host "/")))
634 (setq localname
635 (replace-match
636 (tramp-get-file-property v "/" "default-location" "~")
637 nil t localname 1)))
638 ;; Tilde expansion is not possible.
639 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
640 (tramp-error
641 v 'file-error
642 "Cannot expand tilde in file `%s'" name))
643 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
644 (setq localname (concat "/" localname)))
645 ;; We do not pass "/..".
646 (if (string-equal "smb" method)
647 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
648 (setq localname (replace-match "/" t t localname 1)))
649 (when (string-match "^/\\.\\./?" localname)
650 (setq localname (replace-match "/" t t localname))))
651 ;; There might be a double slash. Remove this.
652 (while (string-match "//" localname)
653 (setq localname (replace-match "/" t t localname)))
654 ;; No tilde characters in file name, do normal
655 ;; `expand-file-name' (this does "/./" and "/../").
656 (tramp-make-tramp-file-name
657 method user host
658 (tramp-run-real-handler
659 'expand-file-name (list localname))))))
660
661 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
662 "Like `file-attributes' for Tramp files."
663 (file-attributes (tramp-gvfs-fuse-file-name filename) id-format))
664
665 (defun tramp-gvfs-handle-file-directory-p (filename)
666 "Like `file-directory-p' for Tramp files."
667 (file-directory-p (tramp-gvfs-fuse-file-name filename)))
668
669 (defun tramp-gvfs-handle-file-executable-p (filename)
670 "Like `file-executable-p' for Tramp files."
671 (file-executable-p (tramp-gvfs-fuse-file-name filename)))
672
673 (defun tramp-gvfs-handle-file-exists-p (filename)
674 "Like `file-exists-p' for Tramp files."
675 (file-exists-p (tramp-gvfs-fuse-file-name filename)))
676
677 (defun tramp-gvfs-handle-file-local-copy (filename)
678 "Like `file-local-copy' for Tramp files."
679 (with-parsed-tramp-file-name filename nil
680 (let ((tmpfile (tramp-compat-make-temp-file filename)))
681 (unless (file-exists-p filename)
682 (tramp-error
683 v 'file-error
684 "Cannot make local copy of non-existing file `%s'" filename))
685 (copy-file filename tmpfile t t)
686 tmpfile)))
687
688 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
689 "Like `file-name-all-completions' for Tramp files."
690 (unless (save-match-data (string-match "/" filename))
691 (file-name-all-completions filename (tramp-gvfs-fuse-file-name directory))))
692
693 (defun tramp-gvfs-handle-file-readable-p (filename)
694 "Like `file-readable-p' for Tramp files."
695 (file-readable-p (tramp-gvfs-fuse-file-name filename)))
696
697 (defun tramp-gvfs-handle-file-selinux-context (filename)
698 "Like `file-selinux-context' for Tramp files."
699 (tramp-compat-funcall
700 'file-selinux-context (tramp-gvfs-fuse-file-name filename)))
701
702 (defun tramp-gvfs-handle-file-writable-p (filename)
703 "Like `file-writable-p' for Tramp files."
704 (file-writable-p (tramp-gvfs-fuse-file-name filename)))
705
706 (defun tramp-gvfs-handle-insert-directory
707 (filename switches &optional wildcard full-directory-p)
708 "Like `insert-directory' for Tramp files."
709 (insert-directory
710 (tramp-gvfs-fuse-file-name filename) switches wildcard full-directory-p))
711
712 (defun tramp-gvfs-handle-insert-file-contents
713 (filename &optional visit beg end replace)
714 "Like `insert-file-contents' for Tramp files."
715 (unwind-protect
716 (let ((fuse-file-name (tramp-gvfs-fuse-file-name filename))
717 (result
718 (insert-file-contents
719 (tramp-gvfs-fuse-file-name filename) visit beg end replace)))
720 (when (string-match fuse-file-name (car result))
721 (setcar result (replace-match filename t t (car result))))
722 result)
723 (setq buffer-file-name filename)))
724
725 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
726 "Like `make-directory' for Tramp files."
727 (with-parsed-tramp-file-name dir nil
728 (condition-case err
729 (with-tramp-gvfs-error-message dir 'make-directory
730 (tramp-gvfs-fuse-file-name dir) parents)
731
732 ;; Error case. Let's try it with the GVFS utilities.
733 (error
734 (tramp-message v 4 "`make-directory' failed, trying `gvfs-mkdir'")
735 (unless
736 (zerop
737 (tramp-gvfs-send-command
738 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir)))
739 ;; Propagate the error.
740 (tramp-error v (car err) "%s" (cdr err)))))))
741
742 (defun tramp-gvfs-handle-process-file
743 (program &optional infile destination display &rest args)
744 "Like `process-file' for Tramp files."
745 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
746 (apply 'call-process program infile destination display args)))
747
748 (defun tramp-gvfs-handle-rename-file
749 (filename newname &optional ok-if-already-exists)
750 "Like `rename-file' for Tramp files."
751 (with-parsed-tramp-file-name
752 (if (tramp-tramp-file-p filename) filename newname) nil
753 (with-progress-reporter
754 v 0 (format "Renaming %s to %s" filename newname)
755 (condition-case err
756 (rename-file
757 (if (tramp-gvfs-file-name-p filename)
758 (tramp-gvfs-fuse-file-name filename)
759 filename)
760 (if (tramp-gvfs-file-name-p newname)
761 (tramp-gvfs-fuse-file-name newname)
762 newname)
763 ok-if-already-exists)
764
765 ;; Error case. Let's try it with the GVFS utilities.
766 (error
767 (tramp-message v 4 "`rename-file' failed, trying `gvfs-move'")
768 (unless
769 (zerop
770 (tramp-gvfs-send-command
771 v "gvfs-move"
772 (tramp-gvfs-url-file-name filename)
773 (tramp-gvfs-url-file-name newname)))
774 ;; Propagate the error.
775 (tramp-error v (car err) "%s" (cdr err)))))))
776
777 (when (file-remote-p filename)
778 (with-parsed-tramp-file-name filename nil
779 (tramp-flush-file-property v (file-name-directory localname))
780 (tramp-flush-file-property v localname)))
781
782 (when (file-remote-p newname)
783 (with-parsed-tramp-file-name newname nil
784 (tramp-flush-file-property v (file-name-directory localname))
785 (tramp-flush-file-property v localname))))
786
787 (defun tramp-gvfs-handle-set-file-modes (filename mode)
788 "Like `set-file-modes' for Tramp files."
789 (with-tramp-gvfs-error-message filename 'set-file-modes
790 (tramp-gvfs-fuse-file-name filename) mode))
791
792 (defun tramp-gvfs-handle-set-file-selinux-context (filename context)
793 "Like `set-file-selinux-context' for Tramp files."
794 (with-tramp-gvfs-error-message filename 'set-file-selinux-context
795 (tramp-gvfs-fuse-file-name filename) context))
796
797 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
798 "Like `set-visited-file-modtime' for Tramp files."
799 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
800 (set-visited-file-modtime time-list)))
801
802 (defun tramp-gvfs-handle-shell-command
803 (command &optional output-buffer error-buffer)
804 "Like `shell-command' for Tramp files."
805 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
806 (shell-command command output-buffer error-buffer)))
807
808 (defun tramp-gvfs-handle-start-file-process (name buffer program &rest args)
809 "Like `start-file-process' for Tramp files."
810 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
811 (apply 'start-process name buffer program args)))
812
813 (defun tramp-gvfs-handle-verify-visited-file-modtime (buf)
814 "Like `verify-visited-file-modtime' for Tramp files."
815 (with-current-buffer buf
816 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
817 (verify-visited-file-modtime buf))))
818
819 (defun tramp-gvfs-handle-write-region
820 (start end filename &optional append visit lockname confirm)
821 "Like `write-region' for Tramp files."
822 (with-parsed-tramp-file-name filename nil
823 (condition-case err
824 (with-tramp-gvfs-error-message filename 'write-region
825 start end (tramp-gvfs-fuse-file-name filename)
826 append visit lockname confirm)
827
828 ;; Error case. Let's try rename.
829 (error
830 (let ((tmpfile (tramp-compat-make-temp-file filename)))
831 (tramp-message v 4 "`write-region' failed, trying `rename-file'")
832 (write-region start end tmpfile)
833 (condition-case nil
834 (rename-file tmpfile filename)
835 (error
836 (delete-file tmpfile)
837 (tramp-error v (car err) "%s" (cdr err)))))))
838
839 ;; Set file modification time.
840 (when (or (eq visit t) (stringp visit))
841 (set-visited-file-modtime (nth 5 (file-attributes filename))))
842
843 ;; The end.
844 (when (or (eq visit t) (null visit) (stringp visit))
845 (tramp-message v 0 "Wrote %s" filename))
846 (run-hooks 'tramp-handle-write-region-hook)))
847
848 \f
849 ;; File name conversions.
850
851 (defun tramp-gvfs-url-file-name (filename)
852 "Return FILENAME in URL syntax."
853 ;; "/" must NOT be hexlified.
854 (let ((url-unreserved-chars (append '(?/) url-unreserved-chars)))
855 (url-recreate-url
856 (if (tramp-tramp-file-p filename)
857 (with-parsed-tramp-file-name (file-truename filename) nil
858 (when (string-match tramp-user-with-domain-regexp user)
859 (setq user
860 (concat (match-string 2 user) ";" (match-string 2 user))))
861 (url-parse-make-urlobj
862 method user nil
863 (tramp-file-name-real-host v) (tramp-file-name-port v)
864 (url-hexify-string localname)))
865 (url-parse-make-urlobj
866 "file" nil nil nil nil (url-hexify-string (file-truename filename)))))))
867
868 (defun tramp-gvfs-object-path (filename)
869 "Create a D-Bus object path from FILENAME."
870 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
871
872 (defun tramp-gvfs-file-name (object-path)
873 "Retrieve file name from D-Bus OBJECT-PATH."
874 (dbus-unescape-from-identifier
875 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
876
877 (defun tramp-gvfs-fuse-file-name (filename)
878 "Return FUSE file name, which is directly accessible."
879 (with-parsed-tramp-file-name (expand-file-name filename) nil
880 (tramp-gvfs-maybe-open-connection v)
881 (let ((prefix (tramp-get-file-property v "/" "prefix" ""))
882 (fuse-mountpoint
883 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
884 (unless fuse-mountpoint
885 (tramp-error
886 v 'file-error "There is no FUSE mount point for `%s'" filename))
887 ;; We must hide the prefix, if any.
888 (when (string-match (concat "^" (regexp-quote prefix)) localname)
889 (setq localname (replace-match "" t t localname)))
890 (tramp-message
891 v 10 "remote file `%s' is local file `%s'"
892 filename (concat fuse-mountpoint localname))
893 (concat fuse-mountpoint localname))))
894
895 (defun tramp-bluez-address (device)
896 "Return bluetooth device address from a given bluetooth DEVICE name."
897 (when (stringp device)
898 (if (string-match tramp-ipv6-regexp device)
899 (match-string 0 device)
900 (cadr (assoc device (tramp-bluez-list-devices))))))
901
902 (defun tramp-bluez-device (address)
903 "Return bluetooth device name from a given bluetooth device ADDRESS.
904 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
905 (when (stringp address)
906 (while (string-match "[][]" address)
907 (setq address (replace-match "" t t address)))
908 (let (result)
909 (dolist (item (tramp-bluez-list-devices) result)
910 (when (string-match address (cadr item))
911 (setq result (car item)))))))
912
913 \f
914 ;; D-Bus GVFS functions.
915
916 (defun tramp-gvfs-handler-askpassword (message user domain flags)
917 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
918 (let* ((filename
919 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
920 (pw-prompt
921 (format
922 "%s for %s "
923 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
924 (capitalize (match-string 1 message))
925 "Password")
926 filename))
927 password)
928
929 (condition-case nil
930 (with-parsed-tramp-file-name filename l
931 (when (and (zerop (length user))
932 (not
933 (zerop (logand flags tramp-gvfs-password-need-username))))
934 (setq user (read-string "User name: ")))
935 (when (and (zerop (length domain))
936 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
937 (setq domain (read-string "Domain name: ")))
938
939 (tramp-message l 6 "%S %S %S %d" message user domain flags)
940 (setq tramp-current-method l-method
941 tramp-current-user user
942 tramp-current-host l-host
943 password (tramp-read-passwd
944 (tramp-get-connection-process l) pw-prompt))
945
946 ;; Return result.
947 (if (stringp password)
948 (list
949 t ;; password handled.
950 nil ;; no abort of D-Bus.
951 password
952 (tramp-file-name-real-user l)
953 domain
954 nil ;; not anonymous.
955 0) ;; no password save.
956 ;; No password provided.
957 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
958
959 ;; When QUIT is raised, we shall return this information to D-Bus.
960 (quit (list nil t "" "" "" nil 0)))))
961
962 (defun tramp-gvfs-handler-askquestion (message choices)
963 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
964 (save-window-excursion
965 (let ((enable-recursive-minibuffers t)
966 choice)
967
968 (condition-case nil
969 (with-parsed-tramp-file-name
970 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
971 (tramp-message v 6 "%S %S" message choices)
972
973 ;; In theory, there can be several choices. Until now,
974 ;; there is only the question whether to accept an unknown
975 ;; host signature.
976 (with-temp-buffer
977 ;; Preserve message for `progress-reporter'.
978 (tramp-compat-with-temp-message ""
979 (insert message)
980 (pop-to-buffer (current-buffer))
981 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
982 (tramp-message v 6 "%d" choice)))
983
984 ;; When the choice is "no", we set a dummy fuse-mountpoint
985 ;; in order to leave the timeout.
986 (unless (zerop choice)
987 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
988
989 (list
990 t ;; handled.
991 nil ;; no abort of D-Bus.
992 choice))
993
994 ;; When QUIT is raised, we shall return this information to D-Bus.
995 (quit (list nil t 0))))))
996
997 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
998 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
999 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1000 (ignore-errors
1001 (let ((signal-name (dbus-event-member-name last-input-event))
1002 (elt mount-info))
1003 ;; Jump over the first elements of the mount info. Since there
1004 ;; were changes in the antries, we cannot access dedicated
1005 ;; elements.
1006 (while (stringp (car elt)) (setq elt (cdr elt)))
1007 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1008 (mount-spec (caddr elt))
1009 (default-location (dbus-byte-array-to-string (cadddr elt)))
1010 (method (dbus-byte-array-to-string
1011 (cadr (assoc "type" (cadr mount-spec)))))
1012 (user (dbus-byte-array-to-string
1013 (cadr (assoc "user" (cadr mount-spec)))))
1014 (domain (dbus-byte-array-to-string
1015 (cadr (assoc "domain" (cadr mount-spec)))))
1016 (host (dbus-byte-array-to-string
1017 (cadr (or (assoc "host" (cadr mount-spec))
1018 (assoc "server" (cadr mount-spec))))))
1019 (port (dbus-byte-array-to-string
1020 (cadr (assoc "port" (cadr mount-spec)))))
1021 (ssl (dbus-byte-array-to-string
1022 (cadr (assoc "ssl" (cadr mount-spec)))))
1023 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1024 (dbus-byte-array-to-string
1025 (cadr (assoc "share" (cadr mount-spec)))))))
1026 (when (string-match "^smb" method)
1027 (setq method "smb"))
1028 (when (string-equal "obex" method)
1029 (setq host (tramp-bluez-device host)))
1030 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1031 (setq method "davs"))
1032 (unless (zerop (length domain))
1033 (setq user (concat user tramp-prefix-domain-format domain)))
1034 (unless (zerop (length port))
1035 (setq host (concat host tramp-prefix-port-format port)))
1036 (with-parsed-tramp-file-name
1037 (tramp-make-tramp-file-name method user host "") nil
1038 (tramp-message
1039 v 6 "%s %s"
1040 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1041 (tramp-set-file-property v "/" "list-mounts" 'undef)
1042 (if (string-equal signal-name "unmounted")
1043 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1044 ;; Set prefix, mountpoint and location.
1045 (unless (string-equal prefix "/")
1046 (tramp-set-file-property v "/" "prefix" prefix))
1047 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1048 (tramp-set-file-property
1049 v "/" "default-location" default-location)))))))
1050
1051 (dbus-register-signal
1052 :session nil tramp-gvfs-path-mounttracker
1053 tramp-gvfs-interface-mounttracker "mounted"
1054 'tramp-gvfs-handler-mounted-unmounted)
1055
1056 (dbus-register-signal
1057 :session nil tramp-gvfs-path-mounttracker
1058 tramp-gvfs-interface-mounttracker "unmounted"
1059 'tramp-gvfs-handler-mounted-unmounted)
1060
1061 (defun tramp-gvfs-connection-mounted-p (vec)
1062 "Check, whether the location is already mounted."
1063 (or
1064 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1065 (catch 'mounted
1066 (dolist
1067 (elt
1068 (with-file-property vec "/" "list-mounts"
1069 (with-tramp-dbus-call-method vec t
1070 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1071 tramp-gvfs-interface-mounttracker "listMounts"))
1072 nil)
1073 ;; Jump over the first elements of the mount info. Since there
1074 ;; were changes in the antries, we cannot access dedicated
1075 ;; elements.
1076 (while (stringp (car elt)) (setq elt (cdr elt)))
1077 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1078 (mount-spec (caddr elt))
1079 (default-location (dbus-byte-array-to-string (cadddr elt)))
1080 (method (dbus-byte-array-to-string
1081 (cadr (assoc "type" (cadr mount-spec)))))
1082 (user (dbus-byte-array-to-string
1083 (cadr (assoc "user" (cadr mount-spec)))))
1084 (domain (dbus-byte-array-to-string
1085 (cadr (assoc "domain" (cadr mount-spec)))))
1086 (host (dbus-byte-array-to-string
1087 (cadr (or (assoc "host" (cadr mount-spec))
1088 (assoc "server" (cadr mount-spec))))))
1089 (port (dbus-byte-array-to-string
1090 (cadr (assoc "port" (cadr mount-spec)))))
1091 (ssl (dbus-byte-array-to-string
1092 (cadr (assoc "ssl" (cadr mount-spec)))))
1093 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1094 (dbus-byte-array-to-string
1095 (cadr (assoc "share" (cadr mount-spec)))))))
1096 (when (string-match "^smb" method)
1097 (setq method "smb"))
1098 (when (string-equal "obex" method)
1099 (setq host (tramp-bluez-device host)))
1100 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1101 (setq method "davs"))
1102 (when (and (string-equal "synce" method) (zerop (length user)))
1103 (setq user (or (tramp-file-name-user vec) "")))
1104 (unless (zerop (length domain))
1105 (setq user (concat user tramp-prefix-domain-format domain)))
1106 (unless (zerop (length port))
1107 (setq host (concat host tramp-prefix-port-format port)))
1108 (when (and
1109 (string-equal method (tramp-file-name-method vec))
1110 (string-equal user (or (tramp-file-name-user vec) ""))
1111 (string-equal host (tramp-file-name-host vec))
1112 (string-match (concat "^" (regexp-quote prefix))
1113 (tramp-file-name-localname vec)))
1114 ;; Set prefix, mountpoint and location.
1115 (unless (string-equal prefix "/")
1116 (tramp-set-file-property vec "/" "prefix" prefix))
1117 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1118 (tramp-set-file-property vec "/" "default-location" default-location)
1119 (throw 'mounted t)))))))
1120
1121 (defun tramp-gvfs-mount-spec (vec)
1122 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1123 (let* ((method (tramp-file-name-method vec))
1124 (user (tramp-file-name-real-user vec))
1125 (domain (tramp-file-name-domain vec))
1126 (host (tramp-file-name-real-host vec))
1127 (port (tramp-file-name-port vec))
1128 (localname (tramp-file-name-localname vec))
1129 (ssl (if (string-match "^davs" method) "true" "false"))
1130 (mount-spec '(:array))
1131 (mount-pref "/"))
1132
1133 (setq
1134 mount-spec
1135 (append
1136 mount-spec
1137 (cond
1138 ((string-equal "smb" method)
1139 (string-match "^/?\\([^/]+\\)" localname)
1140 `((:struct "type" ,(dbus-string-to-byte-array "smb-share"))
1141 (:struct "server" ,(dbus-string-to-byte-array host))
1142 (:struct "share" ,(dbus-string-to-byte-array
1143 (match-string 1 localname)))))
1144 ((string-equal "obex" method)
1145 `((:struct "type" ,(dbus-string-to-byte-array method))
1146 (:struct "host" ,(dbus-string-to-byte-array
1147 (concat "[" (tramp-bluez-address host) "]")))))
1148 ((string-match "^dav" method)
1149 `((:struct "type" ,(dbus-string-to-byte-array "dav"))
1150 (:struct "host" ,(dbus-string-to-byte-array host))
1151 (:struct "ssl" ,(dbus-string-to-byte-array ssl))))
1152 (t
1153 `((:struct "type" ,(dbus-string-to-byte-array method))
1154 (:struct "host" ,(dbus-string-to-byte-array host)))))))
1155
1156 (when user
1157 (add-to-list
1158 'mount-spec
1159 `(:struct "user" ,(dbus-string-to-byte-array user))
1160 'append))
1161
1162 (when domain
1163 (add-to-list
1164 'mount-spec
1165 `(:struct "domain" ,(dbus-string-to-byte-array domain))
1166 'append))
1167
1168 (when port
1169 (add-to-list
1170 'mount-spec
1171 `(:struct "port" ,(dbus-string-to-byte-array (number-to-string port)))
1172 'append))
1173
1174 (when (and (string-match "^dav" method)
1175 (string-match "^/?[^/]+" localname))
1176 (setq mount-pref (match-string 0 localname)))
1177
1178 ;; Return.
1179 `(:struct ,(dbus-string-to-byte-array mount-pref) ,mount-spec)))
1180
1181 \f
1182 ;; Connection functions
1183
1184 (defun tramp-gvfs-maybe-open-connection (vec)
1185 "Maybe open a connection VEC.
1186 Does not do anything if a connection is already open, but re-opens the
1187 connection if a previous connection has died for some reason."
1188
1189 ;; We set the file name, in case there are incoming D-Bus signals or
1190 ;; D-Bus errors.
1191 (setq tramp-gvfs-dbus-event-vector vec)
1192
1193 ;; For password handling, we need a process bound to the connection
1194 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1195 ;; better solution?
1196 (unless (get-buffer-process (tramp-get-buffer vec))
1197 (let ((p (make-network-process
1198 :name (tramp-buffer-name vec)
1199 :buffer (tramp-get-buffer vec)
1200 :server t :host 'local :service t)))
1201 (tramp-set-process-query-on-exit-flag p nil)))
1202
1203 (unless (tramp-gvfs-connection-mounted-p vec)
1204 (let* ((method (tramp-file-name-method vec))
1205 (user (tramp-file-name-user vec))
1206 (host (tramp-file-name-host vec))
1207 (object-path
1208 (tramp-gvfs-object-path
1209 (tramp-make-tramp-file-name method user host ""))))
1210
1211 (with-progress-reporter
1212 vec 3
1213 (if (zerop (length user))
1214 (format "Opening connection for %s using %s" host method)
1215 (format "Opening connection for %s@%s using %s" user host method))
1216
1217 ;; Enable auth-sorce and password-cache.
1218 (tramp-set-connection-property vec "first-password-request" t)
1219
1220 ;; There will be a callback of "askPassword", when a password is
1221 ;; needed.
1222 (dbus-register-method
1223 :session dbus-service-emacs object-path
1224 tramp-gvfs-interface-mountoperation "askPassword"
1225 'tramp-gvfs-handler-askpassword)
1226
1227 ;; There could be a callback of "askQuestion", when adding fingerprint.
1228 (dbus-register-method
1229 :session dbus-service-emacs object-path
1230 tramp-gvfs-interface-mountoperation "askQuestion"
1231 'tramp-gvfs-handler-askquestion)
1232
1233 ;; The call must be asynchronously, because of the "askPassword"
1234 ;; or "askQuestion"callbacks.
1235 (with-tramp-dbus-call-method vec nil
1236 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1237 tramp-gvfs-interface-mounttracker "mountLocation"
1238 (tramp-gvfs-mount-spec vec) (dbus-get-unique-name :session)
1239 :object-path object-path)
1240
1241 ;; We must wait, until the mount is applied. This will be
1242 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1243 ;; file property.
1244 (with-timeout
1245 (60
1246 (if (zerop (length (tramp-file-name-user vec)))
1247 (tramp-error
1248 vec 'file-error
1249 "Timeout reached mounting %s using %s" host method)
1250 (tramp-error
1251 vec 'file-error
1252 "Timeout reached mounting %s@%s using %s" user host method)))
1253 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1254 (read-event nil nil 0.1)))
1255
1256 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1257 ;; is marked with the fuse-mountpoint "/". We shall react.
1258 (when (string-equal
1259 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1260 (tramp-error vec 'file-error "FUSE mount denied"))
1261
1262 ;; We set the connection property "started" in order to put the
1263 ;; remote location into the cache, which is helpful for further
1264 ;; completion.
1265 (tramp-set-connection-property vec "started" t)))))
1266
1267 (defun tramp-gvfs-send-command (vec command &rest args)
1268 "Send the COMMAND with its ARGS to connection VEC.
1269 COMMAND is usually a command from the gvfs-* utilities.
1270 `call-process' is applied, and its return code is returned."
1271 (let (result)
1272 (with-current-buffer (tramp-get-buffer vec)
1273 (erase-buffer)
1274 (tramp-message vec 6 "%s %s" command (mapconcat 'identity args " "))
1275 (setq result (apply 'tramp-local-call-process command nil t nil args))
1276 (tramp-message vec 6 "%s" (buffer-string))
1277 result)))
1278
1279 \f
1280 ;; D-Bus BLUEZ functions.
1281
1282 (defun tramp-bluez-list-devices ()
1283 "Return all discovered bluetooth devices as list.
1284 Every entry is a list (NAME ADDRESS).
1285
1286 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1287 discovery happened more time before indicated there, a rescan will be
1288 started, which lasts some ten seconds. Otherwise, cached results will
1289 be used."
1290 ;; Reset the scanned devices list if time has passed.
1291 (and (integerp tramp-bluez-discover-devices-timeout)
1292 (integerp tramp-bluez-discovery)
1293 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1294 tramp-bluez-discover-devices-timeout)
1295 (setq tramp-bluez-devices nil))
1296
1297 ;; Rescan if needed.
1298 (unless tramp-bluez-devices
1299 (let ((object-path
1300 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1301 :system tramp-bluez-service "/"
1302 tramp-bluez-interface-manager "DefaultAdapter")))
1303 (setq tramp-bluez-devices nil
1304 tramp-bluez-discovery t)
1305 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1306 :system tramp-bluez-service object-path
1307 tramp-bluez-interface-adapter "StartDiscovery")
1308 (while tramp-bluez-discovery
1309 (read-event nil nil 0.1))))
1310 (setq tramp-bluez-discovery (current-time))
1311 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1312 tramp-bluez-devices)
1313
1314 (defun tramp-bluez-property-changed (property value)
1315 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1316 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1317 (cond
1318 ((string-equal property "Discovering")
1319 (unless (car value)
1320 ;; "Discovering" FALSE means discovery run has been completed.
1321 ;; We stop it, because we don't need another run.
1322 (setq tramp-bluez-discovery nil)
1323 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1324 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1325 tramp-bluez-interface-adapter "StopDiscovery")))))
1326
1327 (dbus-register-signal
1328 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1329 'tramp-bluez-property-changed)
1330
1331 (defun tramp-bluez-device-found (device args)
1332 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1333 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1334 (let ((alias (car (cadr (assoc "Alias" args))))
1335 (address (car (cadr (assoc "Address" args)))))
1336 ;; Maybe we shall check the device class for being a proper
1337 ;; device, and call also SDP in order to find the obex service.
1338 (add-to-list 'tramp-bluez-devices (list alias address))))
1339
1340 (dbus-register-signal
1341 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1342 'tramp-bluez-device-found)
1343
1344 (defun tramp-bluez-parse-device-names (ignore)
1345 "Return a list of (nil host) tuples allowed to access."
1346 (mapcar
1347 (lambda (x) (list nil (car x)))
1348 (tramp-bluez-list-devices)))
1349
1350 ;; Add completion function for OBEX method.
1351 (when (member tramp-bluez-service (dbus-list-known-names :system))
1352 (tramp-set-completion-function
1353 "obex" '((tramp-bluez-parse-device-names ""))))
1354
1355 \f
1356 ;; D-Bus zeroconf functions.
1357
1358 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1359 "Return a list of (user host) tuples allowed to access."
1360 (mapcar
1361 (lambda (x)
1362 (list nil (zeroconf-service-host x)))
1363 (zeroconf-list-services "_workstation._tcp")))
1364
1365 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1366 "Return a list of (user host) tuples allowed to access."
1367 (mapcar
1368 (lambda (x)
1369 (let ((host (zeroconf-service-host x))
1370 (port (zeroconf-service-port x))
1371 (text (zeroconf-service-txt x))
1372 user)
1373 (when port
1374 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1375 ;; A user is marked in a TXT field like "u=guest".
1376 (while text
1377 (when (string-match "u=\\(.+\\)$" (car text))
1378 (setq user (match-string 1 (car text))))
1379 (setq text (cdr text)))
1380 (list user host)))
1381 (zeroconf-list-services "_webdav._tcp")))
1382
1383 ;; Add completion function for DAV and DAVS methods.
1384 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1385 (zeroconf-init tramp-gvfs-zeroconf-domain)
1386 (tramp-set-completion-function
1387 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1388 (tramp-set-completion-function
1389 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1390 (tramp-set-completion-function
1391 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1392
1393 \f
1394 ;; D-Bus SYNCE functions.
1395
1396 (defun tramp-synce-list-devices ()
1397 "Return all discovered synce devices as list.
1398 They are retrieved from the hal daemon."
1399 (let (tramp-synce-devices)
1400 (dolist (device
1401 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1402 :system tramp-hal-service tramp-hal-path-manager
1403 tramp-hal-interface-manager "GetAllDevices"))
1404 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1405 :system tramp-hal-service device tramp-hal-interface-device
1406 "PropertyExists" "sync.plugin")
1407 (add-to-list
1408 'tramp-synce-devices
1409 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1410 :system tramp-hal-service device tramp-hal-interface-device
1411 "GetPropertyString" "pda.pocketpc.name"))))
1412 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1413 tramp-synce-devices))
1414
1415 (defun tramp-synce-parse-device-names (ignore)
1416 "Return a list of (nil host) tuples allowed to access."
1417 (mapcar
1418 (lambda (x) (list nil x))
1419 (tramp-synce-list-devices)))
1420
1421 ;; Add completion function for SYNCE method.
1422 (tramp-set-completion-function
1423 "synce" '((tramp-synce-parse-device-names "")))
1424
1425 (add-hook 'tramp-unload-hook
1426 (lambda ()
1427 (unload-feature 'tramp-gvfs 'force)))
1428
1429 (provide 'tramp-gvfs)
1430
1431 ;;; TODO:
1432
1433 ;; * Host name completion via smb-server or smb-network.
1434 ;; * Check, how two shares of the same SMB server can be mounted in
1435 ;; parallel.
1436 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1437 ;; capability.
1438 ;; * Implement obex for other serial communication but bluetooth.
1439
1440 ;; arch-tag: f7f660ce-77f4-4132-9663-f5c25a47f7ed
1441 ;;; tramp-gvfs.el ends here