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