1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
31 (eval-when-compile (require 'cl)) ; block, return
34 ;; Define SMB method ...
36 (defconst tramp-smb-method "smb"
37 "*Method to connect SAMBA and M$ SMB servers.")
39 ;; ... and add it to the method list.
41 (unless (memq system-type '(cygwin windows-nt))
42 (add-to-list 'tramp-methods (cons tramp-smb-method nil)))
44 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
45 ;; a domain in USER, it must be the SMB method.
47 (add-to-list 'tramp-default-method-alist
48 `(nil ,tramp-prefix-domain-regexp ,tramp-smb-method))
50 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
51 ;; the anonymous user is chosen.
53 (add-to-list 'tramp-default-user-alist
54 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
56 ;; Add completion function for SMB method.
57 (tramp-set-completion-function
59 '((tramp-parse-netrc "~/.netrc")))
61 (defcustom tramp-smb-program "smbclient"
62 "*Name of SMB client to run."
66 (defcustom tramp-smb-conf "/dev/null"
67 "*Path of the smb.conf file.
68 If it is nil, no smb.conf will be added to the `tramp-smb-program'
69 call, letting the SMB client use the default one."
71 :type '(choice (const nil) (file :must-match t)))
73 (defvar tramp-smb-version nil
74 "*Version string of the SMB client.")
76 (defconst tramp-smb-prompt "^smb: .+> \\|^\\s-+Server\\s-+Comment$"
77 "Regexp used as prompt in smbclient.")
79 (defconst tramp-smb-errors
80 ;; `regexp-opt' not possible because of first string.
83 '(;; Connection error / timeout / unknown command.
84 "Connection to \\S-+ failed"
85 "Read from server failed, maybe it closed the connection"
86 "Call timed out: server did not respond"
87 "\\S-+: command not found"
88 "Server doesn't support UNIX CIFS calls"
99 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
100 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003).
101 "NT_STATUS_ACCESS_DENIED"
102 "NT_STATUS_ACCOUNT_LOCKED_OUT"
103 "NT_STATUS_BAD_NETWORK_NAME"
104 "NT_STATUS_CANNOT_DELETE"
105 "NT_STATUS_CONNECTION_REFUSED"
106 "NT_STATUS_DIRECTORY_NOT_EMPTY"
107 "NT_STATUS_DUPLICATE_NAME"
108 "NT_STATUS_FILE_IS_A_DIRECTORY"
109 "NT_STATUS_LOGON_FAILURE"
110 "NT_STATUS_NETWORK_ACCESS_DENIED"
111 "NT_STATUS_NOT_IMPLEMENTED"
112 "NT_STATUS_NO_SUCH_FILE"
113 "NT_STATUS_OBJECT_NAME_COLLISION"
114 "NT_STATUS_OBJECT_NAME_INVALID"
115 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
116 "NT_STATUS_SHARING_VIOLATION"
117 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
118 "NT_STATUS_WRONG_PASSWORD")
120 "Regexp for possible error strings of SMB servers.
121 Used instead of analyzing error codes of commands.")
123 (defconst tramp-smb-actions-with-share
124 '((tramp-smb-prompt tramp-action-succeed)
125 (tramp-password-prompt-regexp tramp-action-password)
126 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
127 (tramp-smb-errors tramp-action-permission-denied)
128 (tramp-process-alive-regexp tramp-action-process-alive))
129 "List of pattern/action pairs.
130 This list is used for login to SMB servers.
132 See `tramp-actions-before-shell' for more info.")
134 (defconst tramp-smb-actions-without-share
135 '((tramp-password-prompt-regexp tramp-action-password)
136 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
137 (tramp-smb-errors tramp-action-permission-denied)
138 (tramp-process-alive-regexp tramp-action-out-of-band))
139 "List of pattern/action pairs.
140 This list is used for login to SMB servers.
142 See `tramp-actions-before-shell' for more info.")
144 ;; New handlers should be added here.
145 (defconst tramp-smb-file-name-handler-alist
147 ;; `access-file' performed by default handler.
148 (add-name-to-file . tramp-smb-handle-add-name-to-file)
149 ;; `byte-compiler-base-file-name' performed by default handler.
150 (copy-directory . tramp-smb-handle-copy-directory)
151 (copy-file . tramp-smb-handle-copy-file)
152 (delete-directory . tramp-smb-handle-delete-directory)
153 (delete-file . tramp-smb-handle-delete-file)
154 ;; `diff-latest-backup-file' performed by default handler.
155 (directory-file-name . tramp-handle-directory-file-name)
156 (directory-files . tramp-smb-handle-directory-files)
157 (directory-files-and-attributes
158 . tramp-handle-directory-files-and-attributes)
159 (dired-call-process . ignore)
160 (dired-compress-file . ignore)
161 (dired-uncache . tramp-handle-dired-uncache)
162 (expand-file-name . tramp-smb-handle-expand-file-name)
163 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
164 (file-attributes . tramp-smb-handle-file-attributes)
165 (file-directory-p . tramp-smb-handle-file-directory-p)
166 (file-executable-p . tramp-handle-file-exists-p)
167 (file-exists-p . tramp-handle-file-exists-p)
168 (file-local-copy . tramp-smb-handle-file-local-copy)
169 (file-modes . tramp-handle-file-modes)
170 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
171 (file-name-as-directory . tramp-handle-file-name-as-directory)
172 (file-name-completion . tramp-handle-file-name-completion)
173 (file-name-directory . tramp-handle-file-name-directory)
174 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
175 ;; `file-name-sans-versions' performed by default handler.
176 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
177 (file-ownership-preserved-p . ignore)
178 (file-readable-p . tramp-handle-file-exists-p)
179 (file-regular-p . tramp-handle-file-regular-p)
180 (file-remote-p . tramp-handle-file-remote-p)
181 ;; `file-selinux-context' performed by default handler.
182 (file-symlink-p . tramp-handle-file-symlink-p)
183 ;; `file-truename' performed by default handler.
184 (file-writable-p . tramp-smb-handle-file-writable-p)
185 (find-backup-file-name . tramp-handle-find-backup-file-name)
186 ;; `find-file-noselect' performed by default handler.
187 ;; `get-file-buffer' performed by default handler.
188 (insert-directory . tramp-smb-handle-insert-directory)
189 (insert-file-contents . tramp-handle-insert-file-contents)
190 (load . tramp-handle-load)
191 (make-directory . tramp-smb-handle-make-directory)
192 (make-directory-internal . tramp-smb-handle-make-directory-internal)
193 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
194 (rename-file . tramp-smb-handle-rename-file)
195 (set-file-modes . tramp-smb-handle-set-file-modes)
196 ;; `set-file-selinux-context' performed by default handler.
197 (set-file-times . ignore)
198 (set-visited-file-modtime . ignore)
199 (shell-command . ignore)
200 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
201 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
202 (vc-registered . ignore)
203 (verify-visited-file-modtime . ignore)
204 (write-region . tramp-smb-handle-write-region)
206 "Alist of handler functions for Tramp SMB method.
207 Operations not mentioned here will be handled by the default Emacs primitives.")
210 (defsubst tramp-smb-file-name-p (filename)
211 "Check if it's a filename for SMB servers."
212 (let ((v (tramp-dissect-file-name filename)))
213 (string= (tramp-file-name-method v) tramp-smb-method)))
216 (defun tramp-smb-file-name-handler (operation &rest args)
217 "Invoke the SMB related OPERATION.
218 First arg specifies the OPERATION, second arg is a list of arguments to
219 pass to the OPERATION."
220 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
222 (save-match-data (apply (cdr fn) args))
223 (tramp-run-real-handler operation args))))
226 (unless (memq system-type '(cygwin windows-nt))
227 (add-to-list 'tramp-foreign-file-name-handler-alist
228 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)))
231 ;; File name primitives.
233 (defun tramp-smb-handle-add-name-to-file
234 (filename newname &optional ok-if-already-exists)
235 "Like `add-name-to-file' for Tramp files."
236 (unless (tramp-equal-remote filename newname)
237 (with-parsed-tramp-file-name
238 (if (tramp-tramp-file-p filename) filename newname) nil
241 "add-name-to-file: %s"
242 "only implemented for same method, same user, same host")))
243 (with-parsed-tramp-file-name filename v1
244 (with-parsed-tramp-file-name newname v2
245 (when (file-directory-p filename)
248 "add-name-to-file: %s must not be a directory" filename))
249 (when (and (not ok-if-already-exists)
250 (file-exists-p newname)
251 (not (numberp ok-if-already-exists))
254 "File %s already exists; make it a new name anyway? "
258 "add-name-to-file: file %s already exists" newname))
259 ;; We must also flush the cache of the directory, because
260 ;; `file-attributes' reads the values from there.
261 (tramp-flush-file-property v2 (file-name-directory v2-localname))
262 (tramp-flush-file-property v2 v2-localname)
264 (tramp-smb-send-command
268 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
269 (tramp-smb-get-localname v1)
270 (tramp-smb-get-localname v2)))
273 "error with add-name-to-file, see buffer `%s' for details"
276 (defun tramp-smb-handle-copy-directory
277 (dirname newname &optional keep-date parents)
278 "Like `copy-directory' for Tramp files. KEEP-DATE is not handled."
279 (setq dirname (expand-file-name dirname)
280 newname (expand-file-name newname))
281 (let ((t1 (tramp-tramp-file-p dirname))
282 (t2 (tramp-tramp-file-p newname)))
283 (with-parsed-tramp-file-name (if t1 dirname newname) nil
285 ;; We must use a local temporary directory.
290 tramp-temp-name-prefix
291 (tramp-compat-temporary-file-directory)))))
294 (tramp-compat-copy-directory dirname tmpdir keep-date parents)
295 (tramp-compat-copy-directory tmpdir newname keep-date parents))
296 (tramp-compat-delete-directory tmpdir 'recursive))))
298 ;; We can copy recursively.
300 (let ((prompt (tramp-smb-send-command v "prompt"))
301 (recurse (tramp-smb-send-command v "recurse")))
302 (unless (file-directory-p newname)
303 (make-directory newname parents))
308 (tramp-smb-send-command
309 v (format "cd \"%s\"" (tramp-smb-get-localname v)))
310 (tramp-smb-send-command
311 v (format "lcd \"%s\"" (if t1 newname dirname)))
313 (tramp-smb-send-command v "mget *")
314 (tramp-smb-send-command v "mput *")))
316 (with-current-buffer (tramp-get-connection-buffer v)
317 (goto-char (point-min))
318 (search-forward-regexp tramp-smb-errors nil t)
321 "%s `%s'" (match-string 0) (if t1 dirname newname))))
323 (tramp-smb-send-command
325 "cd %s" (if (tramp-smb-get-cifs-capabilities v) "/" "\\")))
326 ;; Toggle prompt and recurse OFF.
327 (if prompt (tramp-smb-send-command v "prompt"))
328 (if recurse (tramp-smb-send-command v "recurse")))))
330 ;; We must do it file-wise.
332 (tramp-run-real-handler
333 'copy-directory (list dirname newname keep-date parents)))))))
335 (defun tramp-smb-handle-copy-file
336 (filename newname &optional ok-if-already-exists keep-date
337 preserve-uid-gid preserve-selinux-context)
338 "Like `copy-file' for Tramp files.
339 KEEP-DATE is not handled in case NEWNAME resides on an SMB server.
340 PRESERVE-UID-GID is completely ignored."
341 (setq filename (expand-file-name filename)
342 newname (expand-file-name newname))
343 (with-progress-reporter
344 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
345 0 (format "Copying %s to %s" filename newname)
347 (let ((tmpfile (file-local-copy filename)))
352 (rename-file tmpfile newname ok-if-already-exists)
354 (delete-file tmpfile)
355 (signal (car err) (cdr err))))
358 (when (file-directory-p newname)
360 (expand-file-name (file-name-nondirectory filename) newname)))
362 (with-parsed-tramp-file-name newname nil
363 (when (and (not ok-if-already-exists)
364 (file-exists-p newname))
365 (tramp-error v 'file-already-exists newname))
367 ;; We must also flush the cache of the directory, because
368 ;; `file-attributes' reads the values from there.
369 (tramp-flush-file-property v (file-name-directory localname))
370 (tramp-flush-file-property v localname)
371 (unless (tramp-smb-get-share v)
373 v 'file-error "Target `%s' must contain a share name" newname))
374 (unless (tramp-smb-send-command
375 v (format "put \"%s\" \"%s\""
376 filename (tramp-smb-get-localname v)))
377 (tramp-error v 'file-error "Cannot copy `%s'" filename))))))
379 ;; KEEP-DATE handling.
380 (when keep-date (set-file-times newname (nth 5 (file-attributes filename)))))
382 (defun tramp-smb-handle-delete-directory (directory &optional recursive)
383 "Like `delete-directory' for Tramp files."
384 (setq directory (directory-file-name (expand-file-name directory)))
385 (when (file-exists-p directory)
389 (if (file-directory-p file)
390 (tramp-compat-delete-directory file recursive)
392 ;; We do not want to delete "." and "..".
394 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
396 (with-parsed-tramp-file-name directory nil
397 ;; We must also flush the cache of the directory, because
398 ;; `file-attributes' reads the values from there.
399 (tramp-flush-file-property v (file-name-directory localname))
400 (tramp-flush-directory-property v localname)
401 (unless (tramp-smb-send-command
404 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
405 (tramp-smb-get-localname v)))
407 (with-current-buffer (tramp-get-connection-buffer v)
408 (goto-char (point-min))
409 (search-forward-regexp tramp-smb-errors nil t)
411 v 'file-error "%s `%s'" (match-string 0) directory))))))
413 (defun tramp-smb-handle-delete-file (filename &optional trash)
414 "Like `delete-file' for Tramp files."
415 (setq filename (expand-file-name filename))
416 (when (file-exists-p filename)
417 (with-parsed-tramp-file-name filename nil
418 ;; We must also flush the cache of the directory, because
419 ;; `file-attributes' reads the values from there.
420 (tramp-flush-file-property v (file-name-directory localname))
421 (tramp-flush-file-property v localname)
422 (unless (tramp-smb-send-command
425 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
426 (tramp-smb-get-localname v)))
428 (with-current-buffer (tramp-get-connection-buffer v)
429 (goto-char (point-min))
430 (search-forward-regexp tramp-smb-errors nil t)
432 v 'file-error "%s `%s'" (match-string 0) filename))))))
434 (defun tramp-smb-handle-directory-files
435 (directory &optional full match nosort)
436 "Like `directory-files' for Tramp files."
437 (let ((result (mapcar 'directory-file-name
438 (file-name-all-completions "" directory))))
439 ;; Discriminate with regexp.
443 (mapcar (lambda (x) (when (string-match match x) x))
449 (lambda (x) (expand-file-name x directory))
451 ;; Sort them if necessary.
452 (unless nosort (setq result (sort result 'string-lessp)))
456 (defun tramp-smb-handle-expand-file-name (name &optional dir)
457 "Like `expand-file-name' for Tramp files."
458 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
459 (setq dir (or dir default-directory "/"))
460 ;; Unless NAME is absolute, concat DIR and NAME.
461 (unless (file-name-absolute-p name)
462 (setq name (concat (file-name-as-directory dir) name)))
463 ;; If NAME is not a Tramp file, run the real handler.
464 (if (not (tramp-tramp-file-p name))
465 (tramp-run-real-handler 'expand-file-name (list name nil))
467 (with-parsed-tramp-file-name name nil
468 ;; Tilde expansion if necessary. We use the user name as share,
469 ;; which is offen the case in domains.
470 (when (string-match "\\`/?~\\([^/]*\\)" localname)
473 (if (zerop (length (match-string 1 localname)))
474 (tramp-file-name-real-user v)
475 (match-string 1 localname))
477 ;; Make the file name absolute.
478 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
479 (setq localname (concat "/" localname)))
480 ;; No tilde characters in file name, do normal
481 ;; `expand-file-name' (this does "/./" and "/../").
482 (tramp-make-tramp-file-name
484 (tramp-run-real-handler 'expand-file-name (list localname))))))
486 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
487 "Like `file-attributes' for Tramp files."
488 (unless id-format (setq id-format 'integer))
489 (with-parsed-tramp-file-name filename nil
490 (with-file-property v localname (format "file-attributes-%s" id-format)
491 (if (and (tramp-smb-get-share v) (tramp-smb-get-stat-capability v))
492 (tramp-smb-do-file-attributes-with-stat v id-format)
493 ;; Reading just the filename entry via "dir localname" is not
494 ;; possible, because when filename is a directory, some
495 ;; smbclient versions return the content of the directory, and
496 ;; other versions don't. Therefore, the whole content of the
497 ;; upper directory is retrieved, and the entry of the filename
498 ;; is extracted from.
499 (let* ((entries (tramp-smb-get-file-entries
500 (file-name-directory filename)))
501 (entry (assoc (file-name-nondirectory filename) entries))
502 (uid (if (equal id-format 'string) "nobody" -1))
503 (gid (if (equal id-format 'string) "nogroup" -1))
504 (inode (tramp-get-inode v))
505 (device (tramp-get-device v)))
509 (list (and (string-match "d" (nth 1 entry))
515 (nth 3 entry) ;5 mtime
517 (nth 2 entry) ;7 size
518 (nth 1 entry) ;8 mode
520 inode ;10 inode number
521 device))))))) ;11 file system number
523 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
524 "Implement `file-attributes' for Tramp files using stat command."
526 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
527 (with-current-buffer (tramp-get-buffer vec)
528 (let* (size id link uid gid atime mtime ctime mode inode)
529 (when (tramp-smb-send-command
530 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
533 (goto-char (point-min))
534 (unless (re-search-forward tramp-smb-errors nil t)
538 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
539 (setq size (string-to-number (match-string 1))
540 id (if (string-equal "directory" (match-string 2)) t
541 (if (string-equal "symbolic" (match-string 2)) ""))))
543 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
544 (setq inode (string-to-number (match-string 1))
545 link (string-to-number (match-string 2))))
547 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
548 (setq mode (match-string 1)
549 uid (if (equal id-format 'string) (match-string 2)
550 (string-to-number (match-string 2)))
551 gid (if (equal id-format 'string) (match-string 3)
552 (string-to-number (match-string 3)))))
554 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
557 (string-to-number (match-string 6)) ;; sec
558 (string-to-number (match-string 5)) ;; min
559 (string-to-number (match-string 4)) ;; hour
560 (string-to-number (match-string 3)) ;; day
561 (string-to-number (match-string 2)) ;; month
562 (string-to-number (match-string 1))))) ;; year
564 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
567 (string-to-number (match-string 6)) ;; sec
568 (string-to-number (match-string 5)) ;; min
569 (string-to-number (match-string 4)) ;; hour
570 (string-to-number (match-string 3)) ;; day
571 (string-to-number (match-string 2)) ;; month
572 (string-to-number (match-string 1))))) ;; year
574 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
577 (string-to-number (match-string 6)) ;; sec
578 (string-to-number (match-string 5)) ;; min
579 (string-to-number (match-string 4)) ;; hour
580 (string-to-number (match-string 3)) ;; day
581 (string-to-number (match-string 2)) ;; month
582 (string-to-number (match-string 1)))))) ;; year
584 ;; Return the result.
585 (list id link uid gid atime mtime ctime size mode nil inode
586 (tramp-get-device vec)))))))
588 (defun tramp-smb-handle-file-directory-p (filename)
589 "Like `file-directory-p' for Tramp files."
590 (and (file-exists-p filename)
591 (eq ?d (aref (nth 8 (file-attributes filename)) 0))))
593 (defun tramp-smb-handle-file-local-copy (filename)
594 "Like `file-local-copy' for Tramp files."
595 (with-parsed-tramp-file-name filename nil
596 (unless (file-exists-p filename)
599 "Cannot make local copy of non-existing file `%s'" filename))
600 (let ((tmpfile (tramp-compat-make-temp-file filename)))
601 (with-progress-reporter
602 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
603 (unless (tramp-smb-send-command
604 v (format "get \"%s\" \"%s\""
605 (tramp-smb-get-localname v) tmpfile))
606 ;; Oops, an error. We shall cleanup.
607 (delete-file tmpfile)
609 v 'file-error "Cannot make local copy of file `%s'" filename)))
612 ;; This function should return "foo/" for directories and "bar" for
614 (defun tramp-smb-handle-file-name-all-completions (filename directory)
615 "Like `file-name-all-completions' for Tramp files."
618 (with-parsed-tramp-file-name directory nil
619 (with-file-property v localname "file-name-all-completions"
621 (let ((entries (tramp-smb-get-file-entries directory)))
625 (if (string-match "d" (nth 1 x))
626 (file-name-as-directory (nth 0 x))
630 (defun tramp-smb-handle-file-writable-p (filename)
631 "Like `file-writable-p' for Tramp files."
632 (if (file-exists-p filename)
633 (string-match "w" (or (nth 8 (file-attributes filename)) ""))
634 (let ((dir (file-name-directory filename)))
635 (and (file-exists-p dir)
636 (file-writable-p dir)))))
638 (defun tramp-smb-handle-insert-directory
639 (filename switches &optional wildcard full-directory-p)
640 "Like `insert-directory' for Tramp files."
641 (setq filename (expand-file-name filename))
643 ;; Called from `dired-add-entry'.
644 (setq filename (file-name-as-directory filename))
645 (setq filename (directory-file-name filename)))
646 (with-parsed-tramp-file-name filename nil
648 (let ((base (file-name-nondirectory filename))
649 ;; We should not destroy the cache entry.
650 (entries (copy-sequence
651 (tramp-smb-get-file-entries
652 (file-name-directory filename)))))
655 (string-match "\\." base)
656 (setq base (replace-match "\\\\." nil nil base))
657 (string-match "\\*" base)
658 (setq base (replace-match ".*" nil nil base))
659 (string-match "\\?" base)
660 (setq base (replace-match ".?" nil nil base)))
666 (if (or wildcard (zerop (length base)))
667 ;; Check for matching entries.
671 (format "^%s" base) (nth 0 x))
674 ;; We just need the only and only entry FILENAME.
675 (list (assoc base entries)))))
682 (if (string-match "t" switches)
684 (tramp-time-less-p (nth 3 y) (nth 3 x))
686 (string-lessp (nth 0 x) (nth 0 y))))))
688 ;; Handle "-F" switch.
689 (when (string-match "F" switches)
692 (when (not (zerop (length (car x))))
694 ((char-equal ?d (string-to-char (nth 1 x)))
695 (setcar x (concat (car x) "/")))
696 ((char-equal ?x (string-to-char (nth 1 x)))
697 (setcar x (concat (car x) "*"))))))
703 (when (not (zerop (length (nth 0 x))))
705 (when (tramp-smb-get-stat-capability v)
707 (file-attributes filename 'string)))))
710 "%10s %3d %-8s %-8s %8s %s "
711 (or (nth 8 attr) (nth 1 x)) ; mode
712 (or (nth 1 attr) 1) ; inode
713 (or (nth 2 attr) "nobody") ; uid
714 (or (nth 3 attr) "nogroup") ; gid
715 (or (nth 7 attr) (nth 2 x)) ; size
717 (if (tramp-time-less-p
718 (tramp-time-subtract (current-time) (nth 3 x))
723 ;; We mark the file name. The inserted name could be
724 ;; from somewhere else, so we use the relative file
725 ;; name of `default-directory'.
726 (let ((start (point)))
732 (nth 0 x) (file-name-directory filename)))))
733 (put-text-property start (1- (point)) 'dired-filename t))
735 (beginning-of-line))))
738 (defun tramp-smb-handle-make-directory (dir &optional parents)
739 "Like `make-directory' for Tramp files."
740 (setq dir (directory-file-name (expand-file-name dir)))
741 (unless (file-name-absolute-p dir)
742 (setq dir (expand-file-name dir default-directory)))
743 (with-parsed-tramp-file-name dir nil
745 (let* ((ldir (file-name-directory dir)))
746 ;; Make missing directory parts.
748 (tramp-smb-get-share v)
749 (not (file-directory-p ldir)))
750 (make-directory ldir parents))
752 (when (file-directory-p ldir)
753 (make-directory-internal dir))
754 (unless (file-directory-p dir)
755 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
757 (defun tramp-smb-handle-make-directory-internal (directory)
758 "Like `make-directory-internal' for Tramp files."
759 (setq directory (directory-file-name (expand-file-name directory)))
760 (unless (file-name-absolute-p directory)
761 (setq directory (expand-file-name directory default-directory)))
762 (with-parsed-tramp-file-name directory nil
764 (let* ((file (tramp-smb-get-localname v)))
765 (when (file-directory-p (file-name-directory directory))
766 (tramp-smb-send-command
768 (if (tramp-smb-get-cifs-capabilities v)
770 "posix_mkdir \"%s\" %s"
771 file (tramp-compat-decimal-to-octal (default-file-modes)))
772 (format "mkdir \"%s\"" file)))
773 ;; We must also flush the cache of the directory, because
774 ;; `file-attributes' reads the values from there.
775 (tramp-flush-file-property v (file-name-directory localname))
776 (tramp-flush-file-property v localname))
777 (unless (file-directory-p directory)
779 v 'file-error "Couldn't make directory %s" directory))))))
781 (defun tramp-smb-handle-make-symbolic-link
782 (filename linkname &optional ok-if-already-exists)
783 "Like `make-symbolic-link' for Tramp files.
784 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
785 the symlink. If LINKNAME is a Tramp file, only the localname component is
786 used as the target of the symlink.
788 If LINKNAME is a Tramp file and the localname component is relative, then
789 it is expanded first, before the localname component is taken. Note that
790 this can give surprising results if the user/host for the source and
791 target of the symlink differ."
792 (unless (tramp-equal-remote filename linkname)
793 (with-parsed-tramp-file-name
794 (if (tramp-tramp-file-p filename) filename linkname) nil
797 "make-symbolic-link: %s"
798 "only implemented for same method, same user, same host")))
799 (with-parsed-tramp-file-name filename v1
800 (with-parsed-tramp-file-name linkname v2
801 (when (file-directory-p filename)
804 "make-symbolic-link: %s must not be a directory" filename))
805 (when (and (not ok-if-already-exists)
806 (file-exists-p linkname)
807 (not (numberp ok-if-already-exists))
810 "File %s already exists; make it a new name anyway? "
814 "make-symbolic-link: file %s already exists" linkname))
815 (unless (tramp-smb-get-cifs-capabilities v1)
816 (tramp-error v2 'file-error "make-symbolic-link not supported"))
817 ;; We must also flush the cache of the directory, because
818 ;; `file-attributes' reads the values from there.
819 (tramp-flush-file-property v2 (file-name-directory v2-localname))
820 (tramp-flush-file-property v2 v2-localname)
822 (tramp-smb-send-command
825 "symlink \"%s\" \"%s\""
826 (tramp-smb-get-localname v1)
827 (tramp-smb-get-localname v2)))
830 "error with make-symbolic-link, see buffer `%s' for details"
833 (defun tramp-smb-handle-rename-file
834 (filename newname &optional ok-if-already-exists)
835 "Like `rename-file' for Tramp files."
836 (setq filename (expand-file-name filename)
837 newname (expand-file-name newname))
838 (with-progress-reporter
839 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
840 0 (format "Renaming %s to %s" filename newname)
842 (let ((tmpfile (file-local-copy filename)))
847 (rename-file tmpfile newname ok-if-already-exists)
849 (delete-file tmpfile)
850 (signal (car err) (cdr err))))
853 (when (file-directory-p newname)
854 (setq newname (expand-file-name
855 (file-name-nondirectory filename) newname)))
857 (with-parsed-tramp-file-name newname nil
858 (when (and (not ok-if-already-exists)
859 (file-exists-p newname))
860 (tramp-error v 'file-already-exists newname))
861 ;; We must also flush the cache of the directory, because
862 ;; `file-attributes' reads the values from there.
863 (tramp-flush-file-property v (file-name-directory localname))
864 (tramp-flush-file-property v localname)
865 (unless (tramp-smb-send-command
866 v (format "put %s \"%s\""
867 filename (tramp-smb-get-localname v)))
868 (tramp-error v 'file-error "Cannot rename `%s'" filename)))))
870 (delete-file filename)))
872 (defun tramp-smb-handle-set-file-modes (filename mode)
873 "Like `set-file-modes' for Tramp files."
874 (with-parsed-tramp-file-name filename nil
875 (when (tramp-smb-get-cifs-capabilities v)
876 (tramp-flush-file-property v localname)
877 (unless (tramp-smb-send-command
878 v (format "chmod \"%s\" %s"
879 (tramp-smb-get-localname v)
880 (tramp-compat-decimal-to-octal mode)))
882 v 'file-error "Error while changing file's mode %s" filename)))))
884 (defun tramp-smb-handle-substitute-in-file-name (filename)
885 "Like `handle-substitute-in-file-name' for Tramp files.
886 \"//\" substitutes only in the local filename part. Catches
887 errors for shares like \"C$/\", which are common in Microsoft Windows."
888 (with-parsed-tramp-file-name filename nil
889 ;; Ignore in LOCALNAME everything before "//".
890 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
892 (concat (file-remote-p filename)
893 (replace-match "\\1" nil nil localname)))))
895 (tramp-run-real-handler 'substitute-in-file-name (list filename))
898 (defun tramp-smb-handle-write-region
899 (start end filename &optional append visit lockname confirm)
900 "Like `write-region' for Tramp files."
901 (setq filename (expand-file-name filename))
902 (with-parsed-tramp-file-name filename nil
903 (unless (eq append nil)
905 v 'file-error "Cannot append to file using Tramp (`%s')" filename))
906 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
907 (when (and (not (featurep 'xemacs))
908 confirm (file-exists-p filename))
909 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
911 (tramp-error v 'file-error "File not overwritten")))
912 ;; We must also flush the cache of the directory, because
913 ;; `file-attributes' reads the values from there.
914 (tramp-flush-file-property v (file-name-directory localname))
915 (tramp-flush-file-property v localname)
916 (let ((curbuf (current-buffer))
917 (tmpfile (tramp-compat-make-temp-file filename)))
918 ;; We say `no-message' here because we don't want the visited file
919 ;; modtime data to be clobbered from the temp file. We call
920 ;; `set-visited-file-modtime' ourselves later on.
921 (tramp-run-real-handler
923 (if confirm ; don't pass this arg unless defined for backward compat.
924 (list start end tmpfile append 'no-message lockname confirm)
925 (list start end tmpfile append 'no-message lockname)))
927 (with-progress-reporter
928 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
930 (unless (tramp-smb-send-command
931 v (format "put %s \"%s\""
932 tmpfile (tramp-smb-get-localname v)))
933 (tramp-error v 'file-error "Cannot write `%s'" filename))
934 (delete-file tmpfile)))
936 (unless (equal curbuf (current-buffer))
939 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
941 (set-visited-file-modtime)))))
944 ;; Internal file name functions.
946 (defun tramp-smb-get-share (vec)
947 "Returns the share name of LOCALNAME."
949 (let ((localname (tramp-file-name-localname vec)))
950 (when (string-match "^/?\\([^/]+\\)/" localname)
951 (match-string 1 localname)))))
953 (defun tramp-smb-get-localname (vec)
954 "Returns the file name of LOCALNAME.
955 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
957 (let ((localname (tramp-file-name-localname vec)))
960 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
961 ;; There is a share, sparated by "/".
962 (if (not (tramp-smb-get-cifs-capabilities vec))
964 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
965 (match-string 1 localname) "")
966 (match-string 1 localname))
967 ;; There is just a share.
968 (if (string-match "^/?\\([^/]+\\)$" localname)
969 (match-string 1 localname)
972 ;; Sometimes we have discarded `substitute-in-file-name'.
973 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
974 (setq localname (replace-match "$" nil nil localname 1)))
978 ;; Share names of a host are cached. It is very unlikely that the
979 ;; shares do change during connection.
980 (defun tramp-smb-get-file-entries (directory)
981 "Read entries which match DIRECTORY.
982 Either the shares are listed, or the `dir' command is executed.
983 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
984 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
985 (setq localname (or localname "/"))
986 (with-file-property v localname "file-entries"
987 (with-current-buffer (tramp-get-buffer v)
988 (let* ((share (tramp-smb-get-share v))
989 (cache (tramp-get-connection-property v "share-cache" nil))
992 (if (and (not share) cache)
993 ;; Return cached shares.
998 (tramp-smb-send-command
999 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1000 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1001 (tramp-smb-maybe-open-connection v))
1003 ;; Loop the listing.
1004 (goto-char (point-min))
1005 (if (re-search-forward tramp-smb-errors nil t)
1006 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1008 (setq entry (tramp-smb-read-file-entry share))
1010 (when entry (add-to-list 'res entry))))
1012 ;; Cache share entries.
1014 (tramp-set-connection-property v "share-cache" res)))
1016 ;; Add directory itself.
1017 (add-to-list 'res '("" "drwxrwxrwx" 0 (0 0)))
1019 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1020 ;; If there's no short delay, it returns nil. No idea about.
1021 (when (featurep 'xemacs) (sleep-for 0.01))
1026 ;; Return either a share name (if SHARE is nil), or a file name.
1028 ;; If shares are listed, the following format is expected:
1030 ;; Disk| - leading spaces
1031 ;; [^|]+| - share name, 14 char
1034 ;; Entries provided by smbclient DIR aren't fully regular.
1035 ;; They should have the format
1037 ;; \s-\{2,2} - leading spaces
1038 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1039 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1040 ;; \s- - space delimeter
1041 ;; \s-+[0-9]+ - size, 8 chars, right bound
1042 ;; \s-\{2,2\} - space delimeter
1043 ;; \w\{3,3\} - weekday
1044 ;; \s- - space delimeter
1045 ;; \w\{3,3\} - month
1046 ;; \s- - space delimeter
1048 ;; \s- - space delimeter
1049 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1050 ;; \s- - space delimeter
1051 ;; [0-9]\{4,4\} - year
1053 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1054 ;; has function display_finfo:
1056 ;; d_printf(" %-30s%7.7s %8.0f %s",
1058 ;; attrib_string(finfo->mode),
1059 ;; (double)finfo->size,
1060 ;; asctime(LocalTime(&t)));
1062 ;; in Samba 1.9, there's the following code:
1064 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1065 ;; CNV_LANG(finfo->name),
1066 ;; attrib_string(finfo->mode),
1068 ;; asctime(LocalTime(&t))));
1071 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1072 ;; available in older Emacsen.
1073 ;; * The length of constructs (file name, size) might exceed the default.
1074 ;; * File names might contain spaces.
1075 ;; * Permissions might be empty.
1077 ;; So we try to analyze backwards.
1078 (defun tramp-smb-read-file-entry (share)
1079 "Parse entry in SMB output buffer.
1080 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1081 Result is the list (LOCALNAME MODE SIZE MTIME)."
1082 ;; We are called from `tramp-smb-get-file-entries', which sets the
1084 (let ((line (buffer-substring (point) (point-at-eol)))
1085 localname mode size month day hour min sec year mtime)
1089 ;; Read share entries.
1090 (when (string-match "^Disk|\\([^|]+\\)|" line)
1091 (setq localname (match-string 1 line)
1099 (if (string-match "\\([0-9]+\\)$" line)
1100 (setq year (string-to-number (match-string 1 line))
1101 line (substring line 0 -5))
1105 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1106 (setq hour (string-to-number (match-string 1 line))
1107 min (string-to-number (match-string 2 line))
1108 sec (string-to-number (match-string 3 line))
1109 line (substring line 0 -9))
1113 (if (string-match "\\([0-9]+\\)$" line)
1114 (setq day (string-to-number (match-string 1 line))
1115 line (substring line 0 -3))
1119 (if (string-match "\\(\\w+\\)$" line)
1120 (setq month (match-string 1 line)
1121 line (substring line 0 -4))
1125 (if (string-match "\\(\\w+\\)$" line)
1126 (setq line (substring line 0 -5))
1130 (if (string-match "\\([0-9]+\\)$" line)
1131 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1132 (setq size (string-to-number (match-string 1 line)))
1133 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1134 (setq length (+ length (match-end 0))))
1135 (setq line (substring line 0 length)))
1138 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1139 (if (string-match "\\([ADHRSV]+\\)?$" line)
1141 mode (or (match-string 1 line) "")
1142 mode (save-match-data (format
1144 (if (string-match "D" mode) "d" "-")
1147 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1148 line (substring line 0 -6))
1152 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1153 (setq localname (match-string 1 line))
1156 (when (and localname mode size)
1158 (if (and sec min hour day month year)
1161 (cdr (assoc (downcase month) tramp-parse-time-months))
1164 (list localname mode size mtime))))
1166 (defun tramp-smb-get-cifs-capabilities (vec)
1167 "Check, whether the SMB server supports POSIX commands."
1168 ;; When we are not logged in yet, we return nil.
1169 (if (let ((p (tramp-get-connection-process vec)))
1170 (and p (processp p) (memq (process-status p) '(run open))))
1171 (with-connection-property
1172 (tramp-get-connection-process vec) "cifs-capabilities"
1174 (when (tramp-smb-send-command vec "posix")
1175 (with-current-buffer (tramp-get-buffer vec)
1176 (goto-char (point-min))
1178 (re-search-forward "Server supports CIFS capabilities" nil t)
1182 (buffer-substring (point) (point-at-eol)) nil t)))))))))
1184 (defun tramp-smb-get-stat-capability (vec)
1185 "Check, whether the SMB server supports the STAT command."
1186 ;; When we are not logged in yet, we return nil.
1187 (if (let ((p (tramp-get-connection-process vec)))
1188 (and p (processp p) (memq (process-status p) '(run open))))
1189 (with-connection-property
1190 (tramp-get-connection-process vec) "stat-capability"
1191 (tramp-smb-send-command vec "stat ."))))
1194 ;; Connection functions.
1196 (defun tramp-smb-send-command (vec command)
1197 "Send the COMMAND to connection VEC.
1198 Returns nil if there has been an error message from smbclient."
1199 (tramp-smb-maybe-open-connection vec)
1200 (tramp-message vec 6 "%s" command)
1201 (tramp-send-string vec command)
1202 (tramp-smb-wait-for-output vec))
1204 (defun tramp-smb-maybe-open-connection (vec)
1205 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1206 Does not do anything if a connection is already open, but re-opens the
1207 connection if a previous connection has died for some reason."
1208 (let* ((share (tramp-smb-get-share vec))
1209 (buf (tramp-get-buffer vec))
1210 (p (get-buffer-process buf)))
1212 ;; Check whether we still have the same smbclient version.
1213 ;; Otherwise, we must delete the connection cache, because
1214 ;; capabilities migh have changed.
1215 (unless (processp p)
1216 (let ((default-directory (tramp-compat-temporary-file-directory))
1217 (command (concat tramp-smb-program " -V")))
1219 (unless tramp-smb-version
1220 (unless (executable-find tramp-smb-program)
1223 "Cannot find command %s in %s" tramp-smb-program exec-path))
1224 (setq tramp-smb-version (shell-command-to-string command))
1225 (tramp-message vec 6 command)
1226 (tramp-message vec 6 "\n%s" tramp-smb-version)
1227 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1228 (setq tramp-smb-version
1229 (replace-match "" nil nil tramp-smb-version))))
1231 (unless (string-equal
1233 (tramp-get-connection-property
1234 vec "smbclient-version" tramp-smb-version))
1235 (tramp-flush-directory-property vec "")
1236 (tramp-flush-connection-property vec))
1238 (tramp-set-connection-property
1239 vec "smbclient-version" tramp-smb-version)))
1241 ;; If too much time has passed since last command was sent, look
1242 ;; whether there has been an error message; maybe due to
1243 ;; connection timeout.
1244 (with-current-buffer buf
1245 (goto-char (point-min))
1246 (when (and (> (tramp-time-diff
1248 (tramp-get-connection-property
1249 p "last-cmd-time" '(0 0 0)))
1251 p (processp p) (memq (process-status p) '(run open))
1252 (re-search-forward tramp-smb-errors nil t))
1256 ;; Check whether it is still the same share.
1258 (and p (processp p) (memq (process-status p) '(run open))
1261 (tramp-get-connection-property p "smb-share" "")))
1264 ;; There might be unread output from checking for share names.
1265 (when buf (with-current-buffer buf (erase-buffer)))
1266 (when (and p (processp p)) (delete-process p))
1268 (let* ((user (tramp-file-name-user vec))
1269 (host (tramp-file-name-host vec))
1270 (real-user (tramp-file-name-real-user vec))
1271 (real-host (tramp-file-name-real-host vec))
1272 (domain (tramp-file-name-domain vec))
1273 (port (tramp-file-name-port vec))
1277 (setq args (list (concat "//" real-host "/" share)))
1278 (setq args (list "-g" "-L" real-host )))
1280 (if (not (zerop (length real-user)))
1281 (setq args (append args (list "-U" real-user)))
1282 (setq args (append args (list "-N"))))
1284 (when domain (setq args (append args (list "-W" domain))))
1285 (when port (setq args (append args (list "-p" port))))
1286 (when tramp-smb-conf
1287 (setq args (append args (list "-s" tramp-smb-conf))))
1290 (with-progress-reporter
1292 (format "Opening connection for //%s%s/%s"
1293 (if (not (zerop (length user))) (concat user "@") "")
1296 (let* ((coding-system-for-read nil)
1297 (process-connection-type tramp-process-connection-type)
1298 (p (let ((default-directory
1299 (tramp-compat-temporary-file-directory)))
1300 (apply #'start-process
1301 (tramp-buffer-name vec) (tramp-get-buffer vec)
1302 tramp-smb-program args))))
1305 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1306 (tramp-compat-set-process-query-on-exit-flag p nil)
1308 ;; Set variables for computing the prompt for reading password.
1309 (setq tramp-current-method tramp-smb-method
1310 tramp-current-user user
1311 tramp-current-host host)
1313 ;; Play login scenario.
1314 (tramp-process-actions
1317 tramp-smb-actions-with-share
1318 tramp-smb-actions-without-share))
1320 ;; Check server version.
1321 (with-current-buffer (tramp-get-connection-buffer vec)
1322 (goto-char (point-min))
1323 (search-forward-regexp
1324 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]" nil t)
1325 (let ((smbserver-version (match-string 0)))
1329 (tramp-get-connection-property
1330 vec "smbserver-version" smbserver-version))
1331 (tramp-flush-directory-property vec "")
1332 (tramp-flush-connection-property vec))
1333 (tramp-set-connection-property
1334 vec "smbserver-version" smbserver-version)))
1336 ;; Set chunksize. Otherwise, `tramp-send-string' might
1338 (tramp-set-connection-property p "smb-share" share)
1339 (tramp-set-connection-property
1340 p "chunksize" tramp-chunksize))))))))
1342 ;; We don't use timeouts. If needed, the caller shall wrap around.
1343 (defun tramp-smb-wait-for-output (vec)
1344 "Wait for output from smbclient command.
1345 Returns nil if an error message has appeared."
1346 (with-current-buffer (tramp-get-buffer vec)
1347 (let ((p (get-buffer-process (current-buffer)))
1348 (found (progn (goto-char (point-min))
1349 (re-search-forward tramp-smb-prompt nil t)))
1350 (err (progn (goto-char (point-min))
1351 (re-search-forward tramp-smb-errors nil t))))
1353 ;; Algorithm: get waiting output. See if last line contains
1354 ;; tramp-smb-prompt sentinel or tramp-smb-errors strings.
1355 ;; If not, wait a bit and again get waiting output.
1356 (while (and (not found) (not err))
1358 ;; Accept pending output.
1359 (tramp-accept-process-output p)
1361 ;; Search for prompt.
1362 (goto-char (point-min))
1363 (setq found (re-search-forward tramp-smb-prompt nil t))
1365 ;; Search for errors.
1366 (goto-char (point-min))
1367 (setq err (re-search-forward tramp-smb-errors nil t)))
1369 ;; When the process is still alive, read pending output.
1370 (while (and (not found) (memq (process-status p) '(run open)))
1372 ;; Accept pending output.
1373 (tramp-accept-process-output p)
1375 ;; Search for prompt.
1376 (goto-char (point-min))
1377 (setq found (re-search-forward tramp-smb-prompt nil t)))
1379 ;; Return value is whether no error message has appeared.
1380 (tramp-message vec 6 "\n%s" (buffer-string))
1383 (add-hook 'tramp-unload-hook
1385 (unload-feature 'tramp-smb 'force)))
1387 (provide 'tramp-smb)
1391 ;; * Error handling in case password is wrong.
1392 ;; * Read password from "~/.netrc".
1393 ;; * Return more comprehensive file permission string.
1394 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1395 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1396 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1398 ;; * Make it multi-hop capable.
1400 ;;; tramp-smb.el ends here