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