1 ;;; tramp-smb.el --- Tramp access functions for SMB servers -*- coding: iso-8859-1; -*-
3 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <Michael.Albinus@alcatel.de>
6 ;; Keywords: comm, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
33 ;; Pacify byte-compiler
37 ;; Emacs 19.34 compatibility hack -- is this needed?
38 (or (>= emacs-major-version
20)
41 ;; Define SMB method ...
42 (defcustom tramp-smb-method
"smb"
43 "*Method to connect SAMBA and M$ SMB servers."
47 ;; ... and add it to the method list.
48 (add-to-list 'tramp-methods
(cons tramp-smb-method nil
))
50 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
51 ;; a domain in USER, it must be the SMB method.
52 (add-to-list 'tramp-default-method-alist
53 (list "%" "" tramp-smb-method
))
55 ;; Add completion function for SMB method.
56 (tramp-set-completion-function
58 '((tramp-parse-netrc "~/.netrc")))
60 (defcustom tramp-smb-program
"smbclient"
61 "*Name of SMB client to run."
65 (defconst tramp-smb-prompt
"^smb: \\S-+> "
66 "Regexp used as prompt in smbclient.")
68 (defconst tramp-smb-errors
72 "Connection to \\S-+ failed"
82 ; Windows NT 4.0, Windows 5.0 (Windows 2000), Windows 5.1 (Windows XP)
83 "NT_STATUS_ACCESS_DENIED"
84 "NT_STATUS_BAD_NETWORK_NAME"
85 "NT_STATUS_CANNOT_DELETE"
86 "NT_STATUS_LOGON_FAILURE"
87 "NT_STATUS_NO_SUCH_FILE"
88 "NT_STATUS_OBJECT_NAME_INVALID"
89 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
90 "NT_STATUS_SHARING_VIOLATION")
92 "Regexp for possible error strings of SMB servers.
93 Used instead of analyzing error codes of commands.")
95 (defvar tramp-smb-share nil
96 "Holds the share name for the current buffer.
97 This variable is local to each buffer.")
98 (make-variable-buffer-local 'tramp-smb-share
)
100 (defvar tramp-smb-share-cache nil
101 "Caches the share names accessible to host related to the current buffer.
102 This variable is local to each buffer.")
103 (make-variable-buffer-local 'tramp-smb-share-cache
)
105 (defvar tramp-smb-process-running nil
106 "Flag whether a corresponding process is still running.
107 Will be changed by corresponding `process-sentinel'.
108 This variable is local to each buffer.")
109 (make-variable-buffer-local 'tramp-smb-process-running
)
111 (defvar tramp-smb-inodes nil
112 "Keeps virtual inodes numbers for SMB files.")
114 ;; New handlers should be added here.
115 (defconst tramp-smb-file-name-handler-alist
117 ;; `access-file' performed by default handler
118 (add-name-to-file . tramp-smb-handle-copy-file
) ;; we're on Windows, honey.
119 ;; `byte-compiler-base-file-name' performed by default handler
120 (copy-file . tramp-smb-handle-copy-file
)
121 (delete-directory . tramp-smb-handle-delete-directory
)
122 (delete-file . tramp-smb-handle-delete-file
)
123 ;; `diff-latest-backup-file' performed by default handler
124 (directory-file-name . tramp-handle-directory-file-name
)
125 (directory-files . tramp-smb-handle-directory-files
)
126 (directory-files-and-attributes . tramp-smb-handle-directory-files-and-attributes
)
127 (dired-call-process . tramp-smb-not-handled
)
128 (dired-compress-file . tramp-smb-not-handled
)
129 ;; `dired-uncache' performed by default handler
130 ;; `expand-file-name' not necessary because we cannot expand "~/"
131 (file-accessible-directory-p . tramp-smb-handle-file-directory-p
)
132 (file-attributes . tramp-smb-handle-file-attributes
)
133 (file-directory-p . tramp-smb-handle-file-directory-p
)
134 (file-executable-p . tramp-smb-handle-file-exists-p
)
135 (file-exists-p . tramp-smb-handle-file-exists-p
)
136 (file-local-copy . tramp-smb-handle-file-local-copy
)
137 (file-modes . tramp-handle-file-modes
)
138 (file-name-all-completions . tramp-smb-handle-file-name-all-completions
)
139 ;; `file-name-as-directory' performed by default handler
140 (file-name-completion . tramp-handle-file-name-completion
)
141 (file-name-directory . tramp-handle-file-name-directory
)
142 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
143 ;; `file-name-sans-versions' performed by default handler
144 (file-newer-than-file-p . tramp-smb-handle-file-newer-than-file-p
)
145 (file-ownership-preserved-p . tramp-smb-not-handled
)
146 (file-readable-p . tramp-smb-handle-file-exists-p
)
147 (file-regular-p . tramp-handle-file-regular-p
)
148 (file-symlink-p . tramp-smb-not-handled
)
149 ;; `file-truename' performed by default handler
150 (file-writable-p . tramp-smb-handle-file-writable-p
)
151 ;; `find-backup-file-name' performed by default handler
152 ;; `find-file-noselect' performed by default handler
153 ;; `get-file-buffer' performed by default handler
154 (insert-directory . tramp-smb-handle-insert-directory
)
155 (insert-file-contents . tramp-handle-insert-file-contents
)
156 (load . tramp-handle-load
)
157 (make-directory . tramp-smb-handle-make-directory
)
158 (make-directory-internal . tramp-smb-handle-make-directory-internal
)
159 (make-symbolic-link . tramp-smb-not-handled
)
160 (rename-file . tramp-smb-handle-rename-file
)
161 (set-file-modes . tramp-smb-not-handled
)
162 (set-visited-file-modtime . tramp-smb-not-handled
)
163 (shell-command . tramp-smb-not-handled
)
164 ;; `substitute-in-file-name' performed by default handler
165 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
166 (vc-registered . tramp-smb-not-handled
)
167 (verify-visited-file-modtime . tramp-smb-not-handled
)
168 (write-region . tramp-smb-handle-write-region
)
170 "Alist of handler functions for Tramp SMB method.
171 Operations not mentioned here will be handled by the default Emacs primitives.")
173 (defun tramp-smb-file-name-p (filename)
174 "Check if it's a filename for SMB servers."
175 (let ((v (tramp-dissect-file-name filename
)))
178 (tramp-file-name-multi-method v
)
179 (tramp-file-name-method v
)
180 (tramp-file-name-user v
)
181 (tramp-file-name-host v
))
184 (defun tramp-smb-file-name-handler (operation &rest args
)
185 "Invoke the SMB related OPERATION.
186 First arg specifies the OPERATION, second arg is a list of arguments to
187 pass to the OPERATION."
188 (let ((fn (assoc operation tramp-smb-file-name-handler-alist
)))
190 (if (eq (cdr fn
) 'tramp-smb-not-handled
)
191 (apply (cdr fn
) operation args
)
192 (save-match-data (apply (cdr fn
) args
)))
193 (tramp-run-real-handler operation args
))))
195 (add-to-list 'tramp-foreign-file-name-handler-alist
196 (cons 'tramp-smb-file-name-p
'tramp-smb-file-name-handler
))
199 ;; File name primitives
201 (defun tramp-smb-not-handled (operation &rest args
)
202 "Default handler for all functions which are disrecarded."
203 (tramp-message 10 "Won't be handled: %s %s" operation args
)
206 (defun tramp-smb-handle-copy-file
207 (filename newname
&optional ok-if-already-exists keep-date
)
208 "Like `copy-file' for tramp files.
209 KEEP-DATE is not handled in case NEWNAME resides on an SMB server."
210 (setq filename
(expand-file-name filename
)
211 newname
(expand-file-name newname
))
213 (let ((tmpfile (file-local-copy filename
)))
217 (rename-file tmpfile newname ok-if-already-exists
)
220 (when (file-directory-p newname
)
221 (setq newname
(expand-file-name
222 (file-name-nondirectory filename
) newname
)))
223 (when (and (not ok-if-already-exists
)
224 (file-exists-p newname
))
225 (error "copy-file: file %s already exists" newname
))
227 ; (with-parsed-tramp-file-name newname nil
228 (let (user host localname
)
229 (with-parsed-tramp-file-name newname l
230 (setq user l-user host l-host localname l-localname
))
232 (let ((share (tramp-smb-get-share localname
))
233 (file (tramp-smb-get-localname localname t
)))
235 (error "Target `%s' must contain a share name" filename
))
236 (tramp-smb-maybe-open-connection user host share
)
237 (tramp-message-for-buffer
238 nil tramp-smb-method user host
239 5 "Copying file %s to file %s..." filename newname
)
240 (if (tramp-smb-send-command
241 user host
(format "put %s \"%s\"" filename file
))
242 (tramp-message-for-buffer
243 nil tramp-smb-method user host
244 5 "Copying file %s to file %s...done" filename newname
)
245 (error "Cannot copy `%s'" filename
))))))))
247 (defun tramp-smb-handle-delete-directory (directory)
248 "Like `delete-directory' for tramp files."
249 (setq directory
(directory-file-name (expand-file-name directory
)))
250 (unless (file-exists-p directory
)
251 (error "Cannot delete non-existing directory `%s'" directory
))
252 ; (with-parsed-tramp-file-name directory nil
253 (let (user host localname
)
254 (with-parsed-tramp-file-name directory l
255 (setq user l-user host l-host localname l-localname
))
257 (let ((share (tramp-smb-get-share localname
))
258 (dir (tramp-smb-get-localname (file-name-directory localname
) t
))
259 (file (file-name-nondirectory localname
)))
260 (tramp-smb-maybe-open-connection user host share
)
262 (tramp-smb-send-command user host
(format "cd \"%s\"" dir
))
263 (tramp-smb-send-command user host
(format "rmdir \"%s\"" file
)))
265 (tramp-smb-send-command user host
(format "cd \\"))
267 (tramp-smb-send-command user host
(format "cd \\"))
268 (error "Cannot delete directory `%s'" directory
))))))
270 (defun tramp-smb-handle-delete-file (filename)
271 "Like `delete-file' for tramp files."
272 (setq filename
(expand-file-name filename
))
273 (unless (file-exists-p filename
)
274 (error "Cannot delete non-existing file `%s'" filename
))
275 ; (with-parsed-tramp-file-name filename nil
276 (let (user host localname
)
277 (with-parsed-tramp-file-name filename l
278 (setq user l-user host l-host localname l-localname
))
280 (let ((share (tramp-smb-get-share localname
))
281 (dir (tramp-smb-get-localname (file-name-directory localname
) t
))
282 (file (file-name-nondirectory localname
)))
283 (unless (file-exists-p filename
)
284 (error "Cannot delete non-existing file `%s'" filename
))
285 (tramp-smb-maybe-open-connection user host share
)
287 (tramp-smb-send-command user host
(format "cd \"%s\"" dir
))
288 (tramp-smb-send-command user host
(format "rm \"%s\"" file
)))
290 (tramp-smb-send-command user host
(format "cd \\"))
292 (tramp-smb-send-command user host
(format "cd \\"))
293 (error "Cannot delete file `%s'" filename
))))))
295 (defun tramp-smb-handle-directory-files
296 (directory &optional full match nosort
)
297 "Like `directory-files' for tramp files."
298 (setq directory
(directory-file-name (expand-file-name directory
)))
299 ; (with-parsed-tramp-file-name directory nil
300 (let (user host localname
)
301 (with-parsed-tramp-file-name directory l
302 (setq user l-user host l-host localname l-localname
))
304 (let* ((share (tramp-smb-get-share localname
))
305 (file (tramp-smb-get-localname localname nil
))
306 (entries (tramp-smb-get-file-entries user host share file
)))
307 ;; Just the file names are needed
308 (setq entries
(mapcar 'car entries
))
309 ;; Discriminate with regexp
313 (mapcar (lambda (x) (when (string-match match x
) x
))
315 ;; Make absolute localnames if necessary
319 (concat (file-name-as-directory directory
) x
))
321 ;; Sort them if necessary
322 (unless nosort
(setq entries
(sort entries
'string-lessp
)))
326 (defun tramp-smb-handle-directory-files-and-attributes
327 (directory &optional full match nosort id-format
)
328 "Like `directory-files-and-attributes' for tramp files."
331 ;; We cannot call `file-attributes' for backward compatibility reasons.
332 ;; Its optional parameter ID-FORMAT is introduced with Emacs 21.4.
333 (cons x
(tramp-smb-handle-file-attributes
334 (if full x
(concat (file-name-as-directory directory
) x
)) id-format
)))
335 (directory-files directory full match nosort
)))
337 (defun tramp-smb-handle-file-attributes (filename &optional id-format
)
338 "Like `file-attributes' for tramp files."
339 ; (with-parsed-tramp-file-name filename nil
340 (let (user host localname
)
341 (with-parsed-tramp-file-name filename l
342 (setq user l-user host l-host localname l-localname
))
344 (let* ((share (tramp-smb-get-share localname
))
345 (file (tramp-smb-get-localname localname nil
))
346 (entries (tramp-smb-get-file-entries user host share file
))
348 (assoc (file-name-nondirectory file
) entries
)))
349 (uid (if (and id-format
(equal id-format
'string
)) "nobody" -
1))
350 (gid (if (and id-format
(equal id-format
'string
)) "nogroup" -
1))
351 (inode (tramp-smb-get-inode share file
))
352 (device (tramp-get-device nil tramp-smb-method user host
)))
356 (list (and (string-match "d" (nth 1 entry
))
362 (nth 3 entry
) ;5 mtime
364 (nth 2 entry
) ;7 size
365 (nth 1 entry
) ;8 mode
367 inode
;10 inode number
368 device
)))))) ;11 file system number
370 (defun tramp-smb-handle-file-directory-p (filename)
371 "Like `file-directory-p' for tramp files."
372 ; (with-parsed-tramp-file-name filename nil
373 (let (user host localname
)
374 (with-parsed-tramp-file-name filename l
375 (setq user l-user host l-host localname l-localname
))
377 (let* ((share (tramp-smb-get-share localname
))
378 (file (tramp-smb-get-localname localname nil
))
379 (entries (tramp-smb-get-file-entries user host share file
))
381 (assoc (file-name-nondirectory file
) entries
))))
383 (string-match "d" (nth 1 entry
))
386 (defun tramp-smb-handle-file-exists-p (filename)
387 "Like `file-exists-p' for tramp files."
388 ; (with-parsed-tramp-file-name filename nil
389 (let (user host localname
)
390 (with-parsed-tramp-file-name filename l
391 (setq user l-user host l-host localname l-localname
))
393 (let* ((share (tramp-smb-get-share localname
))
394 (file (tramp-smb-get-localname localname nil
))
395 (entries (tramp-smb-get-file-entries user host share file
)))
397 (member (file-name-nondirectory file
) (mapcar 'car entries
))
400 (defun tramp-smb-handle-file-local-copy (filename)
401 "Like `file-local-copy' for tramp files."
402 (with-parsed-tramp-file-name filename nil
404 (let ((share (tramp-smb-get-share localname
))
405 (file (tramp-smb-get-localname localname t
))
406 (tmpfil (tramp-make-temp-file)))
407 (unless (file-exists-p filename
)
408 (error "Cannot make local copy of non-existing file `%s'" filename
))
409 (tramp-message-for-buffer
410 nil tramp-smb-method user host
411 5 "Fetching %s to tmp file %s..." filename tmpfil
)
412 (tramp-smb-maybe-open-connection user host share
)
413 (if (tramp-smb-send-command
414 user host
(format "get \"%s\" %s" file tmpfil
))
415 (tramp-message-for-buffer
416 nil tramp-smb-method user host
417 5 "Fetching %s to tmp file %s...done" filename tmpfil
)
418 (error "Cannot make local copy of file `%s'" filename
))
421 ;; This function should return "foo/" for directories and "bar" for
423 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
424 "Like `file-name-all-completions' for tramp files."
425 ; (with-parsed-tramp-file-name directory nil
426 (let (user host localname
)
427 (with-parsed-tramp-file-name directory l
428 (setq user l-user host l-host localname l-localname
))
431 (let* ((share (tramp-smb-get-share localname
))
432 (file (tramp-smb-get-localname localname nil
))
433 (entries (tramp-smb-get-file-entries user host share file
)))
440 (if (string-match "d" (nth 1 x
))
441 (file-name-as-directory (nth 0 x
))
445 (defun tramp-smb-handle-file-newer-than-file-p (file1 file2
)
446 "Like `file-newer-than-file-p' for tramp files."
448 ((not (file-exists-p file1
)) nil
)
449 ((not (file-exists-p file2
)) t
)
450 (t (tramp-smb-time-less-p (file-attributes file2
)
451 (file-attributes file1
)))))
453 (defun tramp-smb-handle-file-writable-p (filename)
454 "Like `file-writable-p' for tramp files."
455 ; (with-parsed-tramp-file-name filename nil
456 (let (user host localname
)
457 (with-parsed-tramp-file-name filename l
458 (setq user l-user host l-host localname l-localname
))
460 (let* ((share (tramp-smb-get-share localname
))
461 (file (tramp-smb-get-localname localname nil
))
462 (entries (tramp-smb-get-file-entries user host share file
))
464 (assoc (file-name-nondirectory file
) entries
))))
466 (string-match "w" (nth 1 entry
))
469 (defun tramp-smb-handle-insert-directory
470 (filename switches
&optional wildcard full-directory-p
)
471 "Like `insert-directory' for tramp files.
472 WILDCARD and FULL-DIRECTORY-P are not handled."
473 (setq filename
(expand-file-name filename
))
474 (when (file-directory-p filename
)
475 ;; This check is a little bit strange, but in `dired-add-entry'
476 ;; this function is called with a non-directory ...
477 (setq filename
(file-name-as-directory filename
)))
478 ; (with-parsed-tramp-file-name filename nil
479 (let (user host localname
)
480 (with-parsed-tramp-file-name filename l
481 (setq user l-user host l-host localname l-localname
))
483 (let* ((share (tramp-smb-get-share localname
))
484 (file (tramp-smb-get-localname localname nil
))
485 (entries (tramp-smb-get-file-entries user host share file
)))
487 ;; Delete dummy "" entry, useless entries
489 (if (file-directory-p filename
)
490 (delq (assoc "" entries
) entries
)
491 ;; We just need the only and only entry FILENAME.
492 (list (assoc (file-name-nondirectory filename
) entries
))))
499 (if (string-match "t" switches
)
501 (tramp-smb-time-less-p (nth 3 y
) (nth 3 x
))
503 (string-lessp (nth 0 x
) (nth 0 y
))))))
510 "%10s %3d %-8s %-8s %8s %s %s\n"
515 (if (tramp-smb-time-less-p
516 (tramp-smb-time-subtract (current-time) (nth 3 x
))
517 tramp-smb-half-a-year
)
521 (nth 0 x
))) ; file name
526 (defun tramp-smb-handle-make-directory (dir &optional parents
)
527 "Like `make-directory' for tramp files."
528 (setq dir
(directory-file-name (expand-file-name dir
)))
529 (unless (file-name-absolute-p dir
)
530 (setq dir
(concat default-directory dir
)))
531 ; (with-parsed-tramp-file-name dir nil
532 (let (user host localname
)
533 (with-parsed-tramp-file-name dir l
534 (setq user l-user host l-host localname l-localname
))
536 (let* ((share (tramp-smb-get-share localname
))
537 (ldir (file-name-directory dir
)))
538 ;; Make missing directory parts
539 (when (and parents share
(not (file-directory-p ldir
)))
540 (make-directory ldir parents
))
542 (when (file-directory-p ldir
)
543 (make-directory-internal dir
))
544 (unless (file-directory-p dir
)
545 (error "Couldn't make directory %s" dir
))))))
547 (defun tramp-smb-handle-make-directory-internal (directory)
548 "Like `make-directory-internal' for tramp files."
549 (setq directory
(directory-file-name (expand-file-name directory
)))
550 (unless (file-name-absolute-p directory
)
551 (setq directory
(concat default-directory directory
)))
552 ; (with-parsed-tramp-file-name directory nil
553 (let (user host localname
)
554 (with-parsed-tramp-file-name directory l
555 (setq user l-user host l-host localname l-localname
))
557 (let* ((share (tramp-smb-get-share localname
))
558 (file (tramp-smb-get-localname localname nil
)))
559 (when (file-directory-p (file-name-directory directory
))
560 (tramp-smb-maybe-open-connection user host share
)
561 (tramp-smb-send-command user host
(format "mkdir \"%s\"" file
)))
562 (unless (file-directory-p directory
)
563 (error "Couldn't make directory %s" directory
))))))
565 (defun tramp-smb-handle-rename-file
566 (filename newname
&optional ok-if-already-exists
)
567 "Like `rename-file' for tramp files."
568 (setq filename
(expand-file-name filename
)
569 newname
(expand-file-name newname
))
571 (let ((tmpfile (file-local-copy filename
)))
575 (rename-file tmpfile newname ok-if-already-exists
)
578 (when (file-directory-p newname
)
579 (setq newname
(expand-file-name
580 (file-name-nondirectory filename
) newname
)))
581 (when (and (not ok-if-already-exists
)
582 (file-exists-p newname
))
583 (error "rename-file: file %s already exists" newname
))
585 ; (with-parsed-tramp-file-name newname nil
586 (let (user host localname
)
587 (with-parsed-tramp-file-name newname l
588 (setq user l-user host l-host localname l-localname
))
590 (let ((share (tramp-smb-get-share localname
))
591 (file (tramp-smb-get-localname localname t
)))
592 (tramp-smb-maybe-open-connection user host share
)
593 (tramp-message-for-buffer
594 nil tramp-smb-method user host
595 5 "Copying file %s to file %s..." filename newname
)
596 (if (tramp-smb-send-command
597 user host
(format "put %s \"%s\"" filename file
))
598 (tramp-message-for-buffer
599 nil tramp-smb-method user host
600 5 "Copying file %s to file %s...done" filename newname
)
601 (error "Cannot rename `%s'" filename
)))))))
603 (delete-file filename
))
605 (defun tramp-smb-handle-write-region
606 (start end filename
&optional append visit lockname confirm
)
607 "Like `write-region' for tramp files."
608 (unless (eq append nil
)
609 (error "Cannot append to file using tramp (`%s')" filename
))
610 (setq filename
(expand-file-name filename
))
611 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
612 (when (and (not (featurep 'xemacs
))
613 confirm
(file-exists-p filename
))
614 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
616 (error "File not overwritten")))
617 ; (with-parsed-tramp-file-name filename nil
618 (let (user host localname
)
619 (with-parsed-tramp-file-name filename l
620 (setq user l-user host l-host localname l-localname
))
622 (let ((share (tramp-smb-get-share localname
))
623 (file (tramp-smb-get-localname localname t
))
624 (curbuf (current-buffer))
625 ;; We use this to save the value of `last-coding-system-used'
626 ;; after writing the tmp file. At the end of the function,
627 ;; we set `last-coding-system-used' to this saved value.
628 ;; This way, any intermediary coding systems used while
629 ;; talking to the remote shell or suchlike won't hose this
630 ;; variable. This approach was snarfed from ange-ftp.el.
633 ;; Write region into a tmp file.
634 (setq tmpfil
(tramp-make-temp-file))
635 ;; We say `no-message' here because we don't want the visited file
636 ;; modtime data to be clobbered from the temp file. We call
637 ;; `set-visited-file-modtime' ourselves later on.
638 (tramp-run-real-handler
640 (if confirm
; don't pass this arg unless defined for backward compat.
641 (list start end tmpfil append
'no-message lockname confirm
)
642 (list start end tmpfil append
'no-message lockname
)))
643 ;; Now, `last-coding-system-used' has the right value. Remember it.
644 (when (boundp 'last-coding-system-used
)
645 (setq coding-system-used last-coding-system-used
))
647 (tramp-smb-maybe-open-connection user host share
)
648 (tramp-message-for-buffer
649 nil tramp-smb-method user host
650 5 "Writing tmp file %s to file %s..." tmpfil filename
)
651 (if (tramp-smb-send-command
652 user host
(format "put %s \"%s\"" tmpfil file
))
653 (tramp-message-for-buffer
654 nil tramp-smb-method user host
655 5 "Writing tmp file %s to file %s...done" tmpfil filename
)
656 (error "Cannot write `%s'" filename
))
659 (unless (equal curbuf
(current-buffer))
660 (error "Buffer has changed from `%s' to `%s'"
661 curbuf
(current-buffer)))
663 (set-visited-file-modtime))
664 ;; Make `last-coding-system-used' have the right value.
665 (when (boundp 'last-coding-system-used
)
666 (setq last-coding-system-used coding-system-used
))))))
669 ;; Internal file name functions
671 (defun tramp-smb-get-share (localname)
672 "Returns the share name of LOCALNAME."
674 (when (string-match "^/?\\([^/]+\\)/" localname
)
675 (match-string 1 localname
))))
677 (defun tramp-smb-get-localname (localname convert
)
678 "Returns the file name of LOCALNAME.
679 If CONVERT is non-nil exchange \"/\" by \"\\\\\"."
681 (let ((res localname
))
684 res
(if (string-match "^/?[^/]+/\\(.*\\)" res
)
687 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
688 (match-string 1 res
) "")
689 (match-string 1 res
))
690 (if (string-match "^/?\\([^/]+\\)$" res
)
694 ;; Sometimes we have discarded `substitute-in-file-name'
695 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" res
)
696 (setq res
(replace-match "$" nil nil res
1)))
700 ;; Share names of a host are cached. It is very unlikely that the
701 ;; shares do change during connection.
702 (defun tramp-smb-get-file-entries (user host share localname
)
703 "Read entries which match LOCALNAME.
704 Either the shares are listed, or the `dir' command is executed.
705 Only entries matching the localname are returned.
706 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
709 (let ((base (or (and (> (length localname
) 0)
710 (string-match "\\([^/]+\\)$" localname
)
711 (regexp-quote (match-string 1 localname
)))
714 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
715 (if (and (not share
) tramp-smb-share-cache
)
716 ;; Return cached shares
717 (setq res tramp-smb-share-cache
)
719 (tramp-smb-maybe-open-connection user host share
)
721 (tramp-smb-send-command
724 (if (zerop (length localname
)) "" (concat "\"" localname
"*\"")))))
725 (goto-char (point-min))
727 (unless (re-search-forward tramp-smb-errors nil t
)
729 (setq entry
(tramp-smb-read-file-entry share
))
731 (when entry
(add-to-list 'res entry
))))
733 ;; Cache share entries
734 (setq tramp-smb-share-cache res
)))
737 ;; Add directory itself
738 (add-to-list 'res
'("" "dr-xr-xr-x" 0 (0 0)))
740 ;; Check for matching entries
742 (lambda (x) (and (string-match base
(nth 0 x
)) x
))
745 ;; Return either a share name (if SHARE is nil), or a file name
747 ;; If shares are listed, the following format is expected
749 ;; \s-\{8,8} - leading spaces
750 ;; \S-\(.*\S-\)\s-* - share name, 14 char
751 ;; \s- - space delimeter
752 ;; \S-+\s-* - type, 8 char, "Disk " expected
753 ;; \(\s-\{2,2\}.*\)? - space delimeter, comment
755 ;; Entries provided by smbclient DIR aren't fully regular.
756 ;; They should have the format
758 ;; \s-\{2,2} - leading spaces
759 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
760 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
761 ;; \s- - space delimeter
762 ;; \s-+[0-9]+ - size, 8 chars, right bound
763 ;; \s-\{2,2\} - space delimeter
764 ;; \w\{3,3\} - weekday
765 ;; \s- - space delimeter
767 ;; \s- - space delimeter
769 ;; \s- - space delimeter
770 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
771 ;; \s- - space delimeter
772 ;; [0-9]\{4,4\} - year
774 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
775 ;; has function display_finfo:
777 ;; d_printf(" %-30s%7.7s %8.0f %s",
779 ;; attrib_string(finfo->mode),
780 ;; (double)finfo->size,
781 ;; asctime(LocalTime(&t)));
783 ;; in Samba 1.9, there's the following code:
785 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
786 ;; CNV_LANG(finfo->name),
787 ;; attrib_string(finfo->mode),
789 ;; asctime(LocalTime(&t))));
792 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
793 ;; available in older Emacsen.
794 ;; * The length of constructs (file name, size) might exceed the default.
795 ;; * File names might contain spaces.
796 ;; * Permissions might be empty.
798 ;; So we try to analyze backwards.
799 (defun tramp-smb-read-file-entry (share)
800 "Parse entry in SMB output buffer.
801 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
802 Result is the list (LOCALNAME MODE SIZE MTIME)."
803 (let ((line (buffer-substring (point) (tramp-point-at-eol)))
804 localname mode size month day hour min sec year mtime
)
809 (when (string-match "^\\s-+\\(\\S-+\\)\\s-+Disk" line
)
810 (setq localname
(match-string 1 line
)
818 (if (string-match "\\([0-9]+\\)$" line
)
819 (setq year
(string-to-number (match-string 1 line
))
820 line
(substring line
0 -
5))
824 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
825 (setq hour
(string-to-number (match-string 1 line
))
826 min
(string-to-number (match-string 2 line
))
827 sec
(string-to-number (match-string 3 line
))
828 line
(substring line
0 -
9))
832 (if (string-match "\\([0-9]+\\)$" line
)
833 (setq day
(string-to-number (match-string 1 line
))
834 line
(substring line
0 -
3))
838 (if (string-match "\\(\\w+\\)$" line
)
839 (setq month
(match-string 1 line
)
840 line
(substring line
0 -
4))
844 (if (string-match "\\(\\w+\\)$" line
)
845 (setq line
(substring line
0 -
5))
849 (if (string-match "\\([0-9]+\\)$" line
)
850 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
851 (setq size
(string-to-number (match-string 1 line
)))
852 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
853 (setq length
(+ length
(match-end 0))))
854 (setq line
(substring line
0 length
)))
857 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID
858 (if (string-match "\\([ADHRSV]+\\)?$" line
)
860 mode
(or (match-string 1 line
) "")
861 mode
(save-match-data (format
863 (if (string-match "D" mode
) "d" "-")
866 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
867 line
(substring line
0 -
7))
871 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
872 (setq localname
(match-string 1 line
))
875 (when (and localname mode size
)
877 (if (and sec min hour day month year
)
880 (cdr (assoc (downcase month
) tramp-smb-parse-time-months
))
883 (list localname mode size mtime
))))
885 ;; Inodes don't exist for SMB files. Therefore we must generate virtual ones.
886 ;; Used in `find-buffer-visiting'.
887 ;; The method applied might be not so efficient (Ange-FTP uses hashes). But
888 ;; performance isn't the major issue given that file transfer will take time.
890 (defun tramp-smb-get-inode (share file
)
891 "Returns the virtual inode number.
892 If it doesn't exist, generate a new one."
893 (let ((string (concat share
"/" (directory-file-name file
))))
894 (unless (assoc string tramp-smb-inodes
)
895 (add-to-list 'tramp-smb-inodes
896 (list string
(length tramp-smb-inodes
))))
897 (nth 1 (assoc string tramp-smb-inodes
))))
900 ;; Connection functions
902 (defun tramp-smb-send-command (user host command
)
903 "Send the COMMAND to USER at HOST (logged into an SMB session).
904 Erases temporary buffer before sending the command. Returns nil if
905 there has been an error message from smbclient."
907 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
909 (tramp-send-command nil tramp-smb-method user host command nil t
)
910 (tramp-smb-wait-for-output user host
)))
912 (defun tramp-smb-maybe-open-connection (user host share
)
913 "Maybe open a connection to HOST, logging in as USER, using `tramp-smb-program'.
914 Does not do anything if a connection is already open, but re-opens the
915 connection if a previous connection has died for some reason."
916 (let ((p (get-buffer-process
917 (tramp-get-buffer nil tramp-smb-method user host
))))
919 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
920 ;; Check whether it is still the same share
921 (unless (and p
(processp p
) (string-equal tramp-smb-share share
))
922 (when (and p
(processp p
))
925 ;; If too much time has passed since last command was sent, look
926 ;; whether process is still alive. If it isn't, kill it.
927 (when (and tramp-last-cmd-time
928 (> (tramp-time-diff (current-time) tramp-last-cmd-time
) 60)
929 p
(processp p
) (memq (process-status p
) '(run open
)))
930 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
933 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
934 (when (and p
(processp p
))
936 (tramp-smb-open-connection user host share
))))
938 (defun tramp-smb-open-connection (user host share
)
939 "Open a connection using `tramp-smb-program'.
940 This starts the command `smbclient //HOST/SHARE -U USER', then waits
941 for a remote password prompt. It queries the user for the password,
942 then sends the password to the remote host.
944 Domain names in USER and port numbers in HOST are acknowledged."
947 (let* ((buffer (tramp-get-buffer nil tramp-smb-method user host
))
952 ; Check for domain ("user%domain") and port ("host#port")
953 (when (and user
(string-match "\\(.+\\)%\\(.+\\)" user
))
954 (setq real-user
(or (match-string 1 user
) user
)
955 domain
(match-string 2 user
)))
957 (when (and host
(string-match "\\(.+\\)#\\(.+\\)" host
))
958 (setq real-host
(or (match-string 1 host
) host
)
959 port
(match-string 2 host
)))
962 (setq args
(list (concat "//" real-host
"/" share
)))
963 (setq args
(list "-L" real-host
)))
966 (setq args
(append args
(list "-U" real-user
)))
967 (setq args
(append args
(list "-N"))))
969 (when domain
(setq args
(append args
(list "-W" domain
))))
970 (when port
(setq args
(append args
(list "-p" port
))))
973 (tramp-pre-connection nil tramp-smb-method user host
)
974 (tramp-message 7 "Opening connection for //%s@%s/%s..."
975 user host
(or share
""))
977 (let* ((default-directory (tramp-temporary-file-directory))
978 ;; If we omit the conditional here, then we would use
979 ;; `undecided-dos' in some cases. With the conditional,
980 ;; we use nil in these cases. Which one is right?
981 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
982 (> emacs-major-version
20))
983 tramp-dos-coding-system
))
984 (p (apply #'start-process
(buffer-name buffer
) buffer
985 tramp-smb-program args
)))
987 (tramp-message 9 "Started process %s" (process-command p
))
988 (process-kill-without-query p
)
990 (set-process-sentinel
991 p
(lambda (proc str
) (setq tramp-smb-process-running nil
)))
992 ; If no share is given, the process will terminate
993 (setq tramp-smb-process-running share
994 tramp-smb-share share
)
998 (let ((pw-prompt "Password:"))
999 (tramp-message 9 "Sending password")
1000 (tramp-enter-password p pw-prompt
)))
1002 (unless (tramp-smb-wait-for-output user host
)
1003 (error "Cannot open connection //%s@%s/%s"
1004 user host
(or share
"")))))))
1006 ;; We don't use timeouts. If needed, the caller shall wrap around.
1007 (defun tramp-smb-wait-for-output (user host
)
1008 "Wait for output from smbclient command.
1009 Sets position to begin of buffer.
1010 Returns nil if an error message has appeared."
1012 (let ((proc (get-buffer-process (current-buffer)))
1013 (found (progn (goto-char (point-max))
1015 (looking-at tramp-smb-prompt
)))
1018 ;; Algorithm: get waiting output. See if last line contains
1019 ;; tramp-smb-prompt sentinel, or process has exited.
1020 ;; If not, wait a bit and again get waiting output.
1021 (while (and (not found
) tramp-smb-process-running
)
1022 (accept-process-output proc
)
1023 (goto-char (point-max))
1025 (setq found
(looking-at tramp-smb-prompt
)))
1027 ;; There might be pending output. If tramp-smb-prompt sentinel
1028 ;; hasn't been found, the process has died already. We should
1029 ;; give it a chance.
1030 (when (not found
) (accept-process-output nil
1))
1032 ;; Search for errors.
1033 (goto-char (point-min))
1034 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1036 ;; Add output to debug buffer if appropriate.
1037 (when tramp-debug-buffer
1039 (tramp-get-debug-buffer nil tramp-smb-method user host
)
1040 (point-min) (point-max))
1041 (when (and (not found
) tramp-smb-process-running
)
1044 (tramp-get-debug-buffer nil tramp-smb-method user host
))
1045 (goto-char (point-max))
1046 (insert (format "[[Remote prompt `%s' not found]]\n"
1047 tramp-smb-prompt
)))))
1048 (goto-char (point-min))
1049 ;; Return value is whether no error message has appeared.
1053 ;; Snarfed code from time-date.el and parse-time.el
1055 (defconst tramp-smb-half-a-year
'(241 17024)
1056 "Evaluated by \"(days-to-time 183)\".")
1058 (defconst tramp-smb-parse-time-months
'(("jan" .
1) ("feb" .
2) ("mar" .
3)
1059 ("apr" .
4) ("may" .
5) ("jun" .
6)
1060 ("jul" .
7) ("aug" .
8) ("sep" .
9)
1061 ("oct" .
10) ("nov" .
11) ("dec" .
12))
1062 "Alist mapping month names to integers.")
1064 (defun tramp-smb-time-less-p (t1 t2
)
1065 "Say whether time value T1 is less than time value T2."
1066 (unless t1
(setq t1
'(0 0)))
1067 (unless t2
(setq t2
'(0 0)))
1068 (or (< (car t1
) (car t2
))
1069 (and (= (car t1
) (car t2
))
1070 (< (nth 1 t1
) (nth 1 t2
)))))
1072 (defun tramp-smb-time-subtract (t1 t2
)
1073 "Subtract two time values.
1074 Return the difference in the format of a time value."
1075 (unless t1
(setq t1
'(0 0)))
1076 (unless t2
(setq t2
'(0 0)))
1077 (let ((borrow (< (cadr t1
) (cadr t2
))))
1078 (list (- (car t1
) (car t2
) (if borrow
1 0))
1079 (- (+ (if borrow
65536 0) (cadr t1
)) (cadr t2
)))))
1082 ;; `PC-do-completion' touches the returning "$$" by `substitute-in-file-name'.
1083 ;; Must be corrected.
1085 (defadvice PC-do-completion
(around tramp-smb-advice-PC-do-completion activate
)
1086 "Changes \"$\" back to \"$$\" in minibuffer."
1087 (if (funcall PC-completion-as-file-name-predicate
)
1090 ;; Substitute file names
1091 (let* ((beg (or (and (functionp 'minibuffer-prompt-end
) ; Emacs 21
1092 (funcall 'minibuffer-prompt-end
))
1095 (str (substitute-in-file-name (buffer-substring beg end
))))
1096 (delete-region beg end
)
1098 (ad-set-arg 2 (point)))
1100 ;; Do `PC-do-completion' without substitution
1102 (fset 'save
(symbol-function 'substitute-in-file-name
))
1103 (fset 'substitute-in-file-name
(symbol-function 'identity
))
1105 (fset 'substitute-in-file-name
(symbol-function 'save
)))
1108 (let* ((beg (or (and (functionp 'minibuffer-prompt-end
) ; Emacs 21
1109 (funcall 'minibuffer-prompt-end
))
1112 (str (buffer-substring beg end
)))
1113 (delete-region beg end
)
1114 (insert (if (string-match "\\(\\$\\)\\(/\\|$\\)" str
)
1115 (replace-match "$$" nil nil str
1)
1118 ;; No file names. Behave unchanged.
1121 (provide 'tramp-smb
)
1125 ;; * Provide a local smb.conf. The default one might not be readable.
1126 ;; * Error handling in case password is wrong.
1127 ;; * Read password from "~/.netrc".
1128 ;; * Use different buffers for different shares. By this, the password
1129 ;; won't be requested again when changing shares on the same host.
1130 ;; * Return more comprehensive file permission string. Think whether it is
1131 ;; possible to implement `set-file-modes'.
1132 ;; * Handle WILDCARD and FULL-DIRECTORY-P in
1133 ;; `tramp-smb-handle-insert-directory'.
1134 ;; * Handle links (FILENAME.LNK).
1135 ;; * Maybe local tmp files should have the same extension like the original
1136 ;; files. Strange behaviour with jka-compr otherwise?
1137 ;; * Copy files in dired from SMB to another method doesn't work.
1138 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1139 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1140 ;; * Provide variables for debug.
1141 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1144 ;;; arch-tag: fcc9dbec-7503-4d73-b638-3c8aa59575f5
1145 ;;; tramp-smb.el ends here