Add current-line in simple.el
[emacs.git] / lisp / net / tramp-smb.el
blob1aadd14fb4102751d8ab33f053784498b9591c3b
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_IO_TIMEOUT"
141 "NT_STATUS_LOGON_FAILURE"
142 "NT_STATUS_NETWORK_ACCESS_DENIED"
143 "NT_STATUS_NOT_IMPLEMENTED"
144 "NT_STATUS_NO_LOGON_SERVERS"
145 "NT_STATUS_NO_SUCH_FILE"
146 "NT_STATUS_NO_SUCH_USER"
147 "NT_STATUS_OBJECT_NAME_COLLISION"
148 "NT_STATUS_OBJECT_NAME_INVALID"
149 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
150 "NT_STATUS_PASSWORD_MUST_CHANGE"
151 "NT_STATUS_SHARING_VIOLATION"
152 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
153 "NT_STATUS_UNSUCCESSFUL"
154 "NT_STATUS_WRONG_PASSWORD")))
155 "\\|")
156 "Regexp for possible error strings of SMB servers.
157 Used instead of analyzing error codes of commands.")
159 (defconst tramp-smb-actions-with-share
160 '((tramp-smb-prompt tramp-action-succeed)
161 (tramp-password-prompt-regexp tramp-action-password)
162 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
163 (tramp-smb-errors tramp-action-permission-denied)
164 (tramp-process-alive-regexp tramp-action-process-alive))
165 "List of pattern/action pairs.
166 This list is used for login to SMB servers.
168 See `tramp-actions-before-shell' for more info.")
170 (defconst tramp-smb-actions-without-share
171 '((tramp-password-prompt-regexp tramp-action-password)
172 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
173 (tramp-smb-errors tramp-action-permission-denied)
174 (tramp-process-alive-regexp tramp-action-out-of-band))
175 "List of pattern/action pairs.
176 This list is used for login to SMB servers.
178 See `tramp-actions-before-shell' for more info.")
180 (defconst tramp-smb-actions-with-tar
181 '((tramp-password-prompt-regexp tramp-action-password)
182 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
183 (tramp-smb-errors tramp-action-permission-denied)
184 (tramp-process-alive-regexp tramp-smb-action-with-tar))
185 "List of pattern/action pairs.
186 This list is used for tar-like copy of directories.
188 See `tramp-actions-before-shell' for more info.")
190 (defconst tramp-smb-actions-get-acl
191 '((tramp-password-prompt-regexp tramp-action-password)
192 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
193 (tramp-smb-errors tramp-action-permission-denied)
194 (tramp-process-alive-regexp tramp-smb-action-get-acl))
195 "List of pattern/action pairs.
196 This list is used for smbcacls actions.
198 See `tramp-actions-before-shell' for more info.")
200 (defconst tramp-smb-actions-set-acl
201 '((tramp-password-prompt-regexp tramp-action-password)
202 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
203 (tramp-smb-errors tramp-action-permission-denied)
204 (tramp-process-alive-regexp tramp-smb-action-set-acl))
205 "List of pattern/action pairs.
206 This list is used for smbcacls actions.
208 See `tramp-actions-before-shell' for more info.")
210 ;; New handlers should be added here.
211 ;;;###tramp-autoload
212 (defconst tramp-smb-file-name-handler-alist
213 '(;; `access-file' performed by default handler.
214 (add-name-to-file . tramp-smb-handle-add-name-to-file)
215 ;; `byte-compiler-base-file-name' performed by default handler.
216 (copy-directory . tramp-smb-handle-copy-directory)
217 (copy-file . tramp-smb-handle-copy-file)
218 (delete-directory . tramp-smb-handle-delete-directory)
219 (delete-file . tramp-smb-handle-delete-file)
220 ;; `diff-latest-backup-file' performed by default handler.
221 (directory-file-name . tramp-handle-directory-file-name)
222 (directory-files . tramp-smb-handle-directory-files)
223 (directory-files-and-attributes
224 . tramp-handle-directory-files-and-attributes)
225 (dired-compress-file . ignore)
226 (dired-uncache . tramp-handle-dired-uncache)
227 (expand-file-name . tramp-smb-handle-expand-file-name)
228 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
229 (file-acl . tramp-smb-handle-file-acl)
230 (file-attributes . tramp-smb-handle-file-attributes)
231 (file-directory-p . tramp-smb-handle-file-directory-p)
232 (file-file-equal-p . tramp-handle-file-equal-p)
233 (file-executable-p . tramp-handle-file-exists-p)
234 (file-exists-p . tramp-handle-file-exists-p)
235 (file-in-directory-p . tramp-handle-file-in-directory-p)
236 (file-local-copy . tramp-smb-handle-file-local-copy)
237 (file-modes . tramp-handle-file-modes)
238 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
239 (file-name-as-directory . tramp-handle-file-name-as-directory)
240 (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
241 (file-name-completion . tramp-handle-file-name-completion)
242 (file-name-directory . tramp-handle-file-name-directory)
243 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
244 ;; `file-name-sans-versions' performed by default handler.
245 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
246 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
247 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
248 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
249 (file-ownership-preserved-p . ignore)
250 (file-readable-p . tramp-handle-file-exists-p)
251 (file-regular-p . tramp-handle-file-regular-p)
252 (file-remote-p . tramp-handle-file-remote-p)
253 ;; `file-selinux-context' performed by default handler.
254 (file-symlink-p . tramp-handle-file-symlink-p)
255 ;; `file-truename' performed by default handler.
256 (file-writable-p . tramp-smb-handle-file-writable-p)
257 (find-backup-file-name . tramp-handle-find-backup-file-name)
258 ;; `find-file-noselect' performed by default handler.
259 ;; `get-file-buffer' performed by default handler.
260 (insert-directory . tramp-smb-handle-insert-directory)
261 (insert-file-contents . tramp-handle-insert-file-contents)
262 (load . tramp-handle-load)
263 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
264 (make-directory . tramp-smb-handle-make-directory)
265 (make-directory-internal . tramp-smb-handle-make-directory-internal)
266 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
267 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
268 (process-file . tramp-smb-handle-process-file)
269 (rename-file . tramp-smb-handle-rename-file)
270 (set-file-acl . tramp-smb-handle-set-file-acl)
271 (set-file-modes . tramp-smb-handle-set-file-modes)
272 (set-file-selinux-context . ignore)
273 (set-file-times . ignore)
274 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
275 (shell-command . tramp-handle-shell-command)
276 (start-file-process . tramp-smb-handle-start-file-process)
277 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
278 (temporary-file-directory . tramp-handle-temporary-file-directory)
279 (unhandled-file-name-directory . ignore)
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.
287 ;;;###tramp-autoload
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."
292 :group 'tramp
293 :type 'string
294 :version "24.3"
295 :require 'tramp)
297 ;;;###tramp-autoload
298 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
299 "Shell to be used for processes on remote machines.
300 This must be Powershell V2 compatible."
301 :group 'tramp
302 :type 'string
303 :version "24.3"
304 :require 'tramp)
306 ;;;###tramp-autoload
307 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
308 "Command switch used together with `tramp-smb-winexe-shell-command'.
309 This can be used to disable echo etc."
310 :group 'tramp
311 :type 'string
312 :version "24.3"
313 :require 'tramp)
315 ;; It must be a `defsubst' in order to push the whole code into
316 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
317 ;;;###tramp-autoload
318 (defsubst tramp-smb-file-name-p (filename)
319 "Check if it's a filename for SMB servers."
320 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
321 tramp-smb-method))
323 ;;;###tramp-autoload
324 (defun tramp-smb-file-name-handler (operation &rest args)
325 "Invoke the SMB related OPERATION.
326 First arg specifies the OPERATION, second arg is a list of arguments to
327 pass to the OPERATION."
328 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
329 (if fn
330 (save-match-data (apply (cdr fn) args))
331 (tramp-run-real-handler operation args))))
333 ;;;###tramp-autoload
334 (unless (memq system-type '(cygwin windows-nt))
335 (tramp-register-foreign-file-name-handler
336 'tramp-smb-file-name-p 'tramp-smb-file-name-handler))
338 ;; File name primitives.
340 (defun tramp-smb-handle-add-name-to-file
341 (filename newname &optional ok-if-already-exists)
342 "Like `add-name-to-file' for Tramp files."
343 (unless (tramp-equal-remote filename newname)
344 (with-parsed-tramp-file-name
345 (if (tramp-tramp-file-p filename) filename newname) nil
346 (tramp-error
347 v 'file-error
348 "add-name-to-file: %s"
349 "only implemented for same method, same user, same host")))
350 (with-parsed-tramp-file-name filename v1
351 (with-parsed-tramp-file-name newname v2
352 (when (file-directory-p filename)
353 (tramp-error
354 v2 'file-error
355 "add-name-to-file: %s must not be a directory" filename))
356 (when (and (not ok-if-already-exists)
357 (file-exists-p newname)
358 (not (numberp ok-if-already-exists))
359 (y-or-n-p
360 (format
361 "File %s already exists; make it a new name anyway? "
362 newname)))
363 (tramp-error
364 v2 'file-error
365 "add-name-to-file: file %s already exists" newname))
366 ;; We must also flush the cache of the directory, because
367 ;; `file-attributes' reads the values from there.
368 (tramp-flush-file-property v2 (file-name-directory v2-localname))
369 (tramp-flush-file-property v2 v2-localname)
370 (unless
371 (tramp-smb-send-command
373 (format
374 "%s \"%s\" \"%s\""
375 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
376 (tramp-smb-get-localname v1)
377 (tramp-smb-get-localname v2)))
378 (tramp-error
379 v2 'file-error
380 "error with add-name-to-file, see buffer `%s' for details"
381 (buffer-name))))))
383 (defun tramp-smb-action-with-tar (proc vec)
384 "Untar from connection buffer."
385 (if (not (process-live-p proc))
386 (throw 'tramp-action 'process-died)
388 (with-current-buffer (tramp-get-connection-buffer vec)
389 (goto-char (point-min))
390 (when (search-forward-regexp tramp-smb-server-version nil t)
391 ;; There might be a hidden password prompt.
392 (widen)
393 (forward-line)
394 (tramp-message vec 6 (buffer-substring (point-min) (point)))
395 (delete-region (point-min) (point))
396 (throw 'tramp-action 'ok)))))
398 (defun tramp-smb-handle-copy-directory
399 (dirname newname &optional keep-date parents copy-contents)
400 "Like `copy-directory' for Tramp files."
401 (if copy-contents
402 ;; We must do it file-wise.
403 (tramp-run-real-handler
404 'copy-directory (list dirname newname keep-date parents copy-contents))
406 (setq dirname (expand-file-name dirname)
407 newname (expand-file-name newname))
408 (let ((t1 (tramp-tramp-file-p dirname))
409 (t2 (tramp-tramp-file-p newname)))
410 (with-parsed-tramp-file-name (if t1 dirname newname) nil
411 (with-tramp-progress-reporter
412 v 0 (format "Copying %s to %s" dirname newname)
413 (cond
414 ;; We must use a local temporary directory.
415 ((and t1 t2)
416 (let ((tmpdir
417 (make-temp-name
418 (expand-file-name
419 tramp-temp-name-prefix
420 (tramp-compat-temporary-file-directory)))))
421 (unwind-protect
422 (progn
423 (make-directory tmpdir)
424 (copy-directory dirname tmpdir keep-date 'parents)
425 (copy-directory
426 (expand-file-name (file-name-nondirectory dirname) tmpdir)
427 newname keep-date parents))
428 (delete-directory tmpdir 'recursive))))
430 ;; We can copy recursively.
431 ((and (or t1 t2) (tramp-smb-get-cifs-capabilities v))
432 (when (and (file-directory-p newname)
433 (not (string-equal (file-name-nondirectory dirname)
434 (file-name-nondirectory newname))))
435 (setq newname
436 (expand-file-name
437 (file-name-nondirectory dirname) newname))
438 (if t2 (setq v (tramp-dissect-file-name newname))))
439 (if (not (file-directory-p newname))
440 (make-directory newname parents))
442 ;; Set variables for computing the prompt for reading password.
443 (setq tramp-current-method method
444 tramp-current-user user
445 tramp-current-domain domain
446 tramp-current-host host
447 tramp-current-port port)
449 (let* ((share (tramp-smb-get-share v))
450 (localname (file-name-as-directory
451 (replace-regexp-in-string
452 "\\\\" "/" (tramp-smb-get-localname v))))
453 (tmpdir (make-temp-name
454 (expand-file-name
455 tramp-temp-name-prefix
456 (tramp-compat-temporary-file-directory))))
457 (args (list (concat "//" host "/" share) "-E")))
459 (if (not (zerop (length user)))
460 (setq args (append args (list "-U" 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))))
465 (when tramp-smb-conf
466 (setq args (append args (list "-s" tramp-smb-conf))))
467 (setq args
468 (if t1
469 ;; Source is remote.
470 (append args
471 (list "-D" (tramp-unquote-shell-quote-argument
472 localname)
473 "-c" (shell-quote-argument "tar qc - *")
474 "|" "tar" "xfC" "-"
475 (tramp-unquote-shell-quote-argument
476 tmpdir)))
477 ;; Target is remote.
478 (append (list "tar" "cfC" "-"
479 (tramp-unquote-shell-quote-argument dirname)
480 "." "|")
481 args
482 (list "-D" (tramp-unquote-shell-quote-argument
483 localname)
484 "-c" (shell-quote-argument "tar qx -")))))
486 (unwind-protect
487 (with-temp-buffer
488 ;; Set the transfer process properties.
489 (tramp-set-connection-property
490 v "process-name" (buffer-name (current-buffer)))
491 (tramp-set-connection-property
492 v "process-buffer" (current-buffer))
494 (when t1
495 ;; The smbclient tar command creates always
496 ;; complete paths. We must emulate the
497 ;; directory structure, and symlink to the real
498 ;; target.
499 (make-directory
500 (expand-file-name
501 ".." (concat tmpdir localname))
502 'parents)
503 (make-symbolic-link
504 newname (directory-file-name (concat tmpdir localname))))
506 ;; Use an asynchronous processes. By this,
507 ;; password can be handled.
508 (let* ((default-directory tmpdir)
509 (p (apply
510 'start-process
511 (tramp-get-connection-name v)
512 (tramp-get-connection-buffer v)
513 tramp-smb-program args)))
515 (tramp-message
516 v 6 "%s" (mapconcat 'identity (process-command p) " "))
517 (tramp-set-connection-property p "vector" v)
518 (process-put p 'adjust-window-size-function 'ignore)
519 (set-process-query-on-exit-flag p nil)
520 (tramp-process-actions p v nil tramp-smb-actions-with-tar)
522 (while (process-live-p p)
523 (sit-for 0.1))
524 (tramp-message v 6 "\n%s" (buffer-string))))
526 ;; Reset the transfer process properties.
527 (tramp-set-connection-property v "process-name" nil)
528 (tramp-set-connection-property v "process-buffer" nil)
529 (when t1 (delete-directory tmpdir 'recurse))))
531 ;; Handle KEEP-DATE argument.
532 (when keep-date
533 (set-file-times
534 newname
535 (tramp-compat-file-attribute-modification-time
536 (file-attributes dirname))))
538 ;; Set the mode.
539 (unless keep-date
540 (set-file-modes newname (tramp-default-file-modes dirname)))
542 ;; When newname did exist, we have wrong cached values.
543 (when t2
544 (with-parsed-tramp-file-name newname nil
545 (tramp-flush-file-property v (file-name-directory localname))
546 (tramp-flush-file-property v localname))))
548 ;; We must do it file-wise.
550 (tramp-run-real-handler
551 'copy-directory (list dirname newname keep-date parents)))))))))
553 (defun tramp-smb-handle-copy-file
554 (filename newname &optional ok-if-already-exists keep-date
555 _preserve-uid-gid _preserve-extended-attributes)
556 "Like `copy-file' for Tramp files.
557 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
558 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
559 (setq filename (expand-file-name filename)
560 newname (expand-file-name newname))
561 (with-tramp-progress-reporter
562 (tramp-dissect-file-name
563 (if (tramp-tramp-file-p filename) filename newname))
564 0 (format "Copying %s to %s" filename newname)
566 (if (file-directory-p filename)
567 (copy-directory
568 filename newname keep-date 'parents 'copy-contents)
570 (let ((tmpfile (file-local-copy filename)))
571 (if tmpfile
572 ;; Remote filename.
573 (condition-case err
574 (rename-file tmpfile newname ok-if-already-exists)
575 ((error quit)
576 (delete-file tmpfile)
577 (signal (car err) (cdr err))))
579 ;; Remote newname.
580 (when (file-directory-p newname)
581 (setq newname
582 (expand-file-name (file-name-nondirectory filename) newname)))
584 (with-parsed-tramp-file-name newname nil
585 (when (and (not ok-if-already-exists)
586 (file-exists-p newname))
587 (tramp-error v 'file-already-exists newname))
589 ;; We must also flush the cache of the directory, because
590 ;; `file-attributes' reads the values from there.
591 (tramp-flush-file-property v (file-name-directory localname))
592 (tramp-flush-file-property v localname)
593 (unless (tramp-smb-get-share v)
594 (tramp-error
595 v 'file-error "Target `%s' must contain a share name" newname))
596 (unless (tramp-smb-send-command
597 v (format "put \"%s\" \"%s\""
598 (tramp-compat-file-name-unquote filename)
599 (tramp-smb-get-localname v)))
600 (tramp-error
601 v 'file-error "Cannot copy `%s' to `%s'" filename newname))))))
603 ;; KEEP-DATE handling.
604 (when keep-date
605 (set-file-times
606 newname
607 (tramp-compat-file-attribute-modification-time
608 (file-attributes filename))))))
610 (defun tramp-smb-handle-delete-directory (directory &optional recursive _trash)
611 "Like `delete-directory' for Tramp files."
612 (setq directory (directory-file-name (expand-file-name directory)))
613 (when (file-exists-p directory)
614 (when recursive
615 (mapc
616 (lambda (file)
617 (if (file-directory-p file)
618 (delete-directory file recursive)
619 (delete-file file)))
620 ;; We do not want to delete "." and "..".
621 (directory-files directory 'full directory-files-no-dot-files-regexp)))
623 (with-parsed-tramp-file-name directory nil
624 ;; We must also flush the cache of the directory, because
625 ;; `file-attributes' reads the values from there.
626 (tramp-flush-file-property v (file-name-directory localname))
627 (tramp-flush-directory-property v localname)
628 (unless (tramp-smb-send-command
629 v (format
630 "%s \"%s\""
631 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
632 (tramp-smb-get-localname v)))
633 ;; Error.
634 (with-current-buffer (tramp-get-connection-buffer v)
635 (goto-char (point-min))
636 (search-forward-regexp tramp-smb-errors nil t)
637 (tramp-error
638 v 'file-error "%s `%s'" (match-string 0) directory))))))
640 (defun tramp-smb-handle-delete-file (filename &optional _trash)
641 "Like `delete-file' for Tramp files."
642 (setq filename (expand-file-name filename))
643 (when (file-exists-p filename)
644 (with-parsed-tramp-file-name filename nil
645 ;; We must also flush the cache of the directory, because
646 ;; `file-attributes' reads the values from there.
647 (tramp-flush-file-property v (file-name-directory localname))
648 (tramp-flush-file-property v localname)
649 (unless (tramp-smb-send-command
650 v (format
651 "%s \"%s\""
652 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
653 (tramp-smb-get-localname v)))
654 ;; Error.
655 (with-current-buffer (tramp-get-connection-buffer v)
656 (goto-char (point-min))
657 (search-forward-regexp tramp-smb-errors nil t)
658 (tramp-error
659 v 'file-error "%s `%s'" (match-string 0) filename))))))
661 (defun tramp-smb-handle-directory-files
662 (directory &optional full match nosort)
663 "Like `directory-files' for Tramp files."
664 (let ((result (mapcar 'directory-file-name
665 (file-name-all-completions "" directory))))
666 ;; Discriminate with regexp.
667 (when match
668 (setq result
669 (delete nil
670 (mapcar (lambda (x) (when (string-match match x) x))
671 result))))
672 ;; Append directory.
673 (when full
674 (setq result
675 (mapcar
676 (lambda (x) (format "%s/%s" directory x))
677 result)))
678 ;; Sort them if necessary.
679 (unless nosort (setq result (sort result 'string-lessp)))
680 result))
682 (defun tramp-smb-handle-expand-file-name (name &optional dir)
683 "Like `expand-file-name' for Tramp files."
684 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
685 (setq dir (or dir default-directory "/"))
686 ;; Unless NAME is absolute, concat DIR and NAME.
687 (unless (file-name-absolute-p name)
688 (setq name (concat (file-name-as-directory dir) name)))
689 ;; If NAME is not a Tramp file, run the real handler.
690 (if (not (tramp-tramp-file-p name))
691 (tramp-run-real-handler 'expand-file-name (list name nil))
692 ;; Dissect NAME.
693 (with-parsed-tramp-file-name name nil
694 ;; Tilde expansion if necessary. We use the user name as share,
695 ;; which is often the case in domains.
696 (when (string-match "\\`/?~\\([^/]*\\)" localname)
697 (setq localname
698 (replace-match
699 (if (zerop (length (match-string 1 localname)))
700 user
701 (match-string 1 localname))
702 nil nil localname)))
703 ;; Make the file name absolute.
704 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
705 (setq localname (concat "/" localname)))
706 ;; No tilde characters in file name, do normal
707 ;; `expand-file-name' (this does "/./" and "/../").
708 (tramp-make-tramp-file-name
709 method user domain host port
710 (tramp-run-real-handler 'expand-file-name (list localname))))))
712 (defun tramp-smb-action-get-acl (proc vec)
713 "Read ACL data from connection buffer."
714 (unless (process-live-p proc)
715 ;; Accept pending output.
716 (while (tramp-accept-process-output proc 0.1))
717 (with-current-buffer (tramp-get-connection-buffer vec)
718 ;; There might be a hidden password prompt.
719 (widen)
720 (tramp-message vec 10 "\n%s" (buffer-string))
721 (goto-char (point-min))
722 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
723 (forward-line)
724 (delete-region (point-min) (point)))
725 (while (and (not (eobp)) (looking-at "^.+:.+"))
726 (forward-line))
727 (delete-region (point) (point-max))
728 (throw 'tramp-action 'ok))))
730 (defun tramp-smb-handle-file-acl (filename)
731 "Like `file-acl' for Tramp files."
732 (with-parsed-tramp-file-name filename nil
733 (with-tramp-file-property v localname "file-acl"
734 (when (executable-find tramp-smb-acl-program)
735 ;; Set variables for computing the prompt for reading password.
736 (setq tramp-current-method method
737 tramp-current-user user
738 tramp-current-domain domain
739 tramp-current-host host
740 tramp-current-port port)
742 (let* ((share (tramp-smb-get-share v))
743 (localname (replace-regexp-in-string
744 "\\\\" "/" (tramp-smb-get-localname v)))
745 (args (list (concat "//" host "/" share) "-E")))
747 (if (not (zerop (length user)))
748 (setq args (append args (list "-U" user)))
749 (setq args (append args (list "-N"))))
751 (when domain (setq args (append args (list "-W" domain))))
752 (when port (setq args (append args (list "-p" port))))
753 (when tramp-smb-conf
754 (setq args (append args (list "-s" tramp-smb-conf))))
755 (setq
756 args
757 (append args (list (tramp-unquote-shell-quote-argument localname)
758 "2>/dev/null")))
760 (unwind-protect
761 (with-temp-buffer
762 ;; Set the transfer process properties.
763 (tramp-set-connection-property
764 v "process-name" (buffer-name (current-buffer)))
765 (tramp-set-connection-property
766 v "process-buffer" (current-buffer))
768 ;; Use an asynchronous processes. By this, password
769 ;; can be handled.
770 (let ((p (apply
771 'start-process
772 (tramp-get-connection-name v)
773 (tramp-get-connection-buffer v)
774 tramp-smb-acl-program args)))
776 (tramp-message
777 v 6 "%s" (mapconcat 'identity (process-command p) " "))
778 (tramp-set-connection-property p "vector" v)
779 (process-put p 'adjust-window-size-function 'ignore)
780 (set-process-query-on-exit-flag p nil)
781 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
782 (when (> (point-max) (point-min))
783 (substring-no-properties (buffer-string)))))
785 ;; Reset the transfer process properties.
786 (tramp-set-connection-property v "process-name" nil)
787 (tramp-set-connection-property v "process-buffer" nil)))))))
789 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
790 "Like `file-attributes' for Tramp files."
791 (unless id-format (setq id-format 'integer))
792 (ignore-errors
793 (with-parsed-tramp-file-name filename nil
794 (with-tramp-file-property
795 v localname (format "file-attributes-%s" id-format)
796 (if (tramp-smb-get-stat-capability v)
797 (tramp-smb-do-file-attributes-with-stat v id-format)
798 ;; Reading just the filename entry via "dir localname" is not
799 ;; possible, because when filename is a directory, some
800 ;; smbclient versions return the content of the directory, and
801 ;; other versions don't. Therefore, the whole content of the
802 ;; upper directory is retrieved, and the entry of the filename
803 ;; is extracted from.
804 (let* ((entries (tramp-smb-get-file-entries
805 (file-name-directory filename)))
806 (entry (assoc (file-name-nondirectory filename) entries))
807 (uid (if (equal id-format 'string) "nobody" -1))
808 (gid (if (equal id-format 'string) "nogroup" -1))
809 (inode (tramp-get-inode v))
810 (device (tramp-get-device v)))
812 ;; Check result.
813 (when entry
814 (list (and (string-match "d" (nth 1 entry))
815 t) ;0 file type
816 -1 ;1 link count
817 uid ;2 uid
818 gid ;3 gid
819 '(0 0) ;4 atime
820 (nth 3 entry) ;5 mtime
821 '(0 0) ;6 ctime
822 (nth 2 entry) ;7 size
823 (nth 1 entry) ;8 mode
824 nil ;9 gid weird
825 inode ;10 inode number
826 device)))))))) ;11 file system number
828 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
829 "Implement `file-attributes' for Tramp files using stat command."
830 (tramp-message
831 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
832 (with-current-buffer (tramp-get-connection-buffer vec)
833 (let* (size id link uid gid atime mtime ctime mode inode)
834 (when (tramp-smb-send-command
835 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
837 ;; Loop the listing.
838 (goto-char (point-min))
839 (unless (re-search-forward tramp-smb-errors nil t)
840 (while (not (eobp))
841 (cond
842 ((looking-at
843 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
844 (setq size (string-to-number (match-string 1))
845 id (if (string-equal "directory" (match-string 2)) t
846 (if (string-equal "symbolic" (match-string 2)) ""))))
847 ((looking-at
848 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
849 (setq inode (string-to-number (match-string 1))
850 link (string-to-number (match-string 2))))
851 ((looking-at
852 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
853 (setq mode (match-string 1)
854 uid (if (equal id-format 'string) (match-string 2)
855 (string-to-number (match-string 2)))
856 gid (if (equal id-format 'string) (match-string 3)
857 (string-to-number (match-string 3)))))
858 ((looking-at
859 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
860 (setq atime
861 (encode-time
862 (string-to-number (match-string 6)) ;; sec
863 (string-to-number (match-string 5)) ;; min
864 (string-to-number (match-string 4)) ;; hour
865 (string-to-number (match-string 3)) ;; day
866 (string-to-number (match-string 2)) ;; month
867 (string-to-number (match-string 1))))) ;; year
868 ((looking-at
869 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
870 (setq mtime
871 (encode-time
872 (string-to-number (match-string 6)) ;; sec
873 (string-to-number (match-string 5)) ;; min
874 (string-to-number (match-string 4)) ;; hour
875 (string-to-number (match-string 3)) ;; day
876 (string-to-number (match-string 2)) ;; month
877 (string-to-number (match-string 1))))) ;; year
878 ((looking-at
879 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
880 (setq ctime
881 (encode-time
882 (string-to-number (match-string 6)) ;; sec
883 (string-to-number (match-string 5)) ;; min
884 (string-to-number (match-string 4)) ;; hour
885 (string-to-number (match-string 3)) ;; day
886 (string-to-number (match-string 2)) ;; month
887 (string-to-number (match-string 1)))))) ;; year
888 (forward-line))
889 ;; Return the result.
890 (list id link uid gid atime mtime ctime size mode nil inode
891 (tramp-get-device vec)))))))
893 (defun tramp-smb-handle-file-directory-p (filename)
894 "Like `file-directory-p' for Tramp files."
895 (and (file-exists-p filename)
896 (eq ?d
897 (aref (tramp-compat-file-attribute-modes (file-attributes filename))
898 0))))
900 (defun tramp-smb-handle-file-local-copy (filename)
901 "Like `file-local-copy' for Tramp files."
902 (with-parsed-tramp-file-name filename nil
903 (unless (file-exists-p filename)
904 (tramp-error
905 v tramp-file-missing
906 "Cannot make local copy of non-existing file `%s'" filename))
907 (let ((tmpfile (tramp-compat-make-temp-file filename)))
908 (with-tramp-progress-reporter
909 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
910 (unless (tramp-smb-send-command
911 v (format "get \"%s\" \"%s\""
912 (tramp-smb-get-localname v) tmpfile))
913 ;; Oops, an error. We shall cleanup.
914 (delete-file tmpfile)
915 (tramp-error
916 v 'file-error "Cannot make local copy of file `%s'" filename)))
917 tmpfile)))
919 ;; This function should return "foo/" for directories and "bar" for
920 ;; files.
921 (defun tramp-smb-handle-file-name-all-completions (filename directory)
922 "Like `file-name-all-completions' for Tramp files."
923 (all-completions
924 filename
925 (with-parsed-tramp-file-name (expand-file-name directory) nil
926 (with-tramp-file-property v localname "file-name-all-completions"
927 (save-match-data
928 (delete-dups
929 (mapcar
930 (lambda (x)
931 (list
932 (if (string-match "d" (nth 1 x))
933 (file-name-as-directory (nth 0 x))
934 (nth 0 x))))
935 (tramp-smb-get-file-entries directory))))))))
937 (defun tramp-smb-handle-file-writable-p (filename)
938 "Like `file-writable-p' for Tramp files."
939 (if (file-exists-p filename)
940 (string-match
942 (or (tramp-compat-file-attribute-modes (file-attributes filename)) ""))
943 (let ((dir (file-name-directory filename)))
944 (and (file-exists-p dir)
945 (file-writable-p dir)))))
947 (defun tramp-smb-handle-insert-directory
948 (filename switches &optional wildcard full-directory-p)
949 "Like `insert-directory' for Tramp files."
950 (setq filename (expand-file-name filename))
951 (unless switches (setq switches ""))
952 ;; Mark trailing "/".
953 (when (and (zerop (length (file-name-nondirectory filename)))
954 (not full-directory-p))
955 (setq switches (concat switches "F")))
956 (if full-directory-p
957 ;; Called from `dired-add-entry'.
958 (setq filename (file-name-as-directory filename))
959 (setq filename (directory-file-name filename)))
960 (with-parsed-tramp-file-name filename nil
961 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
962 (save-match-data
963 (let ((base (file-name-nondirectory filename))
964 ;; We should not destroy the cache entry.
965 (entries (copy-sequence
966 (tramp-smb-get-file-entries
967 (file-name-directory filename)))))
969 (when wildcard
970 (string-match "\\." base)
971 (setq base (replace-match "\\\\." nil nil base))
972 (string-match "\\*" base)
973 (setq base (replace-match ".*" nil nil base))
974 (string-match "\\?" base)
975 (setq base (replace-match ".?" nil nil base)))
977 ;; Filter entries.
978 (setq entries
979 (delq
981 (if (or wildcard (zerop (length base)))
982 ;; Check for matching entries.
983 (mapcar
984 (lambda (x)
985 (when (string-match
986 (format "^%s" base) (nth 0 x))
988 entries)
989 ;; We just need the only and only entry FILENAME.
990 (list (assoc base entries)))))
992 ;; Sort entries.
993 (setq entries
994 (sort
995 entries
996 (lambda (x y)
997 (if (string-match "t" switches)
998 ;; Sort by date.
999 (time-less-p (nth 3 y) (nth 3 x))
1000 ;; Sort by name.
1001 (string-lessp (nth 0 x) (nth 0 y))))))
1003 ;; Handle "-F" switch.
1004 (when (string-match "F" switches)
1005 (mapc
1006 (lambda (x)
1007 (when (not (zerop (length (car x))))
1008 (cond
1009 ((char-equal ?d (string-to-char (nth 1 x)))
1010 (setcar x (concat (car x) "/")))
1011 ((char-equal ?x (string-to-char (nth 1 x)))
1012 (setcar x (concat (car x) "*"))))))
1013 entries))
1015 ;; Print entries.
1016 (mapc
1017 (lambda (x)
1018 (when (not (zerop (length (nth 0 x))))
1019 (when (string-match "l" switches)
1020 (let ((attr
1021 (when (tramp-smb-get-stat-capability v)
1022 (ignore-errors
1023 (file-attributes filename 'string)))))
1024 (insert
1025 (format
1026 "%10s %3d %-8s %-8s %8s %s "
1027 (or (tramp-compat-file-attribute-modes attr) (nth 1 x))
1028 (or (tramp-compat-file-attribute-link-number attr) 1)
1029 (or (tramp-compat-file-attribute-user-id attr) "nobody")
1030 (or (tramp-compat-file-attribute-group-id attr) "nogroup")
1031 (or (tramp-compat-file-attribute-size attr) (nth 2 x))
1032 (format-time-string
1033 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1034 tramp-half-a-year)
1035 "%b %e %R"
1036 "%b %e %Y")
1037 (nth 3 x)))))) ; date
1039 ;; We mark the file name. The inserted name could be
1040 ;; from somewhere else, so we use the relative file name
1041 ;; of `default-directory'.
1042 (let ((start (point)))
1043 (insert
1044 (format
1045 "%s\n"
1046 (file-relative-name
1047 (expand-file-name
1048 (nth 0 x) (file-name-directory filename))
1049 (when full-directory-p (file-name-directory filename)))))
1050 (put-text-property start (1- (point)) 'dired-filename t))
1051 (forward-line)
1052 (beginning-of-line)))
1053 entries))))))
1055 (defun tramp-smb-handle-make-directory (dir &optional parents)
1056 "Like `make-directory' for Tramp files."
1057 (setq dir (directory-file-name (expand-file-name dir)))
1058 (unless (file-name-absolute-p dir)
1059 (setq dir (expand-file-name dir default-directory)))
1060 (with-parsed-tramp-file-name dir nil
1061 (save-match-data
1062 (let* ((ldir (file-name-directory dir)))
1063 ;; Make missing directory parts.
1064 (when (and parents
1065 (tramp-smb-get-share v)
1066 (not (file-directory-p ldir)))
1067 (make-directory ldir parents))
1068 ;; Just do it.
1069 (when (file-directory-p ldir)
1070 (make-directory-internal dir))
1071 (unless (file-directory-p dir)
1072 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1074 (defun tramp-smb-handle-make-directory-internal (directory)
1075 "Like `make-directory-internal' for Tramp files."
1076 (setq directory (directory-file-name (expand-file-name directory)))
1077 (unless (file-name-absolute-p directory)
1078 (setq directory (expand-file-name directory default-directory)))
1079 (with-parsed-tramp-file-name directory nil
1080 (save-match-data
1081 (let* ((file (tramp-smb-get-localname v)))
1082 (when (file-directory-p (file-name-directory directory))
1083 (tramp-smb-send-command
1085 (if (tramp-smb-get-cifs-capabilities v)
1086 (format "posix_mkdir \"%s\" %o" file (default-file-modes))
1087 (format "mkdir \"%s\"" file)))
1088 ;; We must also flush the cache of the directory, because
1089 ;; `file-attributes' reads the values from there.
1090 (tramp-flush-file-property v (file-name-directory localname))
1091 (tramp-flush-file-property v localname))
1092 (unless (file-directory-p directory)
1093 (tramp-error
1094 v 'file-error "Couldn't make directory %s" directory))))))
1096 (defun tramp-smb-handle-make-symbolic-link
1097 (filename linkname &optional ok-if-already-exists)
1098 "Like `make-symbolic-link' for Tramp files.
1099 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1100 the symlink. If LINKNAME is a Tramp file, only the localname component is
1101 used as the target of the symlink.
1103 If LINKNAME is a Tramp file and the localname component is relative, then
1104 it is expanded first, before the localname component is taken. Note that
1105 this can give surprising results if the user/host for the source and
1106 target of the symlink differ."
1107 (unless (tramp-equal-remote filename linkname)
1108 (with-parsed-tramp-file-name
1109 (if (tramp-tramp-file-p filename) filename linkname) nil
1110 (tramp-error
1111 v 'file-error
1112 "make-symbolic-link: %s"
1113 "only implemented for same method, same user, same host")))
1114 (with-parsed-tramp-file-name filename v1
1115 (with-parsed-tramp-file-name linkname v2
1116 (when (file-directory-p filename)
1117 (tramp-error
1118 v2 'file-error
1119 "make-symbolic-link: %s must not be a directory" filename))
1120 (when (and (not ok-if-already-exists)
1121 (file-exists-p linkname)
1122 (not (numberp ok-if-already-exists))
1123 (y-or-n-p
1124 (format
1125 "File %s already exists; make it a new name anyway? "
1126 linkname)))
1127 (tramp-error
1128 v2 'file-already-exists
1129 "make-symbolic-link: file %s already exists" linkname))
1130 (unless (tramp-smb-get-cifs-capabilities v1)
1131 (tramp-error v2 'file-error "make-symbolic-link not supported"))
1132 ;; We must also flush the cache of the directory, because
1133 ;; `file-attributes' reads the values from there.
1134 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1135 (tramp-flush-file-property v2 v2-localname)
1136 (unless
1137 (tramp-smb-send-command
1139 (format
1140 "symlink \"%s\" \"%s\""
1141 (tramp-smb-get-localname v1)
1142 (tramp-smb-get-localname v2)))
1143 (tramp-error
1144 v2 'file-error
1145 "error with make-symbolic-link, see buffer `%s' for details"
1146 (buffer-name))))))
1148 (defun tramp-smb-handle-process-file
1149 (program &optional infile destination display &rest args)
1150 "Like `process-file' for Tramp files."
1151 ;; The implementation is not complete yet.
1152 (when (and (numberp destination) (zerop destination))
1153 (error "Implementation does not handle immediate return"))
1155 (with-parsed-tramp-file-name default-directory nil
1156 (let* ((name (file-name-nondirectory program))
1157 (name1 name)
1158 (i 0)
1159 input tmpinput outbuf command ret)
1161 ;; Determine input.
1162 (when infile
1163 (setq infile (expand-file-name infile))
1164 (if (tramp-equal-remote default-directory infile)
1165 ;; INFILE is on the same remote host.
1166 (setq input (with-parsed-tramp-file-name infile nil localname))
1167 ;; INFILE must be copied to remote host.
1168 (setq input (tramp-make-tramp-temp-file v)
1169 tmpinput
1170 (tramp-make-tramp-file-name method user domain host port input))
1171 (copy-file infile tmpinput t))
1172 ;; Transform input into a filename powershell does understand.
1173 (setq input (format "//%s%s" host input)))
1175 ;; Determine output.
1176 (cond
1177 ;; Just a buffer.
1178 ((bufferp destination)
1179 (setq outbuf destination))
1180 ;; A buffer name.
1181 ((stringp destination)
1182 (setq outbuf (get-buffer-create destination)))
1183 ;; (REAL-DESTINATION ERROR-DESTINATION)
1184 ((consp destination)
1185 ;; output.
1186 (cond
1187 ((bufferp (car destination))
1188 (setq outbuf (car destination)))
1189 ((stringp (car destination))
1190 (setq outbuf (get-buffer-create (car destination))))
1191 ((car destination)
1192 (setq outbuf (current-buffer))))
1193 ;; stderr.
1194 (tramp-message v 2 "%s" "STDERR not supported"))
1195 ;; 't
1196 (destination
1197 (setq outbuf (current-buffer))))
1199 ;; Construct command.
1200 (setq command (mapconcat 'identity (cons program args) " ")
1201 command (if input
1202 (format
1203 "get-content %s | & %s"
1204 (tramp-smb-shell-quote-argument input) command)
1205 (format "& %s" command)))
1207 (while (get-process name1)
1208 ;; NAME must be unique as process name.
1209 (setq i (1+ i)
1210 name1 (format "%s<%d>" name i)))
1212 ;; Set the new process properties.
1213 (tramp-set-connection-property v "process-name" name1)
1214 (tramp-set-connection-property
1215 v "process-buffer"
1216 (or outbuf (generate-new-buffer tramp-temp-buffer-name)))
1218 ;; Call it.
1219 (condition-case nil
1220 (with-current-buffer (tramp-get-connection-buffer v)
1221 ;; Preserve buffer contents.
1222 (narrow-to-region (point-max) (point-max))
1223 (tramp-smb-call-winexe v)
1224 (when (tramp-smb-get-share v)
1225 (tramp-smb-send-command
1226 v (format "cd \"//%s%s\"" host (file-name-directory localname))))
1227 (tramp-smb-send-command v command)
1228 ;; Preserve command output.
1229 (narrow-to-region (point-max) (point-max))
1230 (let ((p (tramp-get-connection-process v)))
1231 (tramp-smb-send-command v "exit $lasterrorcode")
1232 (while (process-live-p p)
1233 (sleep-for 0.1)
1234 (setq ret (process-exit-status p))))
1235 (delete-region (point-min) (point-max))
1236 (widen))
1238 ;; When the user did interrupt, we should do it also. We use
1239 ;; return code -1 as marker.
1240 (quit
1241 (setq ret -1))
1242 ;; Handle errors.
1243 (error
1244 (setq ret 1)))
1246 ;; We should redisplay the output.
1247 (when (and display outbuf (get-buffer-window outbuf t)) (redisplay))
1249 ;; Cleanup. We remove all file cache values for the connection,
1250 ;; because the remote process could have changed them.
1251 (tramp-set-connection-property v "process-name" nil)
1252 (tramp-set-connection-property v "process-buffer" nil)
1253 (when tmpinput (delete-file tmpinput))
1254 (unless outbuf
1255 (kill-buffer (tramp-get-connection-property v "process-buffer" nil)))
1257 (unless process-file-side-effects
1258 (tramp-flush-directory-property v ""))
1260 ;; Return exit status.
1261 (if (equal ret -1)
1262 (keyboard-quit)
1263 ret))))
1265 (defun tramp-smb-handle-rename-file
1266 (filename newname &optional ok-if-already-exists)
1267 "Like `rename-file' for Tramp files."
1268 (setq filename (expand-file-name filename)
1269 newname (expand-file-name newname))
1271 (when (and (not ok-if-already-exists)
1272 (file-exists-p newname))
1273 (tramp-error
1274 (tramp-dissect-file-name
1275 (if (tramp-tramp-file-p filename) filename newname))
1276 'file-already-exists newname))
1278 (with-tramp-progress-reporter
1279 (tramp-dissect-file-name
1280 (if (tramp-tramp-file-p filename) filename newname))
1281 0 (format "Renaming %s to %s" filename newname)
1283 (if (and (not (file-exists-p newname))
1284 (tramp-equal-remote filename newname)
1285 (string-equal
1286 (tramp-smb-get-share (tramp-dissect-file-name filename))
1287 (tramp-smb-get-share (tramp-dissect-file-name newname))))
1288 ;; We can rename directly.
1289 (with-parsed-tramp-file-name filename v1
1290 (with-parsed-tramp-file-name newname v2
1292 ;; We must also flush the cache of the directory, because
1293 ;; `file-attributes' reads the values from there.
1294 (tramp-flush-file-property v1 (file-name-directory v1-localname))
1295 (tramp-flush-file-property v1 v1-localname)
1296 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1297 (tramp-flush-file-property v2 v2-localname)
1298 (unless (tramp-smb-get-share v2)
1299 (tramp-error
1300 v2 'file-error "Target `%s' must contain a share name" newname))
1301 (unless (tramp-smb-send-command
1302 v2 (format "rename \"%s\" \"%s\""
1303 (tramp-smb-get-localname v1)
1304 (tramp-smb-get-localname v2)))
1305 (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
1307 ;; We must rename via copy.
1308 (copy-file
1309 filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
1310 (if (file-directory-p filename)
1311 (delete-directory filename 'recursive)
1312 (delete-file filename)))))
1314 (defun tramp-smb-action-set-acl (proc vec)
1315 "Read ACL data from connection buffer."
1316 (unless (process-live-p proc)
1317 ;; Accept pending output.
1318 (while (tramp-accept-process-output proc 0.1))
1319 (with-current-buffer (tramp-get-connection-buffer vec)
1320 (tramp-message vec 10 "\n%s" (buffer-string))
1321 (throw 'tramp-action 'ok))))
1323 (defun tramp-smb-handle-set-file-acl (filename acl-string)
1324 "Like `set-file-acl' for Tramp files."
1325 (ignore-errors
1326 (with-parsed-tramp-file-name filename nil
1327 (when (and (stringp acl-string) (executable-find tramp-smb-acl-program))
1328 ;; Set variables for computing the prompt for reading password.
1329 (setq tramp-current-method method
1330 tramp-current-user user
1331 tramp-current-domain domain
1332 tramp-current-host host
1333 tramp-current-port port)
1334 (tramp-set-file-property v localname "file-acl" 'undef)
1336 (let* ((share (tramp-smb-get-share v))
1337 (localname (replace-regexp-in-string
1338 "\\\\" "/" (tramp-smb-get-localname v)))
1339 (args (list (concat "//" host "/" share) "-E" "-S"
1340 (replace-regexp-in-string
1341 "\n" "," acl-string))))
1343 (if (not (zerop (length user)))
1344 (setq args (append args (list "-U" user)))
1345 (setq args (append args (list "-N"))))
1347 (when domain (setq args (append args (list "-W" domain))))
1348 (when port (setq args (append args (list "-p" port))))
1349 (when tramp-smb-conf
1350 (setq args (append args (list "-s" tramp-smb-conf))))
1351 (setq
1352 args
1353 (append args (list (tramp-unquote-shell-quote-argument localname)
1354 "&&" "echo" "tramp_exit_status" "0"
1355 "||" "echo" "tramp_exit_status" "1")))
1357 (unwind-protect
1358 (with-temp-buffer
1359 ;; Set the transfer process properties.
1360 (tramp-set-connection-property
1361 v "process-name" (buffer-name (current-buffer)))
1362 (tramp-set-connection-property
1363 v "process-buffer" (current-buffer))
1365 ;; Use an asynchronous processes. By this, password can
1366 ;; be handled.
1367 (let ((p (apply
1368 'start-process
1369 (tramp-get-connection-name v)
1370 (tramp-get-connection-buffer v)
1371 tramp-smb-acl-program args)))
1373 (tramp-message
1374 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1375 (tramp-set-connection-property p "vector" v)
1376 (process-put p 'adjust-window-size-function 'ignore)
1377 (set-process-query-on-exit-flag p nil)
1378 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1379 (goto-char (point-max))
1380 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1381 (tramp-error
1382 v 'file-error
1383 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1384 (skip-chars-forward "^ ")
1385 (when (zerop (read (current-buffer)))
1386 ;; Success.
1387 (tramp-set-file-property v localname "file-acl" acl-string)
1388 t)))
1390 ;; Reset the transfer process properties.
1391 (tramp-set-connection-property v "process-name" nil)
1392 (tramp-set-connection-property v "process-buffer" nil)))))))
1394 (defun tramp-smb-handle-set-file-modes (filename mode)
1395 "Like `set-file-modes' for Tramp files."
1396 (with-parsed-tramp-file-name filename nil
1397 (when (tramp-smb-get-cifs-capabilities v)
1398 (tramp-flush-file-property v localname)
1399 (unless (tramp-smb-send-command
1400 v (format "chmod \"%s\" %o" (tramp-smb-get-localname v) mode))
1401 (tramp-error
1402 v 'file-error "Error while changing file's mode %s" filename)))))
1404 ;; We use BUFFER also as connection buffer during setup. Because of
1405 ;; this, its original contents must be saved, and restored once
1406 ;; connection has been setup.
1407 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1408 "Like `start-file-process' for Tramp files."
1409 (with-parsed-tramp-file-name default-directory nil
1410 (let* ((buffer
1411 (if buffer
1412 (get-buffer-create buffer)
1413 ;; BUFFER can be nil. We use a temporary buffer.
1414 (generate-new-buffer tramp-temp-buffer-name)))
1415 (command (mapconcat 'identity (cons program args) " "))
1416 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1417 (name1 name)
1418 (i 0))
1419 (unwind-protect
1420 (save-excursion
1421 (save-restriction
1422 (while (get-process name1)
1423 ;; NAME must be unique as process name.
1424 (setq i (1+ i)
1425 name1 (format "%s<%d>" name i)))
1426 ;; Set the new process properties.
1427 (tramp-set-connection-property v "process-name" name1)
1428 (tramp-set-connection-property v "process-buffer" buffer)
1429 ;; Activate narrowing in order to save BUFFER contents.
1430 (with-current-buffer (tramp-get-connection-buffer v)
1431 (let ((buffer-undo-list t))
1432 (narrow-to-region (point-max) (point-max))
1433 (tramp-smb-call-winexe v)
1434 (when (tramp-smb-get-share v)
1435 (tramp-smb-send-command
1436 v (format
1437 "cd \"//%s%s\""
1438 host (file-name-directory localname))))
1439 (tramp-message v 6 "(%s); exit" command)
1440 (tramp-send-string v command)))
1441 ;; Return value.
1442 (tramp-get-connection-process v)))
1444 ;; Save exit.
1445 (with-current-buffer (tramp-get-connection-buffer v)
1446 (if (string-match tramp-temp-buffer-name (buffer-name))
1447 (progn
1448 (set-process-buffer (tramp-get-connection-process v) nil)
1449 (kill-buffer (current-buffer)))
1450 (set-buffer-modified-p bmp)))
1451 (tramp-set-connection-property v "process-name" nil)
1452 (tramp-set-connection-property v "process-buffer" nil)))))
1454 (defun tramp-smb-handle-substitute-in-file-name (filename)
1455 "Like `handle-substitute-in-file-name' for Tramp files.
1456 \"//\" substitutes only in the local filename part. Catches
1457 errors for shares like \"C$/\", which are common in Microsoft Windows."
1458 ;; Check, whether the local part is a quoted file name.
1459 (if (tramp-compat-file-name-quoted-p filename)
1460 filename
1461 (with-parsed-tramp-file-name filename nil
1462 ;; Ignore in LOCALNAME everything before "//".
1463 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1464 (setq filename
1465 (concat (file-remote-p filename)
1466 (replace-match "\\1" nil nil localname)))))
1467 (condition-case nil
1468 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1469 (error filename))))
1471 (defun tramp-smb-handle-write-region
1472 (start end filename &optional append visit lockname confirm)
1473 "Like `write-region' for Tramp files."
1474 (setq filename (expand-file-name filename))
1475 (with-parsed-tramp-file-name filename nil
1476 (when (and confirm (file-exists-p filename))
1477 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1478 filename))
1479 (tramp-error v 'file-error "File not overwritten")))
1480 ;; We must also flush the cache of the directory, because
1481 ;; `file-attributes' reads the values from there.
1482 (tramp-flush-file-property v (file-name-directory localname))
1483 (tramp-flush-file-property v localname)
1484 (let ((curbuf (current-buffer))
1485 (tmpfile (tramp-compat-make-temp-file filename)))
1486 (when (and append (file-exists-p filename))
1487 (copy-file filename tmpfile 'ok))
1488 ;; We say `no-message' here because we don't want the visited file
1489 ;; modtime data to be clobbered from the temp file. We call
1490 ;; `set-visited-file-modtime' ourselves later on.
1491 (tramp-run-real-handler
1492 'write-region
1493 (if confirm ; don't pass this arg unless defined for backward compat.
1494 (list start end tmpfile append 'no-message lockname confirm)
1495 (list start end tmpfile append 'no-message lockname)))
1497 (with-tramp-progress-reporter
1498 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1499 (unwind-protect
1500 (unless (tramp-smb-send-command
1501 v (format "put %s \"%s\""
1502 tmpfile (tramp-smb-get-localname v)))
1503 (tramp-error v 'file-error "Cannot write `%s'" filename))
1504 (delete-file tmpfile)))
1506 (unless (equal curbuf (current-buffer))
1507 (tramp-error
1508 v 'file-error
1509 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1510 (when (eq visit t)
1511 (set-visited-file-modtime)))))
1514 ;; Internal file name functions.
1516 (defun tramp-smb-get-share (vec)
1517 "Returns the share name of LOCALNAME."
1518 (save-match-data
1519 (let ((localname (tramp-file-name-unquote-localname vec)))
1520 (when (string-match "^/?\\([^/]+\\)/" localname)
1521 (match-string 1 localname)))))
1523 (defun tramp-smb-get-localname (vec)
1524 "Returns the file name of LOCALNAME.
1525 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1526 (save-match-data
1527 (let ((localname (tramp-file-name-unquote-localname vec)))
1528 (setq
1529 localname
1530 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1531 ;; There is a share, separated by "/".
1532 (if (not (tramp-smb-get-cifs-capabilities vec))
1533 (mapconcat
1534 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1535 (match-string 1 localname) "")
1536 (match-string 1 localname))
1537 ;; There is just a share.
1538 (if (string-match "^/?\\([^/]+\\)$" localname)
1539 (match-string 1 localname)
1540 "")))
1542 ;; Sometimes we have discarded `substitute-in-file-name'.
1543 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1544 (setq localname (replace-match "$" nil nil localname 1)))
1546 localname)))
1548 ;; Share names of a host are cached. It is very unlikely that the
1549 ;; shares do change during connection.
1550 (defun tramp-smb-get-file-entries (directory)
1551 "Read entries which match DIRECTORY.
1552 Either the shares are listed, or the `dir' command is executed.
1553 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1554 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1555 (setq localname (or localname "/"))
1556 (with-tramp-file-property v localname "file-entries"
1557 (with-current-buffer (tramp-get-connection-buffer v)
1558 (let* ((share (tramp-smb-get-share v))
1559 (cache (tramp-get-connection-property v "share-cache" nil))
1560 res entry)
1562 (if (and (not share) cache)
1563 ;; Return cached shares.
1564 (setq res cache)
1566 ;; Read entries.
1567 (if share
1568 (tramp-smb-send-command
1569 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1570 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1571 (tramp-smb-maybe-open-connection v))
1573 ;; Loop the listing.
1574 (goto-char (point-min))
1575 (if (re-search-forward tramp-smb-errors nil t)
1576 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1577 (while (not (eobp))
1578 (setq entry (tramp-smb-read-file-entry share))
1579 (forward-line)
1580 (when entry (push entry res))))
1582 ;; Cache share entries.
1583 (unless share
1584 (tramp-set-connection-property v "share-cache" res)))
1586 ;; Add directory itself.
1587 (push '("" "drwxrwxrwx" 0 (0 0)) res)
1589 ;; Return entries.
1590 (delq nil res))))))
1592 ;; Return either a share name (if SHARE is nil), or a file name.
1594 ;; If shares are listed, the following format is expected:
1596 ;; Disk| - leading spaces
1597 ;; [^|]+| - share name, 14 char
1598 ;; .* - comment
1600 ;; Entries provided by smbclient DIR aren't fully regular.
1601 ;; They should have the format
1603 ;; \s-\{2,2} - leading spaces
1604 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1605 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1606 ;; \s- - space delimiter
1607 ;; \s-+[0-9]+ - size, 8 chars, right bound
1608 ;; \s-\{2,2\} - space delimiter
1609 ;; \w\{3,3\} - weekday
1610 ;; \s- - space delimiter
1611 ;; \w\{3,3\} - month
1612 ;; \s- - space delimiter
1613 ;; [ 12][0-9] - day
1614 ;; \s- - space delimiter
1615 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1616 ;; \s- - space delimiter
1617 ;; [0-9]\{4,4\} - year
1619 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1620 ;; has function display_finfo:
1622 ;; d_printf(" %-30s%7.7s %8.0f %s",
1623 ;; finfo->name,
1624 ;; attrib_string(finfo->mode),
1625 ;; (double)finfo->size,
1626 ;; asctime(LocalTime(&t)));
1628 ;; in Samba 1.9, there's the following code:
1630 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1631 ;; CNV_LANG(finfo->name),
1632 ;; attrib_string(finfo->mode),
1633 ;; finfo->size,
1634 ;; asctime(LocalTime(&t))));
1636 ;; Problems:
1637 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1638 ;; available in older Emacsen.
1639 ;; * The length of constructs (file name, size) might exceed the default.
1640 ;; * File names might contain spaces.
1641 ;; * Permissions might be empty.
1643 ;; So we try to analyze backwards.
1644 (defun tramp-smb-read-file-entry (share)
1645 "Parse entry in SMB output buffer.
1646 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1647 Result is the list (LOCALNAME MODE SIZE MTIME)."
1648 ;; We are called from `tramp-smb-get-file-entries', which sets the
1649 ;; current buffer.
1650 (let ((line (buffer-substring (point) (point-at-eol)))
1651 localname mode size month day hour min sec year mtime)
1653 (if (not share)
1655 ;; Read share entries.
1656 (when (string-match "^Disk|\\([^|]+\\)|" line)
1657 (setq localname (match-string 1 line)
1658 mode "dr-xr-xr-x"
1659 size 0))
1661 ;; Real listing.
1662 (cl-block nil
1664 ;; year.
1665 (if (string-match "\\([0-9]+\\)$" line)
1666 (setq year (string-to-number (match-string 1 line))
1667 line (substring line 0 -5))
1668 (cl-return))
1670 ;; time.
1671 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1672 (setq hour (string-to-number (match-string 1 line))
1673 min (string-to-number (match-string 2 line))
1674 sec (string-to-number (match-string 3 line))
1675 line (substring line 0 -9))
1676 (cl-return))
1678 ;; day.
1679 (if (string-match "\\([0-9]+\\)$" line)
1680 (setq day (string-to-number (match-string 1 line))
1681 line (substring line 0 -3))
1682 (cl-return))
1684 ;; month.
1685 (if (string-match "\\(\\w+\\)$" line)
1686 (setq month (match-string 1 line)
1687 line (substring line 0 -4))
1688 (cl-return))
1690 ;; weekday.
1691 (if (string-match "\\(\\w+\\)$" line)
1692 (setq line (substring line 0 -5))
1693 (cl-return))
1695 ;; size.
1696 (if (string-match "\\([0-9]+\\)$" line)
1697 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1698 (setq size (string-to-number (match-string 1 line)))
1699 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1700 (setq length (+ length (match-end 0))))
1701 (setq line (substring line 0 length)))
1702 (cl-return))
1704 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1705 (if (string-match "\\([ADHRSV]+\\)?$" line)
1706 (setq
1707 mode (or (match-string 1 line) "")
1708 mode (save-match-data (format
1709 "%s%s"
1710 (if (string-match "D" mode) "d" "-")
1711 (mapconcat
1712 (lambda (_x) "") " "
1713 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1714 line (substring line 0 -6))
1715 (cl-return))
1717 ;; localname.
1718 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1719 (setq localname (match-string 1 line))
1720 (cl-return))))
1722 (when (and localname mode size)
1723 (setq mtime
1724 (if (and sec min hour day month year)
1725 (encode-time
1726 sec min hour day
1727 (cdr (assoc (downcase month) parse-time-months))
1728 year)
1729 '(0 0)))
1730 (list localname mode size mtime))))
1732 (defun tramp-smb-get-cifs-capabilities (vec)
1733 "Check, whether the SMB server supports POSIX commands."
1734 ;; When we are not logged in yet, we return nil.
1735 (if (process-live-p (tramp-get-connection-process vec))
1736 (with-tramp-connection-property
1737 (tramp-get-connection-process vec) "cifs-capabilities"
1738 (save-match-data
1739 (when (tramp-smb-send-command vec "posix")
1740 (with-current-buffer (tramp-get-connection-buffer vec)
1741 (goto-char (point-min))
1742 (when
1743 (re-search-forward "Server supports CIFS capabilities" nil t)
1744 (member
1745 "pathnames"
1746 (split-string
1747 (buffer-substring (point) (point-at-eol)) nil 'omit)))))))))
1749 (defun tramp-smb-get-stat-capability (vec)
1750 "Check, whether the SMB server supports the STAT command."
1751 ;; When we are not logged in yet, we return nil.
1752 (if (and (tramp-smb-get-share vec)
1753 (process-live-p (tramp-get-connection-process vec)))
1754 (with-tramp-connection-property
1755 (tramp-get-connection-process vec) "stat-capability"
1756 (tramp-smb-send-command vec "stat \"/\""))))
1759 ;; Connection functions.
1761 (defun tramp-smb-send-command (vec command)
1762 "Send the COMMAND to connection VEC.
1763 Returns nil if there has been an error message from smbclient."
1764 (tramp-smb-maybe-open-connection vec)
1765 (tramp-message vec 6 "%s" command)
1766 (tramp-send-string vec command)
1767 (tramp-smb-wait-for-output vec))
1769 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1770 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1771 Does not do anything if a connection is already open, but re-opens the
1772 connection if a previous connection has died for some reason.
1773 If ARGUMENT is non-nil, use it as argument for
1774 `tramp-smb-winexe-program', and suppress any checks."
1775 (let* ((share (tramp-smb-get-share vec))
1776 (buf (tramp-get-connection-buffer vec))
1777 (p (get-buffer-process buf)))
1779 ;; Check whether we still have the same smbclient version.
1780 ;; Otherwise, we must delete the connection cache, because
1781 ;; capabilities migh have changed.
1782 (unless (or argument (processp p))
1783 (let ((default-directory (tramp-compat-temporary-file-directory))
1784 (command (concat tramp-smb-program " -V")))
1786 (unless tramp-smb-version
1787 (unless (executable-find tramp-smb-program)
1788 (tramp-error
1789 vec 'file-error
1790 "Cannot find command %s in %s" tramp-smb-program exec-path))
1791 (setq tramp-smb-version (shell-command-to-string command))
1792 (tramp-message vec 6 command)
1793 (tramp-message vec 6 "\n%s" tramp-smb-version)
1794 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1795 (setq tramp-smb-version
1796 (replace-match "" nil nil tramp-smb-version))))
1798 (unless (string-equal
1799 tramp-smb-version
1800 (tramp-get-connection-property
1801 vec "smbclient-version" tramp-smb-version))
1802 (tramp-flush-directory-property vec "")
1803 (tramp-flush-connection-property vec))
1805 (tramp-set-connection-property
1806 vec "smbclient-version" tramp-smb-version)))
1808 ;; If too much time has passed since last command was sent, look
1809 ;; whether there has been an error message; maybe due to
1810 ;; connection timeout.
1811 (with-current-buffer buf
1812 (goto-char (point-min))
1813 (when (and (> (tramp-time-diff
1814 (current-time)
1815 (tramp-get-connection-property
1816 p "last-cmd-time" '(0 0 0)))
1818 (process-live-p p)
1819 (re-search-forward tramp-smb-errors nil t))
1820 (delete-process p)
1821 (setq p nil)))
1823 ;; Check whether it is still the same share.
1824 (unless (and (process-live-p p)
1825 (or argument
1826 (string-equal
1827 share
1828 (tramp-get-connection-property p "smb-share" ""))))
1830 (save-match-data
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 (domain (tramp-file-name-domain vec))
1838 (port (tramp-file-name-port vec))
1839 args)
1841 (cond
1842 (argument
1843 (setq args (list (concat "//" host))))
1844 (share
1845 (setq args (list (concat "//" host "/" share))))
1847 (setq args (list "-g" "-L" host ))))
1849 (if (not (zerop (length user)))
1850 (setq args (append args (list "-U" user)))
1851 (setq args (append args (list "-N"))))
1853 (when domain (setq args (append args (list "-W" domain))))
1854 (when port (setq args (append args (list "-p" port))))
1855 (when tramp-smb-conf
1856 (setq args (append args (list "-s" tramp-smb-conf))))
1857 (when argument
1858 (setq args (append args (list argument))))
1860 ;; OK, let's go.
1861 (with-tramp-progress-reporter
1862 vec 3
1863 (format "Opening connection for //%s%s/%s"
1864 (if (not (zerop (length user))) (concat user "@") "")
1865 host (or share ""))
1867 (let* ((coding-system-for-read nil)
1868 (process-connection-type tramp-process-connection-type)
1869 (p (let ((default-directory
1870 (tramp-compat-temporary-file-directory)))
1871 (apply #'start-process
1872 (tramp-get-connection-name vec)
1873 (tramp-get-connection-buffer vec)
1874 (if argument
1875 tramp-smb-winexe-program tramp-smb-program)
1876 args))))
1878 (tramp-message
1879 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1880 (tramp-set-connection-property p "vector" vec)
1881 (process-put p 'adjust-window-size-function 'ignore)
1882 (set-process-query-on-exit-flag p nil)
1884 ;; Set variables for computing the prompt for reading password.
1885 (setq tramp-current-method tramp-smb-method
1886 tramp-current-user user
1887 tramp-current-domain domain
1888 tramp-current-host host
1889 tramp-current-port port)
1891 (condition-case err
1892 (let (tramp-message-show-message)
1893 ;; Play login scenario.
1894 (tramp-process-actions
1895 p vec nil
1896 (if (or argument share)
1897 tramp-smb-actions-with-share
1898 tramp-smb-actions-without-share))
1900 ;; Check server version.
1901 (unless argument
1902 (with-current-buffer (tramp-get-connection-buffer vec)
1903 (goto-char (point-min))
1904 (search-forward-regexp tramp-smb-server-version nil t)
1905 (let ((smbserver-version (match-string 0)))
1906 (unless
1907 (string-equal
1908 smbserver-version
1909 (tramp-get-connection-property
1910 vec "smbserver-version" smbserver-version))
1911 (tramp-flush-directory-property vec "")
1912 (tramp-flush-connection-property vec))
1913 (tramp-set-connection-property
1914 vec "smbserver-version" smbserver-version))))
1916 ;; Set chunksize to 1. smbclient reads its input
1917 ;; character by character; if we send the string
1918 ;; at once, it is read painfully slow.
1919 (tramp-set-connection-property p "smb-share" share)
1920 (tramp-set-connection-property p "chunksize" 1)
1922 ;; Set connection-local variables.
1923 (tramp-set-connection-local-variables vec)
1925 ;; Mark it as connected.
1926 (tramp-set-connection-property p "connected" t))
1928 ;; Check for the error reason. If it was due to wrong
1929 ;; password, reestablish the connection. We cannot
1930 ;; handle this in `tramp-process-actions', because
1931 ;; smbclient does not ask for the password, again.
1932 (error
1933 (with-current-buffer (tramp-get-connection-buffer vec)
1934 (goto-char (point-min))
1935 (if (and (bound-and-true-p auth-sources)
1936 (search-forward-regexp
1937 tramp-smb-wrong-passwd-regexp nil t))
1938 ;; Disable `auth-source' and `password-cache'.
1939 (let (auth-sources)
1940 (tramp-message
1941 vec 3 "Retry connection with new password")
1942 (tramp-cleanup-connection vec t)
1943 (tramp-smb-maybe-open-connection vec argument))
1944 ;; Propagate the error.
1945 (signal (car err) (cdr err)))))))))))))
1947 ;; We don't use timeouts. If needed, the caller shall wrap around.
1948 (defun tramp-smb-wait-for-output (vec)
1949 "Wait for output from smbclient command.
1950 Returns nil if an error message has appeared."
1951 (with-current-buffer (tramp-get-connection-buffer vec)
1952 (let ((p (get-buffer-process (current-buffer)))
1953 (found (progn (goto-char (point-min))
1954 (re-search-forward tramp-smb-prompt nil t)))
1955 (err (progn (goto-char (point-min))
1956 (re-search-forward tramp-smb-errors nil t)))
1957 buffer-read-only)
1959 ;; Algorithm: get waiting output. See if last line contains
1960 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1961 ;; If not, wait a bit and again get waiting output.
1962 (while (and (not found) (not err) (process-live-p p))
1964 ;; Accept pending output.
1965 (tramp-accept-process-output p 0.1)
1967 ;; Search for prompt.
1968 (goto-char (point-min))
1969 (setq found (re-search-forward tramp-smb-prompt nil t))
1971 ;; Search for errors.
1972 (goto-char (point-min))
1973 (setq err (re-search-forward tramp-smb-errors nil t)))
1975 ;; When the process is still alive, read pending output.
1976 (while (and (not found) (process-live-p p))
1978 ;; Accept pending output.
1979 (tramp-accept-process-output p 0.1)
1981 ;; Search for prompt.
1982 (goto-char (point-min))
1983 (setq found (re-search-forward tramp-smb-prompt nil t)))
1985 (tramp-message vec 6 "\n%s" (buffer-string))
1987 ;; Remove prompt.
1988 (when found
1989 (goto-char (point-max))
1990 (re-search-backward tramp-smb-prompt nil t)
1991 (delete-region (point) (point-max)))
1993 ;; Return value is whether no error message has appeared.
1994 (not err))))
1996 (defun tramp-smb-kill-winexe-function ()
1997 "Send SIGKILL to the winexe process."
1998 (ignore-errors
1999 (let ((p (get-buffer-process (current-buffer))))
2000 (when (process-live-p p)
2001 (signal-process (process-id p) 'SIGINT)))))
2003 (defun tramp-smb-call-winexe (vec)
2004 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2006 ;; Check for program.
2007 (unless (executable-find tramp-smb-winexe-program)
2008 (tramp-error
2009 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
2011 ;; winexe does not supports ports.
2012 (when (tramp-file-name-port vec)
2013 (tramp-error vec 'file-error "Port not supported for remote processes"))
2015 (tramp-smb-maybe-open-connection
2017 (format
2018 "%s %s"
2019 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2021 (set (make-local-variable 'kill-buffer-hook)
2022 '(tramp-smb-kill-winexe-function))
2024 ;; Suppress "^M". Shouldn't we specify utf8?
2025 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2027 ;; Set width to 128. This avoids mixing prompt and long error messages.
2028 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2029 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2030 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2031 (tramp-smb-send-command vec "$bufsize.Width = 128")
2032 (tramp-smb-send-command vec "$winsize.Width = 128")
2033 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2034 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2036 (defun tramp-smb-shell-quote-argument (s)
2037 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2038 (let ((system-type 'ms-dos))
2039 (tramp-unquote-shell-quote-argument s)))
2041 (add-hook 'tramp-unload-hook
2042 (lambda ()
2043 (unload-feature 'tramp-smb 'force)))
2045 (provide 'tramp-smb)
2047 ;;; TODO:
2049 ;; * Return more comprehensive file permission string.
2051 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2052 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2054 ;; * Ignore case in file names.
2056 ;;; tramp-smb.el ends here