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