]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-gvfs.el
Explain, why `tramp-*-file-name-p' is a defsubst.
[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 ;; It must be a `defsubst' in order to push the whole code into
436 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
437 ;;;###tramp-autoload
438 (defsubst tramp-gvfs-file-name-p (filename)
439 "Check if it's a filename handled by the GVFS daemon."
440 (and (tramp-tramp-file-p filename)
441 (let ((method
442 (tramp-file-name-method (tramp-dissect-file-name filename))))
443 (and (stringp method) (member method tramp-gvfs-methods)))))
444
445 ;;;###tramp-autoload
446 (defun tramp-gvfs-file-name-handler (operation &rest args)
447 "Invoke the GVFS related OPERATION.
448 First arg specifies the OPERATION, second arg is a list of arguments to
449 pass to the OPERATION."
450 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
451 (if fn
452 (save-match-data (apply (cdr fn) args))
453 (tramp-run-real-handler operation args))))
454
455 ;; This might be moved to tramp.el. It shall be the first file name
456 ;; handler.
457 ;;;###tramp-autoload
458 (when (featurep 'dbusbind)
459 (add-to-list 'tramp-foreign-file-name-handler-alist
460 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
461
462 (defun tramp-gvfs-stringify-dbus-message (message)
463 "Convert a D-Bus message into readable UTF8 strings, used for traces."
464 (cond
465 ((and (consp message) (characterp (car message)))
466 (format "%S" (dbus-byte-array-to-string message)))
467 ((consp message)
468 (mapcar 'tramp-gvfs-stringify-dbus-message message))
469 ((stringp message)
470 (format "%S" message))
471 (t message)))
472
473 (defmacro with-tramp-dbus-call-method
474 (vec synchronous bus service path interface method &rest args)
475 "Apply a D-Bus call on bus BUS.
476
477 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
478 it is an asynchronous call, with `ignore' as callback function.
479
480 The other arguments have the same meaning as with `dbus-call-method'
481 or `dbus-call-method-asynchronously'. Additionally, the call
482 will be traced by Tramp with trace level 6."
483 `(let ((func (if ,synchronous
484 'dbus-call-method 'dbus-call-method-asynchronously))
485 (args (append (list ,bus ,service ,path ,interface ,method)
486 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
487 result)
488 (tramp-message ,vec 6 "%s %s" func args)
489 (setq result (apply func args))
490 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
491 result))
492
493 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
494 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
495 (tramp-compat-font-lock-add-keywords
496 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
497
498 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
499 "Apply a Tramp GVFS `handler'.
500 In case of an error, modify the error message by replacing
501 `filename' with its GVFS mounted name."
502 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
503 elt)
504 (condition-case err
505 (tramp-compat-funcall ,handler ,@args)
506 (error
507 (setq elt (cdr err))
508 (while elt
509 (when (and (stringp (car elt))
510 (string-match fuse-file-name (car elt)))
511 (setcar elt (replace-match ,filename t t (car elt))))
512 (setq elt (cdr elt)))
513 (signal (car err) (cdr err))))))
514
515 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
516 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
517 (tramp-compat-font-lock-add-keywords
518 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
519
520 (defvar tramp-gvfs-dbus-event-vector nil
521 "Current Tramp file name to be used, as vector.
522 It is needed when D-Bus signals or errors arrive, because there
523 is no information where to trace the message.")
524
525 (defun tramp-gvfs-dbus-event-error (event err)
526 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
527 (when tramp-gvfs-dbus-event-vector
528 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
529 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
530
531 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
532 (add-hook
533 (if (boundp 'dbus-event-error-functions)
534 'dbus-event-error-functions 'dbus-event-error-hooks)
535 'tramp-gvfs-dbus-event-error)
536
537 \f
538 ;; File name primitives.
539
540 (defun tramp-gvfs-handle-copy-file
541 (filename newname &optional ok-if-already-exists keep-date
542 preserve-uid-gid preserve-selinux-context)
543 "Like `copy-file' for Tramp files."
544 (with-parsed-tramp-file-name
545 (if (tramp-tramp-file-p filename) filename newname) nil
546 (with-tramp-progress-reporter
547 v 0 (format "Copying %s to %s" filename newname)
548 (condition-case err
549 (let ((args
550 (list
551 (if (tramp-gvfs-file-name-p filename)
552 (tramp-gvfs-fuse-file-name filename)
553 filename)
554 (if (tramp-gvfs-file-name-p newname)
555 (tramp-gvfs-fuse-file-name newname)
556 newname)
557 ok-if-already-exists keep-date preserve-uid-gid)))
558 (when preserve-selinux-context
559 (setq args (append args (list preserve-selinux-context))))
560 (apply 'copy-file args))
561
562 ;; Error case. Let's try it with the GVFS utilities.
563 (error
564 (tramp-message v 4 "`copy-file' failed, trying `gvfs-copy'")
565 (unless
566 (zerop
567 (let ((args
568 (append (if (or keep-date preserve-uid-gid)
569 (list "--preserve")
570 nil)
571 (list
572 (tramp-gvfs-url-file-name filename)
573 (tramp-gvfs-url-file-name newname)))))
574 (apply 'tramp-gvfs-send-command v "gvfs-copy" args)))
575 ;; Propagate the error.
576 (tramp-error v (car err) "%s" (cdr err)))))))
577
578 (when (file-remote-p newname)
579 (with-parsed-tramp-file-name newname nil
580 (tramp-flush-file-property v (file-name-directory localname))
581 (tramp-flush-file-property v localname))))
582
583 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive)
584 "Like `delete-directory' for Tramp files."
585 (tramp-compat-delete-directory
586 (tramp-gvfs-fuse-file-name directory) recursive))
587
588 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
589 "Like `delete-file' for Tramp files."
590 (tramp-compat-delete-file (tramp-gvfs-fuse-file-name filename) trash))
591
592 (defun tramp-gvfs-handle-directory-files
593 (directory &optional full match nosort)
594 "Like `directory-files' for Tramp files."
595 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
596 (mapcar
597 (lambda (x)
598 (if (string-match fuse-file-name x)
599 (replace-match directory t t x)
600 x))
601 (directory-files fuse-file-name full match nosort))))
602
603 (defun tramp-gvfs-handle-directory-files-and-attributes
604 (directory &optional full match nosort id-format)
605 "Like `directory-files-and-attributes' for Tramp files."
606 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
607 (mapcar
608 (lambda (x)
609 (when (string-match fuse-file-name (car x))
610 (setcar x (replace-match directory t t (car x))))
611 x)
612 (directory-files-and-attributes
613 fuse-file-name full match nosort id-format))))
614
615 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
616 "Like `expand-file-name' for Tramp files."
617 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
618 (setq dir (or dir default-directory "/"))
619 ;; Unless NAME is absolute, concat DIR and NAME.
620 (unless (file-name-absolute-p name)
621 (setq name (concat (file-name-as-directory dir) name)))
622 ;; If NAME is not a Tramp file, run the real handler.
623 (if (not (tramp-tramp-file-p name))
624 (tramp-run-real-handler 'expand-file-name (list name nil))
625 ;; Dissect NAME.
626 (with-parsed-tramp-file-name name nil
627 ;; If there is a default location, expand tilde.
628 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
629 (save-match-data
630 (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
631 (setq localname
632 (replace-match
633 (tramp-get-file-property v "/" "default-location" "~")
634 nil t localname 1)))
635 ;; Tilde expansion is not possible.
636 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
637 (tramp-error
638 v 'file-error
639 "Cannot expand tilde in file `%s'" name))
640 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
641 (setq localname (concat "/" localname)))
642 ;; We do not pass "/..".
643 (if (string-equal "smb" method)
644 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
645 (setq localname (replace-match "/" t t localname 1)))
646 (when (string-match "^/\\.\\./?" localname)
647 (setq localname (replace-match "/" t t localname))))
648 ;; There might be a double slash. Remove this.
649 (while (string-match "//" localname)
650 (setq localname (replace-match "/" t t localname)))
651 ;; No tilde characters in file name, do normal
652 ;; `expand-file-name' (this does "/./" and "/../").
653 (tramp-make-tramp-file-name
654 method user host
655 (tramp-run-real-handler
656 'expand-file-name (list localname))))))
657
658 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
659 "Like `file-attributes' for Tramp files."
660 (file-attributes (tramp-gvfs-fuse-file-name filename) id-format))
661
662 (defun tramp-gvfs-handle-file-directory-p (filename)
663 "Like `file-directory-p' for Tramp files."
664 (file-directory-p (tramp-gvfs-fuse-file-name filename)))
665
666 (defun tramp-gvfs-handle-file-executable-p (filename)
667 "Like `file-executable-p' for Tramp files."
668 (file-executable-p (tramp-gvfs-fuse-file-name filename)))
669
670 (defun tramp-gvfs-handle-file-exists-p (filename)
671 "Like `file-exists-p' for Tramp files."
672 (file-exists-p (tramp-gvfs-fuse-file-name filename)))
673
674 (defun tramp-gvfs-handle-file-local-copy (filename)
675 "Like `file-local-copy' for Tramp files."
676 (with-parsed-tramp-file-name filename nil
677 (let ((tmpfile (tramp-compat-make-temp-file filename)))
678 (unless (file-exists-p filename)
679 (tramp-error
680 v 'file-error
681 "Cannot make local copy of non-existing file `%s'" filename))
682 (copy-file filename tmpfile t t)
683 tmpfile)))
684
685 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
686 "Like `file-name-all-completions' for Tramp files."
687 (unless (save-match-data (string-match "/" filename))
688 (file-name-all-completions filename (tramp-gvfs-fuse-file-name directory))))
689
690 (defun tramp-gvfs-handle-file-readable-p (filename)
691 "Like `file-readable-p' for Tramp files."
692 (file-readable-p (tramp-gvfs-fuse-file-name filename)))
693
694 (defun tramp-gvfs-handle-file-selinux-context (filename)
695 "Like `file-selinux-context' for Tramp files."
696 (tramp-compat-funcall
697 'file-selinux-context (tramp-gvfs-fuse-file-name filename)))
698
699 (defun tramp-gvfs-handle-file-writable-p (filename)
700 "Like `file-writable-p' for Tramp files."
701 (file-writable-p (tramp-gvfs-fuse-file-name filename)))
702
703 (defun tramp-gvfs-handle-insert-directory
704 (filename switches &optional wildcard full-directory-p)
705 "Like `insert-directory' for Tramp files."
706 (insert-directory
707 (tramp-gvfs-fuse-file-name filename) switches wildcard full-directory-p))
708
709 (defun tramp-gvfs-handle-insert-file-contents
710 (filename &optional visit beg end replace)
711 "Like `insert-file-contents' for Tramp files."
712 (unwind-protect
713 (let ((fuse-file-name (tramp-gvfs-fuse-file-name filename))
714 (result
715 (insert-file-contents
716 (tramp-gvfs-fuse-file-name filename) visit beg end replace)))
717 (when (string-match fuse-file-name (car result))
718 (setcar result (replace-match filename t t (car result))))
719 result)
720 (setq buffer-file-name filename)))
721
722 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
723 "Like `make-directory' for Tramp files."
724 (with-parsed-tramp-file-name dir nil
725 (condition-case err
726 (with-tramp-gvfs-error-message dir 'make-directory
727 (tramp-gvfs-fuse-file-name dir) parents)
728
729 ;; Error case. Let's try it with the GVFS utilities.
730 (error
731 (tramp-message v 4 "`make-directory' failed, trying `gvfs-mkdir'")
732 (unless
733 (zerop
734 (tramp-gvfs-send-command
735 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir)))
736 ;; Propagate the error.
737 (tramp-error v (car err) "%s" (cdr err)))))))
738
739 (defun tramp-gvfs-handle-process-file
740 (program &optional infile destination display &rest args)
741 "Like `process-file' for Tramp files."
742 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
743 (apply 'call-process program infile destination display args)))
744
745 (defun tramp-gvfs-handle-rename-file
746 (filename newname &optional ok-if-already-exists)
747 "Like `rename-file' for Tramp files."
748 (with-parsed-tramp-file-name
749 (if (tramp-tramp-file-p filename) filename newname) nil
750 (with-tramp-progress-reporter
751 v 0 (format "Renaming %s to %s" filename newname)
752 (condition-case err
753 (rename-file
754 (if (tramp-gvfs-file-name-p filename)
755 (tramp-gvfs-fuse-file-name filename)
756 filename)
757 (if (tramp-gvfs-file-name-p newname)
758 (tramp-gvfs-fuse-file-name newname)
759 newname)
760 ok-if-already-exists)
761
762 ;; Error case. Let's try it with the GVFS utilities.
763 (error
764 (tramp-message v 4 "`rename-file' failed, trying `gvfs-move'")
765 (unless
766 (zerop
767 (tramp-gvfs-send-command
768 v "gvfs-move"
769 (tramp-gvfs-url-file-name filename)
770 (tramp-gvfs-url-file-name newname)))
771 ;; Propagate the error.
772 (tramp-error v (car err) "%s" (cdr err)))))))
773
774 (when (file-remote-p filename)
775 (with-parsed-tramp-file-name filename nil
776 (tramp-flush-file-property v (file-name-directory localname))
777 (tramp-flush-file-property v localname)))
778
779 (when (file-remote-p newname)
780 (with-parsed-tramp-file-name newname nil
781 (tramp-flush-file-property v (file-name-directory localname))
782 (tramp-flush-file-property v localname))))
783
784 (defun tramp-gvfs-handle-set-file-modes (filename mode)
785 "Like `set-file-modes' for Tramp files."
786 (with-tramp-gvfs-error-message filename 'set-file-modes
787 (tramp-gvfs-fuse-file-name filename) mode))
788
789 (defun tramp-gvfs-handle-set-file-selinux-context (filename context)
790 "Like `set-file-selinux-context' for Tramp files."
791 (with-tramp-gvfs-error-message filename 'set-file-selinux-context
792 (tramp-gvfs-fuse-file-name filename) context))
793
794 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
795 "Like `set-visited-file-modtime' for Tramp files."
796 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
797 (set-visited-file-modtime time-list)))
798
799 (defun tramp-gvfs-handle-shell-command
800 (command &optional output-buffer error-buffer)
801 "Like `shell-command' for Tramp files."
802 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
803 (shell-command command output-buffer error-buffer)))
804
805 (defun tramp-gvfs-handle-start-file-process (name buffer program &rest args)
806 "Like `start-file-process' for Tramp files."
807 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
808 (apply 'start-process name buffer program args)))
809
810 (defun tramp-gvfs-handle-verify-visited-file-modtime (buf)
811 "Like `verify-visited-file-modtime' for Tramp files."
812 (with-current-buffer buf
813 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
814 (verify-visited-file-modtime buf))))
815
816 (defun tramp-gvfs-handle-write-region
817 (start end filename &optional append visit lockname confirm)
818 "Like `write-region' for Tramp files."
819 (with-parsed-tramp-file-name filename nil
820 (condition-case err
821 (with-tramp-gvfs-error-message filename 'write-region
822 start end (tramp-gvfs-fuse-file-name filename)
823 append visit lockname confirm)
824
825 ;; Error case. Let's try rename.
826 (error
827 (let ((tmpfile (tramp-compat-make-temp-file filename)))
828 (tramp-message v 4 "`write-region' failed, trying `rename-file'")
829 (write-region start end tmpfile)
830 (condition-case nil
831 (rename-file tmpfile filename)
832 (error
833 (delete-file tmpfile)
834 (tramp-error v (car err) "%s" (cdr err)))))))
835
836 ;; Set file modification time.
837 (when (or (eq visit t) (stringp visit))
838 (set-visited-file-modtime (nth 5 (file-attributes filename))))
839
840 ;; The end.
841 (when (or (eq visit t) (null visit) (stringp visit))
842 (tramp-message v 0 "Wrote %s" filename))
843 (run-hooks 'tramp-handle-write-region-hook)))
844
845 \f
846 ;; File name conversions.
847
848 (defun tramp-gvfs-url-file-name (filename)
849 "Return FILENAME in URL syntax."
850 ;; "/" must NOT be hexlified.
851 (let ((url-unreserved-chars (append '(?/) url-unreserved-chars)))
852 (url-recreate-url
853 (if (tramp-tramp-file-p filename)
854 (with-parsed-tramp-file-name (file-truename filename) nil
855 (when (string-match tramp-user-with-domain-regexp user)
856 (setq user
857 (concat (match-string 2 user) ";" (match-string 2 user))))
858 (url-parse-make-urlobj
859 method user nil
860 (tramp-file-name-real-host v) (tramp-file-name-port v)
861 (url-hexify-string localname)))
862 (url-parse-make-urlobj
863 "file" nil nil nil nil (url-hexify-string (file-truename filename)))))))
864
865 (defun tramp-gvfs-object-path (filename)
866 "Create a D-Bus object path from FILENAME."
867 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
868
869 (defun tramp-gvfs-file-name (object-path)
870 "Retrieve file name from D-Bus OBJECT-PATH."
871 (dbus-unescape-from-identifier
872 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
873
874 (defun tramp-gvfs-fuse-file-name (filename)
875 "Return FUSE file name, which is directly accessible."
876 (with-parsed-tramp-file-name (expand-file-name filename) nil
877 (tramp-gvfs-maybe-open-connection v)
878 (let ((prefix (tramp-get-file-property v "/" "prefix" ""))
879 (fuse-mountpoint
880 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
881 (unless fuse-mountpoint
882 (tramp-error
883 v 'file-error "There is no FUSE mount point for `%s'" filename))
884 ;; We must hide the prefix, if any.
885 (when (string-match (concat "^" (regexp-quote prefix)) localname)
886 (setq localname (replace-match "" t t localname)))
887 (tramp-message
888 v 10 "remote file `%s' is local file `%s'"
889 filename (concat fuse-mountpoint localname))
890 (concat fuse-mountpoint localname))))
891
892 (defun tramp-bluez-address (device)
893 "Return bluetooth device address from a given bluetooth DEVICE name."
894 (when (stringp device)
895 (if (string-match tramp-ipv6-regexp device)
896 (match-string 0 device)
897 (cadr (assoc device (tramp-bluez-list-devices))))))
898
899 (defun tramp-bluez-device (address)
900 "Return bluetooth device name from a given bluetooth device ADDRESS.
901 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
902 (when (stringp address)
903 (while (string-match "[][]" address)
904 (setq address (replace-match "" t t address)))
905 (let (result)
906 (dolist (item (tramp-bluez-list-devices) result)
907 (when (string-match address (cadr item))
908 (setq result (car item)))))))
909
910 \f
911 ;; D-Bus GVFS functions.
912
913 (defun tramp-gvfs-handler-askpassword (message user domain flags)
914 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
915 (let* ((filename
916 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
917 (pw-prompt
918 (format
919 "%s for %s "
920 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
921 (capitalize (match-string 1 message))
922 "Password")
923 filename))
924 password)
925
926 (condition-case nil
927 (with-parsed-tramp-file-name filename l
928 (when (and (zerop (length user))
929 (not
930 (zerop (logand flags tramp-gvfs-password-need-username))))
931 (setq user (read-string "User name: ")))
932 (when (and (zerop (length domain))
933 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
934 (setq domain (read-string "Domain name: ")))
935
936 (tramp-message l 6 "%S %S %S %d" message user domain flags)
937 (setq tramp-current-method l-method
938 tramp-current-user user
939 tramp-current-host l-host
940 password (tramp-read-passwd
941 (tramp-get-connection-process l) pw-prompt))
942
943 ;; Return result.
944 (if (stringp password)
945 (list
946 t ;; password handled.
947 nil ;; no abort of D-Bus.
948 password
949 (tramp-file-name-real-user l)
950 domain
951 nil ;; not anonymous.
952 0) ;; no password save.
953 ;; No password provided.
954 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
955
956 ;; When QUIT is raised, we shall return this information to D-Bus.
957 (quit (list nil t "" "" "" nil 0)))))
958
959 (defun tramp-gvfs-handler-askquestion (message choices)
960 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
961 (save-window-excursion
962 (let ((enable-recursive-minibuffers t)
963 choice)
964
965 (condition-case nil
966 (with-parsed-tramp-file-name
967 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
968 (tramp-message v 6 "%S %S" message choices)
969
970 ;; In theory, there can be several choices. Until now,
971 ;; there is only the question whether to accept an unknown
972 ;; host signature.
973 (with-temp-buffer
974 ;; Preserve message for `progress-reporter'.
975 (tramp-compat-with-temp-message ""
976 (insert message)
977 (pop-to-buffer (current-buffer))
978 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
979 (tramp-message v 6 "%d" choice)))
980
981 ;; When the choice is "no", we set a dummy fuse-mountpoint
982 ;; in order to leave the timeout.
983 (unless (zerop choice)
984 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
985
986 (list
987 t ;; handled.
988 nil ;; no abort of D-Bus.
989 choice))
990
991 ;; When QUIT is raised, we shall return this information to D-Bus.
992 (quit (list nil t 0))))))
993
994 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
995 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
996 \"org.gtk.vfs.MountTracker.unmounted\" signals."
997 (ignore-errors
998 (let ((signal-name (dbus-event-member-name last-input-event))
999 (elt mount-info))
1000 ;; Jump over the first elements of the mount info. Since there
1001 ;; were changes in the entries, we cannot access dedicated
1002 ;; elements.
1003 (while (stringp (car elt)) (setq elt (cdr elt)))
1004 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1005 (mount-spec (caddr elt))
1006 (default-location (dbus-byte-array-to-string (cadddr elt)))
1007 (method (dbus-byte-array-to-string
1008 (cadr (assoc "type" (cadr mount-spec)))))
1009 (user (dbus-byte-array-to-string
1010 (cadr (assoc "user" (cadr mount-spec)))))
1011 (domain (dbus-byte-array-to-string
1012 (cadr (assoc "domain" (cadr mount-spec)))))
1013 (host (dbus-byte-array-to-string
1014 (cadr (or (assoc "host" (cadr mount-spec))
1015 (assoc "server" (cadr mount-spec))))))
1016 (port (dbus-byte-array-to-string
1017 (cadr (assoc "port" (cadr mount-spec)))))
1018 (ssl (dbus-byte-array-to-string
1019 (cadr (assoc "ssl" (cadr mount-spec)))))
1020 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1021 (dbus-byte-array-to-string
1022 (cadr (assoc "share" (cadr mount-spec)))))))
1023 (when (string-match "^smb" method)
1024 (setq method "smb"))
1025 (when (string-equal "obex" method)
1026 (setq host (tramp-bluez-device host)))
1027 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1028 (setq method "davs"))
1029 (unless (zerop (length domain))
1030 (setq user (concat user tramp-prefix-domain-format domain)))
1031 (unless (zerop (length port))
1032 (setq host (concat host tramp-prefix-port-format port)))
1033 (with-parsed-tramp-file-name
1034 (tramp-make-tramp-file-name method user host "") nil
1035 (tramp-message
1036 v 6 "%s %s"
1037 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1038 (tramp-set-file-property v "/" "list-mounts" 'undef)
1039 (if (string-equal signal-name "unmounted")
1040 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1041 ;; Set prefix, mountpoint and location.
1042 (unless (string-equal prefix "/")
1043 (tramp-set-file-property v "/" "prefix" prefix))
1044 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1045 (tramp-set-file-property
1046 v "/" "default-location" default-location)))))))
1047
1048 (dbus-register-signal
1049 :session nil tramp-gvfs-path-mounttracker
1050 tramp-gvfs-interface-mounttracker "mounted"
1051 'tramp-gvfs-handler-mounted-unmounted)
1052
1053 (dbus-register-signal
1054 :session nil tramp-gvfs-path-mounttracker
1055 tramp-gvfs-interface-mounttracker "unmounted"
1056 'tramp-gvfs-handler-mounted-unmounted)
1057
1058 (defun tramp-gvfs-connection-mounted-p (vec)
1059 "Check, whether the location is already mounted."
1060 (or
1061 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1062 (catch 'mounted
1063 (dolist
1064 (elt
1065 (with-tramp-file-property vec "/" "list-mounts"
1066 (with-tramp-dbus-call-method vec t
1067 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1068 tramp-gvfs-interface-mounttracker "listMounts"))
1069 nil)
1070 ;; Jump over the first elements of the mount info. Since there
1071 ;; were changes in the entries, we cannot access dedicated
1072 ;; elements.
1073 (while (stringp (car elt)) (setq elt (cdr elt)))
1074 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1075 (mount-spec (caddr elt))
1076 (default-location (dbus-byte-array-to-string (cadddr elt)))
1077 (method (dbus-byte-array-to-string
1078 (cadr (assoc "type" (cadr mount-spec)))))
1079 (user (dbus-byte-array-to-string
1080 (cadr (assoc "user" (cadr mount-spec)))))
1081 (domain (dbus-byte-array-to-string
1082 (cadr (assoc "domain" (cadr mount-spec)))))
1083 (host (dbus-byte-array-to-string
1084 (cadr (or (assoc "host" (cadr mount-spec))
1085 (assoc "server" (cadr mount-spec))))))
1086 (port (dbus-byte-array-to-string
1087 (cadr (assoc "port" (cadr mount-spec)))))
1088 (ssl (dbus-byte-array-to-string
1089 (cadr (assoc "ssl" (cadr mount-spec)))))
1090 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1091 (dbus-byte-array-to-string
1092 (cadr (assoc "share" (cadr mount-spec)))))))
1093 (when (string-match "^smb" method)
1094 (setq method "smb"))
1095 (when (string-equal "obex" method)
1096 (setq host (tramp-bluez-device host)))
1097 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1098 (setq method "davs"))
1099 (when (and (string-equal "synce" method) (zerop (length user)))
1100 (setq user (or (tramp-file-name-user vec) "")))
1101 (unless (zerop (length domain))
1102 (setq user (concat user tramp-prefix-domain-format domain)))
1103 (unless (zerop (length port))
1104 (setq host (concat host tramp-prefix-port-format port)))
1105 (when (and
1106 (string-equal method (tramp-file-name-method vec))
1107 (string-equal user (or (tramp-file-name-user vec) ""))
1108 (string-equal host (tramp-file-name-host vec))
1109 (string-match (concat "^" (regexp-quote prefix))
1110 (tramp-file-name-localname vec)))
1111 ;; Set prefix, mountpoint and location.
1112 (unless (string-equal prefix "/")
1113 (tramp-set-file-property vec "/" "prefix" prefix))
1114 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1115 (tramp-set-file-property vec "/" "default-location" default-location)
1116 (throw 'mounted t)))))))
1117
1118 (defun tramp-gvfs-mount-spec (vec)
1119 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1120 (let* ((method (tramp-file-name-method vec))
1121 (user (tramp-file-name-real-user vec))
1122 (domain (tramp-file-name-domain vec))
1123 (host (tramp-file-name-real-host vec))
1124 (port (tramp-file-name-port vec))
1125 (localname (tramp-file-name-localname vec))
1126 (ssl (if (string-match "^davs" method) "true" "false"))
1127 (mount-spec '(:array))
1128 (mount-pref "/"))
1129
1130 (setq
1131 mount-spec
1132 (append
1133 mount-spec
1134 (cond
1135 ((string-equal "smb" method)
1136 (string-match "^/?\\([^/]+\\)" localname)
1137 `((:struct "type" ,(dbus-string-to-byte-array "smb-share"))
1138 (:struct "server" ,(dbus-string-to-byte-array host))
1139 (:struct "share" ,(dbus-string-to-byte-array
1140 (match-string 1 localname)))))
1141 ((string-equal "obex" method)
1142 `((:struct "type" ,(dbus-string-to-byte-array method))
1143 (:struct "host" ,(dbus-string-to-byte-array
1144 (concat "[" (tramp-bluez-address host) "]")))))
1145 ((string-match "^dav" method)
1146 `((:struct "type" ,(dbus-string-to-byte-array "dav"))
1147 (:struct "host" ,(dbus-string-to-byte-array host))
1148 (:struct "ssl" ,(dbus-string-to-byte-array ssl))))
1149 (t
1150 `((:struct "type" ,(dbus-string-to-byte-array method))
1151 (:struct "host" ,(dbus-string-to-byte-array host)))))))
1152
1153 (when user
1154 (add-to-list
1155 'mount-spec
1156 `(:struct "user" ,(dbus-string-to-byte-array user))
1157 'append))
1158
1159 (when domain
1160 (add-to-list
1161 'mount-spec
1162 `(:struct "domain" ,(dbus-string-to-byte-array domain))
1163 'append))
1164
1165 (when port
1166 (add-to-list
1167 'mount-spec
1168 `(:struct "port" ,(dbus-string-to-byte-array (number-to-string port)))
1169 'append))
1170
1171 (when (and (string-match "^dav" method)
1172 (string-match "^/?[^/]+" localname))
1173 (setq mount-pref (match-string 0 localname)))
1174
1175 ;; Return.
1176 `(:struct ,(dbus-string-to-byte-array mount-pref) ,mount-spec)))
1177
1178 \f
1179 ;; Connection functions
1180
1181 (defun tramp-gvfs-maybe-open-connection (vec)
1182 "Maybe open a connection VEC.
1183 Does not do anything if a connection is already open, but re-opens the
1184 connection if a previous connection has died for some reason."
1185
1186 ;; We set the file name, in case there are incoming D-Bus signals or
1187 ;; D-Bus errors.
1188 (setq tramp-gvfs-dbus-event-vector vec)
1189
1190 ;; For password handling, we need a process bound to the connection
1191 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1192 ;; better solution?
1193 (unless (get-buffer-process (tramp-get-buffer vec))
1194 (let ((p (make-network-process
1195 :name (tramp-buffer-name vec)
1196 :buffer (tramp-get-buffer vec)
1197 :server t :host 'local :service t)))
1198 (tramp-compat-set-process-query-on-exit-flag p nil)))
1199
1200 (unless (tramp-gvfs-connection-mounted-p vec)
1201 (let* ((method (tramp-file-name-method vec))
1202 (user (tramp-file-name-user vec))
1203 (host (tramp-file-name-host vec))
1204 (object-path
1205 (tramp-gvfs-object-path
1206 (tramp-make-tramp-file-name method user host ""))))
1207
1208 (with-tramp-progress-reporter
1209 vec 3
1210 (if (zerop (length user))
1211 (format "Opening connection for %s using %s" host method)
1212 (format "Opening connection for %s@%s using %s" user host method))
1213
1214 ;; Enable auth-source and password-cache.
1215 (tramp-set-connection-property vec "first-password-request" t)
1216
1217 ;; There will be a callback of "askPassword" when a password is
1218 ;; needed.
1219 (dbus-register-method
1220 :session dbus-service-emacs object-path
1221 tramp-gvfs-interface-mountoperation "askPassword"
1222 'tramp-gvfs-handler-askpassword)
1223
1224 ;; There could be a callback of "askQuestion" when adding fingerprint.
1225 (dbus-register-method
1226 :session dbus-service-emacs object-path
1227 tramp-gvfs-interface-mountoperation "askQuestion"
1228 'tramp-gvfs-handler-askquestion)
1229
1230 ;; The call must be asynchronously, because of the "askPassword"
1231 ;; or "askQuestion"callbacks.
1232 (with-tramp-dbus-call-method vec nil
1233 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1234 tramp-gvfs-interface-mounttracker "mountLocation"
1235 (tramp-gvfs-mount-spec vec) (dbus-get-unique-name :session)
1236 :object-path object-path)
1237
1238 ;; We must wait, until the mount is applied. This will be
1239 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1240 ;; file property.
1241 (with-timeout
1242 (60
1243 (if (zerop (length (tramp-file-name-user vec)))
1244 (tramp-error
1245 vec 'file-error
1246 "Timeout reached mounting %s using %s" host method)
1247 (tramp-error
1248 vec 'file-error
1249 "Timeout reached mounting %s@%s using %s" user host method)))
1250 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1251 (read-event nil nil 0.1)))
1252
1253 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1254 ;; is marked with the fuse-mountpoint "/". We shall react.
1255 (when (string-equal
1256 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1257 (tramp-error vec 'file-error "FUSE mount denied"))
1258
1259 ;; We set the connection property "started" in order to put the
1260 ;; remote location into the cache, which is helpful for further
1261 ;; completion.
1262 (tramp-set-connection-property vec "started" t)))))
1263
1264 (defun tramp-gvfs-send-command (vec command &rest args)
1265 "Send the COMMAND with its ARGS to connection VEC.
1266 COMMAND is usually a command from the gvfs-* utilities.
1267 `call-process' is applied, and its return code is returned."
1268 (let (result)
1269 (with-current-buffer (tramp-get-buffer vec)
1270 (erase-buffer)
1271 (tramp-message vec 6 "%s %s" command (mapconcat 'identity args " "))
1272 (setq result (apply 'tramp-compat-call-process command nil t nil args))
1273 (tramp-message vec 6 "%s" (buffer-string))
1274 result)))
1275
1276 \f
1277 ;; D-Bus BLUEZ functions.
1278
1279 (defun tramp-bluez-list-devices ()
1280 "Return all discovered bluetooth devices as list.
1281 Every entry is a list (NAME ADDRESS).
1282
1283 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1284 discovery happened more time before indicated there, a rescan will be
1285 started, which lasts some ten seconds. Otherwise, cached results will
1286 be used."
1287 ;; Reset the scanned devices list if time has passed.
1288 (and (integerp tramp-bluez-discover-devices-timeout)
1289 (integerp tramp-bluez-discovery)
1290 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1291 tramp-bluez-discover-devices-timeout)
1292 (setq tramp-bluez-devices nil))
1293
1294 ;; Rescan if needed.
1295 (unless tramp-bluez-devices
1296 (let ((object-path
1297 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1298 :system tramp-bluez-service "/"
1299 tramp-bluez-interface-manager "DefaultAdapter")))
1300 (setq tramp-bluez-devices nil
1301 tramp-bluez-discovery t)
1302 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1303 :system tramp-bluez-service object-path
1304 tramp-bluez-interface-adapter "StartDiscovery")
1305 (while tramp-bluez-discovery
1306 (read-event nil nil 0.1))))
1307 (setq tramp-bluez-discovery (current-time))
1308 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1309 tramp-bluez-devices)
1310
1311 (defun tramp-bluez-property-changed (property value)
1312 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1313 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1314 (cond
1315 ((string-equal property "Discovering")
1316 (unless (car value)
1317 ;; "Discovering" FALSE means discovery run has been completed.
1318 ;; We stop it, because we don't need another run.
1319 (setq tramp-bluez-discovery nil)
1320 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1321 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1322 tramp-bluez-interface-adapter "StopDiscovery")))))
1323
1324 (dbus-register-signal
1325 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1326 'tramp-bluez-property-changed)
1327
1328 (defun tramp-bluez-device-found (device args)
1329 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1330 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1331 (let ((alias (car (cadr (assoc "Alias" args))))
1332 (address (car (cadr (assoc "Address" args)))))
1333 ;; Maybe we shall check the device class for being a proper
1334 ;; device, and call also SDP in order to find the obex service.
1335 (add-to-list 'tramp-bluez-devices (list alias address))))
1336
1337 (dbus-register-signal
1338 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1339 'tramp-bluez-device-found)
1340
1341 (defun tramp-bluez-parse-device-names (ignore)
1342 "Return a list of (nil host) tuples allowed to access."
1343 (mapcar
1344 (lambda (x) (list nil (car x)))
1345 (tramp-bluez-list-devices)))
1346
1347 ;; Add completion function for OBEX method.
1348 (when (member tramp-bluez-service (dbus-list-known-names :system))
1349 (tramp-set-completion-function
1350 "obex" '((tramp-bluez-parse-device-names ""))))
1351
1352 \f
1353 ;; D-Bus zeroconf functions.
1354
1355 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1356 "Return a list of (user host) tuples allowed to access."
1357 (mapcar
1358 (lambda (x)
1359 (list nil (zeroconf-service-host x)))
1360 (zeroconf-list-services "_workstation._tcp")))
1361
1362 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1363 "Return a list of (user host) tuples allowed to access."
1364 (mapcar
1365 (lambda (x)
1366 (let ((host (zeroconf-service-host x))
1367 (port (zeroconf-service-port x))
1368 (text (zeroconf-service-txt x))
1369 user)
1370 (when port
1371 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1372 ;; A user is marked in a TXT field like "u=guest".
1373 (while text
1374 (when (string-match "u=\\(.+\\)$" (car text))
1375 (setq user (match-string 1 (car text))))
1376 (setq text (cdr text)))
1377 (list user host)))
1378 (zeroconf-list-services "_webdav._tcp")))
1379
1380 ;; Add completion function for DAV and DAVS methods.
1381 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1382 (zeroconf-init tramp-gvfs-zeroconf-domain)
1383 (tramp-set-completion-function
1384 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1385 (tramp-set-completion-function
1386 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1387 (tramp-set-completion-function
1388 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1389
1390 \f
1391 ;; D-Bus SYNCE functions.
1392
1393 (defun tramp-synce-list-devices ()
1394 "Return all discovered synce devices as list.
1395 They are retrieved from the hal daemon."
1396 (let (tramp-synce-devices)
1397 (dolist (device
1398 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1399 :system tramp-hal-service tramp-hal-path-manager
1400 tramp-hal-interface-manager "GetAllDevices"))
1401 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1402 :system tramp-hal-service device tramp-hal-interface-device
1403 "PropertyExists" "sync.plugin")
1404 (add-to-list
1405 'tramp-synce-devices
1406 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1407 :system tramp-hal-service device tramp-hal-interface-device
1408 "GetPropertyString" "pda.pocketpc.name"))))
1409 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1410 tramp-synce-devices))
1411
1412 (defun tramp-synce-parse-device-names (ignore)
1413 "Return a list of (nil host) tuples allowed to access."
1414 (mapcar
1415 (lambda (x) (list nil x))
1416 (tramp-synce-list-devices)))
1417
1418 ;; Add completion function for SYNCE method.
1419 (tramp-set-completion-function
1420 "synce" '((tramp-synce-parse-device-names "")))
1421
1422 (add-hook 'tramp-unload-hook
1423 (lambda ()
1424 (unload-feature 'tramp-gvfs 'force)))
1425
1426 (provide 'tramp-gvfs)
1427
1428 ;;; TODO:
1429
1430 ;; * Host name completion via smb-server or smb-network.
1431 ;; * Check how two shares of the same SMB server can be mounted in
1432 ;; parallel.
1433 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1434 ;; capability.
1435 ;; * Implement obex for other serial communication but bluetooth.
1436
1437 ;;; tramp-gvfs.el ends here