1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2015 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.
32 ;; Pacify byte-compiler.
36 ;; Define SMB method ...
38 (defconst tramp-smb-method "smb"
39 "Method to connect SAMBA and M$ SMB servers.")
41 ;; ... and add it to the method list.
43 (unless (memq system-type '(cygwin windows-nt))
44 (add-to-list 'tramp-methods
46 ;; We define an empty command, because `tramp-smb-call-winexe'
47 ;; opens already the powershell. Used in `tramp-handle-shell-command'.
48 (tramp-remote-shell "")
49 ;; This is just a guess. We don't know whether the share "C$"
50 ;; is available for public use, and whether the user has write
52 (tramp-tmpdir "/C$/Temp"))))
54 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
55 ;; a domain in USER, it must be the SMB method.
57 (add-to-list 'tramp-default-method-alist
58 `(nil ,tramp-prefix-domain-regexp ,tramp-smb-method))
60 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
61 ;; the anonymous user is chosen.
63 (add-to-list 'tramp-default-user-alist
64 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
66 ;; Add completion function for SMB method.
68 (eval-after-load 'tramp
69 '(tramp-set-completion-function
71 '((tramp-parse-netrc "~/.netrc"))))
74 (defcustom tramp-smb-program "smbclient"
75 "Name of SMB client to run."
80 (defcustom tramp-smb-acl-program "smbcacls"
81 "Name of SMB acls to run."
87 (defcustom tramp-smb-conf "/dev/null"
88 "Path of the smb.conf file.
89 If it is nil, no smb.conf will be added to the `tramp-smb-program'
90 call, letting the SMB client use the default one."
92 :type '(choice (const nil) (file :must-match t)))
94 (defvar tramp-smb-version nil
95 "Version string of the SMB client.")
97 (defconst tramp-smb-server-version
98 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
99 "Regexp of SMB server identification.")
101 (defconst tramp-smb-prompt "^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
102 "Regexp used as prompt in smbclient or powershell.")
104 (defconst tramp-smb-wrong-passwd-regexp
106 '("NT_STATUS_LOGON_FAILURE"
107 "NT_STATUS_WRONG_PASSWORD"))
108 "Regexp for login error strings of SMB servers.")
110 (defconst tramp-smb-errors
113 `(;; Connection error / timeout / unknown command.
114 "Connection\\( to \\S-+\\)? failed"
115 "Read from server failed, maybe it closed the connection"
116 "Call timed out: server did not respond"
117 "\\S-+: command not found"
118 "Server doesn't support UNIX CIFS calls"
130 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
131 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
132 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7).
133 "NT_STATUS_ACCESS_DENIED"
134 "NT_STATUS_ACCOUNT_LOCKED_OUT"
135 "NT_STATUS_BAD_NETWORK_NAME"
136 "NT_STATUS_CANNOT_DELETE"
137 "NT_STATUS_CONNECTION_REFUSED"
138 "NT_STATUS_DIRECTORY_NOT_EMPTY"
139 "NT_STATUS_DUPLICATE_NAME"
140 "NT_STATUS_FILE_IS_A_DIRECTORY"
141 "NT_STATUS_HOST_UNREACHABLE"
142 "NT_STATUS_IMAGE_ALREADY_LOADED"
143 "NT_STATUS_INVALID_LEVEL"
144 "NT_STATUS_IO_TIMEOUT"
145 "NT_STATUS_LOGON_FAILURE"
146 "NT_STATUS_NETWORK_ACCESS_DENIED"
147 "NT_STATUS_NOT_IMPLEMENTED"
148 "NT_STATUS_NO_LOGON_SERVERS"
149 "NT_STATUS_NO_SUCH_FILE"
150 "NT_STATUS_NO_SUCH_USER"
151 "NT_STATUS_OBJECT_NAME_COLLISION"
152 "NT_STATUS_OBJECT_NAME_INVALID"
153 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
154 "NT_STATUS_SHARING_VIOLATION"
155 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
156 "NT_STATUS_UNSUCCESSFUL"
157 "NT_STATUS_WRONG_PASSWORD")))
159 "Regexp for possible error strings of SMB servers.
160 Used instead of analyzing error codes of commands.")
162 (defconst tramp-smb-actions-with-share
163 '((tramp-smb-prompt tramp-action-succeed)
164 (tramp-password-prompt-regexp tramp-action-password)
165 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
166 (tramp-smb-errors tramp-action-permission-denied)
167 (tramp-process-alive-regexp tramp-action-process-alive))
168 "List of pattern/action pairs.
169 This list is used for login to SMB servers.
171 See `tramp-actions-before-shell' for more info.")
173 (defconst tramp-smb-actions-without-share
174 '((tramp-password-prompt-regexp tramp-action-password)
175 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
176 (tramp-smb-errors tramp-action-permission-denied)
177 (tramp-process-alive-regexp tramp-action-out-of-band))
178 "List of pattern/action pairs.
179 This list is used for login to SMB servers.
181 See `tramp-actions-before-shell' for more info.")
183 (defconst tramp-smb-actions-with-tar
184 '((tramp-password-prompt-regexp tramp-action-password)
185 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
186 (tramp-smb-errors tramp-action-permission-denied)
187 (tramp-process-alive-regexp tramp-smb-action-with-tar))
188 "List of pattern/action pairs.
189 This list is used for tar-like copy of directories.
191 See `tramp-actions-before-shell' for more info.")
193 (defconst tramp-smb-actions-get-acl
194 '((tramp-password-prompt-regexp tramp-action-password)
195 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
196 (tramp-smb-errors tramp-action-permission-denied)
197 (tramp-process-alive-regexp tramp-smb-action-get-acl))
198 "List of pattern/action pairs.
199 This list is used for smbcacls actions.
201 See `tramp-actions-before-shell' for more info.")
203 (defconst tramp-smb-actions-set-acl
204 '((tramp-password-prompt-regexp tramp-action-password)
205 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
206 (tramp-smb-errors tramp-action-permission-denied)
207 (tramp-process-alive-regexp tramp-smb-action-set-acl))
208 "List of pattern/action pairs.
209 This list is used for smbcacls actions.
211 See `tramp-actions-before-shell' for more info.")
213 ;; New handlers should be added here.
214 (defconst tramp-smb-file-name-handler-alist
215 '(;; `access-file' performed by default handler.
216 (add-name-to-file . tramp-smb-handle-add-name-to-file)
217 ;; `byte-compiler-base-file-name' performed by default handler.
218 (copy-directory . tramp-smb-handle-copy-directory)
219 (copy-file . tramp-smb-handle-copy-file)
220 (delete-directory . tramp-smb-handle-delete-directory)
221 (delete-file . tramp-smb-handle-delete-file)
222 ;; `diff-latest-backup-file' performed by default handler.
223 (directory-file-name . tramp-handle-directory-file-name)
224 (directory-files . tramp-smb-handle-directory-files)
225 (directory-files-and-attributes
226 . tramp-handle-directory-files-and-attributes)
227 (dired-call-process . ignore)
228 (dired-compress-file . ignore)
229 (dired-uncache . tramp-handle-dired-uncache)
230 (expand-file-name . tramp-smb-handle-expand-file-name)
231 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
232 (file-acl . tramp-smb-handle-file-acl)
233 (file-attributes . tramp-smb-handle-file-attributes)
234 (file-directory-p . tramp-smb-handle-file-directory-p)
235 ;; `file-equal-p' performed by default handler.
236 (file-executable-p . tramp-handle-file-exists-p)
237 (file-exists-p . tramp-handle-file-exists-p)
238 ;; `file-in-directory-p' performed by default handler.
239 (file-local-copy . tramp-smb-handle-file-local-copy)
240 (file-modes . tramp-handle-file-modes)
241 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
242 (file-name-as-directory . tramp-handle-file-name-as-directory)
243 (file-name-completion . tramp-handle-file-name-completion)
244 (file-name-directory . tramp-handle-file-name-directory)
245 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
246 ;; `file-name-sans-versions' performed by default handler.
247 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
248 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
249 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
250 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
251 (file-ownership-preserved-p . ignore)
252 (file-readable-p . tramp-handle-file-exists-p)
253 (file-regular-p . tramp-handle-file-regular-p)
254 (file-remote-p . tramp-handle-file-remote-p)
255 ;; `file-selinux-context' performed by default handler.
256 (file-symlink-p . tramp-handle-file-symlink-p)
257 ;; `file-truename' performed by default handler.
258 (file-writable-p . tramp-smb-handle-file-writable-p)
259 (find-backup-file-name . tramp-handle-find-backup-file-name)
260 ;; `find-file-noselect' performed by default handler.
261 ;; `get-file-buffer' performed by default handler.
262 (insert-directory . tramp-smb-handle-insert-directory)
263 (insert-file-contents . tramp-handle-insert-file-contents)
264 (load . tramp-handle-load)
265 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
266 (make-directory . tramp-smb-handle-make-directory)
267 (make-directory-internal . tramp-smb-handle-make-directory-internal)
268 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
269 (process-file . tramp-smb-handle-process-file)
270 (rename-file . tramp-smb-handle-rename-file)
271 (set-file-acl . tramp-smb-handle-set-file-acl)
272 (set-file-modes . tramp-smb-handle-set-file-modes)
273 (set-file-selinux-context . ignore)
274 (set-file-times . ignore)
275 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
276 (shell-command . tramp-handle-shell-command)
277 (start-file-process . tramp-smb-handle-start-file-process)
278 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
279 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
280 (vc-registered . ignore)
281 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
282 (write-region . tramp-smb-handle-write-region))
283 "Alist of handler functions for Tramp SMB method.
284 Operations not mentioned here will be handled by the default Emacs primitives.")
286 ;; Options for remote processes via winexe.
288 (defcustom tramp-smb-winexe-program "winexe"
289 "Name of winexe client to run.
290 If it isn't found in the local $PATH, the absolute path of winexe
291 shall be given. This is needed for remote processes."
297 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
298 "Shell to be used for processes on remote machines.
299 This must be Powershell V2 compatible."
305 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
306 "Command switch used together with `tramp-smb-winexe-shell-command'.
307 This can be used to disable echo etc."
312 ;; It must be a `defsubst' in order to push the whole code into
313 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
315 (defsubst tramp-smb-file-name-p (filename)
316 "Check if it's a filename for SMB servers."
317 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
321 (defun tramp-smb-file-name-handler (operation &rest args)
322 "Invoke the SMB related OPERATION.
323 First arg specifies the OPERATION, second arg is a list of arguments to
324 pass to the OPERATION."
325 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
327 (save-match-data (apply (cdr fn) args))
328 (tramp-run-real-handler operation args))))
331 (unless (memq system-type '(cygwin windows-nt))
332 (add-to-list 'tramp-foreign-file-name-handler-alist
333 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)))
336 ;; File name primitives.
338 (defun tramp-smb-handle-add-name-to-file
339 (filename newname &optional ok-if-already-exists)
340 "Like `add-name-to-file' for Tramp files."
341 (unless (tramp-equal-remote filename newname)
342 (with-parsed-tramp-file-name
343 (if (tramp-tramp-file-p filename) filename newname) nil
346 "add-name-to-file: %s"
347 "only implemented for same method, same user, same host")))
348 (with-parsed-tramp-file-name filename v1
349 (with-parsed-tramp-file-name newname v2
350 (when (file-directory-p filename)
353 "add-name-to-file: %s must not be a directory" filename))
354 (when (and (not ok-if-already-exists)
355 (file-exists-p newname)
356 (not (numberp ok-if-already-exists))
359 "File %s already exists; make it a new name anyway? "
363 "add-name-to-file: file %s already exists" newname))
364 ;; We must also flush the cache of the directory, because
365 ;; `file-attributes' reads the values from there.
366 (tramp-flush-file-property v2 (file-name-directory v2-localname))
367 (tramp-flush-file-property v2 v2-localname)
369 (tramp-smb-send-command
373 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
374 (tramp-smb-get-localname v1)
375 (tramp-smb-get-localname v2)))
378 "error with add-name-to-file, see buffer `%s' for details"
381 (defun tramp-smb-action-with-tar (proc vec)
382 "Untar from connection buffer."
383 (if (not (memq (process-status proc) '(run open)))
384 (throw 'tramp-action 'process-died)
386 (with-current-buffer (tramp-get-connection-buffer vec)
387 (goto-char (point-min))
388 (when (search-forward-regexp tramp-smb-server-version nil t)
389 ;; There might be a hidden password prompt.
392 (tramp-message vec 6 (buffer-substring (point-min) (point)))
393 (delete-region (point-min) (point))
394 (throw 'tramp-action 'ok)))))
396 (defun tramp-smb-handle-copy-directory
397 (dirname newname &optional keep-date parents copy-contents)
398 "Like `copy-directory' for Tramp files."
400 ;; We must do it file-wise.
401 (tramp-run-real-handler
402 'copy-directory (list dirname newname keep-date parents copy-contents))
404 (setq dirname (expand-file-name dirname)
405 newname (expand-file-name newname))
406 (let ((t1 (tramp-tramp-file-p dirname))
407 (t2 (tramp-tramp-file-p newname)))
408 (with-parsed-tramp-file-name (if t1 dirname newname) nil
409 (with-tramp-progress-reporter
410 v 0 (format "Copying %s to %s" dirname newname)
412 ;; We must use a local temporary directory.
417 tramp-temp-name-prefix
418 (tramp-compat-temporary-file-directory)))))
421 (make-directory tmpdir)
422 (tramp-compat-copy-directory
423 dirname tmpdir keep-date 'parents)
424 (tramp-compat-copy-directory
425 (expand-file-name (file-name-nondirectory dirname) tmpdir)
426 newname keep-date parents))
427 (tramp-compat-delete-directory tmpdir 'recursive))))
429 ;; We can copy recursively.
431 (when (and (file-directory-p newname)
432 (not (string-equal (file-name-nondirectory dirname)
433 (file-name-nondirectory newname))))
436 (file-name-nondirectory dirname) newname))
437 (if t2 (setq v (tramp-dissect-file-name newname))))
438 (if (not (file-directory-p newname))
439 (make-directory newname parents))
441 (setq tramp-current-method (tramp-file-name-method v)
442 tramp-current-user (tramp-file-name-user v)
443 tramp-current-host (tramp-file-name-real-host v))
445 (let* ((real-user (tramp-file-name-real-user v))
446 (real-host (tramp-file-name-real-host v))
447 (domain (tramp-file-name-domain v))
448 (port (tramp-file-name-port v))
449 (share (tramp-smb-get-share v))
450 (localname (file-name-as-directory
451 (tramp-compat-replace-regexp-in-string
452 "\\\\" "/" (tramp-smb-get-localname v))))
453 (tmpdir (make-temp-name
455 tramp-temp-name-prefix
456 (tramp-compat-temporary-file-directory))))
457 (args (list (concat "//" real-host "/" share) "-E")))
459 (if (not (zerop (length real-user)))
460 (setq args (append args (list "-U" real-user)))
461 (setq args (append args (list "-N"))))
463 (when domain (setq args (append args (list "-W" domain))))
464 (when port (setq args (append args (list "-p" port))))
466 (setq args (append args (list "-s" tramp-smb-conf))))
471 (list "-D" (shell-quote-argument localname)
472 "-c" (shell-quote-argument "tar qc - *")
474 (shell-quote-argument tmpdir)))
476 (append (list "tar" "cfC" "-"
477 (shell-quote-argument dirname) "." "|")
479 (list "-D" (shell-quote-argument localname)
480 "-c" (shell-quote-argument "tar qx -")))))
484 ;; Set the transfer process properties.
485 (tramp-set-connection-property
486 v "process-name" (buffer-name (current-buffer)))
487 (tramp-set-connection-property
488 v "process-buffer" (current-buffer))
491 ;; The smbclient tar command creates always
492 ;; complete paths. We must emulate the
493 ;; directory structure, and symlink to the real
497 ".." (concat tmpdir localname)) 'parents)
499 newname (directory-file-name (concat tmpdir localname))))
501 ;; Use an asynchronous processes. By this,
502 ;; password can be handled.
503 (let* ((default-directory tmpdir)
506 (tramp-get-connection-name v)
507 (tramp-get-connection-buffer v)
508 tramp-smb-program args)))
511 v 6 "%s" (mapconcat 'identity (process-command p) " "))
512 (tramp-set-connection-property p "vector" v)
513 (tramp-compat-set-process-query-on-exit-flag p nil)
514 (tramp-process-actions p v nil tramp-smb-actions-with-tar)
516 (while (memq (process-status p) '(run open))
518 (tramp-message v 6 "\n%s" (buffer-string))))
520 ;; Reset the transfer process properties.
521 (tramp-set-connection-property v "process-name" nil)
522 (tramp-set-connection-property v "process-buffer" nil)
523 (when t1 (tramp-compat-delete-directory tmpdir 'recurse))))
525 ;; Handle KEEP-DATE argument.
527 (set-file-times newname (nth 5 (file-attributes dirname))))
531 (set-file-modes newname (tramp-default-file-modes dirname)))
533 ;; When newname did exist, we have wrong cached values.
535 (with-parsed-tramp-file-name newname nil
536 (tramp-flush-file-property v (file-name-directory localname))
537 (tramp-flush-file-property v localname))))
539 ;; We must do it file-wise.
541 (tramp-run-real-handler
542 'copy-directory (list dirname newname keep-date parents)))))))))
544 (defun tramp-smb-handle-copy-file
545 (filename newname &optional ok-if-already-exists keep-date
546 _preserve-uid-gid _preserve-extended-attributes)
547 "Like `copy-file' for Tramp files.
548 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
549 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
550 (setq filename (expand-file-name filename)
551 newname (expand-file-name newname))
552 (with-tramp-progress-reporter
553 (tramp-dissect-file-name
554 (if (tramp-tramp-file-p filename) filename newname))
555 0 (format "Copying %s to %s" filename newname)
557 (if (file-directory-p filename)
558 (tramp-compat-copy-directory filename newname keep-date t t)
560 (let ((tmpfile (file-local-copy filename)))
564 (rename-file tmpfile newname ok-if-already-exists)
566 (delete-file tmpfile)
567 (signal (car err) (cdr err))))
570 (when (file-directory-p newname)
572 (expand-file-name (file-name-nondirectory filename) newname)))
574 (with-parsed-tramp-file-name newname nil
575 (when (and (not ok-if-already-exists)
576 (file-exists-p newname))
577 (tramp-error v 'file-already-exists newname))
579 ;; We must also flush the cache of the directory, because
580 ;; `file-attributes' reads the values from there.
581 (tramp-flush-file-property v (file-name-directory localname))
582 (tramp-flush-file-property v localname)
583 (unless (tramp-smb-get-share v)
585 v 'file-error "Target `%s' must contain a share name" newname))
586 (unless (tramp-smb-send-command
587 v (format "put \"%s\" \"%s\""
588 filename (tramp-smb-get-localname v)))
590 v 'file-error "Cannot copy `%s' to `%s'" filename newname))))))
592 ;; KEEP-DATE handling.
594 (set-file-times newname (nth 5 (file-attributes filename))))))
596 (defun tramp-smb-handle-delete-directory (directory &optional recursive)
597 "Like `delete-directory' for Tramp files."
598 (setq directory (directory-file-name (expand-file-name directory)))
599 (when (file-exists-p directory)
603 (if (file-directory-p file)
604 (tramp-compat-delete-directory file recursive)
606 ;; We do not want to delete "." and "..".
608 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
610 (with-parsed-tramp-file-name directory nil
611 ;; We must also flush the cache of the directory, because
612 ;; `file-attributes' reads the values from there.
613 (tramp-flush-file-property v (file-name-directory localname))
614 (tramp-flush-directory-property v localname)
615 (unless (tramp-smb-send-command
618 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
619 (tramp-smb-get-localname v)))
621 (with-current-buffer (tramp-get-connection-buffer v)
622 (goto-char (point-min))
623 (search-forward-regexp tramp-smb-errors nil t)
625 v 'file-error "%s `%s'" (match-string 0) directory))))))
627 (defun tramp-smb-handle-delete-file (filename &optional _trash)
628 "Like `delete-file' for Tramp files."
629 (setq filename (expand-file-name filename))
630 (when (file-exists-p filename)
631 (with-parsed-tramp-file-name filename nil
632 ;; We must also flush the cache of the directory, because
633 ;; `file-attributes' reads the values from there.
634 (tramp-flush-file-property v (file-name-directory localname))
635 (tramp-flush-file-property v localname)
636 (unless (tramp-smb-send-command
639 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
640 (tramp-smb-get-localname v)))
642 (with-current-buffer (tramp-get-connection-buffer v)
643 (goto-char (point-min))
644 (search-forward-regexp tramp-smb-errors nil t)
646 v 'file-error "%s `%s'" (match-string 0) filename))))))
648 (defun tramp-smb-handle-directory-files
649 (directory &optional full match nosort)
650 "Like `directory-files' for Tramp files."
651 (let ((result (mapcar 'directory-file-name
652 (file-name-all-completions "" directory)))
654 ;; Discriminate with regexp.
658 (mapcar (lambda (x) (when (string-match match x) x))
664 (lambda (x) (format "%s/%s" directory x))
666 ;; Sort them if necessary.
667 (unless nosort (setq result (sort result 'string-lessp)))
668 ;; Remove double entries.
669 (dolist (elt result res)
670 (add-to-list 'res elt 'append))))
672 (defun tramp-smb-handle-expand-file-name (name &optional dir)
673 "Like `expand-file-name' for Tramp files."
674 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
675 (setq dir (or dir default-directory "/"))
676 ;; Unless NAME is absolute, concat DIR and NAME.
677 (unless (file-name-absolute-p name)
678 (setq name (concat (file-name-as-directory dir) name)))
679 ;; If NAME is not a Tramp file, run the real handler.
680 (if (not (tramp-tramp-file-p name))
681 (tramp-run-real-handler 'expand-file-name (list name nil))
683 (with-parsed-tramp-file-name name nil
684 ;; Tilde expansion if necessary. We use the user name as share,
685 ;; which is often the case in domains.
686 (when (string-match "\\`/?~\\([^/]*\\)" localname)
689 (if (zerop (length (match-string 1 localname)))
690 (tramp-file-name-real-user v)
691 (match-string 1 localname))
693 ;; Make the file name absolute.
694 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
695 (setq localname (concat "/" localname)))
696 ;; No tilde characters in file name, do normal
697 ;; `expand-file-name' (this does "/./" and "/../").
698 (tramp-make-tramp-file-name
700 (tramp-run-real-handler 'expand-file-name (list localname))))))
702 (defun tramp-smb-action-get-acl (proc vec)
703 "Read ACL data from connection buffer."
704 (when (not (memq (process-status proc) '(run open)))
705 ;; Accept pending output.
706 (while (tramp-accept-process-output proc 0.1))
707 (with-current-buffer (tramp-get-connection-buffer vec)
708 ;; There might be a hidden password prompt.
710 (tramp-message vec 10 "\n%s" (buffer-string))
711 (goto-char (point-min))
712 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
714 (delete-region (point-min) (point)))
715 (while (and (not (eobp)) (looking-at "^.+:.+"))
717 (delete-region (point) (point-max))
718 (throw 'tramp-action 'ok))))
720 (defun tramp-smb-handle-file-acl (filename)
721 "Like `file-acl' for Tramp files."
722 (with-parsed-tramp-file-name filename nil
723 (with-tramp-file-property v localname "file-acl"
724 (when (executable-find tramp-smb-acl-program)
726 (setq tramp-current-method (tramp-file-name-method v)
727 tramp-current-user (tramp-file-name-user v)
728 tramp-current-host (tramp-file-name-real-host v))
730 (let* ((real-user (tramp-file-name-real-user v))
731 (real-host (tramp-file-name-real-host v))
732 (domain (tramp-file-name-domain v))
733 (port (tramp-file-name-port v))
734 (share (tramp-smb-get-share v))
735 (localname (tramp-compat-replace-regexp-in-string
736 "\\\\" "/" (tramp-smb-get-localname v)))
737 (args (list (concat "//" real-host "/" share) "-E")))
739 (if (not (zerop (length real-user)))
740 (setq args (append args (list "-U" real-user)))
741 (setq args (append args (list "-N"))))
743 (when domain (setq args (append args (list "-W" domain))))
744 (when port (setq args (append args (list "-p" port))))
746 (setq args (append args (list "-s" tramp-smb-conf))))
749 (append args (list (shell-quote-argument localname) "2>/dev/null")))
753 ;; Set the transfer process properties.
754 (tramp-set-connection-property
755 v "process-name" (buffer-name (current-buffer)))
756 (tramp-set-connection-property
757 v "process-buffer" (current-buffer))
759 ;; Use an asynchronous processes. By this, password
763 (tramp-get-connection-name v)
764 (tramp-get-connection-buffer v)
765 tramp-smb-acl-program args)))
768 v 6 "%s" (mapconcat 'identity (process-command p) " "))
769 (tramp-set-connection-property p "vector" v)
770 (tramp-compat-set-process-query-on-exit-flag p nil)
771 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
772 (when (> (point-max) (point-min))
773 (tramp-compat-funcall
774 'substring-no-properties (buffer-string)))))
776 ;; Reset the transfer process properties.
777 (tramp-set-connection-property v "process-name" nil)
778 (tramp-set-connection-property v "process-buffer" nil)))))))
780 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
781 "Like `file-attributes' for Tramp files."
782 (unless id-format (setq id-format 'integer))
784 (with-parsed-tramp-file-name filename nil
785 (with-tramp-file-property
786 v localname (format "file-attributes-%s" id-format)
787 (if (tramp-smb-get-stat-capability v)
788 (tramp-smb-do-file-attributes-with-stat v id-format)
789 ;; Reading just the filename entry via "dir localname" is not
790 ;; possible, because when filename is a directory, some
791 ;; smbclient versions return the content of the directory, and
792 ;; other versions don't. Therefore, the whole content of the
793 ;; upper directory is retrieved, and the entry of the filename
794 ;; is extracted from.
795 (let* ((entries (tramp-smb-get-file-entries
796 (file-name-directory filename)))
797 (entry (assoc (file-name-nondirectory filename) entries))
798 (uid (if (equal id-format 'string) "nobody" -1))
799 (gid (if (equal id-format 'string) "nogroup" -1))
800 (inode (tramp-get-inode v))
801 (device (tramp-get-device v)))
805 (list (and (string-match "d" (nth 1 entry))
811 (nth 3 entry) ;5 mtime
813 (nth 2 entry) ;7 size
814 (nth 1 entry) ;8 mode
816 inode ;10 inode number
817 device)))))))) ;11 file system number
819 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
820 "Implement `file-attributes' for Tramp files using stat command."
822 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
823 (with-current-buffer (tramp-get-connection-buffer vec)
824 (let* (size id link uid gid atime mtime ctime mode inode)
825 (when (tramp-smb-send-command
826 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
829 (goto-char (point-min))
830 (unless (re-search-forward tramp-smb-errors nil t)
834 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
835 (setq size (string-to-number (match-string 1))
836 id (if (string-equal "directory" (match-string 2)) t
837 (if (string-equal "symbolic" (match-string 2)) ""))))
839 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
840 (setq inode (string-to-number (match-string 1))
841 link (string-to-number (match-string 2))))
843 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
844 (setq mode (match-string 1)
845 uid (if (equal id-format 'string) (match-string 2)
846 (string-to-number (match-string 2)))
847 gid (if (equal id-format 'string) (match-string 3)
848 (string-to-number (match-string 3)))))
850 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
853 (string-to-number (match-string 6)) ;; sec
854 (string-to-number (match-string 5)) ;; min
855 (string-to-number (match-string 4)) ;; hour
856 (string-to-number (match-string 3)) ;; day
857 (string-to-number (match-string 2)) ;; month
858 (string-to-number (match-string 1))))) ;; year
860 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
863 (string-to-number (match-string 6)) ;; sec
864 (string-to-number (match-string 5)) ;; min
865 (string-to-number (match-string 4)) ;; hour
866 (string-to-number (match-string 3)) ;; day
867 (string-to-number (match-string 2)) ;; month
868 (string-to-number (match-string 1))))) ;; year
870 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
873 (string-to-number (match-string 6)) ;; sec
874 (string-to-number (match-string 5)) ;; min
875 (string-to-number (match-string 4)) ;; hour
876 (string-to-number (match-string 3)) ;; day
877 (string-to-number (match-string 2)) ;; month
878 (string-to-number (match-string 1)))))) ;; year
880 ;; Return the result.
881 (list id link uid gid atime mtime ctime size mode nil inode
882 (tramp-get-device vec)))))))
884 (defun tramp-smb-handle-file-directory-p (filename)
885 "Like `file-directory-p' for Tramp files."
886 (and (file-exists-p filename)
887 (eq ?d (aref (nth 8 (file-attributes filename)) 0))))
889 (defun tramp-smb-handle-file-local-copy (filename)
890 "Like `file-local-copy' for Tramp files."
891 (with-parsed-tramp-file-name filename nil
892 (unless (file-exists-p filename)
895 "Cannot make local copy of non-existing file `%s'" filename))
896 (let ((tmpfile (tramp-compat-make-temp-file filename)))
897 (with-tramp-progress-reporter
898 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
899 (unless (tramp-smb-send-command
900 v (format "get \"%s\" \"%s\""
901 (tramp-smb-get-localname v) tmpfile))
902 ;; Oops, an error. We shall cleanup.
903 (delete-file tmpfile)
905 v 'file-error "Cannot make local copy of file `%s'" filename)))
908 ;; This function should return "foo/" for directories and "bar" for
910 (defun tramp-smb-handle-file-name-all-completions (filename directory)
911 "Like `file-name-all-completions' for Tramp files."
914 (with-parsed-tramp-file-name directory nil
915 (with-tramp-file-property v localname "file-name-all-completions"
917 (let ((entries (tramp-smb-get-file-entries directory)))
921 (if (string-match "d" (nth 1 x))
922 (file-name-as-directory (nth 0 x))
926 (defun tramp-smb-handle-file-writable-p (filename)
927 "Like `file-writable-p' for Tramp files."
928 (if (file-exists-p filename)
929 (string-match "w" (or (nth 8 (file-attributes filename)) ""))
930 (let ((dir (file-name-directory filename)))
931 (and (file-exists-p dir)
932 (file-writable-p dir)))))
934 (defun tramp-smb-handle-insert-directory
935 (filename switches &optional wildcard full-directory-p)
936 "Like `insert-directory' for Tramp files."
937 (setq filename (expand-file-name filename))
938 (unless switches (setq switches ""))
939 ;; Mark trailing "/".
940 (when (and (zerop (length (file-name-nondirectory filename)))
941 (not full-directory-p))
942 (setq switches (concat switches "F")))
944 ;; Called from `dired-add-entry'.
945 (setq filename (file-name-as-directory filename))
946 (setq filename (directory-file-name filename)))
947 (with-parsed-tramp-file-name filename nil
948 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
950 (let ((base (file-name-nondirectory filename))
951 ;; We should not destroy the cache entry.
952 (entries (copy-sequence
953 (tramp-smb-get-file-entries
954 (file-name-directory filename)))))
957 (string-match "\\." base)
958 (setq base (replace-match "\\\\." nil nil base))
959 (string-match "\\*" base)
960 (setq base (replace-match ".*" nil nil base))
961 (string-match "\\?" base)
962 (setq base (replace-match ".?" nil nil base)))
968 (if (or wildcard (zerop (length base)))
969 ;; Check for matching entries.
973 (format "^%s" base) (nth 0 x))
976 ;; We just need the only and only entry FILENAME.
977 (list (assoc base entries)))))
984 (if (string-match "t" switches)
986 (time-less-p (nth 3 y) (nth 3 x))
988 (string-lessp (nth 0 x) (nth 0 y))))))
990 ;; Handle "-F" switch.
991 (when (string-match "F" switches)
994 (when (not (zerop (length (car x))))
996 ((char-equal ?d (string-to-char (nth 1 x)))
997 (setcar x (concat (car x) "/")))
998 ((char-equal ?x (string-to-char (nth 1 x)))
999 (setcar x (concat (car x) "*"))))))
1005 (when (not (zerop (length (nth 0 x))))
1006 (when (string-match "l" switches)
1008 (when (tramp-smb-get-stat-capability v)
1010 (file-attributes filename 'string)))))
1013 "%10s %3d %-8s %-8s %8s %s "
1014 (or (nth 8 attr) (nth 1 x)) ; mode
1015 (or (nth 1 attr) 1) ; inode
1016 (or (nth 2 attr) "nobody") ; uid
1017 (or (nth 3 attr) "nogroup") ; gid
1018 (or (nth 7 attr) (nth 2 x)) ; size
1020 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1024 (nth 3 x)))))) ; date
1026 ;; We mark the file name. The inserted name could be
1027 ;; from somewhere else, so we use the relative file name
1028 ;; of `default-directory'.
1029 (let ((start (point)))
1035 (nth 0 x) (file-name-directory filename))
1036 (when full-directory-p (file-name-directory filename)))))
1037 (put-text-property start (1- (point)) 'dired-filename t))
1039 (beginning-of-line)))
1042 (defun tramp-smb-handle-make-directory (dir &optional parents)
1043 "Like `make-directory' for Tramp files."
1044 (setq dir (directory-file-name (expand-file-name dir)))
1045 (unless (file-name-absolute-p dir)
1046 (setq dir (expand-file-name dir default-directory)))
1047 (with-parsed-tramp-file-name dir nil
1049 (let* ((ldir (file-name-directory dir)))
1050 ;; Make missing directory parts.
1052 (tramp-smb-get-share v)
1053 (not (file-directory-p ldir)))
1054 (make-directory ldir parents))
1056 (when (file-directory-p ldir)
1057 (make-directory-internal dir))
1058 (unless (file-directory-p dir)
1059 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1061 (defun tramp-smb-handle-make-directory-internal (directory)
1062 "Like `make-directory-internal' for Tramp files."
1063 (setq directory (directory-file-name (expand-file-name directory)))
1064 (unless (file-name-absolute-p directory)
1065 (setq directory (expand-file-name directory default-directory)))
1066 (with-parsed-tramp-file-name directory nil
1068 (let* ((file (tramp-smb-get-localname v)))
1069 (when (file-directory-p (file-name-directory directory))
1070 (tramp-smb-send-command
1072 (if (tramp-smb-get-cifs-capabilities v)
1074 "posix_mkdir \"%s\" %s"
1075 file (tramp-compat-decimal-to-octal (default-file-modes)))
1076 (format "mkdir \"%s\"" file)))
1077 ;; We must also flush the cache of the directory, because
1078 ;; `file-attributes' reads the values from there.
1079 (tramp-flush-file-property v (file-name-directory localname))
1080 (tramp-flush-file-property v localname))
1081 (unless (file-directory-p directory)
1083 v 'file-error "Couldn't make directory %s" directory))))))
1085 (defun tramp-smb-handle-make-symbolic-link
1086 (filename linkname &optional ok-if-already-exists)
1087 "Like `make-symbolic-link' for Tramp files.
1088 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1089 the symlink. If LINKNAME is a Tramp file, only the localname component is
1090 used as the target of the symlink.
1092 If LINKNAME is a Tramp file and the localname component is relative, then
1093 it is expanded first, before the localname component is taken. Note that
1094 this can give surprising results if the user/host for the source and
1095 target of the symlink differ."
1096 (unless (tramp-equal-remote filename linkname)
1097 (with-parsed-tramp-file-name
1098 (if (tramp-tramp-file-p filename) filename linkname) nil
1101 "make-symbolic-link: %s"
1102 "only implemented for same method, same user, same host")))
1103 (with-parsed-tramp-file-name filename v1
1104 (with-parsed-tramp-file-name linkname v2
1105 (when (file-directory-p filename)
1108 "make-symbolic-link: %s must not be a directory" filename))
1109 (when (and (not ok-if-already-exists)
1110 (file-exists-p linkname)
1111 (not (numberp ok-if-already-exists))
1114 "File %s already exists; make it a new name anyway? "
1118 "make-symbolic-link: file %s already exists" linkname))
1119 (unless (tramp-smb-get-cifs-capabilities v1)
1120 (tramp-error v2 'file-error "make-symbolic-link not supported"))
1121 ;; We must also flush the cache of the directory, because
1122 ;; `file-attributes' reads the values from there.
1123 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1124 (tramp-flush-file-property v2 v2-localname)
1126 (tramp-smb-send-command
1129 "symlink \"%s\" \"%s\""
1130 (tramp-smb-get-localname v1)
1131 (tramp-smb-get-localname v2)))
1134 "error with make-symbolic-link, see buffer `%s' for details"
1137 (defun tramp-smb-handle-process-file
1138 (program &optional infile destination display &rest args)
1139 "Like `process-file' for Tramp files."
1140 ;; The implementation is not complete yet.
1141 (when (and (numberp destination) (zerop destination))
1142 (error "Implementation does not handle immediate return"))
1144 (with-parsed-tramp-file-name default-directory nil
1145 (let* ((name (file-name-nondirectory program))
1148 input tmpinput outbuf command ret)
1152 (setq infile (expand-file-name infile))
1153 (if (tramp-equal-remote default-directory infile)
1154 ;; INFILE is on the same remote host.
1155 (setq input (with-parsed-tramp-file-name infile nil localname))
1156 ;; INFILE must be copied to remote host.
1157 (setq input (tramp-make-tramp-temp-file v)
1158 tmpinput (tramp-make-tramp-file-name method user host input))
1159 (copy-file infile tmpinput t))
1160 ;; Transform input into a filename powershell does understand.
1161 (setq input (format "//%s%s" host input)))
1163 ;; Determine output.
1166 ((bufferp destination)
1167 (setq outbuf destination))
1169 ((stringp destination)
1170 (setq outbuf (get-buffer-create destination)))
1171 ;; (REAL-DESTINATION ERROR-DESTINATION)
1172 ((consp destination)
1175 ((bufferp (car destination))
1176 (setq outbuf (car destination)))
1177 ((stringp (car destination))
1178 (setq outbuf (get-buffer-create (car destination))))
1180 (setq outbuf (current-buffer))))
1182 (tramp-message v 2 "%s" "STDERR not supported"))
1185 (setq outbuf (current-buffer))))
1187 ;; Construct command.
1188 (setq command (mapconcat 'identity (cons program args) " ")
1191 "get-content %s | & %s"
1192 (tramp-smb-shell-quote-argument input) command)
1193 (format "& %s" command)))
1195 (while (get-process name1)
1196 ;; NAME must be unique as process name.
1198 name1 (format "%s<%d>" name i)))
1200 ;; Set the new process properties.
1201 (tramp-set-connection-property v "process-name" name1)
1202 (tramp-set-connection-property
1204 (or outbuf (generate-new-buffer tramp-temp-buffer-name)))
1208 (with-current-buffer (tramp-get-connection-buffer v)
1209 ;; Preserve buffer contents.
1210 (narrow-to-region (point-max) (point-max))
1211 (tramp-smb-call-winexe v)
1212 (when (tramp-smb-get-share v)
1213 (tramp-smb-send-command
1214 v (format "cd \"//%s%s\"" host (file-name-directory localname))))
1215 (tramp-smb-send-command v command)
1216 ;; Preserve command output.
1217 (narrow-to-region (point-max) (point-max))
1218 (let ((p (tramp-get-connection-process v)))
1219 (tramp-smb-send-command v "exit $lasterrorcode")
1220 (while (memq (process-status p) '(run open))
1222 (setq ret (process-exit-status p))))
1223 (delete-region (point-min) (point-max))
1226 ;; When the user did interrupt, we should do it also. We use
1227 ;; return code -1 as marker.
1234 ;; We should redisplay the output.
1235 (when (and display outbuf (get-buffer-window outbuf t)) (redisplay))
1237 ;; Cleanup. We remove all file cache values for the connection,
1238 ;; because the remote process could have changed them.
1239 (tramp-set-connection-property v "process-name" nil)
1240 (tramp-set-connection-property v "process-buffer" nil)
1241 (when tmpinput (delete-file tmpinput))
1243 (kill-buffer (tramp-get-connection-property v "process-buffer" nil)))
1245 ;; `process-file-side-effects' has been introduced with GNU
1246 ;; Emacs 23.2. If set to nil, no remote file will be changed
1247 ;; by `program'. If it doesn't exist, we assume its default
1249 (unless (and (boundp 'process-file-side-effects)
1250 (not (symbol-value 'process-file-side-effects)))
1251 (tramp-flush-directory-property v ""))
1253 ;; Return exit status.
1258 (defun tramp-smb-handle-rename-file
1259 (filename newname &optional ok-if-already-exists)
1260 "Like `rename-file' for Tramp files."
1261 (setq filename (expand-file-name filename)
1262 newname (expand-file-name newname))
1264 (when (and (not ok-if-already-exists)
1265 (file-exists-p newname))
1267 (tramp-dissect-file-name
1268 (if (tramp-tramp-file-p filename) filename newname))
1269 'file-already-exists newname))
1271 (with-tramp-progress-reporter
1272 (tramp-dissect-file-name
1273 (if (tramp-tramp-file-p filename) filename newname))
1274 0 (format "Renaming %s to %s" filename newname)
1276 (if (and (not (file-exists-p newname))
1277 (tramp-equal-remote filename newname)
1279 (tramp-smb-get-share (tramp-dissect-file-name filename))
1280 (tramp-smb-get-share (tramp-dissect-file-name newname))))
1281 ;; We can rename directly.
1282 (with-parsed-tramp-file-name filename v1
1283 (with-parsed-tramp-file-name newname v2
1285 ;; We must also flush the cache of the directory, because
1286 ;; `file-attributes' reads the values from there.
1287 (tramp-flush-file-property v1 (file-name-directory v1-localname))
1288 (tramp-flush-file-property v1 v1-localname)
1289 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1290 (tramp-flush-file-property v2 v2-localname)
1291 (unless (tramp-smb-get-share v2)
1293 v2 'file-error "Target `%s' must contain a share name" newname))
1294 (unless (tramp-smb-send-command
1295 v2 (format "rename \"%s\" \"%s\""
1296 (tramp-smb-get-localname v1)
1297 (tramp-smb-get-localname v2)))
1298 (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
1300 ;; We must rename via copy.
1301 (tramp-compat-copy-file filename newname ok-if-already-exists t t t)
1302 (if (file-directory-p filename)
1303 (tramp-compat-delete-directory filename 'recursive)
1304 (delete-file filename)))))
1306 (defun tramp-smb-action-set-acl (proc vec)
1307 "Read ACL data from connection buffer."
1308 (when (not (memq (process-status proc) '(run open)))
1309 ;; Accept pending output.
1310 (while (tramp-accept-process-output proc 0.1))
1311 (with-current-buffer (tramp-get-connection-buffer vec)
1312 (tramp-message vec 10 "\n%s" (buffer-string))
1313 (throw 'tramp-action 'ok))))
1315 (defun tramp-smb-handle-set-file-acl (filename acl-string)
1316 "Like `set-file-acl' for Tramp files."
1318 (with-parsed-tramp-file-name filename nil
1319 (when (and (stringp acl-string) (executable-find tramp-smb-acl-program))
1320 (setq tramp-current-method (tramp-file-name-method v)
1321 tramp-current-user (tramp-file-name-user v)
1322 tramp-current-host (tramp-file-name-real-host v))
1323 (tramp-set-file-property v localname "file-acl" 'undef)
1325 (let* ((real-user (tramp-file-name-real-user v))
1326 (real-host (tramp-file-name-real-host v))
1327 (domain (tramp-file-name-domain v))
1328 (port (tramp-file-name-port v))
1329 (share (tramp-smb-get-share v))
1330 (localname (tramp-compat-replace-regexp-in-string
1331 "\\\\" "/" (tramp-smb-get-localname v)))
1332 (args (list (concat "//" real-host "/" share) "-E" "-S"
1333 (tramp-compat-replace-regexp-in-string
1334 "\n" "," acl-string))))
1336 (if (not (zerop (length real-user)))
1337 (setq args (append args (list "-U" real-user)))
1338 (setq args (append args (list "-N"))))
1340 (when domain (setq args (append args (list "-W" domain))))
1341 (when port (setq args (append args (list "-p" port))))
1342 (when tramp-smb-conf
1343 (setq args (append args (list "-s" tramp-smb-conf))))
1346 (append args (list (shell-quote-argument localname)
1347 "&&" "echo" "tramp_exit_status" "0"
1348 "||" "echo" "tramp_exit_status" "1")))
1352 ;; Set the transfer process properties.
1353 (tramp-set-connection-property
1354 v "process-name" (buffer-name (current-buffer)))
1355 (tramp-set-connection-property
1356 v "process-buffer" (current-buffer))
1358 ;; Use an asynchronous processes. By this, password can
1362 (tramp-get-connection-name v)
1363 (tramp-get-connection-buffer v)
1364 tramp-smb-acl-program args)))
1367 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1368 (tramp-set-connection-property p "vector" v)
1369 (tramp-compat-set-process-query-on-exit-flag p nil)
1370 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1371 (goto-char (point-max))
1372 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1375 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1376 (skip-chars-forward "^ ")
1377 (when (zerop (read (current-buffer)))
1379 (tramp-set-file-property v localname "file-acl" acl-string)
1382 ;; Reset the transfer process properties.
1383 (tramp-set-connection-property v "process-name" nil)
1384 (tramp-set-connection-property v "process-buffer" nil)))))))
1386 (defun tramp-smb-handle-set-file-modes (filename mode)
1387 "Like `set-file-modes' for Tramp files."
1388 (with-parsed-tramp-file-name filename nil
1389 (when (tramp-smb-get-cifs-capabilities v)
1390 (tramp-flush-file-property v localname)
1391 (unless (tramp-smb-send-command
1392 v (format "chmod \"%s\" %s"
1393 (tramp-smb-get-localname v)
1394 (tramp-compat-decimal-to-octal mode)))
1396 v 'file-error "Error while changing file's mode %s" filename)))))
1398 ;; We use BUFFER also as connection buffer during setup. Because of
1399 ;; this, its original contents must be saved, and restored once
1400 ;; connection has been setup.
1401 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1402 "Like `start-file-process' for Tramp files."
1403 (with-parsed-tramp-file-name default-directory nil
1404 (let ((command (mapconcat 'identity (cons program args) " "))
1405 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1412 ;; BUFFER can be nil. We use a temporary buffer.
1413 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
1414 (while (get-process name1)
1415 ;; NAME must be unique as process name.
1417 name1 (format "%s<%d>" name i)))
1418 ;; Set the new process properties.
1419 (tramp-set-connection-property v "process-name" name1)
1420 (tramp-set-connection-property v "process-buffer" buffer)
1421 ;; Activate narrowing in order to save BUFFER contents.
1422 (with-current-buffer (tramp-get-connection-buffer v)
1423 (let ((buffer-undo-list t))
1424 (narrow-to-region (point-max) (point-max))
1425 (tramp-smb-call-winexe v)
1426 (when (tramp-smb-get-share v)
1427 (tramp-smb-send-command
1430 host (file-name-directory localname))))
1431 (tramp-message v 6 "(%s); exit" command)
1432 (tramp-send-string v command)))
1434 (tramp-get-connection-process v)))
1437 (with-current-buffer (tramp-get-connection-buffer v)
1438 (if (string-match tramp-temp-buffer-name (buffer-name))
1440 (set-process-buffer (tramp-get-connection-process v) nil)
1441 (kill-buffer (current-buffer)))
1442 (set-buffer-modified-p bmp)))
1443 (tramp-set-connection-property v "process-name" nil)
1444 (tramp-set-connection-property v "process-buffer" nil)))))
1446 (defun tramp-smb-handle-substitute-in-file-name (filename)
1447 "Like `handle-substitute-in-file-name' for Tramp files.
1448 \"//\" substitutes only in the local filename part. Catches
1449 errors for shares like \"C$/\", which are common in Microsoft Windows."
1450 (with-parsed-tramp-file-name filename nil
1451 ;; Ignore in LOCALNAME everything before "//".
1452 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1454 (concat (file-remote-p filename)
1455 (replace-match "\\1" nil nil localname)))))
1457 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1460 (defun tramp-smb-handle-write-region
1461 (start end filename &optional append visit lockname confirm)
1462 "Like `write-region' for Tramp files."
1463 (setq filename (expand-file-name filename))
1464 (with-parsed-tramp-file-name filename nil
1465 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1466 (when (and (not (featurep 'xemacs))
1467 confirm (file-exists-p filename))
1468 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1470 (tramp-error v 'file-error "File not overwritten")))
1471 ;; We must also flush the cache of the directory, because
1472 ;; `file-attributes' reads the values from there.
1473 (tramp-flush-file-property v (file-name-directory localname))
1474 (tramp-flush-file-property v localname)
1475 (let ((curbuf (current-buffer))
1476 (tmpfile (tramp-compat-make-temp-file filename)))
1477 (when (and append (file-exists-p filename))
1478 (copy-file filename tmpfile 'ok))
1479 ;; We say `no-message' here because we don't want the visited file
1480 ;; modtime data to be clobbered from the temp file. We call
1481 ;; `set-visited-file-modtime' ourselves later on.
1482 (tramp-run-real-handler
1484 (if confirm ; don't pass this arg unless defined for backward compat.
1485 (list start end tmpfile append 'no-message lockname confirm)
1486 (list start end tmpfile append 'no-message lockname)))
1488 (with-tramp-progress-reporter
1489 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1491 (unless (tramp-smb-send-command
1492 v (format "put %s \"%s\""
1493 tmpfile (tramp-smb-get-localname v)))
1494 (tramp-error v 'file-error "Cannot write `%s'" filename))
1495 (delete-file tmpfile)))
1497 (unless (equal curbuf (current-buffer))
1500 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1502 (set-visited-file-modtime)))))
1505 ;; Internal file name functions.
1507 (defun tramp-smb-get-share (vec)
1508 "Returns the share name of LOCALNAME."
1510 (let ((localname (tramp-file-name-localname vec)))
1511 (when (string-match "^/?\\([^/]+\\)/" localname)
1512 (match-string 1 localname)))))
1514 (defun tramp-smb-get-localname (vec)
1515 "Returns the file name of LOCALNAME.
1516 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1518 (let ((localname (tramp-file-name-localname vec)))
1521 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1522 ;; There is a share, separated by "/".
1523 (if (not (tramp-smb-get-cifs-capabilities vec))
1525 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1526 (match-string 1 localname) "")
1527 (match-string 1 localname))
1528 ;; There is just a share.
1529 (if (string-match "^/?\\([^/]+\\)$" localname)
1530 (match-string 1 localname)
1533 ;; Sometimes we have discarded `substitute-in-file-name'.
1534 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1535 (setq localname (replace-match "$" nil nil localname 1)))
1539 ;; Share names of a host are cached. It is very unlikely that the
1540 ;; shares do change during connection.
1541 (defun tramp-smb-get-file-entries (directory)
1542 "Read entries which match DIRECTORY.
1543 Either the shares are listed, or the `dir' command is executed.
1544 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1545 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1546 (setq localname (or localname "/"))
1547 (with-tramp-file-property v localname "file-entries"
1548 (with-current-buffer (tramp-get-connection-buffer v)
1549 (let* ((share (tramp-smb-get-share v))
1550 (cache (tramp-get-connection-property v "share-cache" nil))
1553 (if (and (not share) cache)
1554 ;; Return cached shares.
1559 (tramp-smb-send-command
1560 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1561 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1562 (tramp-smb-maybe-open-connection v))
1564 ;; Loop the listing.
1565 (goto-char (point-min))
1566 (if (re-search-forward tramp-smb-errors nil t)
1567 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1569 (setq entry (tramp-smb-read-file-entry share))
1571 (when entry (push entry res))))
1573 ;; Cache share entries.
1575 (tramp-set-connection-property v "share-cache" res)))
1577 ;; Add directory itself.
1578 (push '("" "drwxrwxrwx" 0 (0 0)) res)
1580 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1581 ;; If there's no short delay, it returns nil. No idea about.
1582 (when (featurep 'xemacs) (sleep-for 0.01))
1587 ;; Return either a share name (if SHARE is nil), or a file name.
1589 ;; If shares are listed, the following format is expected:
1591 ;; Disk| - leading spaces
1592 ;; [^|]+| - share name, 14 char
1595 ;; Entries provided by smbclient DIR aren't fully regular.
1596 ;; They should have the format
1598 ;; \s-\{2,2} - leading spaces
1599 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1600 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1601 ;; \s- - space delimiter
1602 ;; \s-+[0-9]+ - size, 8 chars, right bound
1603 ;; \s-\{2,2\} - space delimiter
1604 ;; \w\{3,3\} - weekday
1605 ;; \s- - space delimiter
1606 ;; \w\{3,3\} - month
1607 ;; \s- - space delimiter
1609 ;; \s- - space delimiter
1610 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1611 ;; \s- - space delimiter
1612 ;; [0-9]\{4,4\} - year
1614 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1615 ;; has function display_finfo:
1617 ;; d_printf(" %-30s%7.7s %8.0f %s",
1619 ;; attrib_string(finfo->mode),
1620 ;; (double)finfo->size,
1621 ;; asctime(LocalTime(&t)));
1623 ;; in Samba 1.9, there's the following code:
1625 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1626 ;; CNV_LANG(finfo->name),
1627 ;; attrib_string(finfo->mode),
1629 ;; asctime(LocalTime(&t))));
1632 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1633 ;; available in older Emacsen.
1634 ;; * The length of constructs (file name, size) might exceed the default.
1635 ;; * File names might contain spaces.
1636 ;; * Permissions might be empty.
1638 ;; So we try to analyze backwards.
1639 (defun tramp-smb-read-file-entry (share)
1640 "Parse entry in SMB output buffer.
1641 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1642 Result is the list (LOCALNAME MODE SIZE MTIME)."
1643 ;; We are called from `tramp-smb-get-file-entries', which sets the
1645 (let ((line (buffer-substring (point) (point-at-eol)))
1646 localname mode size month day hour min sec year mtime)
1650 ;; Read share entries.
1651 (when (string-match "^Disk|\\([^|]+\\)|" line)
1652 (setq localname (match-string 1 line)
1660 (if (string-match "\\([0-9]+\\)$" line)
1661 (setq year (string-to-number (match-string 1 line))
1662 line (substring line 0 -5))
1666 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1667 (setq hour (string-to-number (match-string 1 line))
1668 min (string-to-number (match-string 2 line))
1669 sec (string-to-number (match-string 3 line))
1670 line (substring line 0 -9))
1674 (if (string-match "\\([0-9]+\\)$" line)
1675 (setq day (string-to-number (match-string 1 line))
1676 line (substring line 0 -3))
1680 (if (string-match "\\(\\w+\\)$" line)
1681 (setq month (match-string 1 line)
1682 line (substring line 0 -4))
1686 (if (string-match "\\(\\w+\\)$" line)
1687 (setq line (substring line 0 -5))
1691 (if (string-match "\\([0-9]+\\)$" line)
1692 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1693 (setq size (string-to-number (match-string 1 line)))
1694 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1695 (setq length (+ length (match-end 0))))
1696 (setq line (substring line 0 length)))
1699 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1700 (if (string-match "\\([ADHRSV]+\\)?$" line)
1702 mode (or (match-string 1 line) "")
1703 mode (save-match-data (format
1705 (if (string-match "D" mode) "d" "-")
1707 (lambda (_x) "") " "
1708 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1709 line (substring line 0 -6))
1713 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1714 (setq localname (match-string 1 line))
1717 (when (and localname mode size)
1719 (if (and sec min hour day month year)
1722 (cdr (assoc (downcase month) tramp-parse-time-months))
1725 (list localname mode size mtime))))
1727 (defun tramp-smb-get-cifs-capabilities (vec)
1728 "Check, whether the SMB server supports POSIX commands."
1729 ;; When we are not logged in yet, we return nil.
1730 (if (let ((p (tramp-get-connection-process vec)))
1731 (and p (processp p) (memq (process-status p) '(run open))))
1732 (with-tramp-connection-property
1733 (tramp-get-connection-process vec) "cifs-capabilities"
1735 (when (tramp-smb-send-command vec "posix")
1736 (with-current-buffer (tramp-get-connection-buffer vec)
1737 (goto-char (point-min))
1739 (re-search-forward "Server supports CIFS capabilities" nil t)
1743 (buffer-substring (point) (point-at-eol)) nil t)))))))))
1745 (defun tramp-smb-get-stat-capability (vec)
1746 "Check, whether the SMB server supports the STAT command."
1747 ;; When we are not logged in yet, we return nil.
1748 (if (and (tramp-smb-get-share vec)
1749 (let ((p (tramp-get-connection-process vec)))
1750 (and p (processp p) (memq (process-status p) '(run open)))))
1751 (with-tramp-connection-property
1752 (tramp-get-connection-process vec) "stat-capability"
1753 (tramp-smb-send-command vec "stat \"/\""))))
1756 ;; Connection functions.
1758 (defun tramp-smb-send-command (vec command)
1759 "Send the COMMAND to connection VEC.
1760 Returns nil if there has been an error message from smbclient."
1761 (tramp-smb-maybe-open-connection vec)
1762 (tramp-message vec 6 "%s" command)
1763 (tramp-send-string vec command)
1764 (tramp-smb-wait-for-output vec))
1766 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1767 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1768 Does not do anything if a connection is already open, but re-opens the
1769 connection if a previous connection has died for some reason.
1770 If ARGUMENT is non-nil, use it as argument for
1771 `tramp-smb-winexe-program', and suppress any checks."
1772 (tramp-check-proper-method-and-host vec)
1774 (let* ((share (tramp-smb-get-share vec))
1775 (buf (tramp-get-connection-buffer vec))
1776 (p (get-buffer-process buf)))
1778 ;; Check whether we still have the same smbclient version.
1779 ;; Otherwise, we must delete the connection cache, because
1780 ;; capabilities migh have changed.
1781 (unless (or argument (processp p))
1782 (let ((default-directory (tramp-compat-temporary-file-directory))
1783 (command (concat tramp-smb-program " -V")))
1785 (unless tramp-smb-version
1786 (unless (executable-find tramp-smb-program)
1789 "Cannot find command %s in %s" tramp-smb-program exec-path))
1790 (setq tramp-smb-version (shell-command-to-string command))
1791 (tramp-message vec 6 command)
1792 (tramp-message vec 6 "\n%s" tramp-smb-version)
1793 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1794 (setq tramp-smb-version
1795 (replace-match "" nil nil tramp-smb-version))))
1797 (unless (string-equal
1799 (tramp-get-connection-property
1800 vec "smbclient-version" tramp-smb-version))
1801 (tramp-flush-directory-property vec "")
1802 (tramp-flush-connection-property vec))
1804 (tramp-set-connection-property
1805 vec "smbclient-version" tramp-smb-version)))
1807 ;; If too much time has passed since last command was sent, look
1808 ;; whether there has been an error message; maybe due to
1809 ;; connection timeout.
1810 (with-current-buffer buf
1811 (goto-char (point-min))
1812 (when (and (> (tramp-time-diff
1814 (tramp-get-connection-property
1815 p "last-cmd-time" '(0 0 0)))
1817 p (processp p) (memq (process-status p) '(run open))
1818 (re-search-forward tramp-smb-errors nil t))
1822 ;; Check whether it is still the same share.
1824 (and p (processp p) (memq (process-status p) '(run open))
1828 (tramp-get-connection-property p "smb-share" ""))))
1831 ;; There might be unread output from checking for share names.
1832 (when buf (with-current-buffer buf (erase-buffer)))
1833 (when (and p (processp p)) (delete-process p))
1835 (let* ((user (tramp-file-name-user vec))
1836 (host (tramp-file-name-host vec))
1837 (real-user (tramp-file-name-real-user vec))
1838 (real-host (tramp-file-name-real-host vec))
1839 (domain (tramp-file-name-domain vec))
1840 (port (tramp-file-name-port vec))
1845 (setq args (list (concat "//" real-host))))
1847 (setq args (list (concat "//" real-host "/" share))))
1849 (setq args (list "-g" "-L" real-host ))))
1851 (if (not (zerop (length real-user)))
1852 (setq args (append args (list "-U" real-user)))
1853 (setq args (append args (list "-N"))))
1855 (when domain (setq args (append args (list "-W" domain))))
1856 (when port (setq args (append args (list "-p" port))))
1857 (when tramp-smb-conf
1858 (setq args (append args (list "-s" tramp-smb-conf))))
1860 (setq args (append args (list argument))))
1863 (with-tramp-progress-reporter
1865 (format "Opening connection for //%s%s/%s"
1866 (if (not (zerop (length user))) (concat user "@") "")
1869 (let* ((coding-system-for-read nil)
1870 (process-connection-type tramp-process-connection-type)
1871 (p (let ((default-directory
1872 (tramp-compat-temporary-file-directory)))
1873 (apply #'start-process
1874 (tramp-get-connection-name vec)
1875 (tramp-get-connection-buffer vec)
1877 tramp-smb-winexe-program tramp-smb-program)
1881 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1882 (tramp-set-connection-property p "vector" vec)
1883 (tramp-compat-set-process-query-on-exit-flag p nil)
1885 ;; Set variables for computing the prompt for reading password.
1886 (setq tramp-current-method tramp-smb-method
1887 tramp-current-user user
1888 tramp-current-host host)
1891 (let (tramp-message-show-message)
1892 ;; Play login scenario.
1893 (tramp-process-actions
1895 (if (or argument share)
1896 tramp-smb-actions-with-share
1897 tramp-smb-actions-without-share))
1899 ;; Check server version.
1901 (with-current-buffer (tramp-get-connection-buffer vec)
1902 (goto-char (point-min))
1903 (search-forward-regexp tramp-smb-server-version nil t)
1904 (let ((smbserver-version (match-string 0)))
1908 (tramp-get-connection-property
1909 vec "smbserver-version" smbserver-version))
1910 (tramp-flush-directory-property vec "")
1911 (tramp-flush-connection-property vec))
1912 (tramp-set-connection-property
1913 vec "smbserver-version" smbserver-version))))
1915 ;; Set chunksize to 1. smbclient reads its input
1916 ;; character by character; if we send the string
1917 ;; at once, it is read painfully slow.
1918 (tramp-set-connection-property p "smb-share" share)
1919 (tramp-set-connection-property p "chunksize" 1))
1921 ;; Check for the error reason. If it was due to wrong
1922 ;; password, reestablish the connection. We cannot
1923 ;; handle this in `tramp-process-actions', because
1924 ;; smbclient does not ask for the password, again.
1926 (with-current-buffer (tramp-get-connection-buffer vec)
1927 (goto-char (point-min))
1928 (if (and (boundp 'auth-sources)
1929 (symbol-value 'auth-sources)
1930 (search-forward-regexp
1931 tramp-smb-wrong-passwd-regexp nil t))
1932 ;; Disable `auth-source' and `password-cache'.
1935 vec 3 "Retry connection with new password")
1936 (tramp-cleanup-connection vec t)
1937 (tramp-smb-maybe-open-connection vec argument))
1938 ;; Propagate the error.
1939 (signal (car err) (cdr err)))))))))))))
1941 ;; We don't use timeouts. If needed, the caller shall wrap around.
1942 (defun tramp-smb-wait-for-output (vec)
1943 "Wait for output from smbclient command.
1944 Returns nil if an error message has appeared."
1945 (with-current-buffer (tramp-get-connection-buffer vec)
1946 (let ((p (get-buffer-process (current-buffer)))
1947 (found (progn (goto-char (point-min))
1948 (re-search-forward tramp-smb-prompt nil t)))
1949 (err (progn (goto-char (point-min))
1950 (re-search-forward tramp-smb-errors nil t)))
1953 ;; Algorithm: get waiting output. See if last line contains
1954 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1955 ;; If not, wait a bit and again get waiting output.
1956 (while (and (not found) (not err) (memq (process-status p) '(run open)))
1958 ;; Accept pending output.
1959 (tramp-accept-process-output p 0.1)
1961 ;; Search for prompt.
1962 (goto-char (point-min))
1963 (setq found (re-search-forward tramp-smb-prompt nil t))
1965 ;; Search for errors.
1966 (goto-char (point-min))
1967 (setq err (re-search-forward tramp-smb-errors nil t)))
1969 ;; When the process is still alive, read pending output.
1970 (while (and (not found) (memq (process-status p) '(run open)))
1972 ;; Accept pending output.
1973 (tramp-accept-process-output p 0.1)
1975 ;; Search for prompt.
1976 (goto-char (point-min))
1977 (setq found (re-search-forward tramp-smb-prompt nil t)))
1979 (tramp-message vec 6 "\n%s" (buffer-string))
1983 (goto-char (point-max))
1984 (re-search-backward tramp-smb-prompt nil t)
1985 (delete-region (point) (point-max)))
1987 ;; Return value is whether no error message has appeared.
1990 (defun tramp-smb-kill-winexe-function ()
1991 "Send SIGKILL to the winexe process."
1993 (let ((p (get-buffer-process (current-buffer))))
1994 (when (and p (processp p) (memq (process-status p) '(run open)))
1995 (signal-process (process-id p) 'SIGINT)))))
1997 (defun tramp-smb-call-winexe (vec)
1998 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2000 ;; Check for program.
2001 (unless (executable-find tramp-smb-winexe-program)
2003 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
2005 ;; winexe does not supports ports.
2006 (when (tramp-file-name-port vec)
2007 (tramp-error vec 'file-error "Port not supported for remote processes"))
2009 (tramp-smb-maybe-open-connection
2013 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2015 (set (make-local-variable 'kill-buffer-hook)
2016 '(tramp-smb-kill-winexe-function))
2018 ;; Suppress "^M". Shouldn't we specify utf8?
2019 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2021 ;; Set width to 128. This avoids mixing prompt and long error messages.
2022 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2023 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2024 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2025 (tramp-smb-send-command vec "$bufsize.Width = 128")
2026 (tramp-smb-send-command vec "$winsize.Width = 128")
2027 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2028 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2030 (defun tramp-smb-shell-quote-argument (s)
2031 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2032 (let ((system-type 'ms-dos))
2033 (shell-quote-argument s)))
2035 (add-hook 'tramp-unload-hook
2037 (unload-feature 'tramp-smb 'force)))
2039 (provide 'tramp-smb)
2043 ;; * Return more comprehensive file permission string.
2044 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2045 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2046 ;; * Ignore case in file names.
2048 ;;; tramp-smb.el ends here