tramp-rclone.el 24.3 KB
Newer Older
Michael Albinus's avatar
Michael Albinus committed
1 2
;;; tramp-rclone.el --- Tramp access functions to cloud storages  -*- lexical-binding:t -*-

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2018-2019 Free Software Foundation, Inc.
Michael Albinus's avatar
Michael Albinus committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

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

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; 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.

;; 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.

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

;;; Commentary:

;; rclone is a command line program to sync files and directories to
;; and from cloud storages.  Tramp uses its mount utility to access
;; files and directories there.  The configuration of rclone for
;; different storage systems is performed outside Tramp, see rclone(1).

;; A remote file under rclone control has the form
;; "/rclone:<remote>:/path/to/file".  <remote> is the name of a
;; storage system in rclone's configuration.  Therefore, such a remote
34
;; file name does not know of any user or port specification.
Michael Albinus's avatar
Michael Albinus committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

;;; Code:

(eval-when-compile (require 'cl-lib))
(require 'tramp)

;;;###tramp-autoload
(defconst tramp-rclone-method "rclone"
  "When this method name is used, forward all calls to rclone mounts.")

(defcustom tramp-rclone-program "rclone"
  "Name of the rclone program."
  :group 'tramp
  :version "27.1"
  :type 'string)

;;;###tramp-autoload
52 53 54 55 56 57 58 59
(tramp--with-startup
 (add-to-list
  'tramp-methods
  `(,tramp-rclone-method
    (tramp-mount-args nil)
    (tramp-copyto-args nil)
    (tramp-moveto-args nil)
    (tramp-about-args ("--full")))))
Michael Albinus's avatar
Michael Albinus committed
60 61

;;;###tramp-autoload
62 63 64
(tramp--with-startup
 (tramp-set-completion-function
  tramp-rclone-method '((tramp-rclone-parse-device-names ""))))
Michael Albinus's avatar
Michael Albinus committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


;; New handlers should be added here.
;;;###tramp-autoload
(defconst tramp-rclone-file-name-handler-alist
  '((access-file . ignore)
    (add-name-to-file . tramp-handle-add-name-to-file)
    ;; `byte-compiler-base-file-name' performed by default handler.
    ;; `copy-directory' performed by default handler.
    (copy-file . tramp-rclone-handle-copy-file)
    (delete-directory . tramp-rclone-handle-delete-directory)
    (delete-file . tramp-rclone-handle-delete-file)
    ;; `diff-latest-backup-file' performed by default handler.
    (directory-file-name . tramp-handle-directory-file-name)
    (directory-files . tramp-rclone-handle-directory-files)
    (directory-files-and-attributes
     . tramp-handle-directory-files-and-attributes)
    (dired-compress-file . ignore)
    (dired-uncache . tramp-handle-dired-uncache)
    (exec-path . ignore)
85
    (expand-file-name . tramp-handle-expand-file-name)
Michael Albinus's avatar
Michael Albinus committed
86 87 88 89 90 91 92 93
    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
    (file-acl . ignore)
    (file-attributes . tramp-rclone-handle-file-attributes)
    (file-directory-p . tramp-handle-file-directory-p)
    (file-equal-p . tramp-handle-file-equal-p)
    (file-executable-p . tramp-rclone-handle-file-executable-p)
    (file-exists-p . tramp-handle-file-exists-p)
    (file-in-directory-p . tramp-handle-file-in-directory-p)
94
    (file-local-copy . tramp-handle-file-local-copy)
Michael Albinus's avatar
Michael Albinus committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    (file-modes . tramp-handle-file-modes)
    (file-name-all-completions . tramp-rclone-handle-file-name-all-completions)
    (file-name-as-directory . tramp-handle-file-name-as-directory)
    (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
    (file-name-completion . tramp-handle-file-name-completion)
    (file-name-directory . tramp-handle-file-name-directory)
    (file-name-nondirectory . tramp-handle-file-name-nondirectory)
    ;; `file-name-sans-versions' performed by default handler.
    (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
    (file-notify-add-watch . ignore)
    (file-notify-rm-watch . ignore)
    (file-notify-valid-p . ignore)
    (file-ownership-preserved-p . ignore)
    (file-readable-p . tramp-rclone-handle-file-readable-p)
    (file-regular-p . tramp-handle-file-regular-p)
    (file-remote-p . tramp-handle-file-remote-p)
    (file-selinux-context . tramp-handle-file-selinux-context)
    (file-symlink-p . tramp-handle-file-symlink-p)
    (file-system-info . tramp-rclone-handle-file-system-info)
    (file-truename . tramp-handle-file-truename)
115
    (file-writable-p . tramp-handle-file-writable-p)
Michael Albinus's avatar
Michael Albinus committed
116 117 118 119 120 121 122 123 124
    (find-backup-file-name . tramp-handle-find-backup-file-name)
    ;; `get-file-buffer' performed by default handler.
    (insert-directory . tramp-handle-insert-directory)
    (insert-file-contents . tramp-handle-insert-file-contents)
    (load . tramp-handle-load)
    (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
    (make-directory . tramp-rclone-handle-make-directory)
    (make-directory-internal . ignore)
    (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
125
    (make-process . ignore)
Michael Albinus's avatar
Michael Albinus committed
126 127 128 129 130 131 132 133 134 135 136 137
    (make-symbolic-link . tramp-handle-make-symbolic-link)
    (process-file . ignore)
    (rename-file . tramp-rclone-handle-rename-file)
    (set-file-acl . ignore)
    (set-file-modes . ignore)
    (set-file-selinux-context . ignore)
    (set-file-times . ignore)
    (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
    (shell-command . ignore)
    (start-file-process . ignore)
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
    (temporary-file-directory . tramp-handle-temporary-file-directory)
Michael Albinus's avatar
Michael Albinus committed
138
    (tramp-set-file-uid-gid . ignore)
Michael Albinus's avatar
Michael Albinus committed
139 140 141
    (unhandled-file-name-directory . ignore)
    (vc-registered . ignore)
    (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
142
    (write-region . tramp-handle-write-region))
Michael Albinus's avatar
Michael Albinus committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  "Alist of handler functions for Tramp RCLONE method.
Operations not mentioned here will be handled by the default Emacs primitives.")

;; It must be a `defsubst' in order to push the whole code into
;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
;;;###tramp-autoload
(defsubst tramp-rclone-file-name-p (filename)
  "Check if it's a filename for rclone."
  (and (tramp-tramp-file-p filename)
       (string= (tramp-file-name-method (tramp-dissect-file-name filename))
		tramp-rclone-method)))

;;;###tramp-autoload
(defun tramp-rclone-file-name-handler (operation &rest args)
  "Invoke the rclone handler for OPERATION.
First arg specifies the OPERATION, second arg is a list of arguments to
pass to the OPERATION."
  (let ((fn (assoc operation tramp-rclone-file-name-handler-alist)))
    (if fn
	(save-match-data (apply (cdr fn) args))
      (tramp-run-real-handler operation args))))

;;;###tramp-autoload
166 167 168
(tramp--with-startup
 (tramp-register-foreign-file-name-handler
  #'tramp-rclone-file-name-p #'tramp-rclone-file-name-handler))
Michael Albinus's avatar
Michael Albinus committed
169 170 171 172

;;;###tramp-autoload
(defun tramp-rclone-parse-device-names (_ignore)
  "Return a list of (nil host) tuples allowed to access."
Michael Albinus's avatar
Michael Albinus committed
173 174 175 176 177 178 179 180 181 182 183 184 185
  (with-tramp-connection-property nil "rclone-device-names"
    (with-timeout (10)
      (with-temp-buffer
	;; `call-process' does not react on timer under MS Windows.
	;; That's why we use `start-process'.
	(let ((p (start-process
		  tramp-rclone-program (current-buffer)
		  tramp-rclone-program "listremotes"))
	      (v (make-tramp-file-name :method tramp-rclone-method))
	      result)
	  (tramp-message v 6 "%s" (mapconcat 'identity (process-command p) " "))
	  (process-put p 'adjust-window-size-function 'ignore)
	  (set-process-query-on-exit-flag p nil)
186
	  (while (accept-process-output p nil nil t))
Michael Albinus's avatar
Michael Albinus committed
187 188 189 190 191
	  (tramp-message v 6 "\n%s" (buffer-string))
	  (goto-char (point-min))
	  (while (search-forward-regexp "^\\(\\S-+\\):$" nil t)
	    (push (list nil (match-string 1)) result))
	  result)))))
Michael Albinus's avatar
Michael Albinus committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258


;; File name primitives.

(defun tramp-rclone-do-copy-or-rename-file
  (op filename newname &optional ok-if-already-exists keep-date
   preserve-uid-gid preserve-extended-attributes)
  "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-rclone-handle-copy-file' and
`tramp-rclone-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))
    (error "Unknown operation `%s', must be `copy' or `rename'" op))

  (setq filename (file-truename filename))
  (if (file-directory-p filename)
      (progn
	(copy-directory filename newname keep-date t)
	(when (eq op 'rename) (delete-directory filename 'recursive)))

    (let ((t1 (tramp-tramp-file-p filename))
	  (t2 (tramp-tramp-file-p newname))
	  (rclone-operation (if (eq op 'copy) "copyto" "moveto"))
	  (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 newname))

	(if (or (and t1 (not (tramp-rclone-file-name-p filename)))
		(and t2 (not (tramp-rclone-file-name-p newname))))

	    ;; We cannot copy or rename directly.
	    (let ((tmpfile (tramp-compat-make-temp-file filename)))
	      (if (eq op 'copy)
		  (copy-file
		   filename tmpfile t keep-date preserve-uid-gid
		   preserve-extended-attributes)
		(rename-file filename tmpfile t))
	      (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 (zerop
		     (tramp-rclone-send-command
		      v rclone-operation
		      (tramp-rclone-remote-file-name filename)
		      (tramp-rclone-remote-file-name newname)))
	      (tramp-error
	       v 'file-error
	       "Error %s `%s' `%s'" msg-operation filename newname)))

	  (when (and t1 (eq op 'rename))
	    (with-parsed-tramp-file-name filename v1
	      (tramp-flush-file-properties
	       v1 (file-name-directory v1-localname))
259 260 261 262 263 264 265 266 267
	      (tramp-flush-file-properties v1 v1-localname)
	      (when (tramp-rclone-file-name-p filename)
		(tramp-rclone-flush-directory-cache v1)
		;; The mount point's directory cache might need time
		;; to flush.
		(while (file-exists-p filename)
		  (tramp-flush-file-properties
		   v1 (file-name-directory v1-localname))
		  (tramp-flush-file-properties v1 v1-localname)))))
Michael Albinus's avatar
Michael Albinus committed
268 269 270 271 272 273 274

	  (when t2
	    (with-parsed-tramp-file-name newname v2
	      (tramp-flush-file-properties
	       v2 (file-name-directory v2-localname))
	      (tramp-flush-file-properties v2 v2-localname)
	      (when (tramp-rclone-file-name-p newname)
275 276 277 278 279 280 281
		(tramp-rclone-flush-directory-cache v2)
		;; The mount point's directory cache might need time
		;; to flush.
		(while (not (file-exists-p newname))
		  (tramp-flush-file-properties
		   v2 (file-name-directory v2-localname))
		  (tramp-flush-file-properties v2 v2-localname))))))))))
Michael Albinus's avatar
Michael Albinus committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

(defun tramp-rclone-handle-copy-file
  (filename newname &optional ok-if-already-exists keep-date
   preserve-uid-gid preserve-extended-attributes)
  "Like `copy-file' for Tramp files."
  (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-rclone-do-copy-or-rename-file
       'copy filename newname ok-if-already-exists keep-date
       preserve-uid-gid preserve-extended-attributes)
    (tramp-run-real-handler
     'copy-file
     (list filename newname ok-if-already-exists keep-date
	   preserve-uid-gid preserve-extended-attributes))))

(defun tramp-rclone-handle-delete-directory
    (directory &optional recursive trash)
  "Like `delete-directory' for Tramp files."
  (with-parsed-tramp-file-name (expand-file-name directory) nil
304
    (delete-directory (tramp-rclone-local-file-name directory) recursive trash)
Michael Albinus's avatar
Michael Albinus committed
305 306
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-directory-properties v localname)
307
    (tramp-rclone-flush-directory-cache v)))
Michael Albinus's avatar
Michael Albinus committed
308 309 310 311

(defun tramp-rclone-handle-delete-file (filename &optional trash)
  "Like `delete-file' for Tramp files."
  (with-parsed-tramp-file-name (expand-file-name filename) nil
312
    (delete-file (tramp-rclone-local-file-name filename) trash)
Michael Albinus's avatar
Michael Albinus committed
313 314
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-file-properties v localname)
315
    (tramp-rclone-flush-directory-cache v)))
Michael Albinus's avatar
Michael Albinus committed
316 317 318 319 320 321 322 323 324 325 326 327

(defun tramp-rclone-handle-directory-files
    (directory &optional full match nosort)
  "Like `directory-files' for Tramp files."
  (when (file-directory-p directory)
    (setq directory (file-name-as-directory (expand-file-name directory)))
    (with-parsed-tramp-file-name directory nil
      (let ((result
	     (directory-files
	      (tramp-rclone-local-file-name directory) full match)))
	;; Massage the result.
	(when full
328 329 330 331
	  (let ((local (concat "^" (regexp-quote (tramp-rclone-mount-point v))))
		(remote (funcall (if (tramp-compat-file-name-quoted-p directory)
				     'tramp-compat-file-name-quote 'identity)
				 (file-remote-p directory))))
Michael Albinus's avatar
Michael Albinus committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	    (setq result
		  (mapcar
		   (lambda (x) (replace-regexp-in-string local remote x))
		   result))))
	;; Some storage systems do not return "." and "..".
	(dolist (item '(".." "."))
	  (when (and (string-match-p (or match (regexp-quote item)) item)
		     (not
		      (member (if full (setq item (concat directory item)) item)
			      result)))
	    (setq result (cons item result))))
	;; Return result.
	(if nosort result (sort result 'string<))))))

(defun tramp-rclone-handle-file-attributes (filename &optional id-format)
  "Like `file-attributes' for Tramp files."
  (with-parsed-tramp-file-name (expand-file-name filename) nil
    (with-tramp-file-property
	v localname (format "file-attributes-%s" id-format)
      (file-attributes (tramp-rclone-local-file-name filename) id-format))))

(defun tramp-rclone-handle-file-executable-p (filename)
  "Like `file-executable-p' for Tramp files."
355 356 357
  (with-parsed-tramp-file-name (expand-file-name filename) nil
    (with-tramp-file-property v localname "file-executable-p"
      (file-executable-p (tramp-rclone-local-file-name filename)))))
Michael Albinus's avatar
Michael Albinus committed
358 359 360

(defun tramp-rclone-handle-file-name-all-completions (filename directory)
  "Like `file-name-all-completions' for Tramp files."
361 362 363 364 365 366 367 368 369 370 371 372 373 374
  (all-completions
   filename
   (delete-dups
    (append
     (file-name-all-completions
      filename (tramp-rclone-local-file-name directory))
     ;; Some storage systems do not return "." and "..".
     (let (result)
       (dolist (item '(".." ".") result)
	 (when (string-prefix-p filename item)
	   (catch 'match
	     (dolist (elt completion-regexp-list)
	       (unless (string-match-p elt item) (throw 'match nil)))
	     (setq result (cons (concat item "/") result))))))))))
Michael Albinus's avatar
Michael Albinus committed
375 376 377

(defun tramp-rclone-handle-file-readable-p (filename)
  "Like `file-readable-p' for Tramp files."
378 379 380
  (with-parsed-tramp-file-name (expand-file-name filename) nil
    (with-tramp-file-property v localname "file-readable-p"
      (file-readable-p (tramp-rclone-local-file-name filename)))))
Michael Albinus's avatar
Michael Albinus committed
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

(defun tramp-rclone-handle-file-system-info (filename)
  "Like `file-system-info' for Tramp files."
  (ignore-errors
    (unless (file-directory-p filename)
      (setq filename (file-name-directory filename)))
    (with-parsed-tramp-file-name (expand-file-name filename) nil
      (tramp-message v 5 "file system info: %s" localname)
      (tramp-rclone-send-command v "about" (concat host ":"))
      (with-current-buffer (tramp-get-connection-buffer v)
	(let (total used free)
	  (goto-char (point-min))
	  (while (not (eobp))
	    (when (looking-at "Total: [[:space:]]+\\([[:digit:]]+\\)")
	      (setq total (string-to-number (match-string 1))))
	    (when (looking-at "Used: [[:space:]]+\\([[:digit:]]+\\)")
	      (setq used (string-to-number (match-string 1))))
	    (when (looking-at "Free: [[:space:]]+\\([[:digit:]]+\\)")
	      (setq free (string-to-number (match-string 1))))
	    (forward-line))
	  (when used
	    ;; The used number of bytes is not part of the result.  As
	    ;; side effect, we store it as file property.
	    (tramp-set-file-property v localname "used-bytes" used))
	  ;; Result.
	  (when (and total free)
	    (list total free (- total free))))))))

(defun tramp-rclone-handle-insert-directory
  (filename switches &optional wildcard full-directory-p)
  "Like `insert-directory' for Tramp files."
  (insert-directory
   (tramp-rclone-local-file-name filename) switches wildcard full-directory-p)
  (goto-char (point-min))
  (while (search-forward (tramp-rclone-local-file-name filename) nil 'noerror)
    (replace-match filename)))

(defun tramp-rclone-handle-insert-file-contents
  (filename &optional visit beg end replace)
  "Like `insert-file-contents' for Tramp files."
  (let ((result
	 (insert-file-contents
	  (tramp-rclone-local-file-name filename) visit beg end replace)))
    (prog1
425
	(list (expand-file-name filename) (cadr result))
Michael Albinus's avatar
Michael Albinus committed
426 427 428 429 430
      (when visit (setq buffer-file-name filename)))))

(defun tramp-rclone-handle-make-directory (dir &optional parents)
  "Like `make-directory' for Tramp files."
  (with-parsed-tramp-file-name (expand-file-name dir) nil
431
    (make-directory (tramp-rclone-local-file-name dir) parents)
Michael Albinus's avatar
Michael Albinus committed
432 433
    ;; When PARENTS is non-nil, DIR could be a chain of non-existent
    ;; directories a/b/c/...  Instead of checking, we simply flush the
434
    ;; whole file cache.
Michael Albinus's avatar
Michael Albinus committed
435 436 437
    (tramp-flush-file-properties v localname)
    (tramp-flush-directory-properties
     v (if parents "/" (file-name-directory localname)))
438
    (tramp-rclone-flush-directory-cache v)))
Michael Albinus's avatar
Michael Albinus committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

(defun tramp-rclone-handle-rename-file
  (filename newname &optional ok-if-already-exists)
  "Like `rename-file' for Tramp files."
  (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-rclone-do-copy-or-rename-file
       'rename filename newname ok-if-already-exists
       'keep-date 'preserve-uid-gid)
    (tramp-run-real-handler
     'rename-file (list filename newname ok-if-already-exists))))


;; File name conversions.

(defun tramp-rclone-mount-point (vec)
  "Return local mount point of VEC."
  (expand-file-name
   (concat
    tramp-temp-name-prefix (tramp-file-name-method vec)
462
    "." (tramp-file-name-host vec))
Michael Albinus's avatar
Michael Albinus committed
463 464 465 466
   (tramp-compat-temporary-file-directory)))

(defun tramp-rclone-mounted-p (vec)
  "Check, whether storage system determined by VEC is mounted."
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  (when (tramp-get-connection-process vec)
    ;; We cannot use `with-connection-property', because we don't want
    ;; to cache a nil result.
    (or (tramp-get-connection-property
	 (tramp-get-connection-process vec) "mounted" nil)
	(tramp-set-connection-property
	 (tramp-get-connection-process vec) "mounted"
	 (let* ((default-directory temporary-file-directory)
		(mount (shell-command-to-string "mount -t fuse.rclone")))
	   (tramp-message vec 6 "%s" "mount -t fuse.rclone")
	   (tramp-message vec 6 "\n%s" mount)
	   (when (string-match
		  (format
		   "^\\(%s:\\S-*\\)" (regexp-quote (tramp-file-name-host vec)))
		  mount)
	     (match-string 1 mount)))))))

(defun tramp-rclone-flush-directory-cache (vec)
Michael Albinus's avatar
Michael Albinus committed
485 486 487
  "Flush directory cache of VEC mount."
  (let ((rclone-pid
	 ;; Identify rclone process.
488 489 490 491 492 493 494 495 496 497 498
	 (when (tramp-get-connection-process vec)
	   (with-tramp-connection-property
	       (tramp-get-connection-process vec) "rclone-pid"
	     (catch 'pid
	       (dolist (pid (list-system-processes)) ;; "pidof rclone" ?
		 (and (string-match-p
		       (regexp-quote
			(format "rclone mount %s:" (tramp-file-name-host vec)))
		       (or (cdr (assoc 'args (process-attributes pid))) ""))
		      (throw 'pid pid))))))))
    ;; Send a SIGHUP in order to flush directory cache.
Michael Albinus's avatar
Michael Albinus committed
499 500 501 502 503 504 505 506
    (when rclone-pid
      (tramp-message
       vec 6 "Send SIGHUP %d: %s"
       rclone-pid (cdr (assoc 'args (process-attributes rclone-pid))))
      (signal-process rclone-pid 'SIGHUP))))

(defun tramp-rclone-local-file-name (filename)
  "Return local mount name of FILENAME."
507 508
  (setq filename (tramp-compat-file-name-unquote (expand-file-name filename)))
  (with-parsed-tramp-file-name filename nil
Michael Albinus's avatar
Michael Albinus committed
509 510 511 512
    ;; As long as we call `tramp-rclone-maybe-open-connection' here,
    ;; we cache the result.
    (with-tramp-file-property v localname "local-file-name"
      (tramp-rclone-maybe-open-connection v)
513 514
      (let ((quoted (tramp-compat-file-name-quoted-p localname))
	    (localname (tramp-compat-file-name-unquote localname)))
Michael Albinus's avatar
Michael Albinus committed
515
	(funcall
516
	 (if quoted 'tramp-compat-file-name-quote 'identity)
Michael Albinus's avatar
Michael Albinus committed
517 518 519 520 521 522 523
	 (expand-file-name
	  (if (file-name-absolute-p localname)
	      (substring localname 1) localname)
	  (tramp-rclone-mount-point v)))))))

(defun tramp-rclone-remote-file-name (filename)
  "Return FILENAME as used in the `rclone' command."
524
  (setq filename (tramp-compat-file-name-unquote (expand-file-name filename)))
Michael Albinus's avatar
Michael Albinus committed
525 526
  (if (tramp-rclone-file-name-p filename)
      (with-parsed-tramp-file-name filename nil
527 528 529 530 531 532 533 534 535
	;; As long as we call `tramp-rclone-maybe-open-connection' here,
	;; we cache the result.
	(with-tramp-file-property v localname "remote-file-name"
	  (tramp-rclone-maybe-open-connection v)
	  ;; TODO: This shall be handled by `expand-file-name'.
	  (setq localname
		(replace-regexp-in-string "^\\." "" (or localname "")))
	  (format "%s%s" (tramp-rclone-mounted-p v) localname)))
    ;; It is a local file name.
Michael Albinus's avatar
Michael Albinus committed
536 537 538 539 540 541
    filename))

(defun tramp-rclone-maybe-open-connection (vec)
  "Maybe open a connection VEC.
Does not do anything if a connection is already open, but re-opens the
connection if a previous connection has died for some reason."
542 543
  (let ((host (tramp-file-name-host vec)))
    (when (rassoc `(,host) (tramp-rclone-parse-device-names nil))
Michael Albinus's avatar
Michael Albinus committed
544 545
      (if (zerop (length host))
	  (tramp-error vec 'file-error "Storage %s not connected" host))
546 547 548 549 550 551 552 553 554

      ;; We need a process bound to the connection buffer.  Therefore,
      ;; we create a dummy process.  Maybe there is a better solution?
      (unless (get-buffer-process (tramp-get-connection-buffer vec))
	(let ((p (make-network-process
		  :name (tramp-buffer-name vec)
		  :buffer (tramp-get-connection-buffer vec)
		  :server t :host 'local :service t :noquery t)))
	  (process-put p 'vector vec)
Michael Albinus's avatar
Michael Albinus committed
555 556 557
	  (set-process-query-on-exit-flag p nil)

	  ;; Set connection-local variables.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	  (tramp-set-connection-local-variables vec)))

      ;; Create directory.
      (unless (file-directory-p (tramp-rclone-mount-point vec))
	(make-directory (tramp-rclone-mount-point vec) 'parents))

      ;; Mount.  This command does not return, so we use 0 as
      ;; DESTINATION of `tramp-call-process'.
      (unless (tramp-rclone-mounted-p vec)
	(apply
	 'tramp-call-process
	 vec tramp-rclone-program nil 0 nil
	 (delq nil
	       `("mount" ,(concat host ":/")
		 ,(tramp-rclone-mount-point vec)
		 ;; This could be nil.
		 ,(tramp-get-method-parameter vec 'tramp-mount-args))))
	(while (not (file-exists-p (tramp-make-tramp-file-name vec 'localname)))
	  (tramp-cleanup-connection vec 'keep-debug 'keep-password)))))
Michael Albinus's avatar
Michael Albinus committed
577 578

  ;; In `tramp-check-cached-permissions', the connection properties
Michael Albinus's avatar
Michael Albinus committed
579
  ;; "{uid,gid}-{integer,string}" are used.  We set them to proper values.
Michael Albinus's avatar
Michael Albinus committed
580 581 582 583 584 585 586 587
  (with-tramp-connection-property
      vec "uid-integer" (tramp-get-local-uid 'integer))
  (with-tramp-connection-property
      vec "gid-integer" (tramp-get-local-gid 'integer))
  (with-tramp-connection-property
      vec "uid-string" (tramp-get-local-uid 'string))
  (with-tramp-connection-property
      vec "gid-string" (tramp-get-local-gid 'string)))
Michael Albinus's avatar
Michael Albinus committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

(defun tramp-rclone-send-command (vec &rest args)
  "Send the COMMAND to connection VEC."
  (with-current-buffer (tramp-get-connection-buffer vec)
    (erase-buffer)
    (let ((flags (tramp-get-method-parameter
		  vec (intern (format "tramp-%s-args" (car args))))))
      (apply 'tramp-call-process
	     vec tramp-rclone-program nil t nil (append args flags)))))

(add-hook 'tramp-unload-hook
	  (lambda ()
	    (unload-feature 'tramp-rclone 'force)))

(provide 'tramp-rclone)

;;; TODO:

606
;; * If possible, get rid of "rclone mount".  Maybe it is more
Michael Albinus's avatar
Michael Albinus committed
607 608 609
;;   performant then.

;;; tramp-rclone.el ends here