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