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