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