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