1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
30 (eval-when-compile (require 'cl)) ; block, return
33 ;; Define SMB method ...
35 (defconst tramp-smb-method "smb"
36 "Method to connect SAMBA and M$ SMB servers.")
38 ;; ... and add it to the method list.
40 (unless (memq system-type '(cygwin windows-nt))
41 (add-to-list 'tramp-methods
43 ;; We define an empty command, because `tramp-smb-call-winexe'
44 ;; opens already the powershell. Used in `tramp-handle-shell-command'.
45 (tramp-remote-shell "")
46 ;; This is just a guess. We don't know whether the share "C$"
47 ;; is available for public use, and whether the user has write
49 (tramp-tmpdir "/C$/Temp"))))
51 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
52 ;; a domain in USER, it must be the SMB method.
54 (add-to-list 'tramp-default-method-alist
55 `(nil ,tramp-prefix-domain-regexp ,tramp-smb-method))
57 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
58 ;; the anonymous user is chosen.
60 (add-to-list 'tramp-default-user-alist
61 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
63 ;; Add completion function for SMB method.
65 (eval-after-load 'tramp
66 '(tramp-set-completion-function
68 '((tramp-parse-netrc "~/.netrc"))))
70 (defcustom tramp-smb-program "smbclient"
71 "Name of SMB client to run."
75 (defcustom tramp-smb-conf "/dev/null"
76 "Path of the smb.conf file.
77 If it is nil, no smb.conf will be added to the `tramp-smb-program'
78 call, letting the SMB client use the default one."
80 :type '(choice (const nil) (file :must-match t)))
82 (defvar tramp-smb-version nil
83 "Version string of the SMB client.")
85 (defconst tramp-smb-server-version
86 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
87 "Regexp of SMB server identification.")
89 (defconst tramp-smb-prompt "^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
90 "Regexp used as prompt in smbclient or powershell.")
92 (defconst tramp-smb-wrong-passwd-regexp
94 '("NT_STATUS_LOGON_FAILURE"
95 "NT_STATUS_WRONG_PASSWORD"))
96 "Regexp for login error strings of SMB servers.")
98 (defconst tramp-smb-errors
101 `(;; Connection error / timeout / unknown command.
102 "Connection\\( to \\S-+\\)? failed"
103 "Read from server failed, maybe it closed the connection"
104 "Call timed out: server did not respond"
105 "\\S-+: command not found"
106 "Server doesn't support UNIX CIFS calls"
118 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
119 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
120 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7).
121 "NT_STATUS_ACCESS_DENIED"
122 "NT_STATUS_ACCOUNT_LOCKED_OUT"
123 "NT_STATUS_BAD_NETWORK_NAME"
124 "NT_STATUS_CANNOT_DELETE"
125 "NT_STATUS_CONNECTION_REFUSED"
126 "NT_STATUS_DIRECTORY_NOT_EMPTY"
127 "NT_STATUS_DUPLICATE_NAME"
128 "NT_STATUS_FILE_IS_A_DIRECTORY"
129 "NT_STATUS_IMAGE_ALREADY_LOADED"
130 "NT_STATUS_IO_TIMEOUT"
131 "NT_STATUS_LOGON_FAILURE"
132 "NT_STATUS_NETWORK_ACCESS_DENIED"
133 "NT_STATUS_NOT_IMPLEMENTED"
134 "NT_STATUS_NO_SUCH_FILE"
135 "NT_STATUS_NO_SUCH_USER"
136 "NT_STATUS_OBJECT_NAME_COLLISION"
137 "NT_STATUS_OBJECT_NAME_INVALID"
138 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
139 "NT_STATUS_SHARING_VIOLATION"
140 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
141 "NT_STATUS_UNSUCCESSFUL"
142 "NT_STATUS_WRONG_PASSWORD")))
144 "Regexp for possible error strings of SMB servers.
145 Used instead of analyzing error codes of commands.")
147 (defconst tramp-smb-actions-with-share
148 '((tramp-smb-prompt tramp-action-succeed)
149 (tramp-password-prompt-regexp tramp-action-password)
150 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
151 (tramp-smb-errors tramp-action-permission-denied)
152 (tramp-process-alive-regexp tramp-action-process-alive))
153 "List of pattern/action pairs.
154 This list is used for login to SMB servers.
156 See `tramp-actions-before-shell' for more info.")
158 (defconst tramp-smb-actions-without-share
159 '((tramp-password-prompt-regexp tramp-action-password)
160 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
161 (tramp-smb-errors tramp-action-permission-denied)
162 (tramp-process-alive-regexp tramp-action-out-of-band))
163 "List of pattern/action pairs.
164 This list is used for login to SMB servers.
166 See `tramp-actions-before-shell' for more info.")
168 (defconst tramp-smb-actions-with-tar
169 '((tramp-password-prompt-regexp tramp-action-password)
170 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
171 (tramp-smb-errors tramp-action-permission-denied)
172 (tramp-process-alive-regexp tramp-smb-action-with-tar))
173 "List of pattern/action pairs.
174 This list is used for tar-like copy of directories.
176 See `tramp-actions-before-shell' for more info.")
178 ;; New handlers should be added here.
179 (defconst tramp-smb-file-name-handler-alist
181 ;; `access-file' performed by default handler.
182 (add-name-to-file . tramp-smb-handle-add-name-to-file)
183 ;; `byte-compiler-base-file-name' performed by default handler.
184 (copy-directory . tramp-smb-handle-copy-directory)
185 (copy-file . tramp-smb-handle-copy-file)
186 (delete-directory . tramp-smb-handle-delete-directory)
187 (delete-file . tramp-smb-handle-delete-file)
188 ;; `diff-latest-backup-file' performed by default handler.
189 (directory-file-name . tramp-handle-directory-file-name)
190 (directory-files . tramp-smb-handle-directory-files)
191 (directory-files-and-attributes
192 . tramp-handle-directory-files-and-attributes)
193 (dired-call-process . ignore)
194 (dired-compress-file . ignore)
195 (dired-uncache . tramp-handle-dired-uncache)
196 (expand-file-name . tramp-smb-handle-expand-file-name)
197 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
198 (file-acl . tramp-smb-handle-file-acl)
199 (file-attributes . tramp-smb-handle-file-attributes)
200 (file-directory-p . tramp-smb-handle-file-directory-p)
201 (file-executable-p . tramp-handle-file-exists-p)
202 (file-exists-p . tramp-handle-file-exists-p)
203 (file-local-copy . tramp-smb-handle-file-local-copy)
204 (file-modes . tramp-handle-file-modes)
205 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
206 (file-name-as-directory . tramp-handle-file-name-as-directory)
207 (file-name-completion . tramp-handle-file-name-completion)
208 (file-name-directory . tramp-handle-file-name-directory)
209 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
210 ;; `file-name-sans-versions' performed by default handler.
211 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
212 (file-ownership-preserved-p . ignore)
213 (file-readable-p . tramp-handle-file-exists-p)
214 (file-regular-p . tramp-handle-file-regular-p)
215 (file-remote-p . tramp-handle-file-remote-p)
216 ;; `file-selinux-context' performed by default handler.
217 (file-symlink-p . tramp-handle-file-symlink-p)
218 ;; `file-truename' performed by default handler.
219 (file-writable-p . tramp-smb-handle-file-writable-p)
220 (find-backup-file-name . tramp-handle-find-backup-file-name)
221 ;; `find-file-noselect' performed by default handler.
222 ;; `get-file-buffer' performed by default handler.
223 (insert-directory . tramp-smb-handle-insert-directory)
224 (insert-file-contents . tramp-handle-insert-file-contents)
225 (load . tramp-handle-load)
226 (make-directory . tramp-smb-handle-make-directory)
227 (make-directory-internal . tramp-smb-handle-make-directory-internal)
228 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
229 (process-file . tramp-smb-handle-process-file)
230 (rename-file . tramp-smb-handle-rename-file)
231 (set-file-acl . ignore)
232 (set-file-modes . tramp-smb-handle-set-file-modes)
233 (set-file-selinux-context . ignore)
234 (set-file-times . ignore)
235 (set-visited-file-modtime . ignore)
236 (shell-command . tramp-handle-shell-command)
237 (start-file-process . tramp-smb-handle-start-file-process)
238 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
239 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
240 (vc-registered . ignore)
241 (verify-visited-file-modtime . ignore)
242 (write-region . tramp-smb-handle-write-region)
244 "Alist of handler functions for Tramp SMB method.
245 Operations not mentioned here will be handled by the default Emacs primitives.")
247 ;; Options for remote processes via winexe.
248 (defcustom tramp-smb-winexe-program "winexe"
249 "Name of winexe client to run.
250 If it isn't found in the local $PATH, the absolute path of winexe
251 shall be given. This is needed for remote processes."
256 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
257 "Shell to be used for processes on remote machines.
258 This must be Powershell V2 compatible."
263 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
264 "Command switch used together with `tramp-smb-winexe-shell-command'.
265 This can be used to disable echo etc."
270 ;; It must be a `defsubst' in order to push the whole code into
271 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
273 (defsubst tramp-smb-file-name-p (filename)
274 "Check if it's a filename for SMB servers."
275 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
279 (defun tramp-smb-file-name-handler (operation &rest args)
280 "Invoke the SMB related OPERATION.
281 First arg specifies the OPERATION, second arg is a list of arguments to
282 pass to the OPERATION."
283 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
285 (save-match-data (apply (cdr fn) args))
286 (tramp-run-real-handler operation args))))
289 (unless (memq system-type '(cygwin windows-nt))
290 (add-to-list 'tramp-foreign-file-name-handler-alist
291 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)))
294 ;; File name primitives.
296 (defun tramp-smb-handle-add-name-to-file
297 (filename newname &optional ok-if-already-exists)
298 "Like `add-name-to-file' for Tramp files."
299 (unless (tramp-equal-remote filename newname)
300 (with-parsed-tramp-file-name
301 (if (tramp-tramp-file-p filename) filename newname) nil
304 "add-name-to-file: %s"
305 "only implemented for same method, same user, same host")))
306 (with-parsed-tramp-file-name filename v1
307 (with-parsed-tramp-file-name newname v2
308 (when (file-directory-p filename)
311 "add-name-to-file: %s must not be a directory" filename))
312 (when (and (not ok-if-already-exists)
313 (file-exists-p newname)
314 (not (numberp ok-if-already-exists))
317 "File %s already exists; make it a new name anyway? "
321 "add-name-to-file: file %s already exists" newname))
322 ;; We must also flush the cache of the directory, because
323 ;; `file-attributes' reads the values from there.
324 (tramp-flush-file-property v2 (file-name-directory v2-localname))
325 (tramp-flush-file-property v2 v2-localname)
327 (tramp-smb-send-command
331 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
332 (tramp-smb-get-localname v1)
333 (tramp-smb-get-localname v2)))
336 "error with add-name-to-file, see buffer `%s' for details"
339 (defun tramp-smb-action-with-tar (proc vec)
340 "Untar from connection buffer."
341 (if (not (memq (process-status proc) '(run open)))
342 (throw 'tramp-action 'process-died)
344 (with-current-buffer (tramp-get-connection-buffer vec)
345 (goto-char (point-min))
346 (when (search-forward-regexp tramp-smb-server-version nil t)
347 ;; There might be a hidden password prompt.
350 (tramp-message vec 6 (buffer-substring (point-min) (point)))
351 (delete-region (point-min) (point))
352 (throw 'tramp-action 'ok)))))
354 (defun tramp-smb-handle-copy-directory
355 (dirname newname &optional keep-date parents copy-contents)
356 "Like `copy-directory' for Tramp files."
357 (setq dirname (expand-file-name dirname)
358 newname (expand-file-name newname))
359 (let ((t1 (tramp-tramp-file-p dirname))
360 (t2 (tramp-tramp-file-p newname)))
361 (with-parsed-tramp-file-name (if t1 dirname newname) nil
362 (with-tramp-progress-reporter
363 v 0 (format "Copying %s to %s" dirname newname)
365 ;; We must use a local temporary directory.
370 tramp-temp-name-prefix
371 (tramp-compat-temporary-file-directory)))))
374 (tramp-compat-copy-directory dirname tmpdir keep-date parents)
375 (tramp-compat-copy-directory tmpdir newname keep-date parents))
376 (tramp-compat-delete-directory tmpdir 'recursive))))
378 ;; We can copy recursively.
380 (when (and (file-directory-p newname)
381 (not (string-equal (file-name-nondirectory dirname)
382 (file-name-nondirectory newname))))
385 (file-name-nondirectory dirname) newname))
386 (if t2 (setq v (tramp-dissect-file-name newname))))
387 (if (not (file-directory-p newname))
388 (make-directory newname parents))
390 (setq tramp-current-method (tramp-file-name-method v)
391 tramp-current-user (tramp-file-name-user v)
392 tramp-current-host (tramp-file-name-real-host v))
394 (let* ((real-user (tramp-file-name-real-user v))
395 (real-host (tramp-file-name-real-host v))
396 (domain (tramp-file-name-domain v))
397 (port (tramp-file-name-port v))
398 (share (tramp-smb-get-share v))
399 (localname (file-name-as-directory
400 (replace-regexp-in-string
401 "\\\\" "/" (tramp-smb-get-localname v))))
402 (tmpdir (make-temp-name
404 tramp-temp-name-prefix
405 (tramp-compat-temporary-file-directory))))
406 (args (list tramp-smb-program
407 (concat "//" real-host "/" share) "-E")))
409 (if (not (zerop (length real-user)))
410 (setq args (append args (list "-U" real-user)))
411 (setq args (append args (list "-N"))))
413 (when domain (setq args (append args (list "-W" domain))))
414 (when port (setq args (append args (list "-p" port))))
416 (setq args (append args (list "-s" tramp-smb-conf))))
421 (list "-D" (shell-quote-argument localname)
422 "-c" (shell-quote-argument "tar qc - *")
424 (shell-quote-argument tmpdir)))
426 (append (list "tar" "cfC" "-" (shell-quote-argument dirname)
429 (list "-D" (shell-quote-argument localname)
430 "-c" (shell-quote-argument "tar qx -")))))
434 ;; Set the transfer process properties.
435 (tramp-set-connection-property
436 v "process-name" (buffer-name (current-buffer)))
437 (tramp-set-connection-property
438 v "process-buffer" (current-buffer))
441 ;; The smbclient tar command creates always complete
442 ;; paths. We must emulate the directory structure,
443 ;; and symlink to the real target.
445 (expand-file-name ".." (concat tmpdir localname)) 'parents)
447 newname (directory-file-name (concat tmpdir localname))))
449 ;; Use an asynchronous processes. By this, password
451 (let* ((default-directory tmpdir)
452 (p (start-process-shell-command
453 (tramp-get-connection-name v)
454 (tramp-get-connection-buffer v)
455 (mapconcat 'identity args " "))))
458 v 6 "%s" (mapconcat 'identity (process-command p) " "))
459 (tramp-compat-set-process-query-on-exit-flag p nil)
460 (tramp-process-actions p v nil tramp-smb-actions-with-tar)
462 (while (memq (process-status p) '(run open))
464 (tramp-message v 6 "\n%s" (buffer-string))))
466 ;; Reset the transfer process properties.
467 (tramp-set-connection-property v "process-name" nil)
468 (tramp-set-connection-property v "process-buffer" nil)
469 (when t1 (delete-directory tmpdir 'recurse))))
471 ;; Handle KEEP-DATE argument.
473 (set-file-times newname (nth 5 (file-attributes dirname))))
477 (set-file-modes newname (tramp-default-file-modes dirname)))
479 ;; When newname did exist, we have wrong cached values.
481 (with-parsed-tramp-file-name newname nil
482 (tramp-flush-file-property v (file-name-directory localname))
483 (tramp-flush-file-property v localname))))
485 ;; We must do it file-wise.
487 (tramp-run-real-handler
488 'copy-directory (list dirname newname keep-date parents))))))))
490 (defun tramp-smb-handle-copy-file
491 (filename newname &optional ok-if-already-exists keep-date
492 preserve-uid-gid preserve-extended-attributes)
493 "Like `copy-file' for Tramp files.
494 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
495 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
496 (setq filename (expand-file-name filename)
497 newname (expand-file-name newname))
498 (with-tramp-progress-reporter
499 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
500 0 (format "Copying %s to %s" filename newname)
502 (if (file-directory-p filename)
503 (tramp-compat-copy-directory filename newname keep-date t t)
505 (let ((tmpfile (file-local-copy filename)))
509 (rename-file tmpfile newname ok-if-already-exists)
511 (delete-file tmpfile)
512 (signal (car err) (cdr err))))
515 (when (file-directory-p newname)
517 (expand-file-name (file-name-nondirectory filename) newname)))
519 (with-parsed-tramp-file-name newname nil
520 (when (and (not ok-if-already-exists)
521 (file-exists-p newname))
522 (tramp-error v 'file-already-exists newname))
524 ;; We must also flush the cache of the directory, because
525 ;; `file-attributes' reads the values from there.
526 (tramp-flush-file-property v (file-name-directory localname))
527 (tramp-flush-file-property v localname)
528 (unless (tramp-smb-get-share v)
530 v 'file-error "Target `%s' must contain a share name" newname))
531 (unless (tramp-smb-send-command
532 v (format "put \"%s\" \"%s\""
533 filename (tramp-smb-get-localname v)))
534 (tramp-error v 'file-error "Cannot copy `%s'" filename))))))
536 ;; KEEP-DATE handling.
538 (set-file-times newname (nth 5 (file-attributes filename))))))
540 (defun tramp-smb-handle-delete-directory (directory &optional recursive)
541 "Like `delete-directory' for Tramp files."
542 (setq directory (directory-file-name (expand-file-name directory)))
543 (when (file-exists-p directory)
547 (if (file-directory-p file)
548 (tramp-compat-delete-directory file recursive)
550 ;; We do not want to delete "." and "..".
552 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
554 (with-parsed-tramp-file-name directory nil
555 ;; We must also flush the cache of the directory, because
556 ;; `file-attributes' reads the values from there.
557 (tramp-flush-file-property v (file-name-directory localname))
558 (tramp-flush-directory-property v localname)
559 (unless (tramp-smb-send-command
562 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
563 (tramp-smb-get-localname v)))
565 (with-current-buffer (tramp-get-connection-buffer v)
566 (goto-char (point-min))
567 (search-forward-regexp tramp-smb-errors nil t)
569 v 'file-error "%s `%s'" (match-string 0) directory))))))
571 (defun tramp-smb-handle-delete-file (filename &optional trash)
572 "Like `delete-file' for Tramp files."
573 (setq filename (expand-file-name filename))
574 (when (file-exists-p filename)
575 (with-parsed-tramp-file-name filename nil
576 ;; We must also flush the cache of the directory, because
577 ;; `file-attributes' reads the values from there.
578 (tramp-flush-file-property v (file-name-directory localname))
579 (tramp-flush-file-property v localname)
580 (unless (tramp-smb-send-command
583 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
584 (tramp-smb-get-localname v)))
586 (with-current-buffer (tramp-get-connection-buffer v)
587 (goto-char (point-min))
588 (search-forward-regexp tramp-smb-errors nil t)
590 v 'file-error "%s `%s'" (match-string 0) filename))))))
592 (defun tramp-smb-handle-directory-files
593 (directory &optional full match nosort)
594 "Like `directory-files' for Tramp files."
595 (let ((result (mapcar 'directory-file-name
596 (file-name-all-completions "" directory))))
597 ;; Discriminate with regexp.
601 (mapcar (lambda (x) (when (string-match match x) x))
607 (lambda (x) (expand-file-name x directory))
609 ;; Sort them if necessary.
610 (unless nosort (setq result (sort result 'string-lessp)))
614 (defun tramp-smb-handle-expand-file-name (name &optional dir)
615 "Like `expand-file-name' for Tramp files."
616 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
617 (setq dir (or dir default-directory "/"))
618 ;; Unless NAME is absolute, concat DIR and NAME.
619 (unless (file-name-absolute-p name)
620 (setq name (concat (file-name-as-directory dir) name)))
621 ;; If NAME is not a Tramp file, run the real handler.
622 (if (not (tramp-tramp-file-p name))
623 (tramp-run-real-handler 'expand-file-name (list name nil))
625 (with-parsed-tramp-file-name name nil
626 ;; Tilde expansion if necessary. We use the user name as share,
627 ;; which is often the case in domains.
628 (when (string-match "\\`/?~\\([^/]*\\)" localname)
631 (if (zerop (length (match-string 1 localname)))
632 (tramp-file-name-real-user v)
633 (match-string 1 localname))
635 ;; Make the file name absolute.
636 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
637 (setq localname (concat "/" localname)))
638 ;; No tilde characters in file name, do normal
639 ;; `expand-file-name' (this does "/./" and "/../").
640 (tramp-make-tramp-file-name
642 (tramp-run-real-handler 'expand-file-name (list localname))))))
644 (defun tramp-smb-handle-file-acl (filename)
645 "Like `file-acl' for Tramp files."
646 (with-parsed-tramp-file-name filename nil
647 (with-tramp-file-property v localname "file-acl"
648 (when (tramp-smb-send-command
649 v (format "getfacl \"%s\"" (tramp-smb-get-localname v)))
650 (with-current-buffer (tramp-get-connection-buffer v)
651 (goto-char (point-min))
652 (while (looking-at-p "^#")
654 (delete-region (point-min) (point)))
655 (goto-char (point-max))
657 (when (> (point-max) (point-min))
658 (substring-no-properties (buffer-string))))))))
660 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
661 "Like `file-attributes' for Tramp files."
662 (unless id-format (setq id-format 'integer))
664 (with-parsed-tramp-file-name filename nil
665 (with-tramp-file-property
666 v localname (format "file-attributes-%s" id-format)
667 (if (and (tramp-smb-get-share v) (tramp-smb-get-stat-capability v))
668 (tramp-smb-do-file-attributes-with-stat v id-format)
669 ;; Reading just the filename entry via "dir localname" is not
670 ;; possible, because when filename is a directory, some
671 ;; smbclient versions return the content of the directory, and
672 ;; other versions don't. Therefore, the whole content of the
673 ;; upper directory is retrieved, and the entry of the filename
674 ;; is extracted from.
675 (let* ((entries (tramp-smb-get-file-entries
676 (file-name-directory filename)))
677 (entry (assoc (file-name-nondirectory filename) entries))
678 (uid (if (equal id-format 'string) "nobody" -1))
679 (gid (if (equal id-format 'string) "nogroup" -1))
680 (inode (tramp-get-inode v))
681 (device (tramp-get-device v)))
685 (list (and (string-match "d" (nth 1 entry))
691 (nth 3 entry) ;5 mtime
693 (nth 2 entry) ;7 size
694 (nth 1 entry) ;8 mode
696 inode ;10 inode number
697 device)))))))) ;11 file system number
699 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
700 "Implement `file-attributes' for Tramp files using stat command."
702 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
703 (with-current-buffer (tramp-get-connection-buffer vec)
704 (let* (size id link uid gid atime mtime ctime mode inode)
705 (when (tramp-smb-send-command
706 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
709 (goto-char (point-min))
710 (unless (re-search-forward tramp-smb-errors nil t)
714 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
715 (setq size (string-to-number (match-string 1))
716 id (if (string-equal "directory" (match-string 2)) t
717 (if (string-equal "symbolic" (match-string 2)) ""))))
719 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
720 (setq inode (string-to-number (match-string 1))
721 link (string-to-number (match-string 2))))
723 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
724 (setq mode (match-string 1)
725 uid (if (equal id-format 'string) (match-string 2)
726 (string-to-number (match-string 2)))
727 gid (if (equal id-format 'string) (match-string 3)
728 (string-to-number (match-string 3)))))
730 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
733 (string-to-number (match-string 6)) ;; sec
734 (string-to-number (match-string 5)) ;; min
735 (string-to-number (match-string 4)) ;; hour
736 (string-to-number (match-string 3)) ;; day
737 (string-to-number (match-string 2)) ;; month
738 (string-to-number (match-string 1))))) ;; year
740 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
743 (string-to-number (match-string 6)) ;; sec
744 (string-to-number (match-string 5)) ;; min
745 (string-to-number (match-string 4)) ;; hour
746 (string-to-number (match-string 3)) ;; day
747 (string-to-number (match-string 2)) ;; month
748 (string-to-number (match-string 1))))) ;; year
750 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
753 (string-to-number (match-string 6)) ;; sec
754 (string-to-number (match-string 5)) ;; min
755 (string-to-number (match-string 4)) ;; hour
756 (string-to-number (match-string 3)) ;; day
757 (string-to-number (match-string 2)) ;; month
758 (string-to-number (match-string 1)))))) ;; year
760 ;; Return the result.
761 (list id link uid gid atime mtime ctime size mode nil inode
762 (tramp-get-device vec)))))))
764 (defun tramp-smb-handle-file-directory-p (filename)
765 "Like `file-directory-p' for Tramp files."
766 (and (file-exists-p filename)
767 (eq ?d (aref (nth 8 (file-attributes filename)) 0))))
769 (defun tramp-smb-handle-file-local-copy (filename)
770 "Like `file-local-copy' for Tramp files."
771 (with-parsed-tramp-file-name filename nil
772 (unless (file-exists-p filename)
775 "Cannot make local copy of non-existing file `%s'" filename))
776 (let ((tmpfile (tramp-compat-make-temp-file filename)))
777 (with-tramp-progress-reporter
778 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
779 (unless (tramp-smb-send-command
780 v (format "get \"%s\" \"%s\""
781 (tramp-smb-get-localname v) tmpfile))
782 ;; Oops, an error. We shall cleanup.
783 (delete-file tmpfile)
785 v 'file-error "Cannot make local copy of file `%s'" filename)))
788 ;; This function should return "foo/" for directories and "bar" for
790 (defun tramp-smb-handle-file-name-all-completions (filename directory)
791 "Like `file-name-all-completions' for Tramp files."
794 (with-parsed-tramp-file-name directory nil
795 (with-tramp-file-property v localname "file-name-all-completions"
797 (let ((entries (tramp-smb-get-file-entries directory)))
801 (if (string-match "d" (nth 1 x))
802 (file-name-as-directory (nth 0 x))
806 (defun tramp-smb-handle-file-writable-p (filename)
807 "Like `file-writable-p' for Tramp files."
808 (if (file-exists-p filename)
809 (string-match "w" (or (nth 8 (file-attributes filename)) ""))
810 (let ((dir (file-name-directory filename)))
811 (and (file-exists-p dir)
812 (file-writable-p dir)))))
814 (defun tramp-smb-handle-insert-directory
815 (filename switches &optional wildcard full-directory-p)
816 "Like `insert-directory' for Tramp files."
817 (setq filename (expand-file-name filename))
819 ;; Called from `dired-add-entry'.
820 (setq filename (file-name-as-directory filename))
821 (setq filename (directory-file-name filename)))
822 (with-parsed-tramp-file-name filename nil
824 (let ((base (file-name-nondirectory filename))
825 ;; We should not destroy the cache entry.
826 (entries (copy-sequence
827 (tramp-smb-get-file-entries
828 (file-name-directory filename)))))
831 (string-match "\\." base)
832 (setq base (replace-match "\\\\." nil nil base))
833 (string-match "\\*" base)
834 (setq base (replace-match ".*" nil nil base))
835 (string-match "\\?" base)
836 (setq base (replace-match ".?" nil nil base)))
842 (if (or wildcard (zerop (length base)))
843 ;; Check for matching entries.
847 (format "^%s" base) (nth 0 x))
850 ;; We just need the only and only entry FILENAME.
851 (list (assoc base entries)))))
858 (if (string-match "t" switches)
860 (tramp-time-less-p (nth 3 y) (nth 3 x))
862 (string-lessp (nth 0 x) (nth 0 y))))))
864 ;; Handle "-F" switch.
865 (when (string-match "F" switches)
868 (when (not (zerop (length (car x))))
870 ((char-equal ?d (string-to-char (nth 1 x)))
871 (setcar x (concat (car x) "/")))
872 ((char-equal ?x (string-to-char (nth 1 x)))
873 (setcar x (concat (car x) "*"))))))
879 (when (not (zerop (length (nth 0 x))))
881 (when (tramp-smb-get-stat-capability v)
883 (file-attributes filename 'string)))))
886 "%10s %3d %-8s %-8s %8s %s "
887 (or (nth 8 attr) (nth 1 x)) ; mode
888 (or (nth 1 attr) 1) ; inode
889 (or (nth 2 attr) "nobody") ; uid
890 (or (nth 3 attr) "nogroup") ; gid
891 (or (nth 7 attr) (nth 2 x)) ; size
893 (if (tramp-time-less-p
894 (tramp-time-subtract (current-time) (nth 3 x))
899 ;; We mark the file name. The inserted name could be
900 ;; from somewhere else, so we use the relative file
901 ;; name of `default-directory'.
902 (let ((start (point)))
908 (nth 0 x) (file-name-directory filename)))))
909 (put-text-property start (1- (point)) 'dired-filename t))
911 (beginning-of-line))))
914 (defun tramp-smb-handle-make-directory (dir &optional parents)
915 "Like `make-directory' for Tramp files."
916 (setq dir (directory-file-name (expand-file-name dir)))
917 (unless (file-name-absolute-p dir)
918 (setq dir (expand-file-name dir default-directory)))
919 (with-parsed-tramp-file-name dir nil
921 (let* ((ldir (file-name-directory dir)))
922 ;; Make missing directory parts.
924 (tramp-smb-get-share v)
925 (not (file-directory-p ldir)))
926 (make-directory ldir parents))
928 (when (file-directory-p ldir)
929 (make-directory-internal dir))
930 (unless (file-directory-p dir)
931 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
933 (defun tramp-smb-handle-make-directory-internal (directory)
934 "Like `make-directory-internal' for Tramp files."
935 (setq directory (directory-file-name (expand-file-name directory)))
936 (unless (file-name-absolute-p directory)
937 (setq directory (expand-file-name directory default-directory)))
938 (with-parsed-tramp-file-name directory nil
940 (let* ((file (tramp-smb-get-localname v)))
941 (when (file-directory-p (file-name-directory directory))
942 (tramp-smb-send-command
944 (if (tramp-smb-get-cifs-capabilities v)
946 "posix_mkdir \"%s\" %s"
947 file (tramp-compat-decimal-to-octal (default-file-modes)))
948 (format "mkdir \"%s\"" file)))
949 ;; We must also flush the cache of the directory, because
950 ;; `file-attributes' reads the values from there.
951 (tramp-flush-file-property v (file-name-directory localname))
952 (tramp-flush-file-property v localname))
953 (unless (file-directory-p directory)
955 v 'file-error "Couldn't make directory %s" directory))))))
957 (defun tramp-smb-handle-make-symbolic-link
958 (filename linkname &optional ok-if-already-exists)
959 "Like `make-symbolic-link' for Tramp files.
960 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
961 the symlink. If LINKNAME is a Tramp file, only the localname component is
962 used as the target of the symlink.
964 If LINKNAME is a Tramp file and the localname component is relative, then
965 it is expanded first, before the localname component is taken. Note that
966 this can give surprising results if the user/host for the source and
967 target of the symlink differ."
968 (unless (tramp-equal-remote filename linkname)
969 (with-parsed-tramp-file-name
970 (if (tramp-tramp-file-p filename) filename linkname) nil
973 "make-symbolic-link: %s"
974 "only implemented for same method, same user, same host")))
975 (with-parsed-tramp-file-name filename v1
976 (with-parsed-tramp-file-name linkname v2
977 (when (file-directory-p filename)
980 "make-symbolic-link: %s must not be a directory" filename))
981 (when (and (not ok-if-already-exists)
982 (file-exists-p linkname)
983 (not (numberp ok-if-already-exists))
986 "File %s already exists; make it a new name anyway? "
990 "make-symbolic-link: file %s already exists" linkname))
991 (unless (tramp-smb-get-cifs-capabilities v1)
992 (tramp-error v2 'file-error "make-symbolic-link not supported"))
993 ;; We must also flush the cache of the directory, because
994 ;; `file-attributes' reads the values from there.
995 (tramp-flush-file-property v2 (file-name-directory v2-localname))
996 (tramp-flush-file-property v2 v2-localname)
998 (tramp-smb-send-command
1001 "symlink \"%s\" \"%s\""
1002 (tramp-smb-get-localname v1)
1003 (tramp-smb-get-localname v2)))
1006 "error with make-symbolic-link, see buffer `%s' for details"
1009 (defun tramp-smb-handle-process-file
1010 (program &optional infile destination display &rest args)
1011 "Like `process-file' for Tramp files."
1012 ;; The implementation is not complete yet.
1013 (when (and (numberp destination) (zerop destination))
1014 (error "Implementation does not handle immediate return"))
1016 (with-parsed-tramp-file-name default-directory nil
1017 (let* ((name (file-name-nondirectory program))
1020 input tmpinput outbuf command ret)
1024 (setq infile (expand-file-name infile))
1025 (if (tramp-equal-remote default-directory infile)
1026 ;; INFILE is on the same remote host.
1027 (setq input (with-parsed-tramp-file-name infile nil localname))
1028 ;; INFILE must be copied to remote host.
1029 (setq input (tramp-make-tramp-temp-file v)
1030 tmpinput (tramp-make-tramp-file-name method user host input))
1031 (copy-file infile tmpinput t))
1032 ;; Transform input into a filename powershell does understand.
1033 (setq input (format "//%s%s" host input)))
1035 ;; Determine output.
1038 ((bufferp destination)
1039 (setq outbuf destination))
1041 ((stringp destination)
1042 (setq outbuf (get-buffer-create destination)))
1043 ;; (REAL-DESTINATION ERROR-DESTINATION)
1044 ((consp destination)
1047 ((bufferp (car destination))
1048 (setq outbuf (car destination)))
1049 ((stringp (car destination))
1050 (setq outbuf (get-buffer-create (car destination))))
1052 (setq outbuf (current-buffer))))
1054 (tramp-message v 2 "%s" "STDERR not supported"))
1057 (setq outbuf (current-buffer))))
1059 ;; Construct command.
1060 (setq command (mapconcat 'identity (cons program args) " ")
1063 "get-content %s | & %s"
1064 (tramp-smb-shell-quote-argument input) command)
1065 (format "& %s" command)))
1067 (while (get-process name1)
1068 ;; NAME must be unique as process name.
1070 name1 (format "%s<%d>" name i)))
1072 ;; Set the new process properties.
1073 (tramp-set-connection-property v "process-name" name1)
1074 (tramp-set-connection-property
1076 (or outbuf (generate-new-buffer tramp-temp-buffer-name)))
1080 (with-current-buffer (tramp-get-connection-buffer v)
1081 ;; Preserve buffer contents.
1082 (narrow-to-region (point-max) (point-max))
1083 (tramp-smb-call-winexe v)
1084 (when (tramp-smb-get-share v)
1085 (tramp-smb-send-command
1086 v (format "cd \"//%s%s\"" host (file-name-directory localname))))
1087 (tramp-smb-send-command v command)
1088 ;; Preserve command output.
1089 (narrow-to-region (point-max) (point-max))
1090 (let ((p (tramp-get-connection-process v)))
1091 (tramp-smb-send-command v "exit $lasterrorcode")
1092 (while (memq (process-status p) '(run open))
1094 (setq ret (process-exit-status p))))
1095 (delete-region (point-min) (point-max))
1098 ;; When the user did interrupt, we should do it also. We use
1099 ;; return code -1 as marker.
1106 ;; We should show the output anyway.
1107 (when (and outbuf display) (display-buffer outbuf))
1109 ;; Cleanup. We remove all file cache values for the connection,
1110 ;; because the remote process could have changed them.
1111 (tramp-set-connection-property v "process-name" nil)
1112 (tramp-set-connection-property v "process-buffer" nil)
1113 (when tmpinput (delete-file tmpinput))
1115 (kill-buffer (tramp-get-connection-property v "process-buffer" nil)))
1117 ;; `process-file-side-effects' has been introduced with GNU
1118 ;; Emacs 23.2. If set to `nil', no remote file will be changed
1119 ;; by `program'. If it doesn't exist, we assume its default
1121 (unless (and (boundp 'process-file-side-effects)
1122 (not (symbol-value 'process-file-side-effects)))
1123 (tramp-flush-directory-property v ""))
1125 ;; Return exit status.
1130 (defun tramp-smb-handle-rename-file
1131 (filename newname &optional ok-if-already-exists)
1132 "Like `rename-file' for Tramp files."
1133 (setq filename (expand-file-name filename)
1134 newname (expand-file-name newname))
1136 (when (and (not ok-if-already-exists)
1137 (file-exists-p newname))
1139 (tramp-dissect-file-name
1140 (if (file-remote-p filename) filename newname))
1141 'file-already-exists newname))
1143 (with-tramp-progress-reporter
1144 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
1145 0 (format "Renaming %s to %s" filename newname)
1147 (if (and (tramp-equal-remote filename newname)
1149 (tramp-smb-get-share (tramp-dissect-file-name filename))
1150 (tramp-smb-get-share (tramp-dissect-file-name newname))))
1151 ;; We can rename directly.
1152 (with-parsed-tramp-file-name filename v1
1153 (with-parsed-tramp-file-name newname v2
1155 ;; We must also flush the cache of the directory, because
1156 ;; `file-attributes' reads the values from there.
1157 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1158 (tramp-flush-file-property v2 v2-localname)
1159 (unless (tramp-smb-get-share v2)
1161 v2 'file-error "Target `%s' must contain a share name" newname))
1162 (unless (tramp-smb-send-command
1163 v2 (format "rename \"%s\" \"%s\""
1164 (tramp-smb-get-localname v1)
1165 (tramp-smb-get-localname v2)))
1166 (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
1168 ;; We must rename via copy.
1169 (tramp-compat-copy-file filename newname ok-if-already-exists t t t)
1170 (if (file-directory-p filename)
1171 (tramp-compat-delete-directory filename 'recursive)
1172 (delete-file filename)))))
1174 (defun tramp-smb-handle-set-file-modes (filename mode)
1175 "Like `set-file-modes' for Tramp files."
1176 (with-parsed-tramp-file-name filename nil
1177 (when (tramp-smb-get-cifs-capabilities v)
1178 (tramp-flush-file-property v localname)
1179 (unless (tramp-smb-send-command
1180 v (format "chmod \"%s\" %s"
1181 (tramp-smb-get-localname v)
1182 (tramp-compat-decimal-to-octal mode)))
1184 v 'file-error "Error while changing file's mode %s" filename)))))
1186 ;; We use BUFFER also as connection buffer during setup. Because of
1187 ;; this, its original contents must be saved, and restored once
1188 ;; connection has been setup.
1189 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1190 "Like `start-file-process' for Tramp files."
1191 (with-parsed-tramp-file-name default-directory nil
1192 (let ((command (mapconcat 'identity (cons program args) " "))
1193 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1200 ;; BUFFER can be nil. We use a temporary buffer.
1201 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
1202 (while (get-process name1)
1203 ;; NAME must be unique as process name.
1205 name1 (format "%s<%d>" name i)))
1206 ;; Set the new process properties.
1207 (tramp-set-connection-property v "process-name" name1)
1208 (tramp-set-connection-property v "process-buffer" buffer)
1209 ;; Activate narrowing in order to save BUFFER contents.
1210 (with-current-buffer (tramp-get-connection-buffer v)
1211 (let ((buffer-undo-list t))
1212 (narrow-to-region (point-max) (point-max))
1213 (tramp-smb-call-winexe v)
1214 (when (tramp-smb-get-share v)
1215 (tramp-smb-send-command
1218 host (file-name-directory localname))))
1219 (tramp-message v 6 "(%s); exit" command)
1220 (tramp-send-string v command)))
1222 (tramp-get-connection-process v)))
1225 (with-current-buffer (tramp-get-connection-buffer v)
1226 (if (string-match tramp-temp-buffer-name (buffer-name))
1228 (set-process-buffer (tramp-get-connection-process v) nil)
1229 (kill-buffer (current-buffer)))
1230 (set-buffer-modified-p bmp)))
1231 (tramp-set-connection-property v "process-name" nil)
1232 (tramp-set-connection-property v "process-buffer" nil)))))
1234 (defun tramp-smb-handle-substitute-in-file-name (filename)
1235 "Like `handle-substitute-in-file-name' for Tramp files.
1236 \"//\" substitutes only in the local filename part. Catches
1237 errors for shares like \"C$/\", which are common in Microsoft Windows."
1238 (with-parsed-tramp-file-name filename nil
1239 ;; Ignore in LOCALNAME everything before "//".
1240 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1242 (concat (file-remote-p filename)
1243 (replace-match "\\1" nil nil localname)))))
1245 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1248 (defun tramp-smb-handle-write-region
1249 (start end filename &optional append visit lockname confirm)
1250 "Like `write-region' for Tramp files."
1251 (setq filename (expand-file-name filename))
1252 (with-parsed-tramp-file-name filename nil
1253 (unless (eq append nil)
1255 v 'file-error "Cannot append to file using Tramp (`%s')" filename))
1256 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1257 (when (and (not (featurep 'xemacs))
1258 confirm (file-exists-p filename))
1259 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1261 (tramp-error v 'file-error "File not overwritten")))
1262 ;; We must also flush the cache of the directory, because
1263 ;; `file-attributes' reads the values from there.
1264 (tramp-flush-file-property v (file-name-directory localname))
1265 (tramp-flush-file-property v localname)
1266 (let ((curbuf (current-buffer))
1267 (tmpfile (tramp-compat-make-temp-file filename)))
1268 ;; We say `no-message' here because we don't want the visited file
1269 ;; modtime data to be clobbered from the temp file. We call
1270 ;; `set-visited-file-modtime' ourselves later on.
1271 (tramp-run-real-handler
1273 (if confirm ; don't pass this arg unless defined for backward compat.
1274 (list start end tmpfile append 'no-message lockname confirm)
1275 (list start end tmpfile append 'no-message lockname)))
1277 (with-tramp-progress-reporter
1278 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1280 (unless (tramp-smb-send-command
1281 v (format "put %s \"%s\""
1282 tmpfile (tramp-smb-get-localname v)))
1283 (tramp-error v 'file-error "Cannot write `%s'" filename))
1284 (delete-file tmpfile)))
1286 (unless (equal curbuf (current-buffer))
1289 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1291 (set-visited-file-modtime)))))
1294 ;; Internal file name functions.
1296 (defun tramp-smb-get-share (vec)
1297 "Returns the share name of LOCALNAME."
1299 (let ((localname (tramp-file-name-localname vec)))
1300 (when (string-match "^/?\\([^/]+\\)/" localname)
1301 (match-string 1 localname)))))
1303 (defun tramp-smb-get-localname (vec)
1304 "Returns the file name of LOCALNAME.
1305 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1307 (let ((localname (tramp-file-name-localname vec)))
1310 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1311 ;; There is a share, separated by "/".
1312 (if (not (tramp-smb-get-cifs-capabilities vec))
1314 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1315 (match-string 1 localname) "")
1316 (match-string 1 localname))
1317 ;; There is just a share.
1318 (if (string-match "^/?\\([^/]+\\)$" localname)
1319 (match-string 1 localname)
1322 ;; Sometimes we have discarded `substitute-in-file-name'.
1323 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1324 (setq localname (replace-match "$" nil nil localname 1)))
1328 ;; Share names of a host are cached. It is very unlikely that the
1329 ;; shares do change during connection.
1330 (defun tramp-smb-get-file-entries (directory)
1331 "Read entries which match DIRECTORY.
1332 Either the shares are listed, or the `dir' command is executed.
1333 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1334 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1335 (setq localname (or localname "/"))
1336 (with-tramp-file-property v localname "file-entries"
1337 (with-current-buffer (tramp-get-connection-buffer v)
1338 (let* ((share (tramp-smb-get-share v))
1339 (cache (tramp-get-connection-property v "share-cache" nil))
1342 (if (and (not share) cache)
1343 ;; Return cached shares.
1348 (tramp-smb-send-command
1349 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1350 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1351 (tramp-smb-maybe-open-connection v))
1353 ;; Loop the listing.
1354 (goto-char (point-min))
1355 (if (re-search-forward tramp-smb-errors nil t)
1356 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1358 (setq entry (tramp-smb-read-file-entry share))
1360 (when entry (add-to-list 'res entry))))
1362 ;; Cache share entries.
1364 (tramp-set-connection-property v "share-cache" res)))
1366 ;; Add directory itself.
1367 (add-to-list 'res '("" "drwxrwxrwx" 0 (0 0)))
1369 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1370 ;; If there's no short delay, it returns nil. No idea about.
1371 (when (featurep 'xemacs) (sleep-for 0.01))
1376 ;; Return either a share name (if SHARE is nil), or a file name.
1378 ;; If shares are listed, the following format is expected:
1380 ;; Disk| - leading spaces
1381 ;; [^|]+| - share name, 14 char
1384 ;; Entries provided by smbclient DIR aren't fully regular.
1385 ;; They should have the format
1387 ;; \s-\{2,2} - leading spaces
1388 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1389 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1390 ;; \s- - space delimiter
1391 ;; \s-+[0-9]+ - size, 8 chars, right bound
1392 ;; \s-\{2,2\} - space delimiter
1393 ;; \w\{3,3\} - weekday
1394 ;; \s- - space delimiter
1395 ;; \w\{3,3\} - month
1396 ;; \s- - space delimiter
1398 ;; \s- - space delimiter
1399 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1400 ;; \s- - space delimiter
1401 ;; [0-9]\{4,4\} - year
1403 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1404 ;; has function display_finfo:
1406 ;; d_printf(" %-30s%7.7s %8.0f %s",
1408 ;; attrib_string(finfo->mode),
1409 ;; (double)finfo->size,
1410 ;; asctime(LocalTime(&t)));
1412 ;; in Samba 1.9, there's the following code:
1414 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1415 ;; CNV_LANG(finfo->name),
1416 ;; attrib_string(finfo->mode),
1418 ;; asctime(LocalTime(&t))));
1421 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1422 ;; available in older Emacsen.
1423 ;; * The length of constructs (file name, size) might exceed the default.
1424 ;; * File names might contain spaces.
1425 ;; * Permissions might be empty.
1427 ;; So we try to analyze backwards.
1428 (defun tramp-smb-read-file-entry (share)
1429 "Parse entry in SMB output buffer.
1430 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1431 Result is the list (LOCALNAME MODE SIZE MTIME)."
1432 ;; We are called from `tramp-smb-get-file-entries', which sets the
1434 (let ((line (buffer-substring (point) (point-at-eol)))
1435 localname mode size month day hour min sec year mtime)
1439 ;; Read share entries.
1440 (when (string-match "^Disk|\\([^|]+\\)|" line)
1441 (setq localname (match-string 1 line)
1449 (if (string-match "\\([0-9]+\\)$" line)
1450 (setq year (string-to-number (match-string 1 line))
1451 line (substring line 0 -5))
1455 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1456 (setq hour (string-to-number (match-string 1 line))
1457 min (string-to-number (match-string 2 line))
1458 sec (string-to-number (match-string 3 line))
1459 line (substring line 0 -9))
1463 (if (string-match "\\([0-9]+\\)$" line)
1464 (setq day (string-to-number (match-string 1 line))
1465 line (substring line 0 -3))
1469 (if (string-match "\\(\\w+\\)$" line)
1470 (setq month (match-string 1 line)
1471 line (substring line 0 -4))
1475 (if (string-match "\\(\\w+\\)$" line)
1476 (setq line (substring line 0 -5))
1480 (if (string-match "\\([0-9]+\\)$" line)
1481 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1482 (setq size (string-to-number (match-string 1 line)))
1483 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1484 (setq length (+ length (match-end 0))))
1485 (setq line (substring line 0 length)))
1488 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1489 (if (string-match "\\([ADHRSV]+\\)?$" line)
1491 mode (or (match-string 1 line) "")
1492 mode (save-match-data (format
1494 (if (string-match "D" mode) "d" "-")
1497 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1498 line (substring line 0 -6))
1502 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1503 (setq localname (match-string 1 line))
1506 (when (and localname mode size)
1508 (if (and sec min hour day month year)
1511 (cdr (assoc (downcase month) tramp-parse-time-months))
1514 (list localname mode size mtime))))
1516 (defun tramp-smb-get-cifs-capabilities (vec)
1517 "Check, whether the SMB server supports POSIX commands."
1518 ;; When we are not logged in yet, we return nil.
1519 (if (let ((p (tramp-get-connection-process vec)))
1520 (and p (processp p) (memq (process-status p) '(run open))))
1521 (with-tramp-connection-property
1522 (tramp-get-connection-process vec) "cifs-capabilities"
1524 (when (tramp-smb-send-command vec "posix")
1525 (with-current-buffer (tramp-get-connection-buffer vec)
1526 (goto-char (point-min))
1528 (re-search-forward "Server supports CIFS capabilities" nil t)
1532 (buffer-substring (point) (point-at-eol)) nil t)))))))))
1534 (defun tramp-smb-get-stat-capability (vec)
1535 "Check, whether the SMB server supports the STAT command."
1536 ;; When we are not logged in yet, we return nil.
1537 (if (let ((p (tramp-get-connection-process vec)))
1538 (and p (processp p) (memq (process-status p) '(run open))))
1539 (with-tramp-connection-property
1540 (tramp-get-connection-process vec) "stat-capability"
1541 (tramp-smb-send-command vec "stat ."))))
1544 ;; Connection functions.
1546 (defun tramp-smb-send-command (vec command)
1547 "Send the COMMAND to connection VEC.
1548 Returns nil if there has been an error message from smbclient."
1549 (tramp-smb-maybe-open-connection vec)
1550 (tramp-message vec 6 "%s" command)
1551 (tramp-send-string vec command)
1552 (tramp-smb-wait-for-output vec))
1554 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1555 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1556 Does not do anything if a connection is already open, but re-opens the
1557 connection if a previous connection has died for some reason.
1558 If ARGUMENT is non-nil, use it as argument for
1559 `tramp-smb-winexe-program', and suppress any checks."
1560 (let* ((share (tramp-smb-get-share vec))
1561 (buf (tramp-get-connection-buffer vec))
1562 (p (get-buffer-process buf)))
1564 ;; Check whether we still have the same smbclient version.
1565 ;; Otherwise, we must delete the connection cache, because
1566 ;; capabilities migh have changed.
1567 (unless (or argument (processp p))
1568 (let ((default-directory (tramp-compat-temporary-file-directory))
1569 (command (concat tramp-smb-program " -V")))
1571 (unless tramp-smb-version
1572 (unless (executable-find tramp-smb-program)
1575 "Cannot find command %s in %s" tramp-smb-program exec-path))
1576 (setq tramp-smb-version (shell-command-to-string command))
1577 (tramp-message vec 6 command)
1578 (tramp-message vec 6 "\n%s" tramp-smb-version)
1579 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1580 (setq tramp-smb-version
1581 (replace-match "" nil nil tramp-smb-version))))
1583 (unless (string-equal
1585 (tramp-get-connection-property
1586 vec "smbclient-version" tramp-smb-version))
1587 (tramp-flush-directory-property vec "")
1588 (tramp-flush-connection-property vec))
1590 (tramp-set-connection-property
1591 vec "smbclient-version" tramp-smb-version)))
1593 ;; If too much time has passed since last command was sent, look
1594 ;; whether there has been an error message; maybe due to
1595 ;; connection timeout.
1596 (with-current-buffer buf
1597 (goto-char (point-min))
1598 (when (and (> (tramp-time-diff
1600 (tramp-get-connection-property
1601 p "last-cmd-time" '(0 0 0)))
1603 p (processp p) (memq (process-status p) '(run open))
1604 (re-search-forward tramp-smb-errors nil t))
1608 ;; Check whether it is still the same share.
1610 (and p (processp p) (memq (process-status p) '(run open))
1614 (tramp-get-connection-property p "smb-share" ""))))
1617 ;; There might be unread output from checking for share names.
1618 (when buf (with-current-buffer buf (erase-buffer)))
1619 (when (and p (processp p)) (delete-process p))
1621 (let* ((user (tramp-file-name-user vec))
1622 (host (tramp-file-name-host vec))
1623 (real-user (tramp-file-name-real-user vec))
1624 (real-host (tramp-file-name-real-host vec))
1625 (domain (tramp-file-name-domain vec))
1626 (port (tramp-file-name-port vec))
1631 (setq args (list (concat "//" real-host))))
1633 (setq args (list (concat "//" real-host "/" share))))
1635 (setq args (list "-g" "-L" real-host ))))
1637 (if (not (zerop (length real-user)))
1638 (setq args (append args (list "-U" real-user)))
1639 (setq args (append args (list "-N"))))
1641 (when domain (setq args (append args (list "-W" domain))))
1642 (when port (setq args (append args (list "-p" port))))
1643 (when tramp-smb-conf
1644 (setq args (append args (list "-s" tramp-smb-conf))))
1646 (setq args (append args (list argument))))
1649 (with-tramp-progress-reporter
1651 (format "Opening connection for //%s%s/%s"
1652 (if (not (zerop (length user))) (concat user "@") "")
1655 (let* ((coding-system-for-read nil)
1656 (process-connection-type tramp-process-connection-type)
1657 (p (let ((default-directory
1658 (tramp-compat-temporary-file-directory)))
1659 (apply #'start-process
1660 (tramp-get-connection-name vec)
1661 (tramp-get-connection-buffer vec)
1663 tramp-smb-winexe-program tramp-smb-program)
1667 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1668 (tramp-compat-set-process-query-on-exit-flag p nil)
1670 ;; Set variables for computing the prompt for reading password.
1671 (setq tramp-current-method tramp-smb-method
1672 tramp-current-user user
1673 tramp-current-host host)
1676 (let (tramp-message-show-message)
1677 ;; Play login scenario.
1678 (tramp-process-actions
1680 (if (or argument share)
1681 tramp-smb-actions-with-share
1682 tramp-smb-actions-without-share))
1684 ;; Check server version.
1686 (with-current-buffer (tramp-get-connection-buffer vec)
1687 (goto-char (point-min))
1688 (search-forward-regexp tramp-smb-server-version nil t)
1689 (let ((smbserver-version (match-string 0)))
1693 (tramp-get-connection-property
1694 vec "smbserver-version" smbserver-version))
1695 (tramp-flush-directory-property vec "")
1696 (tramp-flush-connection-property vec))
1697 (tramp-set-connection-property
1698 vec "smbserver-version" smbserver-version))))
1700 ;; Set chunksize to 1. smbclient reads its input
1701 ;; character by character; if we send the string
1702 ;; at once, it is read painfully slow.
1703 (tramp-set-connection-property p "smb-share" share)
1704 (tramp-set-connection-property p "chunksize" 1))
1706 ;; Check for the error reason. If it was due to wrong
1707 ;; password, reestablish the connection. We cannot
1708 ;; handle this in `tramp-process-actions', because
1709 ;; smbclient does not ask for the password, again.
1711 (with-current-buffer (tramp-get-connection-buffer vec)
1712 (goto-char (point-min))
1713 (if (search-forward-regexp
1714 tramp-smb-wrong-passwd-regexp nil t)
1715 ;; Disable `auth-source' and `password-cache'.
1718 (tramp-smb-maybe-open-connection vec argument))
1719 ;; Propagate the error.
1720 (signal (car err) (cdr err)))))))))))))
1722 ;; We don't use timeouts. If needed, the caller shall wrap around.
1723 (defun tramp-smb-wait-for-output (vec)
1724 "Wait for output from smbclient command.
1725 Returns nil if an error message has appeared."
1726 (with-current-buffer (tramp-get-connection-buffer vec)
1727 (let ((p (get-buffer-process (current-buffer)))
1728 (found (progn (goto-char (point-min))
1729 (re-search-forward tramp-smb-prompt nil t)))
1730 (err (progn (goto-char (point-min))
1731 (re-search-forward tramp-smb-errors nil t)))
1734 ;; Algorithm: get waiting output. See if last line contains
1735 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1736 ;; If not, wait a bit and again get waiting output.
1737 (while (and (not found) (not err) (memq (process-status p) '(run open)))
1739 ;; Accept pending output.
1740 (tramp-accept-process-output p 0.1)
1742 ;; Search for prompt.
1743 (goto-char (point-min))
1744 (setq found (re-search-forward tramp-smb-prompt nil t))
1746 ;; Search for errors.
1747 (goto-char (point-min))
1748 (setq err (re-search-forward tramp-smb-errors nil t)))
1750 ;; When the process is still alive, read pending output.
1751 (while (and (not found) (memq (process-status p) '(run open)))
1753 ;; Accept pending output.
1754 (tramp-accept-process-output p 0.1)
1756 ;; Search for prompt.
1757 (goto-char (point-min))
1758 (setq found (re-search-forward tramp-smb-prompt nil t)))
1760 (tramp-message vec 6 "\n%s" (buffer-string))
1764 (goto-char (point-max))
1765 (re-search-backward tramp-smb-prompt nil t)
1766 (delete-region (point) (point-max)))
1768 ;; Return value is whether no error message has appeared.
1771 (defun tramp-smb-kill-winexe-function ()
1772 "Send SIGKILL to the winexe process."
1774 (let ((p (get-buffer-process (current-buffer))))
1775 (when (and p (processp p) (memq (process-status p) '(run open)))
1776 (signal-process (process-id p) 'SIGINT)))))
1778 (defun tramp-smb-call-winexe (vec)
1779 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
1781 ;; We call `tramp-get-buffer' in order to get a debug buffer for
1783 (tramp-get-buffer vec)
1785 ;; Check for program.
1786 (unless (let ((default-directory
1787 (tramp-compat-temporary-file-directory)))
1788 (executable-find tramp-smb-winexe-program))
1790 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
1792 ;; winexe does not supports ports.
1793 (when (tramp-file-name-port vec)
1794 (tramp-error vec 'file-error "Port not supported for remote processes"))
1796 (tramp-smb-maybe-open-connection
1800 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
1802 (set (make-local-variable 'kill-buffer-hook)
1803 '(tramp-smb-kill-winexe-function))
1805 ;; Suppress "^M". Shouldn't we specify utf8?
1806 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
1808 ;; Set width to 128. This avoids mixing prompt and long error messages.
1809 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
1810 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
1811 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
1812 (tramp-smb-send-command vec "$bufsize.Width = 128")
1813 (tramp-smb-send-command vec "$winsize.Width = 128")
1814 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
1815 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
1817 (defun tramp-smb-shell-quote-argument (s)
1818 "Similar to `shell-quote-argument', but uses windows cmd syntax."
1819 (let ((system-type 'ms-dos))
1820 (shell-quote-argument s)))
1822 (add-hook 'tramp-unload-hook
1824 (unload-feature 'tramp-smb 'force)))
1826 (provide 'tramp-smb)
1830 ;; * Return more comprehensive file permission string.
1831 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1832 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1833 ;; * Ignore case in file names.
1835 ;;; tramp-smb.el ends here