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 (tramp-with-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 (tramp-with-progress-reporter
495 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
496 0 (format "Copying %s to %s" filename newname
)
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-file-property v localname
(format "file-attributes-%s" id-format
)
646 (if (and (tramp-smb-get-share v
) (tramp-smb-get-stat-capability v
))
647 (tramp-smb-do-file-attributes-with-stat v id-format
)
648 ;; Reading just the filename entry via "dir localname" is not
649 ;; possible, because when filename is a directory, some
650 ;; smbclient versions return the content of the directory, and
651 ;; other versions don't. Therefore, the whole content of the
652 ;; upper directory is retrieved, and the entry of the filename
653 ;; is extracted from.
654 (let* ((entries (tramp-smb-get-file-entries
655 (file-name-directory filename
)))
656 (entry (assoc (file-name-nondirectory filename
) entries
))
657 (uid (if (equal id-format
'string
) "nobody" -
1))
658 (gid (if (equal id-format
'string
) "nogroup" -
1))
659 (inode (tramp-get-inode v
))
660 (device (tramp-get-device v
)))
664 (list (and (string-match "d" (nth 1 entry
))
670 (nth 3 entry
) ;5 mtime
672 (nth 2 entry
) ;7 size
673 (nth 1 entry
) ;8 mode
675 inode
;10 inode number
676 device
)))))))) ;11 file system number
678 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format
)
679 "Implement `file-attributes' for Tramp files using stat command."
681 vec
5 "file attributes with stat: %s" (tramp-file-name-localname vec
))
682 (with-current-buffer (tramp-get-connection-buffer vec
)
683 (let* (size id link uid gid atime mtime ctime mode inode
)
684 (when (tramp-smb-send-command
685 vec
(format "stat \"%s\"" (tramp-smb-get-localname vec
)))
688 (goto-char (point-min))
689 (unless (re-search-forward tramp-smb-errors nil t
)
693 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
694 (setq size
(string-to-number (match-string 1))
695 id
(if (string-equal "directory" (match-string 2)) t
696 (if (string-equal "symbolic" (match-string 2)) ""))))
698 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
699 (setq inode
(string-to-number (match-string 1))
700 link
(string-to-number (match-string 2))))
702 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
703 (setq mode
(match-string 1)
704 uid
(if (equal id-format
'string
) (match-string 2)
705 (string-to-number (match-string 2)))
706 gid
(if (equal id-format
'string
) (match-string 3)
707 (string-to-number (match-string 3)))))
709 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
712 (string-to-number (match-string 6)) ;; sec
713 (string-to-number (match-string 5)) ;; min
714 (string-to-number (match-string 4)) ;; hour
715 (string-to-number (match-string 3)) ;; day
716 (string-to-number (match-string 2)) ;; month
717 (string-to-number (match-string 1))))) ;; year
719 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
722 (string-to-number (match-string 6)) ;; sec
723 (string-to-number (match-string 5)) ;; min
724 (string-to-number (match-string 4)) ;; hour
725 (string-to-number (match-string 3)) ;; day
726 (string-to-number (match-string 2)) ;; month
727 (string-to-number (match-string 1))))) ;; year
729 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
732 (string-to-number (match-string 6)) ;; sec
733 (string-to-number (match-string 5)) ;; min
734 (string-to-number (match-string 4)) ;; hour
735 (string-to-number (match-string 3)) ;; day
736 (string-to-number (match-string 2)) ;; month
737 (string-to-number (match-string 1)))))) ;; year
739 ;; Return the result.
740 (list id link uid gid atime mtime ctime size mode nil inode
741 (tramp-get-device vec
)))))))
743 (defun tramp-smb-handle-file-directory-p (filename)
744 "Like `file-directory-p' for Tramp files."
745 (and (file-exists-p filename
)
746 (eq ?d
(aref (nth 8 (file-attributes filename
)) 0))))
748 (defun tramp-smb-handle-file-local-copy (filename)
749 "Like `file-local-copy' for Tramp files."
750 (with-parsed-tramp-file-name filename nil
751 (unless (file-exists-p filename
)
754 "Cannot make local copy of non-existing file `%s'" filename
))
755 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
756 (tramp-with-progress-reporter
757 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
758 (unless (tramp-smb-send-command
759 v
(format "get \"%s\" \"%s\""
760 (tramp-smb-get-localname v
) tmpfile
))
761 ;; Oops, an error. We shall cleanup.
762 (delete-file tmpfile
)
764 v
'file-error
"Cannot make local copy of file `%s'" filename
)))
767 ;; This function should return "foo/" for directories and "bar" for
769 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
770 "Like `file-name-all-completions' for Tramp files."
773 (with-parsed-tramp-file-name directory nil
774 (with-file-property v localname
"file-name-all-completions"
776 (let ((entries (tramp-smb-get-file-entries directory
)))
780 (if (string-match "d" (nth 1 x
))
781 (file-name-as-directory (nth 0 x
))
785 (defun tramp-smb-handle-file-writable-p (filename)
786 "Like `file-writable-p' for Tramp files."
787 (if (file-exists-p filename
)
788 (string-match "w" (or (nth 8 (file-attributes filename
)) ""))
789 (let ((dir (file-name-directory filename
)))
790 (and (file-exists-p dir
)
791 (file-writable-p dir
)))))
793 (defun tramp-smb-handle-insert-directory
794 (filename switches
&optional wildcard full-directory-p
)
795 "Like `insert-directory' for Tramp files."
796 (setq filename
(expand-file-name filename
))
798 ;; Called from `dired-add-entry'.
799 (setq filename
(file-name-as-directory filename
))
800 (setq filename
(directory-file-name filename
)))
801 (with-parsed-tramp-file-name filename nil
803 (let ((base (file-name-nondirectory filename
))
804 ;; We should not destroy the cache entry.
805 (entries (copy-sequence
806 (tramp-smb-get-file-entries
807 (file-name-directory filename
)))))
810 (string-match "\\." base
)
811 (setq base
(replace-match "\\\\." nil nil base
))
812 (string-match "\\*" base
)
813 (setq base
(replace-match ".*" nil nil base
))
814 (string-match "\\?" base
)
815 (setq base
(replace-match ".?" nil nil base
)))
821 (if (or wildcard
(zerop (length base
)))
822 ;; Check for matching entries.
826 (format "^%s" base
) (nth 0 x
))
829 ;; We just need the only and only entry FILENAME.
830 (list (assoc base entries
)))))
837 (if (string-match "t" switches
)
839 (tramp-time-less-p (nth 3 y
) (nth 3 x
))
841 (string-lessp (nth 0 x
) (nth 0 y
))))))
843 ;; Handle "-F" switch.
844 (when (string-match "F" switches
)
847 (when (not (zerop (length (car x
))))
849 ((char-equal ?d
(string-to-char (nth 1 x
)))
850 (setcar x
(concat (car x
) "/")))
851 ((char-equal ?x
(string-to-char (nth 1 x
)))
852 (setcar x
(concat (car x
) "*"))))))
858 (when (not (zerop (length (nth 0 x
))))
860 (when (tramp-smb-get-stat-capability v
)
862 (file-attributes filename
'string
)))))
865 "%10s %3d %-8s %-8s %8s %s "
866 (or (nth 8 attr
) (nth 1 x
)) ; mode
867 (or (nth 1 attr
) 1) ; inode
868 (or (nth 2 attr
) "nobody") ; uid
869 (or (nth 3 attr
) "nogroup") ; gid
870 (or (nth 7 attr
) (nth 2 x
)) ; size
872 (if (tramp-time-less-p
873 (tramp-time-subtract (current-time) (nth 3 x
))
878 ;; We mark the file name. The inserted name could be
879 ;; from somewhere else, so we use the relative file
880 ;; name of `default-directory'.
881 (let ((start (point)))
887 (nth 0 x
) (file-name-directory filename
)))))
888 (put-text-property start
(1- (point)) 'dired-filename t
))
890 (beginning-of-line))))
893 (defun tramp-smb-handle-make-directory (dir &optional parents
)
894 "Like `make-directory' for Tramp files."
895 (setq dir
(directory-file-name (expand-file-name dir
)))
896 (unless (file-name-absolute-p dir
)
897 (setq dir
(expand-file-name dir default-directory
)))
898 (with-parsed-tramp-file-name dir nil
900 (let* ((ldir (file-name-directory dir
)))
901 ;; Make missing directory parts.
903 (tramp-smb-get-share v
)
904 (not (file-directory-p ldir
)))
905 (make-directory ldir parents
))
907 (when (file-directory-p ldir
)
908 (make-directory-internal dir
))
909 (unless (file-directory-p dir
)
910 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
912 (defun tramp-smb-handle-make-directory-internal (directory)
913 "Like `make-directory-internal' for Tramp files."
914 (setq directory
(directory-file-name (expand-file-name directory
)))
915 (unless (file-name-absolute-p directory
)
916 (setq directory
(expand-file-name directory default-directory
)))
917 (with-parsed-tramp-file-name directory nil
919 (let* ((file (tramp-smb-get-localname v
)))
920 (when (file-directory-p (file-name-directory directory
))
921 (tramp-smb-send-command
923 (if (tramp-smb-get-cifs-capabilities v
)
925 "posix_mkdir \"%s\" %s"
926 file
(tramp-compat-decimal-to-octal (default-file-modes)))
927 (format "mkdir \"%s\"" file
)))
928 ;; We must also flush the cache of the directory, because
929 ;; `file-attributes' reads the values from there.
930 (tramp-flush-file-property v
(file-name-directory localname
))
931 (tramp-flush-file-property v localname
))
932 (unless (file-directory-p directory
)
934 v
'file-error
"Couldn't make directory %s" directory
))))))
936 (defun tramp-smb-handle-make-symbolic-link
937 (filename linkname
&optional ok-if-already-exists
)
938 "Like `make-symbolic-link' for Tramp files.
939 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
940 the symlink. If LINKNAME is a Tramp file, only the localname component is
941 used as the target of the symlink.
943 If LINKNAME is a Tramp file and the localname component is relative, then
944 it is expanded first, before the localname component is taken. Note that
945 this can give surprising results if the user/host for the source and
946 target of the symlink differ."
947 (unless (tramp-equal-remote filename linkname
)
948 (with-parsed-tramp-file-name
949 (if (tramp-tramp-file-p filename
) filename linkname
) nil
952 "make-symbolic-link: %s"
953 "only implemented for same method, same user, same host")))
954 (with-parsed-tramp-file-name filename v1
955 (with-parsed-tramp-file-name linkname v2
956 (when (file-directory-p filename
)
959 "make-symbolic-link: %s must not be a directory" filename
))
960 (when (and (not ok-if-already-exists
)
961 (file-exists-p linkname
)
962 (not (numberp ok-if-already-exists
))
965 "File %s already exists; make it a new name anyway? "
969 "make-symbolic-link: file %s already exists" linkname
))
970 (unless (tramp-smb-get-cifs-capabilities v1
)
971 (tramp-error v2
'file-error
"make-symbolic-link not supported"))
972 ;; We must also flush the cache of the directory, because
973 ;; `file-attributes' reads the values from there.
974 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
975 (tramp-flush-file-property v2 v2-localname
)
977 (tramp-smb-send-command
980 "symlink \"%s\" \"%s\""
981 (tramp-smb-get-localname v1
)
982 (tramp-smb-get-localname v2
)))
985 "error with make-symbolic-link, see buffer `%s' for details"
988 (defun tramp-smb-handle-process-file
989 (program &optional infile destination display
&rest args
)
990 "Like `process-file' for Tramp files."
991 ;; The implementation is not complete yet.
992 (when (and (numberp destination
) (zerop destination
))
993 (error "Implementation does not handle immediate return"))
995 (with-parsed-tramp-file-name default-directory nil
996 (let* ((name (file-name-nondirectory program
))
999 input tmpinput outbuf command ret
)
1003 (setq infile
(expand-file-name infile
))
1004 (if (tramp-equal-remote default-directory infile
)
1005 ;; INFILE is on the same remote host.
1006 (setq input
(with-parsed-tramp-file-name infile nil localname
))
1007 ;; INFILE must be copied to remote host.
1008 (setq input
(tramp-make-tramp-temp-file v
)
1009 tmpinput
(tramp-make-tramp-file-name method user host input
))
1010 (copy-file infile tmpinput t
))
1011 ;; Transform input into a filename powershell does understand.
1012 (setq input
(format "//%s%s" host input
)))
1014 ;; Determine output.
1017 ((bufferp destination
)
1018 (setq outbuf destination
))
1020 ((stringp destination
)
1021 (setq outbuf
(get-buffer-create destination
)))
1022 ;; (REAL-DESTINATION ERROR-DESTINATION)
1023 ((consp destination
)
1026 ((bufferp (car destination
))
1027 (setq outbuf
(car destination
)))
1028 ((stringp (car destination
))
1029 (setq outbuf
(get-buffer-create (car destination
))))
1031 (setq outbuf
(current-buffer))))
1033 (tramp-message v
2 "%s" "STDERR not supported"))
1036 (setq outbuf
(current-buffer))))
1038 ;; Construct command.
1039 (setq command
(mapconcat 'identity
(cons program args
) " ")
1042 "get-content %s | & %s"
1043 (tramp-smb-shell-quote-argument input
) command
)
1044 (format "& %s" command
)))
1046 (while (get-process name1
)
1047 ;; NAME must be unique as process name.
1049 name1
(format "%s<%d>" name i
)))
1051 ;; Set the new process properties.
1052 (tramp-set-connection-property v
"process-name" name1
)
1053 (tramp-set-connection-property
1055 (or outbuf
(generate-new-buffer tramp-temp-buffer-name
)))
1059 (with-current-buffer (tramp-get-connection-buffer v
)
1060 ;; Preserve buffer contents.
1061 (narrow-to-region (point-max) (point-max))
1062 (tramp-smb-call-winexe v
)
1063 (when (tramp-smb-get-share v
)
1064 (tramp-smb-send-command
1065 v
(format "cd \"//%s%s\"" host
(file-name-directory localname
))))
1066 (tramp-smb-send-command v command
)
1067 ;; Preserve command output.
1068 (narrow-to-region (point-max) (point-max))
1069 (let ((p (tramp-get-connection-process v
)))
1070 (tramp-smb-send-command v
"exit $lasterrorcode")
1071 (while (memq (process-status p
) '(run open
))
1073 (setq ret
(process-exit-status p
))))
1074 (delete-region (point-min) (point-max))
1077 ;; When the user did interrupt, we should do it also. We use
1078 ;; return code -1 as marker.
1085 ;; We should show the output anyway.
1086 (when (and outbuf display
) (display-buffer outbuf
))
1088 ;; Cleanup. We remove all file cache values for the connection,
1089 ;; because the remote process could have changed them.
1090 (tramp-set-connection-property v
"process-name" nil
)
1091 (tramp-set-connection-property v
"process-buffer" nil
)
1092 (when tmpinput
(delete-file tmpinput
))
1094 (kill-buffer (tramp-get-connection-property v
"process-buffer" nil
)))
1096 ;; `process-file-side-effects' has been introduced with GNU
1097 ;; Emacs 23.2. If set to `nil', no remote file will be changed
1098 ;; by `program'. If it doesn't exist, we assume its default
1100 (unless (and (boundp 'process-file-side-effects
)
1101 (not (symbol-value 'process-file-side-effects
)))
1102 (tramp-flush-directory-property v
""))
1104 ;; Return exit status.
1109 (defun tramp-smb-handle-rename-file
1110 (filename newname
&optional ok-if-already-exists
)
1111 "Like `rename-file' for Tramp files."
1112 (setq filename
(expand-file-name filename
)
1113 newname
(expand-file-name newname
))
1115 (when (and (not ok-if-already-exists
)
1116 (file-exists-p newname
))
1118 (tramp-dissect-file-name
1119 (if (file-remote-p filename
) filename newname
))
1120 'file-already-exists newname
))
1122 (tramp-with-progress-reporter
1123 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
1124 0 (format "Renaming %s to %s" filename newname
)
1126 (if (and (tramp-equal-remote filename newname
)
1128 (tramp-smb-get-share (tramp-dissect-file-name filename
))
1129 (tramp-smb-get-share (tramp-dissect-file-name newname
))))
1130 ;; We can rename directly.
1131 (with-parsed-tramp-file-name filename v1
1132 (with-parsed-tramp-file-name newname v2
1134 ;; We must also flush the cache of the directory, because
1135 ;; `file-attributes' reads the values from there.
1136 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1137 (tramp-flush-file-property v2 v2-localname
)
1138 (unless (tramp-smb-get-share v2
)
1140 v2
'file-error
"Target `%s' must contain a share name" newname
))
1141 (unless (tramp-smb-send-command
1142 v2
(format "rename \"%s\" \"%s\""
1143 (tramp-smb-get-localname v1
)
1144 (tramp-smb-get-localname v2
)))
1145 (tramp-error v2
'file-error
"Cannot rename `%s'" filename
))))
1147 ;; We must rename via copy.
1148 (tramp-compat-copy-file filename newname ok-if-already-exists t t t
)
1149 (if (file-directory-p filename
)
1150 (tramp-compat-delete-directory filename
'recursive
)
1151 (delete-file filename
)))))
1153 (defun tramp-smb-handle-set-file-modes (filename mode
)
1154 "Like `set-file-modes' for Tramp files."
1155 (with-parsed-tramp-file-name filename nil
1156 (when (tramp-smb-get-cifs-capabilities v
)
1157 (tramp-flush-file-property v localname
)
1158 (unless (tramp-smb-send-command
1159 v
(format "chmod \"%s\" %s"
1160 (tramp-smb-get-localname v
)
1161 (tramp-compat-decimal-to-octal mode
)))
1163 v
'file-error
"Error while changing file's mode %s" filename
)))))
1165 ;; We use BUFFER also as connection buffer during setup. Because of
1166 ;; this, its original contents must be saved, and restored once
1167 ;; connection has been setup.
1168 (defun tramp-smb-handle-start-file-process (name buffer program
&rest args
)
1169 "Like `start-file-process' for Tramp files."
1170 (with-parsed-tramp-file-name default-directory nil
1171 (let ((command (mapconcat 'identity
(cons program args
) " "))
1172 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
1179 ;; BUFFER can be nil. We use a temporary buffer.
1180 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
1181 (while (get-process name1
)
1182 ;; NAME must be unique as process name.
1184 name1
(format "%s<%d>" name i
)))
1185 ;; Set the new process properties.
1186 (tramp-set-connection-property v
"process-name" name1
)
1187 (tramp-set-connection-property v
"process-buffer" buffer
)
1188 ;; Activate narrowing in order to save BUFFER contents.
1189 (with-current-buffer (tramp-get-connection-buffer v
)
1190 (let ((buffer-undo-list t
))
1191 (narrow-to-region (point-max) (point-max))
1192 (tramp-smb-call-winexe v
)
1193 (when (tramp-smb-get-share v
)
1194 (tramp-smb-send-command
1197 host
(file-name-directory localname
))))
1198 (tramp-message v
6 "(%s); exit" command
)
1199 (tramp-send-string v command
)))
1201 (tramp-get-connection-process v
)))
1204 (with-current-buffer (tramp-get-connection-buffer v
)
1205 (if (string-match tramp-temp-buffer-name
(buffer-name))
1207 (set-process-buffer (tramp-get-connection-process v
) nil
)
1208 (kill-buffer (current-buffer)))
1209 (set-buffer-modified-p bmp
)))
1210 (tramp-set-connection-property v
"process-name" nil
)
1211 (tramp-set-connection-property v
"process-buffer" nil
)))))
1213 (defun tramp-smb-handle-substitute-in-file-name (filename)
1214 "Like `handle-substitute-in-file-name' for Tramp files.
1215 \"//\" substitutes only in the local filename part. Catches
1216 errors for shares like \"C$/\", which are common in Microsoft Windows."
1217 (with-parsed-tramp-file-name filename nil
1218 ;; Ignore in LOCALNAME everything before "//".
1219 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
1221 (concat (file-remote-p filename
)
1222 (replace-match "\\1" nil nil localname
)))))
1224 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
1227 (defun tramp-smb-handle-write-region
1228 (start end filename
&optional append visit lockname confirm
)
1229 "Like `write-region' for Tramp files."
1230 (setq filename
(expand-file-name filename
))
1231 (with-parsed-tramp-file-name filename nil
1232 (unless (eq append nil
)
1234 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
1235 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
1236 (when (and (not (featurep 'xemacs
))
1237 confirm
(file-exists-p filename
))
1238 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
1240 (tramp-error v
'file-error
"File not overwritten")))
1241 ;; We must also flush the cache of the directory, because
1242 ;; `file-attributes' reads the values from there.
1243 (tramp-flush-file-property v
(file-name-directory localname
))
1244 (tramp-flush-file-property v localname
)
1245 (let ((curbuf (current-buffer))
1246 (tmpfile (tramp-compat-make-temp-file filename
)))
1247 ;; We say `no-message' here because we don't want the visited file
1248 ;; modtime data to be clobbered from the temp file. We call
1249 ;; `set-visited-file-modtime' ourselves later on.
1250 (tramp-run-real-handler
1252 (if confirm
; don't pass this arg unless defined for backward compat.
1253 (list start end tmpfile append
'no-message lockname confirm
)
1254 (list start end tmpfile append
'no-message lockname
)))
1256 (tramp-with-progress-reporter
1257 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
1259 (unless (tramp-smb-send-command
1260 v
(format "put %s \"%s\""
1261 tmpfile
(tramp-smb-get-localname v
)))
1262 (tramp-error v
'file-error
"Cannot write `%s'" filename
))
1263 (delete-file tmpfile
)))
1265 (unless (equal curbuf
(current-buffer))
1268 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer)))
1270 (set-visited-file-modtime)))))
1273 ;; Internal file name functions.
1275 (defun tramp-smb-get-share (vec)
1276 "Returns the share name of LOCALNAME."
1278 (let ((localname (tramp-file-name-localname vec
)))
1279 (when (string-match "^/?\\([^/]+\\)/" localname
)
1280 (match-string 1 localname
)))))
1282 (defun tramp-smb-get-localname (vec)
1283 "Returns the file name of LOCALNAME.
1284 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
1286 (let ((localname (tramp-file-name-localname vec
)))
1289 (if (string-match "^/?[^/]+\\(/.*\\)" localname
)
1290 ;; There is a share, separated by "/".
1291 (if (not (tramp-smb-get-cifs-capabilities vec
))
1293 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
1294 (match-string 1 localname
) "")
1295 (match-string 1 localname
))
1296 ;; There is just a share.
1297 (if (string-match "^/?\\([^/]+\\)$" localname
)
1298 (match-string 1 localname
)
1301 ;; Sometimes we have discarded `substitute-in-file-name'.
1302 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname
)
1303 (setq localname
(replace-match "$" nil nil localname
1)))
1307 ;; Share names of a host are cached. It is very unlikely that the
1308 ;; shares do change during connection.
1309 (defun tramp-smb-get-file-entries (directory)
1310 "Read entries which match DIRECTORY.
1311 Either the shares are listed, or the `dir' command is executed.
1312 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
1313 (with-parsed-tramp-file-name (file-name-as-directory directory
) nil
1314 (setq localname
(or localname
"/"))
1315 (with-file-property v localname
"file-entries"
1316 (with-current-buffer (tramp-get-connection-buffer v
)
1317 (let* ((share (tramp-smb-get-share v
))
1318 (cache (tramp-get-connection-property v
"share-cache" nil
))
1321 (if (and (not share
) cache
)
1322 ;; Return cached shares.
1327 (tramp-smb-send-command
1328 v
(format "dir \"%s*\"" (tramp-smb-get-localname v
)))
1329 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1330 (tramp-smb-maybe-open-connection v
))
1332 ;; Loop the listing.
1333 (goto-char (point-min))
1334 (if (re-search-forward tramp-smb-errors nil t
)
1335 (tramp-error v
'file-error
"%s `%s'" (match-string 0) directory
)
1337 (setq entry
(tramp-smb-read-file-entry share
))
1339 (when entry
(add-to-list 'res entry
))))
1341 ;; Cache share entries.
1343 (tramp-set-connection-property v
"share-cache" res
)))
1345 ;; Add directory itself.
1346 (add-to-list 'res
'("" "drwxrwxrwx" 0 (0 0)))
1348 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1349 ;; If there's no short delay, it returns nil. No idea about.
1350 (when (featurep 'xemacs
) (sleep-for 0.01))
1355 ;; Return either a share name (if SHARE is nil), or a file name.
1357 ;; If shares are listed, the following format is expected:
1359 ;; Disk| - leading spaces
1360 ;; [^|]+| - share name, 14 char
1363 ;; Entries provided by smbclient DIR aren't fully regular.
1364 ;; They should have the format
1366 ;; \s-\{2,2} - leading spaces
1367 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1368 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1369 ;; \s- - space delimiter
1370 ;; \s-+[0-9]+ - size, 8 chars, right bound
1371 ;; \s-\{2,2\} - space delimiter
1372 ;; \w\{3,3\} - weekday
1373 ;; \s- - space delimiter
1374 ;; \w\{3,3\} - month
1375 ;; \s- - space delimiter
1377 ;; \s- - space delimiter
1378 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1379 ;; \s- - space delimiter
1380 ;; [0-9]\{4,4\} - year
1382 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1383 ;; has function display_finfo:
1385 ;; d_printf(" %-30s%7.7s %8.0f %s",
1387 ;; attrib_string(finfo->mode),
1388 ;; (double)finfo->size,
1389 ;; asctime(LocalTime(&t)));
1391 ;; in Samba 1.9, there's the following code:
1393 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1394 ;; CNV_LANG(finfo->name),
1395 ;; attrib_string(finfo->mode),
1397 ;; asctime(LocalTime(&t))));
1400 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1401 ;; available in older Emacsen.
1402 ;; * The length of constructs (file name, size) might exceed the default.
1403 ;; * File names might contain spaces.
1404 ;; * Permissions might be empty.
1406 ;; So we try to analyze backwards.
1407 (defun tramp-smb-read-file-entry (share)
1408 "Parse entry in SMB output buffer.
1409 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1410 Result is the list (LOCALNAME MODE SIZE MTIME)."
1411 ;; We are called from `tramp-smb-get-file-entries', which sets the
1413 (let ((line (buffer-substring (point) (point-at-eol)))
1414 localname mode size month day hour min sec year mtime
)
1418 ;; Read share entries.
1419 (when (string-match "^Disk|\\([^|]+\\)|" line
)
1420 (setq localname
(match-string 1 line
)
1428 (if (string-match "\\([0-9]+\\)$" line
)
1429 (setq year
(string-to-number (match-string 1 line
))
1430 line
(substring line
0 -
5))
1434 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
1435 (setq hour
(string-to-number (match-string 1 line
))
1436 min
(string-to-number (match-string 2 line
))
1437 sec
(string-to-number (match-string 3 line
))
1438 line
(substring line
0 -
9))
1442 (if (string-match "\\([0-9]+\\)$" line
)
1443 (setq day
(string-to-number (match-string 1 line
))
1444 line
(substring line
0 -
3))
1448 (if (string-match "\\(\\w+\\)$" line
)
1449 (setq month
(match-string 1 line
)
1450 line
(substring line
0 -
4))
1454 (if (string-match "\\(\\w+\\)$" line
)
1455 (setq line
(substring line
0 -
5))
1459 (if (string-match "\\([0-9]+\\)$" line
)
1460 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
1461 (setq size
(string-to-number (match-string 1 line
)))
1462 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
1463 (setq length
(+ length
(match-end 0))))
1464 (setq line
(substring line
0 length
)))
1467 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1468 (if (string-match "\\([ADHRSV]+\\)?$" line
)
1470 mode
(or (match-string 1 line
) "")
1471 mode
(save-match-data (format
1473 (if (string-match "D" mode
) "d" "-")
1476 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
1477 line
(substring line
0 -
6))
1481 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
1482 (setq localname
(match-string 1 line
))
1485 (when (and localname mode size
)
1487 (if (and sec min hour day month year
)
1490 (cdr (assoc (downcase month
) tramp-parse-time-months
))
1493 (list localname mode size mtime
))))
1495 (defun tramp-smb-get-cifs-capabilities (vec)
1496 "Check, whether the SMB server supports POSIX commands."
1497 ;; When we are not logged in yet, we return nil.
1498 (if (let ((p (tramp-get-connection-process vec
)))
1499 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1500 (with-connection-property
1501 (tramp-get-connection-process vec
) "cifs-capabilities"
1503 (when (tramp-smb-send-command vec
"posix")
1504 (with-current-buffer (tramp-get-connection-buffer vec
)
1505 (goto-char (point-min))
1507 (re-search-forward "Server supports CIFS capabilities" nil t
)
1511 (buffer-substring (point) (point-at-eol)) nil t
)))))))))
1513 (defun tramp-smb-get-stat-capability (vec)
1514 "Check, whether the SMB server supports the STAT command."
1515 ;; When we are not logged in yet, we return nil.
1516 (if (let ((p (tramp-get-connection-process vec
)))
1517 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1518 (with-connection-property
1519 (tramp-get-connection-process vec
) "stat-capability"
1520 (tramp-smb-send-command vec
"stat ."))))
1523 ;; Connection functions.
1525 (defun tramp-smb-send-command (vec command
)
1526 "Send the COMMAND to connection VEC.
1527 Returns nil if there has been an error message from smbclient."
1528 (tramp-smb-maybe-open-connection vec
)
1529 (tramp-message vec
6 "%s" command
)
1530 (tramp-send-string vec command
)
1531 (tramp-smb-wait-for-output vec
))
1533 (defun tramp-smb-maybe-open-connection (vec &optional argument
)
1534 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1535 Does not do anything if a connection is already open, but re-opens the
1536 connection if a previous connection has died for some reason.
1537 If ARGUMENT is non-nil, use it as argument for
1538 `tramp-smb-winexe-program', and suppress any checks."
1539 (let* ((share (tramp-smb-get-share vec
))
1540 (buf (tramp-get-connection-buffer vec
))
1541 (p (get-buffer-process buf
)))
1543 ;; Check whether we still have the same smbclient version.
1544 ;; Otherwise, we must delete the connection cache, because
1545 ;; capabilities migh have changed.
1546 (unless (or argument
(processp p
))
1547 (let ((default-directory (tramp-compat-temporary-file-directory))
1548 (command (concat tramp-smb-program
" -V")))
1550 (unless tramp-smb-version
1551 (unless (executable-find tramp-smb-program
)
1554 "Cannot find command %s in %s" tramp-smb-program exec-path
))
1555 (setq tramp-smb-version
(shell-command-to-string command
))
1556 (tramp-message vec
6 command
)
1557 (tramp-message vec
6 "\n%s" tramp-smb-version
)
1558 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version
)
1559 (setq tramp-smb-version
1560 (replace-match "" nil nil tramp-smb-version
))))
1562 (unless (string-equal
1564 (tramp-get-connection-property
1565 vec
"smbclient-version" tramp-smb-version
))
1566 (tramp-flush-directory-property vec
"")
1567 (tramp-flush-connection-property vec
))
1569 (tramp-set-connection-property
1570 vec
"smbclient-version" tramp-smb-version
)))
1572 ;; If too much time has passed since last command was sent, look
1573 ;; whether there has been an error message; maybe due to
1574 ;; connection timeout.
1575 (with-current-buffer buf
1576 (goto-char (point-min))
1577 (when (and (> (tramp-time-diff
1579 (tramp-get-connection-property
1580 p
"last-cmd-time" '(0 0 0)))
1582 p
(processp p
) (memq (process-status p
) '(run open
))
1583 (re-search-forward tramp-smb-errors nil t
))
1587 ;; Check whether it is still the same share.
1589 (and p
(processp p
) (memq (process-status p
) '(run open
))
1593 (tramp-get-connection-property p
"smb-share" ""))))
1596 ;; There might be unread output from checking for share names.
1597 (when buf
(with-current-buffer buf
(erase-buffer)))
1598 (when (and p
(processp p
)) (delete-process p
))
1600 (let* ((user (tramp-file-name-user vec
))
1601 (host (tramp-file-name-host vec
))
1602 (real-user (tramp-file-name-real-user vec
))
1603 (real-host (tramp-file-name-real-host vec
))
1604 (domain (tramp-file-name-domain vec
))
1605 (port (tramp-file-name-port vec
))
1610 (setq args
(list (concat "//" real-host
))))
1612 (setq args
(list (concat "//" real-host
"/" share
))))
1614 (setq args
(list "-g" "-L" real-host
))))
1616 (if (not (zerop (length real-user
)))
1617 (setq args
(append args
(list "-U" real-user
)))
1618 (setq args
(append args
(list "-N"))))
1620 (when domain
(setq args
(append args
(list "-W" domain
))))
1621 (when port
(setq args
(append args
(list "-p" port
))))
1622 (when tramp-smb-conf
1623 (setq args
(append args
(list "-s" tramp-smb-conf
))))
1625 (setq args
(append args
(list argument
))))
1628 (tramp-with-progress-reporter
1630 (format "Opening connection for //%s%s/%s"
1631 (if (not (zerop (length user
))) (concat user
"@") "")
1634 (let* ((coding-system-for-read nil
)
1635 (process-connection-type tramp-process-connection-type
)
1636 (p (let ((default-directory
1637 (tramp-compat-temporary-file-directory)))
1638 (apply #'start-process
1639 (tramp-get-connection-name vec
)
1640 (tramp-get-connection-buffer vec
)
1642 tramp-smb-winexe-program tramp-smb-program
)
1646 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1647 (tramp-compat-set-process-query-on-exit-flag p nil
)
1649 ;; Set variables for computing the prompt for reading password.
1650 (setq tramp-current-method tramp-smb-method
1651 tramp-current-user user
1652 tramp-current-host host
)
1655 (let (tramp-message-show-message)
1656 ;; Play login scenario.
1657 (tramp-process-actions
1659 (if (or argument share
)
1660 tramp-smb-actions-with-share
1661 tramp-smb-actions-without-share
))
1663 ;; Check server version.
1665 (with-current-buffer (tramp-get-connection-buffer vec
)
1666 (goto-char (point-min))
1667 (search-forward-regexp tramp-smb-server-version nil t
)
1668 (let ((smbserver-version (match-string 0)))
1672 (tramp-get-connection-property
1673 vec
"smbserver-version" smbserver-version
))
1674 (tramp-flush-directory-property vec
"")
1675 (tramp-flush-connection-property vec
))
1676 (tramp-set-connection-property
1677 vec
"smbserver-version" smbserver-version
))))
1679 ;; Set chunksize. Otherwise, `tramp-send-string' might
1681 (tramp-set-connection-property p
"smb-share" share
)
1682 (tramp-set-connection-property
1683 p
"chunksize" tramp-chunksize
))
1685 ;; Check for the error reason. If it was due to wrong
1686 ;; password, reestablish the connection. We cannot
1687 ;; handle this in `tramp-process-actions', because
1688 ;; smbclient does not ask for the password, again.
1690 (with-current-buffer (tramp-get-connection-buffer vec
)
1691 (goto-char (point-min))
1692 (if (search-forward-regexp
1693 tramp-smb-wrong-passwd-regexp nil t
)
1694 ;; Disable `auth-source' and `password-cache'.
1697 (tramp-smb-maybe-open-connection vec argument
))
1698 ;; Propagate the error.
1699 (signal (car err
) (cdr err
)))))))))))))
1701 ;; We don't use timeouts. If needed, the caller shall wrap around.
1702 (defun tramp-smb-wait-for-output (vec)
1703 "Wait for output from smbclient command.
1704 Returns nil if an error message has appeared."
1705 (with-current-buffer (tramp-get-connection-buffer vec
)
1706 (let ((p (get-buffer-process (current-buffer)))
1707 (found (progn (goto-char (point-min))
1708 (re-search-forward tramp-smb-prompt nil t
)))
1709 (err (progn (goto-char (point-min))
1710 (re-search-forward tramp-smb-errors nil t
)))
1713 ;; Algorithm: get waiting output. See if last line contains
1714 ;; `tramp-smb-prompt' sentinel or `tramp-smb-errors' strings.
1715 ;; If not, wait a bit and again get waiting output.
1716 (while (and (not found
) (not err
) (memq (process-status p
) '(run open
)))
1718 ;; Accept pending output.
1719 (tramp-accept-process-output p
)
1721 ;; Search for prompt.
1722 (goto-char (point-min))
1723 (setq found
(re-search-forward tramp-smb-prompt nil t
))
1725 ;; Search for errors.
1726 (goto-char (point-min))
1727 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1729 ;; When the process is still alive, read pending output.
1730 (while (and (not found
) (memq (process-status p
) '(run open
)))
1732 ;; Accept pending output.
1733 (tramp-accept-process-output p
)
1735 ;; Search for prompt.
1736 (goto-char (point-min))
1737 (setq found
(re-search-forward tramp-smb-prompt nil t
)))
1739 (tramp-message vec
6 "\n%s" (buffer-string))
1743 (goto-char (point-max))
1744 (re-search-backward tramp-smb-prompt nil t
)
1745 (delete-region (point) (point-max)))
1747 ;; Return value is whether no error message has appeared.
1750 (defun tramp-smb-kill-winexe-function ()
1751 "Send SIGKILL to the winexe process."
1753 (let ((p (get-buffer-process (current-buffer))))
1754 (when (and p
(processp p
) (memq (process-status p
) '(run open
)))
1755 (signal-process (process-id p
) 'SIGINT
)))))
1757 (defun tramp-smb-call-winexe (vec)
1758 "Apply a remote command, if possible, using `tramp-smb-winexe-program'."
1760 ;; We call `tramp-get-buffer' in order to get a debug buffer for
1762 (tramp-get-buffer vec
)
1764 ;; Check for program.
1765 (unless (let ((default-directory
1766 (tramp-compat-temporary-file-directory)))
1767 (executable-find tramp-smb-winexe-program
))
1769 vec
'file-error
"Cannot find program: %s" tramp-smb-winexe-program
))
1771 ;; winexe does not supports ports.
1772 (when (tramp-file-name-port vec
)
1773 (tramp-error vec
'file-error
"Port not supported for remote processes"))
1775 (tramp-smb-maybe-open-connection
1779 tramp-smb-winexe-shell-command tramp-smb-winexe-shell-command-switch
))
1781 (set (make-local-variable 'kill-buffer-hook
)
1782 '(tramp-smb-kill-winexe-function))
1784 ;; Suppress "^M". Shouldn't we specify utf8?
1785 (set-process-coding-system (tramp-get-connection-process vec
) 'raw-text-dos
)
1787 ;; Set width to 128. This avoids mixing prompt and long error messages.
1788 (tramp-smb-send-command vec
"$rawui = (Get-Host).UI.RawUI")
1789 (tramp-smb-send-command vec
"$bufsize = $rawui.BufferSize")
1790 (tramp-smb-send-command vec
"$winsize = $rawui.WindowSize")
1791 (tramp-smb-send-command vec
"$bufsize.Width = 128")
1792 (tramp-smb-send-command vec
"$winsize.Width = 128")
1793 (tramp-smb-send-command vec
"$rawui.BufferSize = $bufsize")
1794 (tramp-smb-send-command vec
"$rawui.WindowSize = $winsize"))
1796 (defun tramp-smb-shell-quote-argument (s)
1797 "Similar to `shell-quote-argument', but uses windows cmd syntax."
1798 (let ((system-type 'ms-dos
))
1799 (shell-quote-argument s
)))
1801 (add-hook 'tramp-unload-hook
1803 (unload-feature 'tramp-smb
'force
)))
1805 (provide 'tramp-smb
)
1809 ;; * Return more comprehensive file permission string.
1810 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1811 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1812 ;; * Ignore case in file names.
1814 ;;; tramp-smb.el ends here