]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-gvfs.el
; Merge from origin/emacs-25
[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 delimiter 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 '("--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 (all-completions
1024 filename
1025 (with-parsed-tramp-file-name (expand-file-name directory) nil
1026 (with-tramp-file-property v localname "file-name-all-completions"
1027 (let ((result '("./" "../"))
1028 entry)
1029 ;; Get a list of directories and files.
1030 (dolist (item (tramp-gvfs-get-directory-attributes directory) result)
1031 (setq entry
1032 (or ;; Use display-name if available (google-drive).
1033 ;(cdr (assoc "standard::display-name" item))
1034 (car item)))
1035 (if (string-equal (cdr (assoc "type" item)) "directory")
1036 (push (file-name-as-directory entry) result)
1037 (push entry result)))))))))
1038
1039 (defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
1040 "Like `file-notify-add-watch' for Tramp files."
1041 (setq file-name (expand-file-name file-name))
1042 (with-parsed-tramp-file-name file-name nil
1043 ;; We cannot watch directories, because `gvfs-monitor-dir' is not
1044 ;; supported for gvfs-mounted directories.
1045 (when (file-directory-p file-name)
1046 (tramp-error
1047 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1048 (let* ((default-directory (file-name-directory file-name))
1049 (events
1050 (cond
1051 ((and (memq 'change flags) (memq 'attribute-change flags))
1052 '(created changed changes-done-hint moved deleted
1053 attribute-changed))
1054 ((memq 'change flags)
1055 '(created changed changes-done-hint moved deleted))
1056 ((memq 'attribute-change flags) '(attribute-changed))))
1057 (p (start-process
1058 "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
1059 "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name))))
1060 (if (not (processp p))
1061 (tramp-error
1062 v 'file-notify-error "Monitoring not supported for `%s'" file-name)
1063 (tramp-message
1064 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
1065 (tramp-set-connection-property p "vector" v)
1066 (process-put p 'events events)
1067 (process-put p 'watch-name localname)
1068 (set-process-query-on-exit-flag p nil)
1069 (set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
1070 ;; There might be an error if the monitor is not supported.
1071 ;; Give the filter a chance to read the output.
1072 (tramp-accept-process-output p 1)
1073 (unless (memq (process-status p) '(run open))
1074 (tramp-error
1075 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
1076 p))))
1077
1078 (defun tramp-gvfs-monitor-file-process-filter (proc string)
1079 "Read output from \"gvfs-monitor-file\" and add corresponding \
1080 file-notify events."
1081 (let* ((rest-string (process-get proc 'rest-string))
1082 (dd (with-current-buffer (process-buffer proc) default-directory))
1083 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
1084 (when rest-string
1085 (tramp-message proc 10 "Previous string:\n%s" rest-string))
1086 (tramp-message proc 6 "%S\n%s" proc string)
1087 (setq string (concat rest-string string)
1088 ;; Attribute change is returned in unused wording.
1089 string (replace-regexp-in-string
1090 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1091 (when (string-match "Monitoring not supported" string)
1092 (delete-process proc))
1093
1094 (while (string-match
1095 (concat "^[\n\r]*"
1096 "File Monitor Event:[\n\r]+"
1097 "File = \\([^\n\r]+\\)[\n\r]+"
1098 "Event = \\([^[:blank:]]+\\)[\n\r]+")
1099 string)
1100 (let ((file (match-string 1 string))
1101 (action (intern-soft
1102 (replace-regexp-in-string
1103 "_" "-" (downcase (match-string 2 string))))))
1104 (setq string (replace-match "" nil nil string))
1105 ;; File names are returned as URL paths. We must convert them.
1106 (when (string-match ddu file)
1107 (setq file (replace-match dd nil nil file)))
1108 (while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
1109 (setq file
1110 (replace-match
1111 (char-to-string (string-to-number (match-string 1 file) 16))
1112 nil nil file)))
1113 ;; Usually, we would add an Emacs event now. Unfortunately,
1114 ;; `unread-command-events' does not accept several events at
1115 ;; once. Therefore, we apply the callback directly.
1116 (tramp-compat-funcall 'file-notify-callback (list proc action file))))
1117
1118 ;; Save rest of the string.
1119 (when (zerop (length string)) (setq string nil))
1120 (when string (tramp-message proc 10 "Rest string:\n%s" string))
1121 (process-put proc 'rest-string string)))
1122
1123 (defun tramp-gvfs-handle-file-readable-p (filename)
1124 "Like `file-readable-p' for Tramp files."
1125 (with-parsed-tramp-file-name filename nil
1126 (with-tramp-file-property v localname "file-readable-p"
1127 (tramp-check-cached-permissions v ?r))))
1128
1129 (defun tramp-gvfs-handle-file-writable-p (filename)
1130 "Like `file-writable-p' for Tramp files."
1131 (with-parsed-tramp-file-name filename nil
1132 (with-tramp-file-property v localname "file-writable-p"
1133 (if (file-exists-p filename)
1134 (tramp-check-cached-permissions v ?w)
1135 ;; If file doesn't exist, check if directory is writable.
1136 (and (file-directory-p (file-name-directory filename))
1137 (file-writable-p (file-name-directory filename)))))))
1138
1139 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
1140 "Like `make-directory' for Tramp files."
1141 (setq dir (directory-file-name (expand-file-name dir)))
1142 (with-parsed-tramp-file-name dir nil
1143 (tramp-flush-file-property v (file-name-directory localname))
1144 (tramp-flush-directory-property v localname)
1145 (save-match-data
1146 (let ((ldir (file-name-directory dir)))
1147 ;; Make missing directory parts. "gvfs-mkdir -p ..." does not
1148 ;; work robust.
1149 (when (and parents (not (file-directory-p ldir)))
1150 (make-directory ldir parents))
1151 ;; Just do it.
1152 (unless (tramp-gvfs-send-command
1153 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1154 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1155
1156 (defun tramp-gvfs-handle-rename-file
1157 (filename newname &optional ok-if-already-exists)
1158 "Like `rename-file' for Tramp files."
1159 ;; Check if both files are local -- invoke normal rename-file.
1160 ;; Otherwise, use Tramp from local system.
1161 (setq filename (expand-file-name filename))
1162 (setq newname (expand-file-name newname))
1163 ;; At least one file a Tramp file?
1164 (if (or (tramp-tramp-file-p filename)
1165 (tramp-tramp-file-p newname))
1166 (tramp-gvfs-do-copy-or-rename-file
1167 'rename filename newname ok-if-already-exists
1168 'keep-date 'preserve-uid-gid)
1169 (tramp-run-real-handler
1170 'rename-file (list filename newname ok-if-already-exists))))
1171
1172 (defun tramp-gvfs-handle-write-region
1173 (start end filename &optional append visit lockname confirm)
1174 "Like `write-region' for Tramp files."
1175 (with-parsed-tramp-file-name filename nil
1176 (when (and confirm (file-exists-p filename))
1177 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
1178 (tramp-error v 'file-error "File not overwritten")))
1179
1180 (let ((tmpfile (tramp-compat-make-temp-file filename)))
1181 (when (and append (file-exists-p filename))
1182 (copy-file filename tmpfile 'ok))
1183 ;; We say `no-message' here because we don't want the visited file
1184 ;; modtime data to be clobbered from the temp file. We call
1185 ;; `set-visited-file-modtime' ourselves later on.
1186 (tramp-run-real-handler
1187 'write-region
1188 (if confirm ; don't pass this arg unless defined for backward compat.
1189 (list start end tmpfile append 'no-message lockname confirm)
1190 (list start end tmpfile append 'no-message lockname)))
1191 (condition-case nil
1192 (rename-file tmpfile filename 'ok-if-already-exists)
1193 (error
1194 (delete-file tmpfile)
1195 (tramp-error
1196 v 'file-error "Couldn't write region to `%s'" filename))))
1197
1198 (tramp-flush-file-property v (file-name-directory localname))
1199 (tramp-flush-file-property v localname)
1200
1201 ;; Set file modification time.
1202 (when (or (eq visit t) (stringp visit))
1203 (set-visited-file-modtime (nth 5 (file-attributes filename))))
1204
1205 ;; The end.
1206 (when (or (eq visit t) (null visit) (stringp visit))
1207 (tramp-message v 0 "Wrote %s" filename))
1208 (run-hooks 'tramp-handle-write-region-hook)))
1209
1210 \f
1211 ;; File name conversions.
1212
1213 (defun tramp-gvfs-url-file-name (filename)
1214 "Return FILENAME in URL syntax."
1215 ;; "/" must NOT be hexlified.
1216 (let ((url-unreserved-chars (cons ?/ url-unreserved-chars))
1217 result)
1218 (setq
1219 result
1220 (url-recreate-url
1221 (if (tramp-tramp-file-p filename)
1222 (with-parsed-tramp-file-name filename nil
1223 (when (and user (string-match tramp-user-with-domain-regexp user))
1224 (setq user
1225 (concat (match-string 2 user) ";" (match-string 1 user))))
1226 (url-parse-make-urlobj
1227 method (and user (url-hexify-string user)) nil
1228 (tramp-file-name-real-host v) (tramp-file-name-port v)
1229 (and localname (url-hexify-string localname)) nil nil t))
1230 (url-parse-make-urlobj
1231 "file" nil nil nil nil
1232 (url-hexify-string (file-truename filename)) nil nil t))))
1233 (when (tramp-tramp-file-p filename)
1234 (with-parsed-tramp-file-name filename nil
1235 (tramp-message v 10 "remote file `%s' is URL `%s'" filename result)))
1236 result))
1237
1238 (defun tramp-gvfs-object-path (filename)
1239 "Create a D-Bus object path from FILENAME."
1240 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
1241
1242 (defun tramp-gvfs-file-name (object-path)
1243 "Retrieve file name from D-Bus OBJECT-PATH."
1244 (dbus-unescape-from-identifier
1245 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
1246
1247 (defun tramp-bluez-address (device)
1248 "Return bluetooth device address from a given bluetooth DEVICE name."
1249 (when (stringp device)
1250 (if (string-match tramp-ipv6-regexp device)
1251 (match-string 0 device)
1252 (cadr (assoc device (tramp-bluez-list-devices))))))
1253
1254 (defun tramp-bluez-device (address)
1255 "Return bluetooth device name from a given bluetooth device ADDRESS.
1256 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
1257 (when (stringp address)
1258 (while (string-match "[][]" address)
1259 (setq address (replace-match "" t t address)))
1260 (let (result)
1261 (dolist (item (tramp-bluez-list-devices) result)
1262 (when (string-match address (cadr item))
1263 (setq result (car item)))))))
1264
1265 \f
1266 ;; D-Bus GVFS functions.
1267
1268 (defun tramp-gvfs-handler-askpassword (message user domain flags)
1269 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
1270 (let* ((filename
1271 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
1272 (pw-prompt
1273 (format
1274 "%s for %s "
1275 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
1276 (capitalize (match-string 1 message))
1277 "Password")
1278 filename))
1279 password)
1280
1281 (condition-case nil
1282 (with-parsed-tramp-file-name filename l
1283 (when (and (zerop (length user))
1284 (not
1285 (zerop (logand flags tramp-gvfs-password-need-username))))
1286 (setq user (read-string "User name: ")))
1287 (when (and (zerop (length domain))
1288 (not
1289 (zerop (logand flags tramp-gvfs-password-need-domain))))
1290 (setq domain (read-string "Domain name: ")))
1291
1292 (tramp-message l 6 "%S %S %S %d" message user domain flags)
1293 (unless (tramp-get-connection-property l "first-password-request" nil)
1294 (tramp-clear-passwd l))
1295
1296 (setq tramp-current-method l-method
1297 tramp-current-user user
1298 tramp-current-host l-host
1299 password (tramp-read-passwd
1300 (tramp-get-connection-process l) pw-prompt))
1301
1302 ;; Return result.
1303 (if (stringp password)
1304 (list
1305 t ;; password handled.
1306 nil ;; no abort of D-Bus.
1307 password
1308 (tramp-file-name-real-user l)
1309 domain
1310 nil ;; not anonymous.
1311 0) ;; no password save.
1312 ;; No password provided.
1313 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
1314
1315 ;; When QUIT is raised, we shall return this information to D-Bus.
1316 (quit (list nil t "" "" "" nil 0)))))
1317
1318 (defun tramp-gvfs-handler-askquestion (message choices)
1319 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
1320 (save-window-excursion
1321 (let ((enable-recursive-minibuffers t)
1322 choice)
1323
1324 (condition-case nil
1325 (with-parsed-tramp-file-name
1326 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
1327 (tramp-message v 6 "%S %S" message choices)
1328
1329 ;; In theory, there can be several choices. Until now,
1330 ;; there is only the question whether to accept an unknown
1331 ;; host signature.
1332 (with-temp-buffer
1333 ;; Preserve message for `progress-reporter'.
1334 (with-temp-message ""
1335 (insert message)
1336 (pop-to-buffer (current-buffer))
1337 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
1338 (tramp-message v 6 "%d" choice)))
1339
1340 ;; When the choice is "no", we set a dummy fuse-mountpoint
1341 ;; in order to leave the timeout.
1342 (unless (zerop choice)
1343 (tramp-set-file-property v "/" "fuse-mountpoint" "/"))
1344
1345 (list
1346 t ;; handled.
1347 nil ;; no abort of D-Bus.
1348 choice))
1349
1350 ;; When QUIT is raised, we shall return this information to D-Bus.
1351 (quit (list nil t 0))))))
1352
1353 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
1354 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
1355 \"org.gtk.vfs.MountTracker.unmounted\" signals."
1356 (ignore-errors
1357 (let ((signal-name (dbus-event-member-name last-input-event))
1358 (elt mount-info))
1359 ;; Jump over the first elements of the mount info. Since there
1360 ;; were changes in the entries, we cannot access dedicated
1361 ;; elements.
1362 (while (stringp (car elt)) (setq elt (cdr elt)))
1363 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string (cadr elt)))
1364 (mount-spec (caddr elt))
1365 (default-location (tramp-gvfs-dbus-byte-array-to-string
1366 (cadddr elt)))
1367 (method (tramp-gvfs-dbus-byte-array-to-string
1368 (cadr (assoc "type" (cadr mount-spec)))))
1369 (user (tramp-gvfs-dbus-byte-array-to-string
1370 (cadr (assoc "user" (cadr mount-spec)))))
1371 (domain (tramp-gvfs-dbus-byte-array-to-string
1372 (cadr (assoc "domain" (cadr mount-spec)))))
1373 (host (tramp-gvfs-dbus-byte-array-to-string
1374 (cadr (or (assoc "host" (cadr mount-spec))
1375 (assoc "server" (cadr mount-spec))))))
1376 (port (tramp-gvfs-dbus-byte-array-to-string
1377 (cadr (assoc "port" (cadr mount-spec)))))
1378 (ssl (tramp-gvfs-dbus-byte-array-to-string
1379 (cadr (assoc "ssl" (cadr mount-spec)))))
1380 (prefix (concat
1381 (tramp-gvfs-dbus-byte-array-to-string
1382 (car mount-spec))
1383 (tramp-gvfs-dbus-byte-array-to-string
1384 (or (cadr (assoc "share" (cadr mount-spec)))
1385 (cadr (assoc "volume" (cadr mount-spec))))))))
1386 (when (string-match "^\\(afp\\|smb\\)" method)
1387 (setq method (match-string 1 method)))
1388 (when (string-equal "obex" method)
1389 (setq host (tramp-bluez-device host)))
1390 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1391 (setq method "davs"))
1392 (unless (zerop (length domain))
1393 (setq user (concat user tramp-prefix-domain-format domain)))
1394 (unless (zerop (length port))
1395 (setq host (concat host tramp-prefix-port-format port)))
1396 (with-parsed-tramp-file-name
1397 (tramp-make-tramp-file-name method user host "") nil
1398 (tramp-message
1399 v 6 "%s %s"
1400 signal-name (tramp-gvfs-stringify-dbus-message mount-info))
1401 (tramp-set-file-property v "/" "list-mounts" 'undef)
1402 (if (string-equal (downcase signal-name) "unmounted")
1403 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
1404 ;; Set prefix, mountpoint and location.
1405 (unless (string-equal prefix "/")
1406 (tramp-set-file-property v "/" "prefix" prefix))
1407 (tramp-set-file-property v "/" "fuse-mountpoint" fuse-mountpoint)
1408 (tramp-set-file-property
1409 v "/" "default-location" default-location)))))))
1410
1411 (when tramp-gvfs-enabled
1412 (dbus-register-signal
1413 :session nil tramp-gvfs-path-mounttracker
1414 tramp-gvfs-interface-mounttracker "mounted"
1415 'tramp-gvfs-handler-mounted-unmounted)
1416 (dbus-register-signal
1417 :session nil tramp-gvfs-path-mounttracker
1418 tramp-gvfs-interface-mounttracker "Mounted"
1419 'tramp-gvfs-handler-mounted-unmounted)
1420
1421 (dbus-register-signal
1422 :session nil tramp-gvfs-path-mounttracker
1423 tramp-gvfs-interface-mounttracker "unmounted"
1424 'tramp-gvfs-handler-mounted-unmounted)
1425 (dbus-register-signal
1426 :session nil tramp-gvfs-path-mounttracker
1427 tramp-gvfs-interface-mounttracker "Unmounted"
1428 'tramp-gvfs-handler-mounted-unmounted))
1429
1430 (defun tramp-gvfs-connection-mounted-p (vec)
1431 "Check, whether the location is already mounted."
1432 (or
1433 (tramp-get-file-property vec "/" "fuse-mountpoint" nil)
1434 (catch 'mounted
1435 (dolist
1436 (elt
1437 (with-tramp-file-property vec "/" "list-mounts"
1438 (with-tramp-dbus-call-method vec t
1439 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1440 tramp-gvfs-interface-mounttracker tramp-gvfs-listmounts))
1441 nil)
1442 ;; Jump over the first elements of the mount info. Since there
1443 ;; were changes in the entries, we cannot access dedicated
1444 ;; elements.
1445 (while (stringp (car elt)) (setq elt (cdr elt)))
1446 (let* ((fuse-mountpoint (tramp-gvfs-dbus-byte-array-to-string
1447 (cadr elt)))
1448 (mount-spec (caddr elt))
1449 (default-location (tramp-gvfs-dbus-byte-array-to-string
1450 (cadddr elt)))
1451 (method (tramp-gvfs-dbus-byte-array-to-string
1452 (cadr (assoc "type" (cadr mount-spec)))))
1453 (user (tramp-gvfs-dbus-byte-array-to-string
1454 (cadr (assoc "user" (cadr mount-spec)))))
1455 (domain (tramp-gvfs-dbus-byte-array-to-string
1456 (cadr (assoc "domain" (cadr mount-spec)))))
1457 (host (tramp-gvfs-dbus-byte-array-to-string
1458 (cadr (or (assoc "host" (cadr mount-spec))
1459 (assoc "server" (cadr mount-spec))))))
1460 (port (tramp-gvfs-dbus-byte-array-to-string
1461 (cadr (assoc "port" (cadr mount-spec)))))
1462 (ssl (tramp-gvfs-dbus-byte-array-to-string
1463 (cadr (assoc "ssl" (cadr mount-spec)))))
1464 (prefix (concat
1465 (tramp-gvfs-dbus-byte-array-to-string
1466 (car mount-spec))
1467 (tramp-gvfs-dbus-byte-array-to-string
1468 (or
1469 (cadr (assoc "share" (cadr mount-spec)))
1470 (cadr (assoc "volume" (cadr mount-spec))))))))
1471 (when (string-match "^\\(afp\\|smb\\)" method)
1472 (setq method (match-string 1 method)))
1473 (when (string-equal "obex" method)
1474 (setq host (tramp-bluez-device host)))
1475 (when (and (string-equal "dav" method) (string-equal "true" ssl))
1476 (setq method "davs"))
1477 (when (and (string-equal "synce" method) (zerop (length user)))
1478 (setq user (or (tramp-file-name-user vec) "")))
1479 (unless (zerop (length domain))
1480 (setq user (concat user tramp-prefix-domain-format domain)))
1481 (unless (zerop (length port))
1482 (setq host (concat host tramp-prefix-port-format port)))
1483 (when (and
1484 (string-equal method (tramp-file-name-method vec))
1485 (string-equal user (or (tramp-file-name-user vec) ""))
1486 (string-equal host (tramp-file-name-host vec))
1487 (string-match (concat "^" (regexp-quote prefix))
1488 (tramp-file-name-localname vec)))
1489 ;; Set prefix, mountpoint and location.
1490 (unless (string-equal prefix "/")
1491 (tramp-set-file-property vec "/" "prefix" prefix))
1492 (tramp-set-file-property vec "/" "fuse-mountpoint" fuse-mountpoint)
1493 (tramp-set-file-property vec "/" "default-location" default-location)
1494 (throw 'mounted t)))))))
1495
1496 (defun tramp-gvfs-mount-spec-entry (key value)
1497 "Construct a mount-spec entry to be used in a mount_spec.
1498 It was \"a(say)\", but has changed to \"a{sv})\"."
1499 (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
1500 (list :dict-entry key
1501 (list :variant (tramp-gvfs-dbus-string-to-byte-array value)))
1502 (list :struct key (tramp-gvfs-dbus-string-to-byte-array value))))
1503
1504 (defun tramp-gvfs-mount-spec (vec)
1505 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
1506 (let* ((method (tramp-file-name-method vec))
1507 (user (tramp-file-name-real-user vec))
1508 (domain (tramp-file-name-domain vec))
1509 (host (tramp-file-name-real-host vec))
1510 (port (tramp-file-name-port vec))
1511 (localname (tramp-file-name-localname vec))
1512 (share (when (string-match "^/?\\([^/]+\\)" localname)
1513 (match-string 1 localname)))
1514 (ssl (when (string-match "^davs" method) "true" "false"))
1515 (mount-spec
1516 `(:array
1517 ,@(cond
1518 ((string-equal "smb" method)
1519 (list (tramp-gvfs-mount-spec-entry "type" "smb-share")
1520 (tramp-gvfs-mount-spec-entry "server" host)
1521 (tramp-gvfs-mount-spec-entry "share" share)))
1522 ((string-equal "obex" method)
1523 (list (tramp-gvfs-mount-spec-entry "type" method)
1524 (tramp-gvfs-mount-spec-entry
1525 "host" (concat "[" (tramp-bluez-address host) "]"))))
1526 ((string-match "\\`dav" method)
1527 (list (tramp-gvfs-mount-spec-entry "type" "dav")
1528 (tramp-gvfs-mount-spec-entry "host" host)
1529 (tramp-gvfs-mount-spec-entry "ssl" ssl)))
1530 ((string-equal "afp" method)
1531 (list (tramp-gvfs-mount-spec-entry "type" "afp-volume")
1532 (tramp-gvfs-mount-spec-entry "host" host)
1533 (tramp-gvfs-mount-spec-entry "volume" share)))
1534 (t
1535 (list (tramp-gvfs-mount-spec-entry "type" method)
1536 (tramp-gvfs-mount-spec-entry "host" host))))
1537 ,@(when user
1538 (list (tramp-gvfs-mount-spec-entry "user" user)))
1539 ,@(when domain
1540 (list (tramp-gvfs-mount-spec-entry "domain" domain)))
1541 ,@(when port
1542 (list (tramp-gvfs-mount-spec-entry
1543 "port" (number-to-string port))))))
1544 (mount-pref
1545 (if (and (string-match "\\`dav" method)
1546 (string-match "^/?[^/]+" localname))
1547 (match-string 0 localname)
1548 "/")))
1549
1550 ;; Return.
1551 `(:struct ,(tramp-gvfs-dbus-string-to-byte-array mount-pref) ,mount-spec)))
1552
1553 \f
1554 ;; Connection functions.
1555
1556 (defun tramp-gvfs-maybe-open-connection (vec)
1557 "Maybe open a connection VEC.
1558 Does not do anything if a connection is already open, but re-opens the
1559 connection if a previous connection has died for some reason."
1560 (tramp-check-proper-method-and-host vec)
1561
1562 ;; We set the file name, in case there are incoming D-Bus signals or
1563 ;; D-Bus errors.
1564 (setq tramp-gvfs-dbus-event-vector vec)
1565
1566 ;; For password handling, we need a process bound to the connection
1567 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1568 ;; better solution?
1569 (unless (get-buffer-process (tramp-get-connection-buffer vec))
1570 (let ((p (make-network-process
1571 :name (tramp-buffer-name vec)
1572 :buffer (tramp-get-connection-buffer vec)
1573 :server t :host 'local :service t :noquery t)))
1574 (set-process-query-on-exit-flag p nil)))
1575
1576 (unless (tramp-gvfs-connection-mounted-p vec)
1577 (let* ((method (tramp-file-name-method vec))
1578 (user (tramp-file-name-user vec))
1579 (host (tramp-file-name-host vec))
1580 (localname (tramp-file-name-localname vec))
1581 (object-path
1582 (tramp-gvfs-object-path
1583 (tramp-make-tramp-file-name method user host ""))))
1584
1585 (when (and (string-equal method "smb")
1586 (string-equal localname "/"))
1587 (tramp-error vec 'file-error "Filename must contain a Windows share"))
1588
1589 (when (and (string-equal method "afp")
1590 (string-equal localname "/"))
1591 (tramp-error vec 'file-error "Filename must contain an AFP volume"))
1592
1593 (with-tramp-progress-reporter
1594 vec 3
1595 (if (zerop (length user))
1596 (format "Opening connection for %s using %s" host method)
1597 (format "Opening connection for %s@%s using %s" user host method))
1598
1599 ;; Enable `auth-source'.
1600 (tramp-set-connection-property vec "first-password-request" t)
1601
1602 ;; There will be a callback of "askPassword" when a password is
1603 ;; needed.
1604 (dbus-register-method
1605 :session dbus-service-emacs object-path
1606 tramp-gvfs-interface-mountoperation "askPassword"
1607 'tramp-gvfs-handler-askpassword)
1608 (dbus-register-method
1609 :session dbus-service-emacs object-path
1610 tramp-gvfs-interface-mountoperation "AskPassword"
1611 'tramp-gvfs-handler-askpassword)
1612
1613 ;; There could be a callback of "askQuestion" when adding fingerprint.
1614 (dbus-register-method
1615 :session dbus-service-emacs object-path
1616 tramp-gvfs-interface-mountoperation "askQuestion"
1617 'tramp-gvfs-handler-askquestion)
1618 (dbus-register-method
1619 :session dbus-service-emacs object-path
1620 tramp-gvfs-interface-mountoperation "AskQuestion"
1621 'tramp-gvfs-handler-askquestion)
1622
1623 ;; The call must be asynchronously, because of the "askPassword"
1624 ;; or "askQuestion"callbacks.
1625 (if (string-match "(so)$" tramp-gvfs-mountlocation-signature)
1626 (with-tramp-dbus-call-method vec nil
1627 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1628 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1629 (tramp-gvfs-mount-spec vec)
1630 `(:struct :string ,(dbus-get-unique-name :session)
1631 :object-path ,object-path))
1632 (with-tramp-dbus-call-method vec nil
1633 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1634 tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation
1635 (tramp-gvfs-mount-spec vec)
1636 :string (dbus-get-unique-name :session) :object-path object-path))
1637
1638 ;; We must wait, until the mount is applied. This will be
1639 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1640 ;; file property.
1641 (with-timeout
1642 ((or (tramp-get-method-parameter vec 'tramp-connection-timeout)
1643 tramp-connection-timeout)
1644 (if (zerop (length (tramp-file-name-user vec)))
1645 (tramp-error
1646 vec 'file-error
1647 "Timeout reached mounting %s using %s" host method)
1648 (tramp-error
1649 vec 'file-error
1650 "Timeout reached mounting %s@%s using %s" user host method)))
1651 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1652 (read-event nil nil 0.1)))
1653
1654 ;; If `tramp-gvfs-handler-askquestion' has returned "No", it
1655 ;; is marked with the fuse-mountpoint "/". We shall react.
1656 (when (string-equal
1657 (tramp-get-file-property vec "/" "fuse-mountpoint" "") "/")
1658 (tramp-error vec 'file-error "FUSE mount denied"))
1659
1660 ;; Mark it as connected.
1661 (tramp-set-connection-property
1662 (tramp-get-connection-process vec) "connected" t))))
1663
1664 ;; In `tramp-check-cached-permissions', the connection properties
1665 ;; {uig,gid}-{integer,string} are used. We set them to their local
1666 ;; counterparts.
1667 (with-tramp-connection-property
1668 vec "uid-integer" (tramp-get-local-uid 'integer))
1669 (with-tramp-connection-property
1670 vec "gid-integer" (tramp-get-local-gid 'integer))
1671 (with-tramp-connection-property
1672 vec "uid-string" (tramp-get-local-uid 'string))
1673 (with-tramp-connection-property
1674 vec "gid-string" (tramp-get-local-gid 'string)))
1675
1676 (defun tramp-gvfs-send-command (vec command &rest args)
1677 "Send the COMMAND with its ARGS to connection VEC.
1678 COMMAND is usually a command from the gvfs-* utilities.
1679 `call-process' is applied, and it returns t if the return code is zero."
1680 (let* ((locale (tramp-get-local-locale vec))
1681 (process-environment
1682 (append
1683 `(,(format "LANG=%s" locale)
1684 ,(format "LANGUAGE=%s" locale)
1685 ,(format "LC_ALL=%s" locale))
1686 process-environment)))
1687 (with-current-buffer (tramp-get-connection-buffer vec)
1688 (tramp-gvfs-maybe-open-connection vec)
1689 (erase-buffer)
1690 (zerop (apply 'tramp-call-process vec command nil t nil args)))))
1691
1692 \f
1693 ;; D-Bus BLUEZ functions.
1694
1695 (defun tramp-bluez-list-devices ()
1696 "Return all discovered bluetooth devices as list.
1697 Every entry is a list (NAME ADDRESS).
1698
1699 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1700 discovery happened more time before indicated there, a rescan will be
1701 started, which lasts some ten seconds. Otherwise, cached results will
1702 be used."
1703 ;; Reset the scanned devices list if time has passed.
1704 (and (integerp tramp-bluez-discover-devices-timeout)
1705 (integerp tramp-bluez-discovery)
1706 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1707 tramp-bluez-discover-devices-timeout)
1708 (setq tramp-bluez-devices nil))
1709
1710 ;; Rescan if needed.
1711 (unless tramp-bluez-devices
1712 (let ((object-path
1713 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1714 :system tramp-bluez-service "/"
1715 tramp-bluez-interface-manager "DefaultAdapter")))
1716 (setq tramp-bluez-devices nil
1717 tramp-bluez-discovery t)
1718 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1719 :system tramp-bluez-service object-path
1720 tramp-bluez-interface-adapter "StartDiscovery")
1721 (while tramp-bluez-discovery
1722 (read-event nil nil 0.1))))
1723 (setq tramp-bluez-discovery (current-time))
1724 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1725 tramp-bluez-devices)
1726
1727 (defun tramp-bluez-property-changed (property value)
1728 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1729 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1730 (cond
1731 ((string-equal property "Discovering")
1732 (unless (car value)
1733 ;; "Discovering" FALSE means discovery run has been completed.
1734 ;; We stop it, because we don't need another run.
1735 (setq tramp-bluez-discovery nil)
1736 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1737 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1738 tramp-bluez-interface-adapter "StopDiscovery")))))
1739
1740 (when tramp-gvfs-enabled
1741 (dbus-register-signal
1742 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1743 'tramp-bluez-property-changed))
1744
1745 (defun tramp-bluez-device-found (device args)
1746 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1747 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1748 (let ((alias (car (cadr (assoc "Alias" args))))
1749 (address (car (cadr (assoc "Address" args)))))
1750 ;; Maybe we shall check the device class for being a proper
1751 ;; device, and call also SDP in order to find the obex service.
1752 (add-to-list 'tramp-bluez-devices (list alias address))))
1753
1754 (when tramp-gvfs-enabled
1755 (dbus-register-signal
1756 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1757 'tramp-bluez-device-found))
1758
1759 (defun tramp-bluez-parse-device-names (_ignore)
1760 "Return a list of (nil host) tuples allowed to access."
1761 (mapcar
1762 (lambda (x) (list nil (car x)))
1763 (tramp-bluez-list-devices)))
1764
1765 ;; Add completion function for OBEX method.
1766 (when (and tramp-gvfs-enabled
1767 (member tramp-bluez-service (dbus-list-known-names :system)))
1768 (tramp-set-completion-function
1769 "obex" '((tramp-bluez-parse-device-names ""))))
1770
1771 \f
1772 ;; D-Bus zeroconf functions.
1773
1774 (defun tramp-zeroconf-parse-device-names (service)
1775 "Return a list of (user host) tuples allowed to access."
1776 (mapcar
1777 (lambda (x)
1778 (let ((host (zeroconf-service-host x))
1779 (port (zeroconf-service-port x))
1780 (text (zeroconf-service-txt x))
1781 user)
1782 (when port
1783 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1784 ;; A user is marked in a TXT field like "u=guest".
1785 (while text
1786 (when (string-match "u=\\(.+\\)$" (car text))
1787 (setq user (match-string 1 (car text))))
1788 (setq text (cdr text)))
1789 (list user host)))
1790 (zeroconf-list-services service)))
1791
1792 ;; We use the TRIM argument of `split-string', which exist since Emacs
1793 ;; 24.4. I mask this for older Emacs versions, there is no harm.
1794 (defun tramp-gvfs-parse-device-names (service)
1795 "Return a list of (user host) tuples allowed to access.
1796 This uses \"avahi-browse\" in case D-Bus is not enabled in Avahi."
1797 (let ((result
1798 (ignore-errors
1799 (tramp-compat-funcall
1800 'split-string
1801 (shell-command-to-string (format "avahi-browse -trkp %s" service))
1802 "[\n\r]+" 'omit "^\\+;.*$"))))
1803 (delete-dups
1804 (mapcar
1805 (lambda (x)
1806 (let* ((list (split-string x ";"))
1807 (host (nth 6 list))
1808 (port (nth 8 list))
1809 (text (tramp-compat-funcall
1810 'split-string (nth 9 list) "\" \"" 'omit "\""))
1811 user)
1812 ; (when (and port (not (string-equal port "0")))
1813 ; (setq host (format "%s%s%s" host tramp-prefix-port-regexp port)))
1814 ;; A user is marked in a TXT field like "u=guest".
1815 (while text
1816 (when (string-match "u=\\(.+\\)$" (car text))
1817 (setq user (match-string 1 (car text))))
1818 (setq text (cdr text)))
1819 (list user host)))
1820 result))))
1821
1822 ;; Add completion functions for AFP, DAV, DAVS, SFTP and SMB methods.
1823 (when tramp-gvfs-enabled
1824 ;; Suppress D-Bus error messages.
1825 (let (tramp-gvfs-dbus-event-vector)
1826 (zeroconf-init tramp-gvfs-zeroconf-domain)
1827 (if (zeroconf-list-service-types)
1828 (progn
1829 (tramp-set-completion-function
1830 "afp" '((tramp-zeroconf-parse-device-names "_afpovertcp._tcp")))
1831 (tramp-set-completion-function
1832 "dav" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1833 (tramp-set-completion-function
1834 "davs" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
1835 (tramp-set-completion-function
1836 "sftp" '((tramp-zeroconf-parse-device-names "_ssh._tcp")
1837 (tramp-zeroconf-parse-device-names "_workstation._tcp")))
1838 (when (member "smb" tramp-gvfs-methods)
1839 (tramp-set-completion-function
1840 "smb" '((tramp-zeroconf-parse-device-names "_smb._tcp")))))
1841
1842 (when (executable-find "avahi-browse")
1843 (tramp-set-completion-function
1844 "afp" '((tramp-gvfs-parse-device-names "_afpovertcp._tcp")))
1845 (tramp-set-completion-function
1846 "dav" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1847 (tramp-set-completion-function
1848 "davs" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
1849 (tramp-set-completion-function
1850 "sftp" '((tramp-gvfs-parse-device-names "_ssh._tcp")
1851 (tramp-gvfs-parse-device-names "_workstation._tcp")))
1852 (when (member "smb" tramp-gvfs-methods)
1853 (tramp-set-completion-function
1854 "smb" '((tramp-gvfs-parse-device-names "_smb._tcp"))))))))
1855
1856 \f
1857 ;; D-Bus SYNCE functions.
1858
1859 (defun tramp-synce-list-devices ()
1860 "Return all discovered synce devices as list.
1861 They are retrieved from the hal daemon."
1862 (let (tramp-synce-devices)
1863 (dolist (device
1864 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1865 :system tramp-hal-service tramp-hal-path-manager
1866 tramp-hal-interface-manager "GetAllDevices"))
1867 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1868 :system tramp-hal-service device tramp-hal-interface-device
1869 "PropertyExists" "sync.plugin")
1870 (let ((prop
1871 (with-tramp-dbus-call-method
1872 tramp-gvfs-dbus-event-vector t
1873 :system tramp-hal-service device tramp-hal-interface-device
1874 "GetPropertyString" "pda.pocketpc.name")))
1875 (unless (member prop tramp-synce-devices)
1876 (push prop tramp-synce-devices)))))
1877 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1878 tramp-synce-devices))
1879
1880 (defun tramp-synce-parse-device-names (_ignore)
1881 "Return a list of (nil host) tuples allowed to access."
1882 (mapcar
1883 (lambda (x) (list nil x))
1884 (tramp-synce-list-devices)))
1885
1886 ;; Add completion function for SYNCE method.
1887 (when tramp-gvfs-enabled
1888 (tramp-set-completion-function
1889 "synce" '((tramp-synce-parse-device-names ""))))
1890
1891 (add-hook 'tramp-unload-hook
1892 (lambda ()
1893 (unload-feature 'tramp-gvfs 'force)))
1894
1895 (provide 'tramp-gvfs)
1896
1897 ;;; TODO:
1898
1899 ;; * Host name completion via afp-server, smb-server or smb-network.
1900 ;; * Check how two shares of the same SMB server can be mounted in
1901 ;; parallel.
1902 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1903 ;; capability.
1904 ;; * Implement obex for other serial communication but bluetooth.
1905
1906 ;;; tramp-gvfs.el ends here