]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-gvfs.el
* net/tramp-gvfs.el (top):
[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 (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 (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 ;; With GVFS 1.14 (Ubuntu 12.10, Gnome 3.6) the interfaces have been
37 ;; changed, again. So we must introspect the D-Bus interfaces.
38
39 ;; All actions to mount a remote location, and to retrieve mount
40 ;; information, are performed by D-Bus messages. File operations
41 ;; themselves are performed via the mounted filesystem in ~/.gvfs.
42 ;; Consequently, GNU Emacs 23.1 with enabled D-Bus bindings is a
43 ;; precondition.
44
45 ;; The GVFS D-Bus interface is said to be unstable. There were even
46 ;; no introspection data before GVFS 1.14. The interface, as
47 ;; discovered during development time, is given in respective
48 ;; comments.
49
50 ;; The customer option `tramp-gvfs-methods' contains the list of
51 ;; supported connection methods. Per default, these are "dav",
52 ;; "davs", "obex" and "synce". Note that with "obex" it might be
53 ;; necessary to pair with the other bluetooth device, if it hasn't
54 ;; been done already. There might be also some few seconds delay in
55 ;; discovering available bluetooth devices.
56
57 ;; Other possible connection methods are "ftp", "sftp" and "smb".
58 ;; When one of these methods is added to the list, the remote access
59 ;; for that method is performed via GVFS instead of the native Tramp
60 ;; implementation.
61
62 ;; GVFS offers even more connection methods. The complete list of
63 ;; connection methods of the actual GVFS implementation can be
64 ;; retrieved by:
65 ;;
66 ;; (message
67 ;; "%s"
68 ;; (mapcar
69 ;; 'car
70 ;; (dbus-call-method
71 ;; :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
72 ;; tramp-gvfs-interface-mounttracker "listMountableInfo")))
73
74 ;; Note that all other connection methods are not tested, beside the
75 ;; ones offered for customization in `tramp-gvfs-methods'. If you
76 ;; request an additional connection method to be supported, please
77 ;; drop me a note.
78
79 ;; For hostname completion, information is retrieved either from the
80 ;; bluez daemon (for the "obex" method), the hal daemon (for the
81 ;; "synce" method), or from the zeroconf daemon (for the "dav",
82 ;; "davs", and "sftp" methods). The zeroconf daemon is pre-configured
83 ;; to discover services in the "local" domain. If another domain
84 ;; shall be used for discovering services, the customer option
85 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
86
87 ;; Restrictions:
88
89 ;; * The current GVFS implementation does not allow to write on the
90 ;; remote bluetooth device via OBEX.
91 ;;
92 ;; * Two shares of the same SMB server cannot be mounted in parallel.
93
94 ;;; Code:
95
96 ;; D-Bus support in the Emacs core can be disabled with configuration
97 ;; option "--without-dbus". Declare used subroutines and variables.
98 (declare-function dbus-get-unique-name "dbusbind.c")
99
100 ;; Pacify byte-compiler
101 (eval-when-compile
102 (require 'cl)
103 (require 'custom))
104
105 (require 'tramp)
106
107 (require 'dbus)
108 (require 'url-parse)
109 (require 'url-util)
110 (require 'zeroconf)
111
112 ;;;###tramp-autoload
113 (defcustom tramp-gvfs-methods '("dav" "davs" "obex" "synce")
114 "List of methods for remote files, accessed with GVFS."
115 :group 'tramp
116 :version "23.2"
117 :type '(repeat (choice (const "dav")
118 (const "davs")
119 (const "ftp")
120 (const "obex")
121 (const "sftp")
122 (const "smb")
123 (const "synce"))))
124
125 ;; Add a default for `tramp-default-user-alist'. Rule: For the SYNCE
126 ;; method, no user is chosen.
127 ;;;###tramp-autoload
128 (add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
129
130 (defcustom tramp-gvfs-zeroconf-domain "local"
131 "Zeroconf domain to be used for discovering services, like host names."
132 :group 'tramp
133 :version "23.2"
134 :type 'string)
135
136 ;; Add the methods to `tramp-methods', in order to allow minibuffer
137 ;; completion.
138 ;;;###tramp-autoload
139 (when (featurep 'dbusbind)
140 (dolist (elt tramp-gvfs-methods)
141 (unless (assoc elt tramp-methods)
142 (add-to-list 'tramp-methods (cons elt nil)))))
143
144 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
145 "The preceding object path for own objects.")
146
147 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
148 "The well known name of the GVFS daemon.")
149
150 ;; Check that GVFS is available. D-Bus integration is available since
151 ;; Emacs 23 on some system types. We don't call `dbus-ping', because
152 ;; this would load dbus.el.
153 (unless (ignore-errors
154 (and (tramp-compat-funcall 'dbus-get-unique-name :session)
155 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
156 (tramp-compat-process-running-p "gvfsd-fuse"))))
157 (tramp-compat-user-error "Package `tramp-gvfs' not supported"))
158
159 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
160 "The object path of the GVFS daemon.")
161
162 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
163 "The mount tracking interface in the GVFS daemon.")
164
165 ;; Introspection data exist since GVFS 1.14. If there are no such
166 ;; data, we expect an earlier interface.
167 (defconst tramp-gvfs-methods-mounttracker
168 (dbus-introspect-get-method-names
169 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
170 tramp-gvfs-interface-mounttracker)
171 "The list of supported methods of the mount tracking interface.")
172
173 (defconst tramp-gvfs-listmounts
174 (if (member "ListMounts" tramp-gvfs-methods-mounttracker)
175 "ListMounts"
176 "listMounts")
177 "The name of the \"listMounts\" method.
178 It has been changed in GVFS 1.14.")
179
180 (defconst tramp-gvfs-mountlocation
181 (if (member "MountLocation" tramp-gvfs-methods-mounttracker)
182 "MountLocation"
183 "mountLocation")
184 "The name of the \"mountLocation\" method.
185 It has been changed in GVFS 1.14.")
186
187 (defconst tramp-gvfs-mountlocation-signature
188 (dbus-introspect-get-signature
189 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
190 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation)
191 "The D-Bus signature of the \"mountLocation\" method.
192 It has been changed in GVFS 1.14.")
193
194 ;; <interface name='org.gtk.vfs.MountTracker'>
195 ;; <method name='listMounts'>
196 ;; <arg name='mount_info_list'
197 ;; type='a{sosssssbay{aya{say}}ay}'
198 ;; direction='out'/>
199 ;; </method>
200 ;; <method name='mountLocation'>
201 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
202 ;; <arg name='dbus_id' type='s' direction='in'/>
203 ;; <arg name='object_path' type='o' direction='in'/>
204 ;; </method>
205 ;; <signal name='mounted'>
206 ;; <arg name='mount_info'
207 ;; type='{sosssssbay{aya{say}}ay}'/>
208 ;; </signal>
209 ;; <signal name='unmounted'>
210 ;; <arg name='mount_info'
211 ;; type='{sosssssbay{aya{say}}ay}'/>
212 ;; </signal>
213 ;; </interface>
214 ;;
215 ;; STRUCT mount_info
216 ;; STRING dbus_id
217 ;; OBJECT_PATH object_path
218 ;; STRING display_name
219 ;; STRING stable_name
220 ;; STRING x_content_types Since GVFS 1.0 only !!!
221 ;; STRING icon
222 ;; STRING preferred_filename_encoding
223 ;; BOOLEAN user_visible
224 ;; ARRAY BYTE fuse_mountpoint
225 ;; STRUCT mount_spec
226 ;; ARRAY BYTE mount_prefix
227 ;; ARRAY
228 ;; STRUCT mount_spec_item
229 ;; STRING key (server, share, type, user, host, port)
230 ;; ARRAY BYTE value
231 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
232
233 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
234 "Used by the dbus-proxying implementation of GMountOperation.")
235
236 ;; <interface name='org.gtk.vfs.MountOperation'>
237 ;; <method name='askPassword'>
238 ;; <arg name='message' type='s' direction='in'/>
239 ;; <arg name='default_user' type='s' direction='in'/>
240 ;; <arg name='default_domain' type='s' direction='in'/>
241 ;; <arg name='flags' type='u' direction='in'/>
242 ;; <arg name='handled' type='b' direction='out'/>
243 ;; <arg name='aborted' type='b' direction='out'/>
244 ;; <arg name='password' type='s' direction='out'/>
245 ;; <arg name='username' type='s' direction='out'/>
246 ;; <arg name='domain' type='s' direction='out'/>
247 ;; <arg name='anonymous' type='b' direction='out'/>
248 ;; <arg name='password_save' type='u' direction='out'/>
249 ;; </method>
250 ;; <method name='askQuestion'>
251 ;; <arg name='message' type='s' direction='in'/>
252 ;; <arg name='choices' type='as' direction='in'/>
253 ;; <arg name='handled' type='b' direction='out'/>
254 ;; <arg name='aborted' type='b' direction='out'/>
255 ;; <arg name='choice' type='u' direction='out'/>
256 ;; </method>
257 ;; </interface>
258
259 ;; The following flags are used in "askPassword". They are defined in
260 ;; /usr/include/glib-2.0/gio/gioenums.h.
261
262 (defconst tramp-gvfs-password-need-password 1
263 "Operation requires a password.")
264
265 (defconst tramp-gvfs-password-need-username 2
266 "Operation requires a username.")
267
268 (defconst tramp-gvfs-password-need-domain 4
269 "Operation requires a domain.")
270
271 (defconst tramp-gvfs-password-saving-supported 8
272 "Operation supports saving settings.")
273
274 (defconst tramp-gvfs-password-anonymous-supported 16
275 "Operation supports anonymous users.")
276
277 (defconst tramp-bluez-service "org.bluez"
278 "The well known name of the BLUEZ service.")
279
280 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
281 "The manager interface of the BLUEZ daemon.")
282
283 ;; <interface name='org.bluez.Manager'>
284 ;; <method name='DefaultAdapter'>
285 ;; <arg type='o' direction='out'/>
286 ;; </method>
287 ;; <method name='FindAdapter'>
288 ;; <arg type='s' direction='in'/>
289 ;; <arg type='o' direction='out'/>
290 ;; </method>
291 ;; <method name='ListAdapters'>
292 ;; <arg type='ao' direction='out'/>
293 ;; </method>
294 ;; <signal name='AdapterAdded'>
295 ;; <arg type='o'/>
296 ;; </signal>
297 ;; <signal name='AdapterRemoved'>
298 ;; <arg type='o'/>
299 ;; </signal>
300 ;; <signal name='DefaultAdapterChanged'>
301 ;; <arg type='o'/>
302 ;; </signal>
303 ;; </interface>
304
305 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
306 "The adapter interface of the BLUEZ daemon.")
307
308 ;; <interface name='org.bluez.Adapter'>
309 ;; <method name='GetProperties'>
310 ;; <arg type='a{sv}' direction='out'/>
311 ;; </method>
312 ;; <method name='SetProperty'>
313 ;; <arg type='s' direction='in'/>
314 ;; <arg type='v' direction='in'/>
315 ;; </method>
316 ;; <method name='RequestMode'>
317 ;; <arg type='s' direction='in'/>
318 ;; </method>
319 ;; <method name='ReleaseMode'/>
320 ;; <method name='RequestSession'/>
321 ;; <method name='ReleaseSession'/>
322 ;; <method name='StartDiscovery'/>
323 ;; <method name='StopDiscovery'/>
324 ;; <method name='ListDevices'>
325 ;; <arg type='ao' direction='out'/>
326 ;; </method>
327 ;; <method name='CreateDevice'>
328 ;; <arg type='s' direction='in'/>
329 ;; <arg type='o' direction='out'/>
330 ;; </method>
331 ;; <method name='CreatePairedDevice'>
332 ;; <arg type='s' direction='in'/>
333 ;; <arg type='o' direction='in'/>
334 ;; <arg type='s' direction='in'/>
335 ;; <arg type='o' direction='out'/>
336 ;; </method>
337 ;; <method name='CancelDeviceCreation'>
338 ;; <arg type='s' direction='in'/>
339 ;; </method>
340 ;; <method name='RemoveDevice'>
341 ;; <arg type='o' direction='in'/>
342 ;; </method>
343 ;; <method name='FindDevice'>
344 ;; <arg type='s' direction='in'/>
345 ;; <arg type='o' direction='out'/>
346 ;; </method>
347 ;; <method name='RegisterAgent'>
348 ;; <arg type='o' direction='in'/>
349 ;; <arg type='s' direction='in'/>
350 ;; </method>
351 ;; <method name='UnregisterAgent'>
352 ;; <arg type='o' direction='in'/>
353 ;; </method>
354 ;; <signal name='DeviceCreated'>
355 ;; <arg type='o'/>
356 ;; </signal>
357 ;; <signal name='DeviceRemoved'>
358 ;; <arg type='o'/>
359 ;; </signal>
360 ;; <signal name='DeviceFound'>
361 ;; <arg type='s'/>
362 ;; <arg type='a{sv}'/>
363 ;; </signal>
364 ;; <signal name='PropertyChanged'>
365 ;; <arg type='s'/>
366 ;; <arg type='v'/>
367 ;; </signal>
368 ;; <signal name='DeviceDisappeared'>
369 ;; <arg type='s'/>
370 ;; </signal>
371 ;; </interface>
372
373 (defcustom tramp-bluez-discover-devices-timeout 60
374 "Defines seconds since last bluetooth device discovery before rescanning.
375 A value of 0 would require an immediate discovery during hostname
376 completion, nil means to use always cached values for discovered
377 devices."
378 :group 'tramp
379 :version "23.2"
380 :type '(choice (const nil) integer))
381
382 (defvar tramp-bluez-discovery nil
383 "Indicator for a running bluetooth device discovery.
384 It keeps the timestamp of last discovery.")
385
386 (defvar tramp-bluez-devices nil
387 "Alist of detected bluetooth devices.
388 Every entry is a list (NAME ADDRESS).")
389
390 (defconst tramp-hal-service "org.freedesktop.Hal"
391 "The well known name of the HAL service.")
392
393 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
394 "The object path of the HAL daemon manager.")
395
396 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
397 "The manager interface of the HAL daemon.")
398
399 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
400 "The device interface of the HAL daemon.")
401
402 \f
403 ;; New handlers should be added here.
404 (defconst tramp-gvfs-file-name-handler-alist
405 '(
406 (access-file . ignore)
407 (add-name-to-file . tramp-gvfs-handle-copy-file)
408 ;; `byte-compiler-base-file-name' performed by default handler.
409 (copy-file . tramp-gvfs-handle-copy-file)
410 (delete-directory . tramp-gvfs-handle-delete-directory)
411 (delete-file . tramp-gvfs-handle-delete-file)
412 ;; `diff-latest-backup-file' performed by default handler.
413 (directory-file-name . tramp-handle-directory-file-name)
414 (directory-files . tramp-handle-directory-files)
415 (directory-files-and-attributes
416 . tramp-handle-directory-files-and-attributes)
417 (dired-call-process . ignore)
418 (dired-compress-file . ignore)
419 (dired-uncache . tramp-handle-dired-uncache)
420 ;; `executable-find' is not official yet. performed by default handler.
421 (expand-file-name . tramp-gvfs-handle-expand-file-name)
422 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
423 (file-acl . ignore)
424 (file-attributes . tramp-gvfs-handle-file-attributes)
425 (file-directory-p . tramp-gvfs-handle-file-directory-p)
426 (file-executable-p . tramp-gvfs-handle-file-executable-p)
427 (file-exists-p . tramp-handle-file-exists-p)
428 (file-local-copy . tramp-gvfs-handle-file-local-copy)
429 (file-modes . tramp-handle-file-modes)
430 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
431 (file-name-as-directory . tramp-handle-file-name-as-directory)
432 (file-name-completion . tramp-handle-file-name-completion)
433 (file-name-directory . tramp-handle-file-name-directory)
434 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
435 ;; `file-name-sans-versions' performed by default handler.
436 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
437 (file-ownership-preserved-p . ignore)
438 (file-readable-p . tramp-gvfs-handle-file-readable-p)
439 (file-regular-p . tramp-handle-file-regular-p)
440 (file-remote-p . tramp-handle-file-remote-p)
441 (file-selinux-context . ignore)
442 (file-symlink-p . tramp-handle-file-symlink-p)
443 ;; `file-truename' performed by default handler.
444 (file-writable-p . tramp-gvfs-handle-file-writable-p)
445 (find-backup-file-name . tramp-handle-find-backup-file-name)
446 ;; `find-file-noselect' performed by default handler.
447 ;; `get-file-buffer' performed by default handler.
448 (insert-directory . tramp-gvfs-handle-insert-directory)
449 (insert-file-contents . tramp-gvfs-handle-insert-file-contents)
450 (load . tramp-handle-load)
451 (make-directory . tramp-gvfs-handle-make-directory)
452 (make-directory-internal . ignore)
453 (make-symbolic-link . ignore)
454 (process-file . ignore)
455 (rename-file . tramp-gvfs-handle-rename-file)
456 (set-file-acl . ignore)
457 (set-file-modes . ignore)
458 (set-file-selinux-context . ignore)
459 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime)
460 (shell-command . ignore)
461 (start-file-process . ignore)
462 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
463 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
464 (vc-registered . ignore)
465 ;; `verify-visited-file-modtime' performed by default handler.
466 (write-region . tramp-gvfs-handle-write-region)
467 )
468 "Alist of handler functions for Tramp GVFS method.
469 Operations not mentioned here will be handled by the default Emacs primitives.")
470
471 ;; It must be a `defsubst' in order to push the whole code into
472 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
473 ;;;###tramp-autoload
474 (defsubst tramp-gvfs-file-name-p (filename)
475 "Check if it's a filename handled by the GVFS daemon."
476 (and (tramp-tramp-file-p filename)
477 (let ((method
478 (tramp-file-name-method (tramp-dissect-file-name filename))))
479 (and (stringp method) (member method tramp-gvfs-methods)))))
480
481 ;;;###tramp-autoload
482 (defun tramp-gvfs-file-name-handler (operation &rest args)
483 "Invoke the GVFS related OPERATION.
484 First arg specifies the OPERATION, second arg is a list of arguments to
485 pass to the OPERATION."
486 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
487 (if fn
488 (save-match-data (apply (cdr fn) args))
489 (tramp-run-real-handler operation args))))
490
491 ;; This might be moved to tramp.el. It shall be the first file name
492 ;; handler.
493 ;;;###tramp-autoload
494 (when (featurep 'dbusbind)
495 (add-to-list 'tramp-foreign-file-name-handler-alist
496 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
497
498 \f
499 ;; D-Bus helper function.
500
501 (defun tramp-gvfs-dbus-string-to-byte-array (string)
502 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
503 (dbus-string-to-byte-array
504 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
505 (concat string (string 0)) string)))
506
507 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
508 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
509 ;; The byte array could be a variant. Take care.
510 (let ((byte-array
511 (if (and (consp byte-array) (atom (car byte-array)))
512 byte-array (car byte-array))))
513 (dbus-byte-array-to-string
514 (if (and (consp byte-array) (zerop (car (last byte-array))))
515 (butlast byte-array) byte-array))))
516
517 (defun tramp-gvfs-stringify-dbus-message (message)
518 "Convert a D-Bus message into readable UTF8 strings, used for traces."
519 (cond
520 ((and (consp message) (characterp (car message)))
521 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
522 ((consp message)
523 (mapcar 'tramp-gvfs-stringify-dbus-message message))
524 ((stringp message)
525 (format "%S" message))
526 (t message)))
527
528 (defmacro with-tramp-dbus-call-method
529 (vec synchronous bus service path interface method &rest args)
530 "Apply a D-Bus call on bus BUS.
531
532 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
533 it is an asynchronous call, with `ignore' as callback function.
534
535 The other arguments have the same meaning as with `dbus-call-method'
536 or `dbus-call-method-asynchronously'. Additionally, the call
537 will be traced by Tramp with trace level 6."
538 `(let ((func (if ,synchronous
539 'dbus-call-method 'dbus-call-method-asynchronously))
540 (args (append (list ,bus ,service ,path ,interface ,method)
541 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
542 result)
543 (tramp-message ,vec 6 "%s %s" func args)
544 (setq result (apply func args))
545 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
546 result))
547
548 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
549 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
550 (tramp-compat-font-lock-add-keywords
551 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
552
553 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
554 "Apply a Tramp GVFS `handler'.
555 In case of an error, modify the error message by replacing
556 `filename' with its GVFS mounted name."
557 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
558 elt)
559 (condition-case err
560 (tramp-compat-funcall ,handler ,@args)
561 (error
562 (setq elt (cdr err))
563 (while elt
564 (when (and (stringp (car elt))
565 (string-match fuse-file-name (car elt)))
566 (setcar elt (replace-match ,filename t t (car elt))))
567 (setq elt (cdr elt)))
568 (signal (car err) (cdr err))))))
569
570 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
571 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
572 (tramp-compat-font-lock-add-keywords
573 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
574
575 (defvar tramp-gvfs-dbus-event-vector nil
576 "Current Tramp file name to be used, as vector.
577 It is needed when D-Bus signals or errors arrive, because there
578 is no information where to trace the message.")
579
580 (defun tramp-gvfs-dbus-event-error (event err)
581 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
582 (when tramp-gvfs-dbus-event-vector
583 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
584 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
585
586 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
587 (add-hook
588 (if (boundp 'dbus-event-error-functions)
589 'dbus-event-error-functions 'dbus-event-error-hooks)
590 'tramp-gvfs-dbus-event-error)
591
592 \f
593 ;; File name primitives.
594
595 (defun tramp-gvfs-handle-copy-file
596 (filename newname &optional ok-if-already-exists keep-date
597 preserve-uid-gid preserve-extended-attributes)
598 "Like `copy-file' for Tramp files."
599 (with-parsed-tramp-file-name
600 (if (tramp-tramp-file-p filename) filename newname) nil
601
602 (when (and (not ok-if-already-exists) (file-exists-p newname))
603 (tramp-error
604 v 'file-already-exists "File %s already exists" newname))
605
606 (if (or (and (tramp-tramp-file-p filename)
607 (not (tramp-gvfs-file-name-p filename)))
608 (and (tramp-tramp-file-p newname)
609 (not (tramp-gvfs-file-name-p newname))))
610
611 ;; We cannot copy directly.
612 (let ((tmpfile (tramp-compat-make-temp-file filename)))
613 (cond
614 (preserve-extended-attributes
615 (copy-file
616 filename tmpfile t keep-date preserve-uid-gid
617 preserve-extended-attributes))
618 (preserve-uid-gid
619 (copy-file filename tmpfile t keep-date preserve-uid-gid))
620 (t
621 (copy-file filename tmpfile t keep-date)))
622 (rename-file tmpfile newname ok-if-already-exists))
623
624 ;; Direct copy.
625 (with-tramp-progress-reporter
626 v 0 (format "Copying %s to %s" filename newname)
627 (unless
628 (let ((args
629 (append (if (or keep-date preserve-uid-gid)
630 (list "--preserve")
631 nil)
632 (list
633 (tramp-gvfs-url-file-name filename)
634 (tramp-gvfs-url-file-name newname)))))
635 (apply 'tramp-gvfs-send-command v "gvfs-copy" args))
636 ;; Propagate the error.
637 (with-current-buffer (tramp-get-connection-buffer v)
638 (goto-char (point-min))
639 (tramp-error-with-buffer
640 nil v 'file-error
641 "Copying failed, see buffer `%s' for details." (buffer-name)))))
642
643 (when (file-remote-p newname)
644 (with-parsed-tramp-file-name newname nil
645 (tramp-flush-file-property v (file-name-directory localname))
646 (tramp-flush-file-property v localname))))))
647
648 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
649 "Like `delete-directory' for Tramp files."
650 (when (and recursive (not (file-symlink-p directory)))
651 (mapc (lambda (file)
652 (if (eq t (car (file-attributes file)))
653 (tramp-compat-delete-directory file recursive trash)
654 (tramp-compat-delete-file file trash)))
655 (directory-files
656 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
657 (with-parsed-tramp-file-name directory nil
658 (tramp-flush-file-property v (file-name-directory localname))
659 (tramp-flush-directory-property v localname)
660 (unless
661 (tramp-gvfs-send-command
662 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
663 (tramp-gvfs-url-file-name directory))
664 ;; Propagate the error.
665 (with-current-buffer (tramp-get-connection-buffer v)
666 (goto-char (point-min))
667 (tramp-error-with-buffer
668 nil v 'file-error "Couldn't delete %s" directory)))))
669
670 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
671 "Like `delete-file' for Tramp files."
672 (with-parsed-tramp-file-name filename nil
673 (tramp-flush-file-property v (file-name-directory localname))
674 (tramp-flush-directory-property v localname)
675 (unless
676 (tramp-gvfs-send-command
677 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
678 (tramp-gvfs-url-file-name filename))
679 ;; Propagate the error.
680 (with-current-buffer (tramp-get-connection-buffer v)
681 (goto-char (point-min))
682 (tramp-error-with-buffer
683 nil v 'file-error "Couldn't delete %s" filename)))))
684
685 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
686 "Like `expand-file-name' for Tramp files."
687 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
688 (setq dir (or dir default-directory "/"))
689 ;; Unless NAME is absolute, concat DIR and NAME.
690 (unless (file-name-absolute-p name)
691 (setq name (concat (file-name-as-directory dir) name)))
692 ;; If NAME is not a Tramp file, run the real handler.
693 (if (not (tramp-tramp-file-p name))
694 (tramp-run-real-handler 'expand-file-name (list name nil))
695 ;; Dissect NAME.
696 (with-parsed-tramp-file-name name nil
697 ;; If there is a default location, expand tilde.
698 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
699 (save-match-data
700 (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
701 (setq localname
702 (replace-match
703 (tramp-get-file-property v "/" "default-location" "~")
704 nil t localname 1)))
705 ;; Tilde expansion is not possible.
706 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
707 (tramp-error
708 v 'file-error
709 "Cannot expand tilde in file `%s'" name))
710 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
711 (setq localname (concat "/" localname)))
712 ;; We do not pass "/..".
713 (if (string-equal "smb" method)
714 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
715 (setq localname (replace-match "/" t t localname 1)))
716 (when (string-match "^/\\.\\./?" localname)
717 (setq localname (replace-match "/" t t localname))))
718 ;; There might be a double slash. Remove this.
719 (while (string-match "//" localname)
720 (setq localname (replace-match "/" t t localname)))
721 ;; No tilde characters in file name, do normal
722 ;; `expand-file-name' (this does "/./" and "/../").
723 (tramp-make-tramp-file-name
724 method user host
725 (tramp-run-real-handler
726 'expand-file-name (list localname))))))
727
728 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
729 "Like `file-attributes' for Tramp files."
730 (unless id-format (setq id-format 'integer))
731 ;; Don't modify `last-coding-system-used' by accident.
732 (let ((last-coding-system-used last-coding-system-used)
733 dirp res-symlink-target res-numlinks res-uid res-gid res-access
734 res-mod res-change res-size res-filemodes res-inode res-device)
735 (with-parsed-tramp-file-name filename nil
736 (with-tramp-file-property
737 v localname (format "file-attributes-%s" id-format)
738 (tramp-message v 5 "file attributes: %s" localname)
739 (tramp-gvfs-send-command
740 v "gvfs-info" (tramp-gvfs-url-file-name filename))
741 ;; Parse output ...
742 (with-current-buffer (tramp-get-connection-buffer v)
743 (goto-char (point-min))
744 (when (re-search-forward "attributes:" nil t)
745 ;; ... directory or symlink
746 (goto-char (point-min))
747 (setq dirp (if (re-search-forward "type:\\s-+directory" nil t) t))
748 (goto-char (point-min))
749 (setq res-symlink-target
750 (if (re-search-forward
751 "standard::symlink-target:\\s-+\\(\\S-+\\)" nil t)
752 (match-string 1)))
753 ;; ... number links
754 (goto-char (point-min))
755 (setq res-numlinks
756 (if (re-search-forward "unix::nlink:\\s-+\\([0-9]+\\)" nil t)
757 (string-to-number (match-string 1)) 0))
758 ;; ... uid and gid
759 (goto-char (point-min))
760 (setq res-uid
761 (or (if (eq id-format 'integer)
762 (if (re-search-forward
763 "unix::uid:\\s-+\\([0-9]+\\)" nil t)
764 (string-to-number (match-string 1)))
765 (if (re-search-forward
766 "owner::user:\\s-+\\(\\S-+\\)" nil t)
767 (match-string 1)))
768 (tramp-get-local-uid id-format)))
769 (setq res-gid
770 (or (if (eq id-format 'integer)
771 (if (re-search-forward
772 "unix::gid:\\s-+\\([0-9]+\\)" nil t)
773 (string-to-number (match-string 1)))
774 (if (re-search-forward
775 "owner::group:\\s-+\\(\\S-+\\)" nil t)
776 (match-string 1)))
777 (tramp-get-local-gid id-format)))
778 ;; ... last access, modification and change time
779 (goto-char (point-min))
780 (setq res-access
781 (if (re-search-forward
782 "time::access:\\s-+\\([0-9]+\\)" nil t)
783 (seconds-to-time (string-to-number (match-string 1)))
784 '(0 0)))
785 (goto-char (point-min))
786 (setq res-mod
787 (if (re-search-forward
788 "time::modified:\\s-+\\([0-9]+\\)" nil t)
789 (seconds-to-time (string-to-number (match-string 1)))
790 '(0 0)))
791 (goto-char (point-min))
792 (setq res-change
793 (if (re-search-forward
794 "time::changed:\\s-+\\([0-9]+\\)" nil t)
795 (seconds-to-time (string-to-number (match-string 1)))
796 '(0 0)))
797 ;; ... size
798 (goto-char (point-min))
799 (setq res-size
800 (if (re-search-forward
801 "standard::size:\\s-+\\([0-9]+\\)" nil t)
802 (string-to-number (match-string 1)) 0))
803 ;; ... file mode flags
804 (goto-char (point-min))
805 (setq res-filemodes
806 (if (re-search-forward "unix::mode:\\s-+\\([0-9]+\\)" nil t)
807 (tramp-file-mode-from-int (match-string 1))
808 (if dirp "drwx------" "-rwx------")))
809 ;; ... inode and device
810 (goto-char (point-min))
811 (setq res-inode
812 (if (re-search-forward "unix::inode:\\s-+\\([0-9]+\\)" nil t)
813 (string-to-number (match-string 1))
814 (tramp-get-inode v)))
815 (goto-char (point-min))
816 (setq res-device
817 (if (re-search-forward "unix::device:\\s-+\\([0-9]+\\)" nil t)
818 (string-to-number (match-string 1))
819 (tramp-get-device v)))
820
821 ;; Return data gathered.
822 (list
823 ;; 0. t for directory, string (name linked to) for
824 ;; symbolic link, or nil.
825 (or dirp res-symlink-target)
826 ;; 1. Number of links to file.
827 res-numlinks
828 ;; 2. File uid.
829 res-uid
830 ;; 3. File gid.
831 res-gid
832 ;; 4. Last access time, as a list of integers.
833 ;; 5. Last modification time, likewise.
834 ;; 6. Last status change time, likewise.
835 res-access res-mod res-change
836 ;; 7. Size in bytes (-1, if number is out of range).
837 res-size
838 ;; 8. File modes.
839 res-filemodes
840 ;; 9. t if file's gid would change if file were deleted
841 ;; and recreated.
842 nil
843 ;; 10. Inode number.
844 res-inode
845 ;; 11. Device number.
846 res-device
847 )))))))
848
849 (defun tramp-gvfs-handle-file-directory-p (filename)
850 "Like `file-directory-p' for Tramp files."
851 (eq t (car (file-attributes filename))))
852
853 (defun tramp-gvfs-handle-file-executable-p (filename)
854 "Like `file-executable-p' for Tramp files."
855 (with-parsed-tramp-file-name filename nil
856 (with-tramp-file-property v localname "file-executable-p"
857 (tramp-check-cached-permissions v ?x))))
858
859 (defun tramp-gvfs-handle-file-local-copy (filename)
860 "Like `file-local-copy' for Tramp files."
861 (with-parsed-tramp-file-name filename nil
862 (let ((tmpfile (tramp-compat-make-temp-file filename)))
863 (unless (file-exists-p filename)
864 (tramp-error
865 v 'file-error
866 "Cannot make local copy of non-existing file `%s'" filename))
867 (copy-file filename tmpfile t t)
868 tmpfile)))
869
870 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
871 "Like `file-name-all-completions' for Tramp files."
872 (unless (save-match-data (string-match "/" filename))
873 (with-parsed-tramp-file-name (expand-file-name directory) nil
874
875 (all-completions
876 filename
877 (mapcar
878 'list
879 (or
880 ;; Try cache entries for filename, filename with last
881 ;; character removed, filename with last two characters
882 ;; removed, ..., and finally the empty string - all
883 ;; concatenated to the local directory name.
884 (let ((remote-file-name-inhibit-cache
885 (or remote-file-name-inhibit-cache
886 tramp-completion-reread-directory-timeout)))
887
888 ;; This is inefficient for very long filenames, pity
889 ;; `reduce' is not available...
890 (car
891 (apply
892 'append
893 (mapcar
894 (lambda (x)
895 (let ((cache-hit
896 (tramp-get-file-property
897 v
898 (concat localname (substring filename 0 x))
899 "file-name-all-completions"
900 nil)))
901 (when cache-hit (list cache-hit))))
902 ;; We cannot use a length of 0, because file properties
903 ;; for "foo" and "foo/" are identical.
904 (tramp-compat-number-sequence (length filename) 1 -1)))))
905
906 ;; Cache expired or no matching cache entry found so we need
907 ;; to perform a remote operation.
908 (let ((result '("." ".."))
909 entry)
910 ;; Get a list of directories and files.
911 (tramp-gvfs-send-command
912 v "gvfs-ls" (tramp-gvfs-url-file-name directory))
913
914 ;; Now grab the output.
915 (with-temp-buffer
916 (insert-buffer-substring (tramp-get-connection-buffer v))
917 (goto-char (point-max))
918 (while (zerop (forward-line -1))
919 (setq entry (buffer-substring (point) (point-at-eol)))
920 (when (string-match filename entry)
921 (if (file-directory-p (expand-file-name entry directory))
922 (push (concat entry "/") result)
923 (push entry result)))))
924
925 ;; Because the remote op went through OK we know the
926 ;; directory we `cd'-ed to exists.
927 (tramp-set-file-property v localname "file-exists-p" t)
928
929 ;; Because the remote op went through OK we know every
930 ;; file listed by `ls' exists.
931 (mapc (lambda (entry)
932 (tramp-set-file-property
933 v (concat localname entry) "file-exists-p" t))
934 result)
935
936 ;; Store result in the cache.
937 (tramp-set-file-property
938 v (concat localname filename)
939 "file-name-all-completions" result))))))))
940
941 (defun tramp-gvfs-handle-file-readable-p (filename)
942 "Like `file-readable-p' for Tramp files."
943 (with-parsed-tramp-file-name filename nil
944 (with-tramp-file-property v localname "file-executable-p"
945 (tramp-check-cached-permissions v ?r))))
946
947 (defun tramp-gvfs-handle-file-writable-p (filename)
948 "Like `file-writable-p' for Tramp files."
949 (with-parsed-tramp-file-name filename nil
950 (with-tramp-file-property v localname "file-writable-p"
951 (if (file-exists-p filename)
952 (tramp-check-cached-permissions v ?w)
953 ;; If file doesn't exist, check if directory is writable.
954 (and (file-directory-p (file-name-directory filename))
955 (file-writable-p (file-name-directory filename)))))))
956
957 (defun tramp-gvfs-handle-insert-directory
958 (filename switches &optional wildcard full-directory-p)
959 "Like `insert-directory' for Tramp files."
960 ;; gvfs-* output is hard to parse. So we let `ls-lisp' do the job.
961 (with-parsed-tramp-file-name (expand-file-name filename) nil
962 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
963 (require 'ls-lisp)
964 (let (ls-lisp-use-insert-directory-program)
965 (tramp-run-real-handler
966 'insert-directory
967 (list filename switches wildcard full-directory-p))))))
968
969 (defun tramp-gvfs-handle-insert-file-contents
970 (filename &optional visit beg end replace)
971 "Like `insert-file-contents' for Tramp files."
972 (barf-if-buffer-read-only)
973 (setq filename (expand-file-name filename))
974 (let (tmpfile result)
975 (unwind-protect
976 (if (not (file-exists-p filename))
977 ;; We don't raise a Tramp error, because it might be
978 ;; suppressed, like in `find-file-noselect-1'.
979 (signal 'file-error (list "File not found on remote host" filename))
980
981 (setq tmpfile (file-local-copy filename)
982 result (insert-file-contents tmpfile visit beg end replace)))
983 ;; Save exit.
984 (when visit
985 (setq buffer-file-name filename)
986 (setq buffer-read-only (not (file-writable-p filename)))
987 (set-visited-file-modtime)
988 (set-buffer-modified-p nil))
989 (when (stringp tmpfile)
990 (delete-file tmpfile)))
991
992 ;; Result.
993 (list filename (cadr result))))
994
995 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
996 "Like `make-directory' for Tramp files."
997 (with-parsed-tramp-file-name dir nil
998 (unless
999 (apply
1000 'tramp-gvfs-send-command v "gvfs-mkdir"
1001 (if parents
1002 (list "-p" (tramp-gvfs-url-file-name dir))
1003 (list (tramp-gvfs-url-file-name dir))))
1004 ;; Propagate the error.
1005 (tramp-error v 'file-error "Couldn't make directory %s" dir))))
1006
1007 (defun tramp-gvfs-handle-rename-file
1008 (filename newname &optional ok-if-already-exists)
1009 "Like `rename-file' for Tramp files."
1010 (with-parsed-tramp-file-name
1011 (if (tramp-tramp-file-p filename) filename newname) nil
1012
1013 (when (and (not ok-if-already-exists) (file-exists-p newname))
1014 (tramp-error
1015 v 'file-already-exists "File %s already exists" newname))
1016
1017 (if (or (and (tramp-tramp-file-p filename)
1018 (not (tramp-gvfs-file-name-p filename)))
1019 (and (tramp-tramp-file-p newname)
1020 (not (tramp-gvfs-file-name-p newname))))
1021
1022 ;; We cannot move directly.
1023 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1024 (rename-file filename tmpfile t)
1025 (rename-file tmpfile newname ok-if-already-exists))
1026
1027 ;; Direct move.
1028 (with-tramp-progress-reporter
1029 v 0 (format "Renaming %s to %s" filename newname)
1030 (unless
1031 (tramp-gvfs-send-command
1032 v "gvfs-move"
1033 (tramp-gvfs-url-file-name filename)
1034 (tramp-gvfs-url-file-name newname))
1035 ;; Propagate the error.
1036 (with-current-buffer (tramp-get-buffer v)
1037 (goto-char (point-min))
1038 (tramp-error-with-buffer
1039 nil v 'file-error
1040 "Renaming failed, see buffer `%s' for details." (buffer-name)))))
1041
1042 (when (file-remote-p filename)
1043 (with-parsed-tramp-file-name filename nil
1044 (tramp-flush-file-property v (file-name-directory localname))
1045 (tramp-flush-file-property v localname)))
1046
1047 (when (file-remote-p newname)
1048 (with-parsed-tramp-file-name newname nil
1049 (tramp-flush-file-property v (file-name-directory localname))
1050 (tramp-flush-file-property v localname))))))
1051
1052 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
1053 "Like `set-visited-file-modtime' for Tramp files."
1054 (unless (buffer-file-name)
1055 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1056 (buffer-name)))
1057 (unless time-list
1058 (let ((f (buffer-file-name)))
1059 (with-parsed-tramp-file-name f nil
1060 (let ((remote-file-name-inhibit-cache t)
1061 (attr (file-attributes f)))
1062 ;; '(-1 65535) means file doesn't exists yet.
1063 (setq time-list (or (nth 5 attr) '(-1 65535)))))))
1064 ;; We use '(0 0) as a don't-know value.
1065 (unless (not (equal time-list '(0 0)))
1066 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))))
1067
1068 (defun tramp-gvfs-handle-write-region
1069 (start end filename &optional append visit lockname confirm)
1070 "Like `write-region' for Tramp files."
1071 (with-parsed-tramp-file-name filename nil
1072 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1073 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
1074 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1075 (tramp-error v 'file-error "File not overwritten")))
1076
1077 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1078 (write-region start end tmpfile)
1079 (condition-case nil
1080 (rename-file tmpfile filename)
1081 (error
1082 (delete-file tmpfile)
1083 (tramp-error
1084 v 'file-error "Couldn't write region to `%s'" filename))))
1085
1086 (tramp-flush-file-property v (file-name-directory localname))
1087 (tramp-flush-file-property v localname)
1088
1089 ;; Set file modification time.
1090 (when (or (eq visit t) (stringp visit))
1091 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1092
1093 ;; The end.
1094 (when (or (eq visit t) (null visit) (stringp visit))
1095 (tramp-message v 0 "Wrote %s" filename))
1096 (run-hooks 'tramp-handle-write-region-hook)))
1097
1098 \f
1099 ;; File name conversions.
1100
1101 (defun tramp-gvfs-url-file-name (filename)
1102 "Return FILENAME in URL syntax."
1103 ;; "/" must NOT be hexlified.
1104 (let ((url-unreserved-chars (append '(?/) url-unreserved-chars))
1105 result)
1106 (setq
1107 result
1108 (url-recreate-url
1109 (if (tramp-tramp-file-p filename)
1110 (with-parsed-tramp-file-name filename nil
1111 (when (and user (string-match tramp-user-with-domain-regexp user))
1112 (setq user
1113 (concat (match-string 2 user) ";" (match-string 1 user))))
1114 (url-parse-make-urlobj
1115 method (url-hexify-string user) nil
1116 (tramp-file-name-real-host v) (tramp-file-name-port v)
1117 (url-hexify-string localname) nil nil t))
1118 (url-parse-make-urlobj
1119 "file" nil nil nil nil
1120 (url-hexify-string (file-truename filename)) nil nil t))))
1121 (when (tramp-tramp-file-p filename)
1122 (with-parsed-tramp-file-name filename nil
1123 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1124 result))
1125
1126 (defun tramp-gvfs-object-path (filename)
1127 "Create a D-Bus object path from FILENAME."
1128 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1129
1130 (defun tramp-gvfs-file-name (object-path)
1131 "Retrieve file name from D-Bus OBJECT-PATH."
1132 (dbus-unescape-from-identifier
1133 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1134
1135 (defun tramp-gvfs-fuse-file-name (filename)
1136 "Return FUSE file name, which is directly accessible."
1137 (with-parsed-tramp-file-name (expand-file-name filename) nil
1138 (tramp-gvfs-maybe-open-connection v)
1139 (let ((prefix (tramp-get-file-property v "/" "prefix" ""))
1140 (fuse-mountpoint
1141 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
1142 (unless fuse-mountpoint
1143 (tramp-error
1144 v 'file-error "There is no FUSE mount point for `%s'" filename))
1145 ;; We must hide the prefix, if any.
1146 (when (string-match (concat "^" (regexp-quote prefix)) localname)
1147 (setq localname (replace-match "" t t localname)))
1148 (tramp-message
1149 v 10 "remote file `%s' is local file `%s'"
1150 filename (concat fuse-mountpoint localname))
1151 (concat fuse-mountpoint localname))))
1152
1153 (defun tramp-bluez-address (device)
1154 "Return bluetooth device address from a given bluetooth DEVICE name."
1155 (when (stringp device)
1156 (if (string-match tramp-ipv6-regexp device)
1157 (match-string 0 device)
1158 (cadr (assoc device (tramp-bluez-list-devices))))))
1159
1160 (defun tramp-bluez-device (address)
1161 "Return bluetooth device name from a given bluetooth device ADDRESS.
1162 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1163 (when (stringp address)
1164 (while (string-match "[][]" address)
1165 (setq address (replace-match "" t t address)))
1166 (let (result)
1167 (dolist (item (tramp-bluez-list-devices) result)
1168 (when (string-match address (cadr item))
1169 (setq result (car item)))))))
1170
1171 \f
1172 ;; D-Bus GVFS functions.
1173
1174 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1175 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1176 (let* ((filename
1177 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1178 (pw-prompt
1179 (format
1180 "%s for %s "
1181 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1182 (capitalize (match-string 1 message))
1183 "Password")
1184 filename))
1185 password)
1186
1187 (condition-case nil
1188 (with-parsed-tramp-file-name filename l
1189 (when (and (zerop (length user))
1190 (not
1191 (zerop (logand flags tramp-gvfs-password-need-username))))
1192 (setq user (read-string "User name: ")))
1193 (when (and (zerop (length domain))
1194 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
1195 (setq domain (read-string "Domain name: ")))
1196
1197 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1198 (setq tramp-current-method l-method
1199 tramp-current-user user
1200 tramp-current-host l-host
1201 password (tramp-read-passwd
1202 (tramp-get-connection-process l) pw-prompt))
1203
1204 ;; Return result.
1205 (if (stringp password)
1206 (list
1207 t ;; password handled.
1208 nil ;; no abort of D-Bus.
1209 password
1210 (tramp-file-name-real-user l)
1211 domain
1212 nil ;; not anonymous.
1213 0) ;; no password save.
1214 ;; No password provided.
1215 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1216
1217 ;; When QUIT is raised, we shall return this information to D-Bus.
1218 (quit (list nil t "" "" "" nil 0)))))
1219
1220 (defun tramp-gvfs-handler-askquestion (message choices)
1221 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1222 (save-window-excursion
1223 (let ((enable-recursive-minibuffers t)
1224 choice)
1225
1226 (condition-case nil
1227 (with-parsed-tramp-file-name
1228 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1229 (tramp-message v 6 "%S %S" message choices)
1230
1231 ;; In theory, there can be several choices. Until now,
1232 ;; there is only the question whether to accept an unknown
1233 ;; host signature.
1234 (with-temp-buffer
1235 ;; Preserve message for `progress-reporter'.
1236 (tramp-compat-with-temp-message ""
1237 (insert message)
1238 (pop-to-buffer (current-buffer))
1239 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1240 (tramp-message v 6 "%d" choice)))
1241
1242 ;; When the choice is "no", we set a dummy fuse-mountpoint
1243 ;; in order to leave the timeout.
1244 (unless (zerop choice)
1245 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1246
1247 (list
1248 t ;; handled.
1249 nil ;; no abort of D-Bus.
1250 choice))
1251
1252 ;; When QUIT is raised, we shall return this information to D-Bus.
1253 (quit (list nil t 0))))))
1254
1255 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1256 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1257 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1258 (ignore-errors
1259 (let ((signal-name (dbus-event-member-name last-input-event))
1260 (elt mount-info))
1261 ;; Jump over the first elements of the mount info. Since there
1262 ;; were changes in the entries, we cannot access dedicated
1263 ;; elements.
1264 (while (stringp (car elt)) (setq elt (cdr elt)))
1265 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1266 (mount-spec (caddr elt))
1267 (default-location (tramp-gvfs-dbus-byte-array-to-string
1268 (cadddr elt)))
1269 (method (tramp-gvfs-dbus-byte-array-to-string
1270 (cadr (assoc "type" (cadr mount-spec)))))
1271 (user (tramp-gvfs-dbus-byte-array-to-string
1272 (cadr (assoc "user" (cadr mount-spec)))))
1273 (domain (tramp-gvfs-dbus-byte-array-to-string
1274 (cadr (assoc "domain" (cadr mount-spec)))))
1275 (host (tramp-gvfs-dbus-byte-array-to-string
1276 (cadr (or (assoc "host" (cadr mount-spec))
1277 (assoc "server" (cadr mount-spec))))))
1278 (port (tramp-gvfs-dbus-byte-array-to-string
1279 (cadr (assoc "port" (cadr mount-spec)))))
1280 (ssl (tramp-gvfs-dbus-byte-array-to-string
1281 (cadr (assoc "ssl" (cadr mount-spec)))))
1282 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1283 (car mount-spec))
1284 (tramp-gvfs-dbus-byte-array-to-string
1285 (cadr (assoc "share" (cadr mount-spec)))))))
1286 (when (string-match "^smb" method)
1287 (setq method "smb"))
1288 (when (string-equal "obex" method)
1289 (setq host (tramp-bluez-device host)))
1290 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1291 (setq method "davs"))
1292 (unless (zerop (length domain))
1293 (setq user (concat user tramp-prefix-domain-format domain)))
1294 (unless (zerop (length port))
1295 (setq host (concat host tramp-prefix-port-format port)))
1296 (with-parsed-tramp-file-name
1297 (tramp-make-tramp-file-name method user host "") nil
1298 (tramp-message
1299 v 6 "%s %s"
1300 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1301 (tramp-set-file-property v "/" "list-mounts" 'undef)
1302 (if (string-equal (downcase signal-name) "unmounted")
1303 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1304 ;; Set prefix, mountpoint and location.
1305 (unless (string-equal prefix "/")
1306 (tramp-set-file-property v "/" "prefix" prefix))
1307 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1308 (tramp-set-file-property
1309 v "/" "default-location" default-location)))))))
1310
1311 (dbus-register-signal
1312 :session nil tramp-gvfs-path-mounttracker
1313 tramp-gvfs-interface-mounttracker "mounted"
1314 'tramp-gvfs-handler-mounted-unmounted)
1315 (dbus-register-signal
1316 :session nil tramp-gvfs-path-mounttracker
1317 tramp-gvfs-interface-mounttracker "Mounted"
1318 'tramp-gvfs-handler-mounted-unmounted)
1319
1320 (dbus-register-signal
1321 :session nil tramp-gvfs-path-mounttracker
1322 tramp-gvfs-interface-mounttracker "unmounted"
1323 'tramp-gvfs-handler-mounted-unmounted)
1324 (dbus-register-signal
1325 :session nil tramp-gvfs-path-mounttracker
1326 tramp-gvfs-interface-mounttracker "Unmounted"
1327 'tramp-gvfs-handler-mounted-unmounted)
1328
1329 (defun tramp-gvfs-connection-mounted-p (vec)
1330 "Check, whether the location is already mounted."
1331 (or
1332 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1333 (catch 'mounted
1334 (dolist
1335 (elt
1336 (with-tramp-file-property vec "/" "list-mounts"
1337 (with-tramp-dbus-call-method vec t
1338 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1339 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1340 nil)
1341 ;; Jump over the first elements of the mount info. Since there
1342 ;; were changes in the entries, we cannot access dedicated
1343 ;; elements.
1344 (while (stringp (car elt)) (setq elt (cdr elt)))
1345 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1346 (cadr elt)))
1347 (mount-spec (caddr elt))
1348 (default-location (tramp-gvfs-dbus-byte-array-to-string
1349 (cadddr elt)))
1350 (method (tramp-gvfs-dbus-byte-array-to-string
1351 (cadr (assoc "type" (cadr mount-spec)))))
1352 (user (tramp-gvfs-dbus-byte-array-to-string
1353 (cadr (assoc "user" (cadr mount-spec)))))
1354 (domain (tramp-gvfs-dbus-byte-array-to-string
1355 (cadr (assoc "domain" (cadr mount-spec)))))
1356 (host (tramp-gvfs-dbus-byte-array-to-string
1357 (cadr (or (assoc "host" (cadr mount-spec))
1358 (assoc "server" (cadr mount-spec))))))
1359 (port (tramp-gvfs-dbus-byte-array-to-string
1360 (cadr (assoc "port" (cadr mount-spec)))))
1361 (ssl (tramp-gvfs-dbus-byte-array-to-string
1362 (cadr (assoc "ssl" (cadr mount-spec)))))
1363 (prefix (concat (tramp-gvfs-dbus-byte-array-to-string
1364 (car mount-spec))
1365 (tramp-gvfs-dbus-byte-array-to-string
1366 (cadr (assoc "share" (cadr mount-spec)))))))
1367 (when (string-match "^smb" method)
1368 (setq method "smb"))
1369 (when (string-equal "obex" method)
1370 (setq host (tramp-bluez-device host)))
1371 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1372 (setq method "davs"))
1373 (when (and (string-equal "synce" method) (zerop (length user)))
1374 (setq user (or (tramp-file-name-user vec) "")))
1375 (unless (zerop (length domain))
1376 (setq user (concat user tramp-prefix-domain-format domain)))
1377 (unless (zerop (length port))
1378 (setq host (concat host tramp-prefix-port-format port)))
1379 (when (and
1380 (string-equal method (tramp-file-name-method vec))
1381 (string-equal user (or (tramp-file-name-user vec) ""))
1382 (string-equal host (tramp-file-name-host vec))
1383 (string-match (concat "^" (regexp-quote prefix))
1384 (tramp-file-name-localname vec)))
1385 ;; Set prefix, mountpoint and location.
1386 (unless (string-equal prefix "/")
1387 (tramp-set-file-property vec "/" "prefix" prefix))
1388 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1389 (tramp-set-file-property vec "/" "default-location" default-location)
1390 (throw 'mounted t)))))))
1391
1392 (defun tramp-gvfs-mount-spec-entry (key value)
1393 "Construct a mount-spec entry to be used in a mount_spec.
1394 It was \"a(say)\", but has changed to \"a{sv})\"."
1395 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1396 (list :dict-entry key
1397 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1398 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1399
1400 (defun tramp-gvfs-mount-spec (vec)
1401 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1402 (let* ((method (tramp-file-name-method vec))
1403 (user (tramp-file-name-real-user vec))
1404 (domain (tramp-file-name-domain vec))
1405 (host (tramp-file-name-real-host vec))
1406 (port (tramp-file-name-port vec))
1407 (localname (tramp-file-name-localname vec))
1408 (ssl (if (string-match "^davs" method) "true" "false"))
1409 (mount-spec '(:array))
1410 (mount-pref "/"))
1411
1412 (setq
1413 mount-spec
1414 (append
1415 mount-spec
1416 (cond
1417 ((string-equal "smb" method)
1418 (string-match "^/?\\([^/]+\\)" localname)
1419 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1420 (tramp-gvfs-mount-spec-entry "server" host)
1421 (tramp-gvfs-mount-spec-entry "share" (match-string 1 localname))))
1422 ((string-equal "obex" method)
1423 (list (tramp-gvfs-mount-spec-entry "type" method)
1424 (tramp-gvfs-mount-spec-entry
1425 "host" (concat "[" (tramp-bluez-address host) "]"))))
1426 ((string-match "^dav" method)
1427 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1428 (tramp-gvfs-mount-spec-entry "host" host)
1429 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1430 (t
1431 (list (tramp-gvfs-mount-spec-entry "type" method)
1432 (tramp-gvfs-mount-spec-entry "host" host))))))
1433
1434 (when user
1435 (add-to-list
1436 'mount-spec (tramp-gvfs-mount-spec-entry "user" user) 'append))
1437
1438 (when domain
1439 (add-to-list
1440 'mount-spec (tramp-gvfs-mount-spec-entry "domain" domain) 'append))
1441
1442 (when port
1443 (add-to-list
1444 'mount-spec (tramp-gvfs-mount-spec-entry "port" (number-to-string port))
1445 'append))
1446
1447 (when (and (string-match "^dav" method)
1448 (string-match "^/?[^/]+" localname))
1449 (setq mount-pref (match-string 0 localname)))
1450
1451 ;; Return.
1452 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1453
1454 \f
1455 ;; Connection functions
1456
1457 (defun tramp-gvfs-maybe-open-connection (vec)
1458 "Maybe open a connection VEC.
1459 Does not do anything if a connection is already open, but re-opens the
1460 connection if a previous connection has died for some reason."
1461
1462 ;; We set the file name, in case there are incoming D-Bus signals or
1463 ;; D-Bus errors.
1464 (setq tramp-gvfs-dbus-event-vector vec)
1465
1466 ;; For password handling, we need a process bound to the connection
1467 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1468 ;; better solution?
1469 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1470 (let ((p (make-network-process
1471 :name (tramp-buffer-name vec)
1472 :buffer (tramp-get-connection-buffer vec)
1473 :server t :host 'local :service t)))
1474 (tramp-compat-set-process-query-on-exit-flag p nil)))
1475
1476 (unless (tramp-gvfs-connection-mounted-p vec)
1477 (let* ((method (tramp-file-name-method vec))
1478 (user (tramp-file-name-user vec))
1479 (host (tramp-file-name-host vec))
1480 (localname (tramp-file-name-localname vec))
1481 (object-path
1482 (tramp-gvfs-object-path
1483 (tramp-make-tramp-file-name method user host ""))))
1484
1485 (when (and (string-equal method "smb")
1486 (string-equal localname "/"))
1487 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1488
1489 (with-tramp-progress-reporter
1490 vec 3
1491 (if (zerop (length user))
1492 (format "Opening connection for %s using %s" host method)
1493 (format "Opening connection for %s@%s using %s" user host method))
1494
1495 ;; Enable auth-source and password-cache.
1496 (tramp-set-connection-property vec "first-password-request" t)
1497
1498 ;; There will be a callback of "askPassword" when a password is
1499 ;; needed.
1500 (dbus-register-method
1501 :session dbus-service-emacs object-path
1502 tramp-gvfs-interface-mountoperation "askPassword"
1503 'tramp-gvfs-handler-askpassword)
1504 (dbus-register-method
1505 :session dbus-service-emacs object-path
1506 tramp-gvfs-interface-mountoperation "AskPassword"
1507 'tramp-gvfs-handler-askpassword)
1508
1509 ;; There could be a callback of "askQuestion" when adding fingerprint.
1510 (dbus-register-method
1511 :session dbus-service-emacs object-path
1512 tramp-gvfs-interface-mountoperation "askQuestion"
1513 'tramp-gvfs-handler-askquestion)
1514 (dbus-register-method
1515 :session dbus-service-emacs object-path
1516 tramp-gvfs-interface-mountoperation "AskQuestion"
1517 'tramp-gvfs-handler-askquestion)
1518
1519 ;; The call must be asynchronously, because of the "askPassword"
1520 ;; or "askQuestion"callbacks.
1521 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1522 (with-tramp-dbus-call-method vec nil
1523 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1524 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1525 (tramp-gvfs-mount-spec vec)
1526 `(:struct :string ,(dbus-get-unique-name :session)
1527 :object-path ,object-path))
1528 (with-tramp-dbus-call-method vec nil
1529 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1530 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1531 (tramp-gvfs-mount-spec vec)
1532 :string (dbus-get-unique-name :session) :object-path object-path))
1533
1534 ;; We must wait, until the mount is applied. This will be
1535 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1536 ;; file property.
1537 (with-timeout
1538 (60
1539 (if (zerop (length (tramp-file-name-user vec)))
1540 (tramp-error
1541 vec 'file-error
1542 "Timeout reached mounting %s using %s" host method)
1543 (tramp-error
1544 vec 'file-error
1545 "Timeout reached mounting %s@%s using %s" user host method)))
1546 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1547 (read-event nil nil 0.1)))
1548
1549 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1550 ;; is marked with the fuse-mountpoint "/". We shall react.
1551 (when (string-equal
1552 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1553 (tramp-error vec 'file-error "FUSE mount denied"))
1554
1555 ;; In `tramp-check-cached-permissions', the connection
1556 ;; properties {uig,gid}-{integer,string} are used. We set
1557 ;; them to their local counterparts.
1558 (tramp-set-connection-property
1559 vec "uid-integer" (tramp-get-local-uid 'integer))
1560 (tramp-set-connection-property
1561 vec "gid-integer" (tramp-get-local-gid 'integer))
1562 (tramp-set-connection-property
1563 vec "uid-string" (tramp-get-local-uid 'string))
1564 (tramp-set-connection-property
1565 vec "gid-string" (tramp-get-local-gid 'string))))))
1566
1567 (defun tramp-gvfs-send-command (vec command &rest args)
1568 "Send the COMMAND with its ARGS to connection VEC.
1569 COMMAND is usually a command from the gvfs-* utilities.
1570 `call-process' is applied, and it returns `t' if the return code is zero."
1571 (let (result)
1572 (with-current-buffer (tramp-get-connection-buffer vec)
1573 (tramp-gvfs-maybe-open-connection vec)
1574 (erase-buffer)
1575 (tramp-message vec 6 "%s %s" command (mapconcat 'identity args " "))
1576 (setq result (apply 'tramp-call-process command nil t nil args))
1577 (tramp-message vec 6 "\n%s" (buffer-string))
1578 (zerop result))))
1579
1580 \f
1581 ;; D-Bus BLUEZ functions.
1582
1583 (defun tramp-bluez-list-devices ()
1584 "Return all discovered bluetooth devices as list.
1585 Every entry is a list (NAME ADDRESS).
1586
1587 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1588 discovery happened more time before indicated there, a rescan will be
1589 started, which lasts some ten seconds. Otherwise, cached results will
1590 be used."
1591 ;; Reset the scanned devices list if time has passed.
1592 (and (integerp tramp-bluez-discover-devices-timeout)
1593 (integerp tramp-bluez-discovery)
1594 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1595 tramp-bluez-discover-devices-timeout)
1596 (setq tramp-bluez-devices nil))
1597
1598 ;; Rescan if needed.
1599 (unless tramp-bluez-devices
1600 (let ((object-path
1601 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1602 :system tramp-bluez-service "/"
1603 tramp-bluez-interface-manager "DefaultAdapter")))
1604 (setq tramp-bluez-devices nil
1605 tramp-bluez-discovery t)
1606 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1607 :system tramp-bluez-service object-path
1608 tramp-bluez-interface-adapter "StartDiscovery")
1609 (while tramp-bluez-discovery
1610 (read-event nil nil 0.1))))
1611 (setq tramp-bluez-discovery (current-time))
1612 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1613 tramp-bluez-devices)
1614
1615 (defun tramp-bluez-property-changed (property value)
1616 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1617 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1618 (cond
1619 ((string-equal property "Discovering")
1620 (unless (car value)
1621 ;; "Discovering" FALSE means discovery run has been completed.
1622 ;; We stop it, because we don't need another run.
1623 (setq tramp-bluez-discovery nil)
1624 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1625 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1626 tramp-bluez-interface-adapter "StopDiscovery")))))
1627
1628 (dbus-register-signal
1629 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1630 'tramp-bluez-property-changed)
1631
1632 (defun tramp-bluez-device-found (device args)
1633 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1634 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1635 (let ((alias (car (cadr (assoc "Alias" args))))
1636 (address (car (cadr (assoc "Address" args)))))
1637 ;; Maybe we shall check the device class for being a proper
1638 ;; device, and call also SDP in order to find the obex service.
1639 (add-to-list 'tramp-bluez-devices (list alias address))))
1640
1641 (dbus-register-signal
1642 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1643 'tramp-bluez-device-found)
1644
1645 (defun tramp-bluez-parse-device-names (ignore)
1646 "Return a list of (nil host) tuples allowed to access."
1647 (mapcar
1648 (lambda (x) (list nil (car x)))
1649 (tramp-bluez-list-devices)))
1650
1651 ;; Add completion function for OBEX method.
1652 (when (member tramp-bluez-service (dbus-list-known-names :system))
1653 (tramp-set-completion-function
1654 "obex" '((tramp-bluez-parse-device-names ""))))
1655
1656 \f
1657 ;; D-Bus zeroconf functions.
1658
1659 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1660 "Return a list of (user host) tuples allowed to access."
1661 (mapcar
1662 (lambda (x)
1663 (list nil (zeroconf-service-host x)))
1664 (zeroconf-list-services "_workstation._tcp")))
1665
1666 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1667 "Return a list of (user host) tuples allowed to access."
1668 (mapcar
1669 (lambda (x)
1670 (let ((host (zeroconf-service-host x))
1671 (port (zeroconf-service-port x))
1672 (text (zeroconf-service-txt x))
1673 user)
1674 (when port
1675 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1676 ;; A user is marked in a TXT field like "u=guest".
1677 (while text
1678 (when (string-match "u=\\(.+\\)$" (car text))
1679 (setq user (match-string 1 (car text))))
1680 (setq text (cdr text)))
1681 (list user host)))
1682 (zeroconf-list-services "_webdav._tcp")))
1683
1684 ;; Add completion function for DAV and DAVS methods.
1685 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1686 (zeroconf-init tramp-gvfs-zeroconf-domain)
1687 (tramp-set-completion-function
1688 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1689 (tramp-set-completion-function
1690 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1691 (tramp-set-completion-function
1692 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1693
1694 \f
1695 ;; D-Bus SYNCE functions.
1696
1697 (defun tramp-synce-list-devices ()
1698 "Return all discovered synce devices as list.
1699 They are retrieved from the hal daemon."
1700 (let (tramp-synce-devices)
1701 (dolist (device
1702 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1703 :system tramp-hal-service tramp-hal-path-manager
1704 tramp-hal-interface-manager "GetAllDevices"))
1705 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1706 :system tramp-hal-service device tramp-hal-interface-device
1707 "PropertyExists" "sync.plugin")
1708 (add-to-list
1709 'tramp-synce-devices
1710 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1711 :system tramp-hal-service device tramp-hal-interface-device
1712 "GetPropertyString" "pda.pocketpc.name"))))
1713 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1714 tramp-synce-devices))
1715
1716 (defun tramp-synce-parse-device-names (ignore)
1717 "Return a list of (nil host) tuples allowed to access."
1718 (mapcar
1719 (lambda (x) (list nil x))
1720 (tramp-synce-list-devices)))
1721
1722 ;; Add completion function for SYNCE method.
1723 (tramp-set-completion-function
1724 "synce" '((tramp-synce-parse-device-names "")))
1725
1726 (add-hook 'tramp-unload-hook
1727 (lambda ()
1728 (unload-feature 'tramp-gvfs 'force)))
1729
1730 (provide 'tramp-gvfs)
1731
1732 ;;; TODO:
1733
1734 ;; * Host name completion via smb-server or smb-network.
1735 ;; * Check how two shares of the same SMB server can be mounted in
1736 ;; parallel.
1737 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1738 ;; capability.
1739 ;; * Implement obex for other serial communication but bluetooth.
1740
1741 ;;; tramp-gvfs.el ends here