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