tramp-gvfs.el 90.9 KB
Newer Older
Michael Albinus's avatar
Michael Albinus committed
1
;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon  -*- lexical-binding:t -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 5 6

;; Author: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes
7
;; Package: tramp
8

Glenn Morris's avatar
Glenn Morris committed
9 10 11
;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
12 13 14 15
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

Glenn Morris's avatar
Glenn Morris committed
16 17 18 19
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; 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 <https://www.gnu.org/licenses/>.
23 24 25 26

;;; Commentary:

;; 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 29 30
;; with GVFS 0.2.5 (Ubuntu 8.04, Gnome 2.22), but there is an
;; incompatibility with the mount_info structure, which has been
;; worked around.
31

32
;; It has also been tested with GVFS 1.6 (Ubuntu 10.04, Gnome 2.30),
33 34 35
;; where the default_location has been added to mount_info (see
;; <https://bugzilla.gnome.org/show_bug.cgi?id=561998>.

36 37 38
;; With GVFS 1.14 (Ubuntu 12.10, Gnome 3.6) the interfaces have been
;; changed, again.  So we must introspect the D-Bus interfaces.

39 40 41
;; All actions to mount a remote location, and to retrieve mount
;; information, are performed by D-Bus messages.  File operations
;; themselves are performed via the mounted filesystem in ~/.gvfs.
42
;; Consequently, GNU Emacs with enabled D-Bus bindings is a
43 44
;; precondition.

45 46 47 48
;; The GVFS D-Bus interface is said to be unstable.  There were even
;; no introspection data before GVFS 1.14.  The interface, as
;; discovered during development time, is given in respective
;; comments.
49

Michael Albinus's avatar
Michael Albinus committed
50
;; The custom option `tramp-gvfs-methods' contains the list of
51
;; supported connection methods.  Per default, these are "afp", "dav",
52 53 54 55 56 57 58 59
;; "davs", "gdrive", "obex", "owncloud", "sftp" and "synce".  Note
;; that with "obex" it might be necessary to pair with the other
;; bluetooth device, if it hasn't been done already.  There might be
;; also some few seconds delay in discovering available bluetooth
;; devices.

;; "gdrive" and "owncloud" connection methods require a respective
;; account in GNOME Online Accounts, with enabled "Files" service.
60

61 62 63 64 65 66
;; Other possible connection methods are "ftp", "http", "https" and
;; "smb".  When one of these methods is added to the list, the remote
;; access for that method is performed via GVFS instead of the native
;; Tramp implementation.  However, this is not recommended.  These
;; methods are listed here for the benefit of file archives, see
;; tramp-archive.el.
67 68 69 70 71 72 73 74 75 76 77

;; GVFS offers even more connection methods.  The complete list of
;; connection methods of the actual GVFS implementation can be
;; retrieved by:
;;
;; (message
;;  "%s"
;;  (mapcar
;;   'car
;;   (dbus-call-method
;;    :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
78
;;    tramp-gvfs-interface-mounttracker "ListMountableInfo")))
79 80 81 82 83 84 85

;; Note that all other connection methods are not tested, beside the
;; ones offered for customization in `tramp-gvfs-methods'.  If you
;; request an additional connection method to be supported, please
;; drop me a note.

;; For hostname completion, information is retrieved either from the
86
;; bluez daemon (for the "obex" method), the hal daemon (for the
87
;; "synce" method), or from the zeroconf daemon (for the "afp", "dav",
88 89
;; "davs", and "sftp" methods).  The zeroconf daemon is pre-configured
;; to discover services in the "local" domain.  If another domain
Michael Albinus's avatar
Michael Albinus committed
90
;; shall be used for discovering services, the custom option
91
;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
92 93 94

;; Restrictions:

95
;; * The current GVFS implementation does not allow writing on the
96 97 98 99 100 101 102 103 104 105
;;   remote bluetooth device via OBEX.
;;
;; * Two shares of the same SMB server cannot be mounted in parallel.

;;; Code:

;; D-Bus support in the Emacs core can be disabled with configuration
;; option "--without-dbus".  Declare used subroutines and variables.
(declare-function dbus-get-unique-name "dbusbind.c")

106
(eval-when-compile (require 'cl-lib))
107
(require 'tramp)
108

109 110
(require 'dbus)
(require 'url-parse)
111
(require 'url-util)
112 113
(require 'zeroconf)

Michael Albinus's avatar
Michael Albinus committed
114 115
;; Pacify byte-compiler.
(eval-when-compile
Michael Albinus's avatar
Michael Albinus committed
116
  (require 'custom))
Michael Albinus's avatar
Michael Albinus committed
117

Michael Albinus's avatar
Michael Albinus committed
118 119 120 121 122 123 124 125 126 127
;; We don't call `dbus-ping', because this would load dbus.el.
(defconst tramp-gvfs-enabled
  (ignore-errors
    (and (featurep 'dbusbind)
	 (tramp-compat-funcall 'dbus-get-unique-name :system)
	 (tramp-compat-funcall 'dbus-get-unique-name :session)
	 (or (tramp-compat-process-running-p "gvfs-fuse-daemon")
	     (tramp-compat-process-running-p "gvfsd-fuse"))))
  "Non-nil when GVFS is available.")

128
;;;###tramp-autoload
129
(defcustom tramp-gvfs-methods
130
  '("afp" "dav" "davs" "gdrive" "obex" "owncloud" "sftp" "synce")
131
  "List of methods for remote files, accessed with GVFS."
132
  :group 'tramp
133
  :version "26.1"
Michael Albinus's avatar
Michael Albinus committed
134 135
  :type '(repeat (choice (const "afp")
			 (const "dav")
136 137
			 (const "davs")
			 (const "ftp")
138
			 (const "gdrive")
139 140
			 (const "http")
			 (const "https")
141
			 (const "obex")
142
			 (const "owncloud")
143
			 (const "sftp")
144
			 (const "smb")
145 146
			 (const "synce")))
  :require 'tramp)
147

148 149 150
(defconst tramp-goa-methods '("gdrive" "owncloud")
  "List of methods which require registration at GNOME Online Accounts.")

Michael Albinus's avatar
Michael Albinus committed
151 152 153
;; Remove GNOME Online Accounts methods if not supported.
(unless (and tramp-gvfs-enabled
	     (member tramp-goa-service (dbus-list-known-names :session)))
154 155 156
  (dolist (method tramp-goa-methods)
    (setq tramp-gvfs-methods (delete method tramp-gvfs-methods))))

157 158 159 160 161 162 163 164
;; Add defaults for `tramp-default-user-alist' and `tramp-default-host-alist'.
;;;###tramp-autoload
(when (string-match "\\(.+\\)@\\(\\(?:gmail\\|googlemail\\)\\.com\\)"
		    user-mail-address)
  (add-to-list 'tramp-default-user-alist
	       `("\\`gdrive\\'" nil ,(match-string 1 user-mail-address)))
  (add-to-list 'tramp-default-host-alist
	       '("\\`gdrive\\'" nil ,(match-string 2 user-mail-address))))
165
;;;###tramp-autoload
166
(add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
167

168
;;;###tramp-autoload
169
(defcustom tramp-gvfs-zeroconf-domain "local"
170
  "Zeroconf domain to be used for discovering services, like host names."
171
  :group 'tramp
172
  :version "23.2"
173 174
  :type 'string
  :require 'tramp)
175 176 177

;; Add the methods to `tramp-methods', in order to allow minibuffer
;; completion.
178 179 180 181 182
;;;###tramp-autoload
(when (featurep 'dbusbind)
  (dolist (elt tramp-gvfs-methods)
    (unless (assoc elt tramp-methods)
      (add-to-list 'tramp-methods (cons elt nil)))))
183 184

(defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
Glenn Morris's avatar
Glenn Morris committed
185
  "The preceding object path for own objects.")
186 187 188 189 190 191 192 193 194 195

(defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
  "The well known name of the GVFS daemon.")

(defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
  "The object path of the GVFS daemon.")

(defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
  "The mount tracking interface in the GVFS daemon.")

196 197 198
;; Introspection data exist since GVFS 1.14.  If there are no such
;; data, we expect an earlier interface.
(defconst tramp-gvfs-methods-mounttracker
199 200 201 202
  (and tramp-gvfs-enabled
       (dbus-introspect-get-method-names
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker))
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  "The list of supported methods of the mount tracking interface.")

(defconst tramp-gvfs-listmounts
  (if (member "ListMounts" tramp-gvfs-methods-mounttracker)
      "ListMounts"
    "listMounts")
  "The name of the \"listMounts\" method.
It has been changed in GVFS 1.14.")

(defconst tramp-gvfs-mountlocation
  (if (member "MountLocation" tramp-gvfs-methods-mounttracker)
      "MountLocation"
    "mountLocation")
  "The name of the \"mountLocation\" method.
It has been changed in GVFS 1.14.")

(defconst tramp-gvfs-mountlocation-signature
220 221 222 223
  (and tramp-gvfs-enabled
       (dbus-introspect-get-signature
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation))
224 225 226
  "The D-Bus signature of the \"mountLocation\" method.
It has been changed in GVFS 1.14.")

227 228 229
;; <interface name='org.gtk.vfs.MountTracker'>
;;   <method name='listMounts'>
;;     <arg name='mount_info_list'
230
;;          type='a{sosssssbay{aya{say}}ay}'
231 232 233 234 235 236 237 238 239
;;          direction='out'/>
;;   </method>
;;   <method name='mountLocation'>
;;     <arg name='mount_spec'  type='{aya{say}}' direction='in'/>
;;     <arg name='dbus_id'     type='s'          direction='in'/>
;;     <arg name='object_path' type='o'          direction='in'/>
;;   </method>
;;   <signal name='mounted'>
;;     <arg name='mount_info'
240
;;          type='{sosssssbay{aya{say}}ay}'/>
241 242 243
;;   </signal>
;;   <signal name='unmounted'>
;;     <arg name='mount_info'
244
;;          type='{sosssssbay{aya{say}}ay}'/>
245 246 247 248 249 250 251 252
;;   </signal>
;; </interface>
;;
;; STRUCT		mount_info
;;   STRING		  dbus_id
;;   OBJECT_PATH	  object_path
;;   STRING		  display_name
;;   STRING		  stable_name
253
;;   STRING		  x_content_types	Since GVFS 1.0 only !!!
254
;;   STRING		  icon
Paul Eggert's avatar
Paul Eggert committed
255
;;   STRING		  preferred_filename_encoding
256 257 258 259 260 261
;;   BOOLEAN		  user_visible
;;   ARRAY BYTE		  fuse_mountpoint
;;   STRUCT		  mount_spec
;;     ARRAY BYTE	    mount_prefix
;;     ARRAY
;;       STRUCT		    mount_spec_item
Michael Albinus's avatar
Michael Albinus committed
262 263
;;         STRING	      key (type, user, domain, host, server,
;;                                 share, volume, port, ssl)
264
;;         ARRAY BYTE	      value
265
;;   ARRAY BYTE           default_location	Since GVFS 1.5 only !!!
266 267 268 269 270 271

(defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
  "Used by the dbus-proxying implementation of GMountOperation.")

;; <interface name='org.gtk.vfs.MountOperation'>
;;   <method name='askPassword'>
272
;;     <arg name='message'        type='s' direction='in'/>
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
;;     <arg name='default_user'   type='s' direction='in'/>
;;     <arg name='default_domain' type='s' direction='in'/>
;;     <arg name='flags'          type='u' direction='in'/>
;;     <arg name='handled'        type='b' direction='out'/>
;;     <arg name='aborted'        type='b' direction='out'/>
;;     <arg name='password'       type='s' direction='out'/>
;;     <arg name='username'       type='s' direction='out'/>
;;     <arg name='domain'         type='s' direction='out'/>
;;     <arg name='anonymous'      type='b' direction='out'/>
;;     <arg name='password_save'  type='u' direction='out'/>
;;   </method>
;;   <method name='askQuestion'>
;;     <arg name='message' type='s'  direction='in'/>
;;     <arg name='choices' type='as' direction='in'/>
;;     <arg name='handled' type='b'  direction='out'/>
;;     <arg name='aborted' type='b'  direction='out'/>
;;     <arg name='choice'  type='u'  direction='out'/>
;;   </method>
;; </interface>

;; The following flags are used in "askPassword".  They are defined in
;; /usr/include/glib-2.0/gio/gioenums.h.

(defconst tramp-gvfs-password-need-password 1
  "Operation requires a password.")

(defconst tramp-gvfs-password-need-username 2
  "Operation requires a username.")

(defconst tramp-gvfs-password-need-domain 4
  "Operation requires a domain.")

(defconst tramp-gvfs-password-saving-supported 8
  "Operation supports saving settings.")

(defconst tramp-gvfs-password-anonymous-supported 16
  "Operation supports anonymous users.")

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
;; For the time being, we just need org.goa.Account and org.goa.Files
;; interfaces.  We document the other ones, just in case.

;;;###tramp-autoload
(defconst tramp-goa-service "org.gnome.OnlineAccounts"
  "The well known name of the GNOME Online Accounts service.")

(defconst tramp-goa-path "/org/gnome/OnlineAccounts"
  "The object path of the GNOME Online Accounts.")

(defconst tramp-goa-path-accounts (concat tramp-goa-path "/Accounts")
  "The object path of the GNOME Online Accounts accounts.")

(defconst tramp-goa-interface-documents "org.gnome.OnlineAccounts.Documents"
  "The documents interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Documents'>
;; </interface>

(defconst tramp-goa-interface-printers "org.gnome.OnlineAccounts.Printers"
  "The printers interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Printers'>
;; </interface>

(defconst tramp-goa-interface-files "org.gnome.OnlineAccounts.Files"
  "The files interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Files'>
;;   <property type='b' name='AcceptSslErrors' access='read'/>
;;   <property type='s' name='Uri' access='read'/>
;; </interface>

(defconst tramp-goa-interface-contacts "org.gnome.OnlineAccounts.Contacts"
  "The contacts interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Contacts'>
;;   <property type='b' name='AcceptSslErrors' access='read'/>
;;   <property type='s' name='Uri' access='read'/>
;; </interface>

(defconst tramp-goa-interface-calendar "org.gnome.OnlineAccounts.Calendar"
  "The calendar interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Calendar'>
;;   <property type='b' name='AcceptSslErrors' access='read'/>
;;   <property type='s' name='Uri' access='read'/>
;; </interface>

(defconst tramp-goa-interface-oauth2based "org.gnome.OnlineAccounts.OAuth2Based"
  "The oauth2based interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.OAuth2Based'>
;;   <method name='GetAccessToken'>
;;     <arg type='s' name='access_token' direction='out'/>
;;     <arg type='i' name='expires_in' direction='out'/>
;;   </method>
;;   <property type='s' name='ClientId' access='read'/>
;;   <property type='s' name='ClientSecret' access='read'/>
;; </interface>

(defconst tramp-goa-interface-account "org.gnome.OnlineAccounts.Account"
  "The account interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Account'>
;;   <method name='Remove'/>
;;   <method name='EnsureCredentials'>
;;     <arg type='i' name='expires_in' direction='out'/>
;;   </method>
;;   <property type='s' name='ProviderType' access='read'/>
;;   <property type='s' name='ProviderName' access='read'/>
;;   <property type='s' name='ProviderIcon' access='read'/>
;;   <property type='s' name='Id' access='read'/>
;;   <property type='b' name='IsLocked' access='read'/>
;;   <property type='b' name='IsTemporary' access='readwrite'/>
;;   <property type='b' name='AttentionNeeded' access='read'/>
;;   <property type='s' name='Identity' access='read'/>
;;   <property type='s' name='PresentationIdentity' access='read'/>
;;   <property type='b' name='MailDisabled' access='readwrite'/>
;;   <property type='b' name='CalendarDisabled' access='readwrite'/>
;;   <property type='b' name='ContactsDisabled' access='readwrite'/>
;;   <property type='b' name='ChatDisabled' access='readwrite'/>
;;   <property type='b' name='DocumentsDisabled' access='readwrite'/>
;;   <property type='b' name='MapsDisabled' access='readwrite'/>
;;   <property type='b' name='MusicDisabled' access='readwrite'/>
;;   <property type='b' name='PrintersDisabled' access='readwrite'/>
;;   <property type='b' name='PhotosDisabled' access='readwrite'/>
;;   <property type='b' name='FilesDisabled' access='readwrite'/>
;;   <property type='b' name='TicketingDisabled' access='readwrite'/>
;;   <property type='b' name='TodoDisabled' access='readwrite'/>
;;   <property type='b' name='ReadLaterDisabled' access='readwrite'/>
;; </interface>

(defconst tramp-goa-identity-regexp
  (concat "^" "\\(" tramp-user-regexp "\\)?"
	  "@" "\\(" tramp-host-regexp "\\)?"
	  "\\(?:" ":""\\(" tramp-port-regexp "\\)" "\\)?")
  "Regexp matching GNOME Online Accounts \"PresentationIdentity\" property.")

(defconst tramp-goa-interface-mail "org.gnome.OnlineAccounts.Mail"
  "The mail interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Mail'>
;;   <property type='s' name='EmailAddress' access='read'/>
;;   <property type='s' name='Name' access='read'/>
;;   <property type='b' name='ImapSupported' access='read'/>
;;   <property type='b' name='ImapAcceptSslErrors' access='read'/>
;;   <property type='s' name='ImapHost' access='read'/>
;;   <property type='b' name='ImapUseSsl' access='read'/>
;;   <property type='b' name='ImapUseTls' access='read'/>
;;   <property type='s' name='ImapUserName' access='read'/>
;;   <property type='b' name='SmtpSupported' access='read'/>
;;   <property type='b' name='SmtpAcceptSslErrors' access='read'/>
;;   <property type='s' name='SmtpHost' access='read'/>
;;   <property type='b' name='SmtpUseAuth' access='read'/>
;;   <property type='b' name='SmtpAuthLogin' access='read'/>
;;   <property type='b' name='SmtpAuthPlain' access='read'/>
;;   <property type='b' name='SmtpAuthXoauth2' access='read'/>
;;   <property type='b' name='SmtpUseSsl' access='read'/>
;;   <property type='b' name='SmtpUseTls' access='read'/>
;;   <property type='s' name='SmtpUserName' access='read'/>
;; </interface>

(defconst tramp-goa-interface-chat "org.gnome.OnlineAccounts.Chat"
  "The chat interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Chat'>
;; </interface>

(defconst tramp-goa-interface-photos "org.gnome.OnlineAccounts.Photos"
  "The photos interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Photos'>
;; </interface>

(defconst tramp-goa-path-manager (concat tramp-goa-path "/Manager")
  "The object path of the GNOME Online Accounts manager.")

(defconst tramp-goa-interface-documents "org.gnome.OnlineAccounts.Manager"
  "The manager interface of the GNOME Online Accounts.")

;; <interface name='org.gnome.OnlineAccounts.Manager'>
;;   <method name='AddAccount'>
;;     <arg type='s' name='provider' direction='in'/>
;;     <arg type='s' name='identity' direction='in'/>
;;     <arg type='s' name='presentation_identity' direction='in'/>
;;     <arg type='a{sv}' name='credentials' direction='in'/>
;;     <arg type='a{ss}' name='details' direction='in'/>
;;     <arg type='o' name='account_object_path' direction='out'/>
;;   </method>
;; </interface>

;; The basic structure for GNOME Online Accounts.  We use a list :type,
;; in order to be compatible with Emacs 24 and 25.
(cl-defstruct (tramp-goa-name (:type list) :named) method user host port)

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
(defconst tramp-bluez-service "org.bluez"
  "The well known name of the BLUEZ service.")

(defconst tramp-bluez-interface-manager "org.bluez.Manager"
  "The manager interface of the BLUEZ daemon.")

;; <interface name='org.bluez.Manager'>
;;   <method name='DefaultAdapter'>
;;     <arg type='o' direction='out'/>
;;   </method>
;;   <method name='FindAdapter'>
;;     <arg type='s' direction='in'/>
;;     <arg type='o' direction='out'/>
;;   </method>
;;   <method name='ListAdapters'>
;;     <arg type='ao' direction='out'/>
;;   </method>
;;   <signal name='AdapterAdded'>
;;     <arg type='o'/>
;;   </signal>
;;   <signal name='AdapterRemoved'>
;;     <arg type='o'/>
;;   </signal>
;;   <signal name='DefaultAdapterChanged'>
;;     <arg type='o'/>
;;   </signal>
;; </interface>

(defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
  "The adapter interface of the BLUEZ daemon.")

;; <interface name='org.bluez.Adapter'>
;;   <method name='GetProperties'>
;;     <arg type='a{sv}' direction='out'/>
;;   </method>
;;   <method name='SetProperty'>
;;     <arg type='s' direction='in'/>
;;     <arg type='v' direction='in'/>
;;   </method>
;;   <method name='RequestMode'>
;;     <arg type='s' direction='in'/>
;;   </method>
;;   <method name='ReleaseMode'/>
;;   <method name='RequestSession'/>
;;   <method name='ReleaseSession'/>
;;   <method name='StartDiscovery'/>
;;   <method name='StopDiscovery'/>
;;   <method name='ListDevices'>
;;     <arg type='ao' direction='out'/>
;;   </method>
;;   <method name='CreateDevice'>
;;     <arg type='s' direction='in'/>
;;     <arg type='o' direction='out'/>
;;   </method>
;;   <method name='CreatePairedDevice'>
;;     <arg type='s' direction='in'/>
;;     <arg type='o' direction='in'/>
;;     <arg type='s' direction='in'/>
;;     <arg type='o' direction='out'/>
;;   </method>
;;   <method name='CancelDeviceCreation'>
;;     <arg type='s' direction='in'/>
;;   </method>
;;   <method name='RemoveDevice'>
;;     <arg type='o' direction='in'/>
;;   </method>
;;   <method name='FindDevice'>
;;     <arg type='s' direction='in'/>
;;     <arg type='o' direction='out'/>
;;   </method>
;;   <method name='RegisterAgent'>
;;     <arg type='o' direction='in'/>
;;     <arg type='s' direction='in'/>
;;   </method>
;;   <method name='UnregisterAgent'>
;;     <arg type='o' direction='in'/>
;;   </method>
;;   <signal name='DeviceCreated'>
;;     <arg type='o'/>
;;   </signal>
;;   <signal name='DeviceRemoved'>
;;     <arg type='o'/>
;;   </signal>
;;   <signal name='DeviceFound'>
;;     <arg type='s'/>
;;     <arg type='a{sv}'/>
;;   </signal>
;;   <signal name='PropertyChanged'>
;;     <arg type='s'/>
;;     <arg type='v'/>
;;   </signal>
;;   <signal name='DeviceDisappeared'>
;;     <arg type='s'/>
;;   </signal>
;; </interface>

563
;;;###tramp-autoload
564 565 566 567 568 569
(defcustom tramp-bluez-discover-devices-timeout 60
  "Defines seconds since last bluetooth device discovery before rescanning.
A value of 0 would require an immediate discovery during hostname
completion, nil means to use always cached values for discovered
devices."
  :group 'tramp
570
  :version "23.2"
571 572
  :type '(choice (const nil) integer)
  :require 'tramp)
573 574 575 576 577 578 579 580 581

(defvar tramp-bluez-discovery nil
  "Indicator for a running bluetooth device discovery.
It keeps the timestamp of last discovery.")

(defvar tramp-bluez-devices nil
  "Alist of detected bluetooth devices.
Every entry is a list (NAME ADDRESS).")

582 583 584 585 586 587 588 589 590 591 592 593
(defconst tramp-hal-service "org.freedesktop.Hal"
  "The well known name of the HAL service.")

(defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
  "The object path of the HAL daemon manager.")

(defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
  "The manager interface of the HAL daemon.")

(defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
  "The device interface of the HAL daemon.")

Michael Albinus's avatar
Michael Albinus committed
594 595 596 597 598 599 600 601
;; "gvfs-<command>" utilities have been deprecated in GVFS 1.31.1.  We
;; must use "gio <command>" tool instead.
(defconst tramp-gvfs-gio-mapping
  '(("gvfs-copy" . "copy")
    ("gvfs-info" . "info")
    ("gvfs-ls" . "list")
    ("gvfs-mkdir" . "mkdir")
    ("gvfs-monitor-file" . "monitor")
602
    ("gvfs-mount" . "mount")
Michael Albinus's avatar
Michael Albinus committed
603 604 605 606 607
    ("gvfs-move" . "move")
    ("gvfs-rm" . "remove")
    ("gvfs-trash" . "trash"))
  "List of cons cells, mapping \"gvfs-<command>\" to \"gio <command>\".")

Michael Albinus's avatar
Michael Albinus committed
608
;; <http://www.pygtk.org/docs/pygobject/gio-constants.html>
Michael Albinus's avatar
Michael Albinus committed
609
(defconst tramp-gvfs-file-attributes
610 611
  '("name"
    "type"
Michael Albinus's avatar
Michael Albinus committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    "standard::display-name"
    "standard::symlink-target"
    "unix::nlink"
    "unix::uid"
    "owner::user"
    "unix::gid"
    "owner::group"
    "time::access"
    "time::modified"
    "time::changed"
    "standard::size"
    "unix::mode"
    "access::can-read"
    "access::can-write"
    "access::can-execute"
    "unix::inode"
    "unix::device")
  "GVFS file attributes.")

(defconst tramp-gvfs-file-attributes-with-gvfs-ls-regexp
632
  (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t) "=\\(.+?\\)")
Michael Albinus's avatar
Michael Albinus committed
633 634 635 636 637 638 639 640
  "Regexp to parse GVFS file attributes with `gvfs-ls'.")

(defconst tramp-gvfs-file-attributes-with-gvfs-info-regexp
  (concat "^[[:blank:]]*"
	  (regexp-opt tramp-gvfs-file-attributes t)
	  ":[[:blank:]]+\\(.*\\)$")
  "Regexp to parse GVFS file attributes with `gvfs-info'.")

641 642 643 644 645 646 647 648 649 650 651 652
(defconst tramp-gvfs-file-system-attributes
  '("filesystem::free"
    "filesystem::size"
    "filesystem::used")
  "GVFS file system attributes.")

(defconst tramp-gvfs-file-system-attributes-regexp
  (concat "^[[:blank:]]*"
	  (regexp-opt tramp-gvfs-file-system-attributes t)
	  ":[[:blank:]]+\\(.*\\)$")
  "Regexp to parse GVFS file system attributes with `gvfs-info'.")

653 654 655 656 657 658 659
(defconst tramp-gvfs-owncloud-default-prefix "/remote.php/webdav"
  "Default prefix for owncloud / nextcloud methods.")

(defconst tramp-gvfs-owncloud-default-prefix-regexp
  (concat (regexp-quote tramp-gvfs-owncloud-default-prefix) "$")
  "Regexp of default prefix for owncloud / nextcloud methods.")

660

661
;; New handlers should be added here.
Michael Albinus's avatar
Michael Albinus committed
662
;;;###tramp-autoload
663
(defconst tramp-gvfs-file-name-handler-alist
Michael Albinus's avatar
Michael Albinus committed
664
  '((access-file . ignore)
665
    (add-name-to-file . tramp-handle-add-name-to-file)
666
    ;; `byte-compiler-base-file-name' performed by default handler.
Michael Albinus's avatar
Michael Albinus committed
667
    ;; `copy-directory' performed by default handler.
668 669 670
    (copy-file . tramp-gvfs-handle-copy-file)
    (delete-directory . tramp-gvfs-handle-delete-directory)
    (delete-file . tramp-gvfs-handle-delete-file)
671
    ;; `diff-latest-backup-file' performed by default handler.
672
    (directory-file-name . tramp-handle-directory-file-name)
673
    (directory-files . tramp-handle-directory-files)
674
    (directory-files-and-attributes
675
     . tramp-handle-directory-files-and-attributes)
676 677 678
    (dired-compress-file . ignore)
    (dired-uncache . tramp-handle-dired-uncache)
    (expand-file-name . tramp-gvfs-handle-expand-file-name)
679
    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
680
    (file-acl . ignore)
681
    (file-attributes . tramp-gvfs-handle-file-attributes)
Michael Albinus's avatar
Michael Albinus committed
682
    (file-directory-p . tramp-handle-file-directory-p)
683
    (file-equal-p . tramp-handle-file-equal-p)
684
    (file-executable-p . tramp-gvfs-handle-file-executable-p)
685
    (file-exists-p . tramp-handle-file-exists-p)
686
    (file-in-directory-p . tramp-handle-file-in-directory-p)
687
    (file-local-copy . tramp-gvfs-handle-file-local-copy)
688
    (file-modes . tramp-handle-file-modes)
689 690
    (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
    (file-name-as-directory . tramp-handle-file-name-as-directory)
691
    (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
692 693 694
    (file-name-completion . tramp-handle-file-name-completion)
    (file-name-directory . tramp-handle-file-name-directory)
    (file-name-nondirectory . tramp-handle-file-name-nondirectory)
695
    ;; `file-name-sans-versions' performed by default handler.
696
    (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
Michael Albinus's avatar
Michael Albinus committed
697 698
    (file-notify-add-watch . tramp-gvfs-handle-file-notify-add-watch)
    (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
699
    (file-notify-valid-p . tramp-handle-file-notify-valid-p)
700 701 702
    (file-ownership-preserved-p . ignore)
    (file-readable-p . tramp-gvfs-handle-file-readable-p)
    (file-regular-p . tramp-handle-file-regular-p)
Michael Albinus's avatar
Michael Albinus committed
703
    (file-remote-p . tramp-handle-file-remote-p)
Michael Albinus's avatar
Michael Albinus committed
704
    (file-selinux-context . tramp-handle-file-selinux-context)
705
    (file-symlink-p . tramp-handle-file-symlink-p)
706
    (file-system-info . tramp-gvfs-handle-file-system-info)
707
    (file-truename . tramp-handle-file-truename)
708 709
    (file-writable-p . tramp-gvfs-handle-file-writable-p)
    (find-backup-file-name . tramp-handle-find-backup-file-name)
710 711
    ;; `find-file-noselect' performed by default handler.
    ;; `get-file-buffer' performed by default handler.
712
    (insert-directory . tramp-handle-insert-directory)
713
    (insert-file-contents . tramp-handle-insert-file-contents)
714
    (load . tramp-handle-load)
715
    (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
716 717
    (make-directory . tramp-gvfs-handle-make-directory)
    (make-directory-internal . ignore)
718
    (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
719
    (make-symbolic-link . tramp-handle-make-symbolic-link)
720
    (process-file . ignore)
721
    (rename-file . tramp-gvfs-handle-rename-file)
722 723 724
    (set-file-acl . ignore)
    (set-file-modes . ignore)
    (set-file-selinux-context . ignore)
Michael Albinus's avatar
Michael Albinus committed
725 726
    (set-file-times . ignore)
    (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
727 728
    (shell-command . ignore)
    (start-file-process . ignore)
729
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
730
    (temporary-file-directory . tramp-handle-temporary-file-directory)
731
    (unhandled-file-name-directory . ignore)
732
    (vc-registered . ignore)
Michael Albinus's avatar
Michael Albinus committed
733 734
    (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
    (write-region . tramp-gvfs-handle-write-region))
735 736 737
  "Alist of handler functions for Tramp GVFS method.
Operations not mentioned here will be handled by the default Emacs primitives.")

738 739
;; It must be a `defsubst' in order to push the whole code into
;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
740 741
;;;###tramp-autoload
(defsubst tramp-gvfs-file-name-p (filename)
742 743 744 745 746 747
  "Check if it's a filename handled by the GVFS daemon."
  (and (tramp-tramp-file-p filename)
       (let ((method
	      (tramp-file-name-method (tramp-dissect-file-name filename))))
	 (and (stringp method) (member method tramp-gvfs-methods)))))

748
;;;###tramp-autoload
749 750 751 752
(defun tramp-gvfs-file-name-handler (operation &rest args)
  "Invoke the GVFS related OPERATION.
First arg specifies the OPERATION, second arg is a list of arguments to
pass to the OPERATION."
753
  (unless tramp-gvfs-enabled
Michael Albinus's avatar
Michael Albinus committed
754
    (tramp-user-error nil "Package `tramp-gvfs' not supported"))
755 756 757 758 759
  (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
    (if fn
	(save-match-data (apply (cdr fn) args))
      (tramp-run-real-handler operation args))))

760 761
;;;###tramp-autoload
(when (featurep 'dbusbind)
Michael Albinus's avatar
Michael Albinus committed
762 763
  (tramp-register-foreign-file-name-handler
   'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler))
764

765 766 767 768 769 770 771 772 773 774

;; D-Bus helper function.

(defun tramp-gvfs-dbus-string-to-byte-array (string)
  "Like `dbus-string-to-byte-array' but add trailing \\0 if needed."
  (dbus-string-to-byte-array
   (if (string-match "^(aya{sv})" tramp-gvfs-mountlocation-signature)
       (concat string (string 0)) string)))

(defun tramp-gvfs-dbus-byte-array-to-string (byte-array)
775 776
  "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists.
Return nil for null BYTE-ARRAY."
777 778 779 780
  ;; The byte array could be a variant.  Take care.
  (let ((byte-array
	 (if (and (consp byte-array) (atom (car byte-array)))
	     byte-array (car byte-array))))
781 782 783 784
    (and byte-array
	 (dbus-byte-array-to-string
	  (if (and (consp byte-array) (zerop (car (last byte-array))))
	      (butlast byte-array) byte-array)))))
785

786 787 788 789
(defun tramp-gvfs-stringify-dbus-message (message)
  "Convert a D-Bus message into readable UTF8 strings, used for traces."
  (cond
   ((and (consp message) (characterp (car message)))
790
    (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
791 792 793
   ((and (consp message) (not (consp (cdr message))))
    (cons (tramp-gvfs-stringify-dbus-message (car message))
	  (tramp-gvfs-stringify-dbus-message (cdr message))))
794 795 796 797 798 799
   ((consp message)
    (mapcar 'tramp-gvfs-stringify-dbus-message message))
   ((stringp message)
    (format "%S" message))
   (t message)))

800 801 802 803 804 805 806 807 808
(defun tramp-dbus-function (vec func args)
  "Apply a D-Bus function FUNC from dbus.el.
The call will be traced by Tramp with trace level 6."
  (let (result)
    (tramp-message vec 6 "%s" (cons func args))
    (setq result (apply func args))
    (tramp-message vec 6 "%s" result(tramp-gvfs-stringify-dbus-message result))
    result))

809 810 811 812 813 814 815 816
(defmacro with-tramp-dbus-call-method
  (vec synchronous bus service path interface method &rest args)
  "Apply a D-Bus call on bus BUS.

If SYNCHRONOUS is non-nil, the call is synchronously.  Otherwise,
it is an asynchronous call, with `ignore' as callback function.

The other arguments have the same meaning as with `dbus-call-method'
817
or `dbus-call-method-asynchronously'."
818 819 820
  `(let ((func (if ,synchronous
		   'dbus-call-method 'dbus-call-method-asynchronously))
	 (args (append (list ,bus ,service ,path ,interface ,method)
821 822
		       (if ,synchronous (list ,@args) (list 'ignore ,@args)))))
     (tramp-dbus-function ,vec func args)))
823 824 825

(put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
(put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
826
(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
827

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
(defmacro with-tramp-dbus-get-all-properties
  (vec bus service path interface)
  "Return all properties of INTERFACE.
The call will be traced by Tramp with trace level 6."
     ;; Check, that interface exists at object path.  Retrieve properties.
  `(when (member
	  ,interface
	  (tramp-dbus-function
	   ,vec 'dbus-introspect-get-interface-names
	   (list ,bus ,service ,path)))
     (tramp-dbus-function
      ,vec 'dbus-get-all-properties (list ,bus ,service ,path ,interface))))

(put 'with-tramp-dbus-get-all-properties 'lisp-indent-function 1)
(put 'with-tramp-dbus-get-all-properties 'edebug-form-spec '(form symbolp body))
(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-get-all-properties\\>"))

845 846 847 848 849 850
(defvar tramp-gvfs-dbus-event-vector nil
  "Current Tramp file name to be used, as vector.
It is needed when D-Bus signals or errors arrive, because there
is no information where to trace the message.")

(defun tramp-gvfs-dbus-event-error (event err)
851
  "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
852
  (when tramp-gvfs-dbus-event-vector
Michael Albinus's avatar
Michael Albinus committed
853
    (tramp-message tramp-gvfs-dbus-event-vector 6 "%S" event)
854
    (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
855

856 857
;; `dbus-event-error-hooks' has been renamed to
;; `dbus-event-error-functions' in Emacs 24.3.
858 859 860 861
(add-hook
 (if (boundp 'dbus-event-error-functions)
     'dbus-event-error-functions 'dbus-event-error-hooks)
 'tramp-gvfs-dbus-event-error)
862 863 864 865


;; File name primitives.

Michael Albinus's avatar
Michael Albinus committed
866 867
(defun tramp-gvfs-do-copy-or-rename-file
  (op filename newname &optional ok-if-already-exists keep-date
868
   preserve-uid-gid preserve-extended-attributes)
Michael Albinus's avatar
Michael Albinus committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
  "Copy or rename a remote file.
OP must be `copy' or `rename' and indicates the operation to perform.
FILENAME specifies the file to copy or rename, NEWNAME is the name of
the new file (for copy) or the new name of the file (for rename).
OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
KEEP-DATE means to make sure that NEWNAME has the same timestamp
as FILENAME.  PRESERVE-UID-GID, when non-nil, instructs to keep
the uid and gid if both files are on the same host.
PRESERVE-EXTENDED-ATTRIBUTES is ignored.

This function is invoked by `tramp-gvfs-handle-copy-file' and
`tramp-gvfs-handle-rename-file'.  It is an error if OP is neither
of `copy' and `rename'.  FILENAME and NEWNAME must be absolute
file names."
  (unless (memq op '(copy rename))
884
    (error "Unknown operation `%s', must be `copy' or `rename'" op))
Michael Albinus's avatar
Michael Albinus committed
885

Michael Albinus's avatar
Michael Albinus committed
886
  (setq filename (file-truename filename))
Michael Albinus's avatar
Michael Albinus committed
887 888 889 890 891
  (if (file-directory-p filename)
      (progn
	(copy-directory filename newname keep-date t)
	(when (eq op 'rename) (delete-directory filename 'recursive)))

Michael Albinus's avatar
Michael Albinus committed
892 893 894 895 896 897 898 899
    (let ((t1 (tramp-tramp-file-p filename))
	  (t2 (tramp-tramp-file-p newname))
	  (equal-remote (tramp-equal-remote filename newname))
	  (gvfs-operation (if (eq op 'copy) "gvfs-copy" "gvfs-move"))
	  (msg-operation (if (eq op 'copy) "Copying" "Renaming")))

      (with-parsed-tramp-file-name (if t1 filename newname) nil
	(when (and (not ok-if-already-exists) (file-exists-p newname))
900
	  (tramp-error v 'file-already-exists newname))
Michael Albinus's avatar
Michael Albinus committed
901 902 903 904 905 906 907 908

	(if (or (and equal-remote
		     (tramp-get-connection-property v "direct-copy-failed" nil))
		(and t1 (not (tramp-gvfs-file-name-p filename)))
		(and t2 (not (tramp-gvfs-file-name-p newname))))

	    ;; We cannot copy or rename directly.
	    (let ((tmpfile (tramp-compat-make-temp-file filename)))
Michael Albinus's avatar
Michael Albinus committed
909 910 911 912 913
	      (if (eq op 'copy)
		  (copy-file
		   filename tmpfile t keep-date preserve-uid-gid
		   preserve-extended-attributes)
		(rename-file filename tmpfile t))
Michael Albinus's avatar
Michael Albinus committed
914 915 916 917 918 919 920 921 922 923
	      (rename-file tmpfile newname ok-if-already-exists))

	  ;; Direct action.
	  (with-tramp-progress-reporter
	      v 0 (format "%s %s to %s" msg-operation filename newname)
	    (unless
		(apply
		 'tramp-gvfs-send-command v gvfs-operation
		 (append
		  (and (eq op 'copy) (or keep-date preserve-uid-gid)
924
		       '("--preserve"))
Michael Albinus's avatar
Michael Albinus committed
925 926 927 928 929 930 931 932 933 934 935 936 937
		  (list
		   (tramp-gvfs-url-file-name filename)
		   (tramp-gvfs-url-file-name newname))))

	      (if (or (not equal-remote)
		      (and equal-remote
			   (tramp-get-connection-property
			    v "direct-copy-failed" nil)))
		  ;; Propagate the error.
		  (with-current-buffer (tramp-get-connection-buffer v)
		    (goto-char (point-min))
		    (tramp-error-with-buffer
		     nil v 'file-error
938
		     "%s failed, see buffer `%s' for details."
Michael Albinus's avatar
Michael Albinus committed
939 940 941 942 943 944 945 946 947 948 949
		     msg-operation (buffer-name)))

		;; Some WebDAV server, like the one from QNAP, do not
		;; support direct copy/move.  Try a fallback.
		(tramp-set-connection-property v "direct-copy-failed" t)
		(tramp-gvfs-do-copy-or-rename-file
		 op filename newname ok-if-already-exists keep-date
		 preserve-uid-gid preserve-extended-attributes))))

	  (when (and t1 (eq op 'rename))
	    (with-parsed-tramp-file-name filename nil
950 951
	      (tramp-flush-file-properties v (file-name-directory localname))
	      (tramp-flush-file-properties v localname)))
Michael Albinus's avatar
Michael Albinus committed
952 953 954

	  (when t2
	    (with-parsed-tramp-file-name newname nil
955 956
	      (tramp-flush-file-properties v (file-name-directory localname))
	      (tramp-flush-file-properties v localname))))))))
Michael Albinus's avatar
Michael Albinus committed
957

958
(defun tramp-gvfs-handle-copy-file
Michael Albinus's avatar
Michael Albinus committed
959
  (filename newname &optional ok-if-already-exists keep-date
960
   preserve-uid-gid preserve-extended-attributes)
961
  "Like `copy-file' for Tramp files."
Michael Albinus's avatar
Michael Albinus committed
962 963
  (setq filename (expand-file-name filename))
  (setq newname (expand-file-name newname))
964 965 966 967 968 969
  ;; At least one file a Tramp file?
  (if (or (tramp-tramp-file-p filename)
	  (tramp-tramp-file-p newname))
      (tramp-gvfs-do-copy-or-rename-file
       'copy filename newname ok-if-already-exists keep-date
       preserve-uid-gid preserve-extended-attributes)
Michael Albinus's avatar
Michael Albinus committed
970 971 972
    (tramp-run-real-handler
     'copy-file
     (list filename newname ok-if-already-exists keep-date
973
	   preserve-uid-gid preserve-extended-attributes))))
974 975

(defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
976
  "Like `delete-directory' for Tramp files."
977
  (with-parsed-tramp-file-name directory nil
978 979
    (if (and recursive (not (file-symlink-p directory)))
	(mapc (lambda (file)
980 981
		(if (eq t (tramp-compat-file-attribute-type
			   (file-attributes file)))
982 983
		    (delete-directory file recursive trash)
		  (delete-file file trash)))
984 985 986 987 988 989
	      (directory-files
	       directory 'full directory-files-no-dot-files-regexp))
      (when (directory-files directory nil directory-files-no-dot-files-regexp)
	(tramp-error
	 v 'file-error "Couldn't delete non-empty %s" directory)))

990 991
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-directory-properties v localname)
992 993 994 995 996 997 998 999
    (unless
	(tramp-gvfs-send-command
	 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
	 (tramp-gvfs-url-file-name directory))
      ;; Propagate the error.
      (with-current-buffer (tramp-get-connection-buffer v)
	(goto-char (point-min))
	(tramp-error-with-buffer
1000
	 nil v 'file-error "Couldn't delete %s" directory)))))
1001

1002
(defun tramp-gvfs-handle-delete-file (filename &optional trash)
1003
  "Like `delete-file' for Tramp files."
1004
  (with-parsed-tramp-file-name filename nil
1005 1006
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-file-properties v localname)
1007 1008 1009 1010 1011 1012 1013 1014
    (unless
	(tramp-gvfs-send-command
	 v (if (and trash delete-by-moving-to-trash) "gvfs-trash" "gvfs-rm")
	 (tramp-gvfs-url-file-name filename))
      ;; Propagate the error.
      (with-current-buffer (tramp-get-connection-buffer v)
	(goto-char (point-min))
	(tramp-error-with-buffer
1015
	 nil v 'file-error "Couldn't delete %s" filename)))))
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

(defun tramp-gvfs-handle-expand-file-name (name &optional dir)
  "Like `expand-file-name' for Tramp files."
  ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
  (setq dir (or dir default-directory "/"))
  ;; Unless NAME is absolute, concat DIR and NAME.
  (unless (file-name-absolute-p name)
    (setq name (concat (file-name-as-directory dir) name)))
  ;; If NAME is not a Tramp file, run the real handler.
  (if (not (tramp-tramp-file-p name))
      (tramp-run-real-handler 'expand-file-name (list name nil))
    ;; Dissect NAME.
    (with-parsed-tramp-file-name name nil
1029 1030 1031
      ;; If there is a default location, expand tilde.
      (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
	(save-match-data
1032
	  (tramp-gvfs-maybe-open-connection
1033 1034 1035
	   (make-tramp-file-name
	    :method method :user user :domain domain
	    :host host :port port :localname "/" :hop hop)))
1036 1037
	(setq localname
	      (replace-match
1038
	       (tramp-get-connection-property v "default-location" "~")
1039
	       nil t localname 1)))
1040 1041 1042 1043
      ;; Tilde expansion is not possible.
      (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
	(tramp-error
	 v 'file-error
1044
	 "Cannot expand tilde in file `%s'" name))
1045 1046 1047
      (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
	(setq localname (concat "/" localname)))
      ;; We do not pass "/..".
1048
      (if (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	  (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
	    (setq localname (replace-match "/" t t localname 1)))
	(when (string-match "^/\\.\\./?" localname)
	  (setq localname (replace-match "/" t t localname))))
      ;; There might be a double slash.  Remove this.
      (while (string-match "//" localname)
	(setq localname (replace-match "/" t t localname)))
      ;; No tilde characters in file name, do normal
      ;; `expand-file-name' (this does "/./" and "/../").
      (tramp-make-tramp-file-name
1059
       method user domain host port
1060
       (tramp-run-real-handler 'expand-file-name (list localname))))))
1061

Michael Albinus's avatar
Michael Albinus committed
1062 1063
(defun tramp-gvfs-get-directory-attributes (directory)
  "Return GVFS attributes association list of all files in DIRECTORY."
1064 1065 1066
  (ignore-errors
    ;; Don't modify `last-coding-system-used' by accident.
    (let ((last-coding-system-used last-coding-system-used)
Michael Albinus's avatar
Michael Albinus committed
1067 1068
	  result)
      (with-parsed-tramp-file-name directory nil
1069
	(with-tramp-file-property v localname "directory-attributes"
Michael Albinus's avatar
Michael Albinus committed
1070 1071 1072 1073 1074 1075
	  (tramp-message v 5 "directory gvfs attributes: %s" localname)
	  ;; Send command.
	  (tramp-gvfs-send-command
	   v "gvfs-ls" "-h" "-n" "-a"
	   (mapconcat 'identity tramp-gvfs-file-attributes ",")
	   (tramp-gvfs-url-file-name directory))
1076
	  ;; Parse output.
Michael Albinus's avatar
Michael Albinus committed
1077 1078
	  (with-current-buffer (tramp-get-connection-buffer v)
	    (goto-char (point-min))
1079
	    (while (looking-at
Michael Albinus's avatar
Michael Albinus committed
1080 1081
		    (concat "^\\(.+\\)[[:blank:]]"
			    "\\([[:digit:]]+\\)[[:blank:]]"
1082 1083 1084
			    "(\\(.+?\\))"
			    tramp-gvfs-file-attributes-with-gvfs-ls-regexp))
	      (let ((item (list (cons "type" (match-string 3))
Michael Albinus's avatar
Michael Albinus committed
1085
				(cons "standard::size" (match-string 2))
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
				(cons "name" (match-string 1)))))
		(goto-char (1+ (match-end 3)))
		(while (looking-at
			(concat
			 tramp-gvfs-file-attributes-with-gvfs-ls-regexp
			 "\\(" tramp-gvfs-file-attributes-with-gvfs-ls-regexp
			 "\\|" "$" "\\)"))
		  (push (cons (match-string 1) (match-string 2)) item)
		  (goto-char (match-end 2)))
		;; Add display name as head.
		(push
		 (cons (cdr (or (assoc "standard::display-name" item)
				(assoc "name" item)))
		       (nreverse item))
		 result))
Michael Albinus's avatar
Michael Albinus committed
1101 1102 1103
	      (forward-line)))
	  result)))))

1104 1105 1106
(defun tramp-gvfs-get-root-attributes (filename &optional file-system)
  "Return GVFS attributes association list of FILENAME.
If FILE-SYSTEM is non-nil, return file system attributes."
Michael Albinus's avatar
Michael Albinus committed
1107 1108 1109 1110
  (ignore-errors
    ;; Don't modify `last-coding-system-used' by accident.
    (let ((last-coding-system-used last-coding-system-used)
	  result)
1111
      (with-parsed-tramp-file-name filename nil
1112 1113 1114 1115 1116 1117
	(with-tramp-file-property
	    v localname
	    (if file-system "file-system-attributes" "file-attributes")
	  (tramp-message
	   v 5 "file%s gvfs attributes: %s"
	   (if file-system " system" "") localname)
Michael Albinus's avatar
Michael Albinus committed
1118
	  ;; Send command.
1119 1120 1121 1122 1123
	  (if file-system
	      (tramp-gvfs-send-command
	       v "gvfs-info" "--filesystem" (tramp-gvfs-url-file-name filename))
	    (tramp-gvfs-send-command
	     v "gvfs-info" (tramp-gvfs-url-file-name filename)))
1124
	  ;; Parse output.
1125
	  (with-current-buffer (tramp-get-connection-buffer v)
1126
	    (goto-char (point-min))
Michael Albinus's avatar
Michael Albinus committed
1127
	    (while (re-search-forward
1128 1129 1130 1131
		    (if file-system
			tramp-gvfs-file-system-attributes-regexp
		      tramp-gvfs-file-attributes-with-gvfs-info-regexp)
		    nil t)
Michael Albinus's avatar
Michael Albinus committed
1132 1133 1134 1135 1136 1137 1138
	      (push (cons (match-string 1) (match-string 2)) result))
	    result))))))

(defun tramp-gvfs-get-file-attributes (filename)
  "Return GVFS attributes association list of FILENAME."
  (setq filename (directory-file-name (expand-file-name filename)))
  (with-parsed-tramp-file-name filename nil
1139
    (setq localname (tramp-compat-file-name-unquote localname))
1140 1141 1142
    (if (or (and (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
		 (string-match "^/?\\([^/]+\\)$" localname))
	    (string-equal localname "/"))
Michael Albinus's avatar
Michael Albinus committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199