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