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