Introduce a defstruct `tramp-file-name' as central data structure.
[emacs.git] / lisp / net / tramp-smb.el
blob7b2a1ba874df8b71a176151830a630ad7c6ac9e2
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 ;; Pacify byte-compiler.
33 (eval-when-compile
34 (require 'cl))
36 ;; Define SMB method ...
37 ;;;###tramp-autoload
38 (defconst tramp-smb-method "smb"
39 "Method to connect SAMBA and M$ SMB servers.")
41 ;; ... and add it to the method list.
42 ;;;###tramp-autoload
43 (unless (memq system-type '(cygwin windows-nt))
44 (add-to-list 'tramp-methods
45 `(,tramp-smb-method
46 ;; We define an empty command, because `tramp-smb-call-winexe'
47 ;; opens already the powershell. Used in `tramp-handle-shell-command'.
48 (tramp-remote-shell "")
49 ;; This is just a guess. We don't know whether the share "C$"
50 ;; is available for public use, and whether the user has write
51 ;; access.
52 (tramp-tmpdir "/C$/Temp")
53 ;; Another guess. We might implement a better check later on.
54 (tramp-case-insensitive t))))
56 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
57 ;; the anonymous user is chosen.
58 ;;;###tramp-autoload
59 (add-to-list 'tramp-default-user-alist
60 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
62 ;; Add completion function for SMB method.
63 ;;;###tramp-autoload
64 (eval-after-load 'tramp
65 '(tramp-set-completion-function
66 tramp-smb-method
67 '((tramp-parse-netrc "~/.netrc"))))
69 ;;;###tramp-autoload
70 (defcustom tramp-smb-program "smbclient"
71 "Name of SMB client to run."
72 :group 'tramp
73 :type 'string
74 :require 'tramp)
76 ;;;###tramp-autoload
77 (defcustom tramp-smb-acl-program "smbcacls"
78 "Name of SMB acls to run."
79 :group 'tramp
80 :type 'string
81 :version "24.4"
82 :require 'tramp)
84 ;;;###tramp-autoload
85 (defcustom tramp-smb-conf "/dev/null"
86 "Path of the smb.conf file.
87 If it is nil, no smb.conf will be added to the `tramp-smb-program'
88 call, letting the SMB client use the default one."
89 :group 'tramp
90 :type '(choice (const nil) (file :must-match t))
91 :require 'tramp)
93 (defvar tramp-smb-version nil
94 "Version string of the SMB client.")
96 (defconst tramp-smb-server-version
97 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
98 "Regexp of SMB server identification.")
100 (defconst tramp-smb-prompt "^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
101 "Regexp used as prompt in smbclient or powershell.")
103 (defconst tramp-smb-wrong-passwd-regexp
104 (regexp-opt
105 '("NT_STATUS_LOGON_FAILURE"
106 "NT_STATUS_WRONG_PASSWORD"))
107 "Regexp for login error strings of SMB servers.")
109 (defconst tramp-smb-errors
110 (mapconcat
111 'identity
112 `(;; Connection error / timeout / unknown command.
113 "Connection\\( to \\S-+\\)? failed"
114 "Read from server failed, maybe it closed the connection"
115 "Call timed out: server did not respond"
116 "\\S-+: command not found"
117 "Server doesn't support UNIX CIFS calls"
118 ,(regexp-opt
119 '(;; Samba.
120 "ERRDOS"
121 "ERRHRD"
122 "ERRSRV"
123 "ERRbadfile"
124 "ERRbadpw"
125 "ERRfilexists"
126 "ERRnoaccess"
127 "ERRnomem"
128 "ERRnosuchshare"
129 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
130 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
131 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7),
132 ;; Windows 6.3 (Windows Server 2012, Windows 10).
133 "NT_STATUS_ACCESS_DENIED"
134 "NT_STATUS_ACCOUNT_LOCKED_OUT"
135 "NT_STATUS_BAD_NETWORK_NAME"
136 "NT_STATUS_CANNOT_DELETE"
137 "NT_STATUS_CONNECTION_REFUSED"
138 "NT_STATUS_DIRECTORY_NOT_EMPTY"
139 "NT_STATUS_DUPLICATE_NAME"
140 "NT_STATUS_FILE_IS_A_DIRECTORY"
141 "NT_STATUS_HOST_UNREACHABLE"
142 "NT_STATUS_IMAGE_ALREADY_LOADED"
143 "NT_STATUS_INVALID_LEVEL"
144 "NT_STATUS_IO_TIMEOUT"
145 "NT_STATUS_LOGON_FAILURE"
146 "NT_STATUS_NETWORK_ACCESS_DENIED"
147 "NT_STATUS_NOT_IMPLEMENTED"
148 "NT_STATUS_NO_LOGON_SERVERS"
149 "NT_STATUS_NO_SUCH_FILE"
150 "NT_STATUS_NO_SUCH_USER"
151 "NT_STATUS_OBJECT_NAME_COLLISION"
152 "NT_STATUS_OBJECT_NAME_INVALID"
153 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
154 "NT_STATUS_PASSWORD_MUST_CHANGE"
155 "NT_STATUS_SHARING_VIOLATION"
156 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
157 "NT_STATUS_UNSUCCESSFUL"
158 "NT_STATUS_WRONG_PASSWORD")))
159 "\\|")
160 "Regexp for possible error strings of SMB servers.
161 Used instead of analyzing error codes of commands.")
163 (defconst tramp-smb-actions-with-share
164 '((tramp-smb-prompt tramp-action-succeed)
165 (tramp-password-prompt-regexp tramp-action-password)
166 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
167 (tramp-smb-errors tramp-action-permission-denied)
168 (tramp-process-alive-regexp tramp-action-process-alive))
169 "List of pattern/action pairs.
170 This list is used for login to SMB servers.
172 See `tramp-actions-before-shell' for more info.")
174 (defconst tramp-smb-actions-without-share
175 '((tramp-password-prompt-regexp tramp-action-password)
176 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
177 (tramp-smb-errors tramp-action-permission-denied)
178 (tramp-process-alive-regexp tramp-action-out-of-band))
179 "List of pattern/action pairs.
180 This list is used for login to SMB servers.
182 See `tramp-actions-before-shell' for more info.")
184 (defconst tramp-smb-actions-with-tar
185 '((tramp-password-prompt-regexp tramp-action-password)
186 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
187 (tramp-smb-errors tramp-action-permission-denied)
188 (tramp-process-alive-regexp tramp-smb-action-with-tar))
189 "List of pattern/action pairs.
190 This list is used for tar-like copy of directories.
192 See `tramp-actions-before-shell' for more info.")
194 (defconst tramp-smb-actions-get-acl
195 '((tramp-password-prompt-regexp tramp-action-password)
196 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
197 (tramp-smb-errors tramp-action-permission-denied)
198 (tramp-process-alive-regexp tramp-smb-action-get-acl))
199 "List of pattern/action pairs.
200 This list is used for smbcacls actions.
202 See `tramp-actions-before-shell' for more info.")
204 (defconst tramp-smb-actions-set-acl
205 '((tramp-password-prompt-regexp tramp-action-password)
206 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
207 (tramp-smb-errors tramp-action-permission-denied)
208 (tramp-process-alive-regexp tramp-smb-action-set-acl))
209 "List of pattern/action pairs.
210 This list is used for smbcacls actions.
212 See `tramp-actions-before-shell' for more info.")
214 ;; New handlers should be added here.
215 ;;;###tramp-autoload
216 (defconst tramp-smb-file-name-handler-alist
217 '(;; `access-file' performed by default handler.
218 (add-name-to-file . tramp-smb-handle-add-name-to-file)
219 ;; `byte-compiler-base-file-name' performed by default handler.
220 (copy-directory . tramp-smb-handle-copy-directory)
221 (copy-file . tramp-smb-handle-copy-file)
222 (delete-directory . tramp-smb-handle-delete-directory)
223 (delete-file . tramp-smb-handle-delete-file)
224 ;; `diff-latest-backup-file' performed by default handler.
225 (directory-file-name . tramp-handle-directory-file-name)
226 (directory-files . tramp-smb-handle-directory-files)
227 (directory-files-and-attributes
228 . tramp-handle-directory-files-and-attributes)
229 (dired-compress-file . ignore)
230 (dired-uncache . tramp-handle-dired-uncache)
231 (expand-file-name . tramp-smb-handle-expand-file-name)
232 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
233 (file-acl . tramp-smb-handle-file-acl)
234 (file-attributes . tramp-smb-handle-file-attributes)
235 (file-directory-p . tramp-smb-handle-file-directory-p)
236 (file-file-equal-p . tramp-handle-file-equal-p)
237 (file-executable-p . tramp-handle-file-exists-p)
238 (file-exists-p . tramp-handle-file-exists-p)
239 (file-in-directory-p . tramp-handle-file-in-directory-p)
240 (file-local-copy . tramp-smb-handle-file-local-copy)
241 (file-modes . tramp-handle-file-modes)
242 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
243 (file-name-as-directory . tramp-handle-file-name-as-directory)
244 (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
245 (file-name-completion . tramp-handle-file-name-completion)
246 (file-name-directory . tramp-handle-file-name-directory)
247 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
248 ;; `file-name-sans-versions' performed by default handler.
249 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
250 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
251 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
252 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
253 (file-ownership-preserved-p . ignore)
254 (file-readable-p . tramp-handle-file-exists-p)
255 (file-regular-p . tramp-handle-file-regular-p)
256 (file-remote-p . tramp-handle-file-remote-p)
257 ;; `file-selinux-context' performed by default handler.
258 (file-symlink-p . tramp-handle-file-symlink-p)
259 ;; `file-truename' performed by default handler.
260 (file-writable-p . tramp-smb-handle-file-writable-p)
261 (find-backup-file-name . tramp-handle-find-backup-file-name)
262 ;; `find-file-noselect' performed by default handler.
263 ;; `get-file-buffer' performed by default handler.
264 (insert-directory . tramp-smb-handle-insert-directory)
265 (insert-file-contents . tramp-handle-insert-file-contents)
266 (load . tramp-handle-load)
267 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
268 (make-directory . tramp-smb-handle-make-directory)
269 (make-directory-internal . tramp-smb-handle-make-directory-internal)
270 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
271 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
272 (process-file . tramp-smb-handle-process-file)
273 (rename-file . tramp-smb-handle-rename-file)
274 (set-file-acl . tramp-smb-handle-set-file-acl)
275 (set-file-modes . tramp-smb-handle-set-file-modes)
276 (set-file-selinux-context . ignore)
277 (set-file-times . ignore)
278 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
279 (shell-command . tramp-handle-shell-command)
280 (start-file-process . tramp-smb-handle-start-file-process)
281 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
282 (temporary-file-directory . tramp-handle-temporary-file-directory)
283 (unhandled-file-name-directory . ignore)
284 (vc-registered . ignore)
285 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
286 (write-region . tramp-smb-handle-write-region))
287 "Alist of handler functions for Tramp SMB method.
288 Operations not mentioned here will be handled by the default Emacs primitives.")
290 ;; Options for remote processes via winexe.
291 ;;;###tramp-autoload
292 (defcustom tramp-smb-winexe-program "winexe"
293 "Name of winexe client to run.
294 If it isn't found in the local $PATH, the absolute path of winexe
295 shall be given. This is needed for remote processes."
296 :group 'tramp
297 :type 'string
298 :version "24.3"
299 :require 'tramp)
301 ;;;###tramp-autoload
302 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
303 "Shell to be used for processes on remote machines.
304 This must be Powershell V2 compatible."
305 :group 'tramp
306 :type 'string
307 :version "24.3"
308 :require 'tramp)
310 ;;;###tramp-autoload
311 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
312 "Command switch used together with `tramp-smb-winexe-shell-command'.
313 This can be used to disable echo etc."
314 :group 'tramp
315 :type 'string
316 :version "24.3"
317 :require 'tramp)
319 ;; It must be a `defsubst' in order to push the whole code into
320 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
321 ;;;###tramp-autoload
322 (defsubst tramp-smb-file-name-p (filename)
323 "Check if it's a filename for SMB servers."
324 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
325 tramp-smb-method))
327 ;;;###tramp-autoload
328 (defun tramp-smb-file-name-handler (operation &rest args)
329 "Invoke the SMB related OPERATION.
330 First arg specifies the OPERATION, second arg is a list of arguments to
331 pass to the OPERATION."
332 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
333 (if fn
334 (save-match-data (apply (cdr fn) args))
335 (tramp-run-real-handler operation args))))
337 ;;;###tramp-autoload
338 (unless (memq system-type '(cygwin windows-nt))
339 (tramp-register-foreign-file-name-handler
340 'tramp-smb-file-name-p 'tramp-smb-file-name-handler))
342 ;; File name primitives.
344 (defun tramp-smb-handle-add-name-to-file
345 (filename newname &optional ok-if-already-exists)
346 "Like `add-name-to-file' for Tramp files."
347 (unless (tramp-equal-remote filename newname)
348 (with-parsed-tramp-file-name
349 (if (tramp-tramp-file-p filename) filename newname) nil
350 (tramp-error
351 v 'file-error
352 "add-name-to-file: %s"
353 "only implemented for same method, same user, same host")))
354 (with-parsed-tramp-file-name filename v1
355 (with-parsed-tramp-file-name newname v2
356 (when (file-directory-p filename)
357 (tramp-error
358 v2 'file-error
359 "add-name-to-file: %s must not be a directory" filename))
360 (when (and (not ok-if-already-exists)
361 (file-exists-p newname)
362 (not (numberp ok-if-already-exists))
363 (y-or-n-p
364 (format
365 "File %s already exists; make it a new name anyway? "
366 newname)))
367 (tramp-error
368 v2 'file-error
369 "add-name-to-file: file %s already exists" newname))
370 ;; We must also flush the cache of the directory, because
371 ;; `file-attributes' reads the values from there.
372 (tramp-flush-file-property v2 (file-name-directory v2-localname))
373 (tramp-flush-file-property v2 v2-localname)
374 (unless
375 (tramp-smb-send-command
377 (format
378 "%s \"%s\" \"%s\""
379 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
380 (tramp-smb-get-localname v1)
381 (tramp-smb-get-localname v2)))
382 (tramp-error
383 v2 'file-error
384 "error with add-name-to-file, see buffer `%s' for details"
385 (buffer-name))))))
387 (defun tramp-smb-action-with-tar (proc vec)
388 "Untar from connection buffer."
389 (if (not (tramp-compat-process-live-p proc))
390 (throw 'tramp-action 'process-died)
392 (with-current-buffer (tramp-get-connection-buffer vec)
393 (goto-char (point-min))
394 (when (search-forward-regexp tramp-smb-server-version nil t)
395 ;; There might be a hidden password prompt.
396 (widen)
397 (forward-line)
398 (tramp-message vec 6 (buffer-substring (point-min) (point)))
399 (delete-region (point-min) (point))
400 (throw 'tramp-action 'ok)))))
402 (defun tramp-smb-handle-copy-directory
403 (dirname newname &optional keep-date parents copy-contents)
404 "Like `copy-directory' for Tramp files."
405 (if copy-contents
406 ;; We must do it file-wise.
407 (tramp-run-real-handler
408 'copy-directory (list dirname newname keep-date parents copy-contents))
410 (setq dirname (expand-file-name dirname)
411 newname (expand-file-name newname))
412 (let ((t1 (tramp-tramp-file-p dirname))
413 (t2 (tramp-tramp-file-p newname)))
414 (with-parsed-tramp-file-name (if t1 dirname newname) nil
415 (with-tramp-progress-reporter
416 v 0 (format "Copying %s to %s" dirname newname)
417 (cond
418 ;; We must use a local temporary directory.
419 ((and t1 t2)
420 (let ((tmpdir
421 (make-temp-name
422 (expand-file-name
423 tramp-temp-name-prefix
424 (tramp-compat-temporary-file-directory)))))
425 (unwind-protect
426 (progn
427 (make-directory tmpdir)
428 (copy-directory dirname tmpdir keep-date 'parents)
429 (copy-directory
430 (expand-file-name (file-name-nondirectory dirname) tmpdir)
431 newname keep-date parents))
432 (delete-directory tmpdir 'recursive))))
434 ;; We can copy recursively.
435 ((and (or t1 t2) (tramp-smb-get-cifs-capabilities v))
436 (when (and (file-directory-p newname)
437 (not (string-equal (file-name-nondirectory dirname)
438 (file-name-nondirectory newname))))
439 (setq newname
440 (expand-file-name
441 (file-name-nondirectory dirname) newname))
442 (if t2 (setq v (tramp-dissect-file-name newname))))
443 (if (not (file-directory-p newname))
444 (make-directory newname parents))
446 (setq tramp-current-method method
447 tramp-current-user user
448 tramp-current-host host)
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 (tramp-compat-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 (tramp-compat-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 (tramp-compat-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)
737 (setq tramp-current-method method
738 tramp-current-user user
739 tramp-current-host host)
741 (let* ((share (tramp-smb-get-share v))
742 (localname (replace-regexp-in-string
743 "\\\\" "/" (tramp-smb-get-localname v)))
744 (args (list (concat "//" host "/" share) "-E")))
746 (if (not (zerop (length user)))
747 (setq args (append args (list "-U" user)))
748 (setq args (append args (list "-N"))))
750 (when domain (setq args (append args (list "-W" domain))))
751 (when port (setq args (append args (list "-p" port))))
752 (when tramp-smb-conf
753 (setq args (append args (list "-s" tramp-smb-conf))))
754 (setq
755 args
756 (append args (list (tramp-unquote-shell-quote-argument localname)
757 "2>/dev/null")))
759 (unwind-protect
760 (with-temp-buffer
761 ;; Set the transfer process properties.
762 (tramp-set-connection-property
763 v "process-name" (buffer-name (current-buffer)))
764 (tramp-set-connection-property
765 v "process-buffer" (current-buffer))
767 ;; Use an asynchronous processes. By this, password
768 ;; can be handled.
769 (let ((p (apply
770 'start-process
771 (tramp-get-connection-name v)
772 (tramp-get-connection-buffer v)
773 tramp-smb-acl-program args)))
775 (tramp-message
776 v 6 "%s" (mapconcat 'identity (process-command p) " "))
777 (tramp-set-connection-property p "vector" v)
778 (process-put p 'adjust-window-size-function 'ignore)
779 (set-process-query-on-exit-flag p nil)
780 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
781 (when (> (point-max) (point-min))
782 (substring-no-properties (buffer-string)))))
784 ;; Reset the transfer process properties.
785 (tramp-set-connection-property v "process-name" nil)
786 (tramp-set-connection-property v "process-buffer" nil)))))))
788 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
789 "Like `file-attributes' for Tramp files."
790 (unless id-format (setq id-format 'integer))
791 (ignore-errors
792 (with-parsed-tramp-file-name filename nil
793 (with-tramp-file-property
794 v localname (format "file-attributes-%s" id-format)
795 (if (tramp-smb-get-stat-capability v)
796 (tramp-smb-do-file-attributes-with-stat v id-format)
797 ;; Reading just the filename entry via "dir localname" is not
798 ;; possible, because when filename is a directory, some
799 ;; smbclient versions return the content of the directory, and
800 ;; other versions don't. Therefore, the whole content of the
801 ;; upper directory is retrieved, and the entry of the filename
802 ;; is extracted from.
803 (let* ((entries (tramp-smb-get-file-entries
804 (file-name-directory filename)))
805 (entry (assoc (file-name-nondirectory filename) entries))
806 (uid (if (equal id-format 'string) "nobody" -1))
807 (gid (if (equal id-format 'string) "nogroup" -1))
808 (inode (tramp-get-inode v))
809 (device (tramp-get-device v)))
811 ;; Check result.
812 (when entry
813 (list (and (string-match "d" (nth 1 entry))
814 t) ;0 file type
815 -1 ;1 link count
816 uid ;2 uid
817 gid ;3 gid
818 '(0 0) ;4 atime
819 (nth 3 entry) ;5 mtime
820 '(0 0) ;6 ctime
821 (nth 2 entry) ;7 size
822 (nth 1 entry) ;8 mode
823 nil ;9 gid weird
824 inode ;10 inode number
825 device)))))))) ;11 file system number
827 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
828 "Implement `file-attributes' for Tramp files using stat command."
829 (tramp-message
830 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
831 (with-current-buffer (tramp-get-connection-buffer vec)
832 (let* (size id link uid gid atime mtime ctime mode inode)
833 (when (tramp-smb-send-command
834 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
836 ;; Loop the listing.
837 (goto-char (point-min))
838 (unless (re-search-forward tramp-smb-errors nil t)
839 (while (not (eobp))
840 (cond
841 ((looking-at
842 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
843 (setq size (string-to-number (match-string 1))
844 id (if (string-equal "directory" (match-string 2)) t
845 (if (string-equal "symbolic" (match-string 2)) ""))))
846 ((looking-at
847 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
848 (setq inode (string-to-number (match-string 1))
849 link (string-to-number (match-string 2))))
850 ((looking-at
851 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
852 (setq mode (match-string 1)
853 uid (if (equal id-format 'string) (match-string 2)
854 (string-to-number (match-string 2)))
855 gid (if (equal id-format 'string) (match-string 3)
856 (string-to-number (match-string 3)))))
857 ((looking-at
858 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
859 (setq atime
860 (encode-time
861 (string-to-number (match-string 6)) ;; sec
862 (string-to-number (match-string 5)) ;; min
863 (string-to-number (match-string 4)) ;; hour
864 (string-to-number (match-string 3)) ;; day
865 (string-to-number (match-string 2)) ;; month
866 (string-to-number (match-string 1))))) ;; year
867 ((looking-at
868 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
869 (setq mtime
870 (encode-time
871 (string-to-number (match-string 6)) ;; sec
872 (string-to-number (match-string 5)) ;; min
873 (string-to-number (match-string 4)) ;; hour
874 (string-to-number (match-string 3)) ;; day
875 (string-to-number (match-string 2)) ;; month
876 (string-to-number (match-string 1))))) ;; year
877 ((looking-at
878 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
879 (setq ctime
880 (encode-time
881 (string-to-number (match-string 6)) ;; sec
882 (string-to-number (match-string 5)) ;; min
883 (string-to-number (match-string 4)) ;; hour
884 (string-to-number (match-string 3)) ;; day
885 (string-to-number (match-string 2)) ;; month
886 (string-to-number (match-string 1)))))) ;; year
887 (forward-line))
888 ;; Return the result.
889 (list id link uid gid atime mtime ctime size mode nil inode
890 (tramp-get-device vec)))))))
892 (defun tramp-smb-handle-file-directory-p (filename)
893 "Like `file-directory-p' for Tramp files."
894 (and (file-exists-p filename)
895 (eq ?d
896 (aref (tramp-compat-file-attribute-modes (file-attributes filename))
897 0))))
899 (defun tramp-smb-handle-file-local-copy (filename)
900 "Like `file-local-copy' for Tramp files."
901 (with-parsed-tramp-file-name filename nil
902 (unless (file-exists-p filename)
903 (tramp-error
904 v tramp-file-missing
905 "Cannot make local copy of non-existing file `%s'" filename))
906 (let ((tmpfile (tramp-compat-make-temp-file filename)))
907 (with-tramp-progress-reporter
908 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
909 (unless (tramp-smb-send-command
910 v (format "get \"%s\" \"%s\""
911 (tramp-smb-get-localname v) tmpfile))
912 ;; Oops, an error. We shall cleanup.
913 (delete-file tmpfile)
914 (tramp-error
915 v 'file-error "Cannot make local copy of file `%s'" filename)))
916 tmpfile)))
918 ;; This function should return "foo/" for directories and "bar" for
919 ;; files.
920 (defun tramp-smb-handle-file-name-all-completions (filename directory)
921 "Like `file-name-all-completions' for Tramp files."
922 (all-completions
923 filename
924 (with-parsed-tramp-file-name (expand-file-name directory) nil
925 (with-tramp-file-property v localname "file-name-all-completions"
926 (save-match-data
927 (delete-dups
928 (mapcar
929 (lambda (x)
930 (list
931 (if (string-match "d" (nth 1 x))
932 (file-name-as-directory (nth 0 x))
933 (nth 0 x))))
934 (tramp-smb-get-file-entries directory))))))))
936 (defun tramp-smb-handle-file-writable-p (filename)
937 "Like `file-writable-p' for Tramp files."
938 (if (file-exists-p filename)
939 (string-match
941 (or (tramp-compat-file-attribute-modes (file-attributes filename)) ""))
942 (let ((dir (file-name-directory filename)))
943 (and (file-exists-p dir)
944 (file-writable-p dir)))))
946 (defun tramp-smb-handle-insert-directory
947 (filename switches &optional wildcard full-directory-p)
948 "Like `insert-directory' for Tramp files."
949 (setq filename (expand-file-name filename))
950 (unless switches (setq switches ""))
951 ;; Mark trailing "/".
952 (when (and (zerop (length (file-name-nondirectory filename)))
953 (not full-directory-p))
954 (setq switches (concat switches "F")))
955 (if full-directory-p
956 ;; Called from `dired-add-entry'.
957 (setq filename (file-name-as-directory filename))
958 (setq filename (directory-file-name filename)))
959 (with-parsed-tramp-file-name filename nil
960 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
961 (save-match-data
962 (let ((base (file-name-nondirectory filename))
963 ;; We should not destroy the cache entry.
964 (entries (copy-sequence
965 (tramp-smb-get-file-entries
966 (file-name-directory filename)))))
968 (when wildcard
969 (string-match "\\." base)
970 (setq base (replace-match "\\\\." nil nil base))
971 (string-match "\\*" base)
972 (setq base (replace-match ".*" nil nil base))
973 (string-match "\\?" base)
974 (setq base (replace-match ".?" nil nil base)))
976 ;; Filter entries.
977 (setq entries
978 (delq
980 (if (or wildcard (zerop (length base)))
981 ;; Check for matching entries.
982 (mapcar
983 (lambda (x)
984 (when (string-match
985 (format "^%s" base) (nth 0 x))
987 entries)
988 ;; We just need the only and only entry FILENAME.
989 (list (assoc base entries)))))
991 ;; Sort entries.
992 (setq entries
993 (sort
994 entries
995 (lambda (x y)
996 (if (string-match "t" switches)
997 ;; Sort by date.
998 (time-less-p (nth 3 y) (nth 3 x))
999 ;; Sort by name.
1000 (string-lessp (nth 0 x) (nth 0 y))))))
1002 ;; Handle "-F" switch.
1003 (when (string-match "F" switches)
1004 (mapc
1005 (lambda (x)
1006 (when (not (zerop (length (car x))))
1007 (cond
1008 ((char-equal ?d (string-to-char (nth 1 x)))
1009 (setcar x (concat (car x) "/")))
1010 ((char-equal ?x (string-to-char (nth 1 x)))
1011 (setcar x (concat (car x) "*"))))))
1012 entries))
1014 ;; Print entries.
1015 (mapc
1016 (lambda (x)
1017 (when (not (zerop (length (nth 0 x))))
1018 (when (string-match "l" switches)
1019 (let ((attr
1020 (when (tramp-smb-get-stat-capability v)
1021 (ignore-errors
1022 (file-attributes filename 'string)))))
1023 (insert
1024 (format
1025 "%10s %3d %-8s %-8s %8s %s "
1026 (or (tramp-compat-file-attribute-modes attr) (nth 1 x))
1027 (or (tramp-compat-file-attribute-link-number attr) 1)
1028 (or (tramp-compat-file-attribute-user-id attr) "nobody")
1029 (or (tramp-compat-file-attribute-group-id attr) "nogroup")
1030 (or (tramp-compat-file-attribute-size attr) (nth 2 x))
1031 (format-time-string
1032 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1033 tramp-half-a-year)
1034 "%b %e %R"
1035 "%b %e %Y")
1036 (nth 3 x)))))) ; date
1038 ;; We mark the file name. The inserted name could be
1039 ;; from somewhere else, so we use the relative file name
1040 ;; of `default-directory'.
1041 (let ((start (point)))
1042 (insert
1043 (format
1044 "%s\n"
1045 (file-relative-name
1046 (expand-file-name
1047 (nth 0 x) (file-name-directory filename))
1048 (when full-directory-p (file-name-directory filename)))))
1049 (put-text-property start (1- (point)) 'dired-filename t))
1050 (forward-line)
1051 (beginning-of-line)))
1052 entries))))))
1054 (defun tramp-smb-handle-make-directory (dir &optional parents)
1055 "Like `make-directory' for Tramp files."
1056 (setq dir (directory-file-name (expand-file-name dir)))
1057 (unless (file-name-absolute-p dir)
1058 (setq dir (expand-file-name dir default-directory)))
1059 (with-parsed-tramp-file-name dir nil
1060 (save-match-data
1061 (let* ((ldir (file-name-directory dir)))
1062 ;; Make missing directory parts.
1063 (when (and parents
1064 (tramp-smb-get-share v)
1065 (not (file-directory-p ldir)))
1066 (make-directory ldir parents))
1067 ;; Just do it.
1068 (when (file-directory-p ldir)
1069 (make-directory-internal dir))
1070 (unless (file-directory-p dir)
1071 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1073 (defun tramp-smb-handle-make-directory-internal (directory)
1074 "Like `make-directory-internal' for Tramp files."
1075 (setq directory (directory-file-name (expand-file-name directory)))
1076 (unless (file-name-absolute-p directory)
1077 (setq directory (expand-file-name directory default-directory)))
1078 (with-parsed-tramp-file-name directory nil
1079 (save-match-data
1080 (let* ((file (tramp-smb-get-localname v)))
1081 (when (file-directory-p (file-name-directory directory))
1082 (tramp-smb-send-command
1084 (if (tramp-smb-get-cifs-capabilities v)
1085 (format "posix_mkdir \"%s\" %o" file (default-file-modes))
1086 (format "mkdir \"%s\"" file)))
1087 ;; We must also flush the cache of the directory, because
1088 ;; `file-attributes' reads the values from there.
1089 (tramp-flush-file-property v (file-name-directory localname))
1090 (tramp-flush-file-property v localname))
1091 (unless (file-directory-p directory)
1092 (tramp-error
1093 v 'file-error "Couldn't make directory %s" directory))))))
1095 (defun tramp-smb-handle-make-symbolic-link
1096 (filename linkname &optional ok-if-already-exists)
1097 "Like `make-symbolic-link' for Tramp files.
1098 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1099 the symlink. If LINKNAME is a Tramp file, only the localname component is
1100 used as the target of the symlink.
1102 If LINKNAME is a Tramp file and the localname component is relative, then
1103 it is expanded first, before the localname component is taken. Note that
1104 this can give surprising results if the user/host for the source and
1105 target of the symlink differ."
1106 (unless (tramp-equal-remote filename linkname)
1107 (with-parsed-tramp-file-name
1108 (if (tramp-tramp-file-p filename) filename linkname) nil
1109 (tramp-error
1110 v 'file-error
1111 "make-symbolic-link: %s"
1112 "only implemented for same method, same user, same host")))
1113 (with-parsed-tramp-file-name filename v1
1114 (with-parsed-tramp-file-name linkname v2
1115 (when (file-directory-p filename)
1116 (tramp-error
1117 v2 'file-error
1118 "make-symbolic-link: %s must not be a directory" filename))
1119 (when (and (not ok-if-already-exists)
1120 (file-exists-p linkname)
1121 (not (numberp ok-if-already-exists))
1122 (y-or-n-p
1123 (format
1124 "File %s already exists; make it a new name anyway? "
1125 linkname)))
1126 (tramp-error
1127 v2 'file-already-exists
1128 "make-symbolic-link: file %s 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 (tramp-compat-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 (tramp-compat-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 (setq tramp-current-method method
1328 tramp-current-user user
1329 tramp-current-host host)
1330 (tramp-set-file-property v localname "file-acl" 'undef)
1332 (let* ((share (tramp-smb-get-share v))
1333 (localname (replace-regexp-in-string
1334 "\\\\" "/" (tramp-smb-get-localname v)))
1335 (args (list (concat "//" host "/" share) "-E" "-S"
1336 (replace-regexp-in-string
1337 "\n" "," acl-string))))
1339 (if (not (zerop (length user)))
1340 (setq args (append args (list "-U" user)))
1341 (setq args (append args (list "-N"))))
1343 (when domain (setq args (append args (list "-W" domain))))
1344 (when port (setq args (append args (list "-p" port))))
1345 (when tramp-smb-conf
1346 (setq args (append args (list "-s" tramp-smb-conf))))
1347 (setq
1348 args
1349 (append args (list (tramp-unquote-shell-quote-argument localname)
1350 "&&" "echo" "tramp_exit_status" "0"
1351 "||" "echo" "tramp_exit_status" "1")))
1353 (unwind-protect
1354 (with-temp-buffer
1355 ;; Set the transfer process properties.
1356 (tramp-set-connection-property
1357 v "process-name" (buffer-name (current-buffer)))
1358 (tramp-set-connection-property
1359 v "process-buffer" (current-buffer))
1361 ;; Use an asynchronous processes. By this, password can
1362 ;; be handled.
1363 (let ((p (apply
1364 'start-process
1365 (tramp-get-connection-name v)
1366 (tramp-get-connection-buffer v)
1367 tramp-smb-acl-program args)))
1369 (tramp-message
1370 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1371 (tramp-set-connection-property p "vector" v)
1372 (process-put p 'adjust-window-size-function 'ignore)
1373 (set-process-query-on-exit-flag p nil)
1374 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1375 (goto-char (point-max))
1376 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1377 (tramp-error
1378 v 'file-error
1379 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1380 (skip-chars-forward "^ ")
1381 (when (zerop (read (current-buffer)))
1382 ;; Success.
1383 (tramp-set-file-property v localname "file-acl" acl-string)
1384 t)))
1386 ;; Reset the transfer process properties.
1387 (tramp-set-connection-property v "process-name" nil)
1388 (tramp-set-connection-property v "process-buffer" nil)))))))
1390 (defun tramp-smb-handle-set-file-modes (filename mode)
1391 "Like `set-file-modes' for Tramp files."
1392 (with-parsed-tramp-file-name filename nil
1393 (when (tramp-smb-get-cifs-capabilities v)
1394 (tramp-flush-file-property v localname)
1395 (unless (tramp-smb-send-command
1396 v (format "chmod \"%s\" %o" (tramp-smb-get-localname v) mode))
1397 (tramp-error
1398 v 'file-error "Error while changing file's mode %s" filename)))))
1400 ;; We use BUFFER also as connection buffer during setup. Because of
1401 ;; this, its original contents must be saved, and restored once
1402 ;; connection has been setup.
1403 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1404 "Like `start-file-process' for Tramp files."
1405 (with-parsed-tramp-file-name default-directory nil
1406 (let* ((buffer
1407 (if buffer
1408 (get-buffer-create buffer)
1409 ;; BUFFER can be nil. We use a temporary buffer.
1410 (generate-new-buffer tramp-temp-buffer-name)))
1411 (command (mapconcat 'identity (cons program args) " "))
1412 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1413 (name1 name)
1414 (i 0))
1415 (unwind-protect
1416 (save-excursion
1417 (save-restriction
1418 (while (get-process name1)
1419 ;; NAME must be unique as process name.
1420 (setq i (1+ i)
1421 name1 (format "%s<%d>" name i)))
1422 ;; Set the new process properties.
1423 (tramp-set-connection-property v "process-name" name1)
1424 (tramp-set-connection-property v "process-buffer" buffer)
1425 ;; Activate narrowing in order to save BUFFER contents.
1426 (with-current-buffer (tramp-get-connection-buffer v)
1427 (let ((buffer-undo-list t))
1428 (narrow-to-region (point-max) (point-max))
1429 (tramp-smb-call-winexe v)
1430 (when (tramp-smb-get-share v)
1431 (tramp-smb-send-command
1432 v (format
1433 "cd \"//%s%s\""
1434 host (file-name-directory localname))))
1435 (tramp-message v 6 "(%s); exit" command)
1436 (tramp-send-string v command)))
1437 ;; Return value.
1438 (tramp-get-connection-process v)))
1440 ;; Save exit.
1441 (with-current-buffer (tramp-get-connection-buffer v)
1442 (if (string-match tramp-temp-buffer-name (buffer-name))
1443 (progn
1444 (set-process-buffer (tramp-get-connection-process v) nil)
1445 (kill-buffer (current-buffer)))
1446 (set-buffer-modified-p bmp)))
1447 (tramp-set-connection-property v "process-name" nil)
1448 (tramp-set-connection-property v "process-buffer" nil)))))
1450 (defun tramp-smb-handle-substitute-in-file-name (filename)
1451 "Like `handle-substitute-in-file-name' for Tramp files.
1452 \"//\" substitutes only in the local filename part. Catches
1453 errors for shares like \"C$/\", which are common in Microsoft Windows."
1454 ;; Check, whether the local part is a quoted file name.
1455 (if (tramp-compat-file-name-quoted-p filename)
1456 filename
1457 (with-parsed-tramp-file-name filename nil
1458 ;; Ignore in LOCALNAME everything before "//".
1459 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1460 (setq filename
1461 (concat (file-remote-p filename)
1462 (replace-match "\\1" nil nil localname)))))
1463 (condition-case nil
1464 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1465 (error filename))))
1467 (defun tramp-smb-handle-write-region
1468 (start end filename &optional append visit lockname confirm)
1469 "Like `write-region' for Tramp files."
1470 (setq filename (expand-file-name filename))
1471 (with-parsed-tramp-file-name filename nil
1472 (when (and confirm (file-exists-p filename))
1473 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1474 filename))
1475 (tramp-error v 'file-error "File not overwritten")))
1476 ;; We must also flush the cache of the directory, because
1477 ;; `file-attributes' reads the values from there.
1478 (tramp-flush-file-property v (file-name-directory localname))
1479 (tramp-flush-file-property v localname)
1480 (let ((curbuf (current-buffer))
1481 (tmpfile (tramp-compat-make-temp-file filename)))
1482 (when (and append (file-exists-p filename))
1483 (copy-file filename tmpfile 'ok))
1484 ;; We say `no-message' here because we don't want the visited file
1485 ;; modtime data to be clobbered from the temp file. We call
1486 ;; `set-visited-file-modtime' ourselves later on.
1487 (tramp-run-real-handler
1488 'write-region
1489 (if confirm ; don't pass this arg unless defined for backward compat.
1490 (list start end tmpfile append 'no-message lockname confirm)
1491 (list start end tmpfile append 'no-message lockname)))
1493 (with-tramp-progress-reporter
1494 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1495 (unwind-protect
1496 (unless (tramp-smb-send-command
1497 v (format "put %s \"%s\""
1498 tmpfile (tramp-smb-get-localname v)))
1499 (tramp-error v 'file-error "Cannot write `%s'" filename))
1500 (delete-file tmpfile)))
1502 (unless (equal curbuf (current-buffer))
1503 (tramp-error
1504 v 'file-error
1505 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1506 (when (eq visit t)
1507 (set-visited-file-modtime)))))
1510 ;; Internal file name functions.
1512 (defun tramp-smb-get-share (vec)
1513 "Returns the share name of LOCALNAME."
1514 (save-match-data
1515 (let ((localname (tramp-file-name-unquote-localname vec)))
1516 (when (string-match "^/?\\([^/]+\\)/" localname)
1517 (match-string 1 localname)))))
1519 (defun tramp-smb-get-localname (vec)
1520 "Returns the file name of LOCALNAME.
1521 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1522 (save-match-data
1523 (let ((localname (tramp-file-name-unquote-localname vec)))
1524 (setq
1525 localname
1526 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1527 ;; There is a share, separated by "/".
1528 (if (not (tramp-smb-get-cifs-capabilities vec))
1529 (mapconcat
1530 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1531 (match-string 1 localname) "")
1532 (match-string 1 localname))
1533 ;; There is just a share.
1534 (if (string-match "^/?\\([^/]+\\)$" localname)
1535 (match-string 1 localname)
1536 "")))
1538 ;; Sometimes we have discarded `substitute-in-file-name'.
1539 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1540 (setq localname (replace-match "$" nil nil localname 1)))
1542 localname)))
1544 ;; Share names of a host are cached. It is very unlikely that the
1545 ;; shares do change during connection.
1546 (defun tramp-smb-get-file-entries (directory)
1547 "Read entries which match DIRECTORY.
1548 Either the shares are listed, or the `dir' command is executed.
1549 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1550 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1551 (setq localname (or localname "/"))
1552 (with-tramp-file-property v localname "file-entries"
1553 (with-current-buffer (tramp-get-connection-buffer v)
1554 (let* ((share (tramp-smb-get-share v))
1555 (cache (tramp-get-connection-property v "share-cache" nil))
1556 res entry)
1558 (if (and (not share) cache)
1559 ;; Return cached shares.
1560 (setq res cache)
1562 ;; Read entries.
1563 (if share
1564 (tramp-smb-send-command
1565 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1566 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1567 (tramp-smb-maybe-open-connection v))
1569 ;; Loop the listing.
1570 (goto-char (point-min))
1571 (if (re-search-forward tramp-smb-errors nil t)
1572 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1573 (while (not (eobp))
1574 (setq entry (tramp-smb-read-file-entry share))
1575 (forward-line)
1576 (when entry (push entry res))))
1578 ;; Cache share entries.
1579 (unless share
1580 (tramp-set-connection-property v "share-cache" res)))
1582 ;; Add directory itself.
1583 (push '("" "drwxrwxrwx" 0 (0 0)) res)
1585 ;; Return entries.
1586 (delq nil res))))))
1588 ;; Return either a share name (if SHARE is nil), or a file name.
1590 ;; If shares are listed, the following format is expected:
1592 ;; Disk| - leading spaces
1593 ;; [^|]+| - share name, 14 char
1594 ;; .* - comment
1596 ;; Entries provided by smbclient DIR aren't fully regular.
1597 ;; They should have the format
1599 ;; \s-\{2,2} - leading spaces
1600 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1601 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1602 ;; \s- - space delimiter
1603 ;; \s-+[0-9]+ - size, 8 chars, right bound
1604 ;; \s-\{2,2\} - space delimiter
1605 ;; \w\{3,3\} - weekday
1606 ;; \s- - space delimiter
1607 ;; \w\{3,3\} - month
1608 ;; \s- - space delimiter
1609 ;; [ 12][0-9] - day
1610 ;; \s- - space delimiter
1611 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1612 ;; \s- - space delimiter
1613 ;; [0-9]\{4,4\} - year
1615 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1616 ;; has function display_finfo:
1618 ;; d_printf(" %-30s%7.7s %8.0f %s",
1619 ;; finfo->name,
1620 ;; attrib_string(finfo->mode),
1621 ;; (double)finfo->size,
1622 ;; asctime(LocalTime(&t)));
1624 ;; in Samba 1.9, there's the following code:
1626 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1627 ;; CNV_LANG(finfo->name),
1628 ;; attrib_string(finfo->mode),
1629 ;; finfo->size,
1630 ;; asctime(LocalTime(&t))));
1632 ;; Problems:
1633 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1634 ;; available in older Emacsen.
1635 ;; * The length of constructs (file name, size) might exceed the default.
1636 ;; * File names might contain spaces.
1637 ;; * Permissions might be empty.
1639 ;; So we try to analyze backwards.
1640 (defun tramp-smb-read-file-entry (share)
1641 "Parse entry in SMB output buffer.
1642 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1643 Result is the list (LOCALNAME MODE SIZE MTIME)."
1644 ;; We are called from `tramp-smb-get-file-entries', which sets the
1645 ;; current buffer.
1646 (let ((line (buffer-substring (point) (point-at-eol)))
1647 localname mode size month day hour min sec year mtime)
1649 (if (not share)
1651 ;; Read share entries.
1652 (when (string-match "^Disk|\\([^|]+\\)|" line)
1653 (setq localname (match-string 1 line)
1654 mode "dr-xr-xr-x"
1655 size 0))
1657 ;; Real listing.
1658 (block nil
1660 ;; year.
1661 (if (string-match "\\([0-9]+\\)$" line)
1662 (setq year (string-to-number (match-string 1 line))
1663 line (substring line 0 -5))
1664 (return))
1666 ;; time.
1667 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1668 (setq hour (string-to-number (match-string 1 line))
1669 min (string-to-number (match-string 2 line))
1670 sec (string-to-number (match-string 3 line))
1671 line (substring line 0 -9))
1672 (return))
1674 ;; day.
1675 (if (string-match "\\([0-9]+\\)$" line)
1676 (setq day (string-to-number (match-string 1 line))
1677 line (substring line 0 -3))
1678 (return))
1680 ;; month.
1681 (if (string-match "\\(\\w+\\)$" line)
1682 (setq month (match-string 1 line)
1683 line (substring line 0 -4))
1684 (return))
1686 ;; weekday.
1687 (if (string-match "\\(\\w+\\)$" line)
1688 (setq line (substring line 0 -5))
1689 (return))
1691 ;; size.
1692 (if (string-match "\\([0-9]+\\)$" line)
1693 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1694 (setq size (string-to-number (match-string 1 line)))
1695 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1696 (setq length (+ length (match-end 0))))
1697 (setq line (substring line 0 length)))
1698 (return))
1700 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1701 (if (string-match "\\([ADHRSV]+\\)?$" line)
1702 (setq
1703 mode (or (match-string 1 line) "")
1704 mode (save-match-data (format
1705 "%s%s"
1706 (if (string-match "D" mode) "d" "-")
1707 (mapconcat
1708 (lambda (_x) "") " "
1709 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1710 line (substring line 0 -6))
1711 (return))
1713 ;; localname.
1714 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1715 (setq localname (match-string 1 line))
1716 (return))))
1718 (when (and localname mode size)
1719 (setq mtime
1720 (if (and sec min hour day month year)
1721 (encode-time
1722 sec min hour day
1723 (cdr (assoc (downcase month) parse-time-months))
1724 year)
1725 '(0 0)))
1726 (list localname mode size mtime))))
1728 (defun tramp-smb-get-cifs-capabilities (vec)
1729 "Check, whether the SMB server supports POSIX commands."
1730 ;; When we are not logged in yet, we return nil.
1731 (if (tramp-compat-process-live-p (tramp-get-connection-process vec))
1732 (with-tramp-connection-property
1733 (tramp-get-connection-process vec) "cifs-capabilities"
1734 (save-match-data
1735 (when (tramp-smb-send-command vec "posix")
1736 (with-current-buffer (tramp-get-connection-buffer vec)
1737 (goto-char (point-min))
1738 (when
1739 (re-search-forward "Server supports CIFS capabilities" nil t)
1740 (member
1741 "pathnames"
1742 (split-string
1743 (buffer-substring (point) (point-at-eol)) nil 'omit)))))))))
1745 (defun tramp-smb-get-stat-capability (vec)
1746 "Check, whether the SMB server supports the STAT command."
1747 ;; When we are not logged in yet, we return nil.
1748 (if (and (tramp-smb-get-share vec)
1749 (tramp-compat-process-live-p (tramp-get-connection-process vec)))
1750 (with-tramp-connection-property
1751 (tramp-get-connection-process vec) "stat-capability"
1752 (tramp-smb-send-command vec "stat \"/\""))))
1755 ;; Connection functions.
1757 (defun tramp-smb-send-command (vec command)
1758 "Send the COMMAND to connection VEC.
1759 Returns nil if there has been an error message from smbclient."
1760 (tramp-smb-maybe-open-connection vec)
1761 (tramp-message vec 6 "%s" command)
1762 (tramp-send-string vec command)
1763 (tramp-smb-wait-for-output vec))
1765 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1766 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1767 Does not do anything if a connection is already open, but re-opens the
1768 connection if a previous connection has died for some reason.
1769 If ARGUMENT is non-nil, use it as argument for
1770 `tramp-smb-winexe-program', and suppress any checks."
1771 (let* ((share (tramp-smb-get-share vec))
1772 (buf (tramp-get-connection-buffer vec))
1773 (p (get-buffer-process buf)))
1775 ;; Check whether we still have the same smbclient version.
1776 ;; Otherwise, we must delete the connection cache, because
1777 ;; capabilities migh have changed.
1778 (unless (or argument (processp p))
1779 (let ((default-directory (tramp-compat-temporary-file-directory))
1780 (command (concat tramp-smb-program " -V")))
1782 (unless tramp-smb-version
1783 (unless (executable-find tramp-smb-program)
1784 (tramp-error
1785 vec 'file-error
1786 "Cannot find command %s in %s" tramp-smb-program exec-path))
1787 (setq tramp-smb-version (shell-command-to-string command))
1788 (tramp-message vec 6 command)
1789 (tramp-message vec 6 "\n%s" tramp-smb-version)
1790 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1791 (setq tramp-smb-version
1792 (replace-match "" nil nil tramp-smb-version))))
1794 (unless (string-equal
1795 tramp-smb-version
1796 (tramp-get-connection-property
1797 vec "smbclient-version" tramp-smb-version))
1798 (tramp-flush-directory-property vec "")
1799 (tramp-flush-connection-property vec))
1801 (tramp-set-connection-property
1802 vec "smbclient-version" tramp-smb-version)))
1804 ;; If too much time has passed since last command was sent, look
1805 ;; whether there has been an error message; maybe due to
1806 ;; connection timeout.
1807 (with-current-buffer buf
1808 (goto-char (point-min))
1809 (when (and (> (tramp-time-diff
1810 (current-time)
1811 (tramp-get-connection-property
1812 p "last-cmd-time" '(0 0 0)))
1814 (tramp-compat-process-live-p p)
1815 (re-search-forward tramp-smb-errors nil t))
1816 (delete-process p)
1817 (setq p nil)))
1819 ;; Check whether it is still the same share.
1820 (unless (and (tramp-compat-process-live-p p)
1821 (or argument
1822 (string-equal
1823 share
1824 (tramp-get-connection-property p "smb-share" ""))))
1826 (save-match-data
1827 ;; There might be unread output from checking for share names.
1828 (when buf (with-current-buffer buf (erase-buffer)))
1829 (when (and p (processp p)) (delete-process p))
1831 (let* ((user (tramp-file-name-user vec))
1832 (host (tramp-file-name-host vec))
1833 (domain (tramp-file-name-domain vec))
1834 (port (tramp-file-name-port vec))
1835 args)
1837 (cond
1838 (argument
1839 (setq args (list (concat "//" host))))
1840 (share
1841 (setq args (list (concat "//" host "/" share))))
1843 (setq args (list "-g" "-L" host ))))
1845 (if (not (zerop (length user)))
1846 (setq args (append args (list "-U" user)))
1847 (setq args (append args (list "-N"))))
1849 (when domain (setq args (append args (list "-W" domain))))
1850 (when port (setq args (append args (list "-p" port))))
1851 (when tramp-smb-conf
1852 (setq args (append args (list "-s" tramp-smb-conf))))
1853 (when argument
1854 (setq args (append args (list argument))))
1856 ;; OK, let's go.
1857 (with-tramp-progress-reporter
1858 vec 3
1859 (format "Opening connection for //%s%s/%s"
1860 (if (not (zerop (length user))) (concat user "@") "")
1861 host (or share ""))
1863 (let* ((coding-system-for-read nil)
1864 (process-connection-type tramp-process-connection-type)
1865 (p (let ((default-directory
1866 (tramp-compat-temporary-file-directory)))
1867 (apply #'start-process
1868 (tramp-get-connection-name vec)
1869 (tramp-get-connection-buffer vec)
1870 (if argument
1871 tramp-smb-winexe-program tramp-smb-program)
1872 args))))
1874 (tramp-message
1875 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1876 (tramp-set-connection-property p "vector" vec)
1877 (process-put p 'adjust-window-size-function 'ignore)
1878 (set-process-query-on-exit-flag p nil)
1880 ;; Set variables for computing the prompt for reading password.
1881 (setq tramp-current-method tramp-smb-method
1882 tramp-current-user user
1883 tramp-current-host host)
1885 (condition-case err
1886 (let (tramp-message-show-message)
1887 ;; Play login scenario.
1888 (tramp-process-actions
1889 p vec nil
1890 (if (or argument share)
1891 tramp-smb-actions-with-share
1892 tramp-smb-actions-without-share))
1894 ;; Check server version.
1895 (unless argument
1896 (with-current-buffer (tramp-get-connection-buffer vec)
1897 (goto-char (point-min))
1898 (search-forward-regexp tramp-smb-server-version nil t)
1899 (let ((smbserver-version (match-string 0)))
1900 (unless
1901 (string-equal
1902 smbserver-version
1903 (tramp-get-connection-property
1904 vec "smbserver-version" smbserver-version))
1905 (tramp-flush-directory-property vec "")
1906 (tramp-flush-connection-property vec))
1907 (tramp-set-connection-property
1908 vec "smbserver-version" smbserver-version))))
1910 ;; Set chunksize to 1. smbclient reads its input
1911 ;; character by character; if we send the string
1912 ;; at once, it is read painfully slow.
1913 (tramp-set-connection-property p "smb-share" share)
1914 (tramp-set-connection-property p "chunksize" 1)
1916 ;; Set connection-local variables.
1917 (tramp-set-connection-local-variables vec)
1919 ;; Mark it as connected.
1920 (tramp-set-connection-property p "connected" t))
1922 ;; Check for the error reason. If it was due to wrong
1923 ;; password, reestablish the connection. We cannot
1924 ;; handle this in `tramp-process-actions', because
1925 ;; smbclient does not ask for the password, again.
1926 (error
1927 (with-current-buffer (tramp-get-connection-buffer vec)
1928 (goto-char (point-min))
1929 (if (and (bound-and-true-p auth-sources)
1930 (search-forward-regexp
1931 tramp-smb-wrong-passwd-regexp nil t))
1932 ;; Disable `auth-source' and `password-cache'.
1933 (let (auth-sources)
1934 (tramp-message
1935 vec 3 "Retry connection with new password")
1936 (tramp-cleanup-connection vec t)
1937 (tramp-smb-maybe-open-connection vec argument))
1938 ;; Propagate the error.
1939 (signal (car err) (cdr err)))))))))))))
1941 ;; We don't use timeouts. If needed, the caller shall wrap around.
1942 (defun tramp-smb-wait-for-output (vec)
1943 "Wait for output from smbclient command.
1944 Returns nil if an error message has appeared."
1945 (with-current-buffer (tramp-get-connection-buffer vec)
1946 (let ((p (get-buffer-process (current-buffer)))
1947 (found (progn (goto-char (point-min))
1948 (re-search-forward tramp-smb-prompt nil t)))
1949 (err (progn (goto-char (point-min))
1950 (re-search-forward tramp-smb-errors nil t)))
1951 buffer-read-only)
1953 ;; Algorithm: get waiting output. See if last line contains
1954 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1955 ;; If not, wait a bit and again get waiting output.
1956 (while (and (not found) (not err) (tramp-compat-process-live-p p))
1958 ;; Accept pending output.
1959 (tramp-accept-process-output p 0.1)
1961 ;; Search for prompt.
1962 (goto-char (point-min))
1963 (setq found (re-search-forward tramp-smb-prompt nil t))
1965 ;; Search for errors.
1966 (goto-char (point-min))
1967 (setq err (re-search-forward tramp-smb-errors nil t)))
1969 ;; When the process is still alive, read pending output.
1970 (while (and (not found) (tramp-compat-process-live-p p))
1972 ;; Accept pending output.
1973 (tramp-accept-process-output p 0.1)
1975 ;; Search for prompt.
1976 (goto-char (point-min))
1977 (setq found (re-search-forward tramp-smb-prompt nil t)))
1979 (tramp-message vec 6 "\n%s" (buffer-string))
1981 ;; Remove prompt.
1982 (when found
1983 (goto-char (point-max))
1984 (re-search-backward tramp-smb-prompt nil t)
1985 (delete-region (point) (point-max)))
1987 ;; Return value is whether no error message has appeared.
1988 (not err))))
1990 (defun tramp-smb-kill-winexe-function ()
1991 "Send SIGKILL to the winexe process."
1992 (ignore-errors
1993 (let ((p (get-buffer-process (current-buffer))))
1994 (when (tramp-compat-process-live-p p)
1995 (signal-process (process-id p) 'SIGINT)))))
1997 (defun tramp-smb-call-winexe (vec)
1998 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2000 ;; Check for program.
2001 (unless (executable-find tramp-smb-winexe-program)
2002 (tramp-error
2003 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
2005 ;; winexe does not supports ports.
2006 (when (tramp-file-name-port vec)
2007 (tramp-error vec 'file-error "Port not supported for remote processes"))
2009 (tramp-smb-maybe-open-connection
2011 (format
2012 "%s %s"
2013 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2015 (set (make-local-variable 'kill-buffer-hook)
2016 '(tramp-smb-kill-winexe-function))
2018 ;; Suppress "^M". Shouldn't we specify utf8?
2019 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2021 ;; Set width to 128. This avoids mixing prompt and long error messages.
2022 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2023 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2024 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2025 (tramp-smb-send-command vec "$bufsize.Width = 128")
2026 (tramp-smb-send-command vec "$winsize.Width = 128")
2027 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2028 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2030 (defun tramp-smb-shell-quote-argument (s)
2031 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2032 (let ((system-type 'ms-dos))
2033 (tramp-unquote-shell-quote-argument s)))
2035 (add-hook 'tramp-unload-hook
2036 (lambda ()
2037 (unload-feature 'tramp-smb 'force)))
2039 (provide 'tramp-smb)
2041 ;;; TODO:
2043 ;; * Return more comprehensive file permission string.
2045 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2046 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2048 ;; * Ignore case in file names.
2050 ;;; tramp-smb.el ends here