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