1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
30 (eval-when-compile (require 'cl
)) ; block, return
33 ;; Define SMB method ...
35 (defconst tramp-smb-method
"smb"
36 "Method to connect SAMBA and M$ SMB servers.")
38 ;; ... and add it to the method list.
40 (unless (memq system-type
'(cygwin windows-nt
))
41 (add-to-list 'tramp-methods
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
49 (tramp-tmpdir "/C$/Temp"))))
51 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
52 ;; a domain in USER, it must be the SMB method.
54 (add-to-list 'tramp-default-method-alist
55 `(nil ,tramp-prefix-domain-regexp
,tramp-smb-method
))
57 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
58 ;; the anonymous user is chosen.
60 (add-to-list 'tramp-default-user-alist
61 `(,(concat "\\`" tramp-smb-method
"\\'") nil nil
))
63 ;; Add completion function for SMB method.
65 (eval-after-load 'tramp
66 '(tramp-set-completion-function
68 '((tramp-parse-netrc "~/.netrc"))))
70 (defcustom tramp-smb-program
"smbclient"
71 "Name of SMB client to run."
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."
80 :type
'(choice (const nil
) (file :must-match t
)))
82 (defvar tramp-smb-version nil
83 "Version string of the SMB client.")
85 (defconst tramp-smb-server-version
86 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]"
87 "Regexp of SMB server identification.")
89 (defconst tramp-smb-prompt
"^\\(smb:\\|PS\\) .+> \\|^\\s-+Server\\s-+Comment$"
90 "Regexp used as prompt in smbclient or powershell.")
92 (defconst tramp-smb-wrong-passwd-regexp
94 '("NT_STATUS_LOGON_FAILURE"
95 "NT_STATUS_WRONG_PASSWORD"))
96 "Regexp for login error strings of SMB servers.")
98 (defconst tramp-smb-errors
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"
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")))
144 "Regexp for possible error strings of SMB servers.
145 Used instead of analyzing error codes of commands.")
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.
156 See `tramp-actions-before-shell' for more info.")
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.
166 See `tramp-actions-before-shell' for more info.")
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.
176 See `tramp-actions-before-shell' for more info.")
178 ;; New handlers should be added here.
179 (defconst tramp-smb-file-name-handler-alist
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
)
242 "Alist of handler functions for Tramp SMB method.
243 Operations not mentioned here will be handled by the default Emacs primitives.")
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."
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."
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."
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
))
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
)))
281 (save-match-data (apply (cdr fn
) args
))
282 (tramp-run-real-handler operation args
))))
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
)))
290 ;; File name primitives.
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
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
)
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
))
313 "File %s already exists; make it a new name anyway? "
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
)
323 (tramp-smb-send-command
327 (if (tramp-smb-get-cifs-capabilities v1
) "link" "hardlink")
328 (tramp-smb-get-localname v1
)
329 (tramp-smb-get-localname v2
)))
332 "error with add-name-to-file, see buffer `%s' for details"
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
)
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.
346 (tramp-message vec
6 (buffer-substring (point-min) (point)))
347 (delete-region (point-min) (point))
348 (throw 'tramp-action
'ok
)))))
350 (defun tramp-smb-handle-copy-directory
351 (dirname newname
&optional keep-date parents copy-contents
)
352 "Like `copy-directory' for Tramp files."
353 (setq dirname
(expand-file-name dirname
)
354 newname
(expand-file-name newname
))
355 (let ((t1 (tramp-tramp-file-p dirname
))
356 (t2 (tramp-tramp-file-p newname
)))
357 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
358 (with-tramp-progress-reporter
359 v
0 (format "Copying %s to %s" dirname newname
)
361 ;; We must use a local temporary directory.
366 tramp-temp-name-prefix
367 (tramp-compat-temporary-file-directory)))))
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
))))
374 ;; We can copy recursively.
376 (when (and (file-directory-p newname
)
377 (not (string-equal (file-name-nondirectory dirname
)
378 (file-name-nondirectory newname
))))
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
))
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
))
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
400 tramp-temp-name-prefix
401 (tramp-compat-temporary-file-directory))))
402 (args (list tramp-smb-program
403 (concat "//" real-host
"/" share
) "-E")))
405 (if (not (zerop (length real-user
)))
406 (setq args
(append args
(list "-U" real-user
)))
407 (setq args
(append args
(list "-N"))))
409 (when domain
(setq args
(append args
(list "-W" domain
))))
410 (when port
(setq args
(append args
(list "-p" port
))))
412 (setq args
(append args
(list "-s" tramp-smb-conf
))))
417 (list "-D" (shell-quote-argument localname
)
418 "-c" (shell-quote-argument "tar qc - *")
420 (shell-quote-argument tmpdir
)))
422 (append (list "tar" "cfC" "-" (shell-quote-argument dirname
)
425 (list "-D" (shell-quote-argument localname
)
426 "-c" (shell-quote-argument "tar qx -")))))
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))
437 ;; The smbclient tar command creates always complete
438 ;; paths. We must emulate the directory structure,
439 ;; and symlink to the real target.
441 (expand-file-name ".." (concat tmpdir localname
)) 'parents
)
443 newname
(directory-file-name (concat tmpdir localname
))))
445 ;; Use an asynchronous processes. By this, password
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
" "))))
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
)
458 (while (memq (process-status p
) '(run open
))
460 (tramp-message v
6 "\n%s" (buffer-string))))
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
))))
467 ;; Handle KEEP-DATE argument.
469 (set-file-times newname
(nth 5 (file-attributes dirname
))))
473 (set-file-modes newname
(tramp-default-file-modes dirname
)))
475 ;; When newname did exist, we have wrong cached values.
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
))))
481 ;; We must do it file-wise.
483 (tramp-run-real-handler
484 'copy-directory
(list dirname newname keep-date parents
))))))))
486 (defun tramp-smb-handle-copy-file
487 (filename newname
&optional ok-if-already-exists keep-date
488 preserve-uid-gid preserve-selinux-context
)
489 "Like `copy-file' for Tramp files.
490 KEEP-DATE has no effect in case NEWNAME resides on an SMB server.
491 PRESERVE-UID-GID and PRESERVE-SELINUX-CONTEXT are completely ignored."
492 (setq filename
(expand-file-name filename
)
493 newname
(expand-file-name newname
))
494 (with-tramp-progress-reporter
495 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
496 0 (format "Copying %s to %s" filename newname
)
498 (if (file-directory-p filename
)
499 (tramp-compat-copy-directory filename newname keep-date t t
)
501 (let ((tmpfile (file-local-copy filename
)))
505 (rename-file tmpfile newname ok-if-already-exists
)
507 (delete-file tmpfile
)
508 (signal (car err
) (cdr err
))))
511 (when (file-directory-p newname
)
513 (expand-file-name (file-name-nondirectory filename
) newname
)))
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
))
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
)
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
))))))
532 ;; KEEP-DATE handling.
534 (set-file-times newname
(nth 5 (file-attributes filename
))))))
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
)
543 (if (file-directory-p file
)
544 (tramp-compat-delete-directory file recursive
)
546 ;; We do not want to delete "." and "..".
548 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
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
558 (if (tramp-smb-get-cifs-capabilities v
) "posix_rmdir" "rmdir")
559 (tramp-smb-get-localname v
)))
561 (with-current-buffer (tramp-get-connection-buffer v
)
562 (goto-char (point-min))
563 (search-forward-regexp tramp-smb-errors nil t
)
565 v
'file-error
"%s `%s'" (match-string 0) directory
))))))
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
579 (if (tramp-smb-get-cifs-capabilities v
) "posix_unlink" "rm")
580 (tramp-smb-get-localname v
)))
582 (with-current-buffer (tramp-get-connection-buffer v
)
583 (goto-char (point-min))
584 (search-forward-regexp tramp-smb-errors nil t
)
586 v
'file-error
"%s `%s'" (match-string 0) filename
))))))
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.
597 (mapcar (lambda (x) (when (string-match match x
) x
))
603 (lambda (x) (expand-file-name x directory
))
605 ;; Sort them if necessary.
606 (unless nosort
(setq result
(sort result
'string-lessp
)))
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
))
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
)
627 (if (zerop (length (match-string 1 localname
)))
628 (tramp-file-name-real-user v
)
629 (match-string 1 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
638 (tramp-run-real-handler 'expand-file-name
(list localname
))))))
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
))
644 (with-parsed-tramp-file-name filename nil
645 (with-tramp-file-property
646 v localname
(format "file-attributes-%s" id-format
)
647 (if (and (tramp-smb-get-share v
) (tramp-smb-get-stat-capability v
))
648 (tramp-smb-do-file-attributes-with-stat v id-format
)
649 ;; Reading just the filename entry via "dir localname" is not
650 ;; possible, because when filename is a directory, some
651 ;; smbclient versions return the content of the directory, and
652 ;; other versions don't. Therefore, the whole content of the
653 ;; upper directory is retrieved, and the entry of the filename
654 ;; is extracted from.
655 (let* ((entries (tramp-smb-get-file-entries
656 (file-name-directory filename
)))
657 (entry (assoc (file-name-nondirectory filename
) entries
))
658 (uid (if (equal id-format
'string
) "nobody" -
1))
659 (gid (if (equal id-format
'string
) "nogroup" -
1))
660 (inode (tramp-get-inode v
))
661 (device (tramp-get-device v
)))
665 (list (and (string-match "d" (nth 1 entry
))
671 (nth 3 entry
) ;5 mtime
673 (nth 2 entry
) ;7 size
674 (nth 1 entry
) ;8 mode
676 inode
;10 inode number
677 device
)))))))) ;11 file system number
679 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format
)
680 "Implement `file-attributes' for Tramp files using stat command."
682 vec
5 "file attributes with stat: %s" (tramp-file-name-localname vec
))
683 (with-current-buffer (tramp-get-connection-buffer vec
)
684 (let* (size id link uid gid atime mtime ctime mode inode
)
685 (when (tramp-smb-send-command
686 vec
(format "stat \"%s\"" (tramp-smb-get-localname vec
)))
689 (goto-char (point-min))
690 (unless (re-search-forward tramp-smb-errors nil t
)
694 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
695 (setq size
(string-to-number (match-string 1))
696 id
(if (string-equal "directory" (match-string 2)) t
697 (if (string-equal "symbolic" (match-string 2)) ""))))
699 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
700 (setq inode
(string-to-number (match-string 1))
701 link
(string-to-number (match-string 2))))
703 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
704 (setq mode
(match-string 1)
705 uid
(if (equal id-format
'string
) (match-string 2)
706 (string-to-number (match-string 2)))
707 gid
(if (equal id-format
'string
) (match-string 3)
708 (string-to-number (match-string 3)))))
710 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
713 (string-to-number (match-string 6)) ;; sec
714 (string-to-number (match-string 5)) ;; min
715 (string-to-number (match-string 4)) ;; hour
716 (string-to-number (match-string 3)) ;; day
717 (string-to-number (match-string 2)) ;; month
718 (string-to-number (match-string 1))))) ;; year
720 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
723 (string-to-number (match-string 6)) ;; sec
724 (string-to-number (match-string 5)) ;; min
725 (string-to-number (match-string 4)) ;; hour
726 (string-to-number (match-string 3)) ;; day
727 (string-to-number (match-string 2)) ;; month
728 (string-to-number (match-string 1))))) ;; year
730 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
733 (string-to-number (match-string 6)) ;; sec
734 (string-to-number (match-string 5)) ;; min
735 (string-to-number (match-string 4)) ;; hour
736 (string-to-number (match-string 3)) ;; day
737 (string-to-number (match-string 2)) ;; month
738 (string-to-number (match-string 1)))))) ;; year
740 ;; Return the result.
741 (list id link uid gid atime mtime ctime size mode nil inode
742 (tramp-get-device vec
)))))))
744 (defun tramp-smb-handle-file-directory-p (filename)
745 "Like `file-directory-p' for Tramp files."
746 (and (file-exists-p filename
)
747 (eq ?d
(aref (nth 8 (file-attributes filename
)) 0))))
749 (defun tramp-smb-handle-file-local-copy (filename)
750 "Like `file-local-copy' for Tramp files."
751 (with-parsed-tramp-file-name filename nil
752 (unless (file-exists-p filename
)
755 "Cannot make local copy of non-existing file `%s'" filename
))
756 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
757 (with-tramp-progress-reporter
758 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
759 (unless (tramp-smb-send-command
760 v
(format "get \"%s\" \"%s\""
761 (tramp-smb-get-localname v
) tmpfile
))
762 ;; Oops, an error. We shall cleanup.
763 (delete-file tmpfile
)
765 v
'file-error
"Cannot make local copy of file `%s'" filename
)))
768 ;; This function should return "foo/" for directories and "bar" for
770 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
771 "Like `file-name-all-completions' for Tramp files."
774 (with-parsed-tramp-file-name directory nil
775 (with-tramp-file-property v localname
"file-name-all-completions"
777 (let ((entries (tramp-smb-get-file-entries directory
)))
781 (if (string-match "d" (nth 1 x
))
782 (file-name-as-directory (nth 0 x
))
786 (defun tramp-smb-handle-file-writable-p (filename)
787 "Like `file-writable-p' for Tramp files."
788 (if (file-exists-p filename
)
789 (string-match "w" (or (nth 8 (file-attributes filename
)) ""))
790 (let ((dir (file-name-directory filename
)))
791 (and (file-exists-p dir
)
792 (file-writable-p dir
)))))
794 (defun tramp-smb-handle-insert-directory
795 (filename switches
&optional wildcard full-directory-p
)
796 "Like `insert-directory' for Tramp files."
797 (setq filename
(expand-file-name filename
))
799 ;; Called from `dired-add-entry'.
800 (setq filename
(file-name-as-directory filename
))
801 (setq filename
(directory-file-name filename
)))
802 (with-parsed-tramp-file-name filename nil
804 (let ((base (file-name-nondirectory filename
))
805 ;; We should not destroy the cache entry.
806 (entries (copy-sequence
807 (tramp-smb-get-file-entries
808 (file-name-directory filename
)))))
811 (string-match "\\." base
)
812 (setq base
(replace-match "\\\\." nil nil base
))
813 (string-match "\\*" base
)
814 (setq base
(replace-match ".*" nil nil base
))
815 (string-match "\\?" base
)
816 (setq base
(replace-match ".?" nil nil base
)))
822 (if (or wildcard
(zerop (length base
)))
823 ;; Check for matching entries.
827 (format "^%s" base
) (nth 0 x
))
830 ;; We just need the only and only entry FILENAME.
831 (list (assoc base entries
)))))
838 (if (string-match "t" switches
)
840 (tramp-time-less-p (nth 3 y
) (nth 3 x
))
842 (string-lessp (nth 0 x
) (nth 0 y
))))))
844 ;; Handle "-F" switch.
845 (when (string-match "F" switches
)
848 (when (not (zerop (length (car x
))))
850 ((char-equal ?d
(string-to-char (nth 1 x
)))
851 (setcar x
(concat (car x
) "/")))
852 ((char-equal ?x
(string-to-char (nth 1 x
)))
853 (setcar x
(concat (car x
) "*"))))))
859 (when (not (zerop (length (nth 0 x
))))
861 (when (tramp-smb-get-stat-capability v
)
863 (file-attributes filename
'string
)))))
866 "%10s %3d %-8s %-8s %8s %s "
867 (or (nth 8 attr
) (nth 1 x
)) ; mode
868 (or (nth 1 attr
) 1) ; inode
869 (or (nth 2 attr
) "nobody") ; uid
870 (or (nth 3 attr
) "nogroup") ; gid
871 (or (nth 7 attr
) (nth 2 x
)) ; size
873 (if (tramp-time-less-p
874 (tramp-time-subtract (current-time) (nth 3 x
))
879 ;; We mark the file name. The inserted name could be
880 ;; from somewhere else, so we use the relative file
881 ;; name of `default-directory'.
882 (let ((start (point)))
888 (nth 0 x
) (file-name-directory filename
)))))
889 (put-text-property start
(1- (point)) 'dired-filename t
))
891 (beginning-of-line))))
894 (defun tramp-smb-handle-make-directory (dir &optional parents
)
895 "Like `make-directory' for Tramp files."
896 (setq dir
(directory-file-name (expand-file-name dir
)))
897 (unless (file-name-absolute-p dir
)
898 (setq dir
(expand-file-name dir default-directory
)))
899 (with-parsed-tramp-file-name dir nil
901 (let* ((ldir (file-name-directory dir
)))
902 ;; Make missing directory parts.
904 (tramp-smb-get-share v
)
905 (not (file-directory-p ldir
)))
906 (make-directory ldir parents
))
908 (when (file-directory-p ldir
)
909 (make-directory-internal dir
))
910 (unless (file-directory-p dir
)
911 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
913 (defun tramp-smb-handle-make-directory-internal (directory)
914 "Like `make-directory-internal' for Tramp files."
915 (setq directory
(directory-file-name (expand-file-name directory
)))
916 (unless (file-name-absolute-p directory
)
917 (setq directory
(expand-file-name directory default-directory
)))
918 (with-parsed-tramp-file-name directory nil
920 (let* ((file (tramp-smb-get-localname v
)))
921 (when (file-directory-p (file-name-directory directory
))
922 (tramp-smb-send-command
924 (if (tramp-smb-get-cifs-capabilities v
)
926 "posix_mkdir \"%s\" %s"
927 file
(tramp-compat-decimal-to-octal (default-file-modes)))
928 (format "mkdir \"%s\"" file
)))
929 ;; We must also flush the cache of the directory, because
930 ;; `file-attributes' reads the values from there.
931 (tramp-flush-file-property v
(file-name-directory localname
))
932 (tramp-flush-file-property v localname
))
933 (unless (file-directory-p directory
)
935 v
'file-error
"Couldn't make directory %s" directory
))))))
937 (defun tramp-smb-handle-make-symbolic-link
938 (filename linkname
&optional ok-if-already-exists
)
939 "Like `make-symbolic-link' for Tramp files.
940 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
941 the symlink. If LINKNAME is a Tramp file, only the localname component is
942 used as the target of the symlink.
944 If LINKNAME is a Tramp file and the localname component is relative, then
945 it is expanded first, before the localname component is taken. Note that
946 this can give surprising results if the user/host for the source and
947 target of the symlink differ."
948 (unless (tramp-equal-remote filename linkname
)
949 (with-parsed-tramp-file-name
950 (if (tramp-tramp-file-p filename
) filename linkname
) nil
953 "make-symbolic-link: %s"
954 "only implemented for same method, same user, same host")))
955 (with-parsed-tramp-file-name filename v1
956 (with-parsed-tramp-file-name linkname v2
957 (when (file-directory-p filename
)
960 "make-symbolic-link: %s must not be a directory" filename
))
961 (when (and (not ok-if-already-exists
)
962 (file-exists-p linkname
)
963 (not (numberp ok-if-already-exists
))
966 "File %s already exists; make it a new name anyway? "
970 "make-symbolic-link: file %s already exists" linkname
))
971 (unless (tramp-smb-get-cifs-capabilities v1
)
972 (tramp-error v2
'file-error
"make-symbolic-link not supported"))
973 ;; We must also flush the cache of the directory, because
974 ;; `file-attributes' reads the values from there.
975 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
976 (tramp-flush-file-property v2 v2-localname
)
978 (tramp-smb-send-command
981 "symlink \"%s\" \"%s\""
982 (tramp-smb-get-localname v1
)
983 (tramp-smb-get-localname v2
)))
986 "error with make-symbolic-link, see buffer `%s' for details"
989 (defun tramp-smb-handle-process-file
990 (program &optional infile destination display
&rest args
)
991 "Like `process-file' for Tramp files."
992 ;; The implementation is not complete yet.
993 (when (and (numberp destination
) (zerop destination
))
994 (error "Implementation does not handle immediate return"))
996 (with-parsed-tramp-file-name default-directory nil
997 (let* ((name (file-name-nondirectory program
))
1000 input tmpinput outbuf command ret
)
1004 (setq infile
(expand-file-name infile
))
1005 (if (tramp-equal-remote default-directory infile
)
1006 ;; INFILE is on the same remote host.
1007 (setq input
(with-parsed-tramp-file-name infile nil localname
))
1008 ;; INFILE must be copied to remote host.
1009 (setq input
(tramp-make-tramp-temp-file v
)
1010 tmpinput
(tramp-make-tramp-file-name method user host input
))
1011 (copy-file infile tmpinput t
))
1012 ;; Transform input into a filename powershell does understand.
1013 (setq input
(format "//%s%s" host input
)))
1015 ;; Determine output.
1018 ((bufferp destination
)
1019 (setq outbuf destination
))
1021 ((stringp destination
)
1022 (setq outbuf
(get-buffer-create destination
)))
1023 ;; (REAL-DESTINATION ERROR-DESTINATION)
1024 ((consp destination
)
1027 ((bufferp (car destination
))
1028 (setq outbuf
(car destination
)))
1029 ((stringp (car destination
))
1030 (setq outbuf
(get-buffer-create (car destination
))))
1032 (setq outbuf
(current-buffer))))
1034 (tramp-message v
2 "%s" "STDERR not supported"))
1037 (setq outbuf
(current-buffer))))
1039 ;; Construct command.
1040 (setq command
(mapconcat 'identity
(cons program args
) " ")
1043 "get-content %s | & %s"
1044 (tramp-smb-shell-quote-argument input
) command
)
1045 (format "& %s" command
)))
1047 (while (get-process name1
)
1048 ;; NAME must be unique as process name.
1050 name1
(format "%s<%d>" name i
)))
1052 ;; Set the new process properties.
1053 (tramp-set-connection-property v
"process-name" name1
)
1054 (tramp-set-connection-property
1056 (or outbuf
(generate-new-buffer tramp-temp-buffer-name
)))
1060 (with-current-buffer (tramp-get-connection-buffer v
)
1061 ;; Preserve buffer contents.
1062 (narrow-to-region (point-max) (point-max))
1063 (tramp-smb-call-winexe v
)
1064 (when (tramp-smb-get-share v
)
1065 (tramp-smb-send-command
1066 v
(format "cd \"//%s%s\"" host
(file-name-directory localname
))))
1067 (tramp-smb-send-command v command
)
1068 ;; Preserve command output.
1069 (narrow-to-region (point-max) (point-max))
1070 (let ((p (tramp-get-connection-process v
)))
1071 (tramp-smb-send-command v
"exit $lasterrorcode")
1072 (while (memq (process-status p
) '(run open
))
1074 (setq ret
(process-exit-status p
))))
1075 (delete-region (point-min) (point-max))
1078 ;; When the user did interrupt, we should do it also. We use
1079 ;; return code -1 as marker.
1086 ;; We should show the output anyway.
1087 (when (and outbuf display
) (display-buffer outbuf
))
1089 ;; Cleanup. We remove all file cache values for the connection,
1090 ;; because the remote process could have changed them.
1091 (tramp-set-connection-property v
"process-name" nil
)
1092 (tramp-set-connection-property v
"process-buffer" nil
)
1093 (when tmpinput
(delete-file tmpinput
))
1095 (kill-buffer (tramp-get-connection-property v
"process-buffer" nil
)))
1097 ;; `process-file-side-effects' has been introduced with GNU
1098 ;; Emacs 23.2. If set to `nil', no remote file will be changed
1099 ;; by `program'. If it doesn't exist, we assume its default
1101 (unless (and (boundp 'process-file-side-effects
)
1102 (not (symbol-value 'process-file-side-effects
)))
1103 (tramp-flush-directory-property v
""))
1105 ;; Return exit status.
1110 (defun tramp-smb-handle-rename-file
1111 (filename newname
&optional ok-if-already-exists
)
1112 "Like `rename-file' for Tramp files."
1113 (setq filename
(expand-file-name filename
)
1114 newname
(expand-file-name newname
))
1116 (when (and (not ok-if-already-exists
)
1117 (file-exists-p newname
))
1119 (tramp-dissect-file-name
1120 (if (file-remote-p filename
) filename newname
))
1121 'file-already-exists newname
))
1123 (with-tramp-progress-reporter
1124 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
1125 0 (format "Renaming %s to %s" filename newname
)
1127 (if (and (tramp-equal-remote filename newname
)
1129 (tramp-smb-get-share (tramp-dissect-file-name filename
))
1130 (tramp-smb-get-share (tramp-dissect-file-name newname
))))
1131 ;; We can rename directly.
1132 (with-parsed-tramp-file-name filename v1
1133 (with-parsed-tramp-file-name newname v2
1135 ;; We must also flush the cache of the directory, because
1136 ;; `file-attributes' reads the values from there.
1137 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1138 (tramp-flush-file-property v2 v2-localname
)
1139 (unless (tramp-smb-get-share v2
)
1141 v2
'file-error
"Target `%s' must contain a share name" newname
))
1142 (unless (tramp-smb-send-command
1143 v2
(format "rename \"%s\" \"%s\""
1144 (tramp-smb-get-localname v1
)
1145 (tramp-smb-get-localname v2
)))
1146 (tramp-error v2
'file-error
"Cannot rename `%s'" filename
))))
1148 ;; We must rename via copy.
1149 (tramp-compat-copy-file filename newname ok-if-already-exists t t t
)
1150 (if (file-directory-p filename
)
1151 (tramp-compat-delete-directory filename
'recursive
)
1152 (delete-file filename
)))))
1154 (defun tramp-smb-handle-set-file-modes (filename mode
)
1155 "Like `set-file-modes' for Tramp files."
1156 (with-parsed-tramp-file-name filename nil
1157 (when (tramp-smb-get-cifs-capabilities v
)
1158 (tramp-flush-file-property v localname
)
1159 (unless (tramp-smb-send-command
1160 v
(format "chmod \"%s\" %s"
1161 (tramp-smb-get-localname v
)
1162 (tramp-compat-decimal-to-octal mode
)))
1164 v
'file-error
"Error while changing file's mode %s" filename
)))))
1166 ;; We use BUFFER also as connection buffer during setup. Because of
1167 ;; this, its original contents must be saved, and restored once
1168 ;; connection has been setup.
1169 (defun tramp-smb-handle-start-file-process (name buffer program
&rest args
)
1170 "Like `start-file-process' for Tramp files."
1171 (with-parsed-tramp-file-name default-directory nil
1172 (let ((command (mapconcat 'identity
(cons program args
) " "))
1173 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
1180 ;; BUFFER can be nil. We use a temporary buffer.
1181 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
1182 (while (get-process name1
)
1183 ;; NAME must be unique as process name.
1185 name1
(format "%s<%d>" name i
)))
1186 ;; Set the new process properties.
1187 (tramp-set-connection-property v
"process-name" name1
)
1188 (tramp-set-connection-property v
"process-buffer" buffer
)
1189 ;; Activate narrowing in order to save BUFFER contents.
1190 (with-current-buffer (tramp-get-connection-buffer v
)
1191 (let ((buffer-undo-list t
))
1192 (narrow-to-region (point-max) (point-max))
1193 (tramp-smb-call-winexe v
)
1194 (when (tramp-smb-get-share v
)
1195 (tramp-smb-send-command
1198 host
(file-name-directory localname
))))
1199 (tramp-message v
6 "(%s); exit" command
)
1200 (tramp-send-string v command
)))
1202 (tramp-get-connection-process v
)))
1205 (with-current-buffer (tramp-get-connection-buffer v
)
1206 (if (string-match tramp-temp-buffer-name
(buffer-name))
1208 (set-process-buffer (tramp-get-connection-process v
) nil
)
1209 (kill-buffer (current-buffer)))
1210 (set-buffer-modified-p bmp
)))
1211 (tramp-set-connection-property v
"process-name" nil
)
1212 (tramp-set-connection-property v
"process-buffer" nil
)))))
1214 (defun tramp-smb-handle-substitute-in-file-name (filename)
1215 "Like `handle-substitute-in-file-name' for Tramp files.
1216 \"//\" substitutes only in the local filename part. Catches
1217 errors for shares like \"C$/\", which are common in Microsoft Windows."
1218 (with-parsed-tramp-file-name filename nil
1219 ;; Ignore in LOCALNAME everything before "//".
1220 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
1222 (concat (file-remote-p filename
)
1223 (replace-match "\\1" nil nil localname
)))))
1225 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
1228 (defun tramp-smb-handle-write-region
1229 (start end filename
&optional append visit lockname confirm
)
1230 "Like `write-region' for Tramp files."
1231 (setq filename
(expand-file-name filename
))
1232 (with-parsed-tramp-file-name filename nil
1233 (unless (eq append nil
)
1235 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
1236 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1237 (when (and (not (featurep 'xemacs
))
1238 confirm
(file-exists-p filename
))
1239 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1241 (tramp-error v
'file-error
"File not overwritten")))
1242 ;; We must also flush the cache of the directory, because
1243 ;; `file-attributes' reads the values from there.
1244 (tramp-flush-file-property v
(file-name-directory localname
))
1245 (tramp-flush-file-property v localname
)
1246 (let ((curbuf (current-buffer))
1247 (tmpfile (tramp-compat-make-temp-file filename
)))
1248 ;; We say `no-message' here because we don't want the visited file
1249 ;; modtime data to be clobbered from the temp file. We call
1250 ;; `set-visited-file-modtime' ourselves later on.
1251 (tramp-run-real-handler
1253 (if confirm
; don't pass this arg unless defined for backward compat.
1254 (list start end tmpfile append
'no-message lockname confirm
)
1255 (list start end tmpfile append
'no-message lockname
)))
1257 (with-tramp-progress-reporter
1258 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
1260 (unless (tramp-smb-send-command
1261 v
(format "put %s \"%s\""
1262 tmpfile
(tramp-smb-get-localname v
)))
1263 (tramp-error v
'file-error
"Cannot write `%s'" filename
))
1264 (delete-file tmpfile
)))
1266 (unless (equal curbuf
(current-buffer))
1269 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer)))
1271 (set-visited-file-modtime)))))
1274 ;; Internal file name functions.
1276 (defun tramp-smb-get-share (vec)
1277 "Returns the share name of LOCALNAME."
1279 (let ((localname (tramp-file-name-localname vec
)))
1280 (when (string-match "^/?\\([^/]+\\)/" localname
)
1281 (match-string 1 localname
)))))
1283 (defun tramp-smb-get-localname (vec)
1284 "Returns the file name of LOCALNAME.
1285 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1287 (let ((localname (tramp-file-name-localname vec
)))
1290 (if (string-match "^/?[^/]+\\(/.*\\)" localname
)
1291 ;; There is a share, separated by "/".
1292 (if (not (tramp-smb-get-cifs-capabilities vec
))
1294 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
1295 (match-string 1 localname
) "")
1296 (match-string 1 localname
))
1297 ;; There is just a share.
1298 (if (string-match "^/?\\([^/]+\\)$" localname
)
1299 (match-string 1 localname
)
1302 ;; Sometimes we have discarded `substitute-in-file-name'.
1303 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname
)
1304 (setq localname
(replace-match "$" nil nil localname
1)))
1308 ;; Share names of a host are cached. It is very unlikely that the
1309 ;; shares do change during connection.
1310 (defun tramp-smb-get-file-entries (directory)
1311 "Read entries which match DIRECTORY.
1312 Either the shares are listed, or the `dir' command is executed.
1313 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1314 (with-parsed-tramp-file-name (file-name-as-directory directory
) nil
1315 (setq localname
(or localname
"/"))
1316 (with-tramp-file-property v localname
"file-entries"
1317 (with-current-buffer (tramp-get-connection-buffer v
)
1318 (let* ((share (tramp-smb-get-share v
))
1319 (cache (tramp-get-connection-property v
"share-cache" nil
))
1322 (if (and (not share
) cache
)
1323 ;; Return cached shares.
1328 (tramp-smb-send-command
1329 v
(format "dir \"%s*\"" (tramp-smb-get-localname v
)))
1330 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1331 (tramp-smb-maybe-open-connection v
))
1333 ;; Loop the listing.
1334 (goto-char (point-min))
1335 (if (re-search-forward tramp-smb-errors nil t
)
1336 (tramp-error v
'file-error
"%s `%s'" (match-string 0) directory
)
1338 (setq entry
(tramp-smb-read-file-entry share
))
1340 (when entry
(add-to-list 'res entry
))))
1342 ;; Cache share entries.
1344 (tramp-set-connection-property v
"share-cache" res
)))
1346 ;; Add directory itself.
1347 (add-to-list 'res
'("" "drwxrwxrwx" 0 (0 0)))
1349 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1350 ;; If there's no short delay, it returns nil. No idea about.
1351 (when (featurep 'xemacs
) (sleep-for 0.01))
1356 ;; Return either a share name (if SHARE is nil), or a file name.
1358 ;; If shares are listed, the following format is expected:
1360 ;; Disk| - leading spaces
1361 ;; [^|]+| - share name, 14 char
1364 ;; Entries provided by smbclient DIR aren't fully regular.
1365 ;; They should have the format
1367 ;; \s-\{2,2} - leading spaces
1368 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1369 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1370 ;; \s- - space delimiter
1371 ;; \s-+[0-9]+ - size, 8 chars, right bound
1372 ;; \s-\{2,2\} - space delimiter
1373 ;; \w\{3,3\} - weekday
1374 ;; \s- - space delimiter
1375 ;; \w\{3,3\} - month
1376 ;; \s- - space delimiter
1378 ;; \s- - space delimiter
1379 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1380 ;; \s- - space delimiter
1381 ;; [0-9]\{4,4\} - year
1383 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1384 ;; has function display_finfo:
1386 ;; d_printf(" %-30s%7.7s %8.0f %s",
1388 ;; attrib_string(finfo->mode),
1389 ;; (double)finfo->size,
1390 ;; asctime(LocalTime(&t)));
1392 ;; in Samba 1.9, there's the following code:
1394 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1395 ;; CNV_LANG(finfo->name),
1396 ;; attrib_string(finfo->mode),
1398 ;; asctime(LocalTime(&t))));
1401 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1402 ;; available in older Emacsen.
1403 ;; * The length of constructs (file name, size) might exceed the default.
1404 ;; * File names might contain spaces.
1405 ;; * Permissions might be empty.
1407 ;; So we try to analyze backwards.
1408 (defun tramp-smb-read-file-entry (share)
1409 "Parse entry in SMB output buffer.
1410 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1411 Result is the list (LOCALNAME MODE SIZE MTIME)."
1412 ;; We are called from `tramp-smb-get-file-entries', which sets the
1414 (let ((line (buffer-substring (point) (point-at-eol)))
1415 localname mode size month day hour min sec year mtime
)
1419 ;; Read share entries.
1420 (when (string-match "^Disk|\\([^|]+\\)|" line
)
1421 (setq localname
(match-string 1 line
)
1429 (if (string-match "\\([0-9]+\\)$" line
)
1430 (setq year
(string-to-number (match-string 1 line
))
1431 line
(substring line
0 -
5))
1435 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
1436 (setq hour
(string-to-number (match-string 1 line
))
1437 min
(string-to-number (match-string 2 line
))
1438 sec
(string-to-number (match-string 3 line
))
1439 line
(substring line
0 -
9))
1443 (if (string-match "\\([0-9]+\\)$" line
)
1444 (setq day
(string-to-number (match-string 1 line
))
1445 line
(substring line
0 -
3))
1449 (if (string-match "\\(\\w+\\)$" line
)
1450 (setq month
(match-string 1 line
)
1451 line
(substring line
0 -
4))
1455 (if (string-match "\\(\\w+\\)$" line
)
1456 (setq line
(substring line
0 -
5))
1460 (if (string-match "\\([0-9]+\\)$" line
)
1461 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
1462 (setq size
(string-to-number (match-string 1 line
)))
1463 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
1464 (setq length
(+ length
(match-end 0))))
1465 (setq line
(substring line
0 length
)))
1468 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1469 (if (string-match "\\([ADHRSV]+\\)?$" line
)
1471 mode
(or (match-string 1 line
) "")
1472 mode
(save-match-data (format
1474 (if (string-match "D" mode
) "d" "-")
1477 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
1478 line
(substring line
0 -
6))
1482 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
1483 (setq localname
(match-string 1 line
))
1486 (when (and localname mode size
)
1488 (if (and sec min hour day month year
)
1491 (cdr (assoc (downcase month
) tramp-parse-time-months
))
1494 (list localname mode size mtime
))))
1496 (defun tramp-smb-get-cifs-capabilities (vec)
1497 "Check, whether the SMB server supports POSIX commands."
1498 ;; When we are not logged in yet, we return nil.
1499 (if (let ((p (tramp-get-connection-process vec
)))
1500 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1501 (with-tramp-connection-property
1502 (tramp-get-connection-process vec
) "cifs-capabilities"
1504 (when (tramp-smb-send-command vec
"posix")
1505 (with-current-buffer (tramp-get-connection-buffer vec
)
1506 (goto-char (point-min))
1508 (re-search-forward "Server supports CIFS capabilities" nil t
)
1512 (buffer-substring (point) (point-at-eol)) nil t
)))))))))
1514 (defun tramp-smb-get-stat-capability (vec)
1515 "Check, whether the SMB server supports the STAT command."
1516 ;; When we are not logged in yet, we return nil.
1517 (if (let ((p (tramp-get-connection-process vec
)))
1518 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1519 (with-tramp-connection-property
1520 (tramp-get-connection-process vec
) "stat-capability"
1521 (tramp-smb-send-command vec
"stat ."))))
1524 ;; Connection functions.
1526 (defun tramp-smb-send-command (vec command
)
1527 "Send the COMMAND to connection VEC.
1528 Returns nil if there has been an error message from smbclient."
1529 (tramp-smb-maybe-open-connection vec
)
1530 (tramp-message vec
6 "%s" command
)
1531 (tramp-send-string vec command
)
1532 (tramp-smb-wait-for-output vec
))
1534 (defun tramp-smb-maybe-open-connection (vec &optional argument
)
1535 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1536 Does not do anything if a connection is already open, but re-opens the
1537 connection if a previous connection has died for some reason.
1538 If ARGUMENT is non-nil, use it as argument for
1539 `tramp-smb-winexe-program', and suppress any checks."
1540 (let* ((share (tramp-smb-get-share vec
))
1541 (buf (tramp-get-connection-buffer vec
))
1542 (p (get-buffer-process buf
)))
1544 ;; Check whether we still have the same smbclient version.
1545 ;; Otherwise, we must delete the connection cache, because
1546 ;; capabilities migh have changed.
1547 (unless (or argument
(processp p
))
1548 (let ((default-directory (tramp-compat-temporary-file-directory))
1549 (command (concat tramp-smb-program
" -V")))
1551 (unless tramp-smb-version
1552 (unless (executable-find tramp-smb-program
)
1555 "Cannot find command %s in %s" tramp-smb-program exec-path
))
1556 (setq tramp-smb-version
(shell-command-to-string command
))
1557 (tramp-message vec
6 command
)
1558 (tramp-message vec
6 "\n%s" tramp-smb-version
)
1559 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version
)
1560 (setq tramp-smb-version
1561 (replace-match "" nil nil tramp-smb-version
))))
1563 (unless (string-equal
1565 (tramp-get-connection-property
1566 vec
"smbclient-version" tramp-smb-version
))
1567 (tramp-flush-directory-property vec
"")
1568 (tramp-flush-connection-property vec
))
1570 (tramp-set-connection-property
1571 vec
"smbclient-version" tramp-smb-version
)))
1573 ;; If too much time has passed since last command was sent, look
1574 ;; whether there has been an error message; maybe due to
1575 ;; connection timeout.
1576 (with-current-buffer buf
1577 (goto-char (point-min))
1578 (when (and (> (tramp-time-diff
1580 (tramp-get-connection-property
1581 p
"last-cmd-time" '(0 0 0)))
1583 p
(processp p
) (memq (process-status p
) '(run open
))
1584 (re-search-forward tramp-smb-errors nil t
))
1588 ;; Check whether it is still the same share.
1590 (and p
(processp p
) (memq (process-status p
) '(run open
))
1594 (tramp-get-connection-property p
"smb-share" ""))))
1597 ;; There might be unread output from checking for share names.
1598 (when buf
(with-current-buffer buf
(erase-buffer)))
1599 (when (and p
(processp p
)) (delete-process p
))
1601 (let* ((user (tramp-file-name-user vec
))
1602 (host (tramp-file-name-host vec
))
1603 (real-user (tramp-file-name-real-user vec
))
1604 (real-host (tramp-file-name-real-host vec
))
1605 (domain (tramp-file-name-domain vec
))
1606 (port (tramp-file-name-port vec
))
1611 (setq args
(list (concat "//" real-host
))))
1613 (setq args
(list (concat "//" real-host
"/" share
))))
1615 (setq args
(list "-g" "-L" real-host
))))
1617 (if (not (zerop (length real-user
)))
1618 (setq args
(append args
(list "-U" real-user
)))
1619 (setq args
(append args
(list "-N"))))
1621 (when domain
(setq args
(append args
(list "-W" domain
))))
1622 (when port
(setq args
(append args
(list "-p" port
))))
1623 (when tramp-smb-conf
1624 (setq args
(append args
(list "-s" tramp-smb-conf
))))
1626 (setq args
(append args
(list argument
))))
1629 (with-tramp-progress-reporter
1631 (format "Opening connection for //%s%s/%s"
1632 (if (not (zerop (length user
))) (concat user
"@") "")
1635 (let* ((coding-system-for-read nil
)
1636 (process-connection-type tramp-process-connection-type
)
1637 (p (let ((default-directory
1638 (tramp-compat-temporary-file-directory)))
1639 (apply #'start-process
1640 (tramp-get-connection-name vec
)
1641 (tramp-get-connection-buffer vec
)
1643 tramp-smb-winexe-program tramp-smb-program
)
1647 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1648 (tramp-compat-set-process-query-on-exit-flag p nil
)
1650 ;; Set variables for computing the prompt for reading password.
1651 (setq tramp-current-method tramp-smb-method
1652 tramp-current-user user
1653 tramp-current-host host
)
1656 (let (tramp-message-show-message)
1657 ;; Play login scenario.
1658 (tramp-process-actions
1660 (if (or argument share
)
1661 tramp-smb-actions-with-share
1662 tramp-smb-actions-without-share
))
1664 ;; Check server version.
1666 (with-current-buffer (tramp-get-connection-buffer vec
)
1667 (goto-char (point-min))
1668 (search-forward-regexp tramp-smb-server-version nil t
)
1669 (let ((smbserver-version (match-string 0)))
1673 (tramp-get-connection-property
1674 vec
"smbserver-version" smbserver-version
))
1675 (tramp-flush-directory-property vec
"")
1676 (tramp-flush-connection-property vec
))
1677 (tramp-set-connection-property
1678 vec
"smbserver-version" smbserver-version
))))
1680 ;; Set chunksize to 1. smbclient reads its input
1681 ;; character by character; if we send the string
1682 ;; at once, it is read painfully slow.
1683 (tramp-set-connection-property p
"smb-share" share
)
1684 (tramp-set-connection-property p
"chunksize" 1))
1686 ;; Check for the error reason. If it was due to wrong
1687 ;; password, reestablish the connection. We cannot
1688 ;; handle this in `tramp-process-actions', because
1689 ;; smbclient does not ask for the password, again.
1691 (with-current-buffer (tramp-get-connection-buffer vec
)
1692 (goto-char (point-min))
1693 (if (search-forward-regexp
1694 tramp-smb-wrong-passwd-regexp nil t
)
1695 ;; Disable `auth-source' and `password-cache'.
1698 (tramp-smb-maybe-open-connection vec argument
))
1699 ;; Propagate the error.
1700 (signal (car err
) (cdr err
)))))))))))))
1702 ;; We don't use timeouts. If needed, the caller shall wrap around.
1703 (defun tramp-smb-wait-for-output (vec)
1704 "Wait for output from smbclient command.
1705 Returns nil if an error message has appeared."
1706 (with-current-buffer (tramp-get-connection-buffer vec
)
1707 (let ((p (get-buffer-process (current-buffer)))
1708 (found (progn (goto-char (point-min))
1709 (re-search-forward tramp-smb-prompt nil t
)))
1710 (err (progn (goto-char (point-min))
1711 (re-search-forward tramp-smb-errors nil t
)))
1714 ;; Algorithm: get waiting output. See if last line contains
1715 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1716 ;; If not, wait a bit and again get waiting output.
1717 (while (and (not found
) (not err
) (memq (process-status p
) '(run open
)))
1719 ;; Accept pending output.
1720 (tramp-accept-process-output p
0.1)
1722 ;; Search for prompt.
1723 (goto-char (point-min))
1724 (setq found
(re-search-forward tramp-smb-prompt nil t
))
1726 ;; Search for errors.
1727 (goto-char (point-min))
1728 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1730 ;; When the process is still alive, read pending output.
1731 (while (and (not found
) (memq (process-status p
) '(run open
)))
1733 ;; Accept pending output.
1734 (tramp-accept-process-output p
0.1)
1736 ;; Search for prompt.
1737 (goto-char (point-min))
1738 (setq found
(re-search-forward tramp-smb-prompt nil t
)))
1740 (tramp-message vec
6 "\n%s" (buffer-string))
1744 (goto-char (point-max))
1745 (re-search-backward tramp-smb-prompt nil t
)
1746 (delete-region (point) (point-max)))
1748 ;; Return value is whether no error message has appeared.
1751 (defun tramp-smb-kill-winexe-function ()
1752 "Send SIGKILL to the winexe process."
1754 (let ((p (get-buffer-process (current-buffer))))
1755 (when (and p
(processp p
) (memq (process-status p
) '(run open
)))
1756 (signal-process (process-id p
) 'SIGINT
)))))
1758 (defun tramp-smb-call-winexe (vec)
1759 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
1761 ;; We call `tramp-get-buffer' in order to get a debug buffer for
1763 (tramp-get-buffer vec
)
1765 ;; Check for program.
1766 (unless (let ((default-directory
1767 (tramp-compat-temporary-file-directory)))
1768 (executable-find tramp-smb-winexe-program
))
1770 vec
'file-error
"Cannot find program: %s" tramp-smb-winexe-program
))
1772 ;; winexe does not supports ports.
1773 (when (tramp-file-name-port vec
)
1774 (tramp-error vec
'file-error
"Port not supported for remote processes"))
1776 (tramp-smb-maybe-open-connection
1780 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch
))
1782 (set (make-local-variable 'kill-buffer-hook
)
1783 '(tramp-smb-kill-winexe-function))
1785 ;; Suppress "^M". Shouldn't we specify utf8?
1786 (set-process-coding-system (tramp-get-connection-process vec
) 'raw-text-dos
)
1788 ;; Set width to 128. This avoids mixing prompt and long error messages.
1789 (tramp-smb-send-command vec
"$rawui = (Get-Host).UI.RawUI")
1790 (tramp-smb-send-command vec
"$bufsize = $rawui.BufferSize")
1791 (tramp-smb-send-command vec
"$winsize = $rawui.WindowSize")
1792 (tramp-smb-send-command vec
"$bufsize.Width = 128")
1793 (tramp-smb-send-command vec
"$winsize.Width = 128")
1794 (tramp-smb-send-command vec
"$rawui.BufferSize = $bufsize")
1795 (tramp-smb-send-command vec
"$rawui.WindowSize = $winsize"))
1797 (defun tramp-smb-shell-quote-argument (s)
1798 "Similar to `shell-quote-argument', but uses windows cmd syntax."
1799 (let ((system-type 'ms-dos
))
1800 (shell-quote-argument s
)))
1802 (add-hook 'tramp-unload-hook
1804 (unload-feature 'tramp-smb
'force
)))
1806 (provide 'tramp-smb
)
1810 ;; * Return more comprehensive file permission string.
1811 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1812 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1813 ;; * Ignore case in file names.
1815 ;;; tramp-smb.el ends here