]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-gvfs.el
Add "afp" method to 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 '("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' performed by default handler.
434 (file-executable-p . tramp-gvfs-handle-file-executable-p)
435 (file-exists-p . tramp-handle-file-exists-p)
436 ;; `file-in-directory-p' performed by default handler.
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:\\s-+directory" nil t) t))
812 (goto-char (point-min))
813 (setq res-symlink-target
814 (if (re-search-forward
815 "standard::symlink-target:\\s-+\\(.*\\)$" nil t)
816 (match-string 1)))
817 ;; ... number links
818 (goto-char (point-min))
819 (setq res-numlinks
820 (if (re-search-forward
821 "unix::nlink:\\s-+\\([0-9]+\\)" nil t)
822 (string-to-number (match-string 1)) 0))
823 ;; ... uid and gid
824 (goto-char (point-min))
825 (setq res-uid
826 (or (if (eq id-format 'integer)
827 (if (re-search-forward
828 "unix::uid:\\s-+\\([0-9]+\\)" nil t)
829 (string-to-number (match-string 1)))
830 (if (and
831 (re-search-forward "owner::user:\\s-+" nil t)
832 (re-search-forward "(\\S-+\\)" (point-at-eol) 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 (and
841 (re-search-forward "owner::group:\\s-+" nil t)
842 (re-search-forward "(\\S-+\\)" (point-at-eol) t))
843 (match-string 1)))
844 (tramp-get-local-gid id-format)))
845 ;; ... last access, modification and change time
846 (goto-char (point-min))
847 (setq res-access
848 (if (re-search-forward
849 "time::access:\\s-+\\([0-9]+\\)" nil t)
850 (seconds-to-time (string-to-number (match-string 1)))
851 '(0 0)))
852 (goto-char (point-min))
853 (setq res-mod
854 (if (re-search-forward
855 "time::modified:\\s-+\\([0-9]+\\)" nil t)
856 (seconds-to-time (string-to-number (match-string 1)))
857 '(0 0)))
858 (goto-char (point-min))
859 (setq res-change
860 (if (re-search-forward
861 "time::changed:\\s-+\\([0-9]+\\)" nil t)
862 (seconds-to-time (string-to-number (match-string 1)))
863 '(0 0)))
864 ;; ... size
865 (goto-char (point-min))
866 (setq res-size
867 (if (re-search-forward
868 "standard::size:\\s-+\\([0-9]+\\)" nil t)
869 (string-to-number (match-string 1)) 0))
870 ;; ... file mode flags
871 (goto-char (point-min))
872 (setq res-filemodes
873 (if (re-search-forward "unix::mode:\\s-+\\([0-9]+\\)" nil t)
874 (tramp-file-mode-from-int
875 (string-to-number (match-string 1)))
876 (if dirp "drwx------" "-rwx------")))
877 ;; ... inode and device
878 (goto-char (point-min))
879 (setq res-inode
880 (if (re-search-forward
881 "unix::inode:\\s-+\\([0-9]+\\)" nil t)
882 (string-to-number (match-string 1))
883 (tramp-get-inode v)))
884 (goto-char (point-min))
885 (setq res-device
886 (if (re-search-forward
887 "unix::device:\\s-+\\([0-9]+\\)" nil t)
888 (string-to-number (match-string 1))
889 (tramp-get-device v)))
890
891 ;; Return data gathered.
892 (list
893 ;; 0. t for directory, string (name linked to) for
894 ;; symbolic link, or nil.
895 (or dirp res-symlink-target)
896 ;; 1. Number of links to file.
897 res-numlinks
898 ;; 2. File uid.
899 res-uid
900 ;; 3. File gid.
901 res-gid
902 ;; 4. Last access time, as a list of integers.
903 ;; 5. Last modification time, likewise.
904 ;; 6. Last status change time, likewise.
905 res-access res-mod res-change
906 ;; 7. Size in bytes (-1, if number is out of range).
907 res-size
908 ;; 8. File modes.
909 res-filemodes
910 ;; 9. t if file's gid would change if file were deleted
911 ;; and recreated.
912 nil
913 ;; 10. Inode number.
914 res-inode
915 ;; 11. Device number.
916 res-device
917 ))))))))
918
919 (defun tramp-gvfs-handle-file-directory-p (filename)
920 "Like `file-directory-p' for Tramp files."
921 (eq t (car (file-attributes filename))))
922
923 (defun tramp-gvfs-handle-file-executable-p (filename)
924 "Like `file-executable-p' for Tramp files."
925 (with-parsed-tramp-file-name filename nil
926 (with-tramp-file-property v localname "file-executable-p"
927 (tramp-check-cached-permissions v ?x))))
928
929 (defun tramp-gvfs-handle-file-local-copy (filename)
930 "Like `file-local-copy' for Tramp files."
931 (with-parsed-tramp-file-name filename nil
932 (let ((tmpfile (tramp-compat-make-temp-file filename)))
933 (unless (file-exists-p filename)
934 (tramp-error
935 v 'file-error
936 "Cannot make local copy of non-existing file `%s'" filename))
937 (copy-file filename tmpfile t t)
938 tmpfile)))
939
940 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
941 "Like `file-name-all-completions' for Tramp files."
942 (unless (save-match-data (string-match "/" filename))
943 (with-parsed-tramp-file-name (expand-file-name directory) nil
944
945 (all-completions
946 filename
947 (mapcar
948 'list
949 (or
950 ;; Try cache entries for filename, filename with last
951 ;; character removed, filename with last two characters
952 ;; removed, ..., and finally the empty string - all
953 ;; concatenated to the local directory name.
954 (let ((remote-file-name-inhibit-cache
955 (or remote-file-name-inhibit-cache
956 tramp-completion-reread-directory-timeout)))
957
958 ;; This is inefficient for very long filenames, pity
959 ;; `reduce' is not available...
960 (car
961 (apply
962 'append
963 (mapcar
964 (lambda (x)
965 (let ((cache-hit
966 (tramp-get-file-property
967 v
968 (concat localname (substring filename 0 x))
969 "file-name-all-completions"
970 nil)))
971 (when cache-hit (list cache-hit))))
972 ;; We cannot use a length of 0, because file properties
973 ;; for "foo" and "foo/" are identical.
974 (tramp-compat-number-sequence (length filename) 1 -1)))))
975
976 ;; Cache expired or no matching cache entry found so we need
977 ;; to perform a remote operation.
978 (let ((result '("." ".."))
979 entry)
980 ;; Get a list of directories and files.
981 (tramp-gvfs-send-command
982 v "gvfs-ls" "-h" (tramp-gvfs-url-file-name directory))
983
984 ;; Now grab the output.
985 (with-temp-buffer
986 (insert-buffer-substring (tramp-get-connection-buffer v))
987 (goto-char (point-max))
988 (while (zerop (forward-line -1))
989 (setq entry (buffer-substring (point) (point-at-eol)))
990 (when (string-match filename entry)
991 (if (file-directory-p (expand-file-name entry directory))
992 (push (concat entry "/") result)
993 (push entry result)))))
994
995 ;; Because the remote op went through OK we know the
996 ;; directory we `cd'-ed to exists.
997 (tramp-set-file-property v localname "file-exists-p" t)
998
999 ;; Because the remote op went through OK we know every
1000 ;; file listed by `ls' exists.
1001 (mapc (lambda (entry)
1002 (tramp-set-file-property
1003 v (concat localname entry) "file-exists-p" t))
1004 result)
1005
1006 ;; Store result in the cache.
1007 (tramp-set-file-property
1008 v (concat localname filename)
1009 "file-name-all-completions" result))))))))
1010
1011 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
1012 "Like `file-notify-add-watch' for Tramp files."
1013 (setq file-name (expand-file-name file-name))
1014 (with-parsed-tramp-file-name file-name nil
1015 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1016 ;; supported for gvfs-mounted directories.
1017 (when (file-directory-p file-name)
1018 (tramp-error
1019 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1020 (let* ((default-directory (file-name-directory file-name))
1021 (events
1022 (cond
1023 ((and (memq 'change flags) (memq 'attribute-change flags))
1024 '(created changed changes-done-hint moved deleted
1025 attribute-changed))
1026 ((memq 'change flags)
1027 '(created changed changes-done-hint moved deleted))
1028 ((memq 'attribute-change flags) '(attribute-changed))))
1029 (p (start-process
1030 "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
1031 "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name))))
1032 (if (not (processp p))
1033 (tramp-error
1034 v 'file-notify-error "Monitoring not supported for `%s'" file-name)
1035 (tramp-message
1036 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
1037 (tramp-set-connection-property p "vector" v)
1038 (tramp-compat-process-put p 'events events)
1039 (tramp-compat-process-put p 'watch-name localname)
1040 (tramp-compat-set-process-query-on-exit-flag p nil)
1041 (set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
1042 ;; There might be an error if the monitor is not supported.
1043 ;; Give the filter a chance to read the output.
1044 (tramp-accept-process-output p 1)
1045 (unless (memq (process-status p) '(run open))
1046 (tramp-error
1047 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1048 p))))
1049
1050 (defun tramp-gvfs-monitor-file-process-filter (proc string)
1051 "Read output from \"gvfs-monitor-file\" and add corresponding \
1052 file-notify events."
1053 (let* ((rest-string (tramp-compat-process-get proc 'rest-string))
1054 (dd (with-current-buffer (process-buffer proc) default-directory))
1055 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
1056 (when rest-string
1057 (tramp-message proc 10 "Previous string:\n%s" rest-string))
1058 (tramp-message proc 6 "%S\n%s" proc string)
1059 (setq string (concat rest-string string)
1060 ;; Attribute change is returned in unused wording.
1061 string (tramp-compat-replace-regexp-in-string
1062 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1063 (when (string-match "Monitoring not supported" string)
1064 (delete-process proc))
1065
1066 (while (string-match
1067 (concat "^[\n\r]*"
1068 "File Monitor Event:[\n\r]+"
1069 "File = \\([^\n\r]+\\)[\n\r]+"
1070 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1071 string)
1072 (let ((file (match-string 1 string))
1073 (action (intern-soft
1074 (tramp-compat-replace-regexp-in-string
1075 "_" "-" (downcase (match-string 2 string))))))
1076 (setq string (replace-match "" nil nil string))
1077 ;; File names are returned as URL paths. We must convert them.
1078 (when (string-match ddu file)
1079 (setq file (replace-match dd nil nil file)))
1080 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
1081 (setq file
1082 (replace-match
1083 (char-to-string (string-to-number (match-string 1 file) 16))
1084 nil nil file)))
1085 ;; Usually, we would add an Emacs event now. Unfortunately,
1086 ;; `unread-command-events' does not accept several events at
1087 ;; once. Therefore, we apply the callback directly.
1088 (tramp-compat-funcall 'file-notify-callback (list proc action file))))
1089
1090 ;; Save rest of the string.
1091 (when (zerop (length string)) (setq string nil))
1092 (when string (tramp-message proc 10 "Rest string:\n%s" string))
1093 (tramp-compat-process-put proc 'rest-string string)))
1094
1095 (defun tramp-gvfs-handle-file-readable-p (filename)
1096 "Like `file-readable-p' for Tramp files."
1097 (with-parsed-tramp-file-name filename nil
1098 (with-tramp-file-property v localname "file-executable-p"
1099 (tramp-check-cached-permissions v ?r))))
1100
1101 (defun tramp-gvfs-handle-file-writable-p (filename)
1102 "Like `file-writable-p' for Tramp files."
1103 (with-parsed-tramp-file-name filename nil
1104 (with-tramp-file-property v localname "file-writable-p"
1105 (if (file-exists-p filename)
1106 (tramp-check-cached-permissions v ?w)
1107 ;; If file doesn't exist, check if directory is writable.
1108 (and (file-directory-p (file-name-directory filename))
1109 (file-writable-p (file-name-directory filename)))))))
1110
1111 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1112 "Like `make-directory' for Tramp files."
1113 (setq dir (directory-file-name (expand-file-name dir)))
1114 (with-parsed-tramp-file-name dir nil
1115 (tramp-flush-file-property v (file-name-directory localname))
1116 (tramp-flush-directory-property v localname)
1117 (save-match-data
1118 (let ((ldir (file-name-directory dir)))
1119 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1120 ;; work robust.
1121 (when (and parents (not (file-directory-p ldir)))
1122 (make-directory ldir parents))
1123 ;; Just do it.
1124 (unless (tramp-gvfs-send-command
1125 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1126 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1127
1128 (defun tramp-gvfs-handle-rename-file
1129 (filename newname &optional ok-if-already-exists)
1130 "Like `rename-file' for Tramp files."
1131 ;; Check if both files are local -- invoke normal rename-file.
1132 ;; Otherwise, use Tramp from local system.
1133 (setq filename (expand-file-name filename))
1134 (setq newname (expand-file-name newname))
1135 ;; At least one file a Tramp file?
1136 (if (or (tramp-tramp-file-p filename)
1137 (tramp-tramp-file-p newname))
1138 (tramp-gvfs-do-copy-or-rename-file
1139 'rename filename newname ok-if-already-exists t t)
1140 (tramp-run-real-handler
1141 'rename-file (list filename newname ok-if-already-exists))))
1142
1143 (defun tramp-gvfs-handle-write-region
1144 (start end filename &optional append visit lockname confirm)
1145 "Like `write-region' for Tramp files."
1146 (with-parsed-tramp-file-name filename nil
1147 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1148 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
1149 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1150 (tramp-error v 'file-error "File not overwritten")))
1151
1152 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1153 (when (and append (file-exists-p filename))
1154 (copy-file filename tmpfile 'ok))
1155 ;; We say `no-message' here because we don't want the visited file
1156 ;; modtime data to be clobbered from the temp file. We call
1157 ;; `set-visited-file-modtime' ourselves later on.
1158 (tramp-run-real-handler
1159 'write-region
1160 (if confirm ; don't pass this arg unless defined for backward compat.
1161 (list start end tmpfile append 'no-message lockname confirm)
1162 (list start end tmpfile append 'no-message lockname)))
1163 (condition-case nil
1164 (rename-file tmpfile filename 'ok-if-already-exists)
1165 (error
1166 (delete-file tmpfile)
1167 (tramp-error
1168 v 'file-error "Couldn't write region to `%s'" filename))))
1169
1170 (tramp-flush-file-property v (file-name-directory localname))
1171 (tramp-flush-file-property v localname)
1172
1173 ;; Set file modification time.
1174 (when (or (eq visit t) (stringp visit))
1175 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1176
1177 ;; The end.
1178 (when (or (eq visit t) (null visit) (stringp visit))
1179 (tramp-message v 0 "Wrote %s" filename))
1180 (run-hooks 'tramp-handle-write-region-hook)))
1181
1182 \f
1183 ;; File name conversions.
1184
1185 (defun tramp-gvfs-url-file-name (filename)
1186 "Return FILENAME in URL syntax."
1187 ;; "/" must NOT be hexlified.
1188 (let ((url-unreserved-chars (cons ?/ url-unreserved-chars))
1189 result)
1190 (setq
1191 result
1192 (url-recreate-url
1193 (if (tramp-tramp-file-p filename)
1194 (with-parsed-tramp-file-name filename nil
1195 (when (and user (string-match tramp-user-with-domain-regexp user))
1196 (setq user
1197 (concat (match-string 2 user) ";" (match-string 1 user))))
1198 (url-parse-make-urlobj
1199 method (and user (url-hexify-string user)) nil
1200 (tramp-file-name-real-host v) (tramp-file-name-port v)
1201 (and localname (url-hexify-string localname)) nil nil t))
1202 (url-parse-make-urlobj
1203 "file" nil nil nil nil
1204 (url-hexify-string (file-truename filename)) nil nil t))))
1205 (when (tramp-tramp-file-p filename)
1206 (with-parsed-tramp-file-name filename nil
1207 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1208 result))
1209
1210 (defun tramp-gvfs-object-path (filename)
1211 "Create a D-Bus object path from FILENAME."
1212 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1213
1214 (defun tramp-gvfs-file-name (object-path)
1215 "Retrieve file name from D-Bus OBJECT-PATH."
1216 (dbus-unescape-from-identifier
1217 (tramp-compat-replace-regexp-in-string
1218 "^.*/\\([^/]+\\)$" "\\1" object-path)))
1219
1220 (defun tramp-bluez-address (device)
1221 "Return bluetooth device address from a given bluetooth DEVICE name."
1222 (when (stringp device)
1223 (if (string-match tramp-ipv6-regexp device)
1224 (match-string 0 device)
1225 (cadr (assoc device (tramp-bluez-list-devices))))))
1226
1227 (defun tramp-bluez-device (address)
1228 "Return bluetooth device name from a given bluetooth device ADDRESS.
1229 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1230 (when (stringp address)
1231 (while (string-match "[][]" address)
1232 (setq address (replace-match "" t t address)))
1233 (let (result)
1234 (dolist (item (tramp-bluez-list-devices) result)
1235 (when (string-match address (cadr item))
1236 (setq result (car item)))))))
1237
1238 \f
1239 ;; D-Bus GVFS functions.
1240
1241 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1242 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1243 (let* ((filename
1244 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1245 (pw-prompt
1246 (format
1247 "%s for %s "
1248 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1249 (capitalize (match-string 1 message))
1250 "Password")
1251 filename))
1252 password)
1253
1254 (condition-case nil
1255 (with-parsed-tramp-file-name filename l
1256 (when (and (zerop (length user))
1257 (not
1258 (zerop (logand flags tramp-gvfs-password-need-username))))
1259 (setq user (read-string "User name: ")))
1260 (when (and (zerop (length domain))
1261 (not
1262 (zerop (logand flags tramp-gvfs-password-need-domain))))
1263 (setq domain (read-string "Domain name: ")))
1264
1265 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1266 (unless (tramp-get-connection-property l "first-password-request" nil)
1267 (tramp-clear-passwd l))
1268
1269 (setq tramp-current-method l-method
1270 tramp-current-user user
1271 tramp-current-host l-host
1272 password (tramp-read-passwd
1273 (tramp-get-connection-process l) pw-prompt))
1274
1275 ;; Return result.
1276 (if (stringp password)
1277 (list
1278 t ;; password handled.
1279 nil ;; no abort of D-Bus.
1280 password
1281 (tramp-file-name-real-user l)
1282 domain
1283 nil ;; not anonymous.
1284 0) ;; no password save.
1285 ;; No password provided.
1286 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1287
1288 ;; When QUIT is raised, we shall return this information to D-Bus.
1289 (quit (list nil t "" "" "" nil 0)))))
1290
1291 (defun tramp-gvfs-handler-askquestion (message choices)
1292 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1293 (save-window-excursion
1294 (let ((enable-recursive-minibuffers t)
1295 choice)
1296
1297 (condition-case nil
1298 (with-parsed-tramp-file-name
1299 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1300 (tramp-message v 6 "%S %S" message choices)
1301
1302 ;; In theory, there can be several choices. Until now,
1303 ;; there is only the question whether to accept an unknown
1304 ;; host signature.
1305 (with-temp-buffer
1306 ;; Preserve message for `progress-reporter'.
1307 (tramp-compat-with-temp-message ""
1308 (insert message)
1309 (pop-to-buffer (current-buffer))
1310 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1311 (tramp-message v 6 "%d" choice)))
1312
1313 ;; When the choice is "no", we set a dummy fuse-mountpoint
1314 ;; in order to leave the timeout.
1315 (unless (zerop choice)
1316 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1317
1318 (list
1319 t ;; handled.
1320 nil ;; no abort of D-Bus.
1321 choice))
1322
1323 ;; When QUIT is raised, we shall return this information to D-Bus.
1324 (quit (list nil t 0))))))
1325
1326 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1327 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1328 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1329 (ignore-errors
1330 (let ((signal-name (dbus-event-member-name last-input-event))
1331 (elt mount-info))
1332 ;; Jump over the first elements of the mount info. Since there
1333 ;; were changes in the entries, we cannot access dedicated
1334 ;; elements.
1335 (while (stringp (car elt)) (setq elt (cdr elt)))
1336 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1337 (mount-spec (caddr elt))
1338 (default-location (tramp-gvfs-dbus-byte-array-to-string
1339 (cadddr elt)))
1340 (method (tramp-gvfs-dbus-byte-array-to-string
1341 (cadr (assoc "type" (cadr mount-spec)))))
1342 (user (tramp-gvfs-dbus-byte-array-to-string
1343 (cadr (assoc "user" (cadr mount-spec)))))
1344 (domain (tramp-gvfs-dbus-byte-array-to-string
1345 (cadr (assoc "domain" (cadr mount-spec)))))
1346 (host (tramp-gvfs-dbus-byte-array-to-string
1347 (cadr (or (assoc "host" (cadr mount-spec))
1348 (assoc "server" (cadr mount-spec))))))
1349 (port (tramp-gvfs-dbus-byte-array-to-string
1350 (cadr (assoc "port" (cadr mount-spec)))))
1351 (ssl (tramp-gvfs-dbus-byte-array-to-string
1352 (cadr (assoc "ssl" (cadr mount-spec)))))
1353 (prefix (concat
1354 (tramp-gvfs-dbus-byte-array-to-string
1355 (car mount-spec))
1356 (tramp-gvfs-dbus-byte-array-to-string
1357 (or (cadr (assoc "share" (cadr mount-spec)))
1358 (cadr (assoc "volume" (cadr mount-spec))))))))
1359 (when (string-match "^\\(afp\\|smb\\)" method)
1360 (setq method (match-string 1 method)))
1361 (when (string-equal "obex" method)
1362 (setq host (tramp-bluez-device host)))
1363 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1364 (setq method "davs"))
1365 (unless (zerop (length domain))
1366 (setq user (concat user tramp-prefix-domain-format domain)))
1367 (unless (zerop (length port))
1368 (setq host (concat host tramp-prefix-port-format port)))
1369 (with-parsed-tramp-file-name
1370 (tramp-make-tramp-file-name method user host "") nil
1371 (tramp-message
1372 v 6 "%s %s"
1373 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1374 (tramp-set-file-property v "/" "list-mounts" 'undef)
1375 (if (string-equal (downcase signal-name) "unmounted")
1376 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1377 ;; Set prefix, mountpoint and location.
1378 (unless (string-equal prefix "/")
1379 (tramp-set-file-property v "/" "prefix" prefix))
1380 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1381 (tramp-set-file-property
1382 v "/" "default-location" default-location)))))))
1383
1384 (when tramp-gvfs-enabled
1385 (dbus-register-signal
1386 :session nil tramp-gvfs-path-mounttracker
1387 tramp-gvfs-interface-mounttracker "mounted"
1388 'tramp-gvfs-handler-mounted-unmounted)
1389 (dbus-register-signal
1390 :session nil tramp-gvfs-path-mounttracker
1391 tramp-gvfs-interface-mounttracker "Mounted"
1392 'tramp-gvfs-handler-mounted-unmounted)
1393
1394 (dbus-register-signal
1395 :session nil tramp-gvfs-path-mounttracker
1396 tramp-gvfs-interface-mounttracker "unmounted"
1397 'tramp-gvfs-handler-mounted-unmounted)
1398 (dbus-register-signal
1399 :session nil tramp-gvfs-path-mounttracker
1400 tramp-gvfs-interface-mounttracker "Unmounted"
1401 'tramp-gvfs-handler-mounted-unmounted))
1402
1403 (defun tramp-gvfs-connection-mounted-p (vec)
1404 "Check, whether the location is already mounted."
1405 (or
1406 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1407 (catch 'mounted
1408 (dolist
1409 (elt
1410 (with-tramp-file-property vec "/" "list-mounts"
1411 (with-tramp-dbus-call-method vec t
1412 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1413 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1414 nil)
1415 ;; Jump over the first elements of the mount info. Since there
1416 ;; were changes in the entries, we cannot access dedicated
1417 ;; elements.
1418 (while (stringp (car elt)) (setq elt (cdr elt)))
1419 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1420 (cadr elt)))
1421 (mount-spec (caddr elt))
1422 (default-location (tramp-gvfs-dbus-byte-array-to-string
1423 (cadddr elt)))
1424 (method (tramp-gvfs-dbus-byte-array-to-string
1425 (cadr (assoc "type" (cadr mount-spec)))))
1426 (user (tramp-gvfs-dbus-byte-array-to-string
1427 (cadr (assoc "user" (cadr mount-spec)))))
1428 (domain (tramp-gvfs-dbus-byte-array-to-string
1429 (cadr (assoc "domain" (cadr mount-spec)))))
1430 (host (tramp-gvfs-dbus-byte-array-to-string
1431 (cadr (or (assoc "host" (cadr mount-spec))
1432 (assoc "server" (cadr mount-spec))))))
1433 (port (tramp-gvfs-dbus-byte-array-to-string
1434 (cadr (assoc "port" (cadr mount-spec)))))
1435 (ssl (tramp-gvfs-dbus-byte-array-to-string
1436 (cadr (assoc "ssl" (cadr mount-spec)))))
1437 (prefix (concat
1438 (tramp-gvfs-dbus-byte-array-to-string
1439 (car mount-spec))
1440 (tramp-gvfs-dbus-byte-array-to-string
1441 (or
1442 (cadr (assoc "share" (cadr mount-spec)))
1443 (cadr (assoc "volume" (cadr mount-spec))))))))
1444 (when (string-match "^\\(afp\\|smb\\)" method)
1445 (setq method (match-string 1 method)))
1446 (when (string-equal "obex" method)
1447 (setq host (tramp-bluez-device host)))
1448 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1449 (setq method "davs"))
1450 (when (and (string-equal "synce" method) (zerop (length user)))
1451 (setq user (or (tramp-file-name-user vec) "")))
1452 (unless (zerop (length domain))
1453 (setq user (concat user tramp-prefix-domain-format domain)))
1454 (unless (zerop (length port))
1455 (setq host (concat host tramp-prefix-port-format port)))
1456 (when (and
1457 (string-equal method (tramp-file-name-method vec))
1458 (string-equal user (or (tramp-file-name-user vec) ""))
1459 (string-equal host (tramp-file-name-host vec))
1460 (string-match (concat "^" (regexp-quote prefix))
1461 (tramp-file-name-localname vec)))
1462 ;; Set prefix, mountpoint and location.
1463 (unless (string-equal prefix "/")
1464 (tramp-set-file-property vec "/" "prefix" prefix))
1465 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1466 (tramp-set-file-property vec "/" "default-location" default-location)
1467 (throw 'mounted t)))))))
1468
1469 (defun tramp-gvfs-mount-spec-entry (key value)
1470 "Construct a mount-spec entry to be used in a mount_spec.
1471 It was \"a(say)\", but has changed to \"a{sv})\"."
1472 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1473 (list :dict-entry key
1474 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1475 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1476
1477 (defun tramp-gvfs-mount-spec (vec)
1478 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1479 (let* ((method (tramp-file-name-method vec))
1480 (user (tramp-file-name-real-user vec))
1481 (domain (tramp-file-name-domain vec))
1482 (host (tramp-file-name-real-host vec))
1483 (port (tramp-file-name-port vec))
1484 (localname (tramp-file-name-localname vec))
1485 (share (when (string-match "^/?\\([^/]+\\)" localname)
1486 (match-string 1 localname)))
1487 (ssl (when (string-match "^davs" method) "true" "false"))
1488 (mount-spec
1489 `(:array
1490 ,@(cond
1491 ((string-equal "smb" method)
1492 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1493 (tramp-gvfs-mount-spec-entry "server" host)
1494 (tramp-gvfs-mount-spec-entry "share" share)))
1495 ((string-equal "obex" method)
1496 (list (tramp-gvfs-mount-spec-entry "type" method)
1497 (tramp-gvfs-mount-spec-entry
1498 "host" (concat "[" (tramp-bluez-address host) "]"))))
1499 ((string-match "\\`dav" method)
1500 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1501 (tramp-gvfs-mount-spec-entry "host" host)
1502 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1503 ((string-equal "afp" method)
1504 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1505 (tramp-gvfs-mount-spec-entry "host" host)
1506 (tramp-gvfs-mount-spec-entry "volume" share)))
1507 (t
1508 (list (tramp-gvfs-mount-spec-entry "type" method)
1509 (tramp-gvfs-mount-spec-entry "host" host))))
1510 ,@(when user
1511 (list (tramp-gvfs-mount-spec-entry "user" user)))
1512 ,@(when domain
1513 (list (tramp-gvfs-mount-spec-entry "domain" domain)))
1514 ,@(when port
1515 (list (tramp-gvfs-mount-spec-entry
1516 "port" (number-to-string port))))))
1517 (mount-pref
1518 (if (and (string-match "\\`dav" method)
1519 (string-match "^/?[^/]+" localname))
1520 (match-string 0 localname)
1521 "/")))
1522
1523 ;; Return.
1524 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1525
1526 \f
1527 ;; Connection functions.
1528
1529 (defun tramp-gvfs-maybe-open-connection (vec)
1530 "Maybe open a connection VEC.
1531 Does not do anything if a connection is already open, but re-opens the
1532 connection if a previous connection has died for some reason."
1533 (tramp-check-proper-method-and-host vec)
1534
1535 ;; We set the file name, in case there are incoming D-Bus signals or
1536 ;; D-Bus errors.
1537 (setq tramp-gvfs-dbus-event-vector vec)
1538
1539 ;; For password handling, we need a process bound to the connection
1540 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1541 ;; better solution?
1542 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1543 (let ((p (make-network-process
1544 :name (tramp-buffer-name vec)
1545 :buffer (tramp-get-connection-buffer vec)
1546 :server t :host 'local :service t)))
1547 (tramp-compat-set-process-query-on-exit-flag p nil)))
1548
1549 (unless (tramp-gvfs-connection-mounted-p vec)
1550 (let* ((method (tramp-file-name-method vec))
1551 (user (tramp-file-name-user vec))
1552 (host (tramp-file-name-host vec))
1553 (localname (tramp-file-name-localname vec))
1554 (object-path
1555 (tramp-gvfs-object-path
1556 (tramp-make-tramp-file-name method user host ""))))
1557
1558 (when (and (string-equal method "smb")
1559 (string-equal localname "/"))
1560 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1561
1562 (when (and (string-equal method "afp")
1563 (string-equal localname "/"))
1564 (tramp-error vec 'file-error "Filename must contain an AFP volume"))
1565
1566 (with-tramp-progress-reporter
1567 vec 3
1568 (if (zerop (length user))
1569 (format "Opening connection for %s using %s" host method)
1570 (format "Opening connection for %s@%s using %s" user host method))
1571
1572 ;; Enable `auth-source'.
1573 (tramp-set-connection-property vec "first-password-request" t)
1574
1575 ;; There will be a callback of "askPassword" when a password is
1576 ;; needed.
1577 (dbus-register-method
1578 :session dbus-service-emacs object-path
1579 tramp-gvfs-interface-mountoperation "askPassword"
1580 'tramp-gvfs-handler-askpassword)
1581 (dbus-register-method
1582 :session dbus-service-emacs object-path
1583 tramp-gvfs-interface-mountoperation "AskPassword"
1584 'tramp-gvfs-handler-askpassword)
1585
1586 ;; There could be a callback of "askQuestion" when adding fingerprint.
1587 (dbus-register-method
1588 :session dbus-service-emacs object-path
1589 tramp-gvfs-interface-mountoperation "askQuestion"
1590 'tramp-gvfs-handler-askquestion)
1591 (dbus-register-method
1592 :session dbus-service-emacs object-path
1593 tramp-gvfs-interface-mountoperation "AskQuestion"
1594 'tramp-gvfs-handler-askquestion)
1595
1596 ;; The call must be asynchronously, because of the "askPassword"
1597 ;; or "askQuestion"callbacks.
1598 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1599 (with-tramp-dbus-call-method vec nil
1600 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1601 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1602 (tramp-gvfs-mount-spec vec)
1603 `(:struct :string ,(dbus-get-unique-name :session)
1604 :object-path ,object-path))
1605 (with-tramp-dbus-call-method vec nil
1606 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1607 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1608 (tramp-gvfs-mount-spec vec)
1609 :string (dbus-get-unique-name :session) :object-path object-path))
1610
1611 ;; We must wait, until the mount is applied. This will be
1612 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1613 ;; file property.
1614 (with-timeout
1615 ((or (tramp-get-method-parameter vec 'tramp-connection-timeout)
1616 tramp-connection-timeout)
1617 (if (zerop (length (tramp-file-name-user vec)))
1618 (tramp-error
1619 vec 'file-error
1620 "Timeout reached mounting %s using %s" host method)
1621 (tramp-error
1622 vec 'file-error
1623 "Timeout reached mounting %s@%s using %s" user host method)))
1624 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1625 (read-event nil nil 0.1)))
1626
1627 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1628 ;; is marked with the fuse-mountpoint "/". We shall react.
1629 (when (string-equal
1630 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1631 (tramp-error vec 'file-error "FUSE mount denied")))))
1632
1633 ;; In `tramp-check-cached-permissions', the connection properties
1634 ;; {uig,gid}-{integer,string} are used. We set them to their local
1635 ;; counterparts.
1636 (with-tramp-connection-property
1637 vec "uid-integer" (tramp-get-local-uid 'integer))
1638 (with-tramp-connection-property
1639 vec "gid-integer" (tramp-get-local-gid 'integer))
1640 (with-tramp-connection-property
1641 vec "uid-string" (tramp-get-local-uid 'string))
1642 (with-tramp-connection-property
1643 vec "gid-string" (tramp-get-local-gid 'string)))
1644
1645 (defun tramp-gvfs-send-command (vec command &rest args)
1646 "Send the COMMAND with its ARGS to connection VEC.
1647 COMMAND is usually a command from the gvfs-* utilities.
1648 `call-process' is applied, and it returns t if the return code is zero."
1649 (with-current-buffer (tramp-get-connection-buffer vec)
1650 (tramp-gvfs-maybe-open-connection vec)
1651 (erase-buffer)
1652 (zerop (apply 'tramp-call-process vec command nil t nil args))))
1653
1654 \f
1655 ;; D-Bus BLUEZ functions.
1656
1657 (defun tramp-bluez-list-devices ()
1658 "Return all discovered bluetooth devices as list.
1659 Every entry is a list (NAME ADDRESS).
1660
1661 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1662 discovery happened more time before indicated there, a rescan will be
1663 started, which lasts some ten seconds. Otherwise, cached results will
1664 be used."
1665 ;; Reset the scanned devices list if time has passed.
1666 (and (integerp tramp-bluez-discover-devices-timeout)
1667 (integerp tramp-bluez-discovery)
1668 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1669 tramp-bluez-discover-devices-timeout)
1670 (setq tramp-bluez-devices nil))
1671
1672 ;; Rescan if needed.
1673 (unless tramp-bluez-devices
1674 (let ((object-path
1675 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1676 :system tramp-bluez-service "/"
1677 tramp-bluez-interface-manager "DefaultAdapter")))
1678 (setq tramp-bluez-devices nil
1679 tramp-bluez-discovery t)
1680 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1681 :system tramp-bluez-service object-path
1682 tramp-bluez-interface-adapter "StartDiscovery")
1683 (while tramp-bluez-discovery
1684 (read-event nil nil 0.1))))
1685 (setq tramp-bluez-discovery (current-time))
1686 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1687 tramp-bluez-devices)
1688
1689 (defun tramp-bluez-property-changed (property value)
1690 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1691 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1692 (cond
1693 ((string-equal property "Discovering")
1694 (unless (car value)
1695 ;; "Discovering" FALSE means discovery run has been completed.
1696 ;; We stop it, because we don't need another run.
1697 (setq tramp-bluez-discovery nil)
1698 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1699 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1700 tramp-bluez-interface-adapter "StopDiscovery")))))
1701
1702 (when tramp-gvfs-enabled
1703 (dbus-register-signal
1704 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1705 'tramp-bluez-property-changed))
1706
1707 (defun tramp-bluez-device-found (device args)
1708 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1709 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1710 (let ((alias (car (cadr (assoc "Alias" args))))
1711 (address (car (cadr (assoc "Address" args)))))
1712 ;; Maybe we shall check the device class for being a proper
1713 ;; device, and call also SDP in order to find the obex service.
1714 (add-to-list 'tramp-bluez-devices (list alias address))))
1715
1716 (when tramp-gvfs-enabled
1717 (dbus-register-signal
1718 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1719 'tramp-bluez-device-found))
1720
1721 (defun tramp-bluez-parse-device-names (_ignore)
1722 "Return a list of (nil host) tuples allowed to access."
1723 (mapcar
1724 (lambda (x) (list nil (car x)))
1725 (tramp-bluez-list-devices)))
1726
1727 ;; Add completion function for OBEX method.
1728 (when (and tramp-gvfs-enabled
1729 (member tramp-bluez-service (dbus-list-known-names :system)))
1730 (tramp-set-completion-function
1731 "obex" '((tramp-bluez-parse-device-names ""))))
1732
1733 \f
1734 ;; D-Bus zeroconf functions.
1735
1736 (defun tramp-zeroconf-parse-workstation-device-names (_ignore)
1737 "Return a list of (user host) tuples allowed to access."
1738 (mapcar
1739 (lambda (x)
1740 (list nil (zeroconf-service-host x)))
1741 (zeroconf-list-services "_workstation._tcp")))
1742
1743 (defun tramp-zeroconf-parse-webdav-device-names (_ignore)
1744 "Return a list of (user host) tuples allowed to access."
1745 (mapcar
1746 (lambda (x)
1747 (let ((host (zeroconf-service-host x))
1748 (port (zeroconf-service-port x))
1749 (text (zeroconf-service-txt x))
1750 user)
1751 (when port
1752 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1753 ;; A user is marked in a TXT field like "u=guest".
1754 (while text
1755 (when (string-match "u=\\(.+\\)$" (car text))
1756 (setq user (match-string 1 (car text))))
1757 (setq text (cdr text)))
1758 (list user host)))
1759 (zeroconf-list-services "_webdav._tcp")))
1760
1761 ;; Add completion function for SFTP, DAV and DAVS methods.
1762 (when (and tramp-gvfs-enabled
1763 (member zeroconf-service-avahi (dbus-list-known-names :system)))
1764 (zeroconf-init tramp-gvfs-zeroconf-domain)
1765 (tramp-set-completion-function
1766 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1767 (tramp-set-completion-function
1768 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1769 (tramp-set-completion-function
1770 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1771
1772 \f
1773 ;; D-Bus SYNCE functions.
1774
1775 (defun tramp-synce-list-devices ()
1776 "Return all discovered synce devices as list.
1777 They are retrieved from the hal daemon."
1778 (let (tramp-synce-devices)
1779 (dolist (device
1780 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1781 :system tramp-hal-service tramp-hal-path-manager
1782 tramp-hal-interface-manager "GetAllDevices"))
1783 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1784 :system tramp-hal-service device tramp-hal-interface-device
1785 "PropertyExists" "sync.plugin")
1786 (let ((prop
1787 (with-tramp-dbus-call-method
1788 tramp-gvfs-dbus-event-vector t
1789 :system tramp-hal-service device tramp-hal-interface-device
1790 "GetPropertyString" "pda.pocketpc.name")))
1791 (unless (member prop tramp-synce-devices)
1792 (push prop tramp-synce-devices)))))
1793 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1794 tramp-synce-devices))
1795
1796 (defun tramp-synce-parse-device-names (_ignore)
1797 "Return a list of (nil host) tuples allowed to access."
1798 (mapcar
1799 (lambda (x) (list nil x))
1800 (tramp-synce-list-devices)))
1801
1802 ;; Add completion function for SYNCE method.
1803 (when tramp-gvfs-enabled
1804 (tramp-set-completion-function
1805 "synce" '((tramp-synce-parse-device-names ""))))
1806
1807 (add-hook 'tramp-unload-hook
1808 (lambda ()
1809 (unload-feature 'tramp-gvfs 'force)))
1810
1811 (provide 'tramp-gvfs)
1812
1813 ;;; TODO:
1814
1815 ;; * Host name completion via afp-server, smb-server or smb-network.
1816 ;; * Check how two shares of the same SMB server can be mounted in
1817 ;; parallel.
1818 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1819 ;; capability.
1820 ;; * Implement obex for other serial communication but bluetooth.
1821
1822 ;;; tramp-gvfs.el ends here