]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-gvfs.el
098d40e7cc0f4944c4f62e7b3ad2c40bb2852140
[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 writing 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-compress-file . ignore)
426 (dired-uncache . tramp-handle-dired-uncache)
427 (expand-file-name . tramp-gvfs-handle-expand-file-name)
428 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
429 (file-acl . ignore)
430 (file-attributes . tramp-gvfs-handle-file-attributes)
431 (file-directory-p . tramp-gvfs-handle-file-directory-p)
432 (file-equal-p . tramp-handle-file-equal-p)
433 (file-executable-p . tramp-gvfs-handle-file-executable-p)
434 (file-exists-p . tramp-handle-file-exists-p)
435 (file-in-directory-p . tramp-handle-file-in-directory-p)
436 (file-local-copy . tramp-gvfs-handle-file-local-copy)
437 (file-modes . tramp-handle-file-modes)
438 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
439 (file-name-as-directory . tramp-handle-file-name-as-directory)
440 (file-name-completion . tramp-handle-file-name-completion)
441 (file-name-directory . tramp-handle-file-name-directory)
442 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
443 ;; `file-name-sans-versions' performed by default handler.
444 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
445 (file-notify-add-watch . tramp-gvfs-handle-file-notify-add-watch)
446 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
447 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
448 (file-ownership-preserved-p . ignore)
449 (file-readable-p . tramp-gvfs-handle-file-readable-p)
450 (file-regular-p . tramp-handle-file-regular-p)
451 (file-remote-p . tramp-handle-file-remote-p)
452 (file-selinux-context . ignore)
453 (file-symlink-p . tramp-handle-file-symlink-p)
454 ;; `file-truename' performed by default handler.
455 (file-writable-p . tramp-gvfs-handle-file-writable-p)
456 (find-backup-file-name . tramp-handle-find-backup-file-name)
457 ;; `find-file-noselect' performed by default handler.
458 ;; `get-file-buffer' performed by default handler.
459 (insert-directory . tramp-handle-insert-directory)
460 (insert-file-contents . tramp-handle-insert-file-contents)
461 (load . tramp-handle-load)
462 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
463 (make-directory . tramp-gvfs-handle-make-directory)
464 (make-directory-internal . ignore)
465 (make-symbolic-link . tramp-handle-make-symbolic-link)
466 (process-file . ignore)
467 (rename-file . tramp-gvfs-handle-rename-file)
468 (set-file-acl . ignore)
469 (set-file-modes . ignore)
470 (set-file-selinux-context . ignore)
471 (set-file-times . ignore)
472 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
473 (shell-command . ignore)
474 (start-file-process . ignore)
475 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
476 (unhandled-file-name-directory . ignore)
477 (vc-registered . ignore)
478 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
479 (write-region . tramp-gvfs-handle-write-region))
480 "Alist of handler functions for Tramp GVFS method.
481 Operations not mentioned here will be handled by the default Emacs primitives.")
482
483 ;; It must be a `defsubst' in order to push the whole code into
484 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
485 ;;;###tramp-autoload
486 (defsubst tramp-gvfs-file-name-p (filename)
487 "Check if it's a filename handled by the GVFS daemon."
488 (and (tramp-tramp-file-p filename)
489 (let ((method
490 (tramp-file-name-method (tramp-dissect-file-name filename))))
491 (and (stringp method) (member method tramp-gvfs-methods)))))
492
493 ;;;###tramp-autoload
494 (defun tramp-gvfs-file-name-handler (operation &rest args)
495 "Invoke the GVFS related OPERATION.
496 First arg specifies the OPERATION, second arg is a list of arguments to
497 pass to the OPERATION."
498 (unless tramp-gvfs-enabled
499 (tramp-user-error nil "Package `tramp-gvfs' not supported"))
500 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
501 (if fn
502 (save-match-data (apply (cdr fn) args))
503 (tramp-run-real-handler operation args))))
504
505 ;; This might be moved to tramp.el. It shall be the first file name
506 ;; handler.
507 ;;;###tramp-autoload
508 (when (featurep 'dbusbind)
509 (add-to-list 'tramp-foreign-file-name-handler-alist
510 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler)))
511
512 \f
513 ;; D-Bus helper function.
514
515 (defun tramp-gvfs-dbus-string-to-byte-array (string)
516 "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
517 (dbus-string-to-byte-array
518 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
519 (concat string (string 0)) string)))
520
521 (defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
522 "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
523 ;; The byte array could be a variant. Take care.
524 (let ((byte-array
525 (if (and (consp byte-array) (atom (car byte-array)))
526 byte-array (car byte-array))))
527 (dbus-byte-array-to-string
528 (if (and (consp byte-array) (zerop (car (last byte-array))))
529 (butlast byte-array) byte-array))))
530
531 (defun tramp-gvfs-stringify-dbus-message (message)
532 "Convert a D-Bus message into readable UTF8 strings, used for traces."
533 (cond
534 ((and (consp message) (characterp (car message)))
535 (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
536 ((consp message)
537 (mapcar 'tramp-gvfs-stringify-dbus-message message))
538 ((stringp message)
539 (format "%S" message))
540 (t message)))
541
542 (defmacro with-tramp-dbus-call-method
543 (vec synchronous bus service path interface method &rest args)
544 "Apply a D-Bus call on bus BUS.
545
546 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
547 it is an asynchronous call, with `ignore' as callback function.
548
549 The other arguments have the same meaning as with `dbus-call-method'
550 or `dbus-call-method-asynchronously'. Additionally, the call
551 will be traced by Tramp with trace level 6."
552 `(let ((func (if ,synchronous
553 'dbus-call-method 'dbus-call-method-asynchronously))
554 (args (append (list ,bus ,service ,path ,interface ,method)
555 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
556 result)
557 (tramp-message ,vec 6 "%s %s" func args)
558 (setq result (apply func args))
559 (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
560 result))
561
562 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
563 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
564 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
565
566 (defvar tramp-gvfs-dbus-event-vector nil
567 "Current Tramp file name to be used, as vector.
568 It is needed when D-Bus signals or errors arrive, because there
569 is no information where to trace the message.")
570
571 (defun tramp-gvfs-dbus-event-error (event err)
572 "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
573 (when tramp-gvfs-dbus-event-vector
574 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
575 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
576
577 ;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
578 (add-hook
579 (if (boundp 'dbus-event-error-functions)
580 'dbus-event-error-functions 'dbus-event-error-hooks)
581 'tramp-gvfs-dbus-event-error)
582
583 \f
584 ;; File name primitives.
585
586 (defun tramp-gvfs-do-copy-or-rename-file
587 (op filename newname &optional ok-if-already-exists keep-date
588 preserve-uid-gid preserve-extended-attributes)
589 "Copy or rename a remote file.
590 OP must be `copy' or `rename' and indicates the operation to perform.
591 FILENAME specifies the file to copy or rename, NEWNAME is the name of
592 the new file (for copy) or the new name of the file (for rename).
593 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
594 KEEP-DATE means to make sure that NEWNAME has the same timestamp
595 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
596 the uid and gid if both files are on the same host.
597 PRESERVE-EXTENDED-ATTRIBUTES is ignored.
598
599 This function is invoked by `tramp-gvfs-handle-copy-file' and
600 `tramp-gvfs-handle-rename-file'. It is an error if OP is neither
601 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
602 file names."
603 (unless (memq op '(copy rename))
604 (error "Unknown operation `%s', must be `copy' or `rename'" op))
605
606 (let ((t1 (tramp-tramp-file-p filename))
607 (t2 (tramp-tramp-file-p newname))
608 (equal-remote (tramp-equal-remote filename newname))
609 (file-operation (intern (format "%s-file" op)))
610 (gvfs-operation (if (eq op 'copy) "gvfs-copy" "gvfs-move"))
611 (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
612
613 (with-parsed-tramp-file-name (if t1 filename newname) nil
614 (when (and (not ok-if-already-exists) (file-exists-p newname))
615 (tramp-error
616 v 'file-already-exists "File %s already exists" newname))
617
618 (if (or (and equal-remote
619 (tramp-get-connection-property v "direct-copy-failed" nil))
620 (and t1 (not (tramp-gvfs-file-name-p filename)))
621 (and t2 (not (tramp-gvfs-file-name-p newname))))
622
623 ;; We cannot copy or rename directly.
624 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with
625 ;; Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and renamed
626 ;; in Emacs 24.3.
627 (let ((tmpfile (tramp-compat-make-temp-file filename)))
628 (cond
629 (preserve-extended-attributes
630 (funcall
631 file-operation
632 filename tmpfile t keep-date preserve-uid-gid
633 preserve-extended-attributes))
634 (t
635 (funcall
636 file-operation filename tmpfile t keep-date preserve-uid-gid)))
637 (rename-file tmpfile newname ok-if-already-exists))
638
639 ;; Direct action.
640 (with-tramp-progress-reporter
641 v 0 (format "%s %s to %s" msg-operation filename newname)
642 (unless
643 (apply
644 'tramp-gvfs-send-command v gvfs-operation
645 (append
646 (and (eq op 'copy) (or keep-date preserve-uid-gid)
647 (list "--preserve"))
648 (list
649 (tramp-gvfs-url-file-name filename)
650 (tramp-gvfs-url-file-name newname))))
651
652 (if (or (not equal-remote)
653 (and equal-remote
654 (tramp-get-connection-property
655 v "direct-copy-failed" nil)))
656 ;; Propagate the error.
657 (with-current-buffer (tramp-get-connection-buffer v)
658 (goto-char (point-min))
659 (tramp-error-with-buffer
660 nil v 'file-error
661 "%s failed, see buffer `%s' for details."
662 msg-operation (buffer-name)))
663
664 ;; Some WebDAV server, like the one from QNAP, do not
665 ;; support direct copy/move. Try a fallback.
666 (tramp-set-connection-property v "direct-copy-failed" t)
667 (tramp-gvfs-do-copy-or-rename-file
668 op filename newname ok-if-already-exists keep-date
669 preserve-uid-gid preserve-extended-attributes))))
670
671 (when (and t1 (eq op 'rename))
672 (with-parsed-tramp-file-name filename nil
673 (tramp-flush-file-property v (file-name-directory localname))
674 (tramp-flush-file-property v localname)))
675
676 (when t2
677 (with-parsed-tramp-file-name newname nil
678 (tramp-flush-file-property v (file-name-directory localname))
679 (tramp-flush-file-property v localname)))))))
680
681 (defun tramp-gvfs-handle-copy-file
682 (filename newname &optional ok-if-already-exists keep-date
683 preserve-uid-gid preserve-extended-attributes)
684 "Like `copy-file' for Tramp files."
685 (setq filename (expand-file-name filename))
686 (setq newname (expand-file-name newname))
687 (cond
688 ;; At least one file a Tramp file?
689 ((or (tramp-tramp-file-p filename)
690 (tramp-tramp-file-p newname))
691 (tramp-gvfs-do-copy-or-rename-file
692 'copy filename newname ok-if-already-exists keep-date
693 preserve-uid-gid preserve-extended-attributes))
694 ;; Compat section. PRESERVE-EXTENDED-ATTRIBUTES has been
695 ;; introduced with Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and
696 ;; renamed in Emacs 24.3.
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 (t
703 (tramp-run-real-handler
704 'copy-file
705 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))))
706
707 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
708 "Like `delete-directory' for Tramp files."
709 (when (and recursive (not (file-symlink-p directory)))
710 (mapc (lambda (file)
711 (if (eq t (car (file-attributes file)))
712 (tramp-compat-delete-directory file recursive trash)
713 (tramp-compat-delete-file file trash)))
714 (directory-files
715 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
716 (with-parsed-tramp-file-name directory nil
717 (tramp-flush-file-property v (file-name-directory localname))
718 (tramp-flush-directory-property v localname)
719 (unless
720 (tramp-gvfs-send-command
721 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
722 (tramp-gvfs-url-file-name directory))
723 ;; Propagate the error.
724 (with-current-buffer (tramp-get-connection-buffer v)
725 (goto-char (point-min))
726 (tramp-error-with-buffer
727 nil v 'file-error "Couldn't delete %s" directory)))))
728
729 (defun tramp-gvfs-handle-delete-file (filename &optional trash)
730 "Like `delete-file' for Tramp files."
731 (with-parsed-tramp-file-name filename nil
732 (tramp-flush-file-property v (file-name-directory localname))
733 (tramp-flush-file-property v localname)
734 (unless
735 (tramp-gvfs-send-command
736 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
737 (tramp-gvfs-url-file-name filename))
738 ;; Propagate the error.
739 (with-current-buffer (tramp-get-connection-buffer v)
740 (goto-char (point-min))
741 (tramp-error-with-buffer
742 nil v 'file-error "Couldn't delete %s" filename)))))
743
744 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
745 "Like `expand-file-name' for Tramp files."
746 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
747 (setq dir (or dir default-directory "/"))
748 ;; Unless NAME is absolute, concat DIR and NAME.
749 (unless (file-name-absolute-p name)
750 (setq name (concat (file-name-as-directory dir) name)))
751 ;; If NAME is not a Tramp file, run the real handler.
752 (if (not (tramp-tramp-file-p name))
753 (tramp-run-real-handler 'expand-file-name (list name nil))
754 ;; Dissect NAME.
755 (with-parsed-tramp-file-name name nil
756 ;; If there is a default location, expand tilde.
757 (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
758 (save-match-data
759 (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
760 (setq localname
761 (replace-match
762 (tramp-get-file-property v "/" "default-location" "~")
763 nil t localname 1)))
764 ;; Tilde expansion is not possible.
765 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
766 (tramp-error
767 v 'file-error
768 "Cannot expand tilde in file `%s'" name))
769 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
770 (setq localname (concat "/" localname)))
771 ;; We do not pass "/..".
772 (if (string-match "^\\(afp\\|smb\\)$" method)
773 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
774 (setq localname (replace-match "/" t t localname 1)))
775 (when (string-match "^/\\.\\./?" localname)
776 (setq localname (replace-match "/" t t localname))))
777 ;; There might be a double slash. Remove this.
778 (while (string-match "//" localname)
779 (setq localname (replace-match "/" t t localname)))
780 ;; No tilde characters in file name, do normal
781 ;; `expand-file-name' (this does "/./" and "/../").
782 (tramp-make-tramp-file-name
783 method user host
784 (tramp-run-real-handler
785 'expand-file-name (list localname))))))
786
787 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
788 "Like `file-attributes' for Tramp files."
789 (unless id-format (setq id-format 'integer))
790 (ignore-errors
791 ;; Don't modify `last-coding-system-used' by accident.
792 (let ((last-coding-system-used last-coding-system-used)
793 (process-environment (cons "LC_MESSAGES=C" process-environment))
794 dirp res-symlink-target res-numlinks res-uid res-gid res-access
795 res-mod res-change res-size res-filemodes res-inode res-device)
796 (with-parsed-tramp-file-name filename nil
797 (with-tramp-file-property
798 v localname (format "file-attributes-%s" id-format)
799 (tramp-message v 5 "file attributes: %s" localname)
800 (tramp-gvfs-send-command
801 v "gvfs-info" (tramp-gvfs-url-file-name filename))
802 ;; Parse output ...
803 (with-current-buffer (tramp-get-connection-buffer v)
804 (goto-char (point-min))
805 (when (re-search-forward "attributes:" nil t)
806 ;; ... directory or symlink
807 (goto-char (point-min))
808 (setq dirp (if (re-search-forward "type: directory" nil t) t))
809 (goto-char (point-min))
810 (setq res-symlink-target
811 (if (re-search-forward
812 "standard::symlink-target: \\(.+\\)$" nil t)
813 (match-string 1)))
814 ;; ... number links
815 (goto-char (point-min))
816 (setq res-numlinks
817 (if (re-search-forward "unix::nlink: \\([0-9]+\\)" nil t)
818 (string-to-number (match-string 1)) 0))
819 ;; ... uid and gid
820 (goto-char (point-min))
821 (setq res-uid
822 (if (eq id-format 'integer)
823 (if (re-search-forward "unix::uid: \\([0-9]+\\)" nil t)
824 (string-to-number (match-string 1))
825 -1)
826 (if (re-search-forward "owner::user: \\(.+\\)$" nil t)
827 (match-string 1)
828 "UNKNOWN")))
829 (setq res-gid
830 (if (eq id-format 'integer)
831 (if (re-search-forward "unix::gid: \\([0-9]+\\)" nil t)
832 (string-to-number (match-string 1))
833 -1)
834 (if (re-search-forward "owner::group: \\(.+\\)$" nil t)
835 (match-string 1)
836 "UNKNOWN")))
837 ;; ... last access, modification and change time
838 (goto-char (point-min))
839 (setq res-access
840 (if (re-search-forward "time::access: \\([0-9]+\\)" nil t)
841 (seconds-to-time (string-to-number (match-string 1)))
842 '(0 0)))
843 (goto-char (point-min))
844 (setq res-mod
845 (if (re-search-forward "time::modified: \\([0-9]+\\)" nil t)
846 (seconds-to-time (string-to-number (match-string 1)))
847 '(0 0)))
848 (goto-char (point-min))
849 (setq res-change
850 (if (re-search-forward "time::changed: \\([0-9]+\\)" nil t)
851 (seconds-to-time (string-to-number (match-string 1)))
852 '(0 0)))
853 ;; ... size
854 (goto-char (point-min))
855 (setq res-size
856 (if (re-search-forward "standard::size: \\([0-9]+\\)" nil t)
857 (string-to-number (match-string 1)) 0))
858 ;; ... file mode flags
859 (goto-char (point-min))
860 (setq res-filemodes
861 (if (re-search-forward "unix::mode: \\([0-9]+\\)" nil t)
862 (tramp-file-mode-from-int
863 (string-to-number (match-string 1)))
864 (if dirp "drwx------" "-rwx------")))
865 ;; ... inode and device
866 (goto-char (point-min))
867 (setq res-inode
868 (if (re-search-forward "unix::inode: \\([0-9]+\\)" nil t)
869 (string-to-number (match-string 1))
870 (tramp-get-inode v)))
871 (goto-char (point-min))
872 (setq res-device
873 (if (re-search-forward "unix::device: \\([0-9]+\\)" nil t)
874 (string-to-number (match-string 1))
875 (tramp-get-device v)))
876
877 ;; Return data gathered.
878 (list
879 ;; 0. t for directory, string (name linked to) for
880 ;; symbolic link, or nil.
881 (or dirp res-symlink-target)
882 ;; 1. Number of links to file.
883 res-numlinks
884 ;; 2. File uid.
885 res-uid
886 ;; 3. File gid.
887 res-gid
888 ;; 4. Last access time, as a list of integers.
889 ;; 5. Last modification time, likewise.
890 ;; 6. Last status change time, likewise.
891 res-access res-mod res-change
892 ;; 7. Size in bytes (-1, if number is out of range).
893 res-size
894 ;; 8. File modes.
895 res-filemodes
896 ;; 9. t if file's gid would change if file were deleted
897 ;; and recreated.
898 nil
899 ;; 10. Inode number.
900 res-inode
901 ;; 11. Device number.
902 res-device
903 ))))))))
904
905 (defun tramp-gvfs-handle-file-directory-p (filename)
906 "Like `file-directory-p' for Tramp files."
907 (eq t (car (file-attributes filename))))
908
909 (defun tramp-gvfs-handle-file-executable-p (filename)
910 "Like `file-executable-p' for Tramp files."
911 (with-parsed-tramp-file-name filename nil
912 (with-tramp-file-property v localname "file-executable-p"
913 (tramp-check-cached-permissions v ?x))))
914
915 (defun tramp-gvfs-handle-file-local-copy (filename)
916 "Like `file-local-copy' for Tramp files."
917 (with-parsed-tramp-file-name filename nil
918 (let ((tmpfile (tramp-compat-make-temp-file filename)))
919 (unless (file-exists-p filename)
920 (tramp-error
921 v 'file-error
922 "Cannot make local copy of non-existing file `%s'" filename))
923 (copy-file filename tmpfile 'ok-if-already-exists 'keep-time)
924 tmpfile)))
925
926 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
927 "Like `file-name-all-completions' for Tramp files."
928 (unless (save-match-data (string-match "/" filename))
929 (with-parsed-tramp-file-name (expand-file-name directory) nil
930
931 (all-completions
932 filename
933 (mapcar
934 'list
935 (or
936 ;; Try cache entries for filename, filename with last
937 ;; character removed, filename with last two characters
938 ;; removed, ..., and finally the empty string - all
939 ;; concatenated to the local directory name.
940 (let ((remote-file-name-inhibit-cache
941 (or remote-file-name-inhibit-cache
942 tramp-completion-reread-directory-timeout)))
943
944 ;; This is inefficient for very long filenames, pity
945 ;; `reduce' is not available...
946 (car
947 (apply
948 'append
949 (mapcar
950 (lambda (x)
951 (let ((cache-hit
952 (tramp-get-file-property
953 v
954 (concat localname (substring filename 0 x))
955 "file-name-all-completions"
956 nil)))
957 (when cache-hit (list cache-hit))))
958 ;; We cannot use a length of 0, because file properties
959 ;; for "foo" and "foo/" are identical.
960 (number-sequence (length filename) 1 -1)))))
961
962 ;; Cache expired or no matching cache entry found so we need
963 ;; to perform a remote operation.
964 (let ((result '("." ".."))
965 entry)
966 ;; Get a list of directories and files.
967 (tramp-gvfs-send-command
968 v "gvfs-ls" "-h" (tramp-gvfs-url-file-name directory))
969
970 ;; Now grab the output.
971 (with-temp-buffer
972 (insert-buffer-substring (tramp-get-connection-buffer v))
973 (goto-char (point-max))
974 (while (zerop (forward-line -1))
975 (setq entry (buffer-substring (point) (point-at-eol)))
976 (when (string-match filename entry)
977 (if (file-directory-p (expand-file-name entry directory))
978 (push (concat entry "/") result)
979 (push entry result)))))
980
981 ;; Because the remote op went through OK we know the
982 ;; directory we `cd'-ed to exists.
983 (tramp-set-file-property v localname "file-exists-p" t)
984
985 ;; Because the remote op went through OK we know every
986 ;; file listed by `ls' exists.
987 (mapc (lambda (entry)
988 (tramp-set-file-property
989 v (concat localname entry) "file-exists-p" t))
990 result)
991
992 ;; Store result in the cache.
993 (tramp-set-file-property
994 v (concat localname filename)
995 "file-name-all-completions" result))))))))
996
997 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
998 "Like `file-notify-add-watch' for Tramp files."
999 (setq file-name (expand-file-name file-name))
1000 (with-parsed-tramp-file-name file-name nil
1001 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1002 ;; supported for gvfs-mounted directories.
1003 (when (file-directory-p file-name)
1004 (tramp-error
1005 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1006 (let* ((default-directory (file-name-directory file-name))
1007 (events
1008 (cond
1009 ((and (memq 'change flags) (memq 'attribute-change flags))
1010 '(created changed changes-done-hint moved deleted
1011 attribute-changed))
1012 ((memq 'change flags)
1013 '(created changed changes-done-hint moved deleted))
1014 ((memq 'attribute-change flags) '(attribute-changed))))
1015 (p (start-process
1016 "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
1017 "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name))))
1018 (if (not (processp p))
1019 (tramp-error
1020 v 'file-notify-error "Monitoring not supported for `%s'" file-name)
1021 (tramp-message
1022 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
1023 (tramp-set-connection-property p "vector" v)
1024 (process-put p 'events events)
1025 (process-put p 'watch-name localname)
1026 (set-process-query-on-exit-flag p nil)
1027 (set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
1028 ;; There might be an error if the monitor is not supported.
1029 ;; Give the filter a chance to read the output.
1030 (tramp-accept-process-output p 1)
1031 (unless (memq (process-status p) '(run open))
1032 (tramp-error
1033 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1034 p))))
1035
1036 (defun tramp-gvfs-monitor-file-process-filter (proc string)
1037 "Read output from \"gvfs-monitor-file\" and add corresponding \
1038 file-notify events."
1039 (let* ((rest-string (process-get proc 'rest-string))
1040 (dd (with-current-buffer (process-buffer proc) default-directory))
1041 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
1042 (when rest-string
1043 (tramp-message proc 10 "Previous string:\n%s" rest-string))
1044 (tramp-message proc 6 "%S\n%s" proc string)
1045 (setq string (concat rest-string string)
1046 ;; Attribute change is returned in unused wording.
1047 string (replace-regexp-in-string
1048 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1049 (when (string-match "Monitoring not supported" string)
1050 (delete-process proc))
1051
1052 (while (string-match
1053 (concat "^[\n\r]*"
1054 "File Monitor Event:[\n\r]+"
1055 "File = \\([^\n\r]+\\)[\n\r]+"
1056 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1057 string)
1058 (let ((file (match-string 1 string))
1059 (action (intern-soft
1060 (replace-regexp-in-string
1061 "_" "-" (downcase (match-string 2 string))))))
1062 (setq string (replace-match "" nil nil string))
1063 ;; File names are returned as URL paths. We must convert them.
1064 (when (string-match ddu file)
1065 (setq file (replace-match dd nil nil file)))
1066 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
1067 (setq file
1068 (replace-match
1069 (char-to-string (string-to-number (match-string 1 file) 16))
1070 nil nil file)))
1071 ;; Usually, we would add an Emacs event now. Unfortunately,
1072 ;; `unread-command-events' does not accept several events at
1073 ;; once. Therefore, we apply the callback directly.
1074 (tramp-compat-funcall 'file-notify-callback (list proc action file))))
1075
1076 ;; Save rest of the string.
1077 (when (zerop (length string)) (setq string nil))
1078 (when string (tramp-message proc 10 "Rest string:\n%s" string))
1079 (process-put proc 'rest-string string)))
1080
1081 (defun tramp-gvfs-handle-file-readable-p (filename)
1082 "Like `file-readable-p' for Tramp files."
1083 (with-parsed-tramp-file-name filename nil
1084 (with-tramp-file-property v localname "file-readable-p"
1085 (tramp-check-cached-permissions v ?r))))
1086
1087 (defun tramp-gvfs-handle-file-writable-p (filename)
1088 "Like `file-writable-p' for Tramp files."
1089 (with-parsed-tramp-file-name filename nil
1090 (with-tramp-file-property v localname "file-writable-p"
1091 (if (file-exists-p filename)
1092 (tramp-check-cached-permissions v ?w)
1093 ;; If file doesn't exist, check if directory is writable.
1094 (and (file-directory-p (file-name-directory filename))
1095 (file-writable-p (file-name-directory filename)))))))
1096
1097 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1098 "Like `make-directory' for Tramp files."
1099 (setq dir (directory-file-name (expand-file-name dir)))
1100 (with-parsed-tramp-file-name dir nil
1101 (tramp-flush-file-property v (file-name-directory localname))
1102 (tramp-flush-directory-property v localname)
1103 (save-match-data
1104 (let ((ldir (file-name-directory dir)))
1105 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1106 ;; work robust.
1107 (when (and parents (not (file-directory-p ldir)))
1108 (make-directory ldir parents))
1109 ;; Just do it.
1110 (unless (tramp-gvfs-send-command
1111 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1112 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1113
1114 (defun tramp-gvfs-handle-rename-file
1115 (filename newname &optional ok-if-already-exists)
1116 "Like `rename-file' for Tramp files."
1117 ;; Check if both files are local -- invoke normal rename-file.
1118 ;; Otherwise, use Tramp from local system.
1119 (setq filename (expand-file-name filename))
1120 (setq newname (expand-file-name newname))
1121 ;; At least one file a Tramp file?
1122 (if (or (tramp-tramp-file-p filename)
1123 (tramp-tramp-file-p newname))
1124 (tramp-gvfs-do-copy-or-rename-file
1125 'rename filename newname ok-if-already-exists
1126 'keep-date 'preserve-uid-gid)
1127 (tramp-run-real-handler
1128 'rename-file (list filename newname ok-if-already-exists))))
1129
1130 (defun tramp-gvfs-handle-write-region
1131 (start end filename &optional append visit lockname confirm)
1132 "Like `write-region' for Tramp files."
1133 (with-parsed-tramp-file-name filename nil
1134 (when (and confirm (file-exists-p filename))
1135 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1136 (tramp-error v 'file-error "File not overwritten")))
1137
1138 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1139 (when (and append (file-exists-p filename))
1140 (copy-file filename tmpfile 'ok))
1141 ;; We say `no-message' here because we don't want the visited file
1142 ;; modtime data to be clobbered from the temp file. We call
1143 ;; `set-visited-file-modtime' ourselves later on.
1144 (tramp-run-real-handler
1145 'write-region
1146 (if confirm ; don't pass this arg unless defined for backward compat.
1147 (list start end tmpfile append 'no-message lockname confirm)
1148 (list start end tmpfile append 'no-message lockname)))
1149 (condition-case nil
1150 (rename-file tmpfile filename 'ok-if-already-exists)
1151 (error
1152 (delete-file tmpfile)
1153 (tramp-error
1154 v 'file-error "Couldn't write region to `%s'" filename))))
1155
1156 (tramp-flush-file-property v (file-name-directory localname))
1157 (tramp-flush-file-property v localname)
1158
1159 ;; Set file modification time.
1160 (when (or (eq visit t) (stringp visit))
1161 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1162
1163 ;; The end.
1164 (when (or (eq visit t) (null visit) (stringp visit))
1165 (tramp-message v 0 "Wrote %s" filename))
1166 (run-hooks 'tramp-handle-write-region-hook)))
1167
1168 \f
1169 ;; File name conversions.
1170
1171 (defun tramp-gvfs-url-file-name (filename)
1172 "Return FILENAME in URL syntax."
1173 ;; "/" must NOT be hexlified.
1174 (let ((url-unreserved-chars (cons ?/ url-unreserved-chars))
1175 result)
1176 (setq
1177 result
1178 (url-recreate-url
1179 (if (tramp-tramp-file-p filename)
1180 (with-parsed-tramp-file-name filename nil
1181 (when (and user (string-match tramp-user-with-domain-regexp user))
1182 (setq user
1183 (concat (match-string 2 user) ";" (match-string 1 user))))
1184 (url-parse-make-urlobj
1185 method (and user (url-hexify-string user)) nil
1186 (tramp-file-name-real-host v) (tramp-file-name-port v)
1187 (and localname (url-hexify-string localname)) nil nil t))
1188 (url-parse-make-urlobj
1189 "file" nil nil nil nil
1190 (url-hexify-string (file-truename filename)) nil nil t))))
1191 (when (tramp-tramp-file-p filename)
1192 (with-parsed-tramp-file-name filename nil
1193 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1194 result))
1195
1196 (defun tramp-gvfs-object-path (filename)
1197 "Create a D-Bus object path from FILENAME."
1198 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1199
1200 (defun tramp-gvfs-file-name (object-path)
1201 "Retrieve file name from D-Bus OBJECT-PATH."
1202 (dbus-unescape-from-identifier
1203 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1204
1205 (defun tramp-bluez-address (device)
1206 "Return bluetooth device address from a given bluetooth DEVICE name."
1207 (when (stringp device)
1208 (if (string-match tramp-ipv6-regexp device)
1209 (match-string 0 device)
1210 (cadr (assoc device (tramp-bluez-list-devices))))))
1211
1212 (defun tramp-bluez-device (address)
1213 "Return bluetooth device name from a given bluetooth device ADDRESS.
1214 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1215 (when (stringp address)
1216 (while (string-match "[][]" address)
1217 (setq address (replace-match "" t t address)))
1218 (let (result)
1219 (dolist (item (tramp-bluez-list-devices) result)
1220 (when (string-match address (cadr item))
1221 (setq result (car item)))))))
1222
1223 \f
1224 ;; D-Bus GVFS functions.
1225
1226 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1227 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1228 (let* ((filename
1229 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1230 (pw-prompt
1231 (format
1232 "%s for %s "
1233 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1234 (capitalize (match-string 1 message))
1235 "Password")
1236 filename))
1237 password)
1238
1239 (condition-case nil
1240 (with-parsed-tramp-file-name filename l
1241 (when (and (zerop (length user))
1242 (not
1243 (zerop (logand flags tramp-gvfs-password-need-username))))
1244 (setq user (read-string "User name: ")))
1245 (when (and (zerop (length domain))
1246 (not
1247 (zerop (logand flags tramp-gvfs-password-need-domain))))
1248 (setq domain (read-string "Domain name: ")))
1249
1250 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1251 (unless (tramp-get-connection-property l "first-password-request" nil)
1252 (tramp-clear-passwd l))
1253
1254 (setq tramp-current-method l-method
1255 tramp-current-user user
1256 tramp-current-host l-host
1257 password (tramp-read-passwd
1258 (tramp-get-connection-process l) pw-prompt))
1259
1260 ;; Return result.
1261 (if (stringp password)
1262 (list
1263 t ;; password handled.
1264 nil ;; no abort of D-Bus.
1265 password
1266 (tramp-file-name-real-user l)
1267 domain
1268 nil ;; not anonymous.
1269 0) ;; no password save.
1270 ;; No password provided.
1271 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1272
1273 ;; When QUIT is raised, we shall return this information to D-Bus.
1274 (quit (list nil t "" "" "" nil 0)))))
1275
1276 (defun tramp-gvfs-handler-askquestion (message choices)
1277 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1278 (save-window-excursion
1279 (let ((enable-recursive-minibuffers t)
1280 choice)
1281
1282 (condition-case nil
1283 (with-parsed-tramp-file-name
1284 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1285 (tramp-message v 6 "%S %S" message choices)
1286
1287 ;; In theory, there can be several choices. Until now,
1288 ;; there is only the question whether to accept an unknown
1289 ;; host signature.
1290 (with-temp-buffer
1291 ;; Preserve message for `progress-reporter'.
1292 (with-temp-message ""
1293 (insert message)
1294 (pop-to-buffer (current-buffer))
1295 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1296 (tramp-message v 6 "%d" choice)))
1297
1298 ;; When the choice is "no", we set a dummy fuse-mountpoint
1299 ;; in order to leave the timeout.
1300 (unless (zerop choice)
1301 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1302
1303 (list
1304 t ;; handled.
1305 nil ;; no abort of D-Bus.
1306 choice))
1307
1308 ;; When QUIT is raised, we shall return this information to D-Bus.
1309 (quit (list nil t 0))))))
1310
1311 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1312 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1313 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1314 (ignore-errors
1315 (let ((signal-name (dbus-event-member-name last-input-event))
1316 (elt mount-info))
1317 ;; Jump over the first elements of the mount info. Since there
1318 ;; were changes in the entries, we cannot access dedicated
1319 ;; elements.
1320 (while (stringp (car elt)) (setq elt (cdr elt)))
1321 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1322 (mount-spec (caddr elt))
1323 (default-location (tramp-gvfs-dbus-byte-array-to-string
1324 (cadddr elt)))
1325 (method (tramp-gvfs-dbus-byte-array-to-string
1326 (cadr (assoc "type" (cadr mount-spec)))))
1327 (user (tramp-gvfs-dbus-byte-array-to-string
1328 (cadr (assoc "user" (cadr mount-spec)))))
1329 (domain (tramp-gvfs-dbus-byte-array-to-string
1330 (cadr (assoc "domain" (cadr mount-spec)))))
1331 (host (tramp-gvfs-dbus-byte-array-to-string
1332 (cadr (or (assoc "host" (cadr mount-spec))
1333 (assoc "server" (cadr mount-spec))))))
1334 (port (tramp-gvfs-dbus-byte-array-to-string
1335 (cadr (assoc "port" (cadr mount-spec)))))
1336 (ssl (tramp-gvfs-dbus-byte-array-to-string
1337 (cadr (assoc "ssl" (cadr mount-spec)))))
1338 (prefix (concat
1339 (tramp-gvfs-dbus-byte-array-to-string
1340 (car mount-spec))
1341 (tramp-gvfs-dbus-byte-array-to-string
1342 (or (cadr (assoc "share" (cadr mount-spec)))
1343 (cadr (assoc "volume" (cadr mount-spec))))))))
1344 (when (string-match "^\\(afp\\|smb\\)" method)
1345 (setq method (match-string 1 method)))
1346 (when (string-equal "obex" method)
1347 (setq host (tramp-bluez-device host)))
1348 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1349 (setq method "davs"))
1350 (unless (zerop (length domain))
1351 (setq user (concat user tramp-prefix-domain-format domain)))
1352 (unless (zerop (length port))
1353 (setq host (concat host tramp-prefix-port-format port)))
1354 (with-parsed-tramp-file-name
1355 (tramp-make-tramp-file-name method user host "") nil
1356 (tramp-message
1357 v 6 "%s %s"
1358 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1359 (tramp-set-file-property v "/" "list-mounts" 'undef)
1360 (if (string-equal (downcase signal-name) "unmounted")
1361 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1362 ;; Set prefix, mountpoint and location.
1363 (unless (string-equal prefix "/")
1364 (tramp-set-file-property v "/" "prefix" prefix))
1365 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1366 (tramp-set-file-property
1367 v "/" "default-location" default-location)))))))
1368
1369 (when tramp-gvfs-enabled
1370 (dbus-register-signal
1371 :session nil tramp-gvfs-path-mounttracker
1372 tramp-gvfs-interface-mounttracker "mounted"
1373 'tramp-gvfs-handler-mounted-unmounted)
1374 (dbus-register-signal
1375 :session nil tramp-gvfs-path-mounttracker
1376 tramp-gvfs-interface-mounttracker "Mounted"
1377 'tramp-gvfs-handler-mounted-unmounted)
1378
1379 (dbus-register-signal
1380 :session nil tramp-gvfs-path-mounttracker
1381 tramp-gvfs-interface-mounttracker "unmounted"
1382 'tramp-gvfs-handler-mounted-unmounted)
1383 (dbus-register-signal
1384 :session nil tramp-gvfs-path-mounttracker
1385 tramp-gvfs-interface-mounttracker "Unmounted"
1386 'tramp-gvfs-handler-mounted-unmounted))
1387
1388 (defun tramp-gvfs-connection-mounted-p (vec)
1389 "Check, whether the location is already mounted."
1390 (or
1391 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1392 (catch 'mounted
1393 (dolist
1394 (elt
1395 (with-tramp-file-property vec "/" "list-mounts"
1396 (with-tramp-dbus-call-method vec t
1397 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1398 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1399 nil)
1400 ;; Jump over the first elements of the mount info. Since there
1401 ;; were changes in the entries, we cannot access dedicated
1402 ;; elements.
1403 (while (stringp (car elt)) (setq elt (cdr elt)))
1404 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1405 (cadr elt)))
1406 (mount-spec (caddr elt))
1407 (default-location (tramp-gvfs-dbus-byte-array-to-string
1408 (cadddr elt)))
1409 (method (tramp-gvfs-dbus-byte-array-to-string
1410 (cadr (assoc "type" (cadr mount-spec)))))
1411 (user (tramp-gvfs-dbus-byte-array-to-string
1412 (cadr (assoc "user" (cadr mount-spec)))))
1413 (domain (tramp-gvfs-dbus-byte-array-to-string
1414 (cadr (assoc "domain" (cadr mount-spec)))))
1415 (host (tramp-gvfs-dbus-byte-array-to-string
1416 (cadr (or (assoc "host" (cadr mount-spec))
1417 (assoc "server" (cadr mount-spec))))))
1418 (port (tramp-gvfs-dbus-byte-array-to-string
1419 (cadr (assoc "port" (cadr mount-spec)))))
1420 (ssl (tramp-gvfs-dbus-byte-array-to-string
1421 (cadr (assoc "ssl" (cadr mount-spec)))))
1422 (prefix (concat
1423 (tramp-gvfs-dbus-byte-array-to-string
1424 (car mount-spec))
1425 (tramp-gvfs-dbus-byte-array-to-string
1426 (or
1427 (cadr (assoc "share" (cadr mount-spec)))
1428 (cadr (assoc "volume" (cadr mount-spec))))))))
1429 (when (string-match "^\\(afp\\|smb\\)" method)
1430 (setq method (match-string 1 method)))
1431 (when (string-equal "obex" method)
1432 (setq host (tramp-bluez-device host)))
1433 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1434 (setq method "davs"))
1435 (when (and (string-equal "synce" method) (zerop (length user)))
1436 (setq user (or (tramp-file-name-user vec) "")))
1437 (unless (zerop (length domain))
1438 (setq user (concat user tramp-prefix-domain-format domain)))
1439 (unless (zerop (length port))
1440 (setq host (concat host tramp-prefix-port-format port)))
1441 (when (and
1442 (string-equal method (tramp-file-name-method vec))
1443 (string-equal user (or (tramp-file-name-user vec) ""))
1444 (string-equal host (tramp-file-name-host vec))
1445 (string-match (concat "^" (regexp-quote prefix))
1446 (tramp-file-name-localname vec)))
1447 ;; Set prefix, mountpoint and location.
1448 (unless (string-equal prefix "/")
1449 (tramp-set-file-property vec "/" "prefix" prefix))
1450 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1451 (tramp-set-file-property vec "/" "default-location" default-location)
1452 (throw 'mounted t)))))))
1453
1454 (defun tramp-gvfs-mount-spec-entry (key value)
1455 "Construct a mount-spec entry to be used in a mount_spec.
1456 It was \"a(say)\", but has changed to \"a{sv})\"."
1457 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1458 (list :dict-entry key
1459 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1460 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1461
1462 (defun tramp-gvfs-mount-spec (vec)
1463 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1464 (let* ((method (tramp-file-name-method vec))
1465 (user (tramp-file-name-real-user vec))
1466 (domain (tramp-file-name-domain vec))
1467 (host (tramp-file-name-real-host vec))
1468 (port (tramp-file-name-port vec))
1469 (localname (tramp-file-name-localname vec))
1470 (share (when (string-match "^/?\\([^/]+\\)" localname)
1471 (match-string 1 localname)))
1472 (ssl (when (string-match "^davs" method) "true" "false"))
1473 (mount-spec
1474 `(:array
1475 ,@(cond
1476 ((string-equal "smb" method)
1477 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1478 (tramp-gvfs-mount-spec-entry "server" host)
1479 (tramp-gvfs-mount-spec-entry "share" share)))
1480 ((string-equal "obex" method)
1481 (list (tramp-gvfs-mount-spec-entry "type" method)
1482 (tramp-gvfs-mount-spec-entry
1483 "host" (concat "[" (tramp-bluez-address host) "]"))))
1484 ((string-match "\\`dav" method)
1485 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1486 (tramp-gvfs-mount-spec-entry "host" host)
1487 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1488 ((string-equal "afp" method)
1489 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1490 (tramp-gvfs-mount-spec-entry "host" host)
1491 (tramp-gvfs-mount-spec-entry "volume" share)))
1492 (t
1493 (list (tramp-gvfs-mount-spec-entry "type" method)
1494 (tramp-gvfs-mount-spec-entry "host" host))))
1495 ,@(when user
1496 (list (tramp-gvfs-mount-spec-entry "user" user)))
1497 ,@(when domain
1498 (list (tramp-gvfs-mount-spec-entry "domain" domain)))
1499 ,@(when port
1500 (list (tramp-gvfs-mount-spec-entry
1501 "port" (number-to-string port))))))
1502 (mount-pref
1503 (if (and (string-match "\\`dav" method)
1504 (string-match "^/?[^/]+" localname))
1505 (match-string 0 localname)
1506 "/")))
1507
1508 ;; Return.
1509 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1510
1511 \f
1512 ;; Connection functions.
1513
1514 (defun tramp-gvfs-maybe-open-connection (vec)
1515 "Maybe open a connection VEC.
1516 Does not do anything if a connection is already open, but re-opens the
1517 connection if a previous connection has died for some reason."
1518 (tramp-check-proper-method-and-host vec)
1519
1520 ;; We set the file name, in case there are incoming D-Bus signals or
1521 ;; D-Bus errors.
1522 (setq tramp-gvfs-dbus-event-vector vec)
1523
1524 ;; For password handling, we need a process bound to the connection
1525 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1526 ;; better solution?
1527 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1528 (let ((p (make-network-process
1529 :name (tramp-buffer-name vec)
1530 :buffer (tramp-get-connection-buffer vec)
1531 :server t :host 'local :service t)))
1532 (set-process-query-on-exit-flag p nil)))
1533
1534 (unless (tramp-gvfs-connection-mounted-p vec)
1535 (let* ((method (tramp-file-name-method vec))
1536 (user (tramp-file-name-user vec))
1537 (host (tramp-file-name-host vec))
1538 (localname (tramp-file-name-localname vec))
1539 (object-path
1540 (tramp-gvfs-object-path
1541 (tramp-make-tramp-file-name method user host ""))))
1542
1543 (when (and (string-equal method "smb")
1544 (string-equal localname "/"))
1545 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1546
1547 (when (and (string-equal method "afp")
1548 (string-equal localname "/"))
1549 (tramp-error vec 'file-error "Filename must contain an AFP volume"))
1550
1551 (with-tramp-progress-reporter
1552 vec 3
1553 (if (zerop (length user))
1554 (format "Opening connection for %s using %s" host method)
1555 (format "Opening connection for %s@%s using %s" user host method))
1556
1557 ;; Enable `auth-source'.
1558 (tramp-set-connection-property vec "first-password-request" t)
1559
1560 ;; There will be a callback of "askPassword" when a password is
1561 ;; needed.
1562 (dbus-register-method
1563 :session dbus-service-emacs object-path
1564 tramp-gvfs-interface-mountoperation "askPassword"
1565 'tramp-gvfs-handler-askpassword)
1566 (dbus-register-method
1567 :session dbus-service-emacs object-path
1568 tramp-gvfs-interface-mountoperation "AskPassword"
1569 'tramp-gvfs-handler-askpassword)
1570
1571 ;; There could be a callback of "askQuestion" when adding fingerprint.
1572 (dbus-register-method
1573 :session dbus-service-emacs object-path
1574 tramp-gvfs-interface-mountoperation "askQuestion"
1575 'tramp-gvfs-handler-askquestion)
1576 (dbus-register-method
1577 :session dbus-service-emacs object-path
1578 tramp-gvfs-interface-mountoperation "AskQuestion"
1579 'tramp-gvfs-handler-askquestion)
1580
1581 ;; The call must be asynchronously, because of the "askPassword"
1582 ;; or "askQuestion"callbacks.
1583 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1584 (with-tramp-dbus-call-method vec nil
1585 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1586 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1587 (tramp-gvfs-mount-spec vec)
1588 `(:struct :string ,(dbus-get-unique-name :session)
1589 :object-path ,object-path))
1590 (with-tramp-dbus-call-method vec nil
1591 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1592 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1593 (tramp-gvfs-mount-spec vec)
1594 :string (dbus-get-unique-name :session) :object-path object-path))
1595
1596 ;; We must wait, until the mount is applied. This will be
1597 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1598 ;; file property.
1599 (with-timeout
1600 ((or (tramp-get-method-parameter vec 'tramp-connection-timeout)
1601 tramp-connection-timeout)
1602 (if (zerop (length (tramp-file-name-user vec)))
1603 (tramp-error
1604 vec 'file-error
1605 "Timeout reached mounting %s using %s" host method)
1606 (tramp-error
1607 vec 'file-error
1608 "Timeout reached mounting %s@%s using %s" user host method)))
1609 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1610 (read-event nil nil 0.1)))
1611
1612 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1613 ;; is marked with the fuse-mountpoint "/". We shall react.
1614 (when (string-equal
1615 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1616 (tramp-error vec 'file-error "FUSE mount denied"))
1617
1618 ;; Mark it as connected.
1619 (tramp-set-connection-property
1620 (tramp-get-connection-process vec) "connected" t))))
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 (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