tramp-gvfs.el 90.1 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 106
;;   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")

(require 'tramp)
107

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

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

117
;;;###tramp-autoload
118
(defcustom tramp-gvfs-methods
119
  '("afp" "dav" "davs" "gdrive" "obex" "owncloud" "sftp" "synce")
120
  "List of methods for remote files, accessed with GVFS."
121
  :group 'tramp
122
  :version "26.1"
Michael Albinus's avatar
Michael Albinus committed
123 124
  :type '(repeat (choice (const "afp")
			 (const "dav")
125 126
			 (const "davs")
			 (const "ftp")
127
			 (const "gdrive")
128 129
			 (const "http")
			 (const "https")
130
			 (const "obex")
131
			 (const "owncloud")
132
			 (const "sftp")
133
			 (const "smb")
134 135
			 (const "synce")))
  :require 'tramp)
136

137 138 139 140 141 142 143 144
(defconst tramp-goa-methods '("gdrive" "owncloud")
  "List of methods which require registration at GNOME Online Accounts.")

;; Remove GNOME Online Accounts if not supported.
(unless (member tramp-goa-service (dbus-list-known-names :session))
  (dolist (method tramp-goa-methods)
    (setq tramp-gvfs-methods (delete method tramp-gvfs-methods))))

145 146 147 148 149 150 151 152
;; 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))))
153
;;;###tramp-autoload
154
(add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
155

156
;;;###tramp-autoload
157
(defcustom tramp-gvfs-zeroconf-domain "local"
158
  "Zeroconf domain to be used for discovering services, like host names."
159
  :group 'tramp
160
  :version "23.2"
161 162
  :type 'string
  :require 'tramp)
163 164 165

;; Add the methods to `tramp-methods', in order to allow minibuffer
;; completion.
166 167 168 169 170
;;;###tramp-autoload
(when (featurep 'dbusbind)
  (dolist (elt tramp-gvfs-methods)
    (unless (assoc elt tramp-methods)
      (add-to-list 'tramp-methods (cons elt nil)))))
171 172

(defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
Glenn Morris's avatar
Glenn Morris committed
173
  "The preceding object path for own objects.")
174 175 176 177

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

178 179 180 181
;; We don't call `dbus-ping', because this would load dbus.el.
(defconst tramp-gvfs-enabled
  (ignore-errors
    (and (featurep 'dbusbind)
182
	 (tramp-compat-funcall 'dbus-get-unique-name :system)
183 184 185 186
	 (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.")
187 188 189 190 191 192 193

(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.")

194 195 196
;; Introspection data exist since GVFS 1.14.  If there are no such
;; data, we expect an earlier interface.
(defconst tramp-gvfs-methods-mounttracker
197 198 199 200
  (and tramp-gvfs-enabled
       (dbus-introspect-get-method-names
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker))
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  "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
218 219 220 221
  (and tramp-gvfs-enabled
       (dbus-introspect-get-signature
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation))
222 223 224
  "The D-Bus signature of the \"mountLocation\" method.
It has been changed in GVFS 1.14.")

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

(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'>
270
;;     <arg name='message'        type='s' direction='in'/>
271 272 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
;;     <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.")

309 310 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
;; 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)

465 466 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
(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>

561
;;;###tramp-autoload
562 563 564 565 566 567
(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
568
  :version "23.2"
569 570
  :type '(choice (const nil) integer)
  :require 'tramp)
571 572 573 574 575 576 577 578 579

(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).")

580 581 582 583 584 585 586 587 588 589 590 591
(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
592 593 594 595 596 597 598 599
;; "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")
600
    ("gvfs-mount" . "mount")
Michael Albinus's avatar
Michael Albinus committed
601 602 603 604 605
    ("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
606
;; <http://www.pygtk.org/docs/pygobject/gio-constants.html>
Michael Albinus's avatar
Michael Albinus committed
607
(defconst tramp-gvfs-file-attributes
608 609
  '("name"
    "type"
Michael Albinus's avatar
Michael Albinus committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    "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
630
  (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t) "=\\(.+?\\)")
Michael Albinus's avatar
Michael Albinus committed
631 632 633 634 635 636 637 638
  "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'.")

639 640 641 642 643 644 645 646 647 648 649 650
(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'.")

651 652 653 654 655 656 657
(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.")

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

736 737
;; It must be a `defsubst' in order to push the whole code into
;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
738 739
;;;###tramp-autoload
(defsubst tramp-gvfs-file-name-p (filename)
740 741 742 743 744 745
  "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)))))

746
;;;###tramp-autoload
747 748 749 750
(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."
751
  (unless tramp-gvfs-enabled
Michael Albinus's avatar
Michael Albinus committed
752
    (tramp-compat-user-error nil "Package `tramp-gvfs' not supported"))
753 754 755 756 757
  (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))))

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

763 764 765 766 767 768 769 770 771 772

;; 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)
773 774
  "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists.
Return nil for null BYTE-ARRAY."
775 776 777 778
  ;; 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))))
779 780 781 782
    (and byte-array
	 (dbus-byte-array-to-string
	  (if (and (consp byte-array) (zerop (car (last byte-array))))
	      (butlast byte-array) byte-array)))))
783

784 785 786 787
(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)))
788
    (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
789 790 791
   ((and (consp message) (not (consp (cdr message))))
    (cons (tramp-gvfs-stringify-dbus-message (car message))
	  (tramp-gvfs-stringify-dbus-message (cdr message))))
792 793 794 795 796 797
   ((consp message)
    (mapcar 'tramp-gvfs-stringify-dbus-message message))
   ((stringp message)
    (format "%S" message))
   (t message)))

798 799 800 801 802 803 804 805 806
(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))

807 808 809 810 811 812 813 814
(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'
815
or `dbus-call-method-asynchronously'."
816 817 818
  `(let ((func (if ,synchronous
		   'dbus-call-method 'dbus-call-method-asynchronously))
	 (args (append (list ,bus ,service ,path ,interface ,method)
819 820
		       (if ,synchronous (list ,@args) (list 'ignore ,@args)))))
     (tramp-dbus-function ,vec func args)))
821 822 823

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

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
(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\\>"))

843 844 845 846 847 848
(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)
849
  "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
850
  (when tramp-gvfs-dbus-event-vector
Michael Albinus's avatar
Michael Albinus committed
851
    (tramp-message tramp-gvfs-dbus-event-vector 6 "%S" event)
852
    (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
853

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


;; File name primitives.

Michael Albinus's avatar
Michael Albinus committed
864 865
(defun tramp-gvfs-do-copy-or-rename-file
  (op filename newname &optional ok-if-already-exists keep-date
866
   preserve-uid-gid preserve-extended-attributes)
Michael Albinus's avatar
Michael Albinus committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
  "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))
882
    (error "Unknown operation `%s', must be `copy' or `rename'" op))
Michael Albinus's avatar
Michael Albinus committed
883

Michael Albinus's avatar
Michael Albinus committed
884
  (setq filename (file-truename filename))
Michael Albinus's avatar
Michael Albinus committed
885 886 887 888 889
  (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
890 891 892 893 894 895 896 897
    (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))
898
	  (tramp-error v 'file-already-exists newname))
Michael Albinus's avatar
Michael Albinus committed
899 900 901 902 903 904 905 906

	(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
907 908 909 910 911
	      (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
912 913 914 915 916 917 918 919 920 921
	      (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)
922
		       '("--preserve"))
Michael Albinus's avatar
Michael Albinus committed
923 924 925 926 927 928 929 930 931 932 933 934 935
		  (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
936
		     "%s failed, see buffer `%s' for details."
Michael Albinus's avatar
Michael Albinus committed
937 938 939 940 941 942 943 944 945 946 947
		     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
948 949
	      (tramp-flush-file-properties v (file-name-directory localname))
	      (tramp-flush-file-properties v localname)))
Michael Albinus's avatar
Michael Albinus committed
950 951 952

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

956
(defun tramp-gvfs-handle-copy-file
Michael Albinus's avatar
Michael Albinus committed
957
  (filename newname &optional ok-if-already-exists keep-date
958
   preserve-uid-gid preserve-extended-attributes)
959
  "Like `copy-file' for Tramp files."
Michael Albinus's avatar
Michael Albinus committed
960 961
  (setq filename (expand-file-name filename))
  (setq newname (expand-file-name newname))
962 963 964 965 966 967
  ;; 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
968 969 970
    (tramp-run-real-handler
     'copy-file
     (list filename newname ok-if-already-exists keep-date
971
	   preserve-uid-gid preserve-extended-attributes))))
972 973

(defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
974
  "Like `delete-directory' for Tramp files."
975
  (with-parsed-tramp-file-name directory nil
976 977
    (if (and recursive (not (file-symlink-p directory)))
	(mapc (lambda (file)
978 979
		(if (eq t (tramp-compat-file-attribute-type
			   (file-attributes file)))
980 981
		    (delete-directory file recursive trash)
		  (delete-file file trash)))
982 983 984 985 986 987
	      (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)))

988 989
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-directory-properties v localname)
990 991 992 993 994 995 996 997
    (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
998
	 nil v 'file-error "Couldn't delete %s" directory)))))
999

1000
(defun tramp-gvfs-handle-delete-file (filename &optional trash)
1001
  "Like `delete-file' for Tramp files."
1002
  (with-parsed-tramp-file-name filename nil
1003 1004
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-file-properties v localname)
1005 1006 1007 1008 1009 1010 1011 1012
    (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
1013
	 nil v 'file-error "Couldn't delete %s" filename)))))
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

(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
1027 1028 1029
      ;; If there is a default location, expand tilde.
      (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
	(save-match-data
1030
	  (tramp-gvfs-maybe-open-connection
1031 1032 1033
	   (make-tramp-file-name
	    :method method :user user :domain domain
	    :host host :port port :localname "/" :hop hop)))
1034 1035
	(setq localname
	      (replace-match
1036
	       (tramp-get-connection-property v "default-location" "~")
1037
	       nil t localname 1)))
1038 1039 1040 1041
      ;; Tilde expansion is not possible.
      (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
	(tramp-error
	 v 'file-error
1042
	 "Cannot expand tilde in file `%s'" name))
1043 1044 1045
      (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
	(setq localname (concat "/" localname)))
      ;; We do not pass "/..".
1046
      (if (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	  (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
1057
       method user domain host port
1058
       (tramp-run-real-handler 'expand-file-name (list localname))))))
1059

Michael Albinus's avatar
Michael Albinus committed
1060 1061
(defun tramp-gvfs-get-directory-attributes (directory)
  "Return GVFS attributes association list of all files in DIRECTORY."
1062 1063 1064
  (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
1065 1066
	  result)
      (with-parsed-tramp-file-name directory nil
1067
	(with-tramp-file-property v localname "directory-attributes"
Michael Albinus's avatar
Michael Albinus committed
1068 1069 1070 1071 1072 1073
	  (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))
1074
	  ;; Parse output.
Michael Albinus's avatar
Michael Albinus committed
1075 1076
	  (with-current-buffer (tramp-get-connection-buffer v)
	    (goto-char (point-min))
1077
	    (while (looking-at
Michael Albinus's avatar
Michael Albinus committed
1078 1079
		    (concat "^\\(.+\\)[[:blank:]]"
			    "\\([[:digit:]]+\\)[[:blank:]]"
1080 1081 1082
			    "(\\(.+?\\))"
			    tramp-gvfs-file-attributes-with-gvfs-ls-regexp))
	      (let ((item (list (cons "type" (match-string 3))
Michael Albinus's avatar
Michael Albinus committed
1083
				(cons "standard::size" (match-string 2))
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
				(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
1099 1100 1101
	      (forward-line)))
	  result)))))

1102 1103 1104
(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
1105 1106 1107 1108
  (ignore-errors
    ;; Don't modify `last-coding-system-used' by accident.
    (let ((last-coding-system-used last-coding-system-used)
	  result)
1109
      (with-parsed-tramp-file-name filename nil
1110 1111 1112 1113 1114 1115
	(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
1116
	  ;; Send command.
1117 1118 1119 1120 1121
	  (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)))
1122
	  ;; Parse output.
1123
	  (with-current-buffer (tramp-get-connection-buffer v)
1124
	    (goto-char (point-min))
Michael Albinus's avatar
Michael Albinus committed
1125
	    (while (re-search-forward
1126 1127 1128 1129
		    (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
1130 1131 1132 1133 1134 1135 1136
	      (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
1137
    (setq localname (tramp-compat-file-name-unquote localname))
1138 1139 1140
    (if (or (and (string-match "^\\(afp\\|davs?\\|smb\\)$" method)
		 (string-match "^/?\\([^/]+\\)$" localname))
	    (string-equal localname "/"))
Michael Albinus's avatar
Michael Albinus committed
1141 1142 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 1200 1201 1202
	(tramp-gvfs-get-root-attributes filename)
      (assoc
       (file-name-nondirectory filename)
       (tramp-gvfs-get-directory-attributes (file-name-directory filename))))))