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