1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
32 ;; Pacify byte-compiler.
36 ;; Define SMB method ...
38 (defconst tramp-smb-method
"smb"
39 "Method to connect SAMBA and M$ SMB servers.")
41 ;; ... and add it to the method list.
43 (unless (memq system-type
'(cygwin windows-nt
))
44 (add-to-list 'tramp-methods
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
52 (tramp-tmpdir "/C$/Temp"))))
54 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
55 ;; a domain in USER, it must be the SMB method.
57 (add-to-list 'tramp-default-method-alist
58 `(nil ,tramp-prefix-domain-regexp
,tramp-smb-method
))
60 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
61 ;; the anonymous user is chosen.
63 (add-to-list 'tramp-default-user-alist
64 `(,(concat "\\`" tramp-smb-method
"\\'") nil nil
))
66 ;; Add completion function for SMB method.
68 (eval-after-load 'tramp
69 '(tramp-set-completion-function
71 '((tramp-parse-netrc "~/.netrc"))))
74 (defcustom tramp-smb-program
"smbclient"
75 "Name of SMB client to run."
81 (defcustom tramp-smb-acl-program
"smbcacls"
82 "Name of SMB acls to run."
89 (defcustom tramp-smb-conf
"/dev/null"
90 "Path of the smb.conf file.
91 If it is nil, no smb.conf will be added to the `tramp-smb-program'
92 call, letting the SMB client use the default one."
94 :type
'(choice (const nil
) (file :must-match t
))
97 (defvar tramp-smb-version nil
98 "Version string of the SMB client.")
100 (defconst tramp-smb-server-version
101 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
102 "Regexp of SMB server identification.")
104 (defconst tramp-smb-prompt
"^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
105 "Regexp used as prompt in smbclient or powershell.")
107 (defconst tramp-smb-wrong-passwd-regexp
109 '("NT_STATUS_LOGON_FAILURE"
110 "NT_STATUS_WRONG_PASSWORD"))
111 "Regexp for login error strings of SMB servers.")
113 (defconst tramp-smb-errors
116 `(;; Connection error / timeout / unknown command.
117 "Connection\\( to \\S-+\\)? failed"
118 "Read from server failed, maybe it closed the connection"
119 "Call timed out: server did not respond"
120 "\\S-+: command not found"
121 "Server doesn't support UNIX CIFS calls"
133 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
134 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003),
135 ;; Windows 6.0 (Windows Vista), Windows 6.1 (Windows 7),
136 ;; Windows 6.3 (Windows Server 2012, Windows 10).
137 "NT_STATUS_ACCESS_DENIED"
138 "NT_STATUS_ACCOUNT_LOCKED_OUT"
139 "NT_STATUS_BAD_NETWORK_NAME"
140 "NT_STATUS_CANNOT_DELETE"
141 "NT_STATUS_CONNECTION_REFUSED"
142 "NT_STATUS_DIRECTORY_NOT_EMPTY"
143 "NT_STATUS_DUPLICATE_NAME"
144 "NT_STATUS_FILE_IS_A_DIRECTORY"
145 "NT_STATUS_HOST_UNREACHABLE"
146 "NT_STATUS_IMAGE_ALREADY_LOADED"
147 "NT_STATUS_INVALID_LEVEL"
148 "NT_STATUS_IO_TIMEOUT"
149 "NT_STATUS_LOGON_FAILURE"
150 "NT_STATUS_NETWORK_ACCESS_DENIED"
151 "NT_STATUS_NOT_IMPLEMENTED"
152 "NT_STATUS_NO_LOGON_SERVERS"
153 "NT_STATUS_NO_SUCH_FILE"
154 "NT_STATUS_NO_SUCH_USER"
155 "NT_STATUS_OBJECT_NAME_COLLISION"
156 "NT_STATUS_OBJECT_NAME_INVALID"
157 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
158 "NT_STATUS_SHARING_VIOLATION"
159 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
160 "NT_STATUS_UNSUCCESSFUL"
161 "NT_STATUS_WRONG_PASSWORD")))
163 "Regexp for possible error strings of SMB servers.
164 Used instead of analyzing error codes of commands.")
166 (defconst tramp-smb-actions-with-share
167 '((tramp-smb-prompt tramp-action-succeed
)
168 (tramp-password-prompt-regexp tramp-action-password
)
169 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
170 (tramp-smb-errors tramp-action-permission-denied
)
171 (tramp-process-alive-regexp tramp-action-process-alive
))
172 "List of pattern/action pairs.
173 This list is used for login to SMB servers.
175 See `tramp-actions-before-shell' for more info.")
177 (defconst tramp-smb-actions-without-share
178 '((tramp-password-prompt-regexp tramp-action-password
)
179 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
180 (tramp-smb-errors tramp-action-permission-denied
)
181 (tramp-process-alive-regexp tramp-action-out-of-band
))
182 "List of pattern/action pairs.
183 This list is used for login to SMB servers.
185 See `tramp-actions-before-shell' for more info.")
187 (defconst tramp-smb-actions-with-tar
188 '((tramp-password-prompt-regexp tramp-action-password
)
189 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
190 (tramp-smb-errors tramp-action-permission-denied
)
191 (tramp-process-alive-regexp tramp-smb-action-with-tar
))
192 "List of pattern/action pairs.
193 This list is used for tar-like copy of directories.
195 See `tramp-actions-before-shell' for more info.")
197 (defconst tramp-smb-actions-get-acl
198 '((tramp-password-prompt-regexp tramp-action-password
)
199 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
200 (tramp-smb-errors tramp-action-permission-denied
)
201 (tramp-process-alive-regexp tramp-smb-action-get-acl
))
202 "List of pattern/action pairs.
203 This list is used for smbcacls actions.
205 See `tramp-actions-before-shell' for more info.")
207 (defconst tramp-smb-actions-set-acl
208 '((tramp-password-prompt-regexp tramp-action-password
)
209 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
210 (tramp-smb-errors tramp-action-permission-denied
)
211 (tramp-process-alive-regexp tramp-smb-action-set-acl
))
212 "List of pattern/action pairs.
213 This list is used for smbcacls actions.
215 See `tramp-actions-before-shell' for more info.")
217 ;; New handlers should be added here.
218 (defconst tramp-smb-file-name-handler-alist
219 '(;; `access-file' performed by default handler.
220 (add-name-to-file . tramp-smb-handle-add-name-to-file
)
221 ;; `byte-compiler-base-file-name' performed by default handler.
222 (copy-directory . tramp-smb-handle-copy-directory
)
223 (copy-file . tramp-smb-handle-copy-file
)
224 (delete-directory . tramp-smb-handle-delete-directory
)
225 (delete-file . tramp-smb-handle-delete-file
)
226 ;; `diff-latest-backup-file' performed by default handler.
227 (directory-file-name . tramp-handle-directory-file-name
)
228 (directory-files . tramp-smb-handle-directory-files
)
229 (directory-files-and-attributes
230 . tramp-handle-directory-files-and-attributes
)
231 (dired-compress-file . ignore
)
232 (dired-uncache . tramp-handle-dired-uncache
)
233 (expand-file-name . tramp-smb-handle-expand-file-name
)
234 (file-accessible-directory-p . tramp-smb-handle-file-directory-p
)
235 (file-acl . tramp-smb-handle-file-acl
)
236 (file-attributes . tramp-smb-handle-file-attributes
)
237 (file-directory-p . tramp-smb-handle-file-directory-p
)
238 (file-file-equal-p . tramp-handle-file-equal-p
)
239 (file-executable-p . tramp-handle-file-exists-p
)
240 (file-exists-p . tramp-handle-file-exists-p
)
241 (file-in-directory-p . tramp-handle-file-in-directory-p
)
242 (file-local-copy . tramp-smb-handle-file-local-copy
)
243 (file-modes . tramp-handle-file-modes
)
244 (file-name-all-completions . tramp-smb-handle-file-name-all-completions
)
245 (file-name-as-directory . tramp-handle-file-name-as-directory
)
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' performed by default handler.
259 (file-symlink-p . tramp-handle-file-symlink-p
)
260 ;; `file-truename' performed by default handler.
261 (file-writable-p . tramp-smb-handle-file-writable-p
)
262 (find-backup-file-name . tramp-handle-find-backup-file-name
)
263 ;; `find-file-noselect' performed by default handler.
264 ;; `get-file-buffer' performed by default handler.
265 (insert-directory . tramp-smb-handle-insert-directory
)
266 (insert-file-contents . tramp-handle-insert-file-contents
)
267 (load . tramp-handle-load
)
268 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
269 (make-directory . tramp-smb-handle-make-directory
)
270 (make-directory-internal . tramp-smb-handle-make-directory-internal
)
271 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file
)
272 (make-symbolic-link . tramp-smb-handle-make-symbolic-link
)
273 (process-file . tramp-smb-handle-process-file
)
274 (rename-file . tramp-smb-handle-rename-file
)
275 (set-file-acl . tramp-smb-handle-set-file-acl
)
276 (set-file-modes . tramp-smb-handle-set-file-modes
)
277 (set-file-selinux-context . ignore
)
278 (set-file-times . ignore
)
279 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
280 (shell-command . tramp-handle-shell-command
)
281 (start-file-process . tramp-smb-handle-start-file-process
)
282 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name
)
283 (temporary-file-directory . tramp-handle-temporary-file-directory
)
284 (unhandled-file-name-directory . ignore
)
285 (vc-registered . ignore
)
286 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
)
287 (write-region . tramp-smb-handle-write-region
))
288 "Alist of handler functions for Tramp SMB method.
289 Operations not mentioned here will be handled by the default Emacs primitives.")
291 ;; Options for remote processes via winexe.
293 (defcustom tramp-smb-winexe-program
"winexe"
294 "Name of winexe client to run.
295 If it isn't found in the local $PATH, the absolute path of winexe
296 shall be given. This is needed for remote processes."
303 (defcustom tramp-smb-winexe-shell-command
"powershell.exe"
304 "Shell to be used for processes on remote machines.
305 This must be Powershell V2 compatible."
312 (defcustom tramp-smb-winexe-shell-command-switch
"-file -"
313 "Command switch used together with `tramp-smb-winexe-shell-command'.
314 This can be used to disable echo etc."
320 ;; It must be a `defsubst' in order to push the whole code into
321 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
323 (defsubst tramp-smb-file-name-p
(filename)
324 "Check if it's a filename for SMB servers."
325 (string= (tramp-file-name-method (tramp-dissect-file-name filename
))
329 (defun tramp-smb-file-name-handler (operation &rest args
)
330 "Invoke the SMB related OPERATION.
331 First arg specifies the OPERATION, second arg is a list of arguments to
332 pass to the OPERATION."
333 (let ((fn (assoc operation tramp-smb-file-name-handler-alist
)))
335 (save-match-data (apply (cdr fn
) args
))
336 (tramp-run-real-handler operation args
))))
339 (unless (memq system-type
'(cygwin windows-nt
))
340 (add-to-list 'tramp-foreign-file-name-handler-alist
341 (cons '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
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
)
361 "add-name-to-file: %s must not be a directory" filename
))
362 (when (and (not ok-if-already-exists
)
363 (file-exists-p newname
)
364 (not (numberp ok-if-already-exists
))
367 "File %s already exists; make it a new name anyway? "
371 "add-name-to-file: file %s already exists" newname
))
372 ;; We must also flush the cache of the directory, because
373 ;; `file-attributes' reads the values from there.
374 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
375 (tramp-flush-file-property v2 v2-localname
)
377 (tramp-smb-send-command
381 (if (tramp-smb-get-cifs-capabilities v1
) "link" "hardlink")
382 (tramp-smb-get-localname v1
)
383 (tramp-smb-get-localname v2
)))
386 "error with add-name-to-file, see buffer `%s' for details"
389 (defun tramp-smb-action-with-tar (proc vec
)
390 "Untar from connection buffer."
391 (if (not (tramp-compat-process-live-p proc
))
392 (throw 'tramp-action
'process-died
)
394 (with-current-buffer (tramp-get-connection-buffer vec
)
395 (goto-char (point-min))
396 (when (search-forward-regexp tramp-smb-server-version nil t
)
397 ;; There might be a hidden password prompt.
400 (tramp-message vec
6 (buffer-substring (point-min) (point)))
401 (delete-region (point-min) (point))
402 (throw 'tramp-action
'ok
)))))
404 (defun tramp-smb-handle-copy-directory
405 (dirname newname
&optional keep-date parents copy-contents
)
406 "Like `copy-directory' for Tramp files."
408 ;; We must do it file-wise.
409 (tramp-run-real-handler
410 'copy-directory
(list dirname newname keep-date parents copy-contents
))
412 (setq dirname
(expand-file-name dirname
)
413 newname
(expand-file-name newname
))
414 (let ((t1 (tramp-tramp-file-p dirname
))
415 (t2 (tramp-tramp-file-p newname
)))
416 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
417 (with-tramp-progress-reporter
418 v
0 (format "Copying %s to %s" dirname newname
)
420 ;; We must use a local temporary directory.
425 tramp-temp-name-prefix
426 (tramp-compat-temporary-file-directory)))))
429 (make-directory tmpdir
)
430 (copy-directory dirname tmpdir keep-date
'parents
)
432 (expand-file-name (file-name-nondirectory dirname
) tmpdir
)
433 newname keep-date parents
))
434 (delete-directory tmpdir
'recursive
))))
436 ;; We can copy recursively.
437 ((and (or t1 t2
) (tramp-smb-get-cifs-capabilities v
))
438 (when (and (file-directory-p newname
)
439 (not (string-equal (file-name-nondirectory dirname
)
440 (file-name-nondirectory newname
))))
443 (file-name-nondirectory dirname
) newname
))
444 (if t2
(setq v
(tramp-dissect-file-name newname
))))
445 (if (not (file-directory-p newname
))
446 (make-directory newname parents
))
448 (setq tramp-current-method
(tramp-file-name-method v
)
449 tramp-current-user
(tramp-file-name-user v
)
450 tramp-current-host
(tramp-file-name-real-host v
))
452 (let* ((real-user (tramp-file-name-real-user v
))
453 (real-host (tramp-file-name-real-host v
))
454 (domain (tramp-file-name-domain v
))
455 (port (tramp-file-name-port v
))
456 (share (tramp-smb-get-share v
))
457 (localname (file-name-as-directory
458 (replace-regexp-in-string
459 "\\\\" "/" (tramp-smb-get-localname v
))))
460 (tmpdir (make-temp-name
462 tramp-temp-name-prefix
463 (tramp-compat-temporary-file-directory))))
464 (args (list (concat "//" real-host
"/" share
) "-E")))
466 (if (not (zerop (length real-user
)))
467 (setq args
(append args
(list "-U" real-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
))))
473 (setq args
(append args
(list "-s" tramp-smb-conf
))))
478 (list "-D" (shell-quote-argument localname
)
479 "-c" (shell-quote-argument "tar qc - *")
481 (shell-quote-argument tmpdir
)))
483 (append (list "tar" "cfC" "-"
484 (shell-quote-argument dirname
) "." "|")
486 (list "-D" (shell-quote-argument localname
)
487 "-c" (shell-quote-argument "tar qx -")))))
491 ;; Set the transfer process properties.
492 (tramp-set-connection-property
493 v
"process-name" (buffer-name (current-buffer)))
494 (tramp-set-connection-property
495 v
"process-buffer" (current-buffer))
498 ;; The smbclient tar command creates always
499 ;; complete paths. We must emulate the
500 ;; directory structure, and symlink to the real
504 ".." (concat tmpdir localname
)) 'parents
)
506 newname
(directory-file-name (concat tmpdir localname
))))
508 ;; Use an asynchronous processes. By this,
509 ;; password can be handled.
510 (let* ((default-directory tmpdir
)
513 (tramp-get-connection-name v
)
514 (tramp-get-connection-buffer v
)
515 tramp-smb-program args
)))
518 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
519 (tramp-set-connection-property p
"vector" v
)
520 (set-process-query-on-exit-flag p nil
)
521 (tramp-process-actions p v nil tramp-smb-actions-with-tar
)
523 (while (tramp-compat-process-live-p p
)
525 (tramp-message v
6 "\n%s" (buffer-string))))
527 ;; Reset the transfer process properties.
528 (tramp-set-connection-property v
"process-name" nil
)
529 (tramp-set-connection-property v
"process-buffer" nil
)
530 (when t1
(delete-directory tmpdir
'recurse
))))
532 ;; Handle KEEP-DATE argument.
536 (tramp-compat-file-attribute-modification-time
537 (file-attributes dirname
))))
541 (set-file-modes newname
(tramp-default-file-modes dirname
)))
543 ;; When newname did exist, we have wrong cached values.
545 (with-parsed-tramp-file-name newname nil
546 (tramp-flush-file-property v
(file-name-directory localname
))
547 (tramp-flush-file-property v localname
))))
549 ;; We must do it file-wise.
551 (tramp-run-real-handler
552 'copy-directory
(list dirname newname keep-date parents
)))))))))
554 (defun tramp-smb-handle-copy-file
555 (filename newname
&optional ok-if-already-exists keep-date
556 _preserve-uid-gid _preserve-extended-attributes
)
557 "Like `copy-file' for Tramp files.
558 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
559 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
560 (setq filename
(expand-file-name filename
)
561 newname
(expand-file-name newname
))
562 (with-tramp-progress-reporter
563 (tramp-dissect-file-name
564 (if (tramp-tramp-file-p filename
) filename newname
))
565 0 (format "Copying %s to %s" filename newname
)
567 (if (file-directory-p filename
)
568 (tramp-compat-copy-directory
569 filename newname keep-date
'parents
'copy-contents
)
571 (let ((tmpfile (file-local-copy filename
)))
575 (rename-file tmpfile newname ok-if-already-exists
)
577 (delete-file tmpfile
)
578 (signal (car err
) (cdr err
))))
581 (when (file-directory-p newname
)
583 (expand-file-name (file-name-nondirectory filename
) newname
)))
585 (with-parsed-tramp-file-name newname nil
586 (when (and (not ok-if-already-exists
)
587 (file-exists-p newname
))
588 (tramp-error v
'file-already-exists newname
))
590 ;; We must also flush the cache of the directory, because
591 ;; `file-attributes' reads the values from there.
592 (tramp-flush-file-property v
(file-name-directory localname
))
593 (tramp-flush-file-property v localname
)
594 (unless (tramp-smb-get-share v
)
596 v
'file-error
"Target `%s' must contain a share name" newname
))
597 (unless (tramp-smb-send-command
598 v
(format "put \"%s\" \"%s\""
599 filename
(tramp-smb-get-localname v
)))
601 v
'file-error
"Cannot copy `%s' to `%s'" filename newname
))))))
603 ;; KEEP-DATE handling.
607 (tramp-compat-file-attribute-modification-time
608 (file-attributes filename
))))))
610 (defun tramp-smb-handle-delete-directory (directory &optional recursive
)
611 "Like `delete-directory' for Tramp files."
612 (setq directory
(directory-file-name (expand-file-name directory
)))
613 (when (file-exists-p directory
)
617 (if (file-directory-p file
)
618 (delete-directory file recursive
)
620 ;; We do not want to delete "." and "..".
621 (directory-files directory
'full directory-files-no-dot-files-regexp
)))
623 (with-parsed-tramp-file-name directory nil
624 ;; We must also flush the cache of the directory, because
625 ;; `file-attributes' reads the values from there.
626 (tramp-flush-file-property v
(file-name-directory localname
))
627 (tramp-flush-directory-property v localname
)
628 (unless (tramp-smb-send-command
631 (if (tramp-smb-get-cifs-capabilities v
) "posix_rmdir" "rmdir")
632 (tramp-smb-get-localname v
)))
634 (with-current-buffer (tramp-get-connection-buffer v
)
635 (goto-char (point-min))
636 (search-forward-regexp tramp-smb-errors nil t
)
638 v
'file-error
"%s `%s'" (match-string 0) directory
))))))
640 (defun tramp-smb-handle-delete-file (filename &optional _trash
)
641 "Like `delete-file' for Tramp files."
642 (setq filename
(expand-file-name filename
))
643 (when (file-exists-p filename
)
644 (with-parsed-tramp-file-name filename nil
645 ;; We must also flush the cache of the directory, because
646 ;; `file-attributes' reads the values from there.
647 (tramp-flush-file-property v
(file-name-directory localname
))
648 (tramp-flush-file-property v localname
)
649 (unless (tramp-smb-send-command
652 (if (tramp-smb-get-cifs-capabilities v
) "posix_unlink" "rm")
653 (tramp-smb-get-localname v
)))
655 (with-current-buffer (tramp-get-connection-buffer v
)
656 (goto-char (point-min))
657 (search-forward-regexp tramp-smb-errors nil t
)
659 v
'file-error
"%s `%s'" (match-string 0) filename
))))))
661 (defun tramp-smb-handle-directory-files
662 (directory &optional full match nosort
)
663 "Like `directory-files' for Tramp files."
664 (let ((result (mapcar 'directory-file-name
665 (file-name-all-completions "" directory
))))
666 ;; Discriminate with regexp.
670 (mapcar (lambda (x) (when (string-match match x
) x
))
676 (lambda (x) (format "%s/%s" directory x
))
678 ;; Sort them if necessary.
679 (unless nosort
(setq result
(sort result
'string-lessp
)))
682 (defun tramp-smb-handle-expand-file-name (name &optional dir
)
683 "Like `expand-file-name' for Tramp files."
684 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
685 (setq dir
(or dir default-directory
"/"))
686 ;; Unless NAME is absolute, concat DIR and NAME.
687 (unless (file-name-absolute-p name
)
688 (setq name
(concat (file-name-as-directory dir
) name
)))
689 ;; If NAME is not a Tramp file, run the real handler.
690 (if (not (tramp-tramp-file-p name
))
691 (tramp-run-real-handler 'expand-file-name
(list name nil
))
693 (with-parsed-tramp-file-name name nil
694 ;; Tilde expansion if necessary. We use the user name as share,
695 ;; which is often the case in domains.
696 (when (string-match "\\`/?~\\([^/]*\\)" localname
)
699 (if (zerop (length (match-string 1 localname
)))
700 (tramp-file-name-real-user v
)
701 (match-string 1 localname
))
703 ;; Make the file name absolute.
704 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
705 (setq localname
(concat "/" localname
)))
706 ;; No tilde characters in file name, do normal
707 ;; `expand-file-name' (this does "/./" and "/../").
708 (tramp-make-tramp-file-name
710 (tramp-run-real-handler 'expand-file-name
(list localname
))))))
712 (defun tramp-smb-action-get-acl (proc vec
)
713 "Read ACL data from connection buffer."
714 (unless (tramp-compat-process-live-p proc
)
715 ;; Accept pending output.
716 (while (tramp-accept-process-output proc
0.1))
717 (with-current-buffer (tramp-get-connection-buffer vec
)
718 ;; There might be a hidden password prompt.
720 (tramp-message vec
10 "\n%s" (buffer-string))
721 (goto-char (point-min))
722 (while (and (not (eobp)) (not (looking-at "^REVISION:")))
724 (delete-region (point-min) (point)))
725 (while (and (not (eobp)) (looking-at "^.+:.+"))
727 (delete-region (point) (point-max))
728 (throw 'tramp-action
'ok
))))
730 (defun tramp-smb-handle-file-acl (filename)
731 "Like `file-acl' for Tramp files."
732 (with-parsed-tramp-file-name filename nil
733 (with-tramp-file-property v localname
"file-acl"
734 (when (executable-find tramp-smb-acl-program
)
736 (setq tramp-current-method
(tramp-file-name-method v
)
737 tramp-current-user
(tramp-file-name-user v
)
738 tramp-current-host
(tramp-file-name-real-host v
))
740 (let* ((real-user (tramp-file-name-real-user v
))
741 (real-host (tramp-file-name-real-host v
))
742 (domain (tramp-file-name-domain v
))
743 (port (tramp-file-name-port v
))
744 (share (tramp-smb-get-share v
))
745 (localname (replace-regexp-in-string
746 "\\\\" "/" (tramp-smb-get-localname v
)))
747 (args (list (concat "//" real-host
"/" share
) "-E")))
749 (if (not (zerop (length real-user
)))
750 (setq args
(append args
(list "-U" real-user
)))
751 (setq args
(append args
(list "-N"))))
753 (when domain
(setq args
(append args
(list "-W" domain
))))
754 (when port
(setq args
(append args
(list "-p" port
))))
756 (setq args
(append args
(list "-s" tramp-smb-conf
))))
759 (append args
(list (shell-quote-argument localname
) "2>/dev/null")))
763 ;; Set the transfer process properties.
764 (tramp-set-connection-property
765 v
"process-name" (buffer-name (current-buffer)))
766 (tramp-set-connection-property
767 v
"process-buffer" (current-buffer))
769 ;; Use an asynchronous processes. By this, password
773 (tramp-get-connection-name v
)
774 (tramp-get-connection-buffer v
)
775 tramp-smb-acl-program args
)))
778 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
779 (tramp-set-connection-property p
"vector" v
)
780 (set-process-query-on-exit-flag p nil
)
781 (tramp-process-actions p v nil tramp-smb-actions-get-acl
)
782 (when (> (point-max) (point-min))
783 (substring-no-properties (buffer-string)))))
785 ;; Reset the transfer process properties.
786 (tramp-set-connection-property v
"process-name" nil
)
787 (tramp-set-connection-property v
"process-buffer" nil
)))))))
789 (defun tramp-smb-handle-file-attributes (filename &optional id-format
)
790 "Like `file-attributes' for Tramp files."
791 (unless id-format
(setq id-format
'integer
))
793 (with-parsed-tramp-file-name filename nil
794 (with-tramp-file-property
795 v localname
(format "file-attributes-%s" id-format
)
796 (if (tramp-smb-get-stat-capability v
)
797 (tramp-smb-do-file-attributes-with-stat v id-format
)
798 ;; Reading just the filename entry via "dir localname" is not
799 ;; possible, because when filename is a directory, some
800 ;; smbclient versions return the content of the directory, and
801 ;; other versions don't. Therefore, the whole content of the
802 ;; upper directory is retrieved, and the entry of the filename
803 ;; is extracted from.
804 (let* ((entries (tramp-smb-get-file-entries
805 (file-name-directory filename
)))
806 (entry (assoc (file-name-nondirectory filename
) entries
))
807 (uid (if (equal id-format
'string
) "nobody" -
1))
808 (gid (if (equal id-format
'string
) "nogroup" -
1))
809 (inode (tramp-get-inode v
))
810 (device (tramp-get-device v
)))
814 (list (and (string-match "d" (nth 1 entry
))
820 (nth 3 entry
) ;5 mtime
822 (nth 2 entry
) ;7 size
823 (nth 1 entry
) ;8 mode
825 inode
;10 inode number
826 device
)))))))) ;11 file system number
828 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format
)
829 "Implement `file-attributes' for Tramp files using stat command."
831 vec
5 "file attributes with stat: %s" (tramp-file-name-localname vec
))
832 (with-current-buffer (tramp-get-connection-buffer vec
)
833 (let* (size id link uid gid atime mtime ctime mode inode
)
834 (when (tramp-smb-send-command
835 vec
(format "stat \"%s\"" (tramp-smb-get-localname vec
)))
838 (goto-char (point-min))
839 (unless (re-search-forward tramp-smb-errors nil t
)
843 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
844 (setq size
(string-to-number (match-string 1))
845 id
(if (string-equal "directory" (match-string 2)) t
846 (if (string-equal "symbolic" (match-string 2)) ""))))
848 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
849 (setq inode
(string-to-number (match-string 1))
850 link
(string-to-number (match-string 2))))
852 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
853 (setq mode
(match-string 1)
854 uid
(if (equal id-format
'string
) (match-string 2)
855 (string-to-number (match-string 2)))
856 gid
(if (equal id-format
'string
) (match-string 3)
857 (string-to-number (match-string 3)))))
859 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
862 (string-to-number (match-string 6)) ;; sec
863 (string-to-number (match-string 5)) ;; min
864 (string-to-number (match-string 4)) ;; hour
865 (string-to-number (match-string 3)) ;; day
866 (string-to-number (match-string 2)) ;; month
867 (string-to-number (match-string 1))))) ;; year
869 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
872 (string-to-number (match-string 6)) ;; sec
873 (string-to-number (match-string 5)) ;; min
874 (string-to-number (match-string 4)) ;; hour
875 (string-to-number (match-string 3)) ;; day
876 (string-to-number (match-string 2)) ;; month
877 (string-to-number (match-string 1))))) ;; year
879 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
882 (string-to-number (match-string 6)) ;; sec
883 (string-to-number (match-string 5)) ;; min
884 (string-to-number (match-string 4)) ;; hour
885 (string-to-number (match-string 3)) ;; day
886 (string-to-number (match-string 2)) ;; month
887 (string-to-number (match-string 1)))))) ;; year
889 ;; Return the result.
890 (list id link uid gid atime mtime ctime size mode nil inode
891 (tramp-get-device vec
)))))))
893 (defun tramp-smb-handle-file-directory-p (filename)
894 "Like `file-directory-p' for Tramp files."
895 (and (file-exists-p filename
)
897 (aref (tramp-compat-file-attribute-modes (file-attributes filename
))
900 (defun tramp-smb-handle-file-local-copy (filename)
901 "Like `file-local-copy' for Tramp files."
902 (with-parsed-tramp-file-name filename nil
903 (unless (file-exists-p filename
)
906 "Cannot make local copy of non-existing file `%s'" filename
))
907 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
908 (with-tramp-progress-reporter
909 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
910 (unless (tramp-smb-send-command
911 v
(format "get \"%s\" \"%s\""
912 (tramp-smb-get-localname v
) tmpfile
))
913 ;; Oops, an error. We shall cleanup.
914 (delete-file tmpfile
)
916 v
'file-error
"Cannot make local copy of file `%s'" filename
)))
919 ;; This function should return "foo/" for directories and "bar" for
921 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
922 "Like `file-name-all-completions' for Tramp files."
925 (with-parsed-tramp-file-name (expand-file-name directory
) nil
926 (with-tramp-file-property v localname
"file-name-all-completions"
932 (if (string-match "d" (nth 1 x
))
933 (file-name-as-directory (nth 0 x
))
935 (tramp-smb-get-file-entries directory
))))))))
937 (defun tramp-smb-handle-file-writable-p (filename)
938 "Like `file-writable-p' for Tramp files."
939 (if (file-exists-p filename
)
942 (or (tramp-compat-file-attribute-modes (file-attributes filename
)) ""))
943 (let ((dir (file-name-directory filename
)))
944 (and (file-exists-p dir
)
945 (file-writable-p dir
)))))
947 (defun tramp-smb-handle-insert-directory
948 (filename switches
&optional wildcard full-directory-p
)
949 "Like `insert-directory' for Tramp files."
950 (setq filename
(expand-file-name filename
))
951 (unless switches
(setq switches
""))
952 ;; Mark trailing "/".
953 (when (and (zerop (length (file-name-nondirectory filename
)))
954 (not full-directory-p
))
955 (setq switches
(concat switches
"F")))
957 ;; Called from `dired-add-entry'.
958 (setq filename
(file-name-as-directory filename
))
959 (setq filename
(directory-file-name filename
)))
960 (with-parsed-tramp-file-name filename nil
961 (with-tramp-progress-reporter v
0 (format "Opening directory %s" filename
)
963 (let ((base (file-name-nondirectory filename
))
964 ;; We should not destroy the cache entry.
965 (entries (copy-sequence
966 (tramp-smb-get-file-entries
967 (file-name-directory filename
)))))
970 (string-match "\\." base
)
971 (setq base
(replace-match "\\\\." nil nil base
))
972 (string-match "\\*" base
)
973 (setq base
(replace-match ".*" nil nil base
))
974 (string-match "\\?" base
)
975 (setq base
(replace-match ".?" nil nil base
)))
981 (if (or wildcard
(zerop (length base
)))
982 ;; Check for matching entries.
986 (format "^%s" base
) (nth 0 x
))
989 ;; We just need the only and only entry FILENAME.
990 (list (assoc base entries
)))))
997 (if (string-match "t" switches
)
999 (time-less-p (nth 3 y
) (nth 3 x
))
1001 (string-lessp (nth 0 x
) (nth 0 y
))))))
1003 ;; Handle "-F" switch.
1004 (when (string-match "F" switches
)
1007 (when (not (zerop (length (car x
))))
1009 ((char-equal ?d
(string-to-char (nth 1 x
)))
1010 (setcar x
(concat (car x
) "/")))
1011 ((char-equal ?x
(string-to-char (nth 1 x
)))
1012 (setcar x
(concat (car x
) "*"))))))
1018 (when (not (zerop (length (nth 0 x
))))
1019 (when (string-match "l" switches
)
1021 (when (tramp-smb-get-stat-capability v
)
1023 (file-attributes filename
'string
)))))
1026 "%10s %3d %-8s %-8s %8s %s "
1027 (or (tramp-compat-file-attribute-modes attr
) (nth 1 x
))
1028 (or (tramp-compat-file-attribute-link-number attr
) 1)
1029 (or (tramp-compat-file-attribute-user-id attr
) "nobody")
1030 (or (tramp-compat-file-attribute-group-id attr
) "nogroup")
1031 (or (tramp-compat-file-attribute-size attr
) (nth 2 x
))
1033 (if (time-less-p (time-subtract (current-time) (nth 3 x
))
1037 (nth 3 x
)))))) ; date
1039 ;; We mark the file name. The inserted name could be
1040 ;; from somewhere else, so we use the relative file name
1041 ;; of `default-directory'.
1042 (let ((start (point)))
1048 (nth 0 x
) (file-name-directory filename
))
1049 (when full-directory-p
(file-name-directory filename
)))))
1050 (put-text-property start
(1- (point)) 'dired-filename t
))
1052 (beginning-of-line)))
1055 (defun tramp-smb-handle-make-directory (dir &optional parents
)
1056 "Like `make-directory' for Tramp files."
1057 (setq dir
(directory-file-name (expand-file-name dir
)))
1058 (unless (file-name-absolute-p dir
)
1059 (setq dir
(expand-file-name dir default-directory
)))
1060 (with-parsed-tramp-file-name dir nil
1062 (let* ((ldir (file-name-directory dir
)))
1063 ;; Make missing directory parts.
1065 (tramp-smb-get-share v
)
1066 (not (file-directory-p ldir
)))
1067 (make-directory ldir parents
))
1069 (when (file-directory-p ldir
)
1070 (make-directory-internal dir
))
1071 (unless (file-directory-p dir
)
1072 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
1074 (defun tramp-smb-handle-make-directory-internal (directory)
1075 "Like `make-directory-internal' for Tramp files."
1076 (setq directory
(directory-file-name (expand-file-name directory
)))
1077 (unless (file-name-absolute-p directory
)
1078 (setq directory
(expand-file-name directory default-directory
)))
1079 (with-parsed-tramp-file-name directory nil
1081 (let* ((file (tramp-smb-get-localname v
)))
1082 (when (file-directory-p (file-name-directory directory
))
1083 (tramp-smb-send-command
1085 (if (tramp-smb-get-cifs-capabilities v
)
1086 (format "posix_mkdir \"%s\" %o" file
(default-file-modes))
1087 (format "mkdir \"%s\"" file
)))
1088 ;; We must also flush the cache of the directory, because
1089 ;; `file-attributes' reads the values from there.
1090 (tramp-flush-file-property v
(file-name-directory localname
))
1091 (tramp-flush-file-property v localname
))
1092 (unless (file-directory-p directory
)
1094 v
'file-error
"Couldn't make directory %s" directory
))))))
1096 (defun tramp-smb-handle-make-symbolic-link
1097 (filename linkname
&optional ok-if-already-exists
)
1098 "Like `make-symbolic-link' for Tramp files.
1099 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1100 the symlink. If LINKNAME is a Tramp file, only the localname component is
1101 used as the target of the symlink.
1103 If LINKNAME is a Tramp file and the localname component is relative, then
1104 it is expanded first, before the localname component is taken. Note that
1105 this can give surprising results if the user/host for the source and
1106 target of the symlink differ."
1107 (unless (tramp-equal-remote filename linkname
)
1108 (with-parsed-tramp-file-name
1109 (if (tramp-tramp-file-p filename
) filename linkname
) nil
1112 "make-symbolic-link: %s"
1113 "only implemented for same method, same user, same host")))
1114 (with-parsed-tramp-file-name filename v1
1115 (with-parsed-tramp-file-name linkname v2
1116 (when (file-directory-p filename
)
1119 "make-symbolic-link: %s must not be a directory" filename
))
1120 (when (and (not ok-if-already-exists
)
1121 (file-exists-p linkname
)
1122 (not (numberp ok-if-already-exists
))
1125 "File %s already exists; make it a new name anyway? "
1128 v2
'file-already-exists
1129 "make-symbolic-link: file %s already exists" linkname
))
1130 (unless (tramp-smb-get-cifs-capabilities v1
)
1131 (tramp-error v2
'file-error
"make-symbolic-link not supported"))
1132 ;; We must also flush the cache of the directory, because
1133 ;; `file-attributes' reads the values from there.
1134 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1135 (tramp-flush-file-property v2 v2-localname
)
1137 (tramp-smb-send-command
1140 "symlink \"%s\" \"%s\""
1141 (tramp-smb-get-localname v1
)
1142 (tramp-smb-get-localname v2
)))
1145 "error with make-symbolic-link, see buffer `%s' for details"
1148 (defun tramp-smb-handle-process-file
1149 (program &optional infile destination display
&rest args
)
1150 "Like `process-file' for Tramp files."
1151 ;; The implementation is not complete yet.
1152 (when (and (numberp destination
) (zerop destination
))
1153 (error "Implementation does not handle immediate return"))
1155 (with-parsed-tramp-file-name default-directory nil
1156 (let* ((name (file-name-nondirectory program
))
1159 input tmpinput outbuf command ret
)
1163 (setq infile
(expand-file-name infile
))
1164 (if (tramp-equal-remote default-directory infile
)
1165 ;; INFILE is on the same remote host.
1166 (setq input
(with-parsed-tramp-file-name infile nil localname
))
1167 ;; INFILE must be copied to remote host.
1168 (setq input
(tramp-make-tramp-temp-file v
)
1169 tmpinput
(tramp-make-tramp-file-name method user host input
))
1170 (copy-file infile tmpinput t
))
1171 ;; Transform input into a filename powershell does understand.
1172 (setq input
(format "//%s%s" host input
)))
1174 ;; Determine output.
1177 ((bufferp destination
)
1178 (setq outbuf destination
))
1180 ((stringp destination
)
1181 (setq outbuf
(get-buffer-create destination
)))
1182 ;; (REAL-DESTINATION ERROR-DESTINATION)
1183 ((consp destination
)
1186 ((bufferp (car destination
))
1187 (setq outbuf
(car destination
)))
1188 ((stringp (car destination
))
1189 (setq outbuf
(get-buffer-create (car destination
))))
1191 (setq outbuf
(current-buffer))))
1193 (tramp-message v
2 "%s" "STDERR not supported"))
1196 (setq outbuf
(current-buffer))))
1198 ;; Construct command.
1199 (setq command
(mapconcat 'identity
(cons program args
) " ")
1202 "get-content %s | & %s"
1203 (tramp-smb-shell-quote-argument input
) command
)
1204 (format "& %s" command
)))
1206 (while (get-process name1
)
1207 ;; NAME must be unique as process name.
1209 name1
(format "%s<%d>" name i
)))
1211 ;; Set the new process properties.
1212 (tramp-set-connection-property v
"process-name" name1
)
1213 (tramp-set-connection-property
1215 (or outbuf
(generate-new-buffer tramp-temp-buffer-name
)))
1219 (with-current-buffer (tramp-get-connection-buffer v
)
1220 ;; Preserve buffer contents.
1221 (narrow-to-region (point-max) (point-max))
1222 (tramp-smb-call-winexe v
)
1223 (when (tramp-smb-get-share v
)
1224 (tramp-smb-send-command
1225 v
(format "cd \"//%s%s\"" host
(file-name-directory localname
))))
1226 (tramp-smb-send-command v command
)
1227 ;; Preserve command output.
1228 (narrow-to-region (point-max) (point-max))
1229 (let ((p (tramp-get-connection-process v
)))
1230 (tramp-smb-send-command v
"exit $lasterrorcode")
1231 (while (tramp-compat-process-live-p p
)
1233 (setq ret
(process-exit-status p
))))
1234 (delete-region (point-min) (point-max))
1237 ;; When the user did interrupt, we should do it also. We use
1238 ;; return code -1 as marker.
1245 ;; We should redisplay the output.
1246 (when (and display outbuf
(get-buffer-window outbuf t
)) (redisplay))
1248 ;; Cleanup. We remove all file cache values for the connection,
1249 ;; because the remote process could have changed them.
1250 (tramp-set-connection-property v
"process-name" nil
)
1251 (tramp-set-connection-property v
"process-buffer" nil
)
1252 (when tmpinput
(delete-file tmpinput
))
1254 (kill-buffer (tramp-get-connection-property v
"process-buffer" nil
)))
1256 (unless process-file-side-effects
1257 (tramp-flush-directory-property v
""))
1259 ;; Return exit status.
1264 (defun tramp-smb-handle-rename-file
1265 (filename newname
&optional ok-if-already-exists
)
1266 "Like `rename-file' for Tramp files."
1267 (setq filename
(expand-file-name filename
)
1268 newname
(expand-file-name newname
))
1270 (when (and (not ok-if-already-exists
)
1271 (file-exists-p newname
))
1273 (tramp-dissect-file-name
1274 (if (tramp-tramp-file-p filename
) filename newname
))
1275 'file-already-exists newname
))
1277 (with-tramp-progress-reporter
1278 (tramp-dissect-file-name
1279 (if (tramp-tramp-file-p filename
) filename newname
))
1280 0 (format "Renaming %s to %s" filename newname
)
1282 (if (and (not (file-exists-p newname
))
1283 (tramp-equal-remote filename newname
)
1285 (tramp-smb-get-share (tramp-dissect-file-name filename
))
1286 (tramp-smb-get-share (tramp-dissect-file-name newname
))))
1287 ;; We can rename directly.
1288 (with-parsed-tramp-file-name filename v1
1289 (with-parsed-tramp-file-name newname v2
1291 ;; We must also flush the cache of the directory, because
1292 ;; `file-attributes' reads the values from there.
1293 (tramp-flush-file-property v1
(file-name-directory v1-localname
))
1294 (tramp-flush-file-property v1 v1-localname
)
1295 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1296 (tramp-flush-file-property v2 v2-localname
)
1297 (unless (tramp-smb-get-share v2
)
1299 v2
'file-error
"Target `%s' must contain a share name" newname
))
1300 (unless (tramp-smb-send-command
1301 v2
(format "rename \"%s\" \"%s\""
1302 (tramp-smb-get-localname v1
)
1303 (tramp-smb-get-localname v2
)))
1304 (tramp-error v2
'file-error
"Cannot rename `%s'" filename
))))
1306 ;; We must rename via copy.
1308 filename newname ok-if-already-exists
'keep-time
'preserve-uid-gid
)
1309 (if (file-directory-p filename
)
1310 (delete-directory filename
'recursive
)
1311 (delete-file filename
)))))
1313 (defun tramp-smb-action-set-acl (proc vec
)
1314 "Read ACL data from connection buffer."
1315 (unless (tramp-compat-process-live-p proc
)
1316 ;; Accept pending output.
1317 (while (tramp-accept-process-output proc
0.1))
1318 (with-current-buffer (tramp-get-connection-buffer vec
)
1319 (tramp-message vec
10 "\n%s" (buffer-string))
1320 (throw 'tramp-action
'ok
))))
1322 (defun tramp-smb-handle-set-file-acl (filename acl-string
)
1323 "Like `set-file-acl' for Tramp files."
1325 (with-parsed-tramp-file-name filename nil
1326 (when (and (stringp acl-string
) (executable-find tramp-smb-acl-program
))
1327 (setq tramp-current-method
(tramp-file-name-method v
)
1328 tramp-current-user
(tramp-file-name-user v
)
1329 tramp-current-host
(tramp-file-name-real-host v
))
1330 (tramp-set-file-property v localname
"file-acl" 'undef
)
1332 (let* ((real-user (tramp-file-name-real-user v
))
1333 (real-host (tramp-file-name-real-host v
))
1334 (domain (tramp-file-name-domain v
))
1335 (port (tramp-file-name-port v
))
1336 (share (tramp-smb-get-share v
))
1337 (localname (replace-regexp-in-string
1338 "\\\\" "/" (tramp-smb-get-localname v
)))
1339 (args (list (concat "//" real-host
"/" share
) "-E" "-S"
1340 (replace-regexp-in-string
1341 "\n" "," acl-string
))))
1343 (if (not (zerop (length real-user
)))
1344 (setq args
(append args
(list "-U" real-user
)))
1345 (setq args
(append args
(list "-N"))))
1347 (when domain
(setq args
(append args
(list "-W" domain
))))
1348 (when port
(setq args
(append args
(list "-p" port
))))
1349 (when tramp-smb-conf
1350 (setq args
(append args
(list "-s" tramp-smb-conf
))))
1353 (append args
(list (shell-quote-argument localname
)
1354 "&&" "echo" "tramp_exit_status" "0"
1355 "||" "echo" "tramp_exit_status" "1")))
1359 ;; Set the transfer process properties.
1360 (tramp-set-connection-property
1361 v
"process-name" (buffer-name (current-buffer)))
1362 (tramp-set-connection-property
1363 v
"process-buffer" (current-buffer))
1365 ;; Use an asynchronous processes. By this, password can
1369 (tramp-get-connection-name v
)
1370 (tramp-get-connection-buffer v
)
1371 tramp-smb-acl-program args
)))
1374 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1375 (tramp-set-connection-property p
"vector" v
)
1376 (set-process-query-on-exit-flag p nil
)
1377 (tramp-process-actions p v nil tramp-smb-actions-set-acl
)
1378 (goto-char (point-max))
1379 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1382 "Couldn't find exit status of `%s'" tramp-smb-acl-program
))
1383 (skip-chars-forward "^ ")
1384 (when (zerop (read (current-buffer)))
1386 (tramp-set-file-property v localname
"file-acl" acl-string
)
1389 ;; Reset the transfer process properties.
1390 (tramp-set-connection-property v
"process-name" nil
)
1391 (tramp-set-connection-property v
"process-buffer" nil
)))))))
1393 (defun tramp-smb-handle-set-file-modes (filename mode
)
1394 "Like `set-file-modes' for Tramp files."
1395 (with-parsed-tramp-file-name filename nil
1396 (when (tramp-smb-get-cifs-capabilities v
)
1397 (tramp-flush-file-property v localname
)
1398 (unless (tramp-smb-send-command
1399 v
(format "chmod \"%s\" %o" (tramp-smb-get-localname v
) mode
))
1401 v
'file-error
"Error while changing file's mode %s" filename
)))))
1403 ;; We use BUFFER also as connection buffer during setup. Because of
1404 ;; this, its original contents must be saved, and restored once
1405 ;; connection has been setup.
1406 (defun tramp-smb-handle-start-file-process (name buffer program
&rest args
)
1407 "Like `start-file-process' for Tramp files."
1408 (with-parsed-tramp-file-name default-directory nil
1411 (get-buffer-create buffer
)
1412 ;; BUFFER can be nil. We use a temporary buffer.
1413 (generate-new-buffer tramp-temp-buffer-name
)))
1414 (command (mapconcat 'identity
(cons program args
) " "))
1415 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
1421 (while (get-process name1
)
1422 ;; NAME must be unique as process name.
1424 name1
(format "%s<%d>" name i
)))
1425 ;; Set the new process properties.
1426 (tramp-set-connection-property v
"process-name" name1
)
1427 (tramp-set-connection-property v
"process-buffer" buffer
)
1428 ;; Activate narrowing in order to save BUFFER contents.
1429 (with-current-buffer (tramp-get-connection-buffer v
)
1430 (let ((buffer-undo-list t
))
1431 (narrow-to-region (point-max) (point-max))
1432 (tramp-smb-call-winexe v
)
1433 (when (tramp-smb-get-share v
)
1434 (tramp-smb-send-command
1437 host
(file-name-directory localname
))))
1438 (tramp-message v
6 "(%s); exit" command
)
1439 (tramp-send-string v command
)))
1441 (tramp-get-connection-process v
)))
1444 (with-current-buffer (tramp-get-connection-buffer v
)
1445 (if (string-match tramp-temp-buffer-name
(buffer-name))
1447 (set-process-buffer (tramp-get-connection-process v
) nil
)
1448 (kill-buffer (current-buffer)))
1449 (set-buffer-modified-p bmp
)))
1450 (tramp-set-connection-property v
"process-name" nil
)
1451 (tramp-set-connection-property v
"process-buffer" nil
)))))
1453 (defun tramp-smb-handle-substitute-in-file-name (filename)
1454 "Like `handle-substitute-in-file-name' for Tramp files.
1455 \"//\" substitutes only in the local filename part. Catches
1456 errors for shares like \"C$/\", which are common in Microsoft Windows."
1457 (with-parsed-tramp-file-name filename nil
1458 ;; Ignore in LOCALNAME everything before "//".
1459 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
1461 (concat (file-remote-p filename
)
1462 (replace-match "\\1" nil nil localname
)))))
1464 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
1467 (defun tramp-smb-handle-write-region
1468 (start end filename
&optional append visit lockname confirm
)
1469 "Like `write-region' for Tramp files."
1470 (setq filename
(expand-file-name filename
))
1471 (with-parsed-tramp-file-name filename nil
1472 (when (and confirm
(file-exists-p filename
))
1473 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1475 (tramp-error v
'file-error
"File not overwritten")))
1476 ;; We must also flush the cache of the directory, because
1477 ;; `file-attributes' reads the values from there.
1478 (tramp-flush-file-property v
(file-name-directory localname
))
1479 (tramp-flush-file-property v localname
)
1480 (let ((curbuf (current-buffer))
1481 (tmpfile (tramp-compat-make-temp-file filename
)))
1482 (when (and append
(file-exists-p filename
))
1483 (copy-file filename tmpfile
'ok
))
1484 ;; We say `no-message' here because we don't want the visited file
1485 ;; modtime data to be clobbered from the temp file. We call
1486 ;; `set-visited-file-modtime' ourselves later on.
1487 (tramp-run-real-handler
1489 (if confirm
; don't pass this arg unless defined for backward compat.
1490 (list start end tmpfile append
'no-message lockname confirm
)
1491 (list start end tmpfile append
'no-message lockname
)))
1493 (with-tramp-progress-reporter
1494 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
1496 (unless (tramp-smb-send-command
1497 v
(format "put %s \"%s\""
1498 tmpfile
(tramp-smb-get-localname v
)))
1499 (tramp-error v
'file-error
"Cannot write `%s'" filename
))
1500 (delete-file tmpfile
)))
1502 (unless (equal curbuf
(current-buffer))
1505 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer)))
1507 (set-visited-file-modtime)))))
1510 ;; Internal file name functions.
1512 (defun tramp-smb-get-share (vec)
1513 "Returns the share name of LOCALNAME."
1515 (let ((localname (tramp-file-name-localname vec
)))
1516 (when (string-match "^/?\\([^/]+\\)/" localname
)
1517 (match-string 1 localname
)))))
1519 (defun tramp-smb-get-localname (vec)
1520 "Returns the file name of LOCALNAME.
1521 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1523 (let ((localname (tramp-file-name-localname vec
)))
1526 (if (string-match "^/?[^/]+\\(/.*\\)" localname
)
1527 ;; There is a share, separated by "/".
1528 (if (not (tramp-smb-get-cifs-capabilities vec
))
1530 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
1531 (match-string 1 localname
) "")
1532 (match-string 1 localname
))
1533 ;; There is just a share.
1534 (if (string-match "^/?\\([^/]+\\)$" localname
)
1535 (match-string 1 localname
)
1538 ;; Sometimes we have discarded `substitute-in-file-name'.
1539 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname
)
1540 (setq localname
(replace-match "$" nil nil localname
1)))
1544 ;; Share names of a host are cached. It is very unlikely that the
1545 ;; shares do change during connection.
1546 (defun tramp-smb-get-file-entries (directory)
1547 "Read entries which match DIRECTORY.
1548 Either the shares are listed, or the `dir' command is executed.
1549 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1550 (with-parsed-tramp-file-name (file-name-as-directory directory
) nil
1551 (setq localname
(or localname
"/"))
1552 (with-tramp-file-property v localname
"file-entries"
1553 (with-current-buffer (tramp-get-connection-buffer v
)
1554 (let* ((share (tramp-smb-get-share v
))
1555 (cache (tramp-get-connection-property v
"share-cache" nil
))
1558 (if (and (not share
) cache
)
1559 ;; Return cached shares.
1564 (tramp-smb-send-command
1565 v
(format "dir \"%s*\"" (tramp-smb-get-localname v
)))
1566 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1567 (tramp-smb-maybe-open-connection v
))
1569 ;; Loop the listing.
1570 (goto-char (point-min))
1571 (if (re-search-forward tramp-smb-errors nil t
)
1572 (tramp-error v
'file-error
"%s `%s'" (match-string 0) directory
)
1574 (setq entry
(tramp-smb-read-file-entry share
))
1576 (when entry
(push entry res
))))
1578 ;; Cache share entries.
1580 (tramp-set-connection-property v
"share-cache" res
)))
1582 ;; Add directory itself.
1583 (push '("" "drwxrwxrwx" 0 (0 0)) res
)
1588 ;; Return either a share name (if SHARE is nil), or a file name.
1590 ;; If shares are listed, the following format is expected:
1592 ;; Disk| - leading spaces
1593 ;; [^|]+| - share name, 14 char
1596 ;; Entries provided by smbclient DIR aren't fully regular.
1597 ;; They should have the format
1599 ;; \s-\{2,2} - leading spaces
1600 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1601 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1602 ;; \s- - space delimiter
1603 ;; \s-+[0-9]+ - size, 8 chars, right bound
1604 ;; \s-\{2,2\} - space delimiter
1605 ;; \w\{3,3\} - weekday
1606 ;; \s- - space delimiter
1607 ;; \w\{3,3\} - month
1608 ;; \s- - space delimiter
1610 ;; \s- - space delimiter
1611 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1612 ;; \s- - space delimiter
1613 ;; [0-9]\{4,4\} - year
1615 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1616 ;; has function display_finfo:
1618 ;; d_printf(" %-30s%7.7s %8.0f %s",
1620 ;; attrib_string(finfo->mode),
1621 ;; (double)finfo->size,
1622 ;; asctime(LocalTime(&t)));
1624 ;; in Samba 1.9, there's the following code:
1626 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1627 ;; CNV_LANG(finfo->name),
1628 ;; attrib_string(finfo->mode),
1630 ;; asctime(LocalTime(&t))));
1633 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1634 ;; available in older Emacsen.
1635 ;; * The length of constructs (file name, size) might exceed the default.
1636 ;; * File names might contain spaces.
1637 ;; * Permissions might be empty.
1639 ;; So we try to analyze backwards.
1640 (defun tramp-smb-read-file-entry (share)
1641 "Parse entry in SMB output buffer.
1642 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1643 Result is the list (LOCALNAME MODE SIZE MTIME)."
1644 ;; We are called from `tramp-smb-get-file-entries', which sets the
1646 (let ((line (buffer-substring (point) (point-at-eol)))
1647 localname mode size month day hour min sec year mtime
)
1651 ;; Read share entries.
1652 (when (string-match "^Disk|\\([^|]+\\)|" line
)
1653 (setq localname
(match-string 1 line
)
1661 (if (string-match "\\([0-9]+\\)$" line
)
1662 (setq year
(string-to-number (match-string 1 line
))
1663 line
(substring line
0 -
5))
1667 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
1668 (setq hour
(string-to-number (match-string 1 line
))
1669 min
(string-to-number (match-string 2 line
))
1670 sec
(string-to-number (match-string 3 line
))
1671 line
(substring line
0 -
9))
1675 (if (string-match "\\([0-9]+\\)$" line
)
1676 (setq day
(string-to-number (match-string 1 line
))
1677 line
(substring line
0 -
3))
1681 (if (string-match "\\(\\w+\\)$" line
)
1682 (setq month
(match-string 1 line
)
1683 line
(substring line
0 -
4))
1687 (if (string-match "\\(\\w+\\)$" line
)
1688 (setq line
(substring line
0 -
5))
1692 (if (string-match "\\([0-9]+\\)$" line
)
1693 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
1694 (setq size
(string-to-number (match-string 1 line
)))
1695 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
1696 (setq length
(+ length
(match-end 0))))
1697 (setq line
(substring line
0 length
)))
1700 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1701 (if (string-match "\\([ADHRSV]+\\)?$" line
)
1703 mode
(or (match-string 1 line
) "")
1704 mode
(save-match-data (format
1706 (if (string-match "D" mode
) "d" "-")
1708 (lambda (_x) "") " "
1709 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
1710 line
(substring line
0 -
6))
1714 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
1715 (setq localname
(match-string 1 line
))
1718 (when (and localname mode size
)
1720 (if (and sec min hour day month year
)
1723 (cdr (assoc (downcase month
) tramp-parse-time-months
))
1726 (list localname mode size mtime
))))
1728 (defun tramp-smb-get-cifs-capabilities (vec)
1729 "Check, whether the SMB server supports POSIX commands."
1730 ;; When we are not logged in yet, we return nil.
1731 (if (tramp-compat-process-live-p (tramp-get-connection-process vec
))
1732 (with-tramp-connection-property
1733 (tramp-get-connection-process vec
) "cifs-capabilities"
1735 (when (tramp-smb-send-command vec
"posix")
1736 (with-current-buffer (tramp-get-connection-buffer vec
)
1737 (goto-char (point-min))
1739 (re-search-forward "Server supports CIFS capabilities" nil t
)
1743 (buffer-substring (point) (point-at-eol)) nil
'omit
)))))))))
1745 (defun tramp-smb-get-stat-capability (vec)
1746 "Check, whether the SMB server supports the STAT command."
1747 ;; When we are not logged in yet, we return nil.
1748 (if (and (tramp-smb-get-share vec
)
1749 (tramp-compat-process-live-p (tramp-get-connection-process vec
)))
1750 (with-tramp-connection-property
1751 (tramp-get-connection-process vec
) "stat-capability"
1752 (tramp-smb-send-command vec
"stat \"/\""))))
1755 ;; Connection functions.
1757 (defun tramp-smb-send-command (vec command
)
1758 "Send the COMMAND to connection VEC.
1759 Returns nil if there has been an error message from smbclient."
1760 (tramp-smb-maybe-open-connection vec
)
1761 (tramp-message vec
6 "%s" command
)
1762 (tramp-send-string vec command
)
1763 (tramp-smb-wait-for-output vec
))
1765 (defun tramp-smb-maybe-open-connection (vec &optional argument
)
1766 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1767 Does not do anything if a connection is already open, but re-opens the
1768 connection if a previous connection has died for some reason.
1769 If ARGUMENT is non-nil, use it as argument for
1770 `tramp-smb-winexe-program', and suppress any checks."
1771 (tramp-check-proper-method-and-host vec
)
1773 (let* ((share (tramp-smb-get-share vec
))
1774 (buf (tramp-get-connection-buffer vec
))
1775 (p (get-buffer-process buf
)))
1777 ;; Check whether we still have the same smbclient version.
1778 ;; Otherwise, we must delete the connection cache, because
1779 ;; capabilities migh have changed.
1780 (unless (or argument
(processp p
))
1781 (let ((default-directory (tramp-compat-temporary-file-directory))
1782 (command (concat tramp-smb-program
" -V")))
1784 (unless tramp-smb-version
1785 (unless (executable-find tramp-smb-program
)
1788 "Cannot find command %s in %s" tramp-smb-program exec-path
))
1789 (setq tramp-smb-version
(shell-command-to-string command
))
1790 (tramp-message vec
6 command
)
1791 (tramp-message vec
6 "\n%s" tramp-smb-version
)
1792 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version
)
1793 (setq tramp-smb-version
1794 (replace-match "" nil nil tramp-smb-version
))))
1796 (unless (string-equal
1798 (tramp-get-connection-property
1799 vec
"smbclient-version" tramp-smb-version
))
1800 (tramp-flush-directory-property vec
"")
1801 (tramp-flush-connection-property vec
))
1803 (tramp-set-connection-property
1804 vec
"smbclient-version" tramp-smb-version
)))
1806 ;; If too much time has passed since last command was sent, look
1807 ;; whether there has been an error message; maybe due to
1808 ;; connection timeout.
1809 (with-current-buffer buf
1810 (goto-char (point-min))
1811 (when (and (> (tramp-time-diff
1813 (tramp-get-connection-property
1814 p
"last-cmd-time" '(0 0 0)))
1816 (tramp-compat-process-live-p p
)
1817 (re-search-forward tramp-smb-errors nil t
))
1821 ;; Check whether it is still the same share.
1822 (unless (and (tramp-compat-process-live-p p
)
1826 (tramp-get-connection-property p
"smb-share" ""))))
1829 ;; There might be unread output from checking for share names.
1830 (when buf
(with-current-buffer buf
(erase-buffer)))
1831 (when (and p
(processp p
)) (delete-process p
))
1833 (let* ((user (tramp-file-name-user vec
))
1834 (host (tramp-file-name-host vec
))
1835 (real-user (tramp-file-name-real-user vec
))
1836 (real-host (tramp-file-name-real-host vec
))
1837 (domain (tramp-file-name-domain vec
))
1838 (port (tramp-file-name-port vec
))
1843 (setq args
(list (concat "//" real-host
))))
1845 (setq args
(list (concat "//" real-host
"/" share
))))
1847 (setq args
(list "-g" "-L" real-host
))))
1849 (if (not (zerop (length real-user
)))
1850 (setq args
(append args
(list "-U" real-user
)))
1851 (setq args
(append args
(list "-N"))))
1853 (when domain
(setq args
(append args
(list "-W" domain
))))
1854 (when port
(setq args
(append args
(list "-p" port
))))
1855 (when tramp-smb-conf
1856 (setq args
(append args
(list "-s" tramp-smb-conf
))))
1858 (setq args
(append args
(list argument
))))
1861 (with-tramp-progress-reporter
1863 (format "Opening connection for //%s%s/%s"
1864 (if (not (zerop (length user
))) (concat user
"@") "")
1867 (let* ((coding-system-for-read nil
)
1868 (process-connection-type tramp-process-connection-type
)
1869 (p (let ((default-directory
1870 (tramp-compat-temporary-file-directory)))
1871 (apply #'start-process
1872 (tramp-get-connection-name vec
)
1873 (tramp-get-connection-buffer vec
)
1875 tramp-smb-winexe-program tramp-smb-program
)
1879 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1880 (tramp-set-connection-property p
"vector" vec
)
1881 (set-process-query-on-exit-flag p nil
)
1883 ;; Set variables for computing the prompt for reading password.
1884 (setq tramp-current-method tramp-smb-method
1885 tramp-current-user user
1886 tramp-current-host host
)
1889 (let (tramp-message-show-message)
1890 ;; Play login scenario.
1891 (tramp-process-actions
1893 (if (or argument share
)
1894 tramp-smb-actions-with-share
1895 tramp-smb-actions-without-share
))
1897 ;; Check server version.
1899 (with-current-buffer (tramp-get-connection-buffer vec
)
1900 (goto-char (point-min))
1901 (search-forward-regexp tramp-smb-server-version nil t
)
1902 (let ((smbserver-version (match-string 0)))
1906 (tramp-get-connection-property
1907 vec
"smbserver-version" smbserver-version
))
1908 (tramp-flush-directory-property vec
"")
1909 (tramp-flush-connection-property vec
))
1910 (tramp-set-connection-property
1911 vec
"smbserver-version" smbserver-version
))))
1913 ;; Set chunksize to 1. smbclient reads its input
1914 ;; character by character; if we send the string
1915 ;; at once, it is read painfully slow.
1916 (tramp-set-connection-property p
"smb-share" share
)
1917 (tramp-set-connection-property p
"chunksize" 1)
1919 ;; Mark it as connected.
1920 (tramp-set-connection-property p
"connected" t
))
1922 ;; Check for the error reason. If it was due to wrong
1923 ;; password, reestablish the connection. We cannot
1924 ;; handle this in `tramp-process-actions', because
1925 ;; smbclient does not ask for the password, again.
1927 (with-current-buffer (tramp-get-connection-buffer vec
)
1928 (goto-char (point-min))
1929 (if (and (boundp 'auth-sources
)
1930 (symbol-value 'auth-sources
)
1931 (search-forward-regexp
1932 tramp-smb-wrong-passwd-regexp nil t
))
1933 ;; Disable `auth-source' and `password-cache'.
1936 vec
3 "Retry connection with new password")
1937 (tramp-cleanup-connection vec t
)
1938 (tramp-smb-maybe-open-connection vec argument
))
1939 ;; Propagate the error.
1940 (signal (car err
) (cdr err
)))))))))))))
1942 ;; We don't use timeouts. If needed, the caller shall wrap around.
1943 (defun tramp-smb-wait-for-output (vec)
1944 "Wait for output from smbclient command.
1945 Returns nil if an error message has appeared."
1946 (with-current-buffer (tramp-get-connection-buffer vec
)
1947 (let ((p (get-buffer-process (current-buffer)))
1948 (found (progn (goto-char (point-min))
1949 (re-search-forward tramp-smb-prompt nil t
)))
1950 (err (progn (goto-char (point-min))
1951 (re-search-forward tramp-smb-errors nil t
)))
1954 ;; Algorithm: get waiting output. See if last line contains
1955 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1956 ;; If not, wait a bit and again get waiting output.
1957 (while (and (not found
) (not err
) (tramp-compat-process-live-p p
))
1959 ;; Accept pending output.
1960 (tramp-accept-process-output p
0.1)
1962 ;; Search for prompt.
1963 (goto-char (point-min))
1964 (setq found
(re-search-forward tramp-smb-prompt nil t
))
1966 ;; Search for errors.
1967 (goto-char (point-min))
1968 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1970 ;; When the process is still alive, read pending output.
1971 (while (and (not found
) (tramp-compat-process-live-p p
))
1973 ;; Accept pending output.
1974 (tramp-accept-process-output p
0.1)
1976 ;; Search for prompt.
1977 (goto-char (point-min))
1978 (setq found
(re-search-forward tramp-smb-prompt nil t
)))
1980 (tramp-message vec
6 "\n%s" (buffer-string))
1984 (goto-char (point-max))
1985 (re-search-backward tramp-smb-prompt nil t
)
1986 (delete-region (point) (point-max)))
1988 ;; Return value is whether no error message has appeared.
1991 (defun tramp-smb-kill-winexe-function ()
1992 "Send SIGKILL to the winexe process."
1994 (let ((p (get-buffer-process (current-buffer))))
1995 (when (tramp-compat-process-live-p p
)
1996 (signal-process (process-id p
) 'SIGINT
)))))
1998 (defun tramp-smb-call-winexe (vec)
1999 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
2001 ;; Check for program.
2002 (unless (executable-find tramp-smb-winexe-program
)
2004 vec
'file-error
"Cannot find program: %s" tramp-smb-winexe-program
))
2006 ;; winexe does not supports ports.
2007 (when (tramp-file-name-port vec
)
2008 (tramp-error vec
'file-error
"Port not supported for remote processes"))
2010 (tramp-smb-maybe-open-connection
2014 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch
))
2016 (set (make-local-variable 'kill-buffer-hook
)
2017 '(tramp-smb-kill-winexe-function))
2019 ;; Suppress "^M". Shouldn't we specify utf8?
2020 (set-process-coding-system (tramp-get-connection-process vec
) 'raw-text-dos
)
2022 ;; Set width to 128. This avoids mixing prompt and long error messages.
2023 (tramp-smb-send-command vec
"$rawui = (Get-Host).UI.RawUI")
2024 (tramp-smb-send-command vec
"$bufsize = $rawui.BufferSize")
2025 (tramp-smb-send-command vec
"$winsize = $rawui.WindowSize")
2026 (tramp-smb-send-command vec
"$bufsize.Width = 128")
2027 (tramp-smb-send-command vec
"$winsize.Width = 128")
2028 (tramp-smb-send-command vec
"$rawui.BufferSize = $bufsize")
2029 (tramp-smb-send-command vec
"$rawui.WindowSize = $winsize"))
2031 (defun tramp-smb-shell-quote-argument (s)
2032 "Similar to `shell-quote-argument', but uses windows cmd syntax."
2033 (let ((system-type 'ms-dos
))
2034 (shell-quote-argument s
)))
2036 (add-hook 'tramp-unload-hook
2038 (unload-feature 'tramp-smb
'force
)))
2040 (provide 'tramp-smb
)
2044 ;; * Return more comprehensive file permission string.
2046 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
2047 ;; several places, especially in `tramp-smb-handle-insert-directory'.
2049 ;; * Ignore case in file names.
2051 ;;; tramp-smb.el ends here