Fix a comment whitespace typo.
[emacs.git] / lisp / net / tramp-smb.el
blob367beb823aaf828998e20b3678e7f1708eaadd31
1 ;;; tramp-smb.el --- Tramp access functions for SMB servers -*- lexical-binding:t -*-
3 ;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
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/>.
24 ;;; Commentary:
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
28 ;;; Code:
30 (require 'tramp)
32 ;; Define SMB method ...
33 ;;;###tramp-autoload
34 (defconst tramp-smb-method "smb"
35 "Method to connect SAMBA and M$ SMB servers.")
37 ;; ... and add it to the method list.
38 ;;;###tramp-autoload
39 (unless (memq system-type '(cygwin windows-nt))
40 (add-to-list 'tramp-methods
41 `(,tramp-smb-method
42 ;; We define an empty command, because `tramp-smb-call-winexe'
43 ;; opens already the powershell. Used in `tramp-handle-shell-command'.
44 (tramp-remote-shell "")
45 ;; This is just a guess. We don't know whether the share "C$"
46 ;; is available for public use, and whether the user has write
47 ;; access.
48 (tramp-tmpdir "/C$/Temp")
49 ;; Another guess. We might implement a better check later on.
50 (tramp-case-insensitive t))))
52 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
53 ;; the anonymous user is chosen.
54 ;;;###tramp-autoload
55 (add-to-list 'tramp-default-user-alist
56 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
58 ;; Add completion function for SMB method.
59 ;;;###tramp-autoload
60 (eval-after-load 'tramp
61 '(tramp-set-completion-function
62 tramp-smb-method
63 '((tramp-parse-netrc "~/.netrc"))))
65 ;;;###tramp-autoload
66 (defcustom tramp-smb-program "smbclient"
67 "Name of SMB client to run."
68 :group 'tramp
69 :type 'string
70 :require 'tramp)
72 ;;;###tramp-autoload
73 (defcustom tramp-smb-acl-program "smbcacls"
74 "Name of SMB acls to run."
75 :group 'tramp
76 :type 'string
77 :version "24.4"
78 :require 'tramp)
80 ;;;###tramp-autoload
81 (defcustom tramp-smb-conf "/dev/null"
82 "Path of the smb.conf file.
83 If it is nil, no smb.conf will be added to the `tramp-smb-program'
84 call, letting the SMB client use the default one."
85 :group 'tramp
86 :type '(choice (const nil) (file :must-match t))
87 :require 'tramp)
89 (defvar tramp-smb-version nil
90 "Version string of the SMB client.")
92 (defconst tramp-smb-server-version
93 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
94 "Regexp of SMB server identification.")
96 (defconst tramp-smb-prompt "^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
97 "Regexp used as prompt in smbclient or powershell.")
99 (defconst tramp-smb-wrong-passwd-regexp
100 (regexp-opt
101 '("NT_STATUS_LOGON_FAILURE"
102 "NT_STATUS_WRONG_PASSWORD"))
103 "Regexp for login error strings of SMB servers.")
105 (defconst tramp-smb-errors
106 (mapconcat
107 'identity
108 `(;; Connection error / timeout / unknown command.
109 "Connection\\( to \\S-+\\)? failed"
110 "Read from server failed, maybe it closed the connection"
111 "Call timed out: server did not respond"
112 "\\S-+: command not found"
113 "Server doesn't support UNIX CIFS calls"
114 ,(regexp-opt
115 '(;; Samba.
116 "ERRDOS"
117 "ERRHRD"
118 "ERRSRV"
119 "ERRbadfile"
120 "ERRbadpw"
121 "ERRfilexists"
122 "ERRnoaccess"
123 "ERRnomem"
124 "ERRnosuchshare"
125 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
126 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
127 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7),
128 ;; Windows 6.3 (Windows Server 2012, Windows 10).
129 "NT_STATUS_ACCESS_DENIED"
130 "NT_STATUS_ACCOUNT_LOCKED_OUT"
131 "NT_STATUS_BAD_NETWORK_NAME"
132 "NT_STATUS_CANNOT_DELETE"
133 "NT_STATUS_CONNECTION_REFUSED"
134 "NT_STATUS_DIRECTORY_NOT_EMPTY"
135 "NT_STATUS_DUPLICATE_NAME"
136 "NT_STATUS_FILE_IS_A_DIRECTORY"
137 "NT_STATUS_HOST_UNREACHABLE"
138 "NT_STATUS_IMAGE_ALREADY_LOADED"
139 "NT_STATUS_INVALID_LEVEL"
140 "NT_STATUS_INVALID_PARAMETER_MIX"
141 "NT_STATUS_IO_TIMEOUT"
142 "NT_STATUS_LOGON_FAILURE"
143 "NT_STATUS_NETWORK_ACCESS_DENIED"
144 "NT_STATUS_NOT_IMPLEMENTED"
145 "NT_STATUS_NO_LOGON_SERVERS"
146 "NT_STATUS_NO_SUCH_FILE"
147 "NT_STATUS_NO_SUCH_USER"
148 "NT_STATUS_OBJECT_NAME_COLLISION"
149 "NT_STATUS_OBJECT_NAME_INVALID"
150 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
151 "NT_STATUS_PASSWORD_MUST_CHANGE"
152 "NT_STATUS_SHARING_VIOLATION"
153 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
154 "NT_STATUS_UNSUCCESSFUL"
155 "NT_STATUS_WRONG_PASSWORD")))
156 "\\|")
157 "Regexp for possible error strings of SMB servers.
158 Used instead of analyzing error codes of commands.")
160 (defconst tramp-smb-actions-with-share
161 '((tramp-smb-prompt tramp-action-succeed)
162 (tramp-password-prompt-regexp tramp-action-password)
163 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
164 (tramp-smb-errors tramp-action-permission-denied)
165 (tramp-process-alive-regexp tramp-action-process-alive))
166 "List of pattern/action pairs.
167 This list is used for login to SMB servers.
169 See `tramp-actions-before-shell' for more info.")
171 (defconst tramp-smb-actions-without-share
172 '((tramp-password-prompt-regexp tramp-action-password)
173 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
174 (tramp-smb-errors tramp-action-permission-denied)
175 (tramp-process-alive-regexp tramp-action-out-of-band))
176 "List of pattern/action pairs.
177 This list is used for login to SMB servers.
179 See `tramp-actions-before-shell' for more info.")
181 (defconst tramp-smb-actions-with-tar
182 '((tramp-password-prompt-regexp tramp-action-password)
183 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
184 (tramp-smb-errors tramp-action-permission-denied)
185 (tramp-process-alive-regexp tramp-smb-action-with-tar))
186 "List of pattern/action pairs.
187 This list is used for tar-like copy of directories.
189 See `tramp-actions-before-shell' for more info.")
191 (defconst tramp-smb-actions-get-acl
192 '((tramp-password-prompt-regexp tramp-action-password)
193 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
194 (tramp-smb-errors tramp-action-permission-denied)
195 (tramp-process-alive-regexp tramp-smb-action-get-acl))
196 "List of pattern/action pairs.
197 This list is used for smbcacls actions.
199 See `tramp-actions-before-shell' for more info.")
201 (defconst tramp-smb-actions-set-acl
202 '((tramp-password-prompt-regexp tramp-action-password)
203 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
204 (tramp-smb-errors tramp-action-permission-denied)
205 (tramp-process-alive-regexp tramp-smb-action-set-acl))
206 "List of pattern/action pairs.
207 This list is used for smbcacls actions.
209 See `tramp-actions-before-shell' for more info.")
211 ;; New handlers should be added here.
212 ;;;###tramp-autoload
213 (defconst tramp-smb-file-name-handler-alist
214 '(;; `access-file' performed by default handler.
215 (add-name-to-file . tramp-smb-handle-add-name-to-file)
216 ;; `byte-compiler-base-file-name' performed by default handler.
217 (copy-directory . tramp-smb-handle-copy-directory)
218 (copy-file . tramp-smb-handle-copy-file)
219 (delete-directory . tramp-smb-handle-delete-directory)
220 (delete-file . tramp-smb-handle-delete-file)
221 ;; `diff-latest-backup-file' performed by default handler.
222 (directory-file-name . tramp-handle-directory-file-name)
223 (directory-files . tramp-smb-handle-directory-files)
224 (directory-files-and-attributes
225 . tramp-handle-directory-files-and-attributes)
226 (dired-compress-file . ignore)
227 (dired-uncache . tramp-handle-dired-uncache)
228 (expand-file-name . tramp-smb-handle-expand-file-name)
229 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
230 (file-acl . tramp-smb-handle-file-acl)
231 (file-attributes . tramp-smb-handle-file-attributes)
232 (file-directory-p . tramp-smb-handle-file-directory-p)
233 (file-file-equal-p . tramp-handle-file-equal-p)
234 (file-executable-p . tramp-handle-file-exists-p)
235 (file-exists-p . tramp-handle-file-exists-p)
236 (file-in-directory-p . tramp-handle-file-in-directory-p)
237 (file-local-copy . tramp-smb-handle-file-local-copy)
238 (file-modes . tramp-handle-file-modes)
239 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
240 (file-name-as-directory . tramp-handle-file-name-as-directory)
241 (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
242 (file-name-completion . tramp-handle-file-name-completion)
243 (file-name-directory . tramp-handle-file-name-directory)
244 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
245 ;; `file-name-sans-versions' performed by default handler.
246 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
247 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
248 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
249 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
250 (file-ownership-preserved-p . ignore)
251 (file-readable-p . tramp-handle-file-exists-p)
252 (file-regular-p . tramp-handle-file-regular-p)
253 (file-remote-p . tramp-handle-file-remote-p)
254 ;; `file-selinux-context' performed by default handler.
255 (file-symlink-p . tramp-handle-file-symlink-p)
256 ;; `file-truename' performed by default handler.
257 (file-writable-p . tramp-smb-handle-file-writable-p)
258 (find-backup-file-name . tramp-handle-find-backup-file-name)
259 ;; `find-file-noselect' performed by default handler.
260 ;; `get-file-buffer' performed by default handler.
261 (insert-directory . tramp-smb-handle-insert-directory)
262 (insert-file-contents . tramp-handle-insert-file-contents)
263 (load . tramp-handle-load)
264 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
265 (make-directory . tramp-smb-handle-make-directory)
266 (make-directory-internal . tramp-smb-handle-make-directory-internal)
267 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
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 (temporary-file-directory . tramp-handle-temporary-file-directory)
280 (unhandled-file-name-directory . ignore)
281 (vc-registered . ignore)
282 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
283 (write-region . tramp-smb-handle-write-region))
284 "Alist of handler functions for Tramp SMB method.
285 Operations not mentioned here will be handled by the default Emacs primitives.")
287 ;; Options for remote processes via winexe.
288 ;;;###tramp-autoload
289 (defcustom tramp-smb-winexe-program "winexe"
290 "Name of winexe client to run.
291 If it isn't found in the local $PATH, the absolute path of winexe
292 shall be given. This is needed for remote processes."
293 :group 'tramp
294 :type 'string
295 :version "24.3"
296 :require 'tramp)
298 ;;;###tramp-autoload
299 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
300 "Shell to be used for processes on remote machines.
301 This must be Powershell V2 compatible."
302 :group 'tramp
303 :type 'string
304 :version "24.3"
305 :require 'tramp)
307 ;;;###tramp-autoload
308 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
309 "Command switch used together with `tramp-smb-winexe-shell-command'.
310 This can be used to disable echo etc."
311 :group 'tramp
312 :type 'string
313 :version "24.3"
314 :require 'tramp)
316 ;; It must be a `defsubst' in order to push the whole code into
317 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
318 ;;;###tramp-autoload
319 (defsubst tramp-smb-file-name-p (filename)
320 "Check if it's a filename for SMB servers."
321 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
322 tramp-smb-method))
324 ;;;###tramp-autoload
325 (defun tramp-smb-file-name-handler (operation &rest args)
326 "Invoke the SMB related OPERATION.
327 First arg specifies the OPERATION, second arg is a list of arguments to
328 pass to the OPERATION."
329 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
330 (if fn
331 (save-match-data (apply (cdr fn) args))
332 (tramp-run-real-handler operation args))))
334 ;;;###tramp-autoload
335 (unless (memq system-type '(cygwin windows-nt))
336 (tramp-register-foreign-file-name-handler
337 'tramp-smb-file-name-p 'tramp-smb-file-name-handler))
339 ;; File name primitives.
341 (defun tramp-smb-handle-add-name-to-file
342 (filename newname &optional ok-if-already-exists)
343 "Like `add-name-to-file' for Tramp files."
344 (unless (tramp-equal-remote filename newname)
345 (with-parsed-tramp-file-name
346 (if (tramp-tramp-file-p filename) filename newname) nil
347 (tramp-error
348 v 'file-error
349 "add-name-to-file: %s"
350 "only implemented for same method, same user, same host")))
351 (with-parsed-tramp-file-name filename v1
352 (with-parsed-tramp-file-name newname v2
353 (when (file-directory-p filename)
354 (tramp-error
355 v2 'file-error
356 "add-name-to-file: %s must not be a directory" filename))
357 (when (and (not ok-if-already-exists)
358 (file-exists-p newname)
359 (not (numberp ok-if-already-exists))
360 (y-or-n-p
361 (format
362 "File %s already exists; make it a new name anyway? "
363 newname)))
364 (tramp-error
365 v2 'file-error
366 "add-name-to-file: file %s already exists" newname))
367 ;; We must also flush the cache of the directory, because
368 ;; `file-attributes' reads the values from there.
369 (tramp-flush-file-property v2 (file-name-directory v2-localname))
370 (tramp-flush-file-property v2 v2-localname)
371 (unless
372 (tramp-smb-send-command
374 (format
375 "%s \"%s\" \"%s\""
376 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
377 (tramp-smb-get-localname v1)
378 (tramp-smb-get-localname v2)))
379 (tramp-error
380 v2 'file-error
381 "error with add-name-to-file, see buffer `%s' for details"
382 (buffer-name))))))
384 (defun tramp-smb-action-with-tar (proc vec)
385 "Untar from connection buffer."
386 (if (not (process-live-p proc))
387 (throw 'tramp-action 'process-died)
389 (with-current-buffer (tramp-get-connection-buffer vec)
390 (goto-char (point-min))
391 (when (search-forward-regexp tramp-smb-server-version nil t)
392 ;; There might be a hidden password prompt.
393 (widen)
394 (forward-line)
395 (tramp-message vec 6 (buffer-substring (point-min) (point)))
396 (delete-region (point-min) (point))
397 (throw 'tramp-action 'ok)))))
399 (defun tramp-smb-handle-copy-directory
400 (dirname newname &optional keep-date parents copy-contents)
401 "Like `copy-directory' for Tramp files."
402 (if copy-contents
403 ;; We must do it file-wise.
404 (tramp-run-real-handler
405 'copy-directory (list dirname newname keep-date parents copy-contents))
407 (setq dirname (expand-file-name dirname)
408 newname (expand-file-name newname))
409 (let ((t1 (tramp-tramp-file-p dirname))
410 (t2 (tramp-tramp-file-p newname)))
411 (with-parsed-tramp-file-name (if t1 dirname newname) nil
412 (with-tramp-progress-reporter
413 v 0 (format "Copying %s to %s" dirname newname)
414 (cond
415 ;; We must use a local temporary directory.
416 ((and t1 t2)
417 (let ((tmpdir
418 (make-temp-name
419 (expand-file-name
420 tramp-temp-name-prefix
421 (tramp-compat-temporary-file-directory)))))
422 (unwind-protect
423 (progn
424 (make-directory tmpdir)
425 (copy-directory dirname tmpdir keep-date 'parents)
426 (copy-directory
427 (expand-file-name (file-name-nondirectory dirname) tmpdir)
428 newname keep-date parents))
429 (delete-directory tmpdir 'recursive))))
431 ;; We can copy recursively.
432 ((and (or t1 t2) (tramp-smb-get-cifs-capabilities v))
433 (when (and (file-directory-p newname)
434 (not (string-equal (file-name-nondirectory dirname)
435 (file-name-nondirectory newname))))
436 (setq newname
437 (expand-file-name
438 (file-name-nondirectory dirname) newname))
439 (if t2 (setq v (tramp-dissect-file-name newname))))
440 (if (not (file-directory-p newname))
441 (make-directory newname parents))
443 ;; Set variables for computing the prompt for reading password.
444 (setq tramp-current-method method
445 tramp-current-user user
446 tramp-current-domain domain
447 tramp-current-host host
448 tramp-current-port port)
450 (let* ((share (tramp-smb-get-share v))
451 (localname (file-name-as-directory
452 (replace-regexp-in-string
453 "\\\\" "/" (tramp-smb-get-localname v))))
454 (tmpdir (make-temp-name
455 (expand-file-name
456 tramp-temp-name-prefix
457 (tramp-compat-temporary-file-directory))))
458 (args (list (concat "//" host "/" share) "-E")))
460 (if (not (zerop (length user)))
461 (setq args (append args (list "-U" user)))
462 (setq args (append args (list "-N"))))
464 (when domain (setq args (append args (list "-W" domain))))
465 (when port (setq args (append args (list "-p" port))))
466 (when tramp-smb-conf
467 (setq args (append args (list "-s" tramp-smb-conf))))
468 (setq args
469 (if t1
470 ;; Source is remote.
471 (append args
472 (list "-D" (tramp-unquote-shell-quote-argument
473 localname)
474 "-c" (shell-quote-argument "tar qc - *")
475 "|" "tar" "xfC" "-"
476 (tramp-unquote-shell-quote-argument
477 tmpdir)))
478 ;; Target is remote.
479 (append (list "tar" "cfC" "-"
480 (tramp-unquote-shell-quote-argument dirname)
481 "." "|")
482 args
483 (list "-D" (tramp-unquote-shell-quote-argument
484 localname)
485 "-c" (shell-quote-argument "tar qx -")))))
487 (unwind-protect
488 (with-temp-buffer
489 ;; Set the transfer process properties.
490 (tramp-set-connection-property
491 v "process-name" (buffer-name (current-buffer)))
492 (tramp-set-connection-property
493 v "process-buffer" (current-buffer))
495 (when t1
496 ;; The smbclient tar command creates always
497 ;; complete paths. We must emulate the
498 ;; directory structure, and symlink to the real
499 ;; target.
500 (make-directory
501 (expand-file-name
502 ".." (concat tmpdir localname))
503 'parents)
504 (make-symbolic-link
505 newname (directory-file-name (concat tmpdir localname))))
507 ;; Use an asynchronous processes. By this,
508 ;; password can be handled.
509 (let* ((default-directory tmpdir)
510 (p (apply
511 'start-process
512 (tramp-get-connection-name v)
513 (tramp-get-connection-buffer v)
514 tramp-smb-program args)))
516 (tramp-message
517 v 6 "%s" (mapconcat 'identity (process-command p) " "))
518 (tramp-set-connection-property p "vector" v)
519 (process-put p 'adjust-window-size-function 'ignore)
520 (set-process-query-on-exit-flag p nil)
521 (tramp-process-actions p v nil tramp-smb-actions-with-tar)
523 (while (process-live-p p)
524 (sit-for 0.1))
525 (tramp-message v 6 "\n%s" (buffer-string))))
527 ;; Reset the transfer process properties.
528 (tramp-set-connection-property v "process-name" nil)
529 (tramp-set-connection-property v "process-buffer" nil)
530 (when t1 (delete-directory tmpdir 'recurse))))
532 ;; Handle KEEP-DATE argument.
533 (when keep-date
534 (set-file-times
535 newname
536 (tramp-compat-file-attribute-modification-time
537 (file-attributes dirname))))
539 ;; Set the mode.
540 (unless keep-date
541 (set-file-modes newname (tramp-default-file-modes dirname)))
543 ;; When newname did exist, we have wrong cached values.
544 (when t2
545 (with-parsed-tramp-file-name newname nil
546 (tramp-flush-file-property v (file-name-directory localname))
547 (tramp-flush-file-property v localname))))
549 ;; We must do it file-wise.
551 (tramp-run-real-handler
552 'copy-directory (list dirname newname keep-date parents)))))))))
554 (defun tramp-smb-handle-copy-file
555 (filename newname &optional ok-if-already-exists keep-date
556 _preserve-uid-gid _preserve-extended-attributes)
557 "Like `copy-file' for Tramp files.
558 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
559 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
560 (setq filename (expand-file-name filename)
561 newname (expand-file-name newname))
562 (with-tramp-progress-reporter
563 (tramp-dissect-file-name
564 (if (tramp-tramp-file-p filename) filename newname))
565 0 (format "Copying %s to %s" filename newname)
567 (if (file-directory-p filename)
568 (copy-directory
569 filename newname keep-date 'parents 'copy-contents)
571 (let ((tmpfile (file-local-copy filename)))
572 (if tmpfile
573 ;; Remote filename.
574 (condition-case err
575 (rename-file tmpfile newname ok-if-already-exists)
576 ((error quit)
577 (delete-file tmpfile)
578 (signal (car err) (cdr err))))
580 ;; Remote newname.
581 (when (file-directory-p newname)
582 (setq newname
583 (expand-file-name (file-name-nondirectory filename) newname)))
585 (with-parsed-tramp-file-name newname nil
586 (when (and (not ok-if-already-exists)
587 (file-exists-p newname))
588 (tramp-error v 'file-already-exists newname))
590 ;; We must also flush the cache of the directory, because
591 ;; `file-attributes' reads the values from there.
592 (tramp-flush-file-property v (file-name-directory localname))
593 (tramp-flush-file-property v localname)
594 (unless (tramp-smb-get-share v)
595 (tramp-error
596 v 'file-error "Target `%s' must contain a share name" newname))
597 (unless (tramp-smb-send-command
598 v (format "put \"%s\" \"%s\""
599 (tramp-compat-file-name-unquote filename)
600 (tramp-smb-get-localname v)))
601 (tramp-error
602 v 'file-error "Cannot copy `%s' to `%s'" filename newname))))))
604 ;; KEEP-DATE handling.
605 (when keep-date
606 (set-file-times
607 newname
608 (tramp-compat-file-attribute-modification-time
609 (file-attributes filename))))))
611 (defun tramp-smb-handle-delete-directory (directory &optional recursive _trash)
612 "Like `delete-directory' for Tramp files."
613 (setq directory (directory-file-name (expand-file-name directory)))
614 (when (file-exists-p directory)
615 (when recursive
616 (mapc
617 (lambda (file)
618 (if (file-directory-p file)
619 (delete-directory file recursive)
620 (delete-file file)))
621 ;; We do not want to delete "." and "..".
622 (directory-files directory 'full directory-files-no-dot-files-regexp)))
624 (with-parsed-tramp-file-name directory nil
625 ;; We must also flush the cache of the directory, because
626 ;; `file-attributes' reads the values from there.
627 (tramp-flush-file-property v (file-name-directory localname))
628 (tramp-flush-directory-property v localname)
629 (unless (tramp-smb-send-command
630 v (format
631 "%s \"%s\""
632 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
633 (tramp-smb-get-localname v)))
634 ;; Error.
635 (with-current-buffer (tramp-get-connection-buffer v)
636 (goto-char (point-min))
637 (search-forward-regexp tramp-smb-errors nil t)
638 (tramp-error
639 v 'file-error "%s `%s'" (match-string 0) directory))))))
641 (defun tramp-smb-handle-delete-file (filename &optional _trash)
642 "Like `delete-file' for Tramp files."
643 (setq filename (expand-file-name filename))
644 (when (file-exists-p filename)
645 (with-parsed-tramp-file-name filename nil
646 ;; We must also flush the cache of the directory, because
647 ;; `file-attributes' reads the values from there.
648 (tramp-flush-file-property v (file-name-directory localname))
649 (tramp-flush-file-property v localname)
650 (unless (tramp-smb-send-command
651 v (format
652 "%s \"%s\""
653 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
654 (tramp-smb-get-localname v)))
655 ;; Error.
656 (with-current-buffer (tramp-get-connection-buffer v)
657 (goto-char (point-min))
658 (search-forward-regexp tramp-smb-errors nil t)
659 (tramp-error
660 v 'file-error "%s `%s'" (match-string 0) filename))))))
662 (defun tramp-smb-handle-directory-files
663 (directory &optional full match nosort)
664 "Like `directory-files' for Tramp files."
665 (let ((result (mapcar 'directory-file-name
666 (file-name-all-completions "" directory))))
667 ;; Discriminate with regexp.
668 (when match
669 (setq result
670 (delete nil
671 (mapcar (lambda (x) (when (string-match match x) x))
672 result))))
673 ;; Append directory.
674 (when full
675 (setq result
676 (mapcar
677 (lambda (x) (format "%s/%s" directory x))
678 result)))
679 ;; Sort them if necessary.
680 (unless nosort (setq result (sort result 'string-lessp)))
681 result))
683 (defun tramp-smb-handle-expand-file-name (name &optional dir)
684 "Like `expand-file-name' for Tramp files."
685 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
686 (setq dir (or dir default-directory "/"))
687 ;; Unless NAME is absolute, concat DIR and NAME.
688 (unless (file-name-absolute-p name)
689 (setq name (concat (file-name-as-directory dir) name)))
690 ;; If NAME is not a Tramp file, run the real handler.
691 (if (not (tramp-tramp-file-p name))
692 (tramp-run-real-handler 'expand-file-name (list name nil))
693 ;; Dissect NAME.
694 (with-parsed-tramp-file-name name nil
695 ;; Tilde expansion if necessary. We use the user name as share,
696 ;; which is often the case in domains.
697 (when (string-match "\\`/?~\\([^/]*\\)" localname)
698 (setq localname
699 (replace-match
700 (if (zerop (length (match-string 1 localname)))
701 user
702 (match-string 1 localname))
703 nil nil localname)))
704 ;; Make the file name absolute.
705 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
706 (setq localname (concat "/" localname)))
707 ;; No tilde characters in file name, do normal
708 ;; `expand-file-name' (this does "/./" and "/../").
709 (tramp-make-tramp-file-name
710 method user domain host port
711 (tramp-run-real-handler 'expand-file-name (list localname))))))
713 (defun tramp-smb-action-get-acl (proc vec)
714 "Read ACL data from connection buffer."
715 (unless (process-live-p proc)
716 ;; Accept pending output.
717 (while (tramp-accept-process-output proc 0.1))
718 (with-current-buffer (tramp-get-connection-buffer vec)
719 ;; There might be a hidden password prompt.
720 (widen)
721 (tramp-message vec 10 "\n%s" (buffer-string))
722 (goto-char (point-min))
723 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
724 (forward-line)
725 (delete-region (point-min) (point)))
726 (while (and (not (eobp)) (looking-at "^.+:.+"))
727 (forward-line))
728 (delete-region (point) (point-max))
729 (throw 'tramp-action 'ok))))
731 (defun tramp-smb-handle-file-acl (filename)
732 "Like `file-acl' for Tramp files."
733 (with-parsed-tramp-file-name filename nil
734 (with-tramp-file-property v localname "file-acl"
735 (when (executable-find tramp-smb-acl-program)
736 ;; Set variables for computing the prompt for reading password.
737 (setq tramp-current-method method
738 tramp-current-user user
739 tramp-current-domain domain
740 tramp-current-host host
741 tramp-current-port port)
743 (let* ((share (tramp-smb-get-share v))
744 (localname (replace-regexp-in-string
745 "\\\\" "/" (tramp-smb-get-localname v)))
746 (args (list (concat "//" host "/" share) "-E")))
748 (if (not (zerop (length user)))
749 (setq args (append args (list "-U" user)))
750 (setq args (append args (list "-N"))))
752 (when domain (setq args (append args (list "-W" domain))))
753 (when port (setq args (append args (list "-p" port))))
754 (when tramp-smb-conf
755 (setq args (append args (list "-s" tramp-smb-conf))))
756 (setq
757 args
758 (append args (list (tramp-unquote-shell-quote-argument localname)
759 "2>/dev/null")))
761 (unwind-protect
762 (with-temp-buffer
763 ;; Set the transfer process properties.
764 (tramp-set-connection-property
765 v "process-name" (buffer-name (current-buffer)))
766 (tramp-set-connection-property
767 v "process-buffer" (current-buffer))
769 ;; Use an asynchronous processes. By this, password
770 ;; can be handled.
771 (let ((p (apply
772 'start-process
773 (tramp-get-connection-name v)
774 (tramp-get-connection-buffer v)
775 tramp-smb-acl-program args)))
777 (tramp-message
778 v 6 "%s" (mapconcat 'identity (process-command p) " "))
779 (tramp-set-connection-property p "vector" v)
780 (process-put p 'adjust-window-size-function 'ignore)
781 (set-process-query-on-exit-flag p nil)
782 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
783 (when (> (point-max) (point-min))
784 (substring-no-properties (buffer-string)))))
786 ;; Reset the transfer process properties.
787 (tramp-set-connection-property v "process-name" nil)
788 (tramp-set-connection-property v "process-buffer" nil)))))))
790 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
791 "Like `file-attributes' for Tramp files."
792 (unless id-format (setq id-format 'integer))
793 (ignore-errors
794 (with-parsed-tramp-file-name filename nil
795 (with-tramp-file-property
796 v localname (format "file-attributes-%s" id-format)
797 (if (tramp-smb-get-stat-capability v)
798 (tramp-smb-do-file-attributes-with-stat v id-format)
799 ;; Reading just the filename entry via "dir localname" is not
800 ;; possible, because when filename is a directory, some
801 ;; smbclient versions return the content of the directory, and
802 ;; other versions don't. Therefore, the whole content of the
803 ;; upper directory is retrieved, and the entry of the filename
804 ;; is extracted from.
805 (let* ((entries (tramp-smb-get-file-entries
806 (file-name-directory filename)))
807 (entry (assoc (file-name-nondirectory filename) entries))
808 (uid (if (equal id-format 'string) "nobody" -1))
809 (gid (if (equal id-format 'string) "nogroup" -1))
810 (inode (tramp-get-inode v))
811 (device (tramp-get-device v)))
813 ;; Check result.
814 (when entry
815 (list (and (string-match "d" (nth 1 entry))
816 t) ;0 file type
817 -1 ;1 link count
818 uid ;2 uid
819 gid ;3 gid
820 '(0 0) ;4 atime
821 (nth 3 entry) ;5 mtime
822 '(0 0) ;6 ctime
823 (nth 2 entry) ;7 size
824 (nth 1 entry) ;8 mode
825 nil ;9 gid weird
826 inode ;10 inode number
827 device)))))))) ;11 file system number
829 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
830 "Implement `file-attributes' for Tramp files using stat command."
831 (tramp-message
832 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
833 (with-current-buffer (tramp-get-connection-buffer vec)
834 (let* (size id link uid gid atime mtime ctime mode inode)
835 (when (tramp-smb-send-command
836 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
838 ;; Loop the listing.
839 (goto-char (point-min))
840 (unless (re-search-forward tramp-smb-errors nil t)
841 (while (not (eobp))
842 (cond
843 ((looking-at
844 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
845 (setq size (string-to-number (match-string 1))
846 id (if (string-equal "directory" (match-string 2)) t
847 (if (string-equal "symbolic" (match-string 2)) ""))))
848 ((looking-at
849 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
850 (setq inode (string-to-number (match-string 1))
851 link (string-to-number (match-string 2))))
852 ((looking-at
853 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
854 (setq mode (match-string 1)
855 uid (if (equal id-format 'string) (match-string 2)
856 (string-to-number (match-string 2)))
857 gid (if (equal id-format 'string) (match-string 3)
858 (string-to-number (match-string 3)))))
859 ((looking-at
860 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
861 (setq atime
862 (encode-time
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
869 ((looking-at
870 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
871 (setq mtime
872 (encode-time
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
879 ((looking-at
880 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
881 (setq ctime
882 (encode-time
883 (string-to-number (match-string 6)) ;; sec
884 (string-to-number (match-string 5)) ;; min
885 (string-to-number (match-string 4)) ;; hour
886 (string-to-number (match-string 3)) ;; day
887 (string-to-number (match-string 2)) ;; month
888 (string-to-number (match-string 1)))))) ;; year
889 (forward-line))
890 ;; Return the result.
891 (list id link uid gid atime mtime ctime size mode nil inode
892 (tramp-get-device vec)))))))
894 (defun tramp-smb-handle-file-directory-p (filename)
895 "Like `file-directory-p' for Tramp files."
896 (and (file-exists-p filename)
897 (eq ?d
898 (aref (tramp-compat-file-attribute-modes (file-attributes filename))
899 0))))
901 (defun tramp-smb-handle-file-local-copy (filename)
902 "Like `file-local-copy' for Tramp files."
903 (with-parsed-tramp-file-name filename nil
904 (unless (file-exists-p filename)
905 (tramp-error
906 v tramp-file-missing
907 "Cannot make local copy of non-existing file `%s'" filename))
908 (let ((tmpfile (tramp-compat-make-temp-file filename)))
909 (with-tramp-progress-reporter
910 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
911 (unless (tramp-smb-send-command
912 v (format "get \"%s\" \"%s\""
913 (tramp-smb-get-localname v) tmpfile))
914 ;; Oops, an error. We shall cleanup.
915 (delete-file tmpfile)
916 (tramp-error
917 v 'file-error "Cannot make local copy of file `%s'" filename)))
918 tmpfile)))
920 ;; This function should return "foo/" for directories and "bar" for
921 ;; files.
922 (defun tramp-smb-handle-file-name-all-completions (filename directory)
923 "Like `file-name-all-completions' for Tramp files."
924 (all-completions
925 filename
926 (with-parsed-tramp-file-name (expand-file-name directory) nil
927 (with-tramp-file-property v localname "file-name-all-completions"
928 (save-match-data
929 (delete-dups
930 (mapcar
931 (lambda (x)
932 (list
933 (if (string-match "d" (nth 1 x))
934 (file-name-as-directory (nth 0 x))
935 (nth 0 x))))
936 (tramp-smb-get-file-entries directory))))))))
938 (defun tramp-smb-handle-file-writable-p (filename)
939 "Like `file-writable-p' for Tramp files."
940 (if (file-exists-p filename)
941 (string-match
943 (or (tramp-compat-file-attribute-modes (file-attributes filename)) ""))
944 (let ((dir (file-name-directory filename)))
945 (and (file-exists-p dir)
946 (file-writable-p dir)))))
948 (defun tramp-smb-handle-insert-directory
949 (filename switches &optional wildcard full-directory-p)
950 "Like `insert-directory' for Tramp files."
951 (setq filename (expand-file-name filename))
952 (unless switches (setq switches ""))
953 ;; Mark trailing "/".
954 (when (and (zerop (length (file-name-nondirectory filename)))
955 (not full-directory-p))
956 (setq switches (concat switches "F")))
957 (if full-directory-p
958 ;; Called from `dired-add-entry'.
959 (setq filename (file-name-as-directory filename))
960 (setq filename (directory-file-name filename)))
961 (with-parsed-tramp-file-name filename nil
962 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
963 (save-match-data
964 (let ((base (file-name-nondirectory filename))
965 ;; We should not destroy the cache entry.
966 (entries (copy-sequence
967 (tramp-smb-get-file-entries
968 (file-name-directory filename)))))
970 (when wildcard
971 (string-match "\\." base)
972 (setq base (replace-match "\\\\." nil nil base))
973 (string-match "\\*" base)
974 (setq base (replace-match ".*" nil nil base))
975 (string-match "\\?" base)
976 (setq base (replace-match ".?" nil nil base)))
978 ;; Filter entries.
979 (setq entries
980 (delq
982 (if (or wildcard (zerop (length base)))
983 ;; Check for matching entries.
984 (mapcar
985 (lambda (x)
986 (when (string-match
987 (format "^%s" base) (nth 0 x))
989 entries)
990 ;; We just need the only and only entry FILENAME.
991 (list (assoc base entries)))))
993 ;; Sort entries.
994 (setq entries
995 (sort
996 entries
997 (lambda (x y)
998 (if (string-match "t" switches)
999 ;; Sort by date.
1000 (time-less-p (nth 3 y) (nth 3 x))
1001 ;; Sort by name.
1002 (string-lessp (nth 0 x) (nth 0 y))))))
1004 ;; Handle "-F" switch.
1005 (when (string-match "F" switches)
1006 (mapc
1007 (lambda (x)
1008 (when (not (zerop (length (car x))))
1009 (cond
1010 ((char-equal ?d (string-to-char (nth 1 x)))
1011 (setcar x (concat (car x) "/")))
1012 ((char-equal ?x (string-to-char (nth 1 x)))
1013 (setcar x (concat (car x) "*"))))))
1014 entries))
1016 ;; Print entries.
1017 (mapc
1018 (lambda (x)
1019 (when (not (zerop (length (nth 0 x))))
1020 (when (string-match "l" switches)
1021 (let ((attr
1022 (when (tramp-smb-get-stat-capability v)
1023 (ignore-errors
1024 (file-attributes filename 'string)))))
1025 (insert
1026 (format
1027 "%10s %3d %-8s %-8s %8s %s "
1028 (or (tramp-compat-file-attribute-modes attr) (nth 1 x))
1029 (or (tramp-compat-file-attribute-link-number attr) 1)
1030 (or (tramp-compat-file-attribute-user-id attr) "nobody")
1031 (or (tramp-compat-file-attribute-group-id attr) "nogroup")
1032 (or (tramp-compat-file-attribute-size attr) (nth 2 x))
1033 (format-time-string
1034 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1035 tramp-half-a-year)
1036 "%b %e %R"
1037 "%b %e %Y")
1038 (nth 3 x)))))) ; date
1040 ;; We mark the file name. The inserted name could be
1041 ;; from somewhere else, so we use the relative file name
1042 ;; of `default-directory'.
1043 (let ((start (point)))
1044 (insert
1045 (format
1046 "%s\n"
1047 (file-relative-name
1048 (expand-file-name
1049 (nth 0 x) (file-name-directory filename))
1050 (when full-directory-p (file-name-directory filename)))))
1051 (put-text-property start (1- (point)) 'dired-filename t))
1052 (forward-line)
1053 (beginning-of-line)))
1054 entries))))))
1056 (defun tramp-smb-handle-make-directory (dir &optional parents)
1057 "Like `make-directory' for Tramp files."
1058 (setq dir (directory-file-name (expand-file-name dir)))
1059 (unless (file-name-absolute-p dir)
1060 (setq dir (expand-file-name dir default-directory)))
1061 (with-parsed-tramp-file-name dir nil
1062 (save-match-data
1063 (let* ((ldir (file-name-directory dir)))
1064 ;; Make missing directory parts.
1065 (when (and parents
1066 (tramp-smb-get-share v)
1067 (not (file-directory-p ldir)))
1068 (make-directory ldir parents))
1069 ;; Just do it.
1070 (when (file-directory-p ldir)
1071 (make-directory-internal dir))
1072 (unless (file-directory-p dir)
1073 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1075 (defun tramp-smb-handle-make-directory-internal (directory)
1076 "Like `make-directory-internal' for Tramp files."
1077 (setq directory (directory-file-name (expand-file-name directory)))
1078 (unless (file-name-absolute-p directory)
1079 (setq directory (expand-file-name directory default-directory)))
1080 (with-parsed-tramp-file-name directory nil
1081 (save-match-data
1082 (let* ((file (tramp-smb-get-localname v)))
1083 (when (file-directory-p (file-name-directory directory))
1084 (tramp-smb-send-command
1086 (if (tramp-smb-get-cifs-capabilities v)
1087 (format "posix_mkdir \"%s\" %o" file (default-file-modes))
1088 (format "mkdir \"%s\"" file)))
1089 ;; We must also flush the cache of the directory, because
1090 ;; `file-attributes' reads the values from there.
1091 (tramp-flush-file-property v (file-name-directory localname))
1092 (tramp-flush-file-property v localname))
1093 (unless (file-directory-p directory)
1094 (tramp-error
1095 v 'file-error "Couldn't make directory %s" directory))))))
1097 (defun tramp-smb-handle-make-symbolic-link
1098 (filename linkname &optional ok-if-already-exists)
1099 "Like `make-symbolic-link' for Tramp files.
1100 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1101 the symlink. If LINKNAME is a Tramp file, only the localname component is
1102 used as the target of the symlink.
1104 If LINKNAME is a Tramp file and the localname component is relative, then
1105 it is expanded first, before the localname component is taken. Note that
1106 this can give surprising results if the user/host for the source and
1107 target of the symlink differ."
1108 (unless (tramp-equal-remote filename linkname)
1109 (with-parsed-tramp-file-name
1110 (if (tramp-tramp-file-p filename) filename linkname) nil
1111 (tramp-error
1112 v 'file-error
1113 "make-symbolic-link: %s"
1114 "only implemented for same method, same user, same host")))
1115 (with-parsed-tramp-file-name filename v1
1116 (with-parsed-tramp-file-name linkname v2
1117 (when (file-directory-p filename)
1118 (tramp-error
1119 v2 'file-error
1120 "make-symbolic-link: %s must not be a directory" filename))
1121 (when (and (not ok-if-already-exists)
1122 (file-exists-p linkname)
1123 (not (numberp ok-if-already-exists))
1124 (y-or-n-p
1125 (format
1126 "File %s already exists; make it a new name anyway? "
1127 linkname)))
1128 (tramp-error v2 'file-already-exists linkname))
1129 (unless (tramp-smb-get-cifs-capabilities v1)
1130 (tramp-error v2 'file-error "make-symbolic-link not supported"))
1131 ;; We must also flush the cache of the directory, because
1132 ;; `file-attributes' reads the values from there.
1133 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1134 (tramp-flush-file-property v2 v2-localname)
1135 (unless
1136 (tramp-smb-send-command
1138 (format
1139 "symlink \"%s\" \"%s\""
1140 (tramp-smb-get-localname v1)
1141 (tramp-smb-get-localname v2)))
1142 (tramp-error
1143 v2 'file-error
1144 "error with make-symbolic-link, see buffer `%s' for details"
1145 (buffer-name))))))
1147 (defun tramp-smb-handle-process-file
1148 (program &optional infile destination display &rest args)
1149 "Like `process-file' for Tramp files."
1150 ;; The implementation is not complete yet.
1151 (when (and (numberp destination) (zerop destination))
1152 (error "Implementation does not handle immediate return"))
1154 (with-parsed-tramp-file-name default-directory nil
1155 (let* ((name (file-name-nondirectory program))
1156 (name1 name)
1157 (i 0)
1158 input tmpinput outbuf command ret)
1160 ;; Determine input.
1161 (when infile
1162 (setq infile (expand-file-name infile))
1163 (if (tramp-equal-remote default-directory infile)
1164 ;; INFILE is on the same remote host.
1165 (setq input (with-parsed-tramp-file-name infile nil localname))
1166 ;; INFILE must be copied to remote host.
1167 (setq input (tramp-make-tramp-temp-file v)
1168 tmpinput
1169 (tramp-make-tramp-file-name method user domain host port input))
1170 (copy-file infile tmpinput t))
1171 ;; Transform input into a filename powershell does understand.
1172 (setq input (format "//%s%s" host input)))
1174 ;; Determine output.
1175 (cond
1176 ;; Just a buffer.
1177 ((bufferp destination)
1178 (setq outbuf destination))
1179 ;; A buffer name.
1180 ((stringp destination)
1181 (setq outbuf (get-buffer-create destination)))
1182 ;; (REAL-DESTINATION ERROR-DESTINATION)
1183 ((consp destination)
1184 ;; output.
1185 (cond
1186 ((bufferp (car destination))
1187 (setq outbuf (car destination)))
1188 ((stringp (car destination))
1189 (setq outbuf (get-buffer-create (car destination))))
1190 ((car destination)
1191 (setq outbuf (current-buffer))))
1192 ;; stderr.
1193 (tramp-message v 2 "%s" "STDERR not supported"))
1194 ;; 't
1195 (destination
1196 (setq outbuf (current-buffer))))
1198 ;; Construct command.
1199 (setq command (mapconcat 'identity (cons program args) " ")
1200 command (if input
1201 (format
1202 "get-content %s | & %s"
1203 (tramp-smb-shell-quote-argument input) command)
1204 (format "& %s" command)))
1206 (while (get-process name1)
1207 ;; NAME must be unique as process name.
1208 (setq i (1+ i)
1209 name1 (format "%s<%d>" name i)))
1211 ;; Set the new process properties.
1212 (tramp-set-connection-property v "process-name" name1)
1213 (tramp-set-connection-property
1214 v "process-buffer"
1215 (or outbuf (generate-new-buffer tramp-temp-buffer-name)))
1217 ;; Call it.
1218 (condition-case nil
1219 (with-current-buffer (tramp-get-connection-buffer v)
1220 ;; Preserve buffer contents.
1221 (narrow-to-region (point-max) (point-max))
1222 (tramp-smb-call-winexe v)
1223 (when (tramp-smb-get-share v)
1224 (tramp-smb-send-command
1225 v (format "cd \"//%s%s\"" host (file-name-directory localname))))
1226 (tramp-smb-send-command v command)
1227 ;; Preserve command output.
1228 (narrow-to-region (point-max) (point-max))
1229 (let ((p (tramp-get-connection-process v)))
1230 (tramp-smb-send-command v "exit $lasterrorcode")
1231 (while (process-live-p p)
1232 (sleep-for 0.1)
1233 (setq ret (process-exit-status p))))
1234 (delete-region (point-min) (point-max))
1235 (widen))
1237 ;; When the user did interrupt, we should do it also. We use
1238 ;; return code -1 as marker.
1239 (quit
1240 (setq ret -1))
1241 ;; Handle errors.
1242 (error
1243 (setq ret 1)))
1245 ;; We should redisplay the output.
1246 (when (and display outbuf (get-buffer-window outbuf t)) (redisplay))
1248 ;; Cleanup. We remove all file cache values for the connection,
1249 ;; because the remote process could have changed them.
1250 (tramp-set-connection-property v "process-name" nil)
1251 (tramp-set-connection-property v "process-buffer" nil)
1252 (when tmpinput (delete-file tmpinput))
1253 (unless outbuf
1254 (kill-buffer (tramp-get-connection-property v "process-buffer" nil)))
1256 (unless process-file-side-effects
1257 (tramp-flush-directory-property v ""))
1259 ;; Return exit status.
1260 (if (equal ret -1)
1261 (keyboard-quit)
1262 ret))))
1264 (defun tramp-smb-handle-rename-file
1265 (filename newname &optional ok-if-already-exists)
1266 "Like `rename-file' for Tramp files."
1267 (setq filename (expand-file-name filename)
1268 newname (expand-file-name newname))
1270 (when (and (not ok-if-already-exists)
1271 (file-exists-p newname))
1272 (tramp-error
1273 (tramp-dissect-file-name
1274 (if (tramp-tramp-file-p filename) filename newname))
1275 'file-already-exists newname))
1277 (with-tramp-progress-reporter
1278 (tramp-dissect-file-name
1279 (if (tramp-tramp-file-p filename) filename newname))
1280 0 (format "Renaming %s to %s" filename newname)
1282 (if (and (not (file-exists-p newname))
1283 (tramp-equal-remote filename newname)
1284 (string-equal
1285 (tramp-smb-get-share (tramp-dissect-file-name filename))
1286 (tramp-smb-get-share (tramp-dissect-file-name newname))))
1287 ;; We can rename directly.
1288 (with-parsed-tramp-file-name filename v1
1289 (with-parsed-tramp-file-name newname v2
1291 ;; We must also flush the cache of the directory, because
1292 ;; `file-attributes' reads the values from there.
1293 (tramp-flush-file-property v1 (file-name-directory v1-localname))
1294 (tramp-flush-file-property v1 v1-localname)
1295 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1296 (tramp-flush-file-property v2 v2-localname)
1297 (unless (tramp-smb-get-share v2)
1298 (tramp-error
1299 v2 'file-error "Target `%s' must contain a share name" newname))
1300 (unless (tramp-smb-send-command
1301 v2 (format "rename \"%s\" \"%s\""
1302 (tramp-smb-get-localname v1)
1303 (tramp-smb-get-localname v2)))
1304 (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
1306 ;; We must rename via copy.
1307 (copy-file
1308 filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
1309 (if (file-directory-p filename)
1310 (delete-directory filename 'recursive)
1311 (delete-file filename)))))
1313 (defun tramp-smb-action-set-acl (proc vec)
1314 "Read ACL data from connection buffer."
1315 (unless (process-live-p proc)
1316 ;; Accept pending output.
1317 (while (tramp-accept-process-output proc 0.1))
1318 (with-current-buffer (tramp-get-connection-buffer vec)
1319 (tramp-message vec 10 "\n%s" (buffer-string))
1320 (throw 'tramp-action 'ok))))
1322 (defun tramp-smb-handle-set-file-acl (filename acl-string)
1323 "Like `set-file-acl' for Tramp files."
1324 (ignore-errors
1325 (with-parsed-tramp-file-name filename nil
1326 (when (and (stringp acl-string) (executable-find tramp-smb-acl-program))
1327 ;; Set variables for computing the prompt for reading password.
1328 (setq tramp-current-method method
1329 tramp-current-user user
1330 tramp-current-domain domain
1331 tramp-current-host host
1332 tramp-current-port port)
1333 (tramp-set-file-property v localname "file-acl" 'undef)
1335 (let* ((share (tramp-smb-get-share v))
1336 (localname (replace-regexp-in-string
1337 "\\\\" "/" (tramp-smb-get-localname v)))
1338 (args (list (concat "//" host "/" share) "-E" "-S"
1339 (replace-regexp-in-string
1340 "\n" "," acl-string))))
1342 (if (not (zerop (length user)))
1343 (setq args (append args (list "-U" user)))
1344 (setq args (append args (list "-N"))))
1346 (when domain (setq args (append args (list "-W" domain))))
1347 (when port (setq args (append args (list "-p" port))))
1348 (when tramp-smb-conf
1349 (setq args (append args (list "-s" tramp-smb-conf))))
1350 (setq
1351 args
1352 (append args (list (tramp-unquote-shell-quote-argument localname)
1353 "&&" "echo" "tramp_exit_status" "0"
1354 "||" "echo" "tramp_exit_status" "1")))
1356 (unwind-protect
1357 (with-temp-buffer
1358 ;; Set the transfer process properties.
1359 (tramp-set-connection-property
1360 v "process-name" (buffer-name (current-buffer)))
1361 (tramp-set-connection-property
1362 v "process-buffer" (current-buffer))
1364 ;; Use an asynchronous processes. By this, password can
1365 ;; be handled.
1366 (let ((p (apply
1367 'start-process
1368 (tramp-get-connection-name v)
1369 (tramp-get-connection-buffer v)
1370 tramp-smb-acl-program args)))
1372 (tramp-message
1373 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1374 (tramp-set-connection-property p "vector" v)
1375 (process-put p 'adjust-window-size-function 'ignore)
1376 (set-process-query-on-exit-flag p nil)
1377 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1378 (goto-char (point-max))
1379 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1380 (tramp-error
1381 v 'file-error
1382 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1383 (skip-chars-forward "^ ")
1384 (when (zerop (read (current-buffer)))
1385 ;; Success.
1386 (tramp-set-file-property v localname "file-acl" acl-string)
1387 t)))
1389 ;; Reset the transfer process properties.
1390 (tramp-set-connection-property v "process-name" nil)
1391 (tramp-set-connection-property v "process-buffer" nil)))))))
1393 (defun tramp-smb-handle-set-file-modes (filename mode)
1394 "Like `set-file-modes' for Tramp files."
1395 (with-parsed-tramp-file-name filename nil
1396 (when (tramp-smb-get-cifs-capabilities v)
1397 (tramp-flush-file-property v localname)
1398 (unless (tramp-smb-send-command
1399 v (format "chmod \"%s\" %o" (tramp-smb-get-localname v) mode))
1400 (tramp-error
1401 v 'file-error "Error while changing file's mode %s" filename)))))
1403 ;; We use BUFFER also as connection buffer during setup. Because of
1404 ;; this, its original contents must be saved, and restored once
1405 ;; connection has been setup.
1406 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1407 "Like `start-file-process' for Tramp files."
1408 (with-parsed-tramp-file-name default-directory nil
1409 (let* ((buffer
1410 (if buffer
1411 (get-buffer-create buffer)
1412 ;; BUFFER can be nil. We use a temporary buffer.
1413 (generate-new-buffer tramp-temp-buffer-name)))
1414 (command (mapconcat 'identity (cons program args) " "))
1415 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1416 (name1 name)
1417 (i 0))
1418 (unwind-protect
1419 (save-excursion
1420 (save-restriction
1421 (while (get-process name1)
1422 ;; NAME must be unique as process name.
1423 (setq i (1+ i)
1424 name1 (format "%s<%d>" name i)))
1425 ;; Set the new process properties.
1426 (tramp-set-connection-property v "process-name" name1)
1427 (tramp-set-connection-property v "process-buffer" buffer)
1428 ;; Activate narrowing in order to save BUFFER contents.
1429 (with-current-buffer (tramp-get-connection-buffer v)
1430 (let ((buffer-undo-list t))
1431 (narrow-to-region (point-max) (point-max))
1432 (tramp-smb-call-winexe v)
1433 (when (tramp-smb-get-share v)
1434 (tramp-smb-send-command
1435 v (format
1436 "cd \"//%s%s\""
1437 host (file-name-directory localname))))
1438 (tramp-message v 6 "(%s); exit" command)
1439 (tramp-send-string v command)))
1440 ;; Return value.
1441 (tramp-get-connection-process v)))
1443 ;; Save exit.
1444 (with-current-buffer (tramp-get-connection-buffer v)
1445 (if (string-match tramp-temp-buffer-name (buffer-name))
1446 (progn
1447 (set-process-buffer (tramp-get-connection-process v) nil)
1448 (kill-buffer (current-buffer)))
1449 (set-buffer-modified-p bmp)))
1450 (tramp-set-connection-property v "process-name" nil)
1451 (tramp-set-connection-property v "process-buffer" nil)))))
1453 (defun tramp-smb-handle-substitute-in-file-name (filename)
1454 "Like `handle-substitute-in-file-name' for Tramp files.
1455 \"//\" substitutes only in the local filename part. Catches
1456 errors for shares like \"C$/\", which are common in Microsoft Windows."
1457 ;; Check, whether the local part is a quoted file name.
1458 (if (tramp-compat-file-name-quoted-p filename)
1459 filename
1460 (with-parsed-tramp-file-name filename nil
1461 ;; Ignore in LOCALNAME everything before "//".
1462 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1463 (setq filename
1464 (concat (file-remote-p filename)
1465 (replace-match "\\1" nil nil localname)))))
1466 (condition-case nil
1467 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1468 (error filename))))
1470 (defun tramp-smb-handle-write-region
1471 (start end filename &optional append visit lockname mustbenew)
1472 "Like `write-region' for Tramp files."
1473 (setq filename (expand-file-name filename))
1474 (with-parsed-tramp-file-name filename nil
1475 (when (and mustbenew (file-exists-p filename)
1476 (or (eq mustbenew 'excl)
1477 (not
1478 (y-or-n-p
1479 (format "File %s exists; overwrite anyway? " filename)))))
1480 (tramp-error v 'file-already-exists filename))
1482 ;; We must also flush the cache of the directory, because
1483 ;; `file-attributes' reads the values from there.
1484 (tramp-flush-file-property v (file-name-directory localname))
1485 (tramp-flush-file-property v localname)
1486 (let ((curbuf (current-buffer))
1487 (tmpfile (tramp-compat-make-temp-file filename)))
1488 (when (and append (file-exists-p filename))
1489 (copy-file filename tmpfile 'ok))
1490 ;; We say `no-message' here because we don't want the visited file
1491 ;; modtime data to be clobbered from the temp file. We call
1492 ;; `set-visited-file-modtime' ourselves later on.
1493 (tramp-run-real-handler
1494 'write-region (list start end tmpfile append 'no-message lockname))
1496 (with-tramp-progress-reporter
1497 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1498 (unwind-protect
1499 (unless (tramp-smb-send-command
1500 v (format "put %s \"%s\""
1501 tmpfile (tramp-smb-get-localname v)))
1502 (tramp-error v 'file-error "Cannot write `%s'" filename))
1503 (delete-file tmpfile)))
1505 (unless (equal curbuf (current-buffer))
1506 (tramp-error
1507 v 'file-error
1508 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1509 (when (eq visit t)
1510 (set-visited-file-modtime)))))
1513 ;; Internal file name functions.
1515 (defun tramp-smb-get-share (vec)
1516 "Returns the share name of LOCALNAME."
1517 (save-match-data
1518 (let ((localname (tramp-file-name-unquote-localname vec)))
1519 (when (string-match "^/?\\([^/]+\\)/" localname)
1520 (match-string 1 localname)))))
1522 (defun tramp-smb-get-localname (vec)
1523 "Returns the file name of LOCALNAME.
1524 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1525 (save-match-data
1526 (let ((localname (tramp-file-name-unquote-localname vec)))
1527 (setq
1528 localname
1529 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1530 ;; There is a share, separated by "/".
1531 (if (not (tramp-smb-get-cifs-capabilities vec))
1532 (mapconcat
1533 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1534 (match-string 1 localname) "")
1535 (match-string 1 localname))
1536 ;; There is just a share.
1537 (if (string-match "^/?\\([^/]+\\)$" localname)
1538 (match-string 1 localname)
1539 "")))
1541 ;; Sometimes we have discarded `substitute-in-file-name'.
1542 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1543 (setq localname (replace-match "$" nil nil localname 1)))
1545 localname)))
1547 ;; Share names of a host are cached. It is very unlikely that the
1548 ;; shares do change during connection.
1549 (defun tramp-smb-get-file-entries (directory)
1550 "Read entries which match DIRECTORY.
1551 Either the shares are listed, or the `dir' command is executed.
1552 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1553 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1554 (setq localname (or localname "/"))
1555 (with-tramp-file-property v localname "file-entries"
1556 (with-current-buffer (tramp-get-connection-buffer v)
1557 (let* ((share (tramp-smb-get-share v))
1558 (cache (tramp-get-connection-property v "share-cache" nil))
1559 res entry)
1561 (if (and (not share) cache)
1562 ;; Return cached shares.
1563 (setq res cache)
1565 ;; Read entries.
1566 (if share
1567 (tramp-smb-send-command
1568 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1569 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1570 (tramp-smb-maybe-open-connection v))
1572 ;; Loop the listing.
1573 (goto-char (point-min))
1574 (if (re-search-forward tramp-smb-errors nil t)
1575 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1576 (while (not (eobp))
1577 (setq entry (tramp-smb-read-file-entry share))
1578 (forward-line)
1579 (when entry (push entry res))))
1581 ;; Cache share entries.
1582 (unless share
1583 (tramp-set-connection-property v "share-cache" res)))
1585 ;; Add directory itself.
1586 (push '("" "drwxrwxrwx" 0 (0 0)) res)
1588 ;; Return entries.
1589 (delq nil res))))))
1591 ;; Return either a share name (if SHARE is nil), or a file name.
1593 ;; If shares are listed, the following format is expected:
1595 ;; Disk| - leading spaces
1596 ;; [^|]+| - share name, 14 char
1597 ;; .* - comment
1599 ;; Entries provided by smbclient DIR aren't fully regular.
1600 ;; They should have the format
1602 ;; \s-\{2,2} - leading spaces
1603 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1604 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1605 ;; \s- - space delimiter
1606 ;; \s-+[0-9]+ - size, 8 chars, right bound
1607 ;; \s-\{2,2\} - space delimiter
1608 ;; \w\{3,3\} - weekday
1609 ;; \s- - space delimiter
1610 ;; \w\{3,3\} - month
1611 ;; \s- - space delimiter
1612 ;; [ 12][0-9] - day
1613 ;; \s- - space delimiter
1614 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1615 ;; \s- - space delimiter
1616 ;; [0-9]\{4,4\} - year
1618 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1619 ;; has function display_finfo:
1621 ;; d_printf(" %-30s%7.7s %8.0f %s",
1622 ;; finfo->name,
1623 ;; attrib_string(finfo->mode),
1624 ;; (double)finfo->size,
1625 ;; asctime(LocalTime(&t)));
1627 ;; in Samba 1.9, there's the following code:
1629 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1630 ;; CNV_LANG(finfo->name),
1631 ;; attrib_string(finfo->mode),
1632 ;; finfo->size,
1633 ;; asctime(LocalTime(&t))));
1635 ;; Problems:
1636 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1637 ;; available in older Emacsen.
1638 ;; * The length of constructs (file name, size) might exceed the default.
1639 ;; * File names might contain spaces.
1640 ;; * Permissions might be empty.
1642 ;; So we try to analyze backwards.
1643 (defun tramp-smb-read-file-entry (share)
1644 "Parse entry in SMB output buffer.
1645 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1646 Result is the list (LOCALNAME MODE SIZE MTIME)."
1647 ;; We are called from `tramp-smb-get-file-entries', which sets the
1648 ;; current buffer.
1649 (let ((line (buffer-substring (point) (point-at-eol)))
1650 localname mode size month day hour min sec year mtime)
1652 (if (not share)
1654 ;; Read share entries.
1655 (when (string-match "^Disk|\\([^|]+\\)|" line)
1656 (setq localname (match-string 1 line)
1657 mode "dr-xr-xr-x"
1658 size 0))
1660 ;; Real listing.
1661 (cl-block nil
1663 ;; year.
1664 (if (string-match "\\([0-9]+\\)$" line)
1665 (setq year (string-to-number (match-string 1 line))
1666 line (substring line 0 -5))
1667 (cl-return))
1669 ;; time.
1670 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1671 (setq hour (string-to-number (match-string 1 line))
1672 min (string-to-number (match-string 2 line))
1673 sec (string-to-number (match-string 3 line))
1674 line (substring line 0 -9))
1675 (cl-return))
1677 ;; day.
1678 (if (string-match "\\([0-9]+\\)$" line)
1679 (setq day (string-to-number (match-string 1 line))
1680 line (substring line 0 -3))
1681 (cl-return))
1683 ;; month.
1684 (if (string-match "\\(\\w+\\)$" line)
1685 (setq month (match-string 1 line)
1686 line (substring line 0 -4))
1687 (cl-return))
1689 ;; weekday.
1690 (if (string-match "\\(\\w+\\)$" line)
1691 (setq line (substring line 0 -5))
1692 (cl-return))
1694 ;; size.
1695 (if (string-match "\\([0-9]+\\)$" line)
1696 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1697 (setq size (string-to-number (match-string 1 line)))
1698 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1699 (setq length (+ length (match-end 0))))
1700 (setq line (substring line 0 length)))
1701 (cl-return))
1703 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1704 (if (string-match "\\([ADHRSV]+\\)?$" line)
1705 (setq
1706 mode (or (match-string 1 line) "")
1707 mode (save-match-data (format
1708 "%s%s"
1709 (if (string-match "D" mode) "d" "-")
1710 (mapconcat
1711 (lambda (_x) "") " "
1712 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1713 line (substring line 0 -6))
1714 (cl-return))
1716 ;; localname.
1717 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1718 (setq localname (match-string 1 line))
1719 (cl-return))))
1721 (when (and localname mode size)
1722 (setq mtime
1723 (if (and sec min hour day month year)
1724 (encode-time
1725 sec min hour day
1726 (cdr (assoc (downcase month) parse-time-months))
1727 year)
1728 '(0 0)))
1729 (list localname mode size mtime))))
1731 (defun tramp-smb-get-cifs-capabilities (vec)
1732 "Check, whether the SMB server supports POSIX commands."
1733 ;; When we are not logged in yet, we return nil.
1734 (if (process-live-p (tramp-get-connection-process vec))
1735 (with-tramp-connection-property
1736 (tramp-get-connection-process vec) "cifs-capabilities"
1737 (save-match-data
1738 (when (tramp-smb-send-command vec "posix")
1739 (with-current-buffer (tramp-get-connection-buffer vec)
1740 (goto-char (point-min))
1741 (when
1742 (re-search-forward "Server supports CIFS capabilities" nil t)
1743 (member
1744 "pathnames"
1745 (split-string
1746 (buffer-substring (point) (point-at-eol)) nil 'omit)))))))))
1748 (defun tramp-smb-get-stat-capability (vec)
1749 "Check, whether the SMB server supports the STAT command."
1750 ;; When we are not logged in yet, we return nil.
1751 (if (and (tramp-smb-get-share vec)
1752 (process-live-p (tramp-get-connection-process vec)))
1753 (with-tramp-connection-property
1754 (tramp-get-connection-process vec) "stat-capability"
1755 (tramp-smb-send-command vec "stat \"/\""))))
1758 ;; Connection functions.
1760 (defun tramp-smb-send-command (vec command)
1761 "Send the COMMAND to connection VEC.
1762 Returns nil if there has been an error message from smbclient."
1763 (tramp-smb-maybe-open-connection vec)
1764 (tramp-message vec 6 "%s" command)
1765 (tramp-send-string vec command)
1766 (tramp-smb-wait-for-output vec))
1768 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1769 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1770 Does not do anything if a connection is already open, but re-opens the
1771 connection if a previous connection has died for some reason.
1772 If ARGUMENT is non-nil, use it as argument for
1773 `tramp-smb-winexe-program', and suppress any checks."
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)
1787 (tramp-error
1788 vec 'file-error
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
1798 tramp-smb-version
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
1813 (current-time)
1814 (tramp-get-connection-property
1815 p "last-cmd-time" '(0 0 0)))
1817 (process-live-p p)
1818 (re-search-forward tramp-smb-errors nil t))
1819 (delete-process p)
1820 (setq p nil)))
1822 ;; Check whether it is still the same share.
1823 (unless (and (process-live-p p)
1824 (or argument
1825 (string-equal
1826 share
1827 (tramp-get-connection-property p "smb-share" ""))))
1829 (save-match-data
1830 ;; There might be unread output from checking for share names.
1831 (when buf (with-current-buffer buf (erase-buffer)))
1832 (when (and p (processp p)) (delete-process p))
1834 (let* ((user (tramp-file-name-user vec))
1835 (host (tramp-file-name-host vec))
1836 (domain (tramp-file-name-domain vec))
1837 (port (tramp-file-name-port vec))
1838 args)
1840 (cond
1841 (argument
1842 (setq args (list (concat "//" host))))
1843 (share
1844 (setq args (list (concat "//" host "/" share))))
1846 (setq args (list "-g" "-L" host ))))
1848 (if (not (zerop (length user)))
1849 (setq args (append args (list "-U" user)))
1850 (setq args (append args (list "-N"))))
1852 (when domain (setq args (append args (list "-W" domain))))
1853 (when port (setq args (append args (list "-p" port))))
1854 (when tramp-smb-conf
1855 (setq args (append args (list "-s" tramp-smb-conf))))
1856 (when argument
1857 (setq args (append args (list argument))))
1859 ;; OK, let's go.
1860 (with-tramp-progress-reporter
1861 vec 3
1862 (format "Opening connection for //%s%s/%s"
1863 (if (not (zerop (length user))) (concat user "@") "")
1864 host (or share ""))
1866 (let* ((coding-system-for-read nil)
1867 (process-connection-type tramp-process-connection-type)
1868 (p (let ((default-directory
1869 (tramp-compat-temporary-file-directory)))
1870 (apply #'start-process
1871 (tramp-get-connection-name vec)
1872 (tramp-get-connection-buffer vec)
1873 (if argument
1874 tramp-smb-winexe-program tramp-smb-program)
1875 args))))
1877 (tramp-message
1878 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1879 (tramp-set-connection-property p "vector" vec)
1880 (process-put p 'adjust-window-size-function 'ignore)
1881 (set-process-query-on-exit-flag p nil)
1883 ;; Set variables for computing the prompt for reading password.
1884 (setq tramp-current-method tramp-smb-method
1885 tramp-current-user user
1886 tramp-current-domain domain
1887 tramp-current-host host
1888 tramp-current-port port)
1890 (condition-case err
1891 (let (tramp-message-show-message)
1892 ;; Play login scenario.
1893 (tramp-process-actions
1894 p vec nil
1895 (if (or argument share)
1896 tramp-smb-actions-with-share
1897 tramp-smb-actions-without-share))
1899 ;; Check server version.
1900 (unless argument
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)))
1905 (unless
1906 (string-equal
1907 smbserver-version
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 ;; Set connection-local variables.
1922 (tramp-set-connection-local-variables vec)
1924 ;; Mark it as connected.
1925 (tramp-set-connection-property p "connected" t))
1927 ;; Check for the error reason. If it was due to wrong
1928 ;; password, reestablish the connection. We cannot
1929 ;; handle this in `tramp-process-actions', because
1930 ;; smbclient does not ask for the password, again.
1931 (error
1932 (with-current-buffer (tramp-get-connection-buffer vec)
1933 (goto-char (point-min))
1934 (if (and (bound-and-true-p auth-sources)
1935 (search-forward-regexp
1936 tramp-smb-wrong-passwd-regexp nil t))
1937 ;; Disable `auth-source' and `password-cache'.
1938 (let (auth-sources)
1939 (tramp-message
1940 vec 3 "Retry connection with new password")
1941 (tramp-cleanup-connection vec t)
1942 (tramp-smb-maybe-open-connection vec argument))
1943 ;; Propagate the error.
1944 (signal (car err) (cdr err)))))))))))))
1946 ;; We don't use timeouts. If needed, the caller shall wrap around.
1947 (defun tramp-smb-wait-for-output (vec)
1948 "Wait for output from smbclient command.
1949 Returns nil if an error message has appeared."
1950 (with-current-buffer (tramp-get-connection-buffer vec)
1951 (let ((p (get-buffer-process (current-buffer)))
1952 (found (progn (goto-char (point-min))
1953 (re-search-forward tramp-smb-prompt nil t)))
1954 (err (progn (goto-char (point-min))
1955 (re-search-forward tramp-smb-errors nil t)))
1956 buffer-read-only)
1958 ;; Algorithm: get waiting output. See if last line contains
1959 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1960 ;; If not, wait a bit and again get waiting output.
1961 (while (and (not found) (not err) (process-live-p p))
1963 ;; Accept pending output.
1964 (tramp-accept-process-output p 0.1)
1966 ;; Search for prompt.
1967 (goto-char (point-min))
1968 (setq found (re-search-forward tramp-smb-prompt nil t))
1970 ;; Search for errors.
1971 (goto-char (point-min))
1972 (setq err (re-search-forward tramp-smb-errors nil t)))
1974 ;; When the process is still alive, read pending output.
1975 (while (and (not found) (process-live-p p))
1977 ;; Accept pending output.
1978 (tramp-accept-process-output p 0.1)
1980 ;; Search for prompt.
1981 (goto-char (point-min))
1982 (setq found (re-search-forward tramp-smb-prompt nil t)))
1984 (tramp-message vec 6 "\n%s" (buffer-string))
1986 ;; Remove prompt.
1987 (when found
1988 (goto-char (point-max))
1989 (re-search-backward tramp-smb-prompt nil t)
1990 (delete-region (point) (point-max)))
1992 ;; Return value is whether no error message has appeared.
1993 (not err))))
1995 (defun tramp-smb-kill-winexe-function ()
1996 "Send SIGKILL to the winexe process."
1997 (ignore-errors
1998 (let ((p (get-buffer-process (current-buffer))))
1999 (when (process-live-p p)
2000 (signal-process (process-id p) 'SIGINT)))))
2002 (defun tramp-smb-call-winexe (vec)
2003 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2005 ;; Check for program.
2006 (unless (executable-find tramp-smb-winexe-program)
2007 (tramp-error
2008 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
2010 ;; winexe does not supports ports.
2011 (when (tramp-file-name-port vec)
2012 (tramp-error vec 'file-error "Port not supported for remote processes"))
2014 (tramp-smb-maybe-open-connection
2016 (format
2017 "%s %s"
2018 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2020 (set (make-local-variable 'kill-buffer-hook)
2021 '(tramp-smb-kill-winexe-function))
2023 ;; Suppress "^M". Shouldn't we specify utf8?
2024 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2026 ;; Set width to 128. This avoids mixing prompt and long error messages.
2027 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2028 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2029 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2030 (tramp-smb-send-command vec "$bufsize.Width = 128")
2031 (tramp-smb-send-command vec "$winsize.Width = 128")
2032 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2033 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2035 (defun tramp-smb-shell-quote-argument (s)
2036 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2037 (let ((system-type 'ms-dos))
2038 (tramp-unquote-shell-quote-argument s)))
2040 (add-hook 'tramp-unload-hook
2041 (lambda ()
2042 (unload-feature 'tramp-smb 'force)))
2044 (provide 'tramp-smb)
2046 ;;; TODO:
2048 ;; * Return more comprehensive file permission string.
2050 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2051 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2053 ;; * Ignore case in file names.
2055 ;;; tramp-smb.el ends here