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