]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-smb.el
Make all Tramp tests pass for "gdrive" method
[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 (when 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 directory 'full directory-files-no-dot-files-regexp)))
608
609 (with-parsed-tramp-file-name directory nil
610 ;; We must also flush the cache of the directory, because
611 ;; `file-attributes' reads the values from there.
612 (tramp-flush-file-property v (file-name-directory localname))
613 (tramp-flush-directory-property v localname)
614 (unless (tramp-smb-send-command
615 v (format
616 "%s \"%s\""
617 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
618 (tramp-smb-get-localname v)))
619 ;; Error.
620 (with-current-buffer (tramp-get-connection-buffer v)
621 (goto-char (point-min))
622 (search-forward-regexp tramp-smb-errors nil t)
623 (tramp-error
624 v 'file-error "%s `%s'" (match-string 0) directory))))))
625
626 (defun tramp-smb-handle-delete-file (filename &optional _trash)
627 "Like `delete-file' for Tramp files."
628 (setq filename (expand-file-name filename))
629 (when (file-exists-p filename)
630 (with-parsed-tramp-file-name filename nil
631 ;; We must also flush the cache of the directory, because
632 ;; `file-attributes' reads the values from there.
633 (tramp-flush-file-property v (file-name-directory localname))
634 (tramp-flush-file-property v localname)
635 (unless (tramp-smb-send-command
636 v (format
637 "%s \"%s\""
638 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
639 (tramp-smb-get-localname v)))
640 ;; Error.
641 (with-current-buffer (tramp-get-connection-buffer v)
642 (goto-char (point-min))
643 (search-forward-regexp tramp-smb-errors nil t)
644 (tramp-error
645 v 'file-error "%s `%s'" (match-string 0) filename))))))
646
647 (defun tramp-smb-handle-directory-files
648 (directory &optional full match nosort)
649 "Like `directory-files' for Tramp files."
650 (let ((result (mapcar 'directory-file-name
651 (file-name-all-completions "" directory))))
652 ;; Discriminate with regexp.
653 (when match
654 (setq result
655 (delete nil
656 (mapcar (lambda (x) (when (string-match match x) x))
657 result))))
658 ;; Append directory.
659 (when full
660 (setq result
661 (mapcar
662 (lambda (x) (format "%s/%s" directory x))
663 result)))
664 ;; Sort them if necessary.
665 (unless nosort (setq result (sort result 'string-lessp)))
666 result))
667
668 (defun tramp-smb-handle-expand-file-name (name &optional dir)
669 "Like `expand-file-name' for Tramp files."
670 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
671 (setq dir (or dir default-directory "/"))
672 ;; Unless NAME is absolute, concat DIR and NAME.
673 (unless (file-name-absolute-p name)
674 (setq name (concat (file-name-as-directory dir) name)))
675 ;; If NAME is not a Tramp file, run the real handler.
676 (if (not (tramp-tramp-file-p name))
677 (tramp-run-real-handler 'expand-file-name (list name nil))
678 ;; Dissect NAME.
679 (with-parsed-tramp-file-name name nil
680 ;; Tilde expansion if necessary. We use the user name as share,
681 ;; which is often the case in domains.
682 (when (string-match "\\`/?~\\([^/]*\\)" localname)
683 (setq localname
684 (replace-match
685 (if (zerop (length (match-string 1 localname)))
686 (tramp-file-name-real-user v)
687 (match-string 1 localname))
688 nil nil localname)))
689 ;; Make the file name absolute.
690 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
691 (setq localname (concat "/" localname)))
692 ;; No tilde characters in file name, do normal
693 ;; `expand-file-name' (this does "/./" and "/../").
694 (tramp-make-tramp-file-name
695 method user host
696 (tramp-run-real-handler 'expand-file-name (list localname))))))
697
698 (defun tramp-smb-action-get-acl (proc vec)
699 "Read ACL data from connection buffer."
700 (when (not (memq (process-status proc) '(run open)))
701 ;; Accept pending output.
702 (while (tramp-accept-process-output proc 0.1))
703 (with-current-buffer (tramp-get-connection-buffer vec)
704 ;; There might be a hidden password prompt.
705 (widen)
706 (tramp-message vec 10 "\n%s" (buffer-string))
707 (goto-char (point-min))
708 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
709 (forward-line)
710 (delete-region (point-min) (point)))
711 (while (and (not (eobp)) (looking-at "^.+:.+"))
712 (forward-line))
713 (delete-region (point) (point-max))
714 (throw 'tramp-action 'ok))))
715
716 (defun tramp-smb-handle-file-acl (filename)
717 "Like `file-acl' for Tramp files."
718 (with-parsed-tramp-file-name filename nil
719 (with-tramp-file-property v localname "file-acl"
720 (when (executable-find tramp-smb-acl-program)
721
722 (setq tramp-current-method (tramp-file-name-method v)
723 tramp-current-user (tramp-file-name-user v)
724 tramp-current-host (tramp-file-name-real-host v))
725
726 (let* ((real-user (tramp-file-name-real-user v))
727 (real-host (tramp-file-name-real-host v))
728 (domain (tramp-file-name-domain v))
729 (port (tramp-file-name-port v))
730 (share (tramp-smb-get-share v))
731 (localname (replace-regexp-in-string
732 "\\\\" "/" (tramp-smb-get-localname v)))
733 (args (list (concat "//" real-host "/" share) "-E")))
734
735 (if (not (zerop (length real-user)))
736 (setq args (append args (list "-U" real-user)))
737 (setq args (append args (list "-N"))))
738
739 (when domain (setq args (append args (list "-W" domain))))
740 (when port (setq args (append args (list "-p" port))))
741 (when tramp-smb-conf
742 (setq args (append args (list "-s" tramp-smb-conf))))
743 (setq
744 args
745 (append args (list (shell-quote-argument localname) "2>/dev/null")))
746
747 (unwind-protect
748 (with-temp-buffer
749 ;; Set the transfer process properties.
750 (tramp-set-connection-property
751 v "process-name" (buffer-name (current-buffer)))
752 (tramp-set-connection-property
753 v "process-buffer" (current-buffer))
754
755 ;; Use an asynchronous processes. By this, password
756 ;; can be handled.
757 (let ((p (apply
758 'start-process
759 (tramp-get-connection-name v)
760 (tramp-get-connection-buffer v)
761 tramp-smb-acl-program args)))
762
763 (tramp-message
764 v 6 "%s" (mapconcat 'identity (process-command p) " "))
765 (tramp-set-connection-property p "vector" v)
766 (set-process-query-on-exit-flag p nil)
767 (tramp-process-actions p v nil tramp-smb-actions-get-acl)
768 (when (> (point-max) (point-min))
769 (substring-no-properties (buffer-string)))))
770
771 ;; Reset the transfer process properties.
772 (tramp-set-connection-property v "process-name" nil)
773 (tramp-set-connection-property v "process-buffer" nil)))))))
774
775 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
776 "Like `file-attributes' for Tramp files."
777 (unless id-format (setq id-format 'integer))
778 (ignore-errors
779 (with-parsed-tramp-file-name filename nil
780 (with-tramp-file-property
781 v localname (format "file-attributes-%s" id-format)
782 (if (tramp-smb-get-stat-capability v)
783 (tramp-smb-do-file-attributes-with-stat v id-format)
784 ;; Reading just the filename entry via "dir localname" is not
785 ;; possible, because when filename is a directory, some
786 ;; smbclient versions return the content of the directory, and
787 ;; other versions don't. Therefore, the whole content of the
788 ;; upper directory is retrieved, and the entry of the filename
789 ;; is extracted from.
790 (let* ((entries (tramp-smb-get-file-entries
791 (file-name-directory filename)))
792 (entry (assoc (file-name-nondirectory filename) entries))
793 (uid (if (equal id-format 'string) "nobody" -1))
794 (gid (if (equal id-format 'string) "nogroup" -1))
795 (inode (tramp-get-inode v))
796 (device (tramp-get-device v)))
797
798 ;; Check result.
799 (when entry
800 (list (and (string-match "d" (nth 1 entry))
801 t) ;0 file type
802 -1 ;1 link count
803 uid ;2 uid
804 gid ;3 gid
805 '(0 0) ;4 atime
806 (nth 3 entry) ;5 mtime
807 '(0 0) ;6 ctime
808 (nth 2 entry) ;7 size
809 (nth 1 entry) ;8 mode
810 nil ;9 gid weird
811 inode ;10 inode number
812 device)))))))) ;11 file system number
813
814 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
815 "Implement `file-attributes' for Tramp files using stat command."
816 (tramp-message
817 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
818 (with-current-buffer (tramp-get-connection-buffer vec)
819 (let* (size id link uid gid atime mtime ctime mode inode)
820 (when (tramp-smb-send-command
821 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
822
823 ;; Loop the listing.
824 (goto-char (point-min))
825 (unless (re-search-forward tramp-smb-errors nil t)
826 (while (not (eobp))
827 (cond
828 ((looking-at
829 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
830 (setq size (string-to-number (match-string 1))
831 id (if (string-equal "directory" (match-string 2)) t
832 (if (string-equal "symbolic" (match-string 2)) ""))))
833 ((looking-at
834 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
835 (setq inode (string-to-number (match-string 1))
836 link (string-to-number (match-string 2))))
837 ((looking-at
838 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
839 (setq mode (match-string 1)
840 uid (if (equal id-format 'string) (match-string 2)
841 (string-to-number (match-string 2)))
842 gid (if (equal id-format 'string) (match-string 3)
843 (string-to-number (match-string 3)))))
844 ((looking-at
845 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
846 (setq atime
847 (encode-time
848 (string-to-number (match-string 6)) ;; sec
849 (string-to-number (match-string 5)) ;; min
850 (string-to-number (match-string 4)) ;; hour
851 (string-to-number (match-string 3)) ;; day
852 (string-to-number (match-string 2)) ;; month
853 (string-to-number (match-string 1))))) ;; year
854 ((looking-at
855 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
856 (setq mtime
857 (encode-time
858 (string-to-number (match-string 6)) ;; sec
859 (string-to-number (match-string 5)) ;; min
860 (string-to-number (match-string 4)) ;; hour
861 (string-to-number (match-string 3)) ;; day
862 (string-to-number (match-string 2)) ;; month
863 (string-to-number (match-string 1))))) ;; year
864 ((looking-at
865 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
866 (setq ctime
867 (encode-time
868 (string-to-number (match-string 6)) ;; sec
869 (string-to-number (match-string 5)) ;; min
870 (string-to-number (match-string 4)) ;; hour
871 (string-to-number (match-string 3)) ;; day
872 (string-to-number (match-string 2)) ;; month
873 (string-to-number (match-string 1)))))) ;; year
874 (forward-line))
875 ;; Return the result.
876 (list id link uid gid atime mtime ctime size mode nil inode
877 (tramp-get-device vec)))))))
878
879 (defun tramp-smb-handle-file-directory-p (filename)
880 "Like `file-directory-p' for Tramp files."
881 (and (file-exists-p filename)
882 (eq ?d (aref (nth 8 (file-attributes filename)) 0))))
883
884 (defun tramp-smb-handle-file-local-copy (filename)
885 "Like `file-local-copy' for Tramp files."
886 (with-parsed-tramp-file-name filename nil
887 (unless (file-exists-p filename)
888 (tramp-error
889 v 'file-error
890 "Cannot make local copy of non-existing file `%s'" filename))
891 (let ((tmpfile (tramp-compat-make-temp-file filename)))
892 (with-tramp-progress-reporter
893 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
894 (unless (tramp-smb-send-command
895 v (format "get \"%s\" \"%s\""
896 (tramp-smb-get-localname v) tmpfile))
897 ;; Oops, an error. We shall cleanup.
898 (delete-file tmpfile)
899 (tramp-error
900 v 'file-error "Cannot make local copy of file `%s'" filename)))
901 tmpfile)))
902
903 ;; This function should return "foo/" for directories and "bar" for
904 ;; files.
905 (defun tramp-smb-handle-file-name-all-completions (filename directory)
906 "Like `file-name-all-completions' for Tramp files."
907 (all-completions
908 filename
909 (with-parsed-tramp-file-name (expand-file-name directory) nil
910 (with-tramp-file-property v localname "file-name-all-completions"
911 (save-match-data
912 (delete-dups
913 (mapcar
914 (lambda (x)
915 (list
916 (if (string-match "d" (nth 1 x))
917 (file-name-as-directory (nth 0 x))
918 (nth 0 x))))
919 (tramp-smb-get-file-entries directory))))))))
920
921 (defun tramp-smb-handle-file-writable-p (filename)
922 "Like `file-writable-p' for Tramp files."
923 (if (file-exists-p filename)
924 (string-match "w" (or (nth 8 (file-attributes filename)) ""))
925 (let ((dir (file-name-directory filename)))
926 (and (file-exists-p dir)
927 (file-writable-p dir)))))
928
929 (defun tramp-smb-handle-insert-directory
930 (filename switches &optional wildcard full-directory-p)
931 "Like `insert-directory' for Tramp files."
932 (setq filename (expand-file-name filename))
933 (unless switches (setq switches ""))
934 ;; Mark trailing "/".
935 (when (and (zerop (length (file-name-nondirectory filename)))
936 (not full-directory-p))
937 (setq switches (concat switches "F")))
938 (if full-directory-p
939 ;; Called from `dired-add-entry'.
940 (setq filename (file-name-as-directory filename))
941 (setq filename (directory-file-name filename)))
942 (with-parsed-tramp-file-name filename nil
943 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
944 (save-match-data
945 (let ((base (file-name-nondirectory filename))
946 ;; We should not destroy the cache entry.
947 (entries (copy-sequence
948 (tramp-smb-get-file-entries
949 (file-name-directory filename)))))
950
951 (when wildcard
952 (string-match "\\." base)
953 (setq base (replace-match "\\\\." nil nil base))
954 (string-match "\\*" base)
955 (setq base (replace-match ".*" nil nil base))
956 (string-match "\\?" base)
957 (setq base (replace-match ".?" nil nil base)))
958
959 ;; Filter entries.
960 (setq entries
961 (delq
962 nil
963 (if (or wildcard (zerop (length base)))
964 ;; Check for matching entries.
965 (mapcar
966 (lambda (x)
967 (when (string-match
968 (format "^%s" base) (nth 0 x))
969 x))
970 entries)
971 ;; We just need the only and only entry FILENAME.
972 (list (assoc base entries)))))
973
974 ;; Sort entries.
975 (setq entries
976 (sort
977 entries
978 (lambda (x y)
979 (if (string-match "t" switches)
980 ;; Sort by date.
981 (time-less-p (nth 3 y) (nth 3 x))
982 ;; Sort by name.
983 (string-lessp (nth 0 x) (nth 0 y))))))
984
985 ;; Handle "-F" switch.
986 (when (string-match "F" switches)
987 (mapc
988 (lambda (x)
989 (when (not (zerop (length (car x))))
990 (cond
991 ((char-equal ?d (string-to-char (nth 1 x)))
992 (setcar x (concat (car x) "/")))
993 ((char-equal ?x (string-to-char (nth 1 x)))
994 (setcar x (concat (car x) "*"))))))
995 entries))
996
997 ;; Print entries.
998 (mapc
999 (lambda (x)
1000 (when (not (zerop (length (nth 0 x))))
1001 (when (string-match "l" switches)
1002 (let ((attr
1003 (when (tramp-smb-get-stat-capability v)
1004 (ignore-errors
1005 (file-attributes filename 'string)))))
1006 (insert
1007 (format
1008 "%10s %3d %-8s %-8s %8s %s "
1009 (or (nth 8 attr) (nth 1 x)) ; mode
1010 (or (nth 1 attr) 1) ; inode
1011 (or (nth 2 attr) "nobody") ; uid
1012 (or (nth 3 attr) "nogroup") ; gid
1013 (or (nth 7 attr) (nth 2 x)) ; size
1014 (format-time-string
1015 (if (time-less-p (time-subtract (current-time) (nth 3 x))
1016 tramp-half-a-year)
1017 "%b %e %R"
1018 "%b %e %Y")
1019 (nth 3 x)))))) ; date
1020
1021 ;; We mark the file name. The inserted name could be
1022 ;; from somewhere else, so we use the relative file name
1023 ;; of `default-directory'.
1024 (let ((start (point)))
1025 (insert
1026 (format
1027 "%s\n"
1028 (file-relative-name
1029 (expand-file-name
1030 (nth 0 x) (file-name-directory filename))
1031 (when full-directory-p (file-name-directory filename)))))
1032 (put-text-property start (1- (point)) 'dired-filename t))
1033 (forward-line)
1034 (beginning-of-line)))
1035 entries))))))
1036
1037 (defun tramp-smb-handle-make-directory (dir &optional parents)
1038 "Like `make-directory' for Tramp files."
1039 (setq dir (directory-file-name (expand-file-name dir)))
1040 (unless (file-name-absolute-p dir)
1041 (setq dir (expand-file-name dir default-directory)))
1042 (with-parsed-tramp-file-name dir nil
1043 (save-match-data
1044 (let* ((ldir (file-name-directory dir)))
1045 ;; Make missing directory parts.
1046 (when (and parents
1047 (tramp-smb-get-share v)
1048 (not (file-directory-p ldir)))
1049 (make-directory ldir parents))
1050 ;; Just do it.
1051 (when (file-directory-p ldir)
1052 (make-directory-internal dir))
1053 (unless (file-directory-p dir)
1054 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
1055
1056 (defun tramp-smb-handle-make-directory-internal (directory)
1057 "Like `make-directory-internal' for Tramp files."
1058 (setq directory (directory-file-name (expand-file-name directory)))
1059 (unless (file-name-absolute-p directory)
1060 (setq directory (expand-file-name directory default-directory)))
1061 (with-parsed-tramp-file-name directory nil
1062 (save-match-data
1063 (let* ((file (tramp-smb-get-localname v)))
1064 (when (file-directory-p (file-name-directory directory))
1065 (tramp-smb-send-command
1066 v
1067 (if (tramp-smb-get-cifs-capabilities v)
1068 (format "posix_mkdir \"%s\" %o" file (default-file-modes))
1069 (format "mkdir \"%s\"" file)))
1070 ;; We must also flush the cache of the directory, because
1071 ;; `file-attributes' reads the values from there.
1072 (tramp-flush-file-property v (file-name-directory localname))
1073 (tramp-flush-file-property v localname))
1074 (unless (file-directory-p directory)
1075 (tramp-error
1076 v 'file-error "Couldn't make directory %s" directory))))))
1077
1078 (defun tramp-smb-handle-make-symbolic-link
1079 (filename linkname &optional ok-if-already-exists)
1080 "Like `make-symbolic-link' for Tramp files.
1081 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1082 the symlink. If LINKNAME is a Tramp file, only the localname component is
1083 used as the target of the symlink.
1084
1085 If LINKNAME is a Tramp file and the localname component is relative, then
1086 it is expanded first, before the localname component is taken. Note that
1087 this can give surprising results if the user/host for the source and
1088 target of the symlink differ."
1089 (unless (tramp-equal-remote filename linkname)
1090 (with-parsed-tramp-file-name
1091 (if (tramp-tramp-file-p filename) filename linkname) nil
1092 (tramp-error
1093 v 'file-error
1094 "make-symbolic-link: %s"
1095 "only implemented for same method, same user, same host")))
1096 (with-parsed-tramp-file-name filename v1
1097 (with-parsed-tramp-file-name linkname v2
1098 (when (file-directory-p filename)
1099 (tramp-error
1100 v2 'file-error
1101 "make-symbolic-link: %s must not be a directory" filename))
1102 (when (and (not ok-if-already-exists)
1103 (file-exists-p linkname)
1104 (not (numberp ok-if-already-exists))
1105 (y-or-n-p
1106 (format
1107 "File %s already exists; make it a new name anyway? "
1108 linkname)))
1109 (tramp-error
1110 v2 'file-error
1111 "make-symbolic-link: file %s already exists" linkname))
1112 (unless (tramp-smb-get-cifs-capabilities v1)
1113 (tramp-error v2 'file-error "make-symbolic-link not supported"))
1114 ;; We must also flush the cache of the directory, because
1115 ;; `file-attributes' reads the values from there.
1116 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1117 (tramp-flush-file-property v2 v2-localname)
1118 (unless
1119 (tramp-smb-send-command
1120 v1
1121 (format
1122 "symlink \"%s\" \"%s\""
1123 (tramp-smb-get-localname v1)
1124 (tramp-smb-get-localname v2)))
1125 (tramp-error
1126 v2 'file-error
1127 "error with make-symbolic-link, see buffer `%s' for details"
1128 (buffer-name))))))
1129
1130 (defun tramp-smb-handle-process-file
1131 (program &optional infile destination display &rest args)
1132 "Like `process-file' for Tramp files."
1133 ;; The implementation is not complete yet.
1134 (when (and (numberp destination) (zerop destination))
1135 (error "Implementation does not handle immediate return"))
1136
1137 (with-parsed-tramp-file-name default-directory nil
1138 (let* ((name (file-name-nondirectory program))
1139 (name1 name)
1140 (i 0)
1141 input tmpinput outbuf command ret)
1142
1143 ;; Determine input.
1144 (when infile
1145 (setq infile (expand-file-name infile))
1146 (if (tramp-equal-remote default-directory infile)
1147 ;; INFILE is on the same remote host.
1148 (setq input (with-parsed-tramp-file-name infile nil localname))
1149 ;; INFILE must be copied to remote host.
1150 (setq input (tramp-make-tramp-temp-file v)
1151 tmpinput (tramp-make-tramp-file-name method user host input))
1152 (copy-file infile tmpinput t))
1153 ;; Transform input into a filename powershell does understand.
1154 (setq input (format "//%s%s" host input)))
1155
1156 ;; Determine output.
1157 (cond
1158 ;; Just a buffer.
1159 ((bufferp destination)
1160 (setq outbuf destination))
1161 ;; A buffer name.
1162 ((stringp destination)
1163 (setq outbuf (get-buffer-create destination)))
1164 ;; (REAL-DESTINATION ERROR-DESTINATION)
1165 ((consp destination)
1166 ;; output.
1167 (cond
1168 ((bufferp (car destination))
1169 (setq outbuf (car destination)))
1170 ((stringp (car destination))
1171 (setq outbuf (get-buffer-create (car destination))))
1172 ((car destination)
1173 (setq outbuf (current-buffer))))
1174 ;; stderr.
1175 (tramp-message v 2 "%s" "STDERR not supported"))
1176 ;; 't
1177 (destination
1178 (setq outbuf (current-buffer))))
1179
1180 ;; Construct command.
1181 (setq command (mapconcat 'identity (cons program args) " ")
1182 command (if input
1183 (format
1184 "get-content %s | & %s"
1185 (tramp-smb-shell-quote-argument input) command)
1186 (format "& %s" command)))
1187
1188 (while (get-process name1)
1189 ;; NAME must be unique as process name.
1190 (setq i (1+ i)
1191 name1 (format "%s<%d>" name i)))
1192
1193 ;; Set the new process properties.
1194 (tramp-set-connection-property v "process-name" name1)
1195 (tramp-set-connection-property
1196 v "process-buffer"
1197 (or outbuf (generate-new-buffer tramp-temp-buffer-name)))
1198
1199 ;; Call it.
1200 (condition-case nil
1201 (with-current-buffer (tramp-get-connection-buffer v)
1202 ;; Preserve buffer contents.
1203 (narrow-to-region (point-max) (point-max))
1204 (tramp-smb-call-winexe v)
1205 (when (tramp-smb-get-share v)
1206 (tramp-smb-send-command
1207 v (format "cd \"//%s%s\"" host (file-name-directory localname))))
1208 (tramp-smb-send-command v command)
1209 ;; Preserve command output.
1210 (narrow-to-region (point-max) (point-max))
1211 (let ((p (tramp-get-connection-process v)))
1212 (tramp-smb-send-command v "exit $lasterrorcode")
1213 (while (memq (process-status p) '(run open))
1214 (sleep-for 0.1)
1215 (setq ret (process-exit-status p))))
1216 (delete-region (point-min) (point-max))
1217 (widen))
1218
1219 ;; When the user did interrupt, we should do it also. We use
1220 ;; return code -1 as marker.
1221 (quit
1222 (setq ret -1))
1223 ;; Handle errors.
1224 (error
1225 (setq ret 1)))
1226
1227 ;; We should redisplay the output.
1228 (when (and display outbuf (get-buffer-window outbuf t)) (redisplay))
1229
1230 ;; Cleanup. We remove all file cache values for the connection,
1231 ;; because the remote process could have changed them.
1232 (tramp-set-connection-property v "process-name" nil)
1233 (tramp-set-connection-property v "process-buffer" nil)
1234 (when tmpinput (delete-file tmpinput))
1235 (unless outbuf
1236 (kill-buffer (tramp-get-connection-property v "process-buffer" nil)))
1237
1238 (unless process-file-side-effects
1239 (tramp-flush-directory-property v ""))
1240
1241 ;; Return exit status.
1242 (if (equal ret -1)
1243 (keyboard-quit)
1244 ret))))
1245
1246 (defun tramp-smb-handle-rename-file
1247 (filename newname &optional ok-if-already-exists)
1248 "Like `rename-file' for Tramp files."
1249 (setq filename (expand-file-name filename)
1250 newname (expand-file-name newname))
1251
1252 (when (and (not ok-if-already-exists)
1253 (file-exists-p newname))
1254 (tramp-error
1255 (tramp-dissect-file-name
1256 (if (tramp-tramp-file-p filename) filename newname))
1257 'file-already-exists newname))
1258
1259 (with-tramp-progress-reporter
1260 (tramp-dissect-file-name
1261 (if (tramp-tramp-file-p filename) filename newname))
1262 0 (format "Renaming %s to %s" filename newname)
1263
1264 (if (and (not (file-exists-p newname))
1265 (tramp-equal-remote filename newname)
1266 (string-equal
1267 (tramp-smb-get-share (tramp-dissect-file-name filename))
1268 (tramp-smb-get-share (tramp-dissect-file-name newname))))
1269 ;; We can rename directly.
1270 (with-parsed-tramp-file-name filename v1
1271 (with-parsed-tramp-file-name newname v2
1272
1273 ;; We must also flush the cache of the directory, because
1274 ;; `file-attributes' reads the values from there.
1275 (tramp-flush-file-property v1 (file-name-directory v1-localname))
1276 (tramp-flush-file-property v1 v1-localname)
1277 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1278 (tramp-flush-file-property v2 v2-localname)
1279 (unless (tramp-smb-get-share v2)
1280 (tramp-error
1281 v2 'file-error "Target `%s' must contain a share name" newname))
1282 (unless (tramp-smb-send-command
1283 v2 (format "rename \"%s\" \"%s\""
1284 (tramp-smb-get-localname v1)
1285 (tramp-smb-get-localname v2)))
1286 (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
1287
1288 ;; We must rename via copy.
1289 (copy-file
1290 filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
1291 (if (file-directory-p filename)
1292 (delete-directory filename 'recursive)
1293 (delete-file filename)))))
1294
1295 (defun tramp-smb-action-set-acl (proc vec)
1296 "Read ACL data from connection buffer."
1297 (when (not (memq (process-status proc) '(run open)))
1298 ;; Accept pending output.
1299 (while (tramp-accept-process-output proc 0.1))
1300 (with-current-buffer (tramp-get-connection-buffer vec)
1301 (tramp-message vec 10 "\n%s" (buffer-string))
1302 (throw 'tramp-action 'ok))))
1303
1304 (defun tramp-smb-handle-set-file-acl (filename acl-string)
1305 "Like `set-file-acl' for Tramp files."
1306 (ignore-errors
1307 (with-parsed-tramp-file-name filename nil
1308 (when (and (stringp acl-string) (executable-find tramp-smb-acl-program))
1309 (setq tramp-current-method (tramp-file-name-method v)
1310 tramp-current-user (tramp-file-name-user v)
1311 tramp-current-host (tramp-file-name-real-host v))
1312 (tramp-set-file-property v localname "file-acl" 'undef)
1313
1314 (let* ((real-user (tramp-file-name-real-user v))
1315 (real-host (tramp-file-name-real-host v))
1316 (domain (tramp-file-name-domain v))
1317 (port (tramp-file-name-port v))
1318 (share (tramp-smb-get-share v))
1319 (localname (replace-regexp-in-string
1320 "\\\\" "/" (tramp-smb-get-localname v)))
1321 (args (list (concat "//" real-host "/" share) "-E" "-S"
1322 (replace-regexp-in-string
1323 "\n" "," acl-string))))
1324
1325 (if (not (zerop (length real-user)))
1326 (setq args (append args (list "-U" real-user)))
1327 (setq args (append args (list "-N"))))
1328
1329 (when domain (setq args (append args (list "-W" domain))))
1330 (when port (setq args (append args (list "-p" port))))
1331 (when tramp-smb-conf
1332 (setq args (append args (list "-s" tramp-smb-conf))))
1333 (setq
1334 args
1335 (append args (list (shell-quote-argument localname)
1336 "&&" "echo" "tramp_exit_status" "0"
1337 "||" "echo" "tramp_exit_status" "1")))
1338
1339 (unwind-protect
1340 (with-temp-buffer
1341 ;; Set the transfer process properties.
1342 (tramp-set-connection-property
1343 v "process-name" (buffer-name (current-buffer)))
1344 (tramp-set-connection-property
1345 v "process-buffer" (current-buffer))
1346
1347 ;; Use an asynchronous processes. By this, password can
1348 ;; be handled.
1349 (let ((p (apply
1350 'start-process
1351 (tramp-get-connection-name v)
1352 (tramp-get-connection-buffer v)
1353 tramp-smb-acl-program args)))
1354
1355 (tramp-message
1356 v 6 "%s" (mapconcat 'identity (process-command p) " "))
1357 (tramp-set-connection-property p "vector" v)
1358 (set-process-query-on-exit-flag p nil)
1359 (tramp-process-actions p v nil tramp-smb-actions-set-acl)
1360 (goto-char (point-max))
1361 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1362 (tramp-error
1363 v 'file-error
1364 "Couldn't find exit status of `%s'" tramp-smb-acl-program))
1365 (skip-chars-forward "^ ")
1366 (when (zerop (read (current-buffer)))
1367 ;; Success.
1368 (tramp-set-file-property v localname "file-acl" acl-string)
1369 t)))
1370
1371 ;; Reset the transfer process properties.
1372 (tramp-set-connection-property v "process-name" nil)
1373 (tramp-set-connection-property v "process-buffer" nil)))))))
1374
1375 (defun tramp-smb-handle-set-file-modes (filename mode)
1376 "Like `set-file-modes' for Tramp files."
1377 (with-parsed-tramp-file-name filename nil
1378 (when (tramp-smb-get-cifs-capabilities v)
1379 (tramp-flush-file-property v localname)
1380 (unless (tramp-smb-send-command
1381 v (format "chmod \"%s\" %o" (tramp-smb-get-localname v) mode))
1382 (tramp-error
1383 v 'file-error "Error while changing file's mode %s" filename)))))
1384
1385 ;; We use BUFFER also as connection buffer during setup. Because of
1386 ;; this, its original contents must be saved, and restored once
1387 ;; connection has been setup.
1388 (defun tramp-smb-handle-start-file-process (name buffer program &rest args)
1389 "Like `start-file-process' for Tramp files."
1390 (with-parsed-tramp-file-name default-directory nil
1391 (let* ((buffer
1392 (if buffer
1393 (get-buffer-create buffer)
1394 ;; BUFFER can be nil. We use a temporary buffer.
1395 (generate-new-buffer tramp-temp-buffer-name)))
1396 (command (mapconcat 'identity (cons program args) " "))
1397 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
1398 (name1 name)
1399 (i 0))
1400 (unwind-protect
1401 (save-excursion
1402 (save-restriction
1403 (while (get-process name1)
1404 ;; NAME must be unique as process name.
1405 (setq i (1+ i)
1406 name1 (format "%s<%d>" name i)))
1407 ;; Set the new process properties.
1408 (tramp-set-connection-property v "process-name" name1)
1409 (tramp-set-connection-property v "process-buffer" buffer)
1410 ;; Activate narrowing in order to save BUFFER contents.
1411 (with-current-buffer (tramp-get-connection-buffer v)
1412 (let ((buffer-undo-list t))
1413 (narrow-to-region (point-max) (point-max))
1414 (tramp-smb-call-winexe v)
1415 (when (tramp-smb-get-share v)
1416 (tramp-smb-send-command
1417 v (format
1418 "cd \"//%s%s\""
1419 host (file-name-directory localname))))
1420 (tramp-message v 6 "(%s); exit" command)
1421 (tramp-send-string v command)))
1422 ;; Return value.
1423 (tramp-get-connection-process v)))
1424
1425 ;; Save exit.
1426 (with-current-buffer (tramp-get-connection-buffer v)
1427 (if (string-match tramp-temp-buffer-name (buffer-name))
1428 (progn
1429 (set-process-buffer (tramp-get-connection-process v) nil)
1430 (kill-buffer (current-buffer)))
1431 (set-buffer-modified-p bmp)))
1432 (tramp-set-connection-property v "process-name" nil)
1433 (tramp-set-connection-property v "process-buffer" nil)))))
1434
1435 (defun tramp-smb-handle-substitute-in-file-name (filename)
1436 "Like `handle-substitute-in-file-name' for Tramp files.
1437 \"//\" substitutes only in the local filename part. Catches
1438 errors for shares like \"C$/\", which are common in Microsoft Windows."
1439 (with-parsed-tramp-file-name filename nil
1440 ;; Ignore in LOCALNAME everything before "//".
1441 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
1442 (setq filename
1443 (concat (file-remote-p filename)
1444 (replace-match "\\1" nil nil localname)))))
1445 (condition-case nil
1446 (tramp-run-real-handler 'substitute-in-file-name (list filename))
1447 (error filename)))
1448
1449 (defun tramp-smb-handle-write-region
1450 (start end filename &optional append visit lockname confirm)
1451 "Like `write-region' for Tramp files."
1452 (setq filename (expand-file-name filename))
1453 (with-parsed-tramp-file-name filename nil
1454 (when (and confirm (file-exists-p filename))
1455 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1456 filename))
1457 (tramp-error v 'file-error "File not overwritten")))
1458 ;; We must also flush the cache of the directory, because
1459 ;; `file-attributes' reads the values from there.
1460 (tramp-flush-file-property v (file-name-directory localname))
1461 (tramp-flush-file-property v localname)
1462 (let ((curbuf (current-buffer))
1463 (tmpfile (tramp-compat-make-temp-file filename)))
1464 (when (and append (file-exists-p filename))
1465 (copy-file filename tmpfile 'ok))
1466 ;; We say `no-message' here because we don't want the visited file
1467 ;; modtime data to be clobbered from the temp file. We call
1468 ;; `set-visited-file-modtime' ourselves later on.
1469 (tramp-run-real-handler
1470 'write-region
1471 (if confirm ; don't pass this arg unless defined for backward compat.
1472 (list start end tmpfile append 'no-message lockname confirm)
1473 (list start end tmpfile append 'no-message lockname)))
1474
1475 (with-tramp-progress-reporter
1476 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
1477 (unwind-protect
1478 (unless (tramp-smb-send-command
1479 v (format "put %s \"%s\""
1480 tmpfile (tramp-smb-get-localname v)))
1481 (tramp-error v 'file-error "Cannot write `%s'" filename))
1482 (delete-file tmpfile)))
1483
1484 (unless (equal curbuf (current-buffer))
1485 (tramp-error
1486 v 'file-error
1487 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
1488 (when (eq visit t)
1489 (set-visited-file-modtime)))))
1490
1491
1492 ;; Internal file name functions.
1493
1494 (defun tramp-smb-get-share (vec)
1495 "Returns the share name of LOCALNAME."
1496 (save-match-data
1497 (let ((localname (tramp-file-name-localname vec)))
1498 (when (string-match "^/?\\([^/]+\\)/" localname)
1499 (match-string 1 localname)))))
1500
1501 (defun tramp-smb-get-localname (vec)
1502 "Returns the file name of LOCALNAME.
1503 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1504 (save-match-data
1505 (let ((localname (tramp-file-name-localname vec)))
1506 (setq
1507 localname
1508 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
1509 ;; There is a share, separated by "/".
1510 (if (not (tramp-smb-get-cifs-capabilities vec))
1511 (mapconcat
1512 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
1513 (match-string 1 localname) "")
1514 (match-string 1 localname))
1515 ;; There is just a share.
1516 (if (string-match "^/?\\([^/]+\\)$" localname)
1517 (match-string 1 localname)
1518 "")))
1519
1520 ;; Sometimes we have discarded `substitute-in-file-name'.
1521 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
1522 (setq localname (replace-match "$" nil nil localname 1)))
1523
1524 localname)))
1525
1526 ;; Share names of a host are cached. It is very unlikely that the
1527 ;; shares do change during connection.
1528 (defun tramp-smb-get-file-entries (directory)
1529 "Read entries which match DIRECTORY.
1530 Either the shares are listed, or the `dir' command is executed.
1531 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1532 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
1533 (setq localname (or localname "/"))
1534 (with-tramp-file-property v localname "file-entries"
1535 (with-current-buffer (tramp-get-connection-buffer v)
1536 (let* ((share (tramp-smb-get-share v))
1537 (cache (tramp-get-connection-property v "share-cache" nil))
1538 res entry)
1539
1540 (if (and (not share) cache)
1541 ;; Return cached shares.
1542 (setq res cache)
1543
1544 ;; Read entries.
1545 (if share
1546 (tramp-smb-send-command
1547 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
1548 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1549 (tramp-smb-maybe-open-connection v))
1550
1551 ;; Loop the listing.
1552 (goto-char (point-min))
1553 (if (re-search-forward tramp-smb-errors nil t)
1554 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1555 (while (not (eobp))
1556 (setq entry (tramp-smb-read-file-entry share))
1557 (forward-line)
1558 (when entry (push entry res))))
1559
1560 ;; Cache share entries.
1561 (unless share
1562 (tramp-set-connection-property v "share-cache" res)))
1563
1564 ;; Add directory itself.
1565 (push '("" "drwxrwxrwx" 0 (0 0)) res)
1566
1567 ;; Return entries.
1568 (delq nil res))))))
1569
1570 ;; Return either a share name (if SHARE is nil), or a file name.
1571 ;;
1572 ;; If shares are listed, the following format is expected:
1573 ;;
1574 ;; Disk| - leading spaces
1575 ;; [^|]+| - share name, 14 char
1576 ;; .* - comment
1577 ;;
1578 ;; Entries provided by smbclient DIR aren't fully regular.
1579 ;; They should have the format
1580 ;;
1581 ;; \s-\{2,2} - leading spaces
1582 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1583 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1584 ;; \s- - space delimiter
1585 ;; \s-+[0-9]+ - size, 8 chars, right bound
1586 ;; \s-\{2,2\} - space delimiter
1587 ;; \w\{3,3\} - weekday
1588 ;; \s- - space delimiter
1589 ;; \w\{3,3\} - month
1590 ;; \s- - space delimiter
1591 ;; [ 12][0-9] - day
1592 ;; \s- - space delimiter
1593 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1594 ;; \s- - space delimiter
1595 ;; [0-9]\{4,4\} - year
1596 ;;
1597 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1598 ;; has function display_finfo:
1599 ;;
1600 ;; d_printf(" %-30s%7.7s %8.0f %s",
1601 ;; finfo->name,
1602 ;; attrib_string(finfo->mode),
1603 ;; (double)finfo->size,
1604 ;; asctime(LocalTime(&t)));
1605 ;;
1606 ;; in Samba 1.9, there's the following code:
1607 ;;
1608 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1609 ;; CNV_LANG(finfo->name),
1610 ;; attrib_string(finfo->mode),
1611 ;; finfo->size,
1612 ;; asctime(LocalTime(&t))));
1613 ;;
1614 ;; Problems:
1615 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1616 ;; available in older Emacsen.
1617 ;; * The length of constructs (file name, size) might exceed the default.
1618 ;; * File names might contain spaces.
1619 ;; * Permissions might be empty.
1620 ;;
1621 ;; So we try to analyze backwards.
1622 (defun tramp-smb-read-file-entry (share)
1623 "Parse entry in SMB output buffer.
1624 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1625 Result is the list (LOCALNAME MODE SIZE MTIME)."
1626 ;; We are called from `tramp-smb-get-file-entries', which sets the
1627 ;; current buffer.
1628 (let ((line (buffer-substring (point) (point-at-eol)))
1629 localname mode size month day hour min sec year mtime)
1630
1631 (if (not share)
1632
1633 ;; Read share entries.
1634 (when (string-match "^Disk|\\([^|]+\\)|" line)
1635 (setq localname (match-string 1 line)
1636 mode "dr-xr-xr-x"
1637 size 0))
1638
1639 ;; Real listing.
1640 (block nil
1641
1642 ;; year.
1643 (if (string-match "\\([0-9]+\\)$" line)
1644 (setq year (string-to-number (match-string 1 line))
1645 line (substring line 0 -5))
1646 (return))
1647
1648 ;; time.
1649 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1650 (setq hour (string-to-number (match-string 1 line))
1651 min (string-to-number (match-string 2 line))
1652 sec (string-to-number (match-string 3 line))
1653 line (substring line 0 -9))
1654 (return))
1655
1656 ;; day.
1657 (if (string-match "\\([0-9]+\\)$" line)
1658 (setq day (string-to-number (match-string 1 line))
1659 line (substring line 0 -3))
1660 (return))
1661
1662 ;; month.
1663 (if (string-match "\\(\\w+\\)$" line)
1664 (setq month (match-string 1 line)
1665 line (substring line 0 -4))
1666 (return))
1667
1668 ;; weekday.
1669 (if (string-match "\\(\\w+\\)$" line)
1670 (setq line (substring line 0 -5))
1671 (return))
1672
1673 ;; size.
1674 (if (string-match "\\([0-9]+\\)$" line)
1675 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1676 (setq size (string-to-number (match-string 1 line)))
1677 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1678 (setq length (+ length (match-end 0))))
1679 (setq line (substring line 0 length)))
1680 (return))
1681
1682 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1683 (if (string-match "\\([ADHRSV]+\\)?$" line)
1684 (setq
1685 mode (or (match-string 1 line) "")
1686 mode (save-match-data (format
1687 "%s%s"
1688 (if (string-match "D" mode) "d" "-")
1689 (mapconcat
1690 (lambda (_x) "") " "
1691 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1692 line (substring line 0 -6))
1693 (return))
1694
1695 ;; localname.
1696 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1697 (setq localname (match-string 1 line))
1698 (return))))
1699
1700 (when (and localname mode size)
1701 (setq mtime
1702 (if (and sec min hour day month year)
1703 (encode-time
1704 sec min hour day
1705 (cdr (assoc (downcase month) tramp-parse-time-months))
1706 year)
1707 '(0 0)))
1708 (list localname mode size mtime))))
1709
1710 (defun tramp-smb-get-cifs-capabilities (vec)
1711 "Check, whether the SMB server supports POSIX commands."
1712 ;; When we are not logged in yet, we return nil.
1713 (if (let ((p (tramp-get-connection-process vec)))
1714 (and p (processp p) (memq (process-status p) '(run open))))
1715 (with-tramp-connection-property
1716 (tramp-get-connection-process vec) "cifs-capabilities"
1717 (save-match-data
1718 (when (tramp-smb-send-command vec "posix")
1719 (with-current-buffer (tramp-get-connection-buffer vec)
1720 (goto-char (point-min))
1721 (when
1722 (re-search-forward "Server supports CIFS capabilities" nil t)
1723 (member
1724 "pathnames"
1725 (split-string
1726 (buffer-substring (point) (point-at-eol)) nil 'omit)))))))))
1727
1728 (defun tramp-smb-get-stat-capability (vec)
1729 "Check, whether the SMB server supports the STAT command."
1730 ;; When we are not logged in yet, we return nil.
1731 (if (and (tramp-smb-get-share vec)
1732 (let ((p (tramp-get-connection-process vec)))
1733 (and p (processp p) (memq (process-status p) '(run open)))))
1734 (with-tramp-connection-property
1735 (tramp-get-connection-process vec) "stat-capability"
1736 (tramp-smb-send-command vec "stat \"/\""))))
1737
1738
1739 ;; Connection functions.
1740
1741 (defun tramp-smb-send-command (vec command)
1742 "Send the COMMAND to connection VEC.
1743 Returns nil if there has been an error message from smbclient."
1744 (tramp-smb-maybe-open-connection vec)
1745 (tramp-message vec 6 "%s" command)
1746 (tramp-send-string vec command)
1747 (tramp-smb-wait-for-output vec))
1748
1749 (defun tramp-smb-maybe-open-connection (vec &optional argument)
1750 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1751 Does not do anything if a connection is already open, but re-opens the
1752 connection if a previous connection has died for some reason.
1753 If ARGUMENT is non-nil, use it as argument for
1754 `tramp-smb-winexe-program', and suppress any checks."
1755 (tramp-check-proper-method-and-host vec)
1756
1757 (let* ((share (tramp-smb-get-share vec))
1758 (buf (tramp-get-connection-buffer vec))
1759 (p (get-buffer-process buf)))
1760
1761 ;; Check whether we still have the same smbclient version.
1762 ;; Otherwise, we must delete the connection cache, because
1763 ;; capabilities migh have changed.
1764 (unless (or argument (processp p))
1765 (let ((default-directory (tramp-compat-temporary-file-directory))
1766 (command (concat tramp-smb-program " -V")))
1767
1768 (unless tramp-smb-version
1769 (unless (executable-find tramp-smb-program)
1770 (tramp-error
1771 vec 'file-error
1772 "Cannot find command %s in %s" tramp-smb-program exec-path))
1773 (setq tramp-smb-version (shell-command-to-string command))
1774 (tramp-message vec 6 command)
1775 (tramp-message vec 6 "\n%s" tramp-smb-version)
1776 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1777 (setq tramp-smb-version
1778 (replace-match "" nil nil tramp-smb-version))))
1779
1780 (unless (string-equal
1781 tramp-smb-version
1782 (tramp-get-connection-property
1783 vec "smbclient-version" tramp-smb-version))
1784 (tramp-flush-directory-property vec "")
1785 (tramp-flush-connection-property vec))
1786
1787 (tramp-set-connection-property
1788 vec "smbclient-version" tramp-smb-version)))
1789
1790 ;; If too much time has passed since last command was sent, look
1791 ;; whether there has been an error message; maybe due to
1792 ;; connection timeout.
1793 (with-current-buffer buf
1794 (goto-char (point-min))
1795 (when (and (> (tramp-time-diff
1796 (current-time)
1797 (tramp-get-connection-property
1798 p "last-cmd-time" '(0 0 0)))
1799 60)
1800 p (processp p) (memq (process-status p) '(run open))
1801 (re-search-forward tramp-smb-errors nil t))
1802 (delete-process p)
1803 (setq p nil)))
1804
1805 ;; Check whether it is still the same share.
1806 (unless
1807 (and p (processp p) (memq (process-status p) '(run open))
1808 (or argument
1809 (string-equal
1810 share
1811 (tramp-get-connection-property p "smb-share" ""))))
1812
1813 (save-match-data
1814 ;; There might be unread output from checking for share names.
1815 (when buf (with-current-buffer buf (erase-buffer)))
1816 (when (and p (processp p)) (delete-process p))
1817
1818 (let* ((user (tramp-file-name-user vec))
1819 (host (tramp-file-name-host vec))
1820 (real-user (tramp-file-name-real-user vec))
1821 (real-host (tramp-file-name-real-host vec))
1822 (domain (tramp-file-name-domain vec))
1823 (port (tramp-file-name-port vec))
1824 args)
1825
1826 (cond
1827 (argument
1828 (setq args (list (concat "//" real-host))))
1829 (share
1830 (setq args (list (concat "//" real-host "/" share))))
1831 (t
1832 (setq args (list "-g" "-L" real-host ))))
1833
1834 (if (not (zerop (length real-user)))
1835 (setq args (append args (list "-U" real-user)))
1836 (setq args (append args (list "-N"))))
1837
1838 (when domain (setq args (append args (list "-W" domain))))
1839 (when port (setq args (append args (list "-p" port))))
1840 (when tramp-smb-conf
1841 (setq args (append args (list "-s" tramp-smb-conf))))
1842 (when argument
1843 (setq args (append args (list argument))))
1844
1845 ;; OK, let's go.
1846 (with-tramp-progress-reporter
1847 vec 3
1848 (format "Opening connection for //%s%s/%s"
1849 (if (not (zerop (length user))) (concat user "@") "")
1850 host (or share ""))
1851
1852 (let* ((coding-system-for-read nil)
1853 (process-connection-type tramp-process-connection-type)
1854 (p (let ((default-directory
1855 (tramp-compat-temporary-file-directory)))
1856 (apply #'start-process
1857 (tramp-get-connection-name vec)
1858 (tramp-get-connection-buffer vec)
1859 (if argument
1860 tramp-smb-winexe-program tramp-smb-program)
1861 args))))
1862
1863 (tramp-message
1864 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1865 (tramp-set-connection-property p "vector" vec)
1866 (set-process-query-on-exit-flag p nil)
1867
1868 ;; Set variables for computing the prompt for reading password.
1869 (setq tramp-current-method tramp-smb-method
1870 tramp-current-user user
1871 tramp-current-host host)
1872
1873 (condition-case err
1874 (let (tramp-message-show-message)
1875 ;; Play login scenario.
1876 (tramp-process-actions
1877 p vec nil
1878 (if (or argument share)
1879 tramp-smb-actions-with-share
1880 tramp-smb-actions-without-share))
1881
1882 ;; Check server version.
1883 (unless argument
1884 (with-current-buffer (tramp-get-connection-buffer vec)
1885 (goto-char (point-min))
1886 (search-forward-regexp tramp-smb-server-version nil t)
1887 (let ((smbserver-version (match-string 0)))
1888 (unless
1889 (string-equal
1890 smbserver-version
1891 (tramp-get-connection-property
1892 vec "smbserver-version" smbserver-version))
1893 (tramp-flush-directory-property vec "")
1894 (tramp-flush-connection-property vec))
1895 (tramp-set-connection-property
1896 vec "smbserver-version" smbserver-version))))
1897
1898 ;; Set chunksize to 1. smbclient reads its input
1899 ;; character by character; if we send the string
1900 ;; at once, it is read painfully slow.
1901 (tramp-set-connection-property p "smb-share" share)
1902 (tramp-set-connection-property p "chunksize" 1)
1903
1904 ;; Mark it as connected.
1905 (tramp-set-connection-property p "connected" t))
1906
1907 ;; Check for the error reason. If it was due to wrong
1908 ;; password, reestablish the connection. We cannot
1909 ;; handle this in `tramp-process-actions', because
1910 ;; smbclient does not ask for the password, again.
1911 (error
1912 (with-current-buffer (tramp-get-connection-buffer vec)
1913 (goto-char (point-min))
1914 (if (and (boundp 'auth-sources)
1915 (symbol-value 'auth-sources)
1916 (search-forward-regexp
1917 tramp-smb-wrong-passwd-regexp nil t))
1918 ;; Disable `auth-source' and `password-cache'.
1919 (let (auth-sources)
1920 (tramp-message
1921 vec 3 "Retry connection with new password")
1922 (tramp-cleanup-connection vec t)
1923 (tramp-smb-maybe-open-connection vec argument))
1924 ;; Propagate the error.
1925 (signal (car err) (cdr err)))))))))))))
1926
1927 ;; We don't use timeouts. If needed, the caller shall wrap around.
1928 (defun tramp-smb-wait-for-output (vec)
1929 "Wait for output from smbclient command.
1930 Returns nil if an error message has appeared."
1931 (with-current-buffer (tramp-get-connection-buffer vec)
1932 (let ((p (get-buffer-process (current-buffer)))
1933 (found (progn (goto-char (point-min))
1934 (re-search-forward tramp-smb-prompt nil t)))
1935 (err (progn (goto-char (point-min))
1936 (re-search-forward tramp-smb-errors nil t)))
1937 buffer-read-only)
1938
1939 ;; Algorithm: get waiting output. See if last line contains
1940 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1941 ;; If not, wait a bit and again get waiting output.
1942 (while (and (not found) (not err) (memq (process-status p) '(run open)))
1943
1944 ;; Accept pending output.
1945 (tramp-accept-process-output p 0.1)
1946
1947 ;; Search for prompt.
1948 (goto-char (point-min))
1949 (setq found (re-search-forward tramp-smb-prompt nil t))
1950
1951 ;; Search for errors.
1952 (goto-char (point-min))
1953 (setq err (re-search-forward tramp-smb-errors nil t)))
1954
1955 ;; When the process is still alive, read pending output.
1956 (while (and (not found) (memq (process-status p) '(run open)))
1957
1958 ;; Accept pending output.
1959 (tramp-accept-process-output p 0.1)
1960
1961 ;; Search for prompt.
1962 (goto-char (point-min))
1963 (setq found (re-search-forward tramp-smb-prompt nil t)))
1964
1965 (tramp-message vec 6 "\n%s" (buffer-string))
1966
1967 ;; Remove prompt.
1968 (when found
1969 (goto-char (point-max))
1970 (re-search-backward tramp-smb-prompt nil t)
1971 (delete-region (point) (point-max)))
1972
1973 ;; Return value is whether no error message has appeared.
1974 (not err))))
1975
1976 (defun tramp-smb-kill-winexe-function ()
1977 "Send SIGKILL to the winexe process."
1978 (ignore-errors
1979 (let ((p (get-buffer-process (current-buffer))))
1980 (when (and p (processp p) (memq (process-status p) '(run open)))
1981 (signal-process (process-id p) 'SIGINT)))))
1982
1983 (defun tramp-smb-call-winexe (vec)
1984 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
1985
1986 ;; Check for program.
1987 (unless (executable-find tramp-smb-winexe-program)
1988 (tramp-error
1989 vec 'file-error "Cannot find program: %s" tramp-smb-winexe-program))
1990
1991 ;; winexe does not supports ports.
1992 (when (tramp-file-name-port vec)
1993 (tramp-error vec 'file-error "Port not supported for remote processes"))
1994
1995 (tramp-smb-maybe-open-connection
1996 vec
1997 (format
1998 "%s %s"
1999 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch))
2000
2001 (set (make-local-variable 'kill-buffer-hook)
2002 '(tramp-smb-kill-winexe-function))
2003
2004 ;; Suppress "^M". Shouldn't we specify utf8?
2005 (set-process-coding-system (tramp-get-connection-process vec) 'raw-text-dos)
2006
2007 ;; Set width to 128. This avoids mixing prompt and long error messages.
2008 (tramp-smb-send-command vec "$rawui = (Get-Host).UI.RawUI")
2009 (tramp-smb-send-command vec "$bufsize = $rawui.BufferSize")
2010 (tramp-smb-send-command vec "$winsize = $rawui.WindowSize")
2011 (tramp-smb-send-command vec "$bufsize.Width = 128")
2012 (tramp-smb-send-command vec "$winsize.Width = 128")
2013 (tramp-smb-send-command vec "$rawui.BufferSize = $bufsize")
2014 (tramp-smb-send-command vec "$rawui.WindowSize = $winsize"))
2015
2016 (defun tramp-smb-shell-quote-argument (s)
2017 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2018 (let ((system-type 'ms-dos))
2019 (shell-quote-argument s)))
2020
2021 (add-hook 'tramp-unload-hook
2022 (lambda ()
2023 (unload-feature 'tramp-smb 'force)))
2024
2025 (provide 'tramp-smb)
2026
2027 ;;; TODO:
2028
2029 ;; * Return more comprehensive file permission string.
2030 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2031 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2032 ;; * Ignore case in file names.
2033
2034 ;;; tramp-smb.el ends here