Fix error codes in Tramp
[emacs.git] / lisp / net / tramp-smb.el
blob0c8ecf470e14c457f58311772b1fa44b9857a9d8
1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2016 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"))))
54 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
55 ;; a domain in USER, it must be the SMB method.
56 ;;;###tramp-autoload
57 (add-to-list 'tramp-default-method-alist
58 `(nil ,tramp-prefix-domain-regexp ,tramp-smb-method))
60 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
61 ;; the anonymous user is chosen.
62 ;;;###tramp-autoload
63 (add-to-list 'tramp-default-user-alist
64 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
66 ;; Add completion function for SMB method.
67 ;;;###tramp-autoload
68 (eval-after-load 'tramp
69 '(tramp-set-completion-function
70 tramp-smb-method
71 '((tramp-parse-netrc "~/.netrc"))))
73 ;;;###tramp-autoload
74 (defcustom tramp-smb-program "smbclient"
75 "Name of SMB client to run."
76 :group 'tramp
77 :type 'string
78 :require 'tramp)
80 ;;;###tramp-autoload
81 (defcustom tramp-smb-acl-program "smbcacls"
82 "Name of SMB acls to run."
83 :group 'tramp
84 :type 'string
85 :version "24.4"
86 :require 'tramp)
88 ;;;###tramp-autoload
89 (defcustom tramp-smb-conf "/dev/null"
90 "Path of the smb.conf file.
91 If it is nil, no smb.conf will be added to the `tramp-smb-program'
92 call, letting the SMB client use the default one."
93 :group 'tramp
94 :type '(choice (const nil) (file :must-match t))
95 :require 'tramp)
97 (defvar tramp-smb-version nil
98 "Version string of the SMB client.")
100 (defconst tramp-smb-server-version
101 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
102 "Regexp of SMB server identification.")
104 (defconst tramp-smb-prompt "^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
105 "Regexp used as prompt in smbclient or powershell.")
107 (defconst tramp-smb-wrong-passwd-regexp
108 (regexp-opt
109 '("NT_STATUS_LOGON_FAILURE"
110 "NT_STATUS_WRONG_PASSWORD"))
111 "Regexp for login error strings of SMB servers.")
113 (defconst tramp-smb-errors
114 (mapconcat
115 'identity
116 `(;; Connection error / timeout / unknown command.
117 "Connection\\( to \\S-+\\)? failed"
118 "Read from server failed, maybe it closed the connection"
119 "Call timed out: server did not respond"
120 "\\S-+: command not found"
121 "Server doesn't support UNIX CIFS calls"
122 ,(regexp-opt
123 '(;; Samba.
124 "ERRDOS"
125 "ERRHRD"
126 "ERRSRV"
127 "ERRbadfile"
128 "ERRbadpw"
129 "ERRfilexists"
130 "ERRnoaccess"
131 "ERRnomem"
132 "ERRnosuchshare"
133 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
134 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
135 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7),
136 ;; Windows 6.3 (Windows Server 2012, Windows 10).
137 "NT_STATUS_ACCESS_DENIED"
138 "NT_STATUS_ACCOUNT_LOCKED_OUT"
139 "NT_STATUS_BAD_NETWORK_NAME"
140 "NT_STATUS_CANNOT_DELETE"
141 "NT_STATUS_CONNECTION_REFUSED"
142 "NT_STATUS_DIRECTORY_NOT_EMPTY"
143 "NT_STATUS_DUPLICATE_NAME"
144 "NT_STATUS_FILE_IS_A_DIRECTORY"
145 "NT_STATUS_HOST_UNREACHABLE"
146 "NT_STATUS_IMAGE_ALREADY_LOADED"
147 "NT_STATUS_INVALID_LEVEL"
148 "NT_STATUS_IO_TIMEOUT"
149 "NT_STATUS_LOGON_FAILURE"
150 "NT_STATUS_NETWORK_ACCESS_DENIED"
151 "NT_STATUS_NOT_IMPLEMENTED"
152 "NT_STATUS_NO_LOGON_SERVERS"
153 "NT_STATUS_NO_SUCH_FILE"
154 "NT_STATUS_NO_SUCH_USER"
155 "NT_STATUS_OBJECT_NAME_COLLISION"
156 "NT_STATUS_OBJECT_NAME_INVALID"
157 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
158 "NT_STATUS_SHARING_VIOLATION"
159 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
160 "NT_STATUS_UNSUCCESSFUL"
161 "NT_STATUS_WRONG_PASSWORD")))
162 "\\|")
163 "Regexp for possible error strings of SMB servers.
164 Used instead of analyzing error codes of commands.")
166 (defconst tramp-smb-actions-with-share
167 '((tramp-smb-prompt tramp-action-succeed)
168 (tramp-password-prompt-regexp tramp-action-password)
169 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
170 (tramp-smb-errors tramp-action-permission-denied)
171 (tramp-process-alive-regexp tramp-action-process-alive))
172 "List of pattern/action pairs.
173 This list is used for login to SMB servers.
175 See `tramp-actions-before-shell' for more info.")
177 (defconst tramp-smb-actions-without-share
178 '((tramp-password-prompt-regexp tramp-action-password)
179 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
180 (tramp-smb-errors tramp-action-permission-denied)
181 (tramp-process-alive-regexp tramp-action-out-of-band))
182 "List of pattern/action pairs.
183 This list is used for login to SMB servers.
185 See `tramp-actions-before-shell' for more info.")
187 (defconst tramp-smb-actions-with-tar
188 '((tramp-password-prompt-regexp tramp-action-password)
189 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
190 (tramp-smb-errors tramp-action-permission-denied)
191 (tramp-process-alive-regexp tramp-smb-action-with-tar))
192 "List of pattern/action pairs.
193 This list is used for tar-like copy of directories.
195 See `tramp-actions-before-shell' for more info.")
197 (defconst tramp-smb-actions-get-acl
198 '((tramp-password-prompt-regexp tramp-action-password)
199 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
200 (tramp-smb-errors tramp-action-permission-denied)
201 (tramp-process-alive-regexp tramp-smb-action-get-acl))
202 "List of pattern/action pairs.
203 This list is used for smbcacls actions.
205 See `tramp-actions-before-shell' for more info.")
207 (defconst tramp-smb-actions-set-acl
208 '((tramp-password-prompt-regexp tramp-action-password)
209 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
210 (tramp-smb-errors tramp-action-permission-denied)
211 (tramp-process-alive-regexp tramp-smb-action-set-acl))
212 "List of pattern/action pairs.
213 This list is used for smbcacls actions.
215 See `tramp-actions-before-shell' for more info.")
217 ;; New handlers should be added here.
218 (defconst tramp-smb-file-name-handler-alist
219 '(;; `access-file' performed by default handler.
220 (add-name-to-file . tramp-smb-handle-add-name-to-file)
221 ;; `byte-compiler-base-file-name' performed by default handler.
222 (copy-directory . tramp-smb-handle-copy-directory)
223 (copy-file . tramp-smb-handle-copy-file)
224 (delete-directory . tramp-smb-handle-delete-directory)
225 (delete-file . tramp-smb-handle-delete-file)
226 ;; `diff-latest-backup-file' performed by default handler.
227 (directory-file-name . tramp-handle-directory-file-name)
228 (directory-files . tramp-smb-handle-directory-files)
229 (directory-files-and-attributes
230 . tramp-handle-directory-files-and-attributes)
231 (dired-compress-file . ignore)
232 (dired-uncache . tramp-handle-dired-uncache)
233 (expand-file-name . tramp-smb-handle-expand-file-name)
234 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
235 (file-acl . tramp-smb-handle-file-acl)
236 (file-attributes . tramp-smb-handle-file-attributes)
237 (file-directory-p . tramp-smb-handle-file-directory-p)
238 (file-file-equal-p . tramp-handle-file-equal-p)
239 (file-executable-p . tramp-handle-file-exists-p)
240 (file-exists-p . tramp-handle-file-exists-p)
241 (file-in-directory-p . tramp-handle-file-in-directory-p)
242 (file-local-copy . tramp-smb-handle-file-local-copy)
243 (file-modes . tramp-handle-file-modes)
244 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
245 (file-name-as-directory . tramp-handle-file-name-as-directory)
246 (file-name-completion . tramp-handle-file-name-completion)
247 (file-name-directory . tramp-handle-file-name-directory)
248 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
249 ;; `file-name-sans-versions' performed by default handler.
250 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
251 (file-notify-add-watch . tramp-handle-file-notify-add-watch)
252 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
253 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
254 (file-ownership-preserved-p . ignore)
255 (file-readable-p . tramp-handle-file-exists-p)
256 (file-regular-p . tramp-handle-file-regular-p)
257 (file-remote-p . tramp-handle-file-remote-p)
258 ;; `file-selinux-context' performed by default handler.
259 (file-symlink-p . tramp-handle-file-symlink-p)
260 ;; `file-truename' performed by default handler.
261 (file-writable-p . tramp-smb-handle-file-writable-p)
262 (find-backup-file-name . tramp-handle-find-backup-file-name)
263 ;; `find-file-noselect' performed by default handler.
264 ;; `get-file-buffer' performed by default handler.
265 (insert-directory . tramp-smb-handle-insert-directory)
266 (insert-file-contents . tramp-handle-insert-file-contents)
267 (load . tramp-handle-load)
268 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
269 (make-directory . tramp-smb-handle-make-directory)
270 (make-directory-internal . tramp-smb-handle-make-directory-internal)
271 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
272 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
273 (process-file . tramp-smb-handle-process-file)
274 (rename-file . tramp-smb-handle-rename-file)
275 (set-file-acl . tramp-smb-handle-set-file-acl)
276 (set-file-modes . tramp-smb-handle-set-file-modes)
277 (set-file-selinux-context . ignore)
278 (set-file-times . ignore)
279 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
280 (shell-command . tramp-handle-shell-command)
281 (start-file-process . tramp-smb-handle-start-file-process)
282 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
283 (temporary-file-directory . tramp-handle-temporary-file-directory)
284 (unhandled-file-name-directory . ignore)
285 (vc-registered . ignore)
286 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
287 (write-region . tramp-smb-handle-write-region))
288 "Alist of handler functions for Tramp SMB method.
289 Operations not mentioned here will be handled by the default Emacs primitives.")
291 ;; Options for remote processes via winexe.
292 ;;;###tramp-autoload
293 (defcustom tramp-smb-winexe-program "winexe"
294 "Name of winexe client to run.
295 If it isn't found in the local $PATH, the absolute path of winexe
296 shall be given. This is needed for remote processes."
297 :group 'tramp
298 :type 'string
299 :version "24.3"
300 :require 'tramp)
302 ;;;###tramp-autoload
303 (defcustom tramp-smb-winexe-shell-command "powershell.exe"
304 "Shell to be used for processes on remote machines.
305 This must be Powershell V2 compatible."
306 :group 'tramp
307 :type 'string
308 :version "24.3"
309 :require 'tramp)
311 ;;;###tramp-autoload
312 (defcustom tramp-smb-winexe-shell-command-switch "-file -"
313 "Command switch used together with `tramp-smb-winexe-shell-command'.
314 This can be used to disable echo etc."
315 :group 'tramp
316 :type 'string
317 :version "24.3"
318 :require 'tramp)
320 ;; It must be a `defsubst' in order to push the whole code into
321 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
322 ;;;###tramp-autoload
323 (defsubst tramp-smb-file-name-p (filename)
324 "Check if it's a filename for SMB servers."
325 (string= (tramp-file-name-method (tramp-dissect-file-name filename))
326 tramp-smb-method))
328 ;;;###tramp-autoload
329 (defun tramp-smb-file-name-handler (operation &rest args)
330 "Invoke the SMB related OPERATION.
331 First arg specifies the OPERATION, second arg is a list of arguments to
332 pass to the OPERATION."
333 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
334 (if fn
335 (save-match-data (apply (cdr fn) args))
336 (tramp-run-real-handler operation args))))
338 ;;;###tramp-autoload
339 (unless (memq system-type '(cygwin windows-nt))
340 (add-to-list 'tramp-foreign-file-name-handler-alist
341 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)))
344 ;; File name primitives.
346 (defun tramp-smb-handle-add-name-to-file
347 (filename newname &optional ok-if-already-exists)
348 "Like `add-name-to-file' for Tramp files."
349 (unless (tramp-equal-remote filename newname)
350 (with-parsed-tramp-file-name
351 (if (tramp-tramp-file-p filename) filename newname) nil
352 (tramp-error
353 v 'file-error
354 "add-name-to-file: %s"
355 "only implemented for same method, same user, same host")))
356 (with-parsed-tramp-file-name filename v1
357 (with-parsed-tramp-file-name newname v2
358 (when (file-directory-p filename)
359 (tramp-error
360 v2 'file-error
361 "add-name-to-file: %s must not be a directory" filename))
362 (when (and (not ok-if-already-exists)
363 (file-exists-p newname)
364 (not (numberp ok-if-already-exists))
365 (y-or-n-p
366 (format
367 "File %s already exists; make it a new name anyway? "
368 newname)))
369 (tramp-error
370 v2 'file-error
371 "add-name-to-file: file %s already exists" newname))
372 ;; We must also flush the cache of the directory, because
373 ;; `file-attributes' reads the values from there.
374 (tramp-flush-file-property v2 (file-name-directory v2-localname))
375 (tramp-flush-file-property v2 v2-localname)
376 (unless
377 (tramp-smb-send-command
379 (format
380 "%s \"%s\" \"%s\""
381 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
382 (tramp-smb-get-localname v1)
383 (tramp-smb-get-localname v2)))
384 (tramp-error
385 v2 'file-error
386 "error with add-name-to-file, see buffer `%s' for details"
387 (buffer-name))))))
389 (defun tramp-smb-action-with-tar (proc vec)
390 "Untar from connection buffer."
391 (if (not (tramp-compat-process-live-p proc))
392 (throw 'tramp-action 'process-died)
394 (with-current-buffer (tramp-get-connection-buffer vec)
395 (goto-char (point-min))
396 (when (search-forward-regexp tramp-smb-server-version nil t)
397 ;; There might be a hidden password prompt.
398 (widen)
399 (forward-line)
400 (tramp-message vec 6 (buffer-substring (point-min) (point)))
401 (delete-region (point-min) (point))
402 (throw 'tramp-action 'ok)))))
404 (defun tramp-smb-handle-copy-directory
405 (dirname newname &optional keep-date parents copy-contents)
406 "Like `copy-directory' for Tramp files."
407 (if copy-contents
408 ;; We must do it file-wise.
409 (tramp-run-real-handler
410 'copy-directory (list dirname newname keep-date parents copy-contents))
412 (setq dirname (expand-file-name dirname)
413 newname (expand-file-name newname))
414 (let ((t1 (tramp-tramp-file-p dirname))
415 (t2 (tramp-tramp-file-p newname)))
416 (with-parsed-tramp-file-name (if t1 dirname newname) nil
417 (with-tramp-progress-reporter
418 v 0 (format "Copying %s to %s" dirname newname)
419 (cond
420 ;; We must use a local temporary directory.
421 ((and t1 t2)
422 (let ((tmpdir
423 (make-temp-name
424 (expand-file-name
425 tramp-temp-name-prefix
426 (tramp-compat-temporary-file-directory)))))
427 (unwind-protect
428 (progn
429 (make-directory tmpdir)
430 (copy-directory dirname tmpdir keep-date 'parents)
431 (copy-directory
432 (expand-file-name (file-name-nondirectory dirname) tmpdir)
433 newname keep-date parents))
434 (delete-directory tmpdir 'recursive))))
436 ;; We can copy recursively.
437 ((and (or t1 t2) (tramp-smb-get-cifs-capabilities v))
438 (when (and (file-directory-p newname)
439 (not (string-equal (file-name-nondirectory dirname)
440 (file-name-nondirectory newname))))
441 (setq newname
442 (expand-file-name
443 (file-name-nondirectory dirname) newname))
444 (if t2 (setq v (tramp-dissect-file-name newname))))
445 (if (not (file-directory-p newname))
446 (make-directory newname parents))
448 (setq tramp-current-method (tramp-file-name-method v)
449 tramp-current-user (tramp-file-name-user v)
450 tramp-current-host (tramp-file-name-real-host v))
452 (let* ((real-user (tramp-file-name-real-user v))
453 (real-host (tramp-file-name-real-host v))
454 (domain (tramp-file-name-domain v))
455 (port (tramp-file-name-port v))
456 (share (tramp-smb-get-share v))
457 (localname (file-name-as-directory
458 (replace-regexp-in-string
459 "\\\\" "/" (tramp-smb-get-localname v))))
460 (tmpdir (make-temp-name
461 (expand-file-name
462 tramp-temp-name-prefix
463 (tramp-compat-temporary-file-directory))))
464 (args (list (concat "//" real-host "/" share) "-E")))
466 (if (not (zerop (length real-user)))
467 (setq args (append args (list "-U" real-user)))
468 (setq args (append args (list "-N"))))
470 (when domain (setq args (append args (list "-W" domain))))
471 (when port (setq args (append args (list "-p" port))))
472 (when tramp-smb-conf
473 (setq args (append args (list "-s" tramp-smb-conf))))
474 (setq args
475 (if t1
476 ;; Source is remote.
477 (append args
478 (list "-D" (shell-quote-argument localname)
479 "-c" (shell-quote-argument "tar qc - *")
480 "|" "tar" "xfC" "-"
481 (shell-quote-argument tmpdir)))
482 ;; Target is remote.
483 (append (list "tar" "cfC" "-"
484 (shell-quote-argument dirname) "." "|")
485 args
486 (list "-D" (shell-quote-argument localname)
487 "-c" (shell-quote-argument "tar qx -")))))
489 (unwind-protect
490 (with-temp-buffer
491 ;; Set the transfer process properties.
492 (tramp-set-connection-property
493 v "process-name" (buffer-name (current-buffer)))
494 (tramp-set-connection-property
495 v "process-buffer" (current-buffer))
497 (when t1
498 ;; The smbclient tar command creates always
499 ;; complete paths. We must emulate the
500 ;; directory structure, and symlink to the real
501 ;; target.
502 (make-directory
503 (expand-file-name
504 ".." (concat tmpdir localname)) 'parents)
505 (make-symbolic-link
506 newname (directory-file-name (concat tmpdir localname))))
508 ;; Use an asynchronous processes. By this,
509 ;; password can be handled.
510 (let* ((default-directory tmpdir)
511 (p (apply
512 'start-process
513 (tramp-get-connection-name v)
514 (tramp-get-connection-buffer v)
515 tramp-smb-program args)))
517 (tramp-message
518 v 6 "%s" (mapconcat 'identity (process-command p) " "))
519 (tramp-set-connection-property p "vector" v)
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 filename (tramp-smb-get-localname v)))
600 (tramp-error
601 v 'file-error "Cannot copy `%s' to `%s'" filename newname))))))
603 ;; KEEP-DATE handling.
604 (when keep-date
605 (set-file-times
606 newname
607 (tramp-compat-file-attribute-modification-time
608 (file-attributes filename))))))
610 (defun tramp-smb-handle-delete-directory (directory &optional recursive)
611 "Like `delete-directory' for Tramp files."
612 (setq directory (directory-file-name (expand-file-name directory)))
613 (when (file-exists-p directory)
614 (when recursive
615 (mapc
616 (lambda (file)
617 (if (file-directory-p file)
618 (delete-directory file recursive)
619 (delete-file file)))
620 ;; We do not want to delete "." and "..".
621 (directory-files directory 'full directory-files-no-dot-files-regexp)))
623 (with-parsed-tramp-file-name directory nil
624 ;; We must also flush the cache of the directory, because
625 ;; `file-attributes' reads the values from there.
626 (tramp-flush-file-property v (file-name-directory localname))
627 (tramp-flush-directory-property v localname)
628 (unless (tramp-smb-send-command
629 v (format
630 "%s \"%s\""
631 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
632 (tramp-smb-get-localname v)))
633 ;; Error.
634 (with-current-buffer (tramp-get-connection-buffer v)
635 (goto-char (point-min))
636 (search-forward-regexp tramp-smb-errors nil t)
637 (tramp-error
638 v 'file-error "%s `%s'" (match-string 0) directory))))))
640 (defun tramp-smb-handle-delete-file (filename &optional _trash)
641 "Like `delete-file' for Tramp files."
642 (setq filename (expand-file-name filename))
643 (when (file-exists-p filename)
644 (with-parsed-tramp-file-name filename nil
645 ;; We must also flush the cache of the directory, because
646 ;; `file-attributes' reads the values from there.
647 (tramp-flush-file-property v (file-name-directory localname))
648 (tramp-flush-file-property v localname)
649 (unless (tramp-smb-send-command
650 v (format
651 "%s \"%s\""
652 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
653 (tramp-smb-get-localname v)))
654 ;; Error.
655 (with-current-buffer (tramp-get-connection-buffer v)
656 (goto-char (point-min))
657 (search-forward-regexp tramp-smb-errors nil t)
658 (tramp-error
659 v 'file-error "%s `%s'" (match-string 0) filename))))))
661 (defun tramp-smb-handle-directory-files
662 (directory &optional full match nosort)
663 "Like `directory-files' for Tramp files."
664 (let ((result (mapcar 'directory-file-name
665 (file-name-all-completions "" directory))))
666 ;; Discriminate with regexp.
667 (when match
668 (setq result
669 (delete nil
670 (mapcar (lambda (x) (when (string-match match x) x))
671 result))))
672 ;; Append directory.
673 (when full
674 (setq result
675 (mapcar
676 (lambda (x) (format "%s/%s" directory x))
677 result)))
678 ;; Sort them if necessary.
679 (unless nosort (setq result (sort result 'string-lessp)))
680 result))
682 (defun tramp-smb-handle-expand-file-name (name &optional dir)
683 "Like `expand-file-name' for Tramp files."
684 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
685 (setq dir (or dir default-directory "/"))
686 ;; Unless NAME is absolute, concat DIR and NAME.
687 (unless (file-name-absolute-p name)
688 (setq name (concat (file-name-as-directory dir) name)))
689 ;; If NAME is not a Tramp file, run the real handler.
690 (if (not (tramp-tramp-file-p name))
691 (tramp-run-real-handler 'expand-file-name (list name nil))
692 ;; Dissect NAME.
693 (with-parsed-tramp-file-name name nil
694 ;; Tilde expansion if necessary. We use the user name as share,
695 ;; which is often the case in domains.
696 (when (string-match "\\`/?~\\([^/]*\\)" localname)
697 (setq localname
698 (replace-match
699 (if (zerop (length (match-string 1 localname)))
700 (tramp-file-name-real-user v)
701 (match-string 1 localname))
702 nil nil localname)))
703 ;; Make the file name absolute.
704 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
705 (setq localname (concat "/" localname)))
706 ;; No tilde characters in file name, do normal
707 ;; `expand-file-name' (this does "/./" and "/../").
708 (tramp-make-tramp-file-name
709 method user host
710 (tramp-run-real-handler 'expand-file-name (list localname))))))
712 (defun tramp-smb-action-get-acl (proc vec)
713 "Read ACL data from connection buffer."
714 (unless (tramp-compat-process-live-p proc)
715 ;; Accept pending output.
716 (while (tramp-accept-process-output proc 0.1))
717 (with-current-buffer (tramp-get-connection-buffer vec)
718 ;; There might be a hidden password prompt.
719 (widen)
720 (tramp-message vec 10 "\n%s" (buffer-string))
721 (goto-char (point-min))
722 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
723 (forward-line)
724 (delete-region (point-min) (point)))
725 (while (and (not (eobp)) (looking-at "^.+:.+"))
726 (forward-line))
727 (delete-region (point) (point-max))
728 (throw 'tramp-action 'ok))))
730 (defun tramp-smb-handle-file-acl (filename)
731 "Like `file-acl' for Tramp files."
732 (with-parsed-tramp-file-name filename nil
733 (with-tramp-file-property v localname "file-acl"
734 (when (executable-find tramp-smb-acl-program)
736 (setq tramp-current-method (tramp-file-name-method v)
737 tramp-current-user (tramp-file-name-user v)
738 tramp-current-host (tramp-file-name-real-host v))
740 (let* ((real-user (tramp-file-name-real-user v))
741 (real-host (tramp-file-name-real-host v))
742 (domain (tramp-file-name-domain v))
743 (port (tramp-file-name-port v))
744 (share (tramp-smb-get-share v))
745 (localname (replace-regexp-in-string
746 "\\\\" "/" (tramp-smb-get-localname v)))
747 (args (list (concat "//" real-host "/" share) "-E")))
749 (if (not (zerop (length real-user)))
750 (setq args (append args (list "-U" real-user)))
751 (setq args (append args (list "-N"))))
753 (when domain (setq args (append args (list "-W" domain))))
754 (when port (setq args (append args (list "-p" port))))
755 (when tramp-smb-conf
756 (setq args (append args (list "-s" tramp-smb-conf))))
757 (setq
758 args
759 (append args (list (shell-quote-argument localname) "2>/dev/null")))
761 (unwind-protect
762 (with-temp-buffer
763 ;; Set the transfer process properties.
764 (tramp-set-connection-property
765 v "process-name" (buffer-name (current-buffer)))
766 (tramp-set-connection-property
767 v "process-buffer" (current-buffer))
769 ;; Use an asynchronous processes. By this, password
770 ;; can be handled.
771 (let ((p (apply
772 'start-process
773 (tramp-get-connection-name v)
774 (tramp-get-connection-buffer v)
775 tramp-smb-acl-program args)))
777 (tramp-message
778 v 6 "%s" (mapconcat 'identity (process-command p) " "))
779 (tramp-set-connection-property p "vector" v)
780 (set-process-query-on-exit-flag p nil)
781 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
782 (when (> (point-max) (point-min))
783 (substring-no-properties (buffer-string)))))
785 ;; Reset the transfer process properties.
786 (tramp-set-connection-property v "process-name" nil)
787 (tramp-set-connection-property v "process-buffer" nil)))))))
789 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
790 "Like `file-attributes' for Tramp files."
791 (unless id-format (setq id-format 'integer))
792 (ignore-errors
793 (with-parsed-tramp-file-name filename nil
794 (with-tramp-file-property
795 v localname (format "file-attributes-%s" id-format)
796 (if (tramp-smb-get-stat-capability v)
797 (tramp-smb-do-file-attributes-with-stat v id-format)
798 ;; Reading just the filename entry via "dir localname" is not
799 ;; possible, because when filename is a directory, some
800 ;; smbclient versions return the content of the directory, and
801 ;; other versions don't. Therefore, the whole content of the
802 ;; upper directory is retrieved, and the entry of the filename
803 ;; is extracted from.
804 (let* ((entries (tramp-smb-get-file-entries
805 (file-name-directory filename)))
806 (entry (assoc (file-name-nondirectory filename) entries))
807 (uid (if (equal id-format 'string) "nobody" -1))
808 (gid (if (equal id-format 'string) "nogroup" -1))
809 (inode (tramp-get-inode v))
810 (device (tramp-get-device v)))
812 ;; Check result.
813 (when entry
814 (list (and (string-match "d" (nth 1 entry))
815 t) ;0 file type
816 -1 ;1 link count
817 uid ;2 uid
818 gid ;3 gid
819 '(0 0) ;4 atime
820 (nth 3 entry) ;5 mtime
821 '(0 0) ;6 ctime
822 (nth 2 entry) ;7 size
823 (nth 1 entry) ;8 mode
824 nil ;9 gid weird
825 inode ;10 inode number
826 device)))))))) ;11 file system number
828 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
829 "Implement `file-attributes' for Tramp files using stat command."
830 (tramp-message
831 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
832 (with-current-buffer (tramp-get-connection-buffer vec)
833 (let* (size id link uid gid atime mtime ctime mode inode)
834 (when (tramp-smb-send-command
835 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
837 ;; Loop the listing.
838 (goto-char (point-min))
839 (unless (re-search-forward tramp-smb-errors nil t)
840 (while (not (eobp))
841 (cond
842 ((looking-at
843 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
844 (setq size (string-to-number (match-string 1))
845 id (if (string-equal "directory" (match-string 2)) t
846 (if (string-equal "symbolic" (match-string 2)) ""))))
847 ((looking-at
848 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
849 (setq inode (string-to-number (match-string 1))
850 link (string-to-number (match-string 2))))
851 ((looking-at
852 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
853 (setq mode (match-string 1)
854 uid (if (equal id-format 'string) (match-string 2)
855 (string-to-number (match-string 2)))
856 gid (if (equal id-format 'string) (match-string 3)
857 (string-to-number (match-string 3)))))
858 ((looking-at
859 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
860 (setq atime
861 (encode-time
862 (string-to-number (match-string 6)) ;; sec
863 (string-to-number (match-string 5)) ;; min
864 (string-to-number (match-string 4)) ;; hour
865 (string-to-number (match-string 3)) ;; day
866 (string-to-number (match-string 2)) ;; month
867 (string-to-number (match-string 1))))) ;; year
868 ((looking-at
869 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
870 (setq mtime
871 (encode-time
872 (string-to-number (match-string 6)) ;; sec
873 (string-to-number (match-string 5)) ;; min
874 (string-to-number (match-string 4)) ;; hour
875 (string-to-number (match-string 3)) ;; day
876 (string-to-number (match-string 2)) ;; month
877 (string-to-number (match-string 1))))) ;; year
878 ((looking-at
879 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
880 (setq ctime
881 (encode-time
882 (string-to-number (match-string 6)) ;; sec
883 (string-to-number (match-string 5)) ;; min
884 (string-to-number (match-string 4)) ;; hour
885 (string-to-number (match-string 3)) ;; day
886 (string-to-number (match-string 2)) ;; month
887 (string-to-number (match-string 1)))))) ;; year
888 (forward-line))
889 ;; Return the result.
890 (list id link uid gid atime mtime ctime size mode nil inode
891 (tramp-get-device vec)))))))
893 (defun tramp-smb-handle-file-directory-p (filename)
894 "Like `file-directory-p' for Tramp files."
895 (and (file-exists-p filename)
896 (eq ?d
897 (aref (tramp-compat-file-attribute-modes (file-attributes filename))
898 0))))
900 (defun tramp-smb-handle-file-local-copy (filename)
901 "Like `file-local-copy' for Tramp files."
902 (with-parsed-tramp-file-name filename nil
903 (unless (file-exists-p filename)
904 (tramp-error
905 v tramp-file-missing
906 "Cannot make local copy of non-existing file `%s'" filename))
907 (let ((tmpfile (tramp-compat-make-temp-file filename)))
908 (with-tramp-progress-reporter
909 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
910 (unless (tramp-smb-send-command
911 v (format "get \"%s\" \"%s\""
912 (tramp-smb-get-localname v) tmpfile))
913 ;; Oops, an error. We shall cleanup.
914 (delete-file tmpfile)
915 (tramp-error
916 v 'file-error "Cannot make local copy of file `%s'" filename)))
917 tmpfile)))
919 ;; This function should return "foo/" for directories and "bar" for
920 ;; files.
921 (defun tramp-smb-handle-file-name-all-completions (filename directory)
922 "Like `file-name-all-completions' for Tramp files."
923 (all-completions
924 filename
925 (with-parsed-tramp-file-name (expand-file-name directory) nil
926 (with-tramp-file-property v localname "file-name-all-completions"
927 (save-match-data
928 (delete-dups
929 (mapcar
930 (lambda (x)
931 (list
932 (if (string-match "d" (nth 1 x))
933 (file-name-as-directory (nth 0 x))
934 (nth 0 x))))
935 (tramp-smb-get-file-entries directory))))))))
937 (defun tramp-smb-handle-file-writable-p (filename)
938 "Like `file-writable-p' for Tramp files."
939 (if (file-exists-p filename)
940 (string-match
942 (or (tramp-compat-file-attribute-modes (file-attributes filename)) ""))
943 (let ((dir (file-name-directory filename)))
944 (and (file-exists-p dir)
945 (file-writable-p dir)))))
947 (defun tramp-smb-handle-insert-directory
948 (filename switches &optional wildcard full-directory-p)
949 "Like `insert-directory' for Tramp files."
950 (setq filename (expand-file-name filename))
951 (unless switches (setq switches ""))
952 ;; Mark trailing "/".
953 (when (and (zerop (length (file-name-nondirectory filename)))
954 (not full-directory-p))
955 (setq switches (concat switches "F")))
956 (if full-directory-p
957 ;; Called from `dired-add-entry'.
958 (setq filename (file-name-as-directory filename))
959 (setq filename (directory-file-name filename)))
960 (with-parsed-tramp-file-name filename nil
961 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
962 (save-match-data
963 (let ((base (file-name-nondirectory filename))
964 ;; We should not destroy the cache entry.
965 (entries (copy-sequence
966 (tramp-smb-get-file-entries
967 (file-name-directory filename)))))
969 (when wildcard
970 (string-match "\\." base)
971 (setq base (replace-match "\\\\." nil nil base))
972 (string-match "\\*" base)
973 (setq base (replace-match ".*" nil nil base))
974 (string-match "\\?" base)
975 (setq base (replace-match ".?" nil nil base)))
977 ;; Filter entries.
978 (setq entries
979 (delq
981 (if (or wildcard (zerop (length base)))
982 ;; Check for matching entries.
983 (mapcar
984 (lambda (x)
985 (when (string-match
986 (format "^%s" base) (nth 0 x))
988 entries)
989 ;; We just need the only and only entry FILENAME.
990 (list (assoc base entries)))))
992 ;; Sort entries.
993 (setq entries
994 (sort
995 entries
996 (lambda (x y)
997 (if (string-match "t" switches)
998 ;; Sort by date.
999 (time-less-p (nth 3 y) (nth 3 x))
1000 ;; Sort by name.
1001 (string-lessp (nth 0 x) (nth 0 y))))))
1003 ;; Handle "-F" switch.
1004 (when (string-match "F" switches)
1005 (mapc
1006 (lambda (x)
1007 (when (not (zerop (length (car x))))
1008 (cond
1009 ((char-equal ?d (string-to-char (nth 1 x)))
1010 (setcar x (concat (car x) "/")))
1011 ((char-equal ?x (string-to-char (nth 1 x)))
1012 (setcar x (concat (car x) "*"))))))
1013 entries))
1015 ;; Print entries.
1016 (mapc
1017 (lambda (x)
1018 (when (not (zerop (length (nth 0 x))))
1019 (when (string-match "l" switches)
1020 (let ((attr
1021 (when (tramp-smb-get-stat-capability v)
1022 (ignore-errors
1023 (file-attributes filename 'string)))))
1024 (insert
1025 (format
1026 "%10s %3d %-8s %-8s %8s %s "
1027 (or (tramp-compat-file-attribute-modes attr) (nth 1 x))
1028 (or (tramp-compat-file-attribute-link-number attr) 1)
1029 (or (tramp-compat-file-attribute-user-id attr) "nobody")
1030 (or (tramp-compat-file-attribute-group-id attr) "nogroup")
1031 (or (tramp-compat-file-attribute-size attr) (nth 2 x))
1032 (format-time-string
1033 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1034 tramp-half-a-year)
1035 "%b %e %R"
1036 "%b %e %Y")
1037 (nth 3 x)))))) ; date
1039 ;; We mark the file name. The inserted name could be
1040 ;; from somewhere else, so we use the relative file name
1041 ;; of `default-directory'.
1042 (let ((start (point)))
1043 (insert
1044 (format
1045 "%s\n"
1046 (file-relative-name
1047 (expand-file-name
1048 (nth 0 x) (file-name-directory filename))
1049 (when full-directory-p (file-name-directory filename)))))
1050 (put-text-property start (1- (point)) 'dired-filename t))
1051 (forward-line)
1052 (beginning-of-line)))
1053 entries))))))
1055 (defun tramp-smb-handle-make-directory (dir &optional parents)
1056 "Like `make-directory' for Tramp files."
1057 (setq dir (directory-file-name (expand-file-name dir)))
1058 (unless (file-name-absolute-p dir)
1059 (setq dir (expand-file-name dir default-directory)))
1060 (with-parsed-tramp-file-name dir nil
1061 (save-match-data
1062 (let* ((ldir (file-name-directory dir)))
1063 ;; Make missing directory parts.
1064 (when (and parents
1065 (tramp-smb-get-share v)
1066 (not (file-directory-p ldir)))
1067 (make-directory ldir parents))
1068 ;; Just do it.
1069 (when (file-directory-p ldir)
1070 (make-directory-internal dir))
1071 (unless (file-directory-p dir)
1072 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1074 (defun tramp-smb-handle-make-directory-internal (directory)
1075 "Like `make-directory-internal' for Tramp files."
1076 (setq directory (directory-file-name (expand-file-name directory)))
1077 (unless (file-name-absolute-p directory)
1078 (setq directory (expand-file-name directory default-directory)))
1079 (with-parsed-tramp-file-name directory nil
1080 (save-match-data
1081 (let* ((file (tramp-smb-get-localname v)))
1082 (when (file-directory-p (file-name-directory directory))
1083 (tramp-smb-send-command
1085 (if (tramp-smb-get-cifs-capabilities v)
1086 (format "posix_mkdir \"%s\" %o" file (default-file-modes))
1087 (format "mkdir \"%s\"" file)))
1088 ;; We must also flush the cache of the directory, because
1089 ;; `file-attributes' reads the values from there.
1090 (tramp-flush-file-property v (file-name-directory localname))
1091 (tramp-flush-file-property v localname))
1092 (unless (file-directory-p directory)
1093 (tramp-error
1094 v 'file-error "Couldn't make directory %s" directory))))))
1096 (defun tramp-smb-handle-make-symbolic-link
1097 (filename linkname &optional ok-if-already-exists)
1098 "Like `make-symbolic-link' for Tramp files.
1099 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1100 the symlink. If LINKNAME is a Tramp file, only the localname component is
1101 used as the target of the symlink.
1103 If LINKNAME is a Tramp file and the localname component is relative, then
1104 it is expanded first, before the localname component is taken. Note that
1105 this can give surprising results if the user/host for the source and
1106 target of the symlink differ."
1107 (unless (tramp-equal-remote filename linkname)
1108 (with-parsed-tramp-file-name
1109 (if (tramp-tramp-file-p filename) filename linkname) nil
1110 (tramp-error
1111 v 'file-error
1112 "make-symbolic-link: %s"
1113 "only implemented for same method, same user, same host")))
1114 (with-parsed-tramp-file-name filename v1
1115 (with-parsed-tramp-file-name linkname v2
1116 (when (file-directory-p filename)
1117 (tramp-error
1118 v2 'file-error
1119 "make-symbolic-link: %s must not be a directory" filename))
1120 (when (and (not ok-if-already-exists)
1121 (file-exists-p linkname)
1122 (not (numberp ok-if-already-exists))
1123 (y-or-n-p
1124 (format
1125 "File %s already exists; make it a new name anyway? "
1126 linkname)))
1127 (tramp-error
1128 v2 'file-already-exists
1129 "make-symbolic-link: file %s already exists" linkname))
1130 (unless (tramp-smb-get-cifs-capabilities v1)
1131 (tramp-error v2 'file-error "make-symbolic-link not supported"))
1132 ;; We must also flush the cache of the directory, because
1133 ;; `file-attributes' reads the values from there.
1134 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1135 (tramp-flush-file-property v2 v2-localname)
1136 (unless
1137 (tramp-smb-send-command
1139 (format
1140 "symlink \"%s\" \"%s\""
1141 (tramp-smb-get-localname v1)
1142 (tramp-smb-get-localname v2)))
1143 (tramp-error
1144 v2 'file-error
1145 "error with make-symbolic-link, see buffer `%s' for details"
1146 (buffer-name))))))
1148 (defun tramp-smb-handle-process-file
1149 (program &optional infile destination display &rest args)
1150 "Like `process-file' for Tramp files."
1151 ;; The implementation is not complete yet.
1152 (when (and (numberp destination) (zerop destination))
1153 (error "Implementation does not handle immediate return"))
1155 (with-parsed-tramp-file-name default-directory nil
1156 (let* ((name (file-name-nondirectory program))
1157 (name1 name)
1158 (i 0)
1159 input tmpinput outbuf command ret)
1161 ;; Determine input.
1162 (when infile
1163 (setq infile (expand-file-name infile))
1164 (if (tramp-equal-remote default-directory infile)
1165 ;; INFILE is on the same remote host.
1166 (setq input (with-parsed-tramp-file-name infile nil localname))
1167 ;; INFILE must be copied to remote host.
1168 (setq input (tramp-make-tramp-temp-file v)
1169 tmpinput (tramp-make-tramp-file-name method user host 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 (tramp-file-name-method v)
1328 tramp-current-user (tramp-file-name-user v)
1329 tramp-current-host (tramp-file-name-real-host v))
1330 (tramp-set-file-property v localname "file-acl" 'undef)
1332 (let* ((real-user (tramp-file-name-real-user v))
1333 (real-host (tramp-file-name-real-host v))
1334 (domain (tramp-file-name-domain v))
1335 (port (tramp-file-name-port v))
1336 (share (tramp-smb-get-share v))
1337 (localname (replace-regexp-in-string
1338 "\\\\" "/" (tramp-smb-get-localname v)))
1339 (args (list (concat "//" real-host "/" share) "-E" "-S"
1340 (replace-regexp-in-string
1341 "\n" "," acl-string))))
1343 (if (not (zerop (length real-user)))
1344 (setq args (append args (list "-U" real-user)))
1345 (setq args (append args (list "-N"))))
1347 (when domain (setq args (append args (list "-W" domain))))
1348 (when port (setq args (append args (list "-p" port))))
1349 (when tramp-smb-conf
1350 (setq args (append args (list "-s" tramp-smb-conf))))
1351 (setq
1352 args
1353 (append args (list (shell-quote-argument localname)
1354 "&&" "echo" "tramp_exit_status" "0"
1355 "||" "echo" "tramp_exit_status" "1")))
1357 (unwind-protect
1358 (with-temp-buffer
1359 ;; Set the transfer process properties.
1360 (tramp-set-connection-property
1361 v "process-name" (buffer-name (current-buffer)))
1362 (tramp-set-connection-property
1363 v "process-buffer" (current-buffer))
1365 ;; Use an asynchronous processes. By this, password can
1366 ;; be handled.
1367 (let ((p (apply
1368 'start-process
1369 (tramp-get-connection-name v)
1370 (tramp-get-connection-buffer v)
1371 tramp-smb-acl-program args)))
1373 (tramp-message
1374 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1375 (tramp-set-connection-property p "vector" v)
1376 (set-process-query-on-exit-flag p nil)
1377 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1378 (goto-char (point-max))
1379 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1380 (tramp-error
1381 v 'file-error
1382 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1383 (skip-chars-forward "^ ")
1384 (when (zerop (read (current-buffer)))
1385 ;; Success.
1386 (tramp-set-file-property v localname "file-acl" acl-string)
1387 t)))
1389 ;; Reset the transfer process properties.
1390 (tramp-set-connection-property v "process-name" nil)
1391 (tramp-set-connection-property v "process-buffer" nil)))))))
1393 (defun tramp-smb-handle-set-file-modes (filename mode)
1394 "Like `set-file-modes' for Tramp files."
1395 (with-parsed-tramp-file-name filename nil
1396 (when (tramp-smb-get-cifs-capabilities v)
1397 (tramp-flush-file-property v localname)
1398 (unless (tramp-smb-send-command
1399 v (format "chmod \"%s\" %o" (tramp-smb-get-localname v) mode))
1400 (tramp-error
1401 v 'file-error "Error while changing file's mode %s" filename)))))
1403 ;; We use BUFFER also as connection buffer during setup. Because of
1404 ;; this, its original contents must be saved, and restored once
1405 ;; connection has been setup.
1406 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1407 "Like `start-file-process' for Tramp files."
1408 (with-parsed-tramp-file-name default-directory nil
1409 (let* ((buffer
1410 (if buffer
1411 (get-buffer-create buffer)
1412 ;; BUFFER can be nil. We use a temporary buffer.
1413 (generate-new-buffer tramp-temp-buffer-name)))
1414 (command (mapconcat 'identity (cons program args) " "))
1415 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1416 (name1 name)
1417 (i 0))
1418 (unwind-protect
1419 (save-excursion
1420 (save-restriction
1421 (while (get-process name1)
1422 ;; NAME must be unique as process name.
1423 (setq i (1+ i)
1424 name1 (format "%s<%d>" name i)))
1425 ;; Set the new process properties.
1426 (tramp-set-connection-property v "process-name" name1)
1427 (tramp-set-connection-property v "process-buffer" buffer)
1428 ;; Activate narrowing in order to save BUFFER contents.
1429 (with-current-buffer (tramp-get-connection-buffer v)
1430 (let ((buffer-undo-list t))
1431 (narrow-to-region (point-max) (point-max))
1432 (tramp-smb-call-winexe v)
1433 (when (tramp-smb-get-share v)
1434 (tramp-smb-send-command
1435 v (format
1436 "cd \"//%s%s\""
1437 host (file-name-directory localname))))
1438 (tramp-message v 6 "(%s); exit" command)
1439 (tramp-send-string v command)))
1440 ;; Return value.
1441 (tramp-get-connection-process v)))
1443 ;; Save exit.
1444 (with-current-buffer (tramp-get-connection-buffer v)
1445 (if (string-match tramp-temp-buffer-name (buffer-name))
1446 (progn
1447 (set-process-buffer (tramp-get-connection-process v) nil)
1448 (kill-buffer (current-buffer)))
1449 (set-buffer-modified-p bmp)))
1450 (tramp-set-connection-property v "process-name" nil)
1451 (tramp-set-connection-property v "process-buffer" nil)))))
1453 (defun tramp-smb-handle-substitute-in-file-name (filename)
1454 "Like `handle-substitute-in-file-name' for Tramp files.
1455 \"//\" substitutes only in the local filename part. Catches
1456 errors for shares like \"C$/\", which are common in Microsoft Windows."
1457 (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-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-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) tramp-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 (tramp-check-proper-method-and-host vec)
1773 (let* ((share (tramp-smb-get-share vec))
1774 (buf (tramp-get-connection-buffer vec))
1775 (p (get-buffer-process buf)))
1777 ;; Check whether we still have the same smbclient version.
1778 ;; Otherwise, we must delete the connection cache, because
1779 ;; capabilities migh have changed.
1780 (unless (or argument (processp p))
1781 (let ((default-directory (tramp-compat-temporary-file-directory))
1782 (command (concat tramp-smb-program " -V")))
1784 (unless tramp-smb-version
1785 (unless (executable-find tramp-smb-program)
1786 (tramp-error
1787 vec 'file-error
1788 "Cannot find command %s in %s" tramp-smb-program exec-path))
1789 (setq tramp-smb-version (shell-command-to-string command))
1790 (tramp-message vec 6 command)
1791 (tramp-message vec 6 "\n%s" tramp-smb-version)
1792 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1793 (setq tramp-smb-version
1794 (replace-match "" nil nil tramp-smb-version))))
1796 (unless (string-equal
1797 tramp-smb-version
1798 (tramp-get-connection-property
1799 vec "smbclient-version" tramp-smb-version))
1800 (tramp-flush-directory-property vec "")
1801 (tramp-flush-connection-property vec))
1803 (tramp-set-connection-property
1804 vec "smbclient-version" tramp-smb-version)))
1806 ;; If too much time has passed since last command was sent, look
1807 ;; whether there has been an error message; maybe due to
1808 ;; connection timeout.
1809 (with-current-buffer buf
1810 (goto-char (point-min))
1811 (when (and (> (tramp-time-diff
1812 (current-time)
1813 (tramp-get-connection-property
1814 p "last-cmd-time" '(0 0 0)))
1816 (tramp-compat-process-live-p p)
1817 (re-search-forward tramp-smb-errors nil t))
1818 (delete-process p)
1819 (setq p nil)))
1821 ;; Check whether it is still the same share.
1822 (unless (and (tramp-compat-process-live-p p)
1823 (or argument
1824 (string-equal
1825 share
1826 (tramp-get-connection-property p "smb-share" ""))))
1828 (save-match-data
1829 ;; There might be unread output from checking for share names.
1830 (when buf (with-current-buffer buf (erase-buffer)))
1831 (when (and p (processp p)) (delete-process p))
1833 (let* ((user (tramp-file-name-user vec))
1834 (host (tramp-file-name-host vec))
1835 (real-user (tramp-file-name-real-user vec))
1836 (real-host (tramp-file-name-real-host vec))
1837 (domain (tramp-file-name-domain vec))
1838 (port (tramp-file-name-port vec))
1839 args)
1841 (cond
1842 (argument
1843 (setq args (list (concat "//" real-host))))
1844 (share
1845 (setq args (list (concat "//" real-host "/" share))))
1847 (setq args (list "-g" "-L" real-host ))))
1849 (if (not (zerop (length real-user)))
1850 (setq args (append args (list "-U" real-user)))
1851 (setq args (append args (list "-N"))))
1853 (when domain (setq args (append args (list "-W" domain))))
1854 (when port (setq args (append args (list "-p" port))))
1855 (when tramp-smb-conf
1856 (setq args (append args (list "-s" tramp-smb-conf))))
1857 (when argument
1858 (setq args (append args (list argument))))
1860 ;; OK, let's go.
1861 (with-tramp-progress-reporter
1862 vec 3
1863 (format "Opening connection for //%s%s/%s"
1864 (if (not (zerop (length user))) (concat user "@") "")
1865 host (or share ""))
1867 (let* ((coding-system-for-read nil)
1868 (process-connection-type tramp-process-connection-type)
1869 (p (let ((default-directory
1870 (tramp-compat-temporary-file-directory)))
1871 (apply #'start-process
1872 (tramp-get-connection-name vec)
1873 (tramp-get-connection-buffer vec)
1874 (if argument
1875 tramp-smb-winexe-program tramp-smb-program)
1876 args))))
1878 (tramp-message
1879 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1880 (tramp-set-connection-property p "vector" vec)
1881 (set-process-query-on-exit-flag p nil)
1883 ;; Set variables for computing the prompt for reading password.
1884 (setq tramp-current-method tramp-smb-method
1885 tramp-current-user user
1886 tramp-current-host host)
1888 (condition-case err
1889 (let (tramp-message-show-message)
1890 ;; Play login scenario.
1891 (tramp-process-actions
1892 p vec nil
1893 (if (or argument share)
1894 tramp-smb-actions-with-share
1895 tramp-smb-actions-without-share))
1897 ;; Check server version.
1898 (unless argument
1899 (with-current-buffer (tramp-get-connection-buffer vec)
1900 (goto-char (point-min))
1901 (search-forward-regexp tramp-smb-server-version nil t)
1902 (let ((smbserver-version (match-string 0)))
1903 (unless
1904 (string-equal
1905 smbserver-version
1906 (tramp-get-connection-property
1907 vec "smbserver-version" smbserver-version))
1908 (tramp-flush-directory-property vec "")
1909 (tramp-flush-connection-property vec))
1910 (tramp-set-connection-property
1911 vec "smbserver-version" smbserver-version))))
1913 ;; Set chunksize to 1. smbclient reads its input
1914 ;; character by character; if we send the string
1915 ;; at once, it is read painfully slow.
1916 (tramp-set-connection-property p "smb-share" share)
1917 (tramp-set-connection-property p "chunksize" 1)
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 (boundp 'auth-sources)
1930 (symbol-value 'auth-sources)
1931 (search-forward-regexp
1932 tramp-smb-wrong-passwd-regexp nil t))
1933 ;; Disable `auth-source' and `password-cache'.
1934 (let (auth-sources)
1935 (tramp-message
1936 vec 3 "Retry connection with new password")
1937 (tramp-cleanup-connection vec t)
1938 (tramp-smb-maybe-open-connection vec argument))
1939 ;; Propagate the error.
1940 (signal (car err) (cdr err)))))))))))))
1942 ;; We don't use timeouts. If needed, the caller shall wrap around.
1943 (defun tramp-smb-wait-for-output (vec)
1944 "Wait for output from smbclient command.
1945 Returns nil if an error message has appeared."
1946 (with-current-buffer (tramp-get-connection-buffer vec)
1947 (let ((p (get-buffer-process (current-buffer)))
1948 (found (progn (goto-char (point-min))
1949 (re-search-forward tramp-smb-prompt nil t)))
1950 (err (progn (goto-char (point-min))
1951 (re-search-forward tramp-smb-errors nil t)))
1952 buffer-read-only)
1954 ;; Algorithm: get waiting output. See if last line contains
1955 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1956 ;; If not, wait a bit and again get waiting output.
1957 (while (and (not found) (not err) (tramp-compat-process-live-p p))
1959 ;; Accept pending output.
1960 (tramp-accept-process-output p 0.1)
1962 ;; Search for prompt.
1963 (goto-char (point-min))
1964 (setq found (re-search-forward tramp-smb-prompt nil t))
1966 ;; Search for errors.
1967 (goto-char (point-min))
1968 (setq err (re-search-forward tramp-smb-errors nil t)))
1970 ;; When the process is still alive, read pending output.
1971 (while (and (not found) (tramp-compat-process-live-p p))
1973 ;; Accept pending output.
1974 (tramp-accept-process-output p 0.1)
1976 ;; Search for prompt.
1977 (goto-char (point-min))
1978 (setq found (re-search-forward tramp-smb-prompt nil t)))
1980 (tramp-message vec 6 "\n%s" (buffer-string))
1982 ;; Remove prompt.
1983 (when found
1984 (goto-char (point-max))
1985 (re-search-backward tramp-smb-prompt nil t)
1986 (delete-region (point) (point-max)))
1988 ;; Return value is whether no error message has appeared.
1989 (not err))))
1991 (defun tramp-smb-kill-winexe-function ()
1992 "Send SIGKILL to the winexe process."
1993 (ignore-errors
1994 (let ((p (get-buffer-process (current-buffer))))
1995 (when (tramp-compat-process-live-p p)
1996 (signal-process (process-id p) 'SIGINT)))))
1998 (defun tramp-smb-call-winexe (vec)
1999 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2001 ;; Check for program.
2002 (unless (executable-find tramp-smb-winexe-program)
2003 (tramp-error
2004 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
2006 ;; winexe does not supports ports.
2007 (when (tramp-file-name-port vec)
2008 (tramp-error vec 'file-error "Port not supported for remote processes"))
2010 (tramp-smb-maybe-open-connection
2012 (format
2013 "%s %s"
2014 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2016 (set (make-local-variable 'kill-buffer-hook)
2017 '(tramp-smb-kill-winexe-function))
2019 ;; Suppress "^M". Shouldn't we specify utf8?
2020 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2022 ;; Set width to 128. This avoids mixing prompt and long error messages.
2023 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2024 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2025 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2026 (tramp-smb-send-command vec "$bufsize.Width = 128")
2027 (tramp-smb-send-command vec "$winsize.Width = 128")
2028 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2029 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2031 (defun tramp-smb-shell-quote-argument (s)
2032 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2033 (let ((system-type 'ms-dos))
2034 (shell-quote-argument s)))
2036 (add-hook 'tramp-unload-hook
2037 (lambda ()
2038 (unload-feature 'tramp-smb 'force)))
2040 (provide 'tramp-smb)
2042 ;;; TODO:
2044 ;; * Return more comprehensive file permission string.
2046 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2047 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2049 ;; * Ignore case in file names.
2051 ;;; tramp-smb.el ends here