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