Merge changes from emacs-23 branch.
[bpt/emacs.git] / lisp / net / tramp-gvfs.el
1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon
2
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Access functions for the GVFS daemon from Tramp. Tested with GVFS
27 ;; 1.0.2 (Ubuntu 8.10, Gnome 2.24). It has been reported also to run
28 ;; with GVFS 0.2.5 (Ubuntu 8.04, Gnome 2.22), but there is an
29 ;; incompatibility with the mount_info structure, which has been
30 ;; worked around.
31
32 ;; It has also been tested with GVFS 1.6.2 (Ubuntu 10.04, Gnome 2.30),
33 ;; where the default_location has been added to mount_info (see
34 ;; <https://bugzilla.gnome.org/show_bug.cgi?id=561998>.
35
36 ;; All actions to mount a remote location, and to retrieve mount
37 ;; information, are performed by D-Bus messages. File operations
38 ;; themselves are performed via the mounted filesystem in ~/.gvfs.
39 ;; Consequently, GNU Emacs 23.1 with enabled D-Bus bindings is a
40 ;; precondition.
41
42 ;; The GVFS D-Bus interface is said to be instable. There are even no
43 ;; introspection data. The interface, as discovered during
44 ;; development time, is given in respective comments.
45
46 ;; The customer option `tramp-gvfs-methods' contains the list of
47 ;; supported connection methods. Per default, these are "dav",
48 ;; "davs", "obex" and "synce". Note that with "obex" it might be
49 ;; necessary to pair with the other bluetooth device, if it hasn't
50 ;; been done already. There might be also some few seconds delay in
51 ;; discovering available bluetooth devices.
52
53 ;; Other possible connection methods are "ftp", "sftp" and "smb".
54 ;; When one of these methods is added to the list, the remote access
55 ;; for that method is performed via GVFS instead of the native Tramp
56 ;; implementation.
57
58 ;; GVFS offers even more connection methods. The complete list of
59 ;; connection methods of the actual GVFS implementation can be
60 ;; retrieved by:
61 ;;
62 ;; (message
63 ;; "%s"
64 ;; (mapcar
65 ;; 'car
66 ;; (dbus-call-method
67 ;; :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
68 ;; tramp-gvfs-interface-mounttracker "listMountableInfo")))
69
70 ;; Note that all other connection methods are not tested, beside the
71 ;; ones offered for customization in `tramp-gvfs-methods'. If you
72 ;; request an additional connection method to be supported, please
73 ;; drop me a note.
74
75 ;; For hostname completion, information is retrieved either from the
76 ;; bluez daemon (for the "obex" method), the hal daemon (for the
77 ;; "synce" method), or from the zeroconf daemon (for the "dav",
78 ;; "davs", and "sftp" methods). The zeroconf daemon is pre-configured
79 ;; to discover services in the "local" domain. If another domain
80 ;; shall be used for discovering services, the customer option
81 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
82
83 ;; Restrictions:
84
85 ;; * The current GVFS implementation does not allow to write on the
86 ;; remote bluetooth device via OBEX.
87 ;;
88 ;; * Two shares of the same SMB server cannot be mounted in parallel.
89
90 ;;; Code:
91
92 ;; D-Bus support in the Emacs core can be disabled with configuration
93 ;; option "--without-dbus". Declare used subroutines and variables.
94 (declare-function dbus-call-method "dbusbind.c")
95 (declare-function dbus-call-method-asynchronously "dbusbind.c")
96 (declare-function dbus-get-unique-name "dbusbind.c")
97 (declare-function dbus-register-method "dbusbind.c")
98 (declare-function dbus-register-signal "dbusbind.c")
99
100 ;; Pacify byte-compiler
101 (eval-when-compile
102 (require 'cl)
103 (require 'custom))
104
105 (require 'tramp)
106
107 ;; We call several `tramp-handle-*' functions directly. So we must
108 ;; reqire that package as well.
109 (require 'tramp-sh)
110
111 (require 'dbus)
112 (require 'url-parse)
113 (require 'url-util)
114 (require 'zeroconf)
115
116 ;;;###tramp-autoload
117 (defcustom tramp-gvfs-methods '("dav" "davs" "obex" "synce")
118 "*List of methods for remote files, accessed with GVFS."
119 :group 'tramp
120 :version "23.2"
121 :type '(repeat (choice (const "dav")
122 (const "davs")
123 (const "ftp")
124 (const "obex")
125 (const "sftp")
126 (const "smb")
127 (const "synce"))))
128
129 ;; Add a default for `tramp-default-user-alist'. Rule: For the SYNCE
130 ;; method, no user is chosen.
131 (add-to-list 'tramp-default-user-alist
132 '("synce" nil nil))
133
134 (defcustom tramp-gvfs-zeroconf-domain "local"
135 "*Zeroconf domain to be used for discovering services, like host names."
136 :group 'tramp
137 :version "23.2"
138 :type 'string)
139
140 ;; Add the methods to `tramp-methods', in order to allow minibuffer
141 ;; completion.
142 ;;;###tramp-autoload
143 (when (featurep 'dbusbind)
144 (dolist (elt tramp-gvfs-methods)
145 (unless (assoc elt tramp-methods)
146 (add-to-list 'tramp-methods (cons elt nil)))))
147
148 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
149 "The preceeding object path for own objects.")
150
151 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
152 "The well known name of the GVFS daemon.")
153
154 ;; Check that GVFS is available. D-Bus integration is available since
155 ;; Emacs 23 on some system types. We don't call `dbus-ping', because
156 ;; this would load dbus.el.
157 (unless (and (tramp-compat-funcall 'dbus-get-unique-name :session)
158 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
159 (error "Package `tramp-gvfs' not supported"))
160
161 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
162 "The object path of the GVFS daemon.")
163
164 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
165 "The mount tracking interface in the GVFS daemon.")
166
167 ;; <interface name='org.gtk.vfs.MountTracker'>
168 ;; <method name='listMounts'>
169 ;; <arg name='mount_info_list'
170 ;; type='a{sosssssbay{aya{say}}ay}'
171 ;; direction='out'/>
172 ;; </method>
173 ;; <method name='mountLocation'>
174 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
175 ;; <arg name='dbus_id' type='s' direction='in'/>
176 ;; <arg name='object_path' type='o' direction='in'/>
177 ;; </method>
178 ;; <signal name='mounted'>
179 ;; <arg name='mount_info'
180 ;; type='{sosssssbay{aya{say}}ay}'/>
181 ;; </signal>
182 ;; <signal name='unmounted'>
183 ;; <arg name='mount_info'
184 ;; type='{sosssssbay{aya{say}}ay}'/>
185 ;; </signal>
186 ;; </interface>
187 ;;
188 ;; STRUCT mount_info
189 ;; STRING dbus_id
190 ;; OBJECT_PATH object_path
191 ;; STRING display_name
192 ;; STRING stable_name
193 ;; STRING x_content_types Since GVFS 1.0 only !!!
194 ;; STRING icon
195 ;; STRING prefered_filename_encoding
196 ;; BOOLEAN user_visible
197 ;; ARRAY BYTE fuse_mountpoint
198 ;; STRUCT mount_spec
199 ;; ARRAY BYTE mount_prefix
200 ;; ARRAY
201 ;; STRUCT mount_spec_item
202 ;; STRING key (server, share, type, user, host, port)
203 ;; ARRAY BYTE value
204 ;; ARRAY BYTE default_location Since GVFS 1.5 only !!!
205
206 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
207 "Used by the dbus-proxying implementation of GMountOperation.")
208
209 ;; <interface name='org.gtk.vfs.MountOperation'>
210 ;; <method name='askPassword'>
211 ;; <arg name='message' type='s' direction='in'/>
212 ;; <arg name='default_user' type='s' direction='in'/>
213 ;; <arg name='default_domain' type='s' direction='in'/>
214 ;; <arg name='flags' type='u' direction='in'/>
215 ;; <arg name='handled' type='b' direction='out'/>
216 ;; <arg name='aborted' type='b' direction='out'/>
217 ;; <arg name='password' type='s' direction='out'/>
218 ;; <arg name='username' type='s' direction='out'/>
219 ;; <arg name='domain' type='s' direction='out'/>
220 ;; <arg name='anonymous' type='b' direction='out'/>
221 ;; <arg name='password_save' type='u' direction='out'/>
222 ;; </method>
223 ;; <method name='askQuestion'>
224 ;; <arg name='message' type='s' direction='in'/>
225 ;; <arg name='choices' type='as' direction='in'/>
226 ;; <arg name='handled' type='b' direction='out'/>
227 ;; <arg name='aborted' type='b' direction='out'/>
228 ;; <arg name='choice' type='u' direction='out'/>
229 ;; </method>
230 ;; </interface>
231
232 ;; The following flags are used in "askPassword". They are defined in
233 ;; /usr/include/glib-2.0/gio/gioenums.h.
234
235 (defconst tramp-gvfs-password-need-password 1
236 "Operation requires a password.")
237
238 (defconst tramp-gvfs-password-need-username 2
239 "Operation requires a username.")
240
241 (defconst tramp-gvfs-password-need-domain 4
242 "Operation requires a domain.")
243
244 (defconst tramp-gvfs-password-saving-supported 8
245 "Operation supports saving settings.")
246
247 (defconst tramp-gvfs-password-anonymous-supported 16
248 "Operation supports anonymous users.")
249
250 (defconst tramp-bluez-service "org.bluez"
251 "The well known name of the BLUEZ service.")
252
253 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
254 "The manager interface of the BLUEZ daemon.")
255
256 ;; <interface name='org.bluez.Manager'>
257 ;; <method name='DefaultAdapter'>
258 ;; <arg type='o' direction='out'/>
259 ;; </method>
260 ;; <method name='FindAdapter'>
261 ;; <arg type='s' direction='in'/>
262 ;; <arg type='o' direction='out'/>
263 ;; </method>
264 ;; <method name='ListAdapters'>
265 ;; <arg type='ao' direction='out'/>
266 ;; </method>
267 ;; <signal name='AdapterAdded'>
268 ;; <arg type='o'/>
269 ;; </signal>
270 ;; <signal name='AdapterRemoved'>
271 ;; <arg type='o'/>
272 ;; </signal>
273 ;; <signal name='DefaultAdapterChanged'>
274 ;; <arg type='o'/>
275 ;; </signal>
276 ;; </interface>
277
278 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
279 "The adapter interface of the BLUEZ daemon.")
280
281 ;; <interface name='org.bluez.Adapter'>
282 ;; <method name='GetProperties'>
283 ;; <arg type='a{sv}' direction='out'/>
284 ;; </method>
285 ;; <method name='SetProperty'>
286 ;; <arg type='s' direction='in'/>
287 ;; <arg type='v' direction='in'/>
288 ;; </method>
289 ;; <method name='RequestMode'>
290 ;; <arg type='s' direction='in'/>
291 ;; </method>
292 ;; <method name='ReleaseMode'/>
293 ;; <method name='RequestSession'/>
294 ;; <method name='ReleaseSession'/>
295 ;; <method name='StartDiscovery'/>
296 ;; <method name='StopDiscovery'/>
297 ;; <method name='ListDevices'>
298 ;; <arg type='ao' direction='out'/>
299 ;; </method>
300 ;; <method name='CreateDevice'>
301 ;; <arg type='s' direction='in'/>
302 ;; <arg type='o' direction='out'/>
303 ;; </method>
304 ;; <method name='CreatePairedDevice'>
305 ;; <arg type='s' direction='in'/>
306 ;; <arg type='o' direction='in'/>
307 ;; <arg type='s' direction='in'/>
308 ;; <arg type='o' direction='out'/>
309 ;; </method>
310 ;; <method name='CancelDeviceCreation'>
311 ;; <arg type='s' direction='in'/>
312 ;; </method>
313 ;; <method name='RemoveDevice'>
314 ;; <arg type='o' direction='in'/>
315 ;; </method>
316 ;; <method name='FindDevice'>
317 ;; <arg type='s' direction='in'/>
318 ;; <arg type='o' direction='out'/>
319 ;; </method>
320 ;; <method name='RegisterAgent'>
321 ;; <arg type='o' direction='in'/>
322 ;; <arg type='s' direction='in'/>
323 ;; </method>
324 ;; <method name='UnregisterAgent'>
325 ;; <arg type='o' direction='in'/>
326 ;; </method>
327 ;; <signal name='DeviceCreated'>
328 ;; <arg type='o'/>
329 ;; </signal>
330 ;; <signal name='DeviceRemoved'>
331 ;; <arg type='o'/>
332 ;; </signal>
333 ;; <signal name='DeviceFound'>
334 ;; <arg type='s'/>
335 ;; <arg type='a{sv}'/>
336 ;; </signal>
337 ;; <signal name='PropertyChanged'>
338 ;; <arg type='s'/>
339 ;; <arg type='v'/>
340 ;; </signal>
341 ;; <signal name='DeviceDisappeared'>
342 ;; <arg type='s'/>
343 ;; </signal>
344 ;; </interface>
345
346 (defcustom tramp-bluez-discover-devices-timeout 60
347 "Defines seconds since last bluetooth device discovery before rescanning.
348 A value of 0 would require an immediate discovery during hostname
349 completion, nil means to use always cached values for discovered
350 devices."
351 :group 'tramp
352 :version "23.2"
353 :type '(choice (const nil) integer))
354
355 (defvar tramp-bluez-discovery nil
356 "Indicator for a running bluetooth device discovery.
357 It keeps the timestamp of last discovery.")
358
359 (defvar tramp-bluez-devices nil
360 "Alist of detected bluetooth devices.
361 Every entry is a list (NAME ADDRESS).")
362
363 (defconst tramp-hal-service "org.freedesktop.Hal"
364 "The well known name of the HAL service.")
365
366 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
367 "The object path of the HAL daemon manager.")
368
369 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
370 "The manager interface of the HAL daemon.")
371
372 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
373 "The device interface of the HAL daemon.")
374
375 \f
376 ;; New handlers should be added here.
377 (defconst tramp-gvfs-file-name-handler-alist
378 '(
379 (access-file . ignore)
380 (add-name-to-file . tramp-gvfs-handle-copy-file)
381 ;; `byte-compiler-base-file-name' performed by default handler.
382 (copy-file . tramp-gvfs-handle-copy-file)
383 (delete-directory . tramp-gvfs-handle-delete-directory)
384 (delete-file . tramp-gvfs-handle-delete-file)
385 ;; `diff-latest-backup-file' performed by default handler.
386 (directory-file-name . tramp-handle-directory-file-name)
387 (directory-files . tramp-gvfs-handle-directory-files)
388 (directory-files-and-attributes
389 . tramp-gvfs-handle-directory-files-and-attributes)
390 (dired-call-process . ignore)
391 (dired-compress-file . ignore)
392 (dired-uncache . tramp-handle-dired-uncache)
393 ;; `executable-find' is not official yet. performed by default handler.
394 (expand-file-name . tramp-gvfs-handle-expand-file-name)
395 ;; `file-accessible-directory-p' performed by default handler.
396 (file-attributes . tramp-gvfs-handle-file-attributes)
397 (file-directory-p . tramp-gvfs-handle-file-directory-p)
398 (file-executable-p . tramp-gvfs-handle-file-executable-p)
399 (file-exists-p . tramp-gvfs-handle-file-exists-p)
400 (file-local-copy . tramp-gvfs-handle-file-local-copy)
401 ;; `file-modes' performed by default handler.
402 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
403 (file-name-as-directory . tramp-handle-file-name-as-directory)
404 (file-name-completion . tramp-handle-file-name-completion)
405 (file-name-directory . tramp-handle-file-name-directory)
406 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
407 ;; `file-name-sans-versions' performed by default handler.
408 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
409 (file-ownership-preserved-p . ignore)
410 (file-readable-p . tramp-gvfs-handle-file-readable-p)
411 (file-regular-p . tramp-handle-file-regular-p)
412 (file-remote-p . tramp-handle-file-remote-p)
413 (file-selinux-context . tramp-gvfs-handle-file-selinux-context)
414 (file-symlink-p . tramp-handle-file-symlink-p)
415 ;; `file-truename' performed by default handler.
416 (file-writable-p . tramp-gvfs-handle-file-writable-p)
417 (find-backup-file-name . tramp-handle-find-backup-file-name)
418 ;; `find-file-noselect' performed by default handler.
419 ;; `get-file-buffer' performed by default handler.
420 (insert-directory . tramp-gvfs-handle-insert-directory)
421 (insert-file-contents . tramp-gvfs-handle-insert-file-contents)
422 (load . tramp-handle-load)
423 (make-directory . tramp-gvfs-handle-make-directory)
424 (make-directory-internal . ignore)
425 (make-symbolic-link . ignore)
426 (process-file . tramp-gvfs-handle-process-file)
427 (rename-file . tramp-gvfs-handle-rename-file)
428 (set-file-modes . tramp-gvfs-handle-set-file-modes)
429 (set-file-selinux-context . tramp-gvfs-handle-set-file-selinux-context)
430 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime)
431 (shell-command . tramp-gvfs-handle-shell-command)
432 (start-file-process . tramp-gvfs-handle-start-file-process)
433 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
434 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
435 (vc-registered . ignore)
436 (verify-visited-file-modtime
437 . tramp-gvfs-handle-verify-visited-file-modtime)
438 (write-region . tramp-gvfs-handle-write-region)
439 )
440 "Alist of handler functions for Tramp GVFS method.
441 Operations not mentioned here will be handled by the default Emacs primitives.")
442
443 ;;;###tramp-autoload
444 (defsubst tramp-gvfs-file-name-p (filename)
445 "Check if it's a filename handled by the GVFS daemon."
446 (and (tramp-tramp-file-p filename)
447 (let ((method
448 (tramp-file-name-method (tramp-dissect-file-name filename))))
449 (and (stringp method) (member method tramp-gvfs-methods)))))
450
451 ;;;###tramp-autoload
452 (defun tramp-gvfs-file-name-handler (operation &rest args)
453 "Invoke the GVFS related OPERATION.
454 First arg specifies the OPERATION, second arg is a list of arguments to
455 pass to the OPERATION."
456 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
457 (if fn
458 (save-match-data (apply (cdr fn) args))
459 (tramp-run-real-handler operation args))))
460
461 ;; This might be moved to tramp.el. It shall be the first file name
462 ;; handler.
463 ;;;###tramp-autoload
464 (when (featurep 'dbusbind)
465 (add-to-list 'tramp-foreign-file-name-handler-alist
466 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
467
468 (defun tramp-gvfs-stringify-dbus-message (message)
469 "Convert a D-Bus message into readable UTF8 strings, used for traces."
470 (cond
471 ((and (consp message) (characterp (car message)))
472 (format "%S" (dbus-byte-array-to-string message)))
473 ((consp message)
474 (mapcar 'tramp-gvfs-stringify-dbus-message message))
475 ((stringp message)
476 (format "%S" message))
477 (t message)))
478
479 (defmacro with-tramp-dbus-call-method
480 (vec synchronous bus service path interface method &rest args)
481 "Apply a D-Bus call on bus BUS.
482
483 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
484 it is an asynchronous call, with `ignore' as callback function.
485
486 The other arguments have the same meaning as with `dbus-call-method'
487 or `dbus-call-method-asynchronously'. Additionally, the call
488 will be traced by Tramp with trace level 6."
489 `(let ((func (if ,synchronous
490 'dbus-call-method 'dbus-call-method-asynchronously))
491 (args (append (list ,bus ,service ,path ,interface ,method)
492 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
493 result)
494 (tramp-message ,vec 6 "%s %s" func args)
495 (setq result (apply func args))
496 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
497 result))
498
499 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
500 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
501 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
502
503 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
504 "Apply a Tramp GVFS `handler'.
505 In case of an error, modify the error message by replacing
506 `filename' with its GVFS mounted name."
507 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
508 elt)
509 (condition-case err
510 (tramp-compat-funcall ,handler ,@args)
511 (error
512 (setq elt (cdr err))
513 (while elt
514 (when (and (stringp (car elt))
515 (string-match fuse-file-name (car elt)))
516 (setcar elt (replace-match ,filename t t (car elt))))
517 (setq elt (cdr elt)))
518 (signal (car err) (cdr err))))))
519
520 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
521 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
522 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
523
524 (defvar tramp-gvfs-dbus-event-vector nil
525 "Current Tramp file name to be used, as vector.
526 It is needed when D-Bus signals or errors arrive, because there
527 is no information where to trace the message.")
528
529 (defun tramp-gvfs-dbus-event-error (event err)
530 "Called when a D-Bus error message arrives, see `dbus-event-error-hooks'."
531 (when tramp-gvfs-dbus-event-vector
532 ;(tramp-cleanup-connection tramp-gvfs-dbus-event-vector)
533 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
534 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
535
536 (add-hook 'dbus-event-error-hooks 'tramp-gvfs-dbus-event-error)
537
538 \f
539 ;; File name primitives.
540
541 (defun tramp-gvfs-handle-copy-file
542 (filename newname &optional ok-if-already-exists keep-date
543 preserve-uid-gid preserve-selinux-context)
544 "Like `copy-file' for Tramp files."
545 (with-parsed-tramp-file-name
546 (if (tramp-tramp-file-p filename) filename newname) nil
547 (with-progress-reporter
548 v 0 (format "Copying %s to %s" filename newname)
549 (condition-case err
550 (let ((args
551 (list
552 (if (tramp-gvfs-file-name-p filename)
553 (tramp-gvfs-fuse-file-name filename)
554 filename)
555 (if (tramp-gvfs-file-name-p newname)
556 (tramp-gvfs-fuse-file-name newname)
557 newname)
558 ok-if-already-exists keep-date preserve-uid-gid)))
559 (when preserve-selinux-context
560 (setq args (append args (list preserve-selinux-context))))
561 (apply 'copy-file args))
562
563 ;; Error case. Let's try it with the GVFS utilities.
564 (error
565 (tramp-message v 4 "`copy-file' failed, trying `gvfs-copy'")
566 (unless
567 (zerop
568 (let ((args
569 (append (if (or keep-date preserve-uid-gid)
570 (list "--preserve")
571 nil)
572 (list
573 (tramp-gvfs-url-file-name filename)
574 (tramp-gvfs-url-file-name newname)))))
575 (apply 'tramp-gvfs-send-command v "gvfs-copy" args)))
576 ;; Propagate the error.
577 (tramp-error v (car err) "%s" (cdr err)))))))
578
579 (when (file-remote-p newname)
580 (with-parsed-tramp-file-name newname nil
581 (tramp-flush-file-property v (file-name-directory localname))
582 (tramp-flush-file-property v localname))))
583
584 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive)
585 "Like `delete-directory' for Tramp files."
586 (tramp-compat-delete-directory
587 (tramp-gvfs-fuse-file-name directory) recursive))
588
589 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
590 "Like `delete-file' for Tramp files."
591 (tramp-compat-delete-file (tramp-gvfs-fuse-file-name filename) trash))
592
593 (defun tramp-gvfs-handle-directory-files
594 (directory &optional full match nosort)
595 "Like `directory-files' for Tramp files."
596 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
597 (mapcar
598 (lambda (x)
599 (if (string-match fuse-file-name x)
600 (replace-match directory t t x)
601 x))
602 (directory-files fuse-file-name full match nosort))))
603
604 (defun tramp-gvfs-handle-directory-files-and-attributes
605 (directory &optional full match nosort id-format)
606 "Like `directory-files-and-attributes' for Tramp files."
607 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
608 (mapcar
609 (lambda (x)
610 (when (string-match fuse-file-name (car x))
611 (setcar x (replace-match directory t t (car x))))
612 x)
613 (directory-files-and-attributes
614 fuse-file-name full match nosort id-format))))
615
616 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
617 "Like `expand-file-name' for Tramp files."
618 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
619 (setq dir (or dir default-directory "/"))
620 ;; Unless NAME is absolute, concat DIR and NAME.
621 (unless (file-name-absolute-p name)
622 (setq name (concat (file-name-as-directory dir) name)))
623 ;; If NAME is not a Tramp file, run the real handler.
624 (if (not (tramp-tramp-file-p name))
625 (tramp-run-real-handler 'expand-file-name (list name nil))
626 ;; Dissect NAME.
627 (with-parsed-tramp-file-name name nil
628 ;; If there is a default location, expand tilde.
629 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
630 (save-match-data
631 (tramp-gvfs-maybe-open-connection (vector method user host "/")))
632 (setq localname
633 (replace-match
634 (tramp-get-file-property v "/" "default-location" "~")
635 nil t localname 1)))
636 ;; Tilde expansion is not possible.
637 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
638 (tramp-error
639 v 'file-error
640 "Cannot expand tilde in file `%s'" name))
641 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
642 (setq localname (concat "/" localname)))
643 ;; We do not pass "/..".
644 (if (string-equal "smb" method)
645 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
646 (setq localname (replace-match "/" t t localname 1)))
647 (when (string-match "^/\\.\\./?" localname)
648 (setq localname (replace-match "/" t t localname))))
649 ;; There might be a double slash. Remove this.
650 (while (string-match "//" localname)
651 (setq localname (replace-match "/" t t localname)))
652 ;; No tilde characters in file name, do normal
653 ;; `expand-file-name' (this does "/./" and "/../").
654 (tramp-make-tramp-file-name
655 method user host
656 (tramp-run-real-handler
657 'expand-file-name (list localname))))))
658
659 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
660 "Like `file-attributes' for Tramp files."
661 (file-attributes (tramp-gvfs-fuse-file-name filename) id-format))
662
663 (defun tramp-gvfs-handle-file-directory-p (filename)
664 "Like `file-directory-p' for Tramp files."
665 (file-directory-p (tramp-gvfs-fuse-file-name filename)))
666
667 (defun tramp-gvfs-handle-file-executable-p (filename)
668 "Like `file-executable-p' for Tramp files."
669 (file-executable-p (tramp-gvfs-fuse-file-name filename)))
670
671 (defun tramp-gvfs-handle-file-exists-p (filename)
672 "Like `file-exists-p' for Tramp files."
673 (file-exists-p (tramp-gvfs-fuse-file-name filename)))
674
675 (defun tramp-gvfs-handle-file-local-copy (filename)
676 "Like `file-local-copy' for Tramp files."
677 (with-parsed-tramp-file-name filename nil
678 (let ((tmpfile (tramp-compat-make-temp-file filename)))
679 (unless (file-exists-p filename)
680 (tramp-error
681 v 'file-error
682 "Cannot make local copy of non-existing file `%s'" filename))
683 (copy-file filename tmpfile t t)
684 tmpfile)))
685
686 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
687 "Like `file-name-all-completions' for Tramp files."
688 (unless (save-match-data (string-match "/" filename))
689 (file-name-all-completions filename (tramp-gvfs-fuse-file-name directory))))
690
691 (defun tramp-gvfs-handle-file-readable-p (filename)
692 "Like `file-readable-p' for Tramp files."
693 (file-readable-p (tramp-gvfs-fuse-file-name filename)))
694
695 (defun tramp-gvfs-handle-file-selinux-context (filename)
696 "Like `file-selinux-context' for Tramp files."
697 (tramp-compat-funcall
698 'file-selinux-context (tramp-gvfs-fuse-file-name filename)))
699
700 (defun tramp-gvfs-handle-file-writable-p (filename)
701 "Like `file-writable-p' for Tramp files."
702 (file-writable-p (tramp-gvfs-fuse-file-name filename)))
703
704 (defun tramp-gvfs-handle-insert-directory
705 (filename switches &optional wildcard full-directory-p)
706 "Like `insert-directory' for Tramp files."
707 (insert-directory
708 (tramp-gvfs-fuse-file-name filename) switches wildcard full-directory-p))
709
710 (defun tramp-gvfs-handle-insert-file-contents
711 (filename &optional visit beg end replace)
712 "Like `insert-file-contents' for Tramp files."
713 (unwind-protect
714 (let ((fuse-file-name (tramp-gvfs-fuse-file-name filename))
715 (result
716 (insert-file-contents
717 (tramp-gvfs-fuse-file-name filename) visit beg end replace)))
718 (when (string-match fuse-file-name (car result))
719 (setcar result (replace-match filename t t (car result))))
720 result)
721 (setq buffer-file-name filename)))
722
723 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
724 "Like `make-directory' for Tramp files."
725 (with-parsed-tramp-file-name dir nil
726 (condition-case err
727 (with-tramp-gvfs-error-message dir 'make-directory
728 (tramp-gvfs-fuse-file-name dir) parents)
729
730 ;; Error case. Let's try it with the GVFS utilities.
731 (error
732 (tramp-message v 4 "`make-directory' failed, trying `gvfs-mkdir'")
733 (unless
734 (zerop
735 (tramp-gvfs-send-command
736 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir)))
737 ;; Propagate the error.
738 (tramp-error v (car err) "%s" (cdr err)))))))
739
740 (defun tramp-gvfs-handle-process-file
741 (program &optional infile destination display &rest args)
742 "Like `process-file' for Tramp files."
743 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
744 (apply 'call-process program infile destination display args)))
745
746 (defun tramp-gvfs-handle-rename-file
747 (filename newname &optional ok-if-already-exists)
748 "Like `rename-file' for Tramp files."
749 (with-parsed-tramp-file-name
750 (if (tramp-tramp-file-p filename) filename newname) nil
751 (with-progress-reporter
752 v 0 (format "Renaming %s to %s" filename newname)
753 (condition-case err
754 (rename-file
755 (if (tramp-gvfs-file-name-p filename)
756 (tramp-gvfs-fuse-file-name filename)
757 filename)
758 (if (tramp-gvfs-file-name-p newname)
759 (tramp-gvfs-fuse-file-name newname)
760 newname)
761 ok-if-already-exists)
762
763 ;; Error case. Let's try it with the GVFS utilities.
764 (error
765 (tramp-message v 4 "`rename-file' failed, trying `gvfs-move'")
766 (unless
767 (zerop
768 (tramp-gvfs-send-command
769 v "gvfs-move"
770 (tramp-gvfs-url-file-name filename)
771 (tramp-gvfs-url-file-name newname)))
772 ;; Propagate the error.
773 (tramp-error v (car err) "%s" (cdr err)))))))
774
775 (when (file-remote-p filename)
776 (with-parsed-tramp-file-name filename nil
777 (tramp-flush-file-property v (file-name-directory localname))
778 (tramp-flush-file-property v localname)))
779
780 (when (file-remote-p newname)
781 (with-parsed-tramp-file-name newname nil
782 (tramp-flush-file-property v (file-name-directory localname))
783 (tramp-flush-file-property v localname))))
784
785 (defun tramp-gvfs-handle-set-file-modes (filename mode)
786 "Like `set-file-modes' for Tramp files."
787 (with-tramp-gvfs-error-message filename 'set-file-modes
788 (tramp-gvfs-fuse-file-name filename) mode))
789
790 (defun tramp-gvfs-handle-set-file-selinux-context (filename context)
791 "Like `set-file-selinux-context' for Tramp files."
792 (with-tramp-gvfs-error-message filename 'set-file-selinux-context
793 (tramp-gvfs-fuse-file-name filename) context))
794
795 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
796 "Like `set-visited-file-modtime' for Tramp files."
797 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
798 (set-visited-file-modtime time-list)))
799
800 (defun tramp-gvfs-handle-shell-command
801 (command &optional output-buffer error-buffer)
802 "Like `shell-command' for Tramp files."
803 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
804 (shell-command command output-buffer error-buffer)))
805
806 (defun tramp-gvfs-handle-start-file-process (name buffer program &rest args)
807 "Like `start-file-process' for Tramp files."
808 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
809 (apply 'start-process name buffer program args)))
810
811 (defun tramp-gvfs-handle-verify-visited-file-modtime (buf)
812 "Like `verify-visited-file-modtime' for Tramp files."
813 (with-current-buffer buf
814 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
815 (verify-visited-file-modtime buf))))
816
817 (defun tramp-gvfs-handle-write-region
818 (start end filename &optional append visit lockname confirm)
819 "Like `write-region' for Tramp files."
820 (with-parsed-tramp-file-name filename nil
821 (condition-case err
822 (with-tramp-gvfs-error-message filename 'write-region
823 start end (tramp-gvfs-fuse-file-name filename)
824 append visit lockname confirm)
825
826 ;; Error case. Let's try rename.
827 (error
828 (let ((tmpfile (tramp-compat-make-temp-file filename)))
829 (tramp-message v 4 "`write-region' failed, trying `rename-file'")
830 (write-region start end tmpfile)
831 (condition-case nil
832 (rename-file tmpfile filename)
833 (error
834 (delete-file tmpfile)
835 (tramp-error v (car err) "%s" (cdr err)))))))
836
837 ;; Set file modification time.
838 (when (or (eq visit t) (stringp visit))
839 (set-visited-file-modtime (nth 5 (file-attributes filename))))
840
841 ;; The end.
842 (when (or (eq visit t) (null visit) (stringp visit))
843 (tramp-message v 0 "Wrote %s" filename))
844 (run-hooks 'tramp-handle-write-region-hook)))
845
846 \f
847 ;; File name conversions.
848
849 (defun tramp-gvfs-url-file-name (filename)
850 "Return FILENAME in URL syntax."
851 ;; "/" must NOT be hexlified.
852 (let ((url-unreserved-chars (append '(?/) url-unreserved-chars)))
853 (url-recreate-url
854 (if (tramp-tramp-file-p filename)
855 (with-parsed-tramp-file-name (file-truename filename) nil
856 (when (string-match tramp-user-with-domain-regexp user)
857 (setq user
858 (concat (match-string 2 user) ";" (match-string 2 user))))
859 (url-parse-make-urlobj
860 method user nil
861 (tramp-file-name-real-host v) (tramp-file-name-port v)
862 (url-hexify-string localname)))
863 (url-parse-make-urlobj
864 "file" nil nil nil nil (url-hexify-string (file-truename filename)))))))
865
866 (defun tramp-gvfs-object-path (filename)
867 "Create a D-Bus object path from FILENAME."
868 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
869
870 (defun tramp-gvfs-file-name (object-path)
871 "Retrieve file name from D-Bus OBJECT-PATH."
872 (dbus-unescape-from-identifier
873 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
874
875 (defun tramp-gvfs-fuse-file-name (filename)
876 "Return FUSE file name, which is directly accessible."
877 (with-parsed-tramp-file-name (expand-file-name filename) nil
878 (tramp-gvfs-maybe-open-connection v)
879 (let ((prefix (tramp-get-file-property v "/" "prefix" ""))
880 (fuse-mountpoint
881 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
882 (unless fuse-mountpoint
883 (tramp-error
884 v 'file-error "There is no FUSE mount point for `%s'" filename))
885 ;; We must hide the prefix, if any.
886 (when (string-match (concat "^" (regexp-quote prefix)) localname)
887 (setq localname (replace-match "" t t localname)))
888 (tramp-message
889 v 10 "remote file `%s' is local file `%s'"
890 filename (concat fuse-mountpoint localname))
891 (concat fuse-mountpoint localname))))
892
893 (defun tramp-bluez-address (device)
894 "Return bluetooth device address from a given bluetooth DEVICE name."
895 (when (stringp device)
896 (if (string-match tramp-ipv6-regexp device)
897 (match-string 0 device)
898 (cadr (assoc device (tramp-bluez-list-devices))))))
899
900 (defun tramp-bluez-device (address)
901 "Return bluetooth device name from a given bluetooth device ADDRESS.
902 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
903 (when (stringp address)
904 (while (string-match "[][]" address)
905 (setq address (replace-match "" t t address)))
906 (let (result)
907 (dolist (item (tramp-bluez-list-devices) result)
908 (when (string-match address (cadr item))
909 (setq result (car item)))))))
910
911 \f
912 ;; D-Bus GVFS functions.
913
914 (defun tramp-gvfs-handler-askpassword (message user domain flags)
915 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
916 (let* ((filename
917 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
918 (pw-prompt
919 (format
920 "%s for %s "
921 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
922 (capitalize (match-string 1 message))
923 "Password")
924 filename))
925 password)
926
927 (condition-case nil
928 (with-parsed-tramp-file-name filename l
929 (when (and (zerop (length user))
930 (not
931 (zerop (logand flags tramp-gvfs-password-need-username))))
932 (setq user (read-string "User name: ")))
933 (when (and (zerop (length domain))
934 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
935 (setq domain (read-string "Domain name: ")))
936
937 (tramp-message l 6 "%S %S %S %d" message user domain flags)
938 (setq tramp-current-method l-method
939 tramp-current-user user
940 tramp-current-host l-host
941 password (tramp-read-passwd
942 (tramp-get-connection-process l) pw-prompt))
943
944 ;; Return result.
945 (if (stringp password)
946 (list
947 t ;; password handled.
948 nil ;; no abort of D-Bus.
949 password
950 (tramp-file-name-real-user l)
951 domain
952 nil ;; not anonymous.
953 0) ;; no password save.
954 ;; No password provided.
955 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
956
957 ;; When QUIT is raised, we shall return this information to D-Bus.
958 (quit (list nil t "" "" "" nil 0)))))
959
960 (defun tramp-gvfs-handler-askquestion (message choices)
961 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
962 (save-window-excursion
963 (let ((enable-recursive-minibuffers t)
964 choice)
965
966 (condition-case nil
967 (with-parsed-tramp-file-name
968 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
969 (tramp-message v 6 "%S %S" message choices)
970
971 ;; In theory, there can be several choices. Until now,
972 ;; there is only the question whether to accept an unknown
973 ;; host signature.
974 (with-temp-buffer
975 ;; Preserve message for `progress-reporter'.
976 (with-temp-message ""
977 (insert message)
978 (pop-to-buffer (current-buffer))
979 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
980 (tramp-message v 6 "%d" choice)))
981
982 ;; When the choice is "no", we set a dummy fuse-mountpoint
983 ;; in order to leave the timeout.
984 (unless (zerop choice)
985 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
986
987 (list
988 t ;; handled.
989 nil ;; no abort of D-Bus.
990 choice))
991
992 ;; When QUIT is raised, we shall return this information to D-Bus.
993 (quit (list nil t 0))))))
994
995 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
996 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
997 \"org.gtk.vfs.MountTracker.unmounted\" signals."
998 (ignore-errors
999 (let ((signal-name (dbus-event-member-name last-input-event))
1000 (elt mount-info))
1001 ;; Jump over the first elements of the mount info. Since there
1002 ;; were changes in the antries, we cannot access dedicated
1003 ;; elements.
1004 (while (stringp (car elt)) (setq elt (cdr elt)))
1005 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1006 (mount-spec (caddr elt))
1007 (default-location (dbus-byte-array-to-string (cadddr elt)))
1008 (method (dbus-byte-array-to-string
1009 (cadr (assoc "type" (cadr mount-spec)))))
1010 (user (dbus-byte-array-to-string
1011 (cadr (assoc "user" (cadr mount-spec)))))
1012 (domain (dbus-byte-array-to-string
1013 (cadr (assoc "domain" (cadr mount-spec)))))
1014 (host (dbus-byte-array-to-string
1015 (cadr (or (assoc "host" (cadr mount-spec))
1016 (assoc "server" (cadr mount-spec))))))
1017 (port (dbus-byte-array-to-string
1018 (cadr (assoc "port" (cadr mount-spec)))))
1019 (ssl (dbus-byte-array-to-string
1020 (cadr (assoc "ssl" (cadr mount-spec)))))
1021 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1022 (dbus-byte-array-to-string
1023 (cadr (assoc "share" (cadr mount-spec)))))))
1024 (when (string-match "^smb" method)
1025 (setq method "smb"))
1026 (when (string-equal "obex" method)
1027 (setq host (tramp-bluez-device host)))
1028 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1029 (setq method "davs"))
1030 (unless (zerop (length domain))
1031 (setq user (concat user tramp-prefix-domain-format domain)))
1032 (unless (zerop (length port))
1033 (setq host (concat host tramp-prefix-port-format port)))
1034 (with-parsed-tramp-file-name
1035 (tramp-make-tramp-file-name method user host "") nil
1036 (tramp-message
1037 v 6 "%s %s"
1038 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1039 (tramp-set-file-property v "/" "list-mounts" 'undef)
1040 (if (string-equal signal-name "unmounted")
1041 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1042 ;; Set prefix, mountpoint and location.
1043 (unless (string-equal prefix "/")
1044 (tramp-set-file-property v "/" "prefix" prefix))
1045 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1046 (tramp-set-file-property
1047 v "/" "default-location" default-location)))))))
1048
1049 (dbus-register-signal
1050 :session nil tramp-gvfs-path-mounttracker
1051 tramp-gvfs-interface-mounttracker "mounted"
1052 'tramp-gvfs-handler-mounted-unmounted)
1053
1054 (dbus-register-signal
1055 :session nil tramp-gvfs-path-mounttracker
1056 tramp-gvfs-interface-mounttracker "unmounted"
1057 'tramp-gvfs-handler-mounted-unmounted)
1058
1059 (defun tramp-gvfs-connection-mounted-p (vec)
1060 "Check, whether the location is already mounted."
1061 (or
1062 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1063 (catch 'mounted
1064 (dolist
1065 (elt
1066 (with-file-property vec "/" "list-mounts"
1067 (with-tramp-dbus-call-method vec t
1068 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1069 tramp-gvfs-interface-mounttracker "listMounts"))
1070 nil)
1071 ;; Jump over the first elements of the mount info. Since there
1072 ;; were changes in the antries, we cannot access dedicated
1073 ;; elements.
1074 (while (stringp (car elt)) (setq elt (cdr elt)))
1075 (let* ((fuse-mountpoint (dbus-byte-array-to-string (cadr elt)))
1076 (mount-spec (caddr elt))
1077 (default-location (dbus-byte-array-to-string (cadddr elt)))
1078 (method (dbus-byte-array-to-string
1079 (cadr (assoc "type" (cadr mount-spec)))))
1080 (user (dbus-byte-array-to-string
1081 (cadr (assoc "user" (cadr mount-spec)))))
1082 (domain (dbus-byte-array-to-string
1083 (cadr (assoc "domain" (cadr mount-spec)))))
1084 (host (dbus-byte-array-to-string
1085 (cadr (or (assoc "host" (cadr mount-spec))
1086 (assoc "server" (cadr mount-spec))))))
1087 (port (dbus-byte-array-to-string
1088 (cadr (assoc "port" (cadr mount-spec)))))
1089 (ssl (dbus-byte-array-to-string
1090 (cadr (assoc "ssl" (cadr mount-spec)))))
1091 (prefix (concat (dbus-byte-array-to-string (car mount-spec))
1092 (dbus-byte-array-to-string
1093 (cadr (assoc "share" (cadr mount-spec)))))))
1094 (when (string-match "^smb" method)
1095 (setq method "smb"))
1096 (when (string-equal "obex" method)
1097 (setq host (tramp-bluez-device host)))
1098 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1099 (setq method "davs"))
1100 (when (and (string-equal "synce" method) (zerop (length user)))
1101 (setq user (or (tramp-file-name-user vec) "")))
1102 (unless (zerop (length domain))
1103 (setq user (concat user tramp-prefix-domain-format domain)))
1104 (unless (zerop (length port))
1105 (setq host (concat host tramp-prefix-port-format port)))
1106 (when (and
1107 (string-equal method (tramp-file-name-method vec))
1108 (string-equal user (or (tramp-file-name-user vec) ""))
1109 (string-equal host (tramp-file-name-host vec))
1110 (string-match (concat "^" (regexp-quote prefix))
1111 (tramp-file-name-localname vec)))
1112 ;; Set prefix, mountpoint and location.
1113 (unless (string-equal prefix "/")
1114 (tramp-set-file-property vec "/" "prefix" prefix))
1115 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1116 (tramp-set-file-property vec "/" "default-location" default-location)
1117 (throw 'mounted t)))))))
1118
1119 (defun tramp-gvfs-mount-spec (vec)
1120 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1121 (let* ((method (tramp-file-name-method vec))
1122 (user (tramp-file-name-real-user vec))
1123 (domain (tramp-file-name-domain vec))
1124 (host (tramp-file-name-real-host vec))
1125 (port (tramp-file-name-port vec))
1126 (localname (tramp-file-name-localname vec))
1127 (ssl (if (string-match "^davs" method) "true" "false"))
1128 (mount-spec '(:array))
1129 (mount-pref "/"))
1130
1131 (setq
1132 mount-spec
1133 (append
1134 mount-spec
1135 (cond
1136 ((string-equal "smb" method)
1137 (string-match "^/?\\([^/]+\\)" localname)
1138 `((:struct "type" ,(dbus-string-to-byte-array "smb-share"))
1139 (:struct "server" ,(dbus-string-to-byte-array host))
1140 (:struct "share" ,(dbus-string-to-byte-array
1141 (match-string 1 localname)))))
1142 ((string-equal "obex" method)
1143 `((:struct "type" ,(dbus-string-to-byte-array method))
1144 (:struct "host" ,(dbus-string-to-byte-array
1145 (concat "[" (tramp-bluez-address host) "]")))))
1146 ((string-match "^dav" method)
1147 `((:struct "type" ,(dbus-string-to-byte-array "dav"))
1148 (:struct "host" ,(dbus-string-to-byte-array host))
1149 (:struct "ssl" ,(dbus-string-to-byte-array ssl))))
1150 (t
1151 `((:struct "type" ,(dbus-string-to-byte-array method))
1152 (:struct "host" ,(dbus-string-to-byte-array host)))))))
1153
1154 (when user
1155 (add-to-list
1156 'mount-spec
1157 `(:struct "user" ,(dbus-string-to-byte-array user))
1158 'append))
1159
1160 (when domain
1161 (add-to-list
1162 'mount-spec
1163 `(:struct "domain" ,(dbus-string-to-byte-array domain))
1164 'append))
1165
1166 (when port
1167 (add-to-list
1168 'mount-spec
1169 `(:struct "port" ,(dbus-string-to-byte-array (number-to-string port)))
1170 'append))
1171
1172 (when (and (string-match "^dav" method)
1173 (string-match "^/?[^/]+" localname))
1174 (setq mount-pref (match-string 0 localname)))
1175
1176 ;; Return.
1177 `(:struct ,(dbus-string-to-byte-array mount-pref) ,mount-spec)))
1178
1179 \f
1180 ;; Connection functions
1181
1182 (defun tramp-gvfs-maybe-open-connection (vec)
1183 "Maybe open a connection VEC.
1184 Does not do anything if a connection is already open, but re-opens the
1185 connection if a previous connection has died for some reason."
1186
1187 ;; We set the file name, in case there are incoming D-Bus signals or
1188 ;; D-Bus errors.
1189 (setq tramp-gvfs-dbus-event-vector vec)
1190
1191 ;; For password handling, we need a process bound to the connection
1192 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1193 ;; better solution?
1194 (unless (get-buffer-process (tramp-get-buffer vec))
1195 (let ((p (make-network-process
1196 :name (tramp-buffer-name vec)
1197 :buffer (tramp-get-buffer vec)
1198 :server t :host 'local :service t)))
1199 (tramp-set-process-query-on-exit-flag p nil)))
1200
1201 (unless (tramp-gvfs-connection-mounted-p vec)
1202 (let* ((method (tramp-file-name-method vec))
1203 (user (tramp-file-name-user vec))
1204 (host (tramp-file-name-host vec))
1205 (object-path
1206 (tramp-gvfs-object-path
1207 (tramp-make-tramp-file-name method user host ""))))
1208
1209 (with-progress-reporter
1210 vec 3
1211 (if (zerop (length user))
1212 (format "Opening connection for %s using %s" host method)
1213 (format "Opening connection for %s@%s using %s" user host method))
1214
1215 ;; Enable auth-sorce and password-cache.
1216 (tramp-set-connection-property vec "first-password-request" t)
1217
1218 ;; There will be a callback of "askPassword", when a password is
1219 ;; needed.
1220 (dbus-register-method
1221 :session dbus-service-emacs object-path
1222 tramp-gvfs-interface-mountoperation "askPassword"
1223 'tramp-gvfs-handler-askpassword)
1224
1225 ;; There could be a callback of "askQuestion", when adding fingerprint.
1226 (dbus-register-method
1227 :session dbus-service-emacs object-path
1228 tramp-gvfs-interface-mountoperation "askQuestion"
1229 'tramp-gvfs-handler-askquestion)
1230
1231 ;; The call must be asynchronously, because of the "askPassword"
1232 ;; or "askQuestion"callbacks.
1233 (with-tramp-dbus-call-method vec nil
1234 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1235 tramp-gvfs-interface-mounttracker "mountLocation"
1236 (tramp-gvfs-mount-spec vec) (dbus-get-unique-name :session)
1237 :object-path object-path)
1238
1239 ;; We must wait, until the mount is applied. This will be
1240 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1241 ;; file property.
1242 (with-timeout
1243 (60
1244 (if (zerop (length (tramp-file-name-user vec)))
1245 (tramp-error
1246 vec 'file-error
1247 "Timeout reached mounting %s using %s" host method)
1248 (tramp-error
1249 vec 'file-error
1250 "Timeout reached mounting %s@%s using %s" user host method)))
1251 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1252 (read-event nil nil 0.1)))
1253
1254 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1255 ;; is marked with the fuse-mountpoint "/". We shall react.
1256 (when (string-equal
1257 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1258 (tramp-error vec 'file-error "FUSE mount denied"))
1259
1260 ;; We set the connection property "started" in order to put the
1261 ;; remote location into the cache, which is helpful for further
1262 ;; completion.
1263 (tramp-set-connection-property vec "started" t)))))
1264
1265 (defun tramp-gvfs-send-command (vec command &rest args)
1266 "Send the COMMAND with its ARGS to connection VEC.
1267 COMMAND is usually a command from the gvfs-* utilities.
1268 `call-process' is applied, and its return code is returned."
1269 (let (result)
1270 (with-current-buffer (tramp-get-buffer vec)
1271 (erase-buffer)
1272 (tramp-message vec 6 "%s %s" command (mapconcat 'identity args " "))
1273 (setq result (apply 'tramp-local-call-process command nil t nil args))
1274 (tramp-message vec 6 "%s" (buffer-string))
1275 result)))
1276
1277 \f
1278 ;; D-Bus BLUEZ functions.
1279
1280 (defun tramp-bluez-list-devices ()
1281 "Return all discovered bluetooth devices as list.
1282 Every entry is a list (NAME ADDRESS).
1283
1284 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1285 discovery happened more time before indicated there, a rescan will be
1286 started, which lasts some ten seconds. Otherwise, cached results will
1287 be used."
1288 ;; Reset the scanned devices list if time has passed.
1289 (and (integerp tramp-bluez-discover-devices-timeout)
1290 (integerp tramp-bluez-discovery)
1291 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1292 tramp-bluez-discover-devices-timeout)
1293 (setq tramp-bluez-devices nil))
1294
1295 ;; Rescan if needed.
1296 (unless tramp-bluez-devices
1297 (let ((object-path
1298 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1299 :system tramp-bluez-service "/"
1300 tramp-bluez-interface-manager "DefaultAdapter")))
1301 (setq tramp-bluez-devices nil
1302 tramp-bluez-discovery t)
1303 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1304 :system tramp-bluez-service object-path
1305 tramp-bluez-interface-adapter "StartDiscovery")
1306 (while tramp-bluez-discovery
1307 (read-event nil nil 0.1))))
1308 (setq tramp-bluez-discovery (current-time))
1309 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1310 tramp-bluez-devices)
1311
1312 (defun tramp-bluez-property-changed (property value)
1313 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1314 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1315 (cond
1316 ((string-equal property "Discovering")
1317 (unless (car value)
1318 ;; "Discovering" FALSE means discovery run has been completed.
1319 ;; We stop it, because we don't need another run.
1320 (setq tramp-bluez-discovery nil)
1321 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1322 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1323 tramp-bluez-interface-adapter "StopDiscovery")))))
1324
1325 (dbus-register-signal
1326 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1327 'tramp-bluez-property-changed)
1328
1329 (defun tramp-bluez-device-found (device args)
1330 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1331 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1332 (let ((alias (car (cadr (assoc "Alias" args))))
1333 (address (car (cadr (assoc "Address" args)))))
1334 ;; Maybe we shall check the device class for being a proper
1335 ;; device, and call also SDP in order to find the obex service.
1336 (add-to-list 'tramp-bluez-devices (list alias address))))
1337
1338 (dbus-register-signal
1339 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1340 'tramp-bluez-device-found)
1341
1342 (defun tramp-bluez-parse-device-names (ignore)
1343 "Return a list of (nil host) tuples allowed to access."
1344 (mapcar
1345 (lambda (x) (list nil (car x)))
1346 (tramp-bluez-list-devices)))
1347
1348 ;; Add completion function for OBEX method.
1349 (when (member tramp-bluez-service (dbus-list-known-names :system))
1350 (tramp-set-completion-function
1351 "obex" '((tramp-bluez-parse-device-names ""))))
1352
1353 \f
1354 ;; D-Bus zeroconf functions.
1355
1356 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1357 "Return a list of (user host) tuples allowed to access."
1358 (mapcar
1359 (lambda (x)
1360 (list nil (zeroconf-service-host x)))
1361 (zeroconf-list-services "_workstation._tcp")))
1362
1363 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1364 "Return a list of (user host) tuples allowed to access."
1365 (mapcar
1366 (lambda (x)
1367 (let ((host (zeroconf-service-host x))
1368 (port (zeroconf-service-port x))
1369 (text (zeroconf-service-txt x))
1370 user)
1371 (when port
1372 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1373 ;; A user is marked in a TXT field like "u=guest".
1374 (while text
1375 (when (string-match "u=\\(.+\\)$" (car text))
1376 (setq user (match-string 1 (car text))))
1377 (setq text (cdr text)))
1378 (list user host)))
1379 (zeroconf-list-services "_webdav._tcp")))
1380
1381 ;; Add completion function for DAV and DAVS methods.
1382 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1383 (zeroconf-init tramp-gvfs-zeroconf-domain)
1384 (tramp-set-completion-function
1385 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1386 (tramp-set-completion-function
1387 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1388 (tramp-set-completion-function
1389 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1390
1391 \f
1392 ;; D-Bus SYNCE functions.
1393
1394 (defun tramp-synce-list-devices ()
1395 "Return all discovered synce devices as list.
1396 They are retrieved from the hal daemon."
1397 (let (tramp-synce-devices)
1398 (dolist (device
1399 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1400 :system tramp-hal-service tramp-hal-path-manager
1401 tramp-hal-interface-manager "GetAllDevices"))
1402 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1403 :system tramp-hal-service device tramp-hal-interface-device
1404 "PropertyExists" "sync.plugin")
1405 (add-to-list
1406 'tramp-synce-devices
1407 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1408 :system tramp-hal-service device tramp-hal-interface-device
1409 "GetPropertyString" "pda.pocketpc.name"))))
1410 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1411 tramp-synce-devices))
1412
1413 (defun tramp-synce-parse-device-names (ignore)
1414 "Return a list of (nil host) tuples allowed to access."
1415 (mapcar
1416 (lambda (x) (list nil x))
1417 (tramp-synce-list-devices)))
1418
1419 ;; Add completion function for SYNCE method.
1420 (tramp-set-completion-function
1421 "synce" '((tramp-synce-parse-device-names "")))
1422
1423 (add-hook 'tramp-unload-hook
1424 (lambda ()
1425 (unload-feature 'tramp-gvfs 'force)))
1426
1427 (provide 'tramp-gvfs)
1428
1429 ;;; TODO:
1430
1431 ;; * Host name completion via smb-server or smb-network.
1432 ;; * Check, how two shares of the same SMB server can be mounted in
1433 ;; parallel.
1434 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1435 ;; capability.
1436 ;; * Implement obex for other serial communication but bluetooth.
1437
1438 ;; arch-tag: f7f660ce-77f4-4132-9663-f5c25a47f7ed
1439 ;;; tramp-gvfs.el ends here