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