1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
3 ;; Copyright (C) 2002-2011 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
(cons tramp-smb-method nil
)))
43 ;; Add a default for `tramp-default-method-alist'. Rule: If there is
44 ;; a domain in USER, it must be the SMB method.
46 (add-to-list 'tramp-default-method-alist
47 `(nil ,tramp-prefix-domain-regexp
,tramp-smb-method
))
49 ;; Add a default for `tramp-default-user-alist'. Rule: For the SMB method,
50 ;; the anonymous user is chosen.
52 (add-to-list 'tramp-default-user-alist
53 `(,(concat "\\`" tramp-smb-method
"\\'") nil nil
))
55 ;; Add completion function for SMB method.
57 (eval-after-load 'tramp
58 '(tramp-set-completion-function
60 '((tramp-parse-netrc "~/.netrc"))))
62 (defcustom tramp-smb-program
"smbclient"
63 "*Name of SMB client to run."
67 (defcustom tramp-smb-conf
"/dev/null"
68 "*Path of the smb.conf file.
69 If it is nil, no smb.conf will be added to the `tramp-smb-program'
70 call, letting the SMB client use the default one."
72 :type
'(choice (const nil
) (file :must-match t
)))
74 (defvar tramp-smb-version nil
75 "*Version string of the SMB client.")
77 (defconst tramp-smb-prompt
"^smb: .+> \\|^\\s-+Server\\s-+Comment$"
78 "Regexp used as prompt in smbclient.")
80 (defconst tramp-smb-errors
83 `(;; Connection error / timeout / unknown command.
84 "Connection\\( to \\S-+\\)? failed"
85 "Read from server failed, maybe it closed the connection"
86 "Call timed out: server did not respond"
87 "\\S-+: command not found"
88 "Server doesn't support UNIX CIFS calls"
100 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
101 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003).
102 "NT_STATUS_ACCESS_DENIED"
103 "NT_STATUS_ACCOUNT_LOCKED_OUT"
104 "NT_STATUS_BAD_NETWORK_NAME"
105 "NT_STATUS_CANNOT_DELETE"
106 "NT_STATUS_CONNECTION_REFUSED"
107 "NT_STATUS_DIRECTORY_NOT_EMPTY"
108 "NT_STATUS_DUPLICATE_NAME"
109 "NT_STATUS_FILE_IS_A_DIRECTORY"
110 "NT_STATUS_IO_TIMEOUT"
111 "NT_STATUS_LOGON_FAILURE"
112 "NT_STATUS_NETWORK_ACCESS_DENIED"
113 "NT_STATUS_NOT_IMPLEMENTED"
114 "NT_STATUS_NO_SUCH_FILE"
115 "NT_STATUS_NO_SUCH_USER"
116 "NT_STATUS_OBJECT_NAME_COLLISION"
117 "NT_STATUS_OBJECT_NAME_INVALID"
118 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
119 "NT_STATUS_SHARING_VIOLATION"
120 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
121 "NT_STATUS_UNSUCCESSFUL"
122 "NT_STATUS_WRONG_PASSWORD")))
124 "Regexp for possible error strings of SMB servers.
125 Used instead of analyzing error codes of commands.")
127 (defconst tramp-smb-actions-with-share
128 '((tramp-smb-prompt tramp-action-succeed
)
129 (tramp-password-prompt-regexp tramp-action-password
)
130 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
131 (tramp-smb-errors tramp-action-permission-denied
)
132 (tramp-process-alive-regexp tramp-action-process-alive
))
133 "List of pattern/action pairs.
134 This list is used for login to SMB servers.
136 See `tramp-actions-before-shell' for more info.")
138 (defconst tramp-smb-actions-without-share
139 '((tramp-password-prompt-regexp tramp-action-password
)
140 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
141 (tramp-smb-errors tramp-action-permission-denied
)
142 (tramp-process-alive-regexp tramp-action-out-of-band
))
143 "List of pattern/action pairs.
144 This list is used for login to SMB servers.
146 See `tramp-actions-before-shell' for more info.")
148 ;; New handlers should be added here.
149 (defconst tramp-smb-file-name-handler-alist
151 ;; `access-file' performed by default handler.
152 (add-name-to-file . tramp-smb-handle-add-name-to-file
)
153 ;; `byte-compiler-base-file-name' performed by default handler.
154 (copy-directory . tramp-smb-handle-copy-directory
)
155 (copy-file . tramp-smb-handle-copy-file
)
156 (delete-directory . tramp-smb-handle-delete-directory
)
157 (delete-file . tramp-smb-handle-delete-file
)
158 ;; `diff-latest-backup-file' performed by default handler.
159 (directory-file-name . tramp-handle-directory-file-name
)
160 (directory-files . tramp-smb-handle-directory-files
)
161 (directory-files-and-attributes
162 . tramp-handle-directory-files-and-attributes
)
163 (dired-call-process . ignore
)
164 (dired-compress-file . ignore
)
165 (dired-uncache . tramp-handle-dired-uncache
)
166 (expand-file-name . tramp-smb-handle-expand-file-name
)
167 (file-accessible-directory-p . tramp-smb-handle-file-directory-p
)
168 (file-attributes . tramp-smb-handle-file-attributes
)
169 (file-directory-p . tramp-smb-handle-file-directory-p
)
170 (file-executable-p . tramp-handle-file-exists-p
)
171 (file-exists-p . tramp-handle-file-exists-p
)
172 (file-local-copy . tramp-smb-handle-file-local-copy
)
173 (file-modes . tramp-handle-file-modes
)
174 (file-name-all-completions . tramp-smb-handle-file-name-all-completions
)
175 (file-name-as-directory . tramp-handle-file-name-as-directory
)
176 (file-name-completion . tramp-handle-file-name-completion
)
177 (file-name-directory . tramp-handle-file-name-directory
)
178 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
179 ;; `file-name-sans-versions' performed by default handler.
180 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
181 (file-ownership-preserved-p . ignore
)
182 (file-readable-p . tramp-handle-file-exists-p
)
183 (file-regular-p . tramp-handle-file-regular-p
)
184 (file-remote-p . tramp-handle-file-remote-p
)
185 ;; `file-selinux-context' performed by default handler.
186 (file-symlink-p . tramp-handle-file-symlink-p
)
187 ;; `file-truename' performed by default handler.
188 (file-writable-p . tramp-smb-handle-file-writable-p
)
189 (find-backup-file-name . tramp-handle-find-backup-file-name
)
190 ;; `find-file-noselect' performed by default handler.
191 ;; `get-file-buffer' performed by default handler.
192 (insert-directory . tramp-smb-handle-insert-directory
)
193 (insert-file-contents . tramp-handle-insert-file-contents
)
194 (load . tramp-handle-load
)
195 (make-directory . tramp-smb-handle-make-directory
)
196 (make-directory-internal . tramp-smb-handle-make-directory-internal
)
197 (make-symbolic-link . tramp-smb-handle-make-symbolic-link
)
198 (rename-file . tramp-smb-handle-rename-file
)
199 (set-file-modes . tramp-smb-handle-set-file-modes
)
200 ;; `set-file-selinux-context' performed by default handler.
201 (set-file-times . ignore
)
202 (set-visited-file-modtime . ignore
)
203 (shell-command . ignore
)
204 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name
)
205 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
206 (vc-registered . ignore
)
207 (verify-visited-file-modtime . ignore
)
208 (write-region . tramp-smb-handle-write-region
)
210 "Alist of handler functions for Tramp SMB method.
211 Operations not mentioned here will be handled by the default Emacs primitives.")
214 (defsubst tramp-smb-file-name-p
(filename)
215 "Check if it's a filename for SMB servers."
216 (let ((v (tramp-dissect-file-name filename
)))
217 (string= (tramp-file-name-method v
) tramp-smb-method
)))
220 (defun tramp-smb-file-name-handler (operation &rest args
)
221 "Invoke the SMB related OPERATION.
222 First arg specifies the OPERATION, second arg is a list of arguments to
223 pass to the OPERATION."
224 (let ((fn (assoc operation tramp-smb-file-name-handler-alist
)))
226 (save-match-data (apply (cdr fn
) args
))
227 (tramp-run-real-handler operation args
))))
230 (unless (memq system-type
'(cygwin windows-nt
))
231 (add-to-list 'tramp-foreign-file-name-handler-alist
232 (cons 'tramp-smb-file-name-p
'tramp-smb-file-name-handler
)))
235 ;; File name primitives.
237 (defun tramp-smb-handle-add-name-to-file
238 (filename newname
&optional ok-if-already-exists
)
239 "Like `add-name-to-file' for Tramp files."
240 (unless (tramp-equal-remote filename newname
)
241 (with-parsed-tramp-file-name
242 (if (tramp-tramp-file-p filename
) filename newname
) nil
245 "add-name-to-file: %s"
246 "only implemented for same method, same user, same host")))
247 (with-parsed-tramp-file-name filename v1
248 (with-parsed-tramp-file-name newname v2
249 (when (file-directory-p filename
)
252 "add-name-to-file: %s must not be a directory" filename
))
253 (when (and (not ok-if-already-exists
)
254 (file-exists-p newname
)
255 (not (numberp ok-if-already-exists
))
258 "File %s already exists; make it a new name anyway? "
262 "add-name-to-file: file %s already exists" newname
))
263 ;; We must also flush the cache of the directory, because
264 ;; `file-attributes' reads the values from there.
265 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
266 (tramp-flush-file-property v2 v2-localname
)
268 (tramp-smb-send-command
272 (if (tramp-smb-get-cifs-capabilities v1
) "link" "hardlink")
273 (tramp-smb-get-localname v1
)
274 (tramp-smb-get-localname v2
)))
277 "error with add-name-to-file, see buffer `%s' for details"
280 (defun tramp-smb-handle-copy-directory
281 (dirname newname
&optional keep-date parents
)
282 "Like `copy-directory' for Tramp files. KEEP-DATE is not handled."
283 (setq dirname
(expand-file-name dirname
)
284 newname
(expand-file-name newname
))
285 (let ((t1 (tramp-tramp-file-p dirname
))
286 (t2 (tramp-tramp-file-p newname
)))
287 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
289 ;; We must use a local temporary directory.
294 tramp-temp-name-prefix
295 (tramp-compat-temporary-file-directory)))))
298 (tramp-compat-copy-directory dirname tmpdir keep-date parents
)
299 (tramp-compat-copy-directory tmpdir newname keep-date parents
))
300 (tramp-compat-delete-directory tmpdir
'recursive
))))
302 ;; We can copy recursively.
304 (let ((prompt (tramp-smb-send-command v
"prompt"))
305 (recurse (tramp-smb-send-command v
"recurse")))
306 (unless (file-directory-p newname
)
307 (make-directory newname parents
))
312 (tramp-smb-send-command
313 v
(format "cd \"%s\"" (tramp-smb-get-localname v
)))
314 (tramp-smb-send-command
315 v
(format "lcd \"%s\"" (if t1 newname dirname
)))
317 (tramp-smb-send-command v
"mget *")
318 (tramp-smb-send-command v
"mput *")))
320 (with-current-buffer (tramp-get-connection-buffer v
)
321 (goto-char (point-min))
322 (search-forward-regexp tramp-smb-errors nil t
)
325 "%s `%s'" (match-string 0) (if t1 dirname newname
))))
327 (tramp-smb-send-command
329 "cd %s" (if (tramp-smb-get-cifs-capabilities v
) "/" "\\")))
330 ;; Toggle prompt and recurse OFF.
331 (if prompt
(tramp-smb-send-command v
"prompt"))
332 (if recurse
(tramp-smb-send-command v
"recurse")))))
334 ;; We must do it file-wise.
336 (tramp-run-real-handler
337 'copy-directory
(list dirname newname keep-date parents
)))))))
339 (defun tramp-smb-handle-copy-file
340 (filename newname
&optional ok-if-already-exists keep-date
341 preserve-uid-gid preserve-selinux-context
)
342 "Like `copy-file' for Tramp files.
343 KEEP-DATE is not handled in case NEWNAME resides on an SMB server.
344 PRESERVE-UID-GID and PRESERVE-SELINUX-CONTEXT are completely ignored."
345 (setq filename
(expand-file-name filename
)
346 newname
(expand-file-name newname
))
347 (tramp-with-progress-reporter
348 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
349 0 (format "Copying %s to %s" filename newname
)
351 (let ((tmpfile (file-local-copy filename
)))
356 (rename-file tmpfile newname ok-if-already-exists
)
358 (delete-file tmpfile
)
359 (signal (car err
) (cdr err
))))
362 (when (file-directory-p newname
)
364 (expand-file-name (file-name-nondirectory filename
) newname
)))
366 (with-parsed-tramp-file-name newname nil
367 (when (and (not ok-if-already-exists
)
368 (file-exists-p newname
))
369 (tramp-error v
'file-already-exists newname
))
371 ;; We must also flush the cache of the directory, because
372 ;; `file-attributes' reads the values from there.
373 (tramp-flush-file-property v
(file-name-directory localname
))
374 (tramp-flush-file-property v localname
)
375 (unless (tramp-smb-get-share v
)
377 v
'file-error
"Target `%s' must contain a share name" newname
))
378 (unless (tramp-smb-send-command
379 v
(format "put \"%s\" \"%s\""
380 filename
(tramp-smb-get-localname v
)))
381 (tramp-error v
'file-error
"Cannot copy `%s'" filename
))))))
383 ;; KEEP-DATE handling.
384 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
)))))
386 (defun tramp-smb-handle-delete-directory (directory &optional recursive
)
387 "Like `delete-directory' for Tramp files."
388 (setq directory
(directory-file-name (expand-file-name directory
)))
389 (when (file-exists-p directory
)
393 (if (file-directory-p file
)
394 (tramp-compat-delete-directory file recursive
)
396 ;; We do not want to delete "." and "..".
398 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
400 (with-parsed-tramp-file-name directory nil
401 ;; We must also flush the cache of the directory, because
402 ;; `file-attributes' reads the values from there.
403 (tramp-flush-file-property v
(file-name-directory localname
))
404 (tramp-flush-directory-property v localname
)
405 (unless (tramp-smb-send-command
408 (if (tramp-smb-get-cifs-capabilities v
) "posix_rmdir" "rmdir")
409 (tramp-smb-get-localname v
)))
411 (with-current-buffer (tramp-get-connection-buffer v
)
412 (goto-char (point-min))
413 (search-forward-regexp tramp-smb-errors nil t
)
415 v
'file-error
"%s `%s'" (match-string 0) directory
))))))
417 (defun tramp-smb-handle-delete-file (filename &optional trash
)
418 "Like `delete-file' for Tramp files."
419 (setq filename
(expand-file-name filename
))
420 (when (file-exists-p filename
)
421 (with-parsed-tramp-file-name filename nil
422 ;; We must also flush the cache of the directory, because
423 ;; `file-attributes' reads the values from there.
424 (tramp-flush-file-property v
(file-name-directory localname
))
425 (tramp-flush-file-property v localname
)
426 (unless (tramp-smb-send-command
429 (if (tramp-smb-get-cifs-capabilities v
) "posix_unlink" "rm")
430 (tramp-smb-get-localname v
)))
432 (with-current-buffer (tramp-get-connection-buffer v
)
433 (goto-char (point-min))
434 (search-forward-regexp tramp-smb-errors nil t
)
436 v
'file-error
"%s `%s'" (match-string 0) filename
))))))
438 (defun tramp-smb-handle-directory-files
439 (directory &optional full match nosort
)
440 "Like `directory-files' for Tramp files."
441 (let ((result (mapcar 'directory-file-name
442 (file-name-all-completions "" directory
))))
443 ;; Discriminate with regexp.
447 (mapcar (lambda (x) (when (string-match match x
) x
))
453 (lambda (x) (expand-file-name x directory
))
455 ;; Sort them if necessary.
456 (unless nosort
(setq result
(sort result
'string-lessp
)))
460 (defun tramp-smb-handle-expand-file-name (name &optional dir
)
461 "Like `expand-file-name' for Tramp files."
462 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
463 (setq dir
(or dir default-directory
"/"))
464 ;; Unless NAME is absolute, concat DIR and NAME.
465 (unless (file-name-absolute-p name
)
466 (setq name
(concat (file-name-as-directory dir
) name
)))
467 ;; If NAME is not a Tramp file, run the real handler.
468 (if (not (tramp-tramp-file-p name
))
469 (tramp-run-real-handler 'expand-file-name
(list name nil
))
471 (with-parsed-tramp-file-name name nil
472 ;; Tilde expansion if necessary. We use the user name as share,
473 ;; which is offen the case in domains.
474 (when (string-match "\\`/?~\\([^/]*\\)" localname
)
477 (if (zerop (length (match-string 1 localname
)))
478 (tramp-file-name-real-user v
)
479 (match-string 1 localname
))
481 ;; Make the file name absolute.
482 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
483 (setq localname
(concat "/" localname
)))
484 ;; No tilde characters in file name, do normal
485 ;; `expand-file-name' (this does "/./" and "/../").
486 (tramp-make-tramp-file-name
488 (tramp-run-real-handler 'expand-file-name
(list localname
))))))
490 (defun tramp-smb-handle-file-attributes (filename &optional id-format
)
491 "Like `file-attributes' for Tramp files."
492 (unless id-format
(setq id-format
'integer
))
493 (with-parsed-tramp-file-name filename nil
494 (with-file-property v localname
(format "file-attributes-%s" id-format
)
495 (if (and (tramp-smb-get-share v
) (tramp-smb-get-stat-capability v
))
496 (tramp-smb-do-file-attributes-with-stat v id-format
)
497 ;; Reading just the filename entry via "dir localname" is not
498 ;; possible, because when filename is a directory, some
499 ;; smbclient versions return the content of the directory, and
500 ;; other versions don't. Therefore, the whole content of the
501 ;; upper directory is retrieved, and the entry of the filename
502 ;; is extracted from.
503 (let* ((entries (tramp-smb-get-file-entries
504 (file-name-directory filename
)))
505 (entry (assoc (file-name-nondirectory filename
) entries
))
506 (uid (if (equal id-format
'string
) "nobody" -
1))
507 (gid (if (equal id-format
'string
) "nogroup" -
1))
508 (inode (tramp-get-inode v
))
509 (device (tramp-get-device v
)))
513 (list (and (string-match "d" (nth 1 entry
))
519 (nth 3 entry
) ;5 mtime
521 (nth 2 entry
) ;7 size
522 (nth 1 entry
) ;8 mode
524 inode
;10 inode number
525 device
))))))) ;11 file system number
527 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format
)
528 "Implement `file-attributes' for Tramp files using stat command."
530 vec
5 "file attributes with stat: %s" (tramp-file-name-localname vec
))
531 (with-current-buffer (tramp-get-buffer vec
)
532 (let* (size id link uid gid atime mtime ctime mode inode
)
533 (when (tramp-smb-send-command
534 vec
(format "stat \"%s\"" (tramp-smb-get-localname vec
)))
537 (goto-char (point-min))
538 (unless (re-search-forward tramp-smb-errors nil t
)
542 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
543 (setq size
(string-to-number (match-string 1))
544 id
(if (string-equal "directory" (match-string 2)) t
545 (if (string-equal "symbolic" (match-string 2)) ""))))
547 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
548 (setq inode
(string-to-number (match-string 1))
549 link
(string-to-number (match-string 2))))
551 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
552 (setq mode
(match-string 1)
553 uid
(if (equal id-format
'string
) (match-string 2)
554 (string-to-number (match-string 2)))
555 gid
(if (equal id-format
'string
) (match-string 3)
556 (string-to-number (match-string 3)))))
558 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
561 (string-to-number (match-string 6)) ;; sec
562 (string-to-number (match-string 5)) ;; min
563 (string-to-number (match-string 4)) ;; hour
564 (string-to-number (match-string 3)) ;; day
565 (string-to-number (match-string 2)) ;; month
566 (string-to-number (match-string 1))))) ;; year
568 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
571 (string-to-number (match-string 6)) ;; sec
572 (string-to-number (match-string 5)) ;; min
573 (string-to-number (match-string 4)) ;; hour
574 (string-to-number (match-string 3)) ;; day
575 (string-to-number (match-string 2)) ;; month
576 (string-to-number (match-string 1))))) ;; year
578 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
581 (string-to-number (match-string 6)) ;; sec
582 (string-to-number (match-string 5)) ;; min
583 (string-to-number (match-string 4)) ;; hour
584 (string-to-number (match-string 3)) ;; day
585 (string-to-number (match-string 2)) ;; month
586 (string-to-number (match-string 1)))))) ;; year
588 ;; Return the result.
589 (list id link uid gid atime mtime ctime size mode nil inode
590 (tramp-get-device vec
)))))))
592 (defun tramp-smb-handle-file-directory-p (filename)
593 "Like `file-directory-p' for Tramp files."
594 (and (file-exists-p filename
)
595 (eq ?d
(aref (nth 8 (file-attributes filename
)) 0))))
597 (defun tramp-smb-handle-file-local-copy (filename)
598 "Like `file-local-copy' for Tramp files."
599 (with-parsed-tramp-file-name filename nil
600 (unless (file-exists-p filename
)
603 "Cannot make local copy of non-existing file `%s'" filename
))
604 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
605 (tramp-with-progress-reporter
606 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
607 (unless (tramp-smb-send-command
608 v
(format "get \"%s\" \"%s\""
609 (tramp-smb-get-localname v
) tmpfile
))
610 ;; Oops, an error. We shall cleanup.
611 (delete-file tmpfile
)
613 v
'file-error
"Cannot make local copy of file `%s'" filename
)))
616 ;; This function should return "foo/" for directories and "bar" for
618 (defun tramp-smb-handle-file-name-all-completions (filename directory
)
619 "Like `file-name-all-completions' for Tramp files."
622 (with-parsed-tramp-file-name directory nil
623 (with-file-property v localname
"file-name-all-completions"
625 (let ((entries (tramp-smb-get-file-entries directory
)))
629 (if (string-match "d" (nth 1 x
))
630 (file-name-as-directory (nth 0 x
))
634 (defun tramp-smb-handle-file-writable-p (filename)
635 "Like `file-writable-p' for Tramp files."
636 (if (file-exists-p filename
)
637 (string-match "w" (or (nth 8 (file-attributes filename
)) ""))
638 (let ((dir (file-name-directory filename
)))
639 (and (file-exists-p dir
)
640 (file-writable-p dir
)))))
642 (defun tramp-smb-handle-insert-directory
643 (filename switches
&optional wildcard full-directory-p
)
644 "Like `insert-directory' for Tramp files."
645 (setq filename
(expand-file-name filename
))
647 ;; Called from `dired-add-entry'.
648 (setq filename
(file-name-as-directory filename
))
649 (setq filename
(directory-file-name filename
)))
650 (with-parsed-tramp-file-name filename nil
652 (let ((base (file-name-nondirectory filename
))
653 ;; We should not destroy the cache entry.
654 (entries (copy-sequence
655 (tramp-smb-get-file-entries
656 (file-name-directory filename
)))))
659 (string-match "\\." base
)
660 (setq base
(replace-match "\\\\." nil nil base
))
661 (string-match "\\*" base
)
662 (setq base
(replace-match ".*" nil nil base
))
663 (string-match "\\?" base
)
664 (setq base
(replace-match ".?" nil nil base
)))
670 (if (or wildcard
(zerop (length base
)))
671 ;; Check for matching entries.
675 (format "^%s" base
) (nth 0 x
))
678 ;; We just need the only and only entry FILENAME.
679 (list (assoc base entries
)))))
686 (if (string-match "t" switches
)
688 (tramp-time-less-p (nth 3 y
) (nth 3 x
))
690 (string-lessp (nth 0 x
) (nth 0 y
))))))
692 ;; Handle "-F" switch.
693 (when (string-match "F" switches
)
696 (when (not (zerop (length (car x
))))
698 ((char-equal ?d
(string-to-char (nth 1 x
)))
699 (setcar x
(concat (car x
) "/")))
700 ((char-equal ?x
(string-to-char (nth 1 x
)))
701 (setcar x
(concat (car x
) "*"))))))
707 (when (not (zerop (length (nth 0 x
))))
709 (when (tramp-smb-get-stat-capability v
)
711 (file-attributes filename
'string
)))))
714 "%10s %3d %-8s %-8s %8s %s "
715 (or (nth 8 attr
) (nth 1 x
)) ; mode
716 (or (nth 1 attr
) 1) ; inode
717 (or (nth 2 attr
) "nobody") ; uid
718 (or (nth 3 attr
) "nogroup") ; gid
719 (or (nth 7 attr
) (nth 2 x
)) ; size
721 (if (tramp-time-less-p
722 (tramp-time-subtract (current-time) (nth 3 x
))
727 ;; We mark the file name. The inserted name could be
728 ;; from somewhere else, so we use the relative file
729 ;; name of `default-directory'.
730 (let ((start (point)))
736 (nth 0 x
) (file-name-directory filename
)))))
737 (put-text-property start
(1- (point)) 'dired-filename t
))
739 (beginning-of-line))))
742 (defun tramp-smb-handle-make-directory (dir &optional parents
)
743 "Like `make-directory' for Tramp files."
744 (setq dir
(directory-file-name (expand-file-name dir
)))
745 (unless (file-name-absolute-p dir
)
746 (setq dir
(expand-file-name dir default-directory
)))
747 (with-parsed-tramp-file-name dir nil
749 (let* ((ldir (file-name-directory dir
)))
750 ;; Make missing directory parts.
752 (tramp-smb-get-share v
)
753 (not (file-directory-p ldir
)))
754 (make-directory ldir parents
))
756 (when (file-directory-p ldir
)
757 (make-directory-internal dir
))
758 (unless (file-directory-p dir
)
759 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
761 (defun tramp-smb-handle-make-directory-internal (directory)
762 "Like `make-directory-internal' for Tramp files."
763 (setq directory
(directory-file-name (expand-file-name directory
)))
764 (unless (file-name-absolute-p directory
)
765 (setq directory
(expand-file-name directory default-directory
)))
766 (with-parsed-tramp-file-name directory nil
768 (let* ((file (tramp-smb-get-localname v
)))
769 (when (file-directory-p (file-name-directory directory
))
770 (tramp-smb-send-command
772 (if (tramp-smb-get-cifs-capabilities v
)
774 "posix_mkdir \"%s\" %s"
775 file
(tramp-compat-decimal-to-octal (default-file-modes)))
776 (format "mkdir \"%s\"" file
)))
777 ;; We must also flush the cache of the directory, because
778 ;; `file-attributes' reads the values from there.
779 (tramp-flush-file-property v
(file-name-directory localname
))
780 (tramp-flush-file-property v localname
))
781 (unless (file-directory-p directory
)
783 v
'file-error
"Couldn't make directory %s" directory
))))))
785 (defun tramp-smb-handle-make-symbolic-link
786 (filename linkname
&optional ok-if-already-exists
)
787 "Like `make-symbolic-link' for Tramp files.
788 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
789 the symlink. If LINKNAME is a Tramp file, only the localname component is
790 used as the target of the symlink.
792 If LINKNAME is a Tramp file and the localname component is relative, then
793 it is expanded first, before the localname component is taken. Note that
794 this can give surprising results if the user/host for the source and
795 target of the symlink differ."
796 (unless (tramp-equal-remote filename linkname
)
797 (with-parsed-tramp-file-name
798 (if (tramp-tramp-file-p filename
) filename linkname
) nil
801 "make-symbolic-link: %s"
802 "only implemented for same method, same user, same host")))
803 (with-parsed-tramp-file-name filename v1
804 (with-parsed-tramp-file-name linkname v2
805 (when (file-directory-p filename
)
808 "make-symbolic-link: %s must not be a directory" filename
))
809 (when (and (not ok-if-already-exists
)
810 (file-exists-p linkname
)
811 (not (numberp ok-if-already-exists
))
814 "File %s already exists; make it a new name anyway? "
818 "make-symbolic-link: file %s already exists" linkname
))
819 (unless (tramp-smb-get-cifs-capabilities v1
)
820 (tramp-error v2
'file-error
"make-symbolic-link not supported"))
821 ;; We must also flush the cache of the directory, because
822 ;; `file-attributes' reads the values from there.
823 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
824 (tramp-flush-file-property v2 v2-localname
)
826 (tramp-smb-send-command
829 "symlink \"%s\" \"%s\""
830 (tramp-smb-get-localname v1
)
831 (tramp-smb-get-localname v2
)))
834 "error with make-symbolic-link, see buffer `%s' for details"
837 (defun tramp-smb-handle-rename-file
838 (filename newname
&optional ok-if-already-exists
)
839 "Like `rename-file' for Tramp files."
840 (setq filename
(expand-file-name filename
)
841 newname
(expand-file-name newname
))
842 (tramp-with-progress-reporter
843 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
844 0 (format "Renaming %s to %s" filename newname
)
846 (let ((tmpfile (file-local-copy filename
)))
851 (rename-file tmpfile newname ok-if-already-exists
)
853 (delete-file tmpfile
)
854 (signal (car err
) (cdr err
))))
857 (when (file-directory-p newname
)
858 (setq newname
(expand-file-name
859 (file-name-nondirectory filename
) newname
)))
861 (with-parsed-tramp-file-name newname nil
862 (when (and (not ok-if-already-exists
)
863 (file-exists-p newname
))
864 (tramp-error v
'file-already-exists newname
))
865 ;; We must also flush the cache of the directory, because
866 ;; `file-attributes' reads the values from there.
867 (tramp-flush-file-property v
(file-name-directory localname
))
868 (tramp-flush-file-property v localname
)
869 (unless (tramp-smb-send-command
870 v
(format "put %s \"%s\""
871 filename
(tramp-smb-get-localname v
)))
872 (tramp-error v
'file-error
"Cannot rename `%s'" filename
)))))
874 (delete-file filename
)))
876 (defun tramp-smb-handle-set-file-modes (filename mode
)
877 "Like `set-file-modes' for Tramp files."
878 (with-parsed-tramp-file-name filename nil
879 (when (tramp-smb-get-cifs-capabilities v
)
880 (tramp-flush-file-property v localname
)
881 (unless (tramp-smb-send-command
882 v
(format "chmod \"%s\" %s"
883 (tramp-smb-get-localname v
)
884 (tramp-compat-decimal-to-octal mode
)))
886 v
'file-error
"Error while changing file's mode %s" filename
)))))
888 (defun tramp-smb-handle-substitute-in-file-name (filename)
889 "Like `handle-substitute-in-file-name' for Tramp files.
890 \"//\" substitutes only in the local filename part. Catches
891 errors for shares like \"C$/\", which are common in Microsoft Windows."
892 (with-parsed-tramp-file-name filename nil
893 ;; Ignore in LOCALNAME everything before "//".
894 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
896 (concat (file-remote-p filename
)
897 (replace-match "\\1" nil nil localname
)))))
899 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
902 (defun tramp-smb-handle-write-region
903 (start end filename
&optional append visit lockname confirm
)
904 "Like `write-region' for Tramp files."
905 (setq filename
(expand-file-name filename
))
906 (with-parsed-tramp-file-name filename nil
907 (unless (eq append nil
)
909 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
910 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
911 (when (and (not (featurep 'xemacs
))
912 confirm
(file-exists-p filename
))
913 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
915 (tramp-error v
'file-error
"File not overwritten")))
916 ;; We must also flush the cache of the directory, because
917 ;; `file-attributes' reads the values from there.
918 (tramp-flush-file-property v
(file-name-directory localname
))
919 (tramp-flush-file-property v localname
)
920 (let ((curbuf (current-buffer))
921 (tmpfile (tramp-compat-make-temp-file filename
)))
922 ;; We say `no-message' here because we don't want the visited file
923 ;; modtime data to be clobbered from the temp file. We call
924 ;; `set-visited-file-modtime' ourselves later on.
925 (tramp-run-real-handler
927 (if confirm
; don't pass this arg unless defined for backward compat.
928 (list start end tmpfile append
'no-message lockname confirm
)
929 (list start end tmpfile append
'no-message lockname
)))
931 (tramp-with-progress-reporter
932 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
934 (unless (tramp-smb-send-command
935 v
(format "put %s \"%s\""
936 tmpfile
(tramp-smb-get-localname v
)))
937 (tramp-error v
'file-error
"Cannot write `%s'" filename
))
938 (delete-file tmpfile
)))
940 (unless (equal curbuf
(current-buffer))
943 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer)))
945 (set-visited-file-modtime)))))
948 ;; Internal file name functions.
950 (defun tramp-smb-get-share (vec)
951 "Returns the share name of LOCALNAME."
953 (let ((localname (tramp-file-name-localname vec
)))
954 (when (string-match "^/?\\([^/]+\\)/" localname
)
955 (match-string 1 localname
)))))
957 (defun tramp-smb-get-localname (vec)
958 "Returns the file name of LOCALNAME.
959 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
961 (let ((localname (tramp-file-name-localname vec
)))
964 (if (string-match "^/?[^/]+\\(/.*\\)" localname
)
965 ;; There is a share, sparated by "/".
966 (if (not (tramp-smb-get-cifs-capabilities vec
))
968 (lambda (x) (if (equal x ?
/) "\\" (char-to-string x
)))
969 (match-string 1 localname
) "")
970 (match-string 1 localname
))
971 ;; There is just a share.
972 (if (string-match "^/?\\([^/]+\\)$" localname
)
973 (match-string 1 localname
)
976 ;; Sometimes we have discarded `substitute-in-file-name'.
977 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname
)
978 (setq localname
(replace-match "$" nil nil localname
1)))
982 ;; Share names of a host are cached. It is very unlikely that the
983 ;; shares do change during connection.
984 (defun tramp-smb-get-file-entries (directory)
985 "Read entries which match DIRECTORY.
986 Either the shares are listed, or the `dir' command is executed.
987 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
988 (with-parsed-tramp-file-name (file-name-as-directory directory
) nil
989 (setq localname
(or localname
"/"))
990 (with-file-property v localname
"file-entries"
991 (with-current-buffer (tramp-get-buffer v
)
992 (let* ((share (tramp-smb-get-share v
))
993 (cache (tramp-get-connection-property v
"share-cache" nil
))
996 (if (and (not share
) cache
)
997 ;; Return cached shares.
1002 (tramp-smb-send-command
1003 v
(format "dir \"%s*\"" (tramp-smb-get-localname v
)))
1004 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1005 (tramp-smb-maybe-open-connection v
))
1007 ;; Loop the listing.
1008 (goto-char (point-min))
1009 (if (re-search-forward tramp-smb-errors nil t
)
1010 (tramp-error v
'file-error
"%s `%s'" (match-string 0) directory
)
1012 (setq entry
(tramp-smb-read-file-entry share
))
1014 (when entry
(add-to-list 'res entry
))))
1016 ;; Cache share entries.
1018 (tramp-set-connection-property v
"share-cache" res
)))
1020 ;; Add directory itself.
1021 (add-to-list 'res
'("" "drwxrwxrwx" 0 (0 0)))
1023 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1024 ;; If there's no short delay, it returns nil. No idea about.
1025 (when (featurep 'xemacs
) (sleep-for 0.01))
1030 ;; Return either a share name (if SHARE is nil), or a file name.
1032 ;; If shares are listed, the following format is expected:
1034 ;; Disk| - leading spaces
1035 ;; [^|]+| - share name, 14 char
1038 ;; Entries provided by smbclient DIR aren't fully regular.
1039 ;; They should have the format
1041 ;; \s-\{2,2} - leading spaces
1042 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1043 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1044 ;; \s- - space delimiter
1045 ;; \s-+[0-9]+ - size, 8 chars, right bound
1046 ;; \s-\{2,2\} - space delimiter
1047 ;; \w\{3,3\} - weekday
1048 ;; \s- - space delimiter
1049 ;; \w\{3,3\} - month
1050 ;; \s- - space delimiter
1052 ;; \s- - space delimiter
1053 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1054 ;; \s- - space delimiter
1055 ;; [0-9]\{4,4\} - year
1057 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1058 ;; has function display_finfo:
1060 ;; d_printf(" %-30s%7.7s %8.0f %s",
1062 ;; attrib_string(finfo->mode),
1063 ;; (double)finfo->size,
1064 ;; asctime(LocalTime(&t)));
1066 ;; in Samba 1.9, there's the following code:
1068 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1069 ;; CNV_LANG(finfo->name),
1070 ;; attrib_string(finfo->mode),
1072 ;; asctime(LocalTime(&t))));
1075 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1076 ;; available in older Emacsen.
1077 ;; * The length of constructs (file name, size) might exceed the default.
1078 ;; * File names might contain spaces.
1079 ;; * Permissions might be empty.
1081 ;; So we try to analyze backwards.
1082 (defun tramp-smb-read-file-entry (share)
1083 "Parse entry in SMB output buffer.
1084 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1085 Result is the list (LOCALNAME MODE SIZE MTIME)."
1086 ;; We are called from `tramp-smb-get-file-entries', which sets the
1088 (let ((line (buffer-substring (point) (point-at-eol)))
1089 localname mode size month day hour min sec year mtime
)
1093 ;; Read share entries.
1094 (when (string-match "^Disk|\\([^|]+\\)|" line
)
1095 (setq localname
(match-string 1 line
)
1103 (if (string-match "\\([0-9]+\\)$" line
)
1104 (setq year
(string-to-number (match-string 1 line
))
1105 line
(substring line
0 -
5))
1109 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line
)
1110 (setq hour
(string-to-number (match-string 1 line
))
1111 min
(string-to-number (match-string 2 line
))
1112 sec
(string-to-number (match-string 3 line
))
1113 line
(substring line
0 -
9))
1117 (if (string-match "\\([0-9]+\\)$" line
)
1118 (setq day
(string-to-number (match-string 1 line
))
1119 line
(substring line
0 -
3))
1123 (if (string-match "\\(\\w+\\)$" line
)
1124 (setq month
(match-string 1 line
)
1125 line
(substring line
0 -
4))
1129 (if (string-match "\\(\\w+\\)$" line
)
1130 (setq line
(substring line
0 -
5))
1134 (if (string-match "\\([0-9]+\\)$" line
)
1135 (let ((length (- (max 10 (1+ (length (match-string 1 line
)))))))
1136 (setq size
(string-to-number (match-string 1 line
)))
1137 (when (string-match "\\([ADHRSV]+\\)" (substring line length
))
1138 (setq length
(+ length
(match-end 0))))
1139 (setq line
(substring line
0 length
)))
1142 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1143 (if (string-match "\\([ADHRSV]+\\)?$" line
)
1145 mode
(or (match-string 1 line
) "")
1146 mode
(save-match-data (format
1148 (if (string-match "D" mode
) "d" "-")
1151 (concat "r" (if (string-match "R" mode
) "-" "w") "x"))))
1152 line
(substring line
0 -
6))
1156 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line
)
1157 (setq localname
(match-string 1 line
))
1160 (when (and localname mode size
)
1162 (if (and sec min hour day month year
)
1165 (cdr (assoc (downcase month
) tramp-parse-time-months
))
1168 (list localname mode size mtime
))))
1170 (defun tramp-smb-get-cifs-capabilities (vec)
1171 "Check, whether the SMB server supports POSIX commands."
1172 ;; When we are not logged in yet, we return nil.
1173 (if (let ((p (tramp-get-connection-process vec
)))
1174 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1175 (with-connection-property
1176 (tramp-get-connection-process vec
) "cifs-capabilities"
1178 (when (tramp-smb-send-command vec
"posix")
1179 (with-current-buffer (tramp-get-buffer vec
)
1180 (goto-char (point-min))
1182 (re-search-forward "Server supports CIFS capabilities" nil t
)
1186 (buffer-substring (point) (point-at-eol)) nil t
)))))))))
1188 (defun tramp-smb-get-stat-capability (vec)
1189 "Check, whether the SMB server supports the STAT command."
1190 ;; When we are not logged in yet, we return nil.
1191 (if (let ((p (tramp-get-connection-process vec
)))
1192 (and p
(processp p
) (memq (process-status p
) '(run open
))))
1193 (with-connection-property
1194 (tramp-get-connection-process vec
) "stat-capability"
1195 (tramp-smb-send-command vec
"stat ."))))
1198 ;; Connection functions.
1200 (defun tramp-smb-send-command (vec command
)
1201 "Send the COMMAND to connection VEC.
1202 Returns nil if there has been an error message from smbclient."
1203 (tramp-smb-maybe-open-connection vec
)
1204 (tramp-message vec
6 "%s" command
)
1205 (tramp-send-string vec command
)
1206 (tramp-smb-wait-for-output vec
))
1208 (defun tramp-smb-maybe-open-connection (vec)
1209 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1210 Does not do anything if a connection is already open, but re-opens the
1211 connection if a previous connection has died for some reason."
1212 (let* ((share (tramp-smb-get-share vec
))
1213 (buf (tramp-get-buffer vec
))
1214 (p (get-buffer-process buf
)))
1216 ;; Check whether we still have the same smbclient version.
1217 ;; Otherwise, we must delete the connection cache, because
1218 ;; capabilities migh have changed.
1219 (unless (processp p
)
1220 (let ((default-directory (tramp-compat-temporary-file-directory))
1221 (command (concat tramp-smb-program
" -V")))
1223 (unless tramp-smb-version
1224 (unless (executable-find tramp-smb-program
)
1227 "Cannot find command %s in %s" tramp-smb-program exec-path
))
1228 (setq tramp-smb-version
(shell-command-to-string command
))
1229 (tramp-message vec
6 command
)
1230 (tramp-message vec
6 "\n%s" tramp-smb-version
)
1231 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version
)
1232 (setq tramp-smb-version
1233 (replace-match "" nil nil tramp-smb-version
))))
1235 (unless (string-equal
1237 (tramp-get-connection-property
1238 vec
"smbclient-version" tramp-smb-version
))
1239 (tramp-flush-directory-property vec
"")
1240 (tramp-flush-connection-property vec
))
1242 (tramp-set-connection-property
1243 vec
"smbclient-version" tramp-smb-version
)))
1245 ;; If too much time has passed since last command was sent, look
1246 ;; whether there has been an error message; maybe due to
1247 ;; connection timeout.
1248 (with-current-buffer buf
1249 (goto-char (point-min))
1250 (when (and (> (tramp-time-diff
1252 (tramp-get-connection-property
1253 p
"last-cmd-time" '(0 0 0)))
1255 p
(processp p
) (memq (process-status p
) '(run open
))
1256 (re-search-forward tramp-smb-errors nil t
))
1260 ;; Check whether it is still the same share.
1262 (and p
(processp p
) (memq (process-status p
) '(run open
))
1265 (tramp-get-connection-property p
"smb-share" "")))
1268 ;; There might be unread output from checking for share names.
1269 (when buf
(with-current-buffer buf
(erase-buffer)))
1270 (when (and p
(processp p
)) (delete-process p
))
1272 (let* ((user (tramp-file-name-user vec
))
1273 (host (tramp-file-name-host vec
))
1274 (real-user (tramp-file-name-real-user vec
))
1275 (real-host (tramp-file-name-real-host vec
))
1276 (domain (tramp-file-name-domain vec
))
1277 (port (tramp-file-name-port vec
))
1281 (setq args
(list (concat "//" real-host
"/" share
)))
1282 (setq args
(list "-g" "-L" real-host
)))
1284 (if (not (zerop (length real-user
)))
1285 (setq args
(append args
(list "-U" real-user
)))
1286 (setq args
(append args
(list "-N"))))
1288 (when domain
(setq args
(append args
(list "-W" domain
))))
1289 (when port
(setq args
(append args
(list "-p" port
))))
1290 (when tramp-smb-conf
1291 (setq args
(append args
(list "-s" tramp-smb-conf
))))
1294 (tramp-with-progress-reporter
1296 (format "Opening connection for //%s%s/%s"
1297 (if (not (zerop (length user
))) (concat user
"@") "")
1300 (let* ((coding-system-for-read nil
)
1301 (process-connection-type tramp-process-connection-type
)
1302 (p (let ((default-directory
1303 (tramp-compat-temporary-file-directory)))
1304 (apply #'start-process
1305 (tramp-buffer-name vec
) (tramp-get-buffer vec
)
1306 tramp-smb-program args
))))
1309 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1310 (tramp-compat-set-process-query-on-exit-flag p nil
)
1312 ;; Set variables for computing the prompt for reading password.
1313 (setq tramp-current-method tramp-smb-method
1314 tramp-current-user user
1315 tramp-current-host host
)
1317 ;; Play login scenario.
1318 (tramp-process-actions
1321 tramp-smb-actions-with-share
1322 tramp-smb-actions-without-share
))
1324 ;; Check server version.
1325 (with-current-buffer (tramp-get-connection-buffer vec
)
1326 (goto-char (point-min))
1327 (search-forward-regexp
1328 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]" nil t
)
1329 (let ((smbserver-version (match-string 0)))
1333 (tramp-get-connection-property
1334 vec
"smbserver-version" smbserver-version
))
1335 (tramp-flush-directory-property vec
"")
1336 (tramp-flush-connection-property vec
))
1337 (tramp-set-connection-property
1338 vec
"smbserver-version" smbserver-version
)))
1340 ;; Set chunksize. Otherwise, `tramp-send-string' might
1342 (tramp-set-connection-property p
"smb-share" share
)
1343 (tramp-set-connection-property
1344 p
"chunksize" tramp-chunksize
))))))))
1346 ;; We don't use timeouts. If needed, the caller shall wrap around.
1347 (defun tramp-smb-wait-for-output (vec)
1348 "Wait for output from smbclient command.
1349 Returns nil if an error message has appeared."
1350 (with-current-buffer (tramp-get-buffer vec
)
1351 (let ((p (get-buffer-process (current-buffer)))
1352 (found (progn (goto-char (point-min))
1353 (re-search-forward tramp-smb-prompt nil t
)))
1354 (err (progn (goto-char (point-min))
1355 (re-search-forward tramp-smb-errors nil t
))))
1357 ;; Algorithm: get waiting output. See if last line contains
1358 ;; tramp-smb-prompt sentinel or tramp-smb-errors strings.
1359 ;; If not, wait a bit and again get waiting output.
1360 (while (and (not found
) (not err
))
1362 ;; Accept pending output.
1363 (tramp-accept-process-output p
)
1365 ;; Search for prompt.
1366 (goto-char (point-min))
1367 (setq found
(re-search-forward tramp-smb-prompt nil t
))
1369 ;; Search for errors.
1370 (goto-char (point-min))
1371 (setq err
(re-search-forward tramp-smb-errors nil t
)))
1373 ;; When the process is still alive, read pending output.
1374 (while (and (not found
) (memq (process-status p
) '(run open
)))
1376 ;; Accept pending output.
1377 (tramp-accept-process-output p
)
1379 ;; Search for prompt.
1380 (goto-char (point-min))
1381 (setq found
(re-search-forward tramp-smb-prompt nil t
)))
1383 ;; Return value is whether no error message has appeared.
1384 (tramp-message vec
6 "\n%s" (buffer-string))
1387 (add-hook 'tramp-unload-hook
1389 (unload-feature 'tramp-smb
'force
)))
1391 (provide 'tramp-smb
)
1395 ;; * Error handling in case password is wrong.
1396 ;; * Read password from "~/.netrc".
1397 ;; * Return more comprehensive file permission string.
1398 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1399 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1400 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1402 ;; * Make it multi-hop capable.
1404 ;;; tramp-smb.el ends here