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