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