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