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