tramp-gvfs.el 75 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-2017 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 22 23 24 25 26

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; 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 23.1 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
;; "davs", "gdrive", "obex", "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.
56

57 58 59
;; Other possible connection methods are "ftp" 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
;; implementation.

;; 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
;;    tramp-gvfs-interface-mounttracker "listMountableInfo")))

;; 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
80
;; bluez daemon (for the "obex" method), the hal daemon (for the
81
;; "synce" method), or from the zeroconf daemon (for the "afp", "dav",
82 83
;; "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
84
;; shall be used for discovering services, the custom option
85
;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
86 87 88

;; Restrictions:

89
;; * The current GVFS implementation does not allow writing on the
90 91 92 93 94 95 96 97 98 99 100
;;   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)
101

102 103
(require 'dbus)
(require 'url-parse)
104
(require 'url-util)
105 106
(require 'zeroconf)

Michael Albinus's avatar
Michael Albinus committed
107 108 109
;; Pacify byte-compiler.
(eval-when-compile
  (require 'cl)
Michael Albinus's avatar
Michael Albinus committed
110
  (require 'custom))
Michael Albinus's avatar
Michael Albinus committed
111

112
;;;###tramp-autoload
113 114
(defcustom tramp-gvfs-methods
  '("afp" "dav" "davs" "gdrive" "obex" "sftp" "synce")
115
  "List of methods for remote files, accessed with GVFS."
116
  :group 'tramp
117
  :version "26.1"
Michael Albinus's avatar
Michael Albinus committed
118 119
  :type '(repeat (choice (const "afp")
			 (const "dav")
120 121
			 (const "davs")
			 (const "ftp")
122
			 (const "gdrive")
123 124
			 (const "obex")
			 (const "sftp")
125
			 (const "smb")
126 127
			 (const "synce")))
  :require 'tramp)
128

129 130 131 132 133 134 135 136
;; 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))))
137
;;;###tramp-autoload
138
(add-to-list 'tramp-default-user-alist '("\\`synce\\'" nil nil))
139

140
;;;###tramp-autoload
141
(defcustom tramp-gvfs-zeroconf-domain "local"
142
  "Zeroconf domain to be used for discovering services, like host names."
143
  :group 'tramp
144
  :version "23.2"
145 146
  :type 'string
  :require 'tramp)
147 148 149

;; Add the methods to `tramp-methods', in order to allow minibuffer
;; completion.
150 151 152 153 154
;;;###tramp-autoload
(when (featurep 'dbusbind)
  (dolist (elt tramp-gvfs-methods)
    (unless (assoc elt tramp-methods)
      (add-to-list 'tramp-methods (cons elt nil)))))
155 156

(defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
Glenn Morris's avatar
Glenn Morris committed
157
  "The preceding object path for own objects.")
158 159 160 161

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

162 163 164 165 166
;; D-Bus integration is available since Emacs 23 on some system types.
;; We don't call `dbus-ping', because this would load dbus.el.
(defconst tramp-gvfs-enabled
  (ignore-errors
    (and (featurep 'dbusbind)
167
	 (tramp-compat-funcall 'dbus-get-unique-name :system)
168 169 170 171
	 (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.")
172 173 174 175 176 177 178

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

179 180 181
;; Introspection data exist since GVFS 1.14.  If there are no such
;; data, we expect an earlier interface.
(defconst tramp-gvfs-methods-mounttracker
182 183 184 185
  (and tramp-gvfs-enabled
       (dbus-introspect-get-method-names
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker))
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  "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
203 204 205 206
  (and tramp-gvfs-enabled
       (dbus-introspect-get-signature
	:session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
	tramp-gvfs-interface-mounttracker tramp-gvfs-mountlocation))
207 208 209
  "The D-Bus signature of the \"mountLocation\" method.
It has been changed in GVFS 1.14.")

210 211 212
;; <interface name='org.gtk.vfs.MountTracker'>
;;   <method name='listMounts'>
;;     <arg name='mount_info_list'
213
;;          type='a{sosssssbay{aya{say}}ay}'
214 215 216 217 218 219 220 221 222
;;          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'
223
;;          type='{sosssssbay{aya{say}}ay}'/>
224 225 226
;;   </signal>
;;   <signal name='unmounted'>
;;     <arg name='mount_info'
227
;;          type='{sosssssbay{aya{say}}ay}'/>
228 229 230 231 232 233 234 235
;;   </signal>
;; </interface>
;;
;; STRUCT		mount_info
;;   STRING		  dbus_id
;;   OBJECT_PATH	  object_path
;;   STRING		  display_name
;;   STRING		  stable_name
236
;;   STRING		  x_content_types	Since GVFS 1.0 only !!!
237
;;   STRING		  icon
Paul Eggert's avatar
Paul Eggert committed
238
;;   STRING		  preferred_filename_encoding
239 240 241 242 243 244
;;   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
245 246
;;         STRING	      key (type, user, domain, host, server,
;;                                 share, volume, port, ssl)
247
;;         ARRAY BYTE	      value
248
;;   ARRAY BYTE           default_location	Since GVFS 1.5 only !!!
249 250 251 252 253 254

(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'>
255
;;     <arg name='message'        type='s' direction='in'/>
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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 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
;;     <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.")

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

390
;;;###tramp-autoload
391 392 393 394 395 396
(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
397
  :version "23.2"
398 399
  :type '(choice (const nil) integer)
  :require 'tramp)
400 401 402 403 404 405 406 407 408

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

409 410 411 412 413 414 415 416 417 418 419 420
(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
421
(defconst tramp-gvfs-file-attributes
422 423
  '("name"
    "type"
Michael Albinus's avatar
Michael Albinus committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    "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
444
  (concat "[[:blank:]]" (regexp-opt tramp-gvfs-file-attributes t) "=\\(.+?\\)")
Michael Albinus's avatar
Michael Albinus committed
445 446 447 448 449 450 451 452
  "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'.")

453

454
;; New handlers should be added here.
Michael Albinus's avatar
Michael Albinus committed
455
;;;###tramp-autoload
456
(defconst tramp-gvfs-file-name-handler-alist
Michael Albinus's avatar
Michael Albinus committed
457
  '((access-file . ignore)
458
    (add-name-to-file . tramp-gvfs-handle-copy-file)
459
    ;; `byte-compiler-base-file-name' performed by default handler.
Michael Albinus's avatar
Michael Albinus committed
460
    ;; `copy-directory' performed by default handler.
461 462 463
    (copy-file . tramp-gvfs-handle-copy-file)
    (delete-directory . tramp-gvfs-handle-delete-directory)
    (delete-file . tramp-gvfs-handle-delete-file)
464
    ;; `diff-latest-backup-file' performed by default handler.
465
    (directory-file-name . tramp-handle-directory-file-name)
466
    (directory-files . tramp-handle-directory-files)
467
    (directory-files-and-attributes
468
     . tramp-handle-directory-files-and-attributes)
469 470 471
    (dired-compress-file . ignore)
    (dired-uncache . tramp-handle-dired-uncache)
    (expand-file-name . tramp-gvfs-handle-expand-file-name)
472
    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
473
    (file-acl . ignore)
474
    (file-attributes . tramp-gvfs-handle-file-attributes)
475
    (file-directory-p . tramp-gvfs-handle-file-directory-p)
476
    (file-equal-p . tramp-handle-file-equal-p)
477
    (file-executable-p . tramp-gvfs-handle-file-executable-p)
478
    (file-exists-p . tramp-handle-file-exists-p)
479
    (file-in-directory-p . tramp-handle-file-in-directory-p)
480
    (file-local-copy . tramp-gvfs-handle-file-local-copy)
481
    (file-modes . tramp-handle-file-modes)
482 483
    (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
    (file-name-as-directory . tramp-handle-file-name-as-directory)
484
    (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
485 486 487
    (file-name-completion . tramp-handle-file-name-completion)
    (file-name-directory . tramp-handle-file-name-directory)
    (file-name-nondirectory . tramp-handle-file-name-nondirectory)
488
    ;; `file-name-sans-versions' performed by default handler.
489
    (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
Michael Albinus's avatar
Michael Albinus committed
490 491
    (file-notify-add-watch . tramp-gvfs-handle-file-notify-add-watch)
    (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
492
    (file-notify-valid-p . tramp-handle-file-notify-valid-p)
493 494 495
    (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
496
    (file-remote-p . tramp-handle-file-remote-p)
497
    (file-selinux-context . ignore)
498
    (file-symlink-p . tramp-handle-file-symlink-p)
499
    ;; `file-truename' performed by default handler.
500 501
    (file-writable-p . tramp-gvfs-handle-file-writable-p)
    (find-backup-file-name . tramp-handle-find-backup-file-name)
502 503
    ;; `find-file-noselect' performed by default handler.
    ;; `get-file-buffer' performed by default handler.
504
    (insert-directory . tramp-handle-insert-directory)
505
    (insert-file-contents . tramp-handle-insert-file-contents)
506
    (load . tramp-handle-load)
507
    (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
508 509
    (make-directory . tramp-gvfs-handle-make-directory)
    (make-directory-internal . ignore)
510
    (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
511
    (make-symbolic-link . tramp-handle-make-symbolic-link)
512
    (process-file . ignore)
513
    (rename-file . tramp-gvfs-handle-rename-file)
514 515 516
    (set-file-acl . ignore)
    (set-file-modes . ignore)
    (set-file-selinux-context . ignore)
Michael Albinus's avatar
Michael Albinus committed
517 518
    (set-file-times . ignore)
    (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
519 520
    (shell-command . ignore)
    (start-file-process . ignore)
521
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
522
    (temporary-file-directory . tramp-handle-temporary-file-directory)
523
    (unhandled-file-name-directory . ignore)
524
    (vc-registered . ignore)
Michael Albinus's avatar
Michael Albinus committed
525 526
    (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
    (write-region . tramp-gvfs-handle-write-region))
527 528 529
  "Alist of handler functions for Tramp GVFS method.
Operations not mentioned here will be handled by the default Emacs primitives.")

530 531
;; It must be a `defsubst' in order to push the whole code into
;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
532 533
;;;###tramp-autoload
(defsubst tramp-gvfs-file-name-p (filename)
534 535 536 537 538 539
  "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)))))

540
;;;###tramp-autoload
541 542 543 544
(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."
545
  (unless tramp-gvfs-enabled
Michael Albinus's avatar
Michael Albinus committed
546
    (tramp-compat-user-error nil "Package `tramp-gvfs' not supported"))
547 548 549 550 551
  (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))))

552 553
;;;###tramp-autoload
(when (featurep 'dbusbind)
Michael Albinus's avatar
Michael Albinus committed
554 555
  (tramp-register-foreign-file-name-handler
   'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler))
556

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

;; 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)
  "Like `dbus-byte-array-to-string' but remove trailing \\0 if exists."
  ;; 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))))
    (dbus-byte-array-to-string
     (if (and (consp byte-array) (zerop (car (last byte-array))))
	 (butlast byte-array) byte-array))))

576 577 578 579
(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)))
580
    (format "%S" (tramp-gvfs-dbus-byte-array-to-string message)))
581 582 583 584 585 586
   ((consp message)
    (mapcar 'tramp-gvfs-stringify-dbus-message message))
   ((stringp message)
    (format "%S" message))
   (t message)))

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
(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'
or `dbus-call-method-asynchronously'.  Additionally, the call
will be traced by Tramp with trace level 6."
  `(let ((func (if ,synchronous
		   'dbus-call-method 'dbus-call-method-asynchronously))
	 (args (append (list ,bus ,service ,path ,interface ,method)
		       (if ,synchronous (list ,@args) (list 'ignore ,@args))))
	 result)
     (tramp-message ,vec 6 "%s %s" func args)
     (setq result (apply func args))
604
     (tramp-message ,vec 6 "%s" (tramp-gvfs-stringify-dbus-message result))
605 606 607 608
     result))

(put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
(put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
609
(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
610 611 612 613 614 615 616

(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)
617
  "Called when a D-Bus error message arrives, see `dbus-event-error-functions'."
618 619 620
  (when tramp-gvfs-dbus-event-vector
    (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
    (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
621

622 623 624 625 626
;; `dbus-event-error-hooks' has been renamed to `dbus-event-error-functions'.
(add-hook
 (if (boundp 'dbus-event-error-functions)
     'dbus-event-error-functions 'dbus-event-error-hooks)
 'tramp-gvfs-dbus-event-error)
627 628 629 630


;; File name primitives.

Michael Albinus's avatar
Michael Albinus committed
631 632
(defun tramp-gvfs-do-copy-or-rename-file
  (op filename newname &optional ok-if-already-exists keep-date
633
   preserve-uid-gid preserve-extended-attributes)
Michael Albinus's avatar
Michael Albinus committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  "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))
649
    (error "Unknown operation `%s', must be `copy' or `rename'" op))
Michael Albinus's avatar
Michael Albinus committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

    (let ((t1 (tramp-tramp-file-p filename))
	  (t2 (tramp-tramp-file-p newname))
	  (equal-remote (tramp-equal-remote filename newname))
	  (file-operation (intern (format "%s-file" op)))
	  (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))
	  (tramp-error
	   v 'file-already-exists "File %s already exists" newname))

	(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.
669 670 671
	    ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with
	    ;; Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and renamed
	    ;; in Emacs 24.3.
Michael Albinus's avatar
Michael Albinus committed
672 673 674
	    (let ((tmpfile (tramp-compat-make-temp-file filename)))
	      (cond
	       (preserve-extended-attributes
675
		(funcall
Michael Albinus's avatar
Michael Albinus committed
676 677 678 679
		 file-operation
		 filename tmpfile t keep-date preserve-uid-gid
		 preserve-extended-attributes))
	       (t
680 681
		(funcall
		 file-operation filename tmpfile t keep-date preserve-uid-gid)))
Michael Albinus's avatar
Michael Albinus committed
682 683 684 685 686 687 688 689 690 691
	      (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)
692
		       '("--preserve"))
Michael Albinus's avatar
Michael Albinus committed
693 694 695 696 697 698 699 700 701 702 703 704 705
		  (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
706
		     "%s failed, see buffer `%s' for details."
Michael Albinus's avatar
Michael Albinus committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		     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
	      (tramp-flush-file-property v (file-name-directory localname))
	      (tramp-flush-file-property v localname)))

	  (when t2
	    (with-parsed-tramp-file-name newname nil
	      (tramp-flush-file-property v (file-name-directory localname))
	      (tramp-flush-file-property v localname)))))))

726
(defun tramp-gvfs-handle-copy-file
Michael Albinus's avatar
Michael Albinus committed
727
  (filename newname &optional ok-if-already-exists keep-date
728
   preserve-uid-gid preserve-extended-attributes)
729
  "Like `copy-file' for Tramp files."
Michael Albinus's avatar
Michael Albinus committed
730 731 732 733 734 735 736 737 738
  (setq filename (expand-file-name filename))
  (setq newname (expand-file-name newname))
  (cond
   ;; At least one file a Tramp file?
   ((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))
739 740 741
   ;; Compat section.  PRESERVE-EXTENDED-ATTRIBUTES has been
   ;; introduced with Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and
   ;; renamed in Emacs 24.3.
Michael Albinus's avatar
Michael Albinus committed
742 743 744 745 746 747 748
   (preserve-extended-attributes
    (tramp-run-real-handler
     'copy-file
     (list filename newname ok-if-already-exists keep-date
	   preserve-uid-gid preserve-extended-attributes)))
   (t
    (tramp-run-real-handler
749 750
     'copy-file
     (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))))
751 752

(defun tramp-gvfs-handle-delete-directory (directory &optional recursive trash)
753
  "Like `delete-directory' for Tramp files."
754
  (with-parsed-tramp-file-name directory nil
755 756
    (if (and recursive (not (file-symlink-p directory)))
	(mapc (lambda (file)
757 758
		(if (eq t (tramp-compat-file-attribute-type
			   (file-attributes file)))
759 760 761 762 763 764 765 766
		    (tramp-compat-delete-directory file recursive trash)
		  (tramp-compat-delete-file file trash)))
	      (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)))

767 768 769 770 771 772 773 774 775 776
    (tramp-flush-file-property v (file-name-directory localname))
    (tramp-flush-directory-property v localname)
    (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
777
	 nil v 'file-error "Couldn't delete %s" directory)))))
778

779
(defun tramp-gvfs-handle-delete-file (filename &optional trash)
780
  "Like `delete-file' for Tramp files."
781 782
  (with-parsed-tramp-file-name filename nil
    (tramp-flush-file-property v (file-name-directory localname))
783
    (tramp-flush-file-property v localname)
784 785 786 787 788 789 790 791
    (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
792
	 nil v 'file-error "Couldn't delete %s" filename)))))
793 794 795 796 797 798 799 800 801 802 803 804 805

(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
806 807 808
      ;; If there is a default location, expand tilde.
      (when (string-match "\\`\\(~\\)\\(/\\|\\'\\)" localname)
	(save-match-data
Michael Albinus's avatar
Michael Albinus committed
809
	  (tramp-gvfs-maybe-open-connection (vector method user host "/" hop)))
810 811
	(setq localname
	      (replace-match
812
	       (tramp-get-connection-property v "default-location" "~")
813
	       nil t localname 1)))
814 815 816 817
      ;; Tilde expansion is not possible.
      (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
	(tramp-error
	 v 'file-error
818
	 "Cannot expand tilde in file `%s'" name))
819 820 821
      (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
	(setq localname (concat "/" localname)))
      ;; We do not pass "/..".
Michael Albinus's avatar
Michael Albinus committed
822
      (if (string-match "^\\(afp\\|smb\\)$" method)
823 824 825 826 827 828 829 830 831 832 833 834 835 836
	  (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
       method user host
       (tramp-run-real-handler
	'expand-file-name (list localname))))))

Michael Albinus's avatar
Michael Albinus committed
837 838
(defun tramp-gvfs-get-directory-attributes (directory)
  "Return GVFS attributes association list of all files in DIRECTORY."
839 840 841
  (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
842 843 844 845 846 847 848 849 850
	  result)
      (with-parsed-tramp-file-name directory nil
	(with-tramp-file-property v localname "directory-gvfs-attributes"
	  (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))
851
	  ;; Parse output.
Michael Albinus's avatar
Michael Albinus committed
852 853
	  (with-current-buffer (tramp-get-connection-buffer v)
	    (goto-char (point-min))
854
	    (while (looking-at
Michael Albinus's avatar
Michael Albinus committed
855 856
		    (concat "^\\(.+\\)[[:blank:]]"
			    "\\([[:digit:]]+\\)[[:blank:]]"
857 858 859
			    "(\\(.+?\\))"
			    tramp-gvfs-file-attributes-with-gvfs-ls-regexp))
	      (let ((item (list (cons "type" (match-string 3))
Michael Albinus's avatar
Michael Albinus committed
860
				(cons "standard::size" (match-string 2))
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
				(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
876 877 878 879 880 881 882 883 884
	      (forward-line)))
	  result)))))

(defun tramp-gvfs-get-root-attributes (filename)
  "Return GVFS attributes association list of FILENAME."
  (ignore-errors
    ;; Don't modify `last-coding-system-used' by accident.
    (let ((last-coding-system-used last-coding-system-used)
	  result)
885
      (with-parsed-tramp-file-name filename nil
Michael Albinus's avatar
Michael Albinus committed
886 887 888
	(with-tramp-file-property v localname "file-gvfs-attributes"
	  (tramp-message v 5 "file gvfs attributes: %s" localname)
	  ;; Send command.
889 890
	  (tramp-gvfs-send-command
	   v "gvfs-info" (tramp-gvfs-url-file-name filename))
891
	  ;; Parse output.
892
	  (with-current-buffer (tramp-get-connection-buffer v)
893
	    (goto-char (point-min))
Michael Albinus's avatar
Michael Albinus committed
894 895 896 897 898 899 900 901 902
	    (while (re-search-forward
		    tramp-gvfs-file-attributes-with-gvfs-info-regexp nil t)
	      (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
903
    (setq localname (tramp-compat-file-name-unquote localname))
Michael Albinus's avatar
Michael Albinus committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    (if (or
	 (and (string-match "^\\(afp\\|smb\\)$" method)
	      (string-match "^/?\\([^/]+\\)$" localname))
	 (string-equal localname "/"))
	(tramp-gvfs-get-root-attributes filename)
      (assoc
       (file-name-nondirectory filename)
       (tramp-gvfs-get-directory-attributes (file-name-directory filename))))))

(defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
  "Like `file-attributes' for Tramp files."
  (unless id-format (setq id-format 'integer))
  (ignore-errors
    (let ((attributes (tramp-gvfs-get-file-attributes filename))
	  dirp res-symlink-target res-numlinks res-uid res-gid res-access
	  res-mod res-change res-size res-filemodes res-inode res-device)
      (when attributes
	;; ... directory or symlink
	(setq dirp (if (equal "directory" (cdr (assoc "type" attributes))) t))
	(setq res-symlink-target
	      (cdr (assoc "standard::symlink-target" attributes)))
	;; ... number links
	(setq res-numlinks
	      (string-to-number
	       (or (cdr (assoc "unix::nlink" attributes)) "0")))
	;; ... uid and gid
	(setq res-uid
	      (if (eq id-format 'integer)
		  (string-to-number
		   (or (cdr (assoc "unix::uid" attributes))
		       (format "%s" tramp-unknown-id-integer)))
		(or (cdr (assoc "owner::user" attributes))
		    (cdr (assoc "unix::uid" attributes))
		    tramp-unknown-id-string)))
	(setq res-gid
	      (if (eq id-format 'integer)
		  (string-to-number
		   (or (cdr (assoc "unix::gid" attributes))
		       (format "%s" tramp-unknown-id-integer)))
		(or (cdr (assoc "owner::group" attributes))
		    (cdr (assoc "unix::gid" attributes))
		    tramp-unknown-id-string)))
	;; ... last access, modification and change time
	(setq res-access
	      (seconds-to-time
	       (string-to-number
		(or (cdr (assoc "time::access" attributes)) "0"))))
	(setq res-mod
	      (seconds-to-time
	       (string-to-number
		(or (cdr (assoc "time::modified" attributes)) "0"))))
	(setq res-change
	      (seconds-to-time
	       (string-to-number
		(or (cdr (assoc "time::changed" attributes)) "0"))))
	;; ... size
	(setq res-size
	      (string-to-number
	       (or (cdr (assoc "standard::size" attributes)) "0")))
	;; ... file mode flags
	(setq res-filemodes
	      (let ((n (cdr (assoc "unix::mode" attributes))))
		(if n
		    (tramp-file-mode-from-int (string-to-number n))
		  (format
		   "%s%s%s%s------"
		   (if dirp "d" "-")
		   (if (equal (cdr (assoc "access::can-read" attributes))
			      "FALSE")
		       "-" "r")
		   (if (equal (cdr (assoc "access::can-write" attributes))
			      "FALSE")
		       "-" "w")
		   (if (equal (cdr (assoc "access::can-execute" attributes))
			      "FALSE")
		       "-" "x")))))
	;; ... inode and device
	(setq res-inode
	      (let ((n (cdr (assoc "unix::inode" attributes))))
		(if n
		    (string-to-number n)
		  (tramp-get-inode (tramp-dissect-file-name filename)))))
	(setq res-device
	      (let ((n (cdr (assoc "unix::device" attributes))))
		(if n
		    (string-to-number n)
		  (tramp-get-device (tramp-dissect-file-name filename)))))

	;; Return data gathered.
	(list
	 ;; 0. t for directory, string (name linked to) for
	 ;; symbolic link, or nil.
	 (or dirp res-symlink-target)
	 ;; 1. Number of links to file.
	 res-numlinks
	 ;; 2. File uid.
	 res-uid
	 ;; 3. File gid.
	 res-gid
	 ;; 4. Last access time, as a list of integers.
	 ;; 5. Last modification time, likewise.
	 ;; 6. Last status change time, likewise.
	 res-access res-mod res-change
	 ;; 7. Size in bytes (-1, if number is out of range).
	 res-size
	 ;; 8. File modes.
	 res-filemodes
	 ;; 9. t if file's gid would change if file were deleted
	 ;; and recreated.
	 nil
	 ;; 10. Inode number.
	 res-inode
	 ;; 11. Device number.
	 res-device
	 )))))
1019

1020 1021
(defun tramp-gvfs-handle-file-directory-p (filename)
  "Like `file-directory-p' for Tramp files."
1022 1023
  (eq t (tramp-compat-file-attribute-type
	 (file-attributes (file-truename filename)))))
1024

1025 1026
(defun tramp-gvfs-handle-file-executable-p (filename)
  "Like `file-executable-p' for Tramp files."
1027 1028 1029
  (with-parsed-tramp-file-name filename nil
    (with-tramp-file-property v localname "file-executable-p"
      (tramp-check-cached-permissions v ?x))))
1030 1031 1032 1033 1034 1035 1036

(defun tramp-gvfs-handle-file-local-copy (filename)
  "Like `file-local-copy' for Tramp files."
  (with-parsed-tramp-file-name filename nil
    (let ((tmpfile (tramp-compat-make-temp-file filename)))
      (unless (file-exists-p filename)
	(tramp-error
Michael Albinus's avatar
Michael Albinus committed
1037
	 v tramp-file-missing
1038
	 "Cannot make local copy of non-existing file `%s'" filename))
1039
      (copy-file filename tmpfile 'ok-if-already-exists 'keep-time)
1040 1041 1042 1043 1044
      tmpfile)))

(defun tramp-gvfs-handle-file-name-all-completions (filename directory)
  "Like `file-name-all-completions' for Tramp files."
  (unless (save-match-data (string-match "/" filename))
Michael Albinus's avatar
Michael Albinus committed
1045 1046 1047 1048
    (all-completions
     filename
     (with-parsed-tramp-file-name (expand-file-name directory) nil
       (with-tramp-file-property v localname "file-name-all-completions"
1049
         (let ((result '("./" "../")))
1050
           ;; Get a list of directories and files.
Michael Albinus's avatar
Michael Albinus committed
1051 1052
	   (dolist (item (tramp-gvfs-get-directory-attributes directory) result)
	     (if (string-equal (cdr (assoc "type" item)) "directory")
1053 1054
		 (push (file-name-as-directory (car item)) result)
	       (push (car item) result)))))))))
1055

1056
(defun tramp-gvfs-handle-file-notify-add-watch (file-name flags _callback)
Michael Albinus's avatar
Michael Albinus committed
1057 1058 1059
  "Like `file-notify-add-watch' for Tramp files."
  (setq file-name (expand-file-name file-name))
  (with-parsed-tramp-file-name file-name nil
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    ;; We cannot watch directories, because `gvfs-monitor-dir' is not
    ;; supported for gvfs-mounted directories.
    (when (file-directory-p file-name)
      (tramp-error
       v 'file-notify-error "Monitoring not supported for `%s'" file-name))
    (let* ((default-directory (file-name-directory file-name))
	   (events
	    (cond
	     ((and (memq 'change flags) (memq 'attribute-change flags))
	      '(created changed changes-done-hint moved deleted
			attribute-changed))
	     ((memq 'change flags)
	      '(created changed changes-done-hint moved deleted))
	     ((memq 'attribute-change flags) '(attribute-changed))))
	   (p (start-process
	       "gvfs-monitor-file" (generate-new-buffer " *gvfs-monitor-file*")
	       "gvfs-monitor-file" (tramp-gvfs-url-file-name file-name))))
Michael Albinus's avatar
Michael Albinus committed
1077 1078
      (if (not (processp p))
	  (tramp-error
1079
	   v 'file-notify-error "Monitoring not supported for `%s'" file-name)
Michael Albinus's avatar
Michael Albinus committed
1080
	(tramp-message
1081
	 v 6 "Run `%s', %S" (mapconcat 'identity (process-command p) " ") p)
Michael Albinus's avatar
Michael Albinus committed
1082
	(tramp-set-connection-property p "vector" v)
1083 1084 1085
	(process-put p 'events events)
	(process-put p 'watch-name localname)
	(set-process-query-on-exit-flag p nil)
1086 1087 1088 1089
	(set-process-filter p 'tramp-gvfs-monitor-file-process-filter)
	;; There might be an error if the monitor is not supported.
	;; Give the filter a chance to read the output.
	(tramp-accept-process-output p 1)
1090
	(unless (tramp-compat-process-live-p p)
1091 1092
	  (tramp-error
	   v 'file-notify-error "Monitoring not supported for `%s'" file-name))
Michael Albinus's avatar
Michael Albinus committed
1093 1094
	p))))

1095 1096 1097
(defun tramp-gvfs-monitor-file-process-filter (proc string)
  "Read output from \"gvfs-monitor-file\" and add corresponding \
file-notify events."
1098
  (let* ((rest-string (process-get proc 'rest-string))
Michael Albinus's avatar
Michael Albinus committed
1099 1100 1101 1102 1103 1104 1105
	 (dd (with-current-buffer (process-buffer proc) default-directory))
	 (ddu (regexp-quote (tramp-gvfs-url-file-name dd))))
    (when rest-string
      (tramp-message proc 10 "Previous string:\n%s" rest-string))
    (tramp-message proc 6 "%S\n%s" proc string)
    (setq string (concat rest-string string)
	  ;; Attribute change is returned in unused wording.
1106
	  string (replace-regexp-in-string
Michael Albinus's avatar
Michael Albinus committed
1107
		  "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
1108 1109
    (when (string-match "Monitoring not supported" string)
      (delete-process proc))
Michael Albinus's avatar
Michael Albinus committed
1110 1111 1112 1113 1114 1115 1116

    (while (string-match
	    (concat "^[\n\r]*"
		    "File Monitor Event:[\n\r]+"
		    "File = \\([^\n\r]+\\)[\n\r]+"
		    "Event = \\([^[:blank:]]+\\)[\n\r]+")
	    string)
1117 1118
      (let ((file (match-string 1 string))
	    (action (intern-soft
1119
		     (replace-regexp-in-string
1120
		      "_" "-" (downcase (match-string 2 string))))))
Michael Albinus's avatar
Michael Albinus committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	(setq string (replace-match "" nil nil string))
	;; File names are returned as URL paths.  We must convert them.
	(when (string-match ddu file)
	  (setq file (replace-match dd nil nil file)))
	(while (string-match "%\\([0-9A-F]\\{2\\}\\)" file)
	  (setq file
		(replace-match
		 (char-to-string (string-to-number (match-string 1 file) 16))
		 nil nil file)))
	;; Usually, we would add an Emacs event now.  Unfortunately,
	;; `unread-command-events' does not accept several events at
	;; once.  Therefore, we apply the callback directly.
	(tramp-compat-funcall 'file-notify-callback (list proc action file))))

    ;; Save rest of the string.
    (when (zerop (length string)) (setq string nil))
    (when string (tramp-message proc 10 "Rest string:\n%s" string))
1138
    (process-put proc 'rest-string string)))
Michael Albinus's avatar
Michael Albinus committed
1139

1140 1141
(defun tramp-gvfs-handle-file-readable-p (filename)
  "Like `file-readable-p' for Tramp files."
1142
  (with-parsed-tramp-file-name filename nil
1143
    (with-tramp-file-property v localname "file-readable-p"
1144
      (tramp-check-cached-permissions v ?r))))
Michael Albinus's avatar
Michael Albinus committed
1145

1146 1147
(defun tramp-gvfs-handle-file-writable-p (filename)
  "Like `file-writable-p' for Tramp files."
1148 1149 1150 1151 1152 1153 1154
  (with-parsed-tramp-file-name filename nil
    (with-tramp-file-property v localname "file-writable-p"
      (if (file-exists-p filename)
	  (tramp-check-cached-permissions v ?w)
	;; If file doesn't exist, check if directory is writable.
	(and (file-directory-p (file-name-directory filename))
	     (file-writable-p (file-name-directory filename)))))))
1155 1156 1157

(defun tramp-gvfs-handle-make-directory (dir &optional parents)
  "Like `make-directory' for Tramp files."
Michael Albinus's avatar
Michael Albinus committed
1158
  (setq dir (directory-file-name (expand-file-name dir)))
1159
  (with-parsed-tramp-file-name dir nil
Michael Albinus's avatar
Michael Albinus committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    (tramp-flush-file-property v (file-name-directory localname))
    (tramp-flush-directory-property v localname)
    (save-match-data
      (let ((ldir (file-name-directory dir)))
	;; Make missing directory parts.  "gvfs-mkdir -p ..." does not
	;; work robust.
	(when (and parents (not (file-directory-p ldir)))
	  (make-directory ldir parents))
	;; Just do it.
	(unless (tramp-gvfs-send-command
		 v "gvfs-mkdir" (tramp-gvfs-url-file-name dir))
1171
	  (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1172

1173 1174 1175
(defun tramp-gvfs-handle-rename-file
  (filename newname &optional ok-if-already-exists)
  "Like `rename-file' for Tramp files."
Michael Albinus's avatar
Michael Albinus committed
1176 1177 1178 1179 1180 1181 1182 1183
  ;; Check if both files are local -- invoke normal rename-file.
  ;; Otherwise, use Tramp from local system.
  (setq filename (expand-file-name filename))
  (setq newname (expand-file-name newname))
  ;; 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
1184 1185
       'rename filename newname ok-if-already-exists
       'keep-date 'preserve-uid-gid)
Michael Albinus's avatar
Michael Albinus committed
1186 1187
    (tramp-run-real-handler
     'rename-file (list filename newname ok-if-already-exists))))
Michael Albinus's avatar
Michael Albinus committed
1188

1189
(defun tramp-gvfs-handle-write-region
1190
  (start end filename &optional append visit lockname confirm)
1191 1192
  "Like `write-region' for Tramp files."
  (with-parsed-tramp-file-name filename nil
1193
    (when (and confirm (file-exists-p filename))
1194 1195 1196 1197
      (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
	(tramp-error v 'file-error "File not overwritten")))

    (let ((tmpfile (tramp-compat-make-temp-file filename)))
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
      (when (and append (file-exists-p filename))
	(copy-file filename tmpfile 'ok))
      ;; We say `no-message' here because we don't want the visited file
      ;; modtime data to be clobbered from the temp file.  We call
      ;; `set-visited-file-modtime' ourselves later on.
      (tramp-run-real-handler
       'write-region
       (if confirm ; don't pass this arg unless defined for backward compat.
	   (list start end tmpfile append 'no-message lockname confirm)
	 (list start end tmpfile append 'no-message lockname)))
1208
      (condition-case nil
Michael Albinus's avatar
Michael Albinus committed
1209
	  (rename-file tmpfile filename 'ok-if-already-exists)
1210 1211 1212
	(error
	 (delete-file tmpfile)
	 (tramp-error
1213
	  v 'file-error "Couldn't write region to `%s'" filename))))