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