1 ;;; tramp-smb.el --- Tramp access functions for SMB servers -*- coding: iso-8859-1; -*-
3 ;; Copyright (C) 2002, 2003, 2004 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-+> \\|^\\s-+Server\\s-+Comment$"
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_ACCOUNT_LOCKED_OUT"
85 "NT_STATUS_BAD_NETWORK_NAME"
86 "NT_STATUS_CANNOT_DELETE"
87 "NT_STATUS_LOGON_FAILURE"
88 "NT_STATUS_NETWORK_ACCESS_DENIED"
89 "NT_STATUS_NO_SUCH_FILE"
90 "NT_STATUS_OBJECT_NAME_INVALID"
91 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
92 "NT_STATUS_SHARING_VIOLATION"
93 "NT_STATUS_WRONG_PASSWORD")
95 "Regexp for possible error strings of SMB servers.
96 Used instead of analyzing error codes of commands.")
98 (defvar tramp-smb-share nil
99 "Holds the share name for the current buffer.
100 This variable is local to each buffer.")
101 (make-variable-buffer-local 'tramp-smb-share)
103 (defvar tramp-smb-share-cache nil
104 "Caches the share names accessible to host related to the current buffer.
105 This variable is local to each buffer.")
106 (make-variable-buffer-local 'tramp-smb-share-cache)
108 (defvar tramp-smb-inodes nil
109 "Keeps virtual inodes numbers for SMB files.")
111 ;; New handlers should be added here.
112 (defconst tramp-smb-file-name-handler-alist
114 ;; `access-file' performed by default handler
115 (add-name-to-file . tramp-smb-handle-copy-file) ;; we're on Windows, honey.
116 ;; `byte-compiler-base-file-name' performed by default handler
117 (copy-file . tramp-smb-handle-copy-file)
118 (delete-directory . tramp-smb-handle-delete-directory)
119 (delete-file . tramp-smb-handle-delete-file)
120 ;; `diff-latest-backup-file' performed by default handler
121 (directory-file-name . tramp-handle-directory-file-name)
122 (directory-files . tramp-smb-handle-directory-files)
123 (directory-files-and-attributes . tramp-smb-handle-directory-files-and-attributes)
124 (dired-call-process . tramp-smb-not-handled)
125 (dired-compress-file . tramp-smb-not-handled)
126 ;; `dired-uncache' performed by default handler
127 ;; `expand-file-name' not necessary because we cannot expand "~/"
128 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
129 (file-attributes . tramp-smb-handle-file-attributes)
130 (file-directory-p . tramp-smb-handle-file-directory-p)
131 (file-executable-p . tramp-smb-handle-file-exists-p)
132 (file-exists-p . tramp-smb-handle-file-exists-p)
133 (file-local-copy . tramp-smb-handle-file-local-copy)
134 (file-modes . tramp-handle-file-modes)
135 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
136 ;; `file-name-as-directory' performed by default handler
137 (file-name-completion . tramp-handle-file-name-completion)
138 (file-name-directory . tramp-handle-file-name-directory)
139 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
140 ;; `file-name-sans-versions' performed by default handler
141 (file-newer-than-file-p . tramp-smb-handle-file-newer-than-file-p)
142 (file-ownership-preserved-p . tramp-smb-not-handled)
143 (file-readable-p . tramp-smb-handle-file-exists-p)
144 (file-regular-p . tramp-handle-file-regular-p)
145 (file-symlink-p . tramp-smb-not-handled)
146 ;; `file-truename' performed by default handler
147 (file-writable-p . tramp-smb-handle-file-writable-p)
148 ;; `find-backup-file-name' performed by default handler
149 ;; `find-file-noselect' performed by default handler
150 ;; `get-file-buffer' performed by default handler
151 (insert-directory . tramp-smb-handle-insert-directory)
152 (insert-file-contents . tramp-handle-insert-file-contents)
153 (load . tramp-handle-load)
154 (make-directory . tramp-smb-handle-make-directory)
155 (make-directory-internal . tramp-smb-handle-make-directory-internal)
156 (make-symbolic-link . tramp-smb-not-handled)
157 (rename-file . tramp-smb-handle-rename-file)
158 (set-file-modes . tramp-smb-not-handled)
159 (set-visited-file-modtime . tramp-smb-not-handled)
160 (shell-command . tramp-smb-not-handled)
161 ;; `substitute-in-file-name' performed by default handler
162 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
163 (vc-registered . tramp-smb-not-handled)
164 (verify-visited-file-modtime . tramp-smb-not-handled)
165 (write-region . tramp-smb-handle-write-region)
167 "Alist of handler functions for Tramp SMB method.
168 Operations not mentioned here will be handled by the default Emacs primitives.")
170 (defun tramp-smb-file-name-p (filename)
171 "Check if it's a filename for SMB servers."
172 (let ((v (tramp-dissect-file-name filename)))
175 (tramp-file-name-multi-method v)
176 (tramp-file-name-method v)
177 (tramp-file-name-user v)
178 (tramp-file-name-host v))
181 (defun tramp-smb-file-name-handler (operation &rest args)
182 "Invoke the SMB related OPERATION.
183 First arg specifies the OPERATION, second arg is a list of arguments to
184 pass to the OPERATION."
185 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
187 (if (eq (cdr fn) 'tramp-smb-not-handled)
188 (apply (cdr fn) operation args)
189 (save-match-data (apply (cdr fn) args)))
190 (tramp-run-real-handler operation args))))
192 (add-to-list 'tramp-foreign-file-name-handler-alist
193 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler))
196 ;; File name primitives
198 (defun tramp-smb-not-handled (operation &rest args)
199 "Default handler for all functions which are disrecarded."
200 (tramp-message 10 "Won't be handled: %s %s" operation args)
203 (defun tramp-smb-handle-copy-file
204 (filename newname &optional ok-if-already-exists keep-date)
205 "Like `copy-file' for tramp files.
206 KEEP-DATE is not handled in case NEWNAME resides on an SMB server."
207 (setq filename (expand-file-name filename)
208 newname (expand-file-name newname))
210 (let ((tmpfile (file-local-copy filename)))
214 (rename-file tmpfile newname ok-if-already-exists)
217 (when (file-directory-p newname)
218 (setq newname (expand-file-name
219 (file-name-nondirectory filename) newname)))
220 (when (and (not ok-if-already-exists)
221 (file-exists-p newname))
222 (error "copy-file: file %s already exists" newname))
224 ; (with-parsed-tramp-file-name newname nil
225 (let (user host localname)
226 (with-parsed-tramp-file-name newname l
227 (setq user l-user host l-host localname l-localname))
229 (let ((share (tramp-smb-get-share localname))
230 (file (tramp-smb-get-localname localname t)))
232 (error "Target `%s' must contain a share name" filename))
233 (tramp-smb-maybe-open-connection user host share)
234 (tramp-message-for-buffer
235 nil tramp-smb-method user host
236 5 "Copying file %s to file %s..." filename newname)
237 (if (tramp-smb-send-command
238 user host (format "put %s \"%s\"" filename file))
239 (tramp-message-for-buffer
240 nil tramp-smb-method user host
241 5 "Copying file %s to file %s...done" filename newname)
242 (error "Cannot copy `%s'" filename))))))))
244 (defun tramp-smb-handle-delete-directory (directory)
245 "Like `delete-directory' for tramp files."
246 (setq directory (directory-file-name (expand-file-name directory)))
247 (unless (file-exists-p directory)
248 (error "Cannot delete non-existing directory `%s'" directory))
249 ; (with-parsed-tramp-file-name directory nil
250 (let (user host localname)
251 (with-parsed-tramp-file-name directory l
252 (setq user l-user host l-host localname l-localname))
254 (let ((share (tramp-smb-get-share localname))
255 (dir (tramp-smb-get-localname (file-name-directory localname) t))
256 (file (file-name-nondirectory localname)))
257 (tramp-smb-maybe-open-connection user host share)
259 (tramp-smb-send-command user host (format "cd \"%s\"" dir))
260 (tramp-smb-send-command user host (format "rmdir \"%s\"" file)))
262 (tramp-smb-send-command user host (format "cd \\"))
264 (tramp-smb-send-command user host (format "cd \\"))
265 (error "Cannot delete directory `%s'" directory))))))
267 (defun tramp-smb-handle-delete-file (filename)
268 "Like `delete-file' for tramp files."
269 (setq filename (expand-file-name filename))
270 (unless (file-exists-p filename)
271 (error "Cannot delete non-existing file `%s'" filename))
272 ; (with-parsed-tramp-file-name filename nil
273 (let (user host localname)
274 (with-parsed-tramp-file-name filename l
275 (setq user l-user host l-host localname l-localname))
277 (let ((share (tramp-smb-get-share localname))
278 (dir (tramp-smb-get-localname (file-name-directory localname) t))
279 (file (file-name-nondirectory localname)))
280 (unless (file-exists-p filename)
281 (error "Cannot delete non-existing file `%s'" filename))
282 (tramp-smb-maybe-open-connection user host share)
284 (tramp-smb-send-command user host (format "cd \"%s\"" dir))
285 (tramp-smb-send-command user host (format "rm \"%s\"" file)))
287 (tramp-smb-send-command user host (format "cd \\"))
289 (tramp-smb-send-command user host (format "cd \\"))
290 (error "Cannot delete file `%s'" filename))))))
292 (defun tramp-smb-handle-directory-files
293 (directory &optional full match nosort)
294 "Like `directory-files' for tramp files."
295 (setq directory (directory-file-name (expand-file-name directory)))
296 ; (with-parsed-tramp-file-name directory nil
297 (let (user host localname)
298 (with-parsed-tramp-file-name directory l
299 (setq user l-user host l-host localname l-localname))
301 (let* ((share (tramp-smb-get-share localname))
302 (file (tramp-smb-get-localname localname nil))
303 (entries (tramp-smb-get-file-entries user host share file)))
304 ;; Just the file names are needed
305 (setq entries (mapcar 'car entries))
306 ;; Discriminate with regexp
310 (mapcar (lambda (x) (when (string-match match x) x))
312 ;; Make absolute localnames if necessary
316 (concat (file-name-as-directory directory) x))
318 ;; Sort them if necessary
319 (unless nosort (setq entries (sort entries 'string-lessp)))
323 (defun tramp-smb-handle-directory-files-and-attributes
324 (directory &optional full match nosort id-format)
325 "Like `directory-files-and-attributes' for tramp files."
328 ;; We cannot call `file-attributes' for backward compatibility reasons.
329 ;; Its optional parameter ID-FORMAT is introduced with Emacs 21.4.
330 (cons x (tramp-smb-handle-file-attributes
331 (if full x (concat (file-name-as-directory directory) x)) id-format)))
332 (directory-files directory full match nosort)))
334 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
335 "Like `file-attributes' for tramp files."
336 ; (with-parsed-tramp-file-name filename nil
337 (let (user host localname)
338 (with-parsed-tramp-file-name filename l
339 (setq user l-user host l-host localname l-localname))
341 (let* ((share (tramp-smb-get-share localname))
342 (file (tramp-smb-get-localname localname nil))
343 (entries (tramp-smb-get-file-entries user host share file))
345 (assoc (file-name-nondirectory file) entries)))
346 (uid (if (and id-format (equal id-format 'string)) "nobody" -1))
347 (gid (if (and id-format (equal id-format 'string)) "nogroup" -1))
348 (inode (tramp-smb-get-inode share file))
349 (device (tramp-get-device nil tramp-smb-method user host)))
353 (list (and (string-match "d" (nth 1 entry))
359 (nth 3 entry) ;5 mtime
361 (nth 2 entry) ;7 size
362 (nth 1 entry) ;8 mode
364 inode ;10 inode number
365 device)))))) ;11 file system number
367 (defun tramp-smb-handle-file-directory-p (filename)
368 "Like `file-directory-p' for tramp files."
369 ; (with-parsed-tramp-file-name filename nil
370 (let (user host localname)
371 (with-parsed-tramp-file-name filename l
372 (setq user l-user host l-host localname l-localname))
374 (let* ((share (tramp-smb-get-share localname))
375 (file (tramp-smb-get-localname localname nil))
376 (entries (tramp-smb-get-file-entries user host share file))
378 (assoc (file-name-nondirectory file) entries))))
380 (string-match "d" (nth 1 entry))
383 (defun tramp-smb-handle-file-exists-p (filename)
384 "Like `file-exists-p' for tramp files."
385 ; (with-parsed-tramp-file-name filename nil
386 (let (user host localname)
387 (with-parsed-tramp-file-name filename l
388 (setq user l-user host l-host localname l-localname))
390 (let* ((share (tramp-smb-get-share localname))
391 (file (tramp-smb-get-localname localname nil))
392 (entries (tramp-smb-get-file-entries user host share file)))
394 (member (file-name-nondirectory file) (mapcar 'car entries))
397 (defun tramp-smb-handle-file-local-copy (filename)
398 "Like `file-local-copy' for tramp files."
399 (with-parsed-tramp-file-name filename nil
401 (let ((share (tramp-smb-get-share localname))
402 (file (tramp-smb-get-localname localname t))
403 (tmpfil (tramp-make-temp-file)))
404 (unless (file-exists-p filename)
405 (error "Cannot make local copy of non-existing file `%s'" filename))
406 (tramp-message-for-buffer
407 nil tramp-smb-method user host
408 5 "Fetching %s to tmp file %s..." filename tmpfil)
409 (tramp-smb-maybe-open-connection user host share)
410 (if (tramp-smb-send-command
411 user host (format "get \"%s\" %s" file tmpfil))
412 (tramp-message-for-buffer
413 nil tramp-smb-method user host
414 5 "Fetching %s to tmp file %s...done" filename tmpfil)
415 (error "Cannot make local copy of file `%s'" filename))
418 ;; This function should return "foo/" for directories and "bar" for
420 (defun tramp-smb-handle-file-name-all-completions (filename directory)
421 "Like `file-name-all-completions' for tramp files."
422 ; (with-parsed-tramp-file-name directory nil
423 (let (user host localname)
424 (with-parsed-tramp-file-name directory l
425 (setq user l-user host l-host localname l-localname))
428 (let* ((share (tramp-smb-get-share localname))
429 (file (tramp-smb-get-localname localname nil))
430 (entries (tramp-smb-get-file-entries user host share file)))
437 (if (string-match "d" (nth 1 x))
438 (file-name-as-directory (nth 0 x))
442 (defun tramp-smb-handle-file-newer-than-file-p (file1 file2)
443 "Like `file-newer-than-file-p' for tramp files."
445 ((not (file-exists-p file1)) nil)
446 ((not (file-exists-p file2)) t)
447 (t (tramp-smb-time-less-p (file-attributes file2)
448 (file-attributes file1)))))
450 (defun tramp-smb-handle-file-writable-p (filename)
451 "Like `file-writable-p' for tramp files."
452 (if (not (file-exists-p filename))
453 (let ((dir (file-name-directory filename)))
454 (and (file-exists-p dir)
455 (file-writable-p dir)))
456 ; (with-parsed-tramp-file-name filename nil
457 (let (user host localname)
458 (with-parsed-tramp-file-name filename l
459 (setq user l-user host l-host localname l-localname))
461 (let* ((share (tramp-smb-get-share localname))
462 (file (tramp-smb-get-localname localname nil))
463 (entries (tramp-smb-get-file-entries user host share file))
465 (assoc (file-name-nondirectory file) entries))))
467 (string-match "w" (nth 1 entry))
470 (defun tramp-smb-handle-insert-directory
471 (filename switches &optional wildcard full-directory-p)
472 "Like `insert-directory' for tramp files.
473 WILDCARD and FULL-DIRECTORY-P are not handled."
474 (setq filename (expand-file-name filename))
475 (when (file-directory-p filename)
476 ;; This check is a little bit strange, but in `dired-add-entry'
477 ;; this function is called with a non-directory ...
478 (setq filename (file-name-as-directory filename)))
479 ; (with-parsed-tramp-file-name filename nil
480 (let (user host localname)
481 (with-parsed-tramp-file-name filename l
482 (setq user l-user host l-host localname l-localname))
484 (let* ((share (tramp-smb-get-share localname))
485 (file (tramp-smb-get-localname localname nil))
486 (entries (tramp-smb-get-file-entries user host share file)))
488 ;; Delete dummy "" entry, useless entries
490 (if (file-directory-p filename)
491 (delq (assoc "" entries) entries)
492 ;; We just need the only and only entry FILENAME.
493 (list (assoc (file-name-nondirectory filename) entries))))
500 (if (string-match "t" switches)
502 (tramp-smb-time-less-p (nth 3 y) (nth 3 x))
504 (string-lessp (nth 0 x) (nth 0 y))))))
511 "%10s %3d %-8s %-8s %8s %s %s\n"
516 (if (tramp-smb-time-less-p
517 (tramp-smb-time-subtract (current-time) (nth 3 x))
518 tramp-smb-half-a-year)
522 (nth 0 x))) ; file name
527 (defun tramp-smb-handle-make-directory (dir &optional parents)
528 "Like `make-directory' for tramp files."
529 (setq dir (directory-file-name (expand-file-name dir)))
530 (unless (file-name-absolute-p dir)
531 (setq dir (concat default-directory dir)))
532 ; (with-parsed-tramp-file-name dir nil
533 (let (user host localname)
534 (with-parsed-tramp-file-name dir l
535 (setq user l-user host l-host localname l-localname))
537 (let* ((share (tramp-smb-get-share localname))
538 (ldir (file-name-directory dir)))
539 ;; Make missing directory parts
540 (when (and parents share (not (file-directory-p ldir)))
541 (make-directory ldir parents))
543 (when (file-directory-p ldir)
544 (make-directory-internal dir))
545 (unless (file-directory-p dir)
546 (error "Couldn't make directory %s" dir))))))
548 (defun tramp-smb-handle-make-directory-internal (directory)
549 "Like `make-directory-internal' for tramp files."
550 (setq directory (directory-file-name (expand-file-name directory)))
551 (unless (file-name-absolute-p directory)
552 (setq directory (concat default-directory directory)))
553 ; (with-parsed-tramp-file-name directory nil
554 (let (user host localname)
555 (with-parsed-tramp-file-name directory l
556 (setq user l-user host l-host localname l-localname))
558 (let* ((share (tramp-smb-get-share localname))
559 (file (tramp-smb-get-localname localname nil)))
560 (when (file-directory-p (file-name-directory directory))
561 (tramp-smb-maybe-open-connection user host share)
562 (tramp-smb-send-command user host (format "mkdir \"%s\"" file)))
563 (unless (file-directory-p directory)
564 (error "Couldn't make directory %s" directory))))))
566 (defun tramp-smb-handle-rename-file
567 (filename newname &optional ok-if-already-exists)
568 "Like `rename-file' for tramp files."
569 (setq filename (expand-file-name filename)
570 newname (expand-file-name newname))
572 (let ((tmpfile (file-local-copy filename)))
576 (rename-file tmpfile newname ok-if-already-exists)
579 (when (file-directory-p newname)
580 (setq newname (expand-file-name
581 (file-name-nondirectory filename) newname)))
582 (when (and (not ok-if-already-exists)
583 (file-exists-p newname))
584 (error "rename-file: file %s already exists" newname))
586 ; (with-parsed-tramp-file-name newname nil
587 (let (user host localname)
588 (with-parsed-tramp-file-name newname l
589 (setq user l-user host l-host localname l-localname))
591 (let ((share (tramp-smb-get-share localname))
592 (file (tramp-smb-get-localname localname t)))
593 (tramp-smb-maybe-open-connection user host share)
594 (tramp-message-for-buffer
595 nil tramp-smb-method user host
596 5 "Copying file %s to file %s..." filename newname)
597 (if (tramp-smb-send-command
598 user host (format "put %s \"%s\"" filename file))
599 (tramp-message-for-buffer
600 nil tramp-smb-method user host
601 5 "Copying file %s to file %s...done" filename newname)
602 (error "Cannot rename `%s'" filename)))))))
604 (delete-file filename))
606 (defun tramp-smb-handle-write-region
607 (start end filename &optional append visit lockname confirm)
608 "Like `write-region' for tramp files."
609 (unless (eq append nil)
610 (error "Cannot append to file using tramp (`%s')" filename))
611 (setq filename (expand-file-name filename))
612 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
613 (when (and (not (featurep 'xemacs))
614 confirm (file-exists-p filename))
615 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
617 (error "File not overwritten")))
618 ; (with-parsed-tramp-file-name filename nil
619 (let (user host localname)
620 (with-parsed-tramp-file-name filename l
621 (setq user l-user host l-host localname l-localname))
623 (let ((share (tramp-smb-get-share localname))
624 (file (tramp-smb-get-localname localname t))
625 (curbuf (current-buffer))
626 ;; We use this to save the value of `last-coding-system-used'
627 ;; after writing the tmp file. At the end of the function,
628 ;; we set `last-coding-system-used' to this saved value.
629 ;; This way, any intermediary coding systems used while
630 ;; talking to the remote shell or suchlike won't hose this
631 ;; variable. This approach was snarfed from ange-ftp.el.
634 ;; Write region into a tmp file.
635 (setq tmpfil (tramp-make-temp-file))
636 ;; We say `no-message' here because we don't want the visited file
637 ;; modtime data to be clobbered from the temp file. We call
638 ;; `set-visited-file-modtime' ourselves later on.
639 (tramp-run-real-handler
641 (if confirm ; don't pass this arg unless defined for backward compat.
642 (list start end tmpfil append 'no-message lockname confirm)
643 (list start end tmpfil append 'no-message lockname)))
644 ;; Now, `last-coding-system-used' has the right value. Remember it.
645 (when (boundp 'last-coding-system-used)
646 (setq coding-system-used last-coding-system-used))
648 (tramp-smb-maybe-open-connection user host share)
649 (tramp-message-for-buffer
650 nil tramp-smb-method user host
651 5 "Writing tmp file %s to file %s..." tmpfil filename)
652 (if (tramp-smb-send-command
653 user host (format "put %s \"%s\"" tmpfil file))
654 (tramp-message-for-buffer
655 nil tramp-smb-method user host
656 5 "Writing tmp file %s to file %s...done" tmpfil filename)
657 (error "Cannot write `%s'" filename))
660 (unless (equal curbuf (current-buffer))
661 (error "Buffer has changed from `%s' to `%s'"
662 curbuf (current-buffer)))
664 (set-visited-file-modtime))
665 ;; Make `last-coding-system-used' have the right value.
666 (when (boundp 'last-coding-system-used)
667 (setq last-coding-system-used coding-system-used))))))
670 ;; Internal file name functions
672 (defun tramp-smb-get-share (localname)
673 "Returns the share name of LOCALNAME."
675 (when (string-match "^/?\\([^/]+\\)/" localname)
676 (match-string 1 localname))))
678 (defun tramp-smb-get-localname (localname convert)
679 "Returns the file name of LOCALNAME.
680 If CONVERT is non-nil exchange \"/\" by \"\\\\\"."
682 (let ((res localname))
685 res (if (string-match "^/?[^/]+/\\(.*\\)" res)
688 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
689 (match-string 1 res) "")
690 (match-string 1 res))
691 (if (string-match "^/?\\([^/]+\\)$" res)
695 ;; Sometimes we have discarded `substitute-in-file-name'
696 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" res)
697 (setq res (replace-match "$" nil nil res 1)))
701 ;; Share names of a host are cached. It is very unlikely that the
702 ;; shares do change during connection.
703 (defun tramp-smb-get-file-entries (user host share localname)
704 "Read entries which match LOCALNAME.
705 Either the shares are listed, or the `dir' command is executed.
706 Only entries matching the localname are returned.
707 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
710 (let ((base (or (and (> (length localname) 0)
711 (string-match "\\([^/]+\\)$" localname)
712 (regexp-quote (match-string 1 localname)))
715 (set-buffer (tramp-get-buffer nil tramp-smb-method user host))
716 (if (and (not share) tramp-smb-share-cache)
717 ;; Return cached shares
718 (setq res tramp-smb-share-cache)
720 (tramp-smb-maybe-open-connection user host share)
722 (tramp-smb-send-command
725 (if (zerop (length localname)) "" (concat "\"" localname "*\"")))))
726 (goto-char (point-min))
728 (unless (re-search-forward tramp-smb-errors nil t)
730 (setq entry (tramp-smb-read-file-entry share))
732 (when entry (add-to-list 'res entry))))
734 ;; Cache share entries
735 (setq tramp-smb-share-cache res)))
737 ;; Add directory itself
738 (add-to-list 'res '("" "drwxrwxrwx" 0 (0 0)))
740 ;; There's a very strange error (debugged with XEmacs 21.4.14)
741 ;; If there's no short delay, it returns nil. No idea about
742 (when (featurep 'xemacs) (sleep-for 0.01))
744 ;; Check for matching entries
746 (lambda (x) (and (string-match base (nth 0 x)) x))
749 ;; Return either a share name (if SHARE is nil), or a file name
751 ;; If shares are listed, the following format is expected
753 ;; \s-\{8,8} - leading spaces
754 ;; \S-\(.*\S-\)\s-* - share name, 14 char
755 ;; \s- - space delimeter
756 ;; \S-+\s-* - type, 8 char, "Disk " expected
757 ;; \(\s-\{2,2\}.*\)? - space delimeter, comment
759 ;; Entries provided by smbclient DIR aren't fully regular.
760 ;; They should have the format
762 ;; \s-\{2,2} - leading spaces
763 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
764 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
765 ;; \s- - space delimeter
766 ;; \s-+[0-9]+ - size, 8 chars, right bound
767 ;; \s-\{2,2\} - space delimeter
768 ;; \w\{3,3\} - weekday
769 ;; \s- - space delimeter
771 ;; \s- - space delimeter
773 ;; \s- - space delimeter
774 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
775 ;; \s- - space delimeter
776 ;; [0-9]\{4,4\} - year
778 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
779 ;; has function display_finfo:
781 ;; d_printf(" %-30s%7.7s %8.0f %s",
783 ;; attrib_string(finfo->mode),
784 ;; (double)finfo->size,
785 ;; asctime(LocalTime(&t)));
787 ;; in Samba 1.9, there's the following code:
789 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
790 ;; CNV_LANG(finfo->name),
791 ;; attrib_string(finfo->mode),
793 ;; asctime(LocalTime(&t))));
796 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
797 ;; available in older Emacsen.
798 ;; * The length of constructs (file name, size) might exceed the default.
799 ;; * File names might contain spaces.
800 ;; * Permissions might be empty.
802 ;; So we try to analyze backwards.
803 (defun tramp-smb-read-file-entry (share)
804 "Parse entry in SMB output buffer.
805 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
806 Result is the list (LOCALNAME MODE SIZE MTIME)."
807 (let ((line (buffer-substring (point) (tramp-point-at-eol)))
808 localname mode size month day hour min sec year mtime)
813 (when (string-match "^\\s-+\\(\\S-+\\)\\s-+Disk" line)
814 (setq localname (match-string 1 line)
822 (if (string-match "\\([0-9]+\\)$" line)
823 (setq year (string-to-number (match-string 1 line))
824 line (substring line 0 -5))
828 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
829 (setq hour (string-to-number (match-string 1 line))
830 min (string-to-number (match-string 2 line))
831 sec (string-to-number (match-string 3 line))
832 line (substring line 0 -9))
836 (if (string-match "\\([0-9]+\\)$" line)
837 (setq day (string-to-number (match-string 1 line))
838 line (substring line 0 -3))
842 (if (string-match "\\(\\w+\\)$" line)
843 (setq month (match-string 1 line)
844 line (substring line 0 -4))
848 (if (string-match "\\(\\w+\\)$" line)
849 (setq line (substring line 0 -5))
853 (if (string-match "\\([0-9]+\\)$" line)
854 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
855 (setq size (string-to-number (match-string 1 line)))
856 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
857 (setq length (+ length (match-end 0))))
858 (setq line (substring line 0 length)))
861 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID
862 (if (string-match "\\([ADHRSV]+\\)?$" line)
864 mode (or (match-string 1 line) "")
865 mode (save-match-data (format
867 (if (string-match "D" mode) "d" "-")
870 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
871 line (substring line 0 -7))
875 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
876 (setq localname (match-string 1 line))
879 (when (and localname mode size)
881 (if (and sec min hour day month year)
884 (cdr (assoc (downcase month) tramp-smb-parse-time-months))
887 (list localname mode size mtime))))
889 ;; Inodes don't exist for SMB files. Therefore we must generate virtual ones.
890 ;; Used in `find-buffer-visiting'.
891 ;; The method applied might be not so efficient (Ange-FTP uses hashes). But
892 ;; performance isn't the major issue given that file transfer will take time.
894 (defun tramp-smb-get-inode (share file)
895 "Returns the virtual inode number.
896 If it doesn't exist, generate a new one."
897 (let ((string (concat share "/" (directory-file-name file))))
898 (unless (assoc string tramp-smb-inodes)
899 (add-to-list 'tramp-smb-inodes
900 (list string (length tramp-smb-inodes))))
901 (nth 1 (assoc string tramp-smb-inodes))))
904 ;; Connection functions
906 (defun tramp-smb-send-command (user host command)
907 "Send the COMMAND to USER at HOST (logged into an SMB session).
908 Erases temporary buffer before sending the command. Returns nil if
909 there has been an error message from smbclient."
911 (set-buffer (tramp-get-buffer nil tramp-smb-method user host))
913 (tramp-send-command nil tramp-smb-method user host command nil t)
914 (tramp-smb-wait-for-output user host)))
916 (defun tramp-smb-maybe-open-connection (user host share)
917 "Maybe open a connection to HOST, logging in as USER, using `tramp-smb-program'.
918 Does not do anything if a connection is already open, but re-opens the
919 connection if a previous connection has died for some reason."
920 (let ((process-connection-type tramp-process-connection-type)
921 (p (get-buffer-process
922 (tramp-get-buffer nil tramp-smb-method user host))))
924 (set-buffer (tramp-get-buffer nil tramp-smb-method user host))
925 ;; Check whether it is still the same share
926 (unless (and p (processp p) (string-equal tramp-smb-share share))
927 (when (and p (processp p))
930 ;; If too much time has passed since last command was sent, look
931 ;; whether process is still alive. If it isn't, kill it.
932 (when (and tramp-last-cmd-time
933 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
934 p (processp p) (memq (process-status p) '(run open)))
935 (unless (and p (processp p) (memq (process-status p) '(run open)))
938 (unless (and p (processp p) (memq (process-status p) '(run open)))
939 (when (and p (processp p))
941 (tramp-smb-open-connection user host share))))
943 (defun tramp-smb-open-connection (user host share)
944 "Open a connection using `tramp-smb-program'.
945 This starts the command `smbclient //HOST/SHARE -U USER', then waits
946 for a remote password prompt. It queries the user for the password,
947 then sends the password to the remote host.
949 Domain names in USER and port numbers in HOST are acknowledged."
952 (let* ((buffer (tramp-get-buffer nil tramp-smb-method user host))
957 ; Check for domain ("user%domain") and port ("host#port")
958 (when (and user (string-match "\\(.+\\)%\\(.+\\)" user))
959 (setq real-user (or (match-string 1 user) user)
960 domain (match-string 2 user)))
962 (when (and host (string-match "\\(.+\\)#\\(.+\\)" host))
963 (setq real-host (or (match-string 1 host) host)
964 port (match-string 2 host)))
967 (setq args (list (concat "//" real-host "/" share)))
968 (setq args (list "-L" real-host )))
971 (setq args (append args (list "-U" real-user)))
972 (setq args (append args (list "-N"))))
974 (when domain (setq args (append args (list "-W" domain))))
975 (when port (setq args (append args (list "-p" port))))
978 (tramp-pre-connection nil tramp-smb-method user host)
979 (tramp-message 7 "Opening connection for //%s@%s/%s..."
980 user host (or share ""))
982 (let* ((default-directory (tramp-temporary-file-directory))
983 ;; If we omit the conditional here, then we would use
984 ;; `undecided-dos' in some cases. With the conditional,
985 ;; we use nil in these cases. Which one is right?
986 (coding-system-for-read (unless (and (not (featurep 'xemacs))
987 (> emacs-major-version 20))
988 tramp-dos-coding-system))
989 (p (apply #'start-process (buffer-name buffer) buffer
990 tramp-smb-program args)))
992 (tramp-message 9 "Started process %s" (process-command p))
993 (process-kill-without-query p)
995 (setq tramp-smb-share share)
999 (let ((pw-prompt "Password:"))
1000 (tramp-message 9 "Sending password")
1001 (tramp-enter-password p pw-prompt)))
1003 (unless (tramp-smb-wait-for-output user host)
1004 (tramp-clear-passwd user host)
1005 (error "Cannot open connection //%s@%s/%s"
1006 user host (or share "")))))))
1008 ;; We don't use timeouts. If needed, the caller shall wrap around.
1009 (defun tramp-smb-wait-for-output (user host)
1010 "Wait for output from smbclient command.
1011 Returns nil if an error message has appeared."
1012 (let ((proc (get-buffer-process (current-buffer)))
1013 (found (progn (goto-char (point-min))
1014 (re-search-forward tramp-smb-prompt nil t)))
1015 (err (progn (goto-char (point-min))
1016 (re-search-forward tramp-smb-errors nil t))))
1018 ;; Algorithm: get waiting output. See if last line contains
1019 ;; tramp-smb-prompt sentinel or tramp-smb-errors strings.
1020 ;; If not, wait a bit and again get waiting output.
1021 (while (and (not found) (not err))
1023 ;; Accept pending output.
1024 (accept-process-output proc)
1026 ;; Search for prompt.
1027 (goto-char (point-min))
1028 (setq found (re-search-forward tramp-smb-prompt nil t))
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)))
1040 ;; Return value is whether no error message has appeared.
1044 ;; Snarfed code from time-date.el and parse-time.el
1046 (defconst tramp-smb-half-a-year '(241 17024)
1047 "Evaluated by \"(days-to-time 183)\".")
1049 (defconst tramp-smb-parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
1050 ("apr" . 4) ("may" . 5) ("jun" . 6)
1051 ("jul" . 7) ("aug" . 8) ("sep" . 9)
1052 ("oct" . 10) ("nov" . 11) ("dec" . 12))
1053 "Alist mapping month names to integers.")
1055 (defun tramp-smb-time-less-p (t1 t2)
1056 "Say whether time value T1 is less than time value T2."
1057 (unless t1 (setq t1 '(0 0)))
1058 (unless t2 (setq t2 '(0 0)))
1059 (or (< (car t1) (car t2))
1060 (and (= (car t1) (car t2))
1061 (< (nth 1 t1) (nth 1 t2)))))
1063 (defun tramp-smb-time-subtract (t1 t2)
1064 "Subtract two time values.
1065 Return the difference in the format of a time value."
1066 (unless t1 (setq t1 '(0 0)))
1067 (unless t2 (setq t2 '(0 0)))
1068 (let ((borrow (< (cadr t1) (cadr t2))))
1069 (list (- (car t1) (car t2) (if borrow 1 0))
1070 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
1073 ;; `PC-do-completion' touches the returning "$$" by `substitute-in-file-name'.
1074 ;; Must be corrected.
1076 (defadvice PC-do-completion (around tramp-smb-advice-PC-do-completion activate)
1077 "Changes \"$\" back to \"$$\" in minibuffer."
1078 (if (funcall PC-completion-as-file-name-predicate)
1081 ;; Substitute file names
1082 (let* ((beg (or (and (functionp 'minibuffer-prompt-end) ; Emacs 21
1083 (funcall 'minibuffer-prompt-end))
1086 (str (substitute-in-file-name (buffer-substring beg end))))
1087 (delete-region beg end)
1089 (ad-set-arg 2 (point)))
1091 ;; Do `PC-do-completion' without substitution
1093 (fset 'save (symbol-function 'substitute-in-file-name))
1094 (fset 'substitute-in-file-name (symbol-function 'identity))
1096 (fset 'substitute-in-file-name (symbol-function 'save)))
1099 (let* ((beg (or (and (functionp 'minibuffer-prompt-end) ; Emacs 21
1100 (funcall 'minibuffer-prompt-end))
1103 (str (buffer-substring beg end)))
1104 (delete-region beg end)
1105 (insert (if (string-match "\\(\\$\\)\\(/\\|$\\)" str)
1106 (replace-match "$$" nil nil str 1)
1109 ;; No file names. Behave unchanged.
1112 (provide 'tramp-smb)
1116 ;; * Provide a local smb.conf. The default one might not be readable.
1117 ;; * Error handling in case password is wrong.
1118 ;; * Read password from "~/.netrc".
1119 ;; * Return more comprehensive file permission string. Think whether it is
1120 ;; possible to implement `set-file-modes'.
1121 ;; * Handle WILDCARD and FULL-DIRECTORY-P in
1122 ;; `tramp-smb-handle-insert-directory'.
1123 ;; * Handle links (FILENAME.LNK).
1124 ;; * Maybe local tmp files should have the same extension like the original
1125 ;; files. Strange behaviour with jka-compr otherwise?
1126 ;; * Copy files in dired from SMB to another method doesn't work.
1127 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1128 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1129 ;; * Provide variables for debug.
1130 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1133 ;;; arch-tag: fcc9dbec-7503-4d73-b638-3c8aa59575f5
1134 ;;; tramp-smb.el ends here