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'" directory
))))))
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
)
328 "Like `directory-files-and-attributes' for tramp files."
331 (cons x
(file-attributes
332 (if full x
(concat (file-name-as-directory directory
) x
)))))
333 (directory-files directory full match nosort
)))
335 (defun tramp-smb-handle-file-attributes (filename &optional nonnumeric
)
336 "Like `file-attributes' for tramp files.
337 Optional argument NONNUMERIC means return user and group name
338 rather than as numbers."
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 (inode (tramp-smb-get-inode share file
))
350 (device (tramp-get-device nil tramp-smb-method user host
)))
354 (list (and (string-match "d" (nth 1 entry
))
360 (nth 3 entry
) ;5 mtime
362 (nth 2 entry
) ;7 size
363 (nth 1 entry
) ;8 mode
365 inode
;10 inode number
366 device
)))))) ;11 file system number
368 (defun tramp-smb-handle-file-directory-p (filename)
369 "Like `file-directory-p' for tramp files."
370 ; (with-parsed-tramp-file-name filename nil
371 (let (user host localname
)
372 (with-parsed-tramp-file-name filename l
373 (setq user l-user host l-host localname l-localname
))
375 (let* ((share (tramp-smb-get-share localname
))
376 (file (tramp-smb-get-localname localname nil
))
377 (entries (tramp-smb-get-file-entries user host share file
))
379 (assoc (file-name-nondirectory file
) entries
))))
381 (string-match "d" (nth 1 entry
))
384 (defun tramp-smb-handle-file-exists-p (filename)
385 "Like `file-exists-p' for tramp files."
386 ; (with-parsed-tramp-file-name filename nil
387 (let (user host localname
)
388 (with-parsed-tramp-file-name filename l
389 (setq user l-user host l-host localname l-localname
))
391 (let* ((share (tramp-smb-get-share localname
))
392 (file (tramp-smb-get-localname localname nil
))
393 (entries (tramp-smb-get-file-entries user host share file
)))
395 (member (file-name-nondirectory file
) (mapcar 'car entries
))
398 (defun tramp-smb-handle-file-local-copy (filename)
399 "Like `file-local-copy' for tramp files."
400 (with-parsed-tramp-file-name filename nil
402 (let ((share (tramp-smb-get-share localname
))
403 (file (tramp-smb-get-localname localname t
))
404 (tmpfil (tramp-make-temp-file)))
405 (unless (file-exists-p filename
)
406 (error "Cannot make local copy of non-existing file `%s'" filename
))
407 (tramp-message-for-buffer
408 nil tramp-smb-method user host
409 5 "Fetching %s to tmp file %s..." filename tmpfil
)
410 (tramp-smb-maybe-open-connection user host share
)
411 (if (tramp-smb-send-command
412 user host
(format "get \"%s\" %s" file tmpfil
))
413 (tramp-message-for-buffer
414 nil tramp-smb-method user host
415 5 "Fetching %s to tmp file %s...done" filename tmpfil
)
416 (error "Cannot make local copy of file `%s'" filename
))
419 ;; This function should return "foo/" for directories and "bar" for
421 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
422 "Like `file-name-all-completions' for tramp files."
423 ; (with-parsed-tramp-file-name directory nil
424 (let (user host localname
)
425 (with-parsed-tramp-file-name directory l
426 (setq user l-user host l-host localname l-localname
))
429 (let* ((share (tramp-smb-get-share localname
))
430 (file (tramp-smb-get-localname localname nil
))
431 (entries (tramp-smb-get-file-entries user host share file
)))
438 (if (string-match "d" (nth 1 x
))
439 (file-name-as-directory (nth 0 x
))
443 (defun tramp-smb-handle-file-newer-than-file-p (file1 file2
)
444 "Like `file-newer-than-file-p' for tramp files."
446 ((not (file-exists-p file1
)) nil
)
447 ((not (file-exists-p file2
)) t
)
448 (t (tramp-smb-time-less-p (file-attributes file2
)
449 (file-attributes file1
)))))
451 (defun tramp-smb-handle-file-writable-p (filename)
452 "Like `file-writable-p' for tramp files."
453 ; (with-parsed-tramp-file-name filename nil
454 (let (user host localname
)
455 (with-parsed-tramp-file-name filename l
456 (setq user l-user host l-host localname l-localname
))
458 (let* ((share (tramp-smb-get-share localname
))
459 (file (tramp-smb-get-localname localname nil
))
460 (entries (tramp-smb-get-file-entries user host share file
))
462 (assoc (file-name-nondirectory file
) entries
))))
464 (string-match "w" (nth 1 entry
))
467 (defun tramp-smb-handle-insert-directory
468 (filename switches
&optional wildcard full-directory-p
)
469 "Like `insert-directory' for tramp files.
470 WILDCARD and FULL-DIRECTORY-P are not handled."
471 (setq filename
(expand-file-name filename
))
472 (when (file-directory-p filename
)
473 ;; This check is a little bit strange, but in `dired-add-entry'
474 ;; this function is called with a non-directory ...
475 (setq filename
(file-name-as-directory filename
)))
476 ; (with-parsed-tramp-file-name filename nil
477 (let (user host localname
)
478 (with-parsed-tramp-file-name filename l
479 (setq user l-user host l-host localname l-localname
))
481 (let* ((share (tramp-smb-get-share localname
))
482 (file (tramp-smb-get-localname localname nil
))
483 (entries (tramp-smb-get-file-entries user host share file
)))
485 ;; Delete dummy "" entry, useless entries
487 (if (file-directory-p filename
)
488 (delq (assoc "" entries
) entries
)
489 ;; We just need the only and only entry FILENAME.
490 (list (assoc (file-name-nondirectory filename
) entries
))))
497 (if (string-match "t" switches
)
499 (tramp-smb-time-less-p (nth 3 y
) (nth 3 x
))
501 (string-lessp (nth 0 x
) (nth 0 y
))))))
508 "%10s %3d %-8s %-8s %8s %s %s\n"
513 (if (tramp-smb-time-less-p
514 (tramp-smb-time-subtract (current-time) (nth 3 x
))
515 tramp-smb-half-a-year
)
519 (nth 0 x
))) ; file name
524 (defun tramp-smb-handle-make-directory (dir &optional parents
)
525 "Like `make-directory' for tramp files."
526 (setq dir
(directory-file-name (expand-file-name dir
)))
527 (unless (file-name-absolute-p dir
)
528 (setq dir
(concat default-directory dir
)))
529 ; (with-parsed-tramp-file-name dir nil
530 (let (user host localname
)
531 (with-parsed-tramp-file-name dir l
532 (setq user l-user host l-host localname l-localname
))
534 (let* ((share (tramp-smb-get-share localname
))
535 (ldir (file-name-directory dir
)))
536 ;; Make missing directory parts
537 (when (and parents share
(not (file-directory-p ldir
)))
538 (make-directory ldir parents
))
540 (when (file-directory-p ldir
)
541 (make-directory-internal dir
))
542 (unless (file-directory-p dir
)
543 (error "Couldn't make directory %s" dir
))))))
545 (defun tramp-smb-handle-make-directory-internal (directory)
546 "Like `make-directory-internal' for tramp files."
547 (setq directory
(directory-file-name (expand-file-name directory
)))
548 (unless (file-name-absolute-p directory
)
549 (setq ldir
(concat default-directory directory
)))
550 ; (with-parsed-tramp-file-name directory nil
551 (let (user host localname
)
552 (with-parsed-tramp-file-name directory l
553 (setq user l-user host l-host localname l-localname
))
555 (let* ((share (tramp-smb-get-share localname
))
556 (file (tramp-smb-get-localname localname nil
)))
557 (when (file-directory-p (file-name-directory directory
))
558 (tramp-smb-maybe-open-connection user host share
)
559 (tramp-smb-send-command user host
(format "mkdir \"%s\"" file
)))
560 (unless (file-directory-p directory
)
561 (error "Couldn't make directory %s" directory
))))))
563 (defun tramp-smb-handle-rename-file
564 (filename newname
&optional ok-if-already-exists
)
565 "Like `rename-file' for tramp files."
566 (setq filename
(expand-file-name filename
)
567 newname
(expand-file-name newname
))
569 (let ((tmpfile (file-local-copy filename
)))
573 (rename-file tmpfile newname ok-if-already-exists
)
576 (when (file-directory-p newname
)
577 (setq newname
(expand-file-name
578 (file-name-nondirectory filename
) newname
)))
579 (when (and (not ok-if-already-exists
)
580 (file-exists-p newname
))
581 (error "rename-file: file %s already exists" newname
))
583 ; (with-parsed-tramp-file-name newname nil
584 (let (user host localname
)
585 (with-parsed-tramp-file-name newname l
586 (setq user l-user host l-host localname l-localname
))
588 (let ((share (tramp-smb-get-share localname
))
589 (file (tramp-smb-get-localname localname t
)))
590 (tramp-smb-maybe-open-connection user host share
)
591 (tramp-message-for-buffer
592 nil tramp-smb-method user host
593 5 "Copying file %s to file %s..." filename newname
)
594 (if (tramp-smb-send-command
595 user host
(format "put %s \"%s\"" filename file
))
596 (tramp-message-for-buffer
597 nil tramp-smb-method user host
598 5 "Copying file %s to file %s...done" filename newname
)
599 (error "Cannot rename `%s'" filename
)))))))
601 (delete-file filename
))
603 (defun tramp-smb-handle-write-region
604 (start end filename
&optional append visit lockname confirm
)
605 "Like `write-region' for tramp files."
606 (unless (eq append nil
)
607 (error "Cannot append to file using tramp (`%s')" filename
))
608 (setq filename
(expand-file-name filename
))
609 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
610 (when (and (not (featurep 'xemacs
))
611 confirm
(file-exists-p filename
))
612 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
614 (error "File not overwritten")))
615 ; (with-parsed-tramp-file-name filename nil
616 (let (user host localname
)
617 (with-parsed-tramp-file-name filename l
618 (setq user l-user host l-host localname l-localname
))
620 (let ((share (tramp-smb-get-share localname
))
621 (file (tramp-smb-get-localname localname t
))
622 (curbuf (current-buffer))
623 ;; We use this to save the value of `last-coding-system-used'
624 ;; after writing the tmp file. At the end of the function,
625 ;; we set `last-coding-system-used' to this saved value.
626 ;; This way, any intermediary coding systems used while
627 ;; talking to the remote shell or suchlike won't hose this
628 ;; variable. This approach was snarfed from ange-ftp.el.
631 ;; Write region into a tmp file.
632 (setq tmpfil
(tramp-make-temp-file))
633 ;; We say `no-message' here because we don't want the visited file
634 ;; modtime data to be clobbered from the temp file. We call
635 ;; `set-visited-file-modtime' ourselves later on.
636 (tramp-run-real-handler
638 (if confirm
; don't pass this arg unless defined for backward compat.
639 (list start end tmpfil append
'no-message lockname confirm
)
640 (list start end tmpfil append
'no-message lockname
)))
641 ;; Now, `last-coding-system-used' has the right value. Remember it.
642 (when (boundp 'last-coding-system-used
)
643 (setq coding-system-used last-coding-system-used
))
645 (tramp-smb-maybe-open-connection user host share
)
646 (tramp-message-for-buffer
647 nil tramp-smb-method user host
648 5 "Writing tmp file %s to file %s..." tmpfil filename
)
649 (if (tramp-smb-send-command
650 user host
(format "put %s \"%s\"" tmpfil file
))
651 (tramp-message-for-buffer
652 nil tramp-smb-method user host
653 5 "Writing tmp file %s to file %s...done" tmpfil filename
)
654 (error "Cannot write `%s'" filename
))
657 (unless (equal curbuf
(current-buffer))
658 (error "Buffer has changed from `%s' to `%s'"
659 curbuf
(current-buffer)))
661 (set-visited-file-modtime))
662 ;; Make `last-coding-system-used' have the right value.
663 (when (boundp 'last-coding-system-used
)
664 (setq last-coding-system-used coding-system-used
))))))
667 ;; Internal file name functions
669 (defun tramp-smb-get-share (localname)
670 "Returns the share name of LOCALNAME."
672 (when (string-match "^/?\\([^/]+\\)/" localname
)
673 (match-string 1 localname
))))
675 (defun tramp-smb-get-localname (localname convert
)
676 "Returns the file name of LOCALNAME.
677 If CONVERT is non-nil exchange \"/\" by \"\\\\\"."
679 (let ((res localname
))
682 res
(if (string-match "^/?[^/]+/\\(.*\\)" res
)
685 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
686 (match-string 1 res
) "")
687 (match-string 1 res
))
688 (if (string-match "^/?\\([^/]+\\)$" res
)
692 ;; Sometimes we have discarded `substitute-in-file-name'
693 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" res
)
694 (setq res
(replace-match "$" nil nil res
1)))
698 ;; Share names of a host are cached. It is very unlikely that the
699 ;; shares do change during connection.
700 (defun tramp-smb-get-file-entries (user host share localname
)
701 "Read entries which match LOCALNAME.
702 Either the shares are listed, or the `dir' command is executed.
703 Only entries matching the localname are returned.
704 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
707 (let ((base (or (and (> (length localname
) 0)
708 (string-match "\\([^/]+\\)$" localname
)
709 (regexp-quote (match-string 1 localname
)))
712 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
713 (if (and (not share
) tramp-smb-share-cache
)
714 ;; Return cached shares
715 (setq res tramp-smb-share-cache
)
717 (tramp-smb-maybe-open-connection user host share
)
719 (tramp-smb-send-command
722 (if (zerop (length localname
)) "" (concat "\"" localname
"*\"")))))
723 (goto-char (point-min))
725 (unless (re-search-forward tramp-smb-errors nil t
)
727 (setq entry
(tramp-smb-read-file-entry share
))
729 (when entry
(add-to-list 'res entry
))))
731 ;; Cache share entries
732 (setq tramp-smb-share-cache res
)))
735 ;; Add directory itself
736 (add-to-list 'res
'("" "dr-xr-xr-x" 0 (0 0)))
738 ;; Check for matching entries
740 (lambda (x) (and (string-match base
(nth 0 x
)) x
))
743 ;; Return either a share name (if SHARE is nil), or a file name
745 ;; If shares are listed, the following format is expected
747 ;; \s-\{8,8} - leading spaces
748 ;; \S-\(.*\S-\)\s-* - share name, 14 char
749 ;; \s- - space delimeter
750 ;; \S-+\s-* - type, 8 char, "Disk " expected
751 ;; \(\s-\{2,2\}.*\)? - space delimeter, comment
753 ;; Entries provided by smbclient DIR aren't fully regular.
754 ;; They should have the format
756 ;; \s-\{2,2} - leading spaces
757 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
758 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
759 ;; \s- - space delimeter
760 ;; \s-+[0-9]+ - size, 8 chars, right bound
761 ;; \s-\{2,2\} - space delimeter
762 ;; \w\{3,3\} - weekday
763 ;; \s- - space delimeter
765 ;; \s- - space delimeter
767 ;; \s- - space delimeter
768 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
769 ;; \s- - space delimeter
770 ;; [0-9]\{4,4\} - year
772 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
773 ;; has function display_finfo:
775 ;; d_printf(" %-30s%7.7s %8.0f %s",
777 ;; attrib_string(finfo->mode),
778 ;; (double)finfo->size,
779 ;; asctime(LocalTime(&t)));
781 ;; in Samba 1.9, there's the following code:
783 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
784 ;; CNV_LANG(finfo->name),
785 ;; attrib_string(finfo->mode),
787 ;; asctime(LocalTime(&t))));
790 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
791 ;; available in older Emacsen.
792 ;; * The length of constructs (file name, size) might exceed the default.
793 ;; * File names might contain spaces.
794 ;; * Permissions might be empty.
796 ;; So we try to analyze backwards.
797 (defun tramp-smb-read-file-entry (share)
798 "Parse entry in SMB output buffer.
799 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
800 Result is the list (LOCALNAME MODE SIZE MTIME)."
801 (let ((line (buffer-substring (point) (tramp-point-at-eol)))
802 localname mode size month day hour min sec year mtime
)
807 (when (string-match "^\\s-+\\(\\S-+\\)\\s-+Disk" line
)
808 (setq localname
(match-string 1 line
)
816 (if (string-match "\\([0-9]+\\)$" line
)
817 (setq year
(string-to-number (match-string 1 line
))
818 line
(substring line
0 -
5))
822 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
823 (setq hour
(string-to-number (match-string 1 line
))
824 min
(string-to-number (match-string 2 line
))
825 sec
(string-to-number (match-string 3 line
))
826 line
(substring line
0 -
9))
830 (if (string-match "\\([0-9]+\\)$" line
)
831 (setq day
(string-to-number (match-string 1 line
))
832 line
(substring line
0 -
3))
836 (if (string-match "\\(\\w+\\)$" line
)
837 (setq month
(match-string 1 line
)
838 line
(substring line
0 -
4))
842 (if (string-match "\\(\\w+\\)$" line
)
843 (setq line
(substring line
0 -
5))
847 (if (string-match "\\([0-9]+\\)$" line
)
848 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
849 (setq size
(string-to-number (match-string 1 line
)))
850 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
851 (setq length
(+ length
(match-end 0))))
852 (setq line
(substring line
0 length
)))
855 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID
856 (if (string-match "\\([ADHRSV]+\\)?$" line
)
858 mode
(or (match-string 1 line
) "")
859 mode
(save-match-data (format
861 (if (string-match "D" mode
) "d" "-")
864 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
865 line
(substring line
0 -
7))
869 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
870 (setq localname
(match-string 1 line
))
873 (when (and localname mode size
)
875 (if (and sec min hour day month year
)
878 (cdr (assoc (downcase month
) tramp-smb-parse-time-months
))
881 (list localname mode size mtime
))))
883 ;; Inodes don't exist for SMB files. Therefore we must generate virtual ones.
884 ;; Used in `find-buffer-visiting'.
885 ;; The method applied might be not so efficient (Ange-FTP uses hashes). But
886 ;; performance isn't the major issue given that file transfer will take time.
888 (defun tramp-smb-get-inode (share file
)
889 "Returns the virtual inode number.
890 If it doesn't exist, generate a new one."
891 (let ((string (concat share
"/" (directory-file-name file
))))
892 (unless (assoc string tramp-smb-inodes
)
893 (add-to-list 'tramp-smb-inodes
894 (list string
(length tramp-smb-inodes
))))
895 (nth 1 (assoc string tramp-smb-inodes
))))
898 ;; Connection functions
900 (defun tramp-smb-send-command (user host command
)
901 "Send the COMMAND to USER at HOST (logged into an SMB session).
902 Erases temporary buffer before sending the command. Returns nil if
903 there has been an error message from smbclient."
905 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
907 (tramp-send-command nil tramp-smb-method user host command nil t
)
908 (tramp-smb-wait-for-output user host
)))
910 (defun tramp-smb-maybe-open-connection (user host share
)
911 "Maybe open a connection to HOST, logging in as USER, using `tramp-smb-program'.
912 Does not do anything if a connection is already open, but re-opens the
913 connection if a previous connection has died for some reason."
914 (let ((p (get-buffer-process
915 (tramp-get-buffer nil tramp-smb-method user host
))))
917 (set-buffer (tramp-get-buffer nil tramp-smb-method user host
))
918 ;; Check whether it is still the same share
919 (unless (and p
(processp p
) (string-equal tramp-smb-share share
))
920 (when (and p
(processp p
))
923 ;; If too much time has passed since last command was sent, look
924 ;; whether process is still alive. If it isn't, kill it.
925 (when (and tramp-last-cmd-time
926 (> (tramp-time-diff (current-time) tramp-last-cmd-time
) 60)
927 p
(processp p
) (memq (process-status p
) '(run open
)))
928 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
931 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
932 (when (and p
(processp p
))
934 (tramp-smb-open-connection user host share
))))
936 (defun tramp-smb-open-connection (user host share
)
937 "Open a connection using `tramp-smb-program'.
938 This starts the command `smbclient //HOST/SHARE -U USER', then waits
939 for a remote password prompt. It queries the user for the password,
940 then sends the password to the remote host.
942 Domain names in USER and port numbers in HOST are acknowledged."
945 (let* ((buffer (tramp-get-buffer nil tramp-smb-method user host
))
950 ; Check for domain ("user%domain") and port ("host#port")
951 (when (and user
(string-match "\\(.+\\)%\\(.+\\)" user
))
952 (setq real-user
(or (match-string 1 user
) user
)
953 domain
(match-string 2 user
)))
955 (when (and host
(string-match "\\(.+\\)#\\(.+\\)" host
))
956 (setq real-host
(or (match-string 1 host
) host
)
957 port
(match-string 2 host
)))
960 (setq args
(list (concat "//" real-host
"/" share
)))
961 (setq args
(list "-L" real-host
)))
964 (setq args
(append args
(list "-U" real-user
)))
965 (setq args
(append args
(list "-N"))))
967 (when domain
(setq args
(append args
(list "-W" domain
))))
968 (when port
(setq args
(append args
(list "-p" port
))))
971 (tramp-pre-connection nil tramp-smb-method user host
)
972 (tramp-message 7 "Opening connection for //%s@%s/%s..."
973 user host
(or share
""))
975 (let* ((default-directory (tramp-temporary-file-directory))
976 ;; If we omit the conditional here, then we would use
977 ;; `undecided-dos' in some cases. With the conditional,
978 ;; we use nil in these cases. Which one is right?
979 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
980 (> emacs-major-version
20))
981 tramp-dos-coding-system
))
982 (p (apply #'start-process
(buffer-name buffer
) buffer
983 tramp-smb-program args
)))
985 (tramp-message 9 "Started process %s" (process-command p
))
986 (process-kill-without-query p
)
988 (set-process-sentinel
989 p
(lambda (proc str
) (setq tramp-smb-process-running nil
)))
990 ; If no share is given, the process will terminate
991 (setq tramp-smb-process-running share
992 tramp-smb-share share
)
996 (let ((pw-prompt "Password:"))
997 (tramp-message 9 "Sending password")
998 (tramp-enter-password p pw-prompt
)))
1000 (unless (tramp-smb-wait-for-output user host
)
1001 (error "Cannot open connection //%s@%s/%s"
1002 user host
(or share
"")))))))
1004 ;; We don't use timeouts. If needed, the caller shall wrap around.
1005 (defun tramp-smb-wait-for-output (user host
)
1006 "Wait for output from smbclient command.
1007 Sets position to begin of buffer.
1008 Returns nil if an error message has appeared."
1010 (let ((proc (get-buffer-process (current-buffer)))
1011 (found (progn (goto-char (point-max))
1013 (looking-at tramp-smb-prompt
)))
1016 ;; Algorithm: get waiting output. See if last line contains
1017 ;; tramp-smb-prompt sentinel, or process has exited.
1018 ;; If not, wait a bit and again get waiting output.
1019 (while (and (not found
) tramp-smb-process-running
)
1020 (accept-process-output proc
)
1021 (goto-char (point-max))
1023 (setq found
(looking-at tramp-smb-prompt
)))
1025 ;; There might be pending output. If tramp-smb-prompt sentinel
1026 ;; hasn't been found, the process has died already. We should
1027 ;; give it a chance.
1028 (when (not found
) (accept-process-output nil
1))
1030 ;; Search for errors.
1031 (goto-char (point-min))
1032 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1034 ;; Add output to debug buffer if appropriate.
1035 (when tramp-debug-buffer
1037 (tramp-get-debug-buffer nil tramp-smb-method user host
)
1038 (point-min) (point-max))
1039 (when (and (not found
) tramp-smb-process-running
)
1042 (tramp-get-debug-buffer nil tramp-smb-method user host
))
1043 (goto-char (point-max))
1044 (insert (format "[[Remote prompt `%s' not found]]\n"
1045 tramp-smb-prompt
)))))
1046 (goto-char (point-min))
1047 ;; Return value is whether no error message has appeared.
1051 ;; Snarfed code from time-date.el and parse-time.el
1053 (defconst tramp-smb-half-a-year
'(241 17024)
1054 "Evaluated by \"(days-to-time 183)\".")
1056 (defconst tramp-smb-parse-time-months
'(("jan" .
1) ("feb" .
2) ("mar" .
3)
1057 ("apr" .
4) ("may" .
5) ("jun" .
6)
1058 ("jul" .
7) ("aug" .
8) ("sep" .
9)
1059 ("oct" .
10) ("nov" .
11) ("dec" .
12))
1060 "Alist mapping month names to integers.")
1062 (defun tramp-smb-time-less-p (t1 t2
)
1063 "Say whether time value T1 is less than time value T2."
1064 (unless t1
(setq t1
'(0 0)))
1065 (unless t2
(setq t2
'(0 0)))
1066 (or (< (car t1
) (car t2
))
1067 (and (= (car t1
) (car t2
))
1068 (< (nth 1 t1
) (nth 1 t2
)))))
1070 (defun tramp-smb-time-subtract (t1 t2
)
1071 "Subtract two time values.
1072 Return the difference in the format of a time value."
1073 (unless t1
(setq t1
'(0 0)))
1074 (unless t2
(setq t2
'(0 0)))
1075 (let ((borrow (< (cadr t1
) (cadr t2
))))
1076 (list (- (car t1
) (car t2
) (if borrow
1 0))
1077 (- (+ (if borrow
65536 0) (cadr t1
)) (cadr t2
)))))
1080 ;; `PC-do-completion' touches the returning "$$" by `substitute-in-file-name'.
1081 ;; Must be corrected.
1083 (defadvice PC-do-completion
(around tramp-smb-advice-PC-do-completion activate
)
1084 "Changes \"$\" back to \"$$\" in minibuffer."
1085 (if (funcall PC-completion-as-file-name-predicate
)
1088 ;; Substitute file names
1089 (let* ((beg (or (and (functionp 'minibuffer-prompt-end
) ; Emacs 21
1090 (funcall 'minibuffer-prompt-end
))
1093 (str (substitute-in-file-name (buffer-substring beg end
))))
1094 (delete-region beg end
)
1096 (ad-set-arg 2 (point)))
1098 ;; Do `PC-do-completion' without substitution
1100 (fset 'save
(symbol-function 'substitute-in-file-name
))
1101 (fset 'substitute-in-file-name
(symbol-function 'identity
))
1103 (fset 'substitute-in-file-name
(symbol-function 'save
)))
1106 (let* ((beg (or (and (functionp 'minibuffer-prompt-end
) ; Emacs 21
1107 (funcall 'minibuffer-prompt-end
))
1110 (str (buffer-substring beg end
)))
1111 (delete-region beg end
)
1112 (insert (if (string-match "\\(\\$\\)\\(/\\|$\\)" str
)
1113 (replace-match "$$" nil nil str
1)
1116 ;; No file names. Behave unchanged.
1119 (provide 'tramp-smb
)
1123 ;; * Provide a local smb.conf. The default one might not be readable.
1124 ;; * Error handling in case password is wrong.
1125 ;; * Read password from "~/.netrc".
1126 ;; * Use different buffers for different shares. By this, the password
1127 ;; won't be requested again when changing shares on the same host.
1128 ;; * Return more comprehensive file permission string. Think whether it is
1129 ;; possible to implement `set-file-modes'.
1130 ;; * Handle WILDCARD and FULL-DIRECTORY-P in
1131 ;; `tramp-smb-handle-insert-directory'.
1132 ;; * Handle links (FILENAME.LNK).
1133 ;; * Maybe local tmp files should have the same extension like the original
1134 ;; files. Strange behaviour with jka-compr otherwise?
1135 ;; * Copy files in dired from SMB to another method doesn't work.
1136 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1137 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1138 ;; * Provide variables for debug.
1139 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1142 ;;; tramp-smb.el ends here