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