File: remote.jl

package info (click to toggle)
librep 0.9-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,576 kB
  • ctags: 1,928
  • sloc: ansic: 21,612; sh: 7,386; lisp: 5,331; makefile: 392; sed: 93
file content (122 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;;;; remote.jl -- Remote file access
;;;  Copyright (C) 1998 John Harper <john@dcs.warwick.ac.uk>
;;;  $Id: remote.jl,v 1.8 1999/07/06 13:21:22 john Exp $

;;; This file is part of Jade.

;;; Jade 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 2, or (at your option)
;;; any later version.

;;; Jade 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 Jade; see the file COPYING.  If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'remote)


;; Configuration

;; A symbol defines a backend type if its `remote-backend' property
;; is a function to call as (FUNCTION SPLIT-NAME-OR-NIL OP ARG-LIST)

(defvar remote-auto-backend-alist nil
  "An alist of (HOST-REGEXP . BACKEND-TYPE) defining how remote files are
accessed on specific hosts.")

(defvar remote-default-backend 'ftp
  "Backend used for otherwise unspecified hosts.")

(defvar remote-host-user-alist nil
  "Alist of (HOST-REGEXP . USER-NAME) matching host names to usernames.
Only used when no username is given in a filename.")

(defvar remote-default-user (user-login-name)
  "Default username to use for file-transfer when none is specified, either
explicitly, or by the remote-ftp-host-user-alist variable.")

;; Remote filename syntax
(defvar remote-file-regexp "^/(([a-zA-Z0-9._-]+)@)?([a-zA-Z0-9._-]+):")


;; Entry point

;;;###autoload
(defun remote-file-handler (op &rest args)
  (cond
   ((filep (car args))
    ;; A previously opened file handle. The backend should have stashed
    ;; it's handler function in the first slot the file's handler-data
    ;; (a vector)
    (let
	((split (remote-split-filename (file-binding (car args)))))
      (funcall (aref (file-handler-data (car args)) 0) split op args)))
   ((eq op 'file-name-absolute-p))	;remote files are absolute?
   ((eq op 'local-file-name)
    ;; can't get a local file name
    nil)
   (t
    (let
	;; Chop up the file name
	((split (remote-split-filename (if (eq op 'copy-file-from-local-fs)
					   ;; remote file is 2nd arg
					   (nth 1 args)
					 (car args)))))
      (cond
       ;; Handle all file name manipulations
       ;; XXX This isn't such a good idea since it presumes that remote
       ;; XXX systems use the same file naming conventions as locally.
       ((eq op 'expand-file-name)
	(remote-join-filename (car split) (nth 1 split)
			      (expand-file-name (nth 2 split) ".")))
       ((eq op 'file-name-nondirectory)
	(file-name-nondirectory (nth 2 split)))
       ((eq op 'file-name-directory)
	(remote-join-filename (car split) (nth 1 split)
			      (file-name-directory (nth 2 split))))
       ((eq op 'file-name-as-directory)
	(remote-join-filename (car split) (nth 1 split)
			      (if (string= (nth 2 split) "")
				  ""
				(file-name-as-directory (nth 2 split)))))
       ((eq op 'directory-file-name)
	(remote-join-filename (car split) (nth 1 split)
			      (directory-file-name (nth 2 split))))
       (t
	;; Anything else, pass off to a backend
	(let
	    ((backend (get (or (cdr (assoc-regexp (nth 1 split)
						  remote-auto-backend-alist t))
			       remote-default-backend)
			   'remote-backend)))
	  (funcall backend split op args))))))))

(defun remote-get-user (host)
  (or (cdr (assoc-regexp host remote-host-user-alist)) remote-default-user))

;; Return (USER-OR-NIL HOST FILE)
(defun remote-split-filename (filename)
  (unless (string-match remote-file-regexp filename)
    (error "Malformed remote file specification: %s" filename))
  (let
      ((host (substring filename (match-start 3) (match-end 3)))
       (file (substring filename (match-end))))
    (list
     (and (match-start 2)
	  (substring filename (match-start 2) (match-end 2)))
     host file)))

;; Create a remote file name. USER may be nil
(defun remote-join-filename (user host file)
  (concat ?/ (and user (concat user ?@)) host ?: file))


;; Initialise handler

;;;###autoload (setq file-handler-alist (cons '("^/(([a-zA-Z0-9._-]+)@)?([a-zA-Z0-9._-]+):" . remote-file-handler) file-handler-alist))