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