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