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