gnus-art.el: Rewrite the Date header formatting functionality.
[emacs.git] / lisp / net / tramp-smb.el
blob51e9c600b0c26b070737f78a93b5cb4776dd3b56
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
7 ;; Package: tramp
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/>.
24 ;;; Commentary:
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
28 ;;; Code:
30 (eval-when-compile (require 'cl)) ; block, return
31 (require 'tramp)
33 ;; Define SMB method ...
34 ;;;###tramp-autoload
35 (defconst tramp-smb-method "smb"
36 "*Method to connect SAMBA and M$ SMB servers.")
38 ;; ... and add it to the method list.
39 ;;;###tramp-autoload
40 (unless (memq system-type '(cygwin windows-nt))
41 (add-to-list 'tramp-methods (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.
45 ;;;###tramp-autoload
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.
51 ;;;###tramp-autoload
52 (add-to-list 'tramp-default-user-alist
53 `(,(concat "\\`" tramp-smb-method "\\'") nil nil))
55 ;; Add completion function for SMB method.
56 (tramp-set-completion-function
57 tramp-smb-method
58 '((tramp-parse-netrc "~/.netrc")))
60 (defcustom tramp-smb-program "smbclient"
61 "*Name of SMB client to run."
62 :group 'tramp
63 :type 'string)
65 (defcustom tramp-smb-conf "/dev/null"
66 "*Path of the smb.conf file.
67 If it is nil, no smb.conf will be added to the `tramp-smb-program'
68 call, letting the SMB client use the default one."
69 :group 'tramp
70 :type '(choice (const nil) (file :must-match t)))
72 (defvar tramp-smb-version nil
73 "*Version string of the SMB client.")
75 (defconst tramp-smb-prompt "^smb: .+> \\|^\\s-+Server\\s-+Comment$"
76 "Regexp used as prompt in smbclient.")
78 (defconst tramp-smb-errors
79 ;; `regexp-opt' not possible because of first string.
80 (mapconcat
81 'identity
82 '(;; Connection error / timeout / unknown command.
83 "Connection to \\S-+ failed"
84 "Read from server failed, maybe it closed the connection"
85 "Call timed out: server did not respond"
86 "\\S-+: command not found"
87 "Server doesn't support UNIX CIFS calls"
88 ;; Samba.
89 "ERRDOS"
90 "ERRHRD"
91 "ERRSRV"
92 "ERRbadfile"
93 "ERRbadpw"
94 "ERRfilexists"
95 "ERRnoaccess"
96 "ERRnomem"
97 "ERRnosuchshare"
98 ;; Windows 4.0 (Windows NT), Windows 5.0 (Windows 2000),
99 ;; Windows 5.1 (Windows XP), Windows 5.2 (Windows Server 2003).
100 "NT_STATUS_ACCESS_DENIED"
101 "NT_STATUS_ACCOUNT_LOCKED_OUT"
102 "NT_STATUS_BAD_NETWORK_NAME"
103 "NT_STATUS_CANNOT_DELETE"
104 "NT_STATUS_CONNECTION_REFUSED"
105 "NT_STATUS_DIRECTORY_NOT_EMPTY"
106 "NT_STATUS_DUPLICATE_NAME"
107 "NT_STATUS_FILE_IS_A_DIRECTORY"
108 "NT_STATUS_LOGON_FAILURE"
109 "NT_STATUS_NETWORK_ACCESS_DENIED"
110 "NT_STATUS_NOT_IMPLEMENTED"
111 "NT_STATUS_NO_SUCH_FILE"
112 "NT_STATUS_OBJECT_NAME_COLLISION"
113 "NT_STATUS_OBJECT_NAME_INVALID"
114 "NT_STATUS_OBJECT_NAME_NOT_FOUND"
115 "NT_STATUS_SHARING_VIOLATION"
116 "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE"
117 "NT_STATUS_WRONG_PASSWORD")
118 "\\|")
119 "Regexp for possible error strings of SMB servers.
120 Used instead of analyzing error codes of commands.")
122 (defconst tramp-smb-actions-with-share
123 '((tramp-smb-prompt tramp-action-succeed)
124 (tramp-password-prompt-regexp tramp-action-password)
125 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
126 (tramp-smb-errors tramp-action-permission-denied)
127 (tramp-process-alive-regexp tramp-action-process-alive))
128 "List of pattern/action pairs.
129 This list is used for login to SMB servers.
131 See `tramp-actions-before-shell' for more info.")
133 (defconst tramp-smb-actions-without-share
134 '((tramp-password-prompt-regexp tramp-action-password)
135 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
136 (tramp-smb-errors tramp-action-permission-denied)
137 (tramp-process-alive-regexp tramp-action-out-of-band))
138 "List of pattern/action pairs.
139 This list is used for login to SMB servers.
141 See `tramp-actions-before-shell' for more info.")
143 ;; New handlers should be added here.
144 (defconst tramp-smb-file-name-handler-alist
146 ;; `access-file' performed by default handler.
147 (add-name-to-file . tramp-smb-handle-add-name-to-file)
148 ;; `byte-compiler-base-file-name' performed by default handler.
149 (copy-directory . tramp-smb-handle-copy-directory)
150 (copy-file . tramp-smb-handle-copy-file)
151 (delete-directory . tramp-smb-handle-delete-directory)
152 (delete-file . tramp-smb-handle-delete-file)
153 ;; `diff-latest-backup-file' performed by default handler.
154 (directory-file-name . tramp-handle-directory-file-name)
155 (directory-files . tramp-smb-handle-directory-files)
156 (directory-files-and-attributes
157 . tramp-handle-directory-files-and-attributes)
158 (dired-call-process . ignore)
159 (dired-compress-file . ignore)
160 (dired-uncache . tramp-handle-dired-uncache)
161 (expand-file-name . tramp-smb-handle-expand-file-name)
162 (file-accessible-directory-p . tramp-smb-handle-file-directory-p)
163 (file-attributes . tramp-smb-handle-file-attributes)
164 (file-directory-p . tramp-smb-handle-file-directory-p)
165 (file-executable-p . tramp-handle-file-exists-p)
166 (file-exists-p . tramp-handle-file-exists-p)
167 (file-local-copy . tramp-smb-handle-file-local-copy)
168 (file-modes . tramp-handle-file-modes)
169 (file-name-all-completions . tramp-smb-handle-file-name-all-completions)
170 (file-name-as-directory . tramp-handle-file-name-as-directory)
171 (file-name-completion . tramp-handle-file-name-completion)
172 (file-name-directory . tramp-handle-file-name-directory)
173 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
174 ;; `file-name-sans-versions' performed by default handler.
175 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
176 (file-ownership-preserved-p . ignore)
177 (file-readable-p . tramp-handle-file-exists-p)
178 (file-regular-p . tramp-handle-file-regular-p)
179 (file-remote-p . tramp-handle-file-remote-p)
180 ;; `file-selinux-context' performed by default handler.
181 (file-symlink-p . tramp-handle-file-symlink-p)
182 ;; `file-truename' performed by default handler.
183 (file-writable-p . tramp-smb-handle-file-writable-p)
184 (find-backup-file-name . tramp-handle-find-backup-file-name)
185 ;; `find-file-noselect' performed by default handler.
186 ;; `get-file-buffer' performed by default handler.
187 (insert-directory . tramp-smb-handle-insert-directory)
188 (insert-file-contents . tramp-handle-insert-file-contents)
189 (load . tramp-handle-load)
190 (make-directory . tramp-smb-handle-make-directory)
191 (make-directory-internal . tramp-smb-handle-make-directory-internal)
192 (make-symbolic-link . tramp-smb-handle-make-symbolic-link)
193 (rename-file . tramp-smb-handle-rename-file)
194 (set-file-modes . tramp-smb-handle-set-file-modes)
195 ;; `set-file-selinux-context' performed by default handler.
196 (set-file-times . ignore)
197 (set-visited-file-modtime . ignore)
198 (shell-command . ignore)
199 (substitute-in-file-name . tramp-smb-handle-substitute-in-file-name)
200 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
201 (vc-registered . ignore)
202 (verify-visited-file-modtime . ignore)
203 (write-region . tramp-smb-handle-write-region)
205 "Alist of handler functions for Tramp SMB method.
206 Operations not mentioned here will be handled by the default Emacs primitives.")
208 ;;;###tramp-autoload
209 (defsubst tramp-smb-file-name-p (filename)
210 "Check if it's a filename for SMB servers."
211 (let ((v (tramp-dissect-file-name filename)))
212 (string= (tramp-file-name-method v) tramp-smb-method)))
214 ;;;###tramp-autoload
215 (defun tramp-smb-file-name-handler (operation &rest args)
216 "Invoke the SMB related OPERATION.
217 First arg specifies the OPERATION, second arg is a list of arguments to
218 pass to the OPERATION."
219 (let ((fn (assoc operation tramp-smb-file-name-handler-alist)))
220 (if fn
221 (save-match-data (apply (cdr fn) args))
222 (tramp-run-real-handler operation args))))
224 ;;;###tramp-autoload
225 (unless (memq system-type '(cygwin windows-nt))
226 (add-to-list 'tramp-foreign-file-name-handler-alist
227 (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)))
230 ;; File name primitives.
232 (defun tramp-smb-handle-add-name-to-file
233 (filename newname &optional ok-if-already-exists)
234 "Like `add-name-to-file' for Tramp files."
235 (unless (tramp-equal-remote filename newname)
236 (with-parsed-tramp-file-name
237 (if (tramp-tramp-file-p filename) filename newname) nil
238 (tramp-error
239 v 'file-error
240 "add-name-to-file: %s"
241 "only implemented for same method, same user, same host")))
242 (with-parsed-tramp-file-name filename v1
243 (with-parsed-tramp-file-name newname v2
244 (when (file-directory-p filename)
245 (tramp-error
246 v2 'file-error
247 "add-name-to-file: %s must not be a directory" filename))
248 (when (and (not ok-if-already-exists)
249 (file-exists-p newname)
250 (not (numberp ok-if-already-exists))
251 (y-or-n-p
252 (format
253 "File %s already exists; make it a new name anyway? "
254 newname)))
255 (tramp-error
256 v2 'file-error
257 "add-name-to-file: file %s already exists" newname))
258 ;; We must also flush the cache of the directory, because
259 ;; `file-attributes' reads the values from there.
260 (tramp-flush-file-property v2 (file-name-directory v2-localname))
261 (tramp-flush-file-property v2 v2-localname)
262 (unless
263 (tramp-smb-send-command
265 (format
266 "%s \"%s\" \"%s\""
267 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
268 (tramp-smb-get-localname v1)
269 (tramp-smb-get-localname v2)))
270 (tramp-error
271 v2 'file-error
272 "error with add-name-to-file, see buffer `%s' for details"
273 (buffer-name))))))
275 (defun tramp-smb-handle-copy-directory
276 (dirname newname &optional keep-date parents)
277 "Like `copy-directory' for Tramp files. KEEP-DATE is not handled."
278 (setq dirname (expand-file-name dirname)
279 newname (expand-file-name newname))
280 (let ((t1 (tramp-tramp-file-p dirname))
281 (t2 (tramp-tramp-file-p newname)))
282 (with-parsed-tramp-file-name (if t1 dirname newname) nil
283 (cond
284 ;; We must use a local temporary directory.
285 ((and t1 t2)
286 (let ((tmpdir
287 (make-temp-name
288 (expand-file-name
289 tramp-temp-name-prefix
290 (tramp-compat-temporary-file-directory)))))
291 (unwind-protect
292 (progn
293 (tramp-compat-copy-directory dirname tmpdir keep-date parents)
294 (tramp-compat-copy-directory tmpdir newname keep-date parents))
295 (tramp-compat-delete-directory tmpdir 'recursive))))
297 ;; We can copy recursively.
298 ((or t1 t2)
299 (let ((prompt (tramp-smb-send-command v "prompt"))
300 (recurse (tramp-smb-send-command v "recurse")))
301 (unless (file-directory-p newname)
302 (make-directory newname parents))
303 (unwind-protect
304 (unless
305 (and
306 prompt recurse
307 (tramp-smb-send-command
308 v (format "cd \"%s\"" (tramp-smb-get-localname v)))
309 (tramp-smb-send-command
310 v (format "lcd \"%s\"" (if t1 newname dirname)))
311 (if t1
312 (tramp-smb-send-command v "mget *")
313 (tramp-smb-send-command v "mput *")))
314 ;; Error.
315 (with-current-buffer (tramp-get-connection-buffer v)
316 (goto-char (point-min))
317 (search-forward-regexp tramp-smb-errors nil t)
318 (tramp-error
319 v 'file-error
320 "%s `%s'" (match-string 0) (if t1 dirname newname))))
321 ;; Go home.
322 (tramp-smb-send-command
323 v (format
324 "cd %s" (if (tramp-smb-get-cifs-capabilities v) "/" "\\")))
325 ;; Toggle prompt and recurse OFF.
326 (if prompt (tramp-smb-send-command v "prompt"))
327 (if recurse (tramp-smb-send-command v "recurse")))))
329 ;; We must do it file-wise.
331 (tramp-run-real-handler
332 'copy-directory (list dirname newname keep-date parents)))))))
334 (defun tramp-smb-handle-copy-file
335 (filename newname &optional ok-if-already-exists keep-date
336 preserve-uid-gid preserve-selinux-context)
337 "Like `copy-file' for Tramp files.
338 KEEP-DATE is not handled in case NEWNAME resides on an SMB server.
339 PRESERVE-UID-GID is completely ignored."
340 (setq filename (expand-file-name filename)
341 newname (expand-file-name newname))
342 (with-progress-reporter
343 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
344 0 (format "Copying %s to %s" filename newname)
346 (let ((tmpfile (file-local-copy filename)))
348 (if tmpfile
349 ;; Remote filename.
350 (condition-case err
351 (rename-file tmpfile newname ok-if-already-exists)
352 ((error quit)
353 (delete-file tmpfile)
354 (signal (car err) (cdr err))))
356 ;; Remote newname.
357 (when (file-directory-p newname)
358 (setq newname
359 (expand-file-name (file-name-nondirectory filename) newname)))
361 (with-parsed-tramp-file-name newname nil
362 (when (and (not ok-if-already-exists)
363 (file-exists-p newname))
364 (tramp-error v 'file-already-exists newname))
366 ;; We must also flush the cache of the directory, because
367 ;; `file-attributes' reads the values from there.
368 (tramp-flush-file-property v (file-name-directory localname))
369 (tramp-flush-file-property v localname)
370 (unless (tramp-smb-get-share v)
371 (tramp-error
372 v 'file-error "Target `%s' must contain a share name" newname))
373 (unless (tramp-smb-send-command
374 v (format "put \"%s\" \"%s\""
375 filename (tramp-smb-get-localname v)))
376 (tramp-error v 'file-error "Cannot copy `%s'" filename))))))
378 ;; KEEP-DATE handling.
379 (when keep-date (set-file-times newname (nth 5 (file-attributes filename)))))
381 (defun tramp-smb-handle-delete-directory (directory &optional recursive)
382 "Like `delete-directory' for Tramp files."
383 (setq directory (directory-file-name (expand-file-name directory)))
384 (when (file-exists-p directory)
385 (if recursive
386 (mapc
387 (lambda (file)
388 (if (file-directory-p file)
389 (tramp-compat-delete-directory file recursive)
390 (delete-file file)))
391 ;; We do not want to delete "." and "..".
392 (directory-files
393 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
395 (with-parsed-tramp-file-name directory nil
396 ;; We must also flush the cache of the directory, because
397 ;; `file-attributes' reads the values from there.
398 (tramp-flush-file-property v (file-name-directory localname))
399 (tramp-flush-directory-property v localname)
400 (unless (tramp-smb-send-command
401 v (format
402 "%s \"%s\""
403 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
404 (tramp-smb-get-localname v)))
405 ;; Error.
406 (with-current-buffer (tramp-get-connection-buffer v)
407 (goto-char (point-min))
408 (search-forward-regexp tramp-smb-errors nil t)
409 (tramp-error
410 v 'file-error "%s `%s'" (match-string 0) directory))))))
412 (defun tramp-smb-handle-delete-file (filename &optional trash)
413 "Like `delete-file' for Tramp files."
414 (setq filename (expand-file-name filename))
415 (when (file-exists-p filename)
416 (with-parsed-tramp-file-name filename nil
417 ;; We must also flush the cache of the directory, because
418 ;; `file-attributes' reads the values from there.
419 (tramp-flush-file-property v (file-name-directory localname))
420 (tramp-flush-file-property v localname)
421 (unless (tramp-smb-send-command
422 v (format
423 "%s \"%s\""
424 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
425 (tramp-smb-get-localname v)))
426 ;; Error.
427 (with-current-buffer (tramp-get-connection-buffer v)
428 (goto-char (point-min))
429 (search-forward-regexp tramp-smb-errors nil t)
430 (tramp-error
431 v 'file-error "%s `%s'" (match-string 0) filename))))))
433 (defun tramp-smb-handle-directory-files
434 (directory &optional full match nosort)
435 "Like `directory-files' for Tramp files."
436 (let ((result (mapcar 'directory-file-name
437 (file-name-all-completions "" directory))))
438 ;; Discriminate with regexp.
439 (when match
440 (setq result
441 (delete nil
442 (mapcar (lambda (x) (when (string-match match x) x))
443 result))))
444 ;; Append directory.
445 (when full
446 (setq result
447 (mapcar
448 (lambda (x) (expand-file-name x directory))
449 result)))
450 ;; Sort them if necessary.
451 (unless nosort (setq result (sort result 'string-lessp)))
452 ;; That's it.
453 result))
455 (defun tramp-smb-handle-expand-file-name (name &optional dir)
456 "Like `expand-file-name' for Tramp files."
457 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
458 (setq dir (or dir default-directory "/"))
459 ;; Unless NAME is absolute, concat DIR and NAME.
460 (unless (file-name-absolute-p name)
461 (setq name (concat (file-name-as-directory dir) name)))
462 ;; If NAME is not a Tramp file, run the real handler.
463 (if (not (tramp-tramp-file-p name))
464 (tramp-run-real-handler 'expand-file-name (list name nil))
465 ;; Dissect NAME.
466 (with-parsed-tramp-file-name name nil
467 ;; Tilde expansion if necessary. We use the user name as share,
468 ;; which is offen the case in domains.
469 (when (string-match "\\`/?~\\([^/]*\\)" localname)
470 (setq localname
471 (replace-match
472 (if (zerop (length (match-string 1 localname)))
473 (tramp-file-name-real-user v)
474 (match-string 1 localname))
475 nil nil localname)))
476 ;; Make the file name absolute.
477 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
478 (setq localname (concat "/" localname)))
479 ;; No tilde characters in file name, do normal
480 ;; `expand-file-name' (this does "/./" and "/../").
481 (tramp-make-tramp-file-name
482 method user host
483 (tramp-run-real-handler 'expand-file-name (list localname))))))
485 (defun tramp-smb-handle-file-attributes (filename &optional id-format)
486 "Like `file-attributes' for Tramp files."
487 (unless id-format (setq id-format 'integer))
488 (with-parsed-tramp-file-name filename nil
489 (with-file-property v localname (format "file-attributes-%s" id-format)
490 (if (and (tramp-smb-get-share v) (tramp-smb-get-stat-capability v))
491 (tramp-smb-do-file-attributes-with-stat v id-format)
492 ;; Reading just the filename entry via "dir localname" is not
493 ;; possible, because when filename is a directory, some
494 ;; smbclient versions return the content of the directory, and
495 ;; other versions don't. Therefore, the whole content of the
496 ;; upper directory is retrieved, and the entry of the filename
497 ;; is extracted from.
498 (let* ((entries (tramp-smb-get-file-entries
499 (file-name-directory filename)))
500 (entry (assoc (file-name-nondirectory filename) entries))
501 (uid (if (equal id-format 'string) "nobody" -1))
502 (gid (if (equal id-format 'string) "nogroup" -1))
503 (inode (tramp-get-inode v))
504 (device (tramp-get-device v)))
506 ;; Check result.
507 (when entry
508 (list (and (string-match "d" (nth 1 entry))
509 t) ;0 file type
510 -1 ;1 link count
511 uid ;2 uid
512 gid ;3 gid
513 '(0 0) ;4 atime
514 (nth 3 entry) ;5 mtime
515 '(0 0) ;6 ctime
516 (nth 2 entry) ;7 size
517 (nth 1 entry) ;8 mode
518 nil ;9 gid weird
519 inode ;10 inode number
520 device))))))) ;11 file system number
522 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
523 "Implement `file-attributes' for Tramp files using stat command."
524 (tramp-message
525 vec 5 "file attributes with stat: %s" (tramp-file-name-localname vec))
526 (with-current-buffer (tramp-get-buffer vec)
527 (let* (size id link uid gid atime mtime ctime mode inode)
528 (when (tramp-smb-send-command
529 vec (format "stat \"%s\"" (tramp-smb-get-localname vec)))
531 ;; Loop the listing.
532 (goto-char (point-min))
533 (unless (re-search-forward tramp-smb-errors nil t)
534 (while (not (eobp))
535 (cond
536 ((looking-at
537 "Size:\\s-+\\([0-9]+\\)\\s-+Blocks:\\s-+[0-9]+\\s-+\\(\\w+\\)")
538 (setq size (string-to-number (match-string 1))
539 id (if (string-equal "directory" (match-string 2)) t
540 (if (string-equal "symbolic" (match-string 2)) ""))))
541 ((looking-at
542 "Inode:\\s-+\\([0-9]+\\)\\s-+Links:\\s-+\\([0-9]+\\)")
543 (setq inode (string-to-number (match-string 1))
544 link (string-to-number (match-string 2))))
545 ((looking-at
546 "Access:\\s-+([0-9]+/\\(\\S-+\\))\\s-+Uid:\\s-+\\([0-9]+\\)\\s-+Gid:\\s-+\\([0-9]+\\)")
547 (setq mode (match-string 1)
548 uid (if (equal id-format 'string) (match-string 2)
549 (string-to-number (match-string 2)))
550 gid (if (equal id-format 'string) (match-string 3)
551 (string-to-number (match-string 3)))))
552 ((looking-at
553 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
554 (setq atime
555 (encode-time
556 (string-to-number (match-string 6)) ;; sec
557 (string-to-number (match-string 5)) ;; min
558 (string-to-number (match-string 4)) ;; hour
559 (string-to-number (match-string 3)) ;; day
560 (string-to-number (match-string 2)) ;; month
561 (string-to-number (match-string 1))))) ;; year
562 ((looking-at
563 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
564 (setq mtime
565 (encode-time
566 (string-to-number (match-string 6)) ;; sec
567 (string-to-number (match-string 5)) ;; min
568 (string-to-number (match-string 4)) ;; hour
569 (string-to-number (match-string 3)) ;; day
570 (string-to-number (match-string 2)) ;; month
571 (string-to-number (match-string 1))))) ;; year
572 ((looking-at
573 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
574 (setq ctime
575 (encode-time
576 (string-to-number (match-string 6)) ;; sec
577 (string-to-number (match-string 5)) ;; min
578 (string-to-number (match-string 4)) ;; hour
579 (string-to-number (match-string 3)) ;; day
580 (string-to-number (match-string 2)) ;; month
581 (string-to-number (match-string 1)))))) ;; year
582 (forward-line))
583 ;; Return the result.
584 (list id link uid gid atime mtime ctime size mode nil inode
585 (tramp-get-device vec)))))))
587 (defun tramp-smb-handle-file-directory-p (filename)
588 "Like `file-directory-p' for Tramp files."
589 (and (file-exists-p filename)
590 (eq ?d (aref (nth 8 (file-attributes filename)) 0))))
592 (defun tramp-smb-handle-file-local-copy (filename)
593 "Like `file-local-copy' for Tramp files."
594 (with-parsed-tramp-file-name filename nil
595 (unless (file-exists-p filename)
596 (tramp-error
597 v 'file-error
598 "Cannot make local copy of non-existing file `%s'" filename))
599 (let ((tmpfile (tramp-compat-make-temp-file filename)))
600 (with-progress-reporter
601 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
602 (unless (tramp-smb-send-command
603 v (format "get \"%s\" \"%s\""
604 (tramp-smb-get-localname v) tmpfile))
605 ;; Oops, an error. We shall cleanup.
606 (delete-file tmpfile)
607 (tramp-error
608 v 'file-error "Cannot make local copy of file `%s'" filename)))
609 tmpfile)))
611 ;; This function should return "foo/" for directories and "bar" for
612 ;; files.
613 (defun tramp-smb-handle-file-name-all-completions (filename directory)
614 "Like `file-name-all-completions' for Tramp files."
615 (all-completions
616 filename
617 (with-parsed-tramp-file-name directory nil
618 (with-file-property v localname "file-name-all-completions"
619 (save-match-data
620 (let ((entries (tramp-smb-get-file-entries directory)))
621 (mapcar
622 (lambda (x)
623 (list
624 (if (string-match "d" (nth 1 x))
625 (file-name-as-directory (nth 0 x))
626 (nth 0 x))))
627 entries)))))))
629 (defun tramp-smb-handle-file-writable-p (filename)
630 "Like `file-writable-p' for Tramp files."
631 (if (file-exists-p filename)
632 (string-match "w" (or (nth 8 (file-attributes filename)) ""))
633 (let ((dir (file-name-directory filename)))
634 (and (file-exists-p dir)
635 (file-writable-p dir)))))
637 (defun tramp-smb-handle-insert-directory
638 (filename switches &optional wildcard full-directory-p)
639 "Like `insert-directory' for Tramp files."
640 (setq filename (expand-file-name filename))
641 (if full-directory-p
642 ;; Called from `dired-add-entry'.
643 (setq filename (file-name-as-directory filename))
644 (setq filename (directory-file-name filename)))
645 (with-parsed-tramp-file-name filename nil
646 (save-match-data
647 (let ((base (file-name-nondirectory filename))
648 ;; We should not destroy the cache entry.
649 (entries (copy-sequence
650 (tramp-smb-get-file-entries
651 (file-name-directory filename)))))
653 (when wildcard
654 (string-match "\\." base)
655 (setq base (replace-match "\\\\." nil nil base))
656 (string-match "\\*" base)
657 (setq base (replace-match ".*" nil nil base))
658 (string-match "\\?" base)
659 (setq base (replace-match ".?" nil nil base)))
661 ;; Filter entries.
662 (setq entries
663 (delq
665 (if (or wildcard (zerop (length base)))
666 ;; Check for matching entries.
667 (mapcar
668 (lambda (x)
669 (when (string-match
670 (format "^%s" base) (nth 0 x))
672 entries)
673 ;; We just need the only and only entry FILENAME.
674 (list (assoc base entries)))))
676 ;; Sort entries.
677 (setq entries
678 (sort
679 entries
680 (lambda (x y)
681 (if (string-match "t" switches)
682 ;; Sort by date.
683 (tramp-time-less-p (nth 3 y) (nth 3 x))
684 ;; Sort by name.
685 (string-lessp (nth 0 x) (nth 0 y))))))
687 ;; Handle "-F" switch.
688 (when (string-match "F" switches)
689 (mapc
690 (lambda (x)
691 (when (not (zerop (length (car x))))
692 (cond
693 ((char-equal ?d (string-to-char (nth 1 x)))
694 (setcar x (concat (car x) "/")))
695 ((char-equal ?x (string-to-char (nth 1 x)))
696 (setcar x (concat (car x) "*"))))))
697 entries))
699 ;; Print entries.
700 (mapc
701 (lambda (x)
702 (when (not (zerop (length (nth 0 x))))
703 (let ((attr
704 (when (tramp-smb-get-stat-capability v)
705 (ignore-errors
706 (file-attributes filename 'string)))))
707 (insert
708 (format
709 "%10s %3d %-8s %-8s %8s %s "
710 (or (nth 8 attr) (nth 1 x)) ; mode
711 (or (nth 1 attr) 1) ; inode
712 (or (nth 2 attr) "nobody") ; uid
713 (or (nth 3 attr) "nogroup") ; gid
714 (or (nth 7 attr) (nth 2 x)) ; size
715 (format-time-string
716 (if (tramp-time-less-p
717 (tramp-time-subtract (current-time) (nth 3 x))
718 tramp-half-a-year)
719 "%b %e %R"
720 "%b %e %Y")
721 (nth 3 x)))) ; date
722 ;; We mark the file name. The inserted name could be
723 ;; from somewhere else, so we use the relative file
724 ;; name of `default-directory'.
725 (let ((start (point)))
726 (insert
727 (format
728 "%s\n"
729 (file-relative-name
730 (expand-file-name
731 (nth 0 x) (file-name-directory filename)))))
732 (put-text-property start (1- (point)) 'dired-filename t))
733 (forward-line)
734 (beginning-of-line))))
735 entries)))))
737 (defun tramp-smb-handle-make-directory (dir &optional parents)
738 "Like `make-directory' for Tramp files."
739 (setq dir (directory-file-name (expand-file-name dir)))
740 (unless (file-name-absolute-p dir)
741 (setq dir (expand-file-name dir default-directory)))
742 (with-parsed-tramp-file-name dir nil
743 (save-match-data
744 (let* ((ldir (file-name-directory dir)))
745 ;; Make missing directory parts.
746 (when (and parents
747 (tramp-smb-get-share v)
748 (not (file-directory-p ldir)))
749 (make-directory ldir parents))
750 ;; Just do it.
751 (when (file-directory-p ldir)
752 (make-directory-internal dir))
753 (unless (file-directory-p dir)
754 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
756 (defun tramp-smb-handle-make-directory-internal (directory)
757 "Like `make-directory-internal' for Tramp files."
758 (setq directory (directory-file-name (expand-file-name directory)))
759 (unless (file-name-absolute-p directory)
760 (setq directory (expand-file-name directory default-directory)))
761 (with-parsed-tramp-file-name directory nil
762 (save-match-data
763 (let* ((file (tramp-smb-get-localname v)))
764 (when (file-directory-p (file-name-directory directory))
765 (tramp-smb-send-command
767 (if (tramp-smb-get-cifs-capabilities v)
768 (format
769 "posix_mkdir \"%s\" %s"
770 file (tramp-compat-decimal-to-octal (default-file-modes)))
771 (format "mkdir \"%s\"" file)))
772 ;; We must also flush the cache of the directory, because
773 ;; `file-attributes' reads the values from there.
774 (tramp-flush-file-property v (file-name-directory localname))
775 (tramp-flush-file-property v localname))
776 (unless (file-directory-p directory)
777 (tramp-error
778 v 'file-error "Couldn't make directory %s" directory))))))
780 (defun tramp-smb-handle-make-symbolic-link
781 (filename linkname &optional ok-if-already-exists)
782 "Like `make-symbolic-link' for Tramp files.
783 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
784 the symlink. If LINKNAME is a Tramp file, only the localname component is
785 used as the target of the symlink.
787 If LINKNAME is a Tramp file and the localname component is relative, then
788 it is expanded first, before the localname component is taken. Note that
789 this can give surprising results if the user/host for the source and
790 target of the symlink differ."
791 (unless (tramp-equal-remote filename linkname)
792 (with-parsed-tramp-file-name
793 (if (tramp-tramp-file-p filename) filename linkname) nil
794 (tramp-error
795 v 'file-error
796 "make-symbolic-link: %s"
797 "only implemented for same method, same user, same host")))
798 (with-parsed-tramp-file-name filename v1
799 (with-parsed-tramp-file-name linkname v2
800 (when (file-directory-p filename)
801 (tramp-error
802 v2 'file-error
803 "make-symbolic-link: %s must not be a directory" filename))
804 (when (and (not ok-if-already-exists)
805 (file-exists-p linkname)
806 (not (numberp ok-if-already-exists))
807 (y-or-n-p
808 (format
809 "File %s already exists; make it a new name anyway? "
810 linkname)))
811 (tramp-error
812 v2 'file-error
813 "make-symbolic-link: file %s already exists" linkname))
814 (unless (tramp-smb-get-cifs-capabilities v1)
815 (tramp-error v2 'file-error "make-symbolic-link not supported"))
816 ;; We must also flush the cache of the directory, because
817 ;; `file-attributes' reads the values from there.
818 (tramp-flush-file-property v2 (file-name-directory v2-localname))
819 (tramp-flush-file-property v2 v2-localname)
820 (unless
821 (tramp-smb-send-command
823 (format
824 "symlink \"%s\" \"%s\""
825 (tramp-smb-get-localname v1)
826 (tramp-smb-get-localname v2)))
827 (tramp-error
828 v2 'file-error
829 "error with make-symbolic-link, see buffer `%s' for details"
830 (buffer-name))))))
832 (defun tramp-smb-handle-rename-file
833 (filename newname &optional ok-if-already-exists)
834 "Like `rename-file' for Tramp files."
835 (setq filename (expand-file-name filename)
836 newname (expand-file-name newname))
837 (with-progress-reporter
838 (tramp-dissect-file-name (if (file-remote-p filename) filename newname))
839 0 (format "Renaming %s to %s" filename newname)
841 (let ((tmpfile (file-local-copy filename)))
843 (if tmpfile
844 ;; Remote filename.
845 (condition-case err
846 (rename-file tmpfile newname ok-if-already-exists)
847 ((error quit)
848 (delete-file tmpfile)
849 (signal (car err) (cdr err))))
851 ;; Remote newname.
852 (when (file-directory-p newname)
853 (setq newname (expand-file-name
854 (file-name-nondirectory filename) newname)))
856 (with-parsed-tramp-file-name newname nil
857 (when (and (not ok-if-already-exists)
858 (file-exists-p newname))
859 (tramp-error v 'file-already-exists newname))
860 ;; We must also flush the cache of the directory, because
861 ;; `file-attributes' reads the values from there.
862 (tramp-flush-file-property v (file-name-directory localname))
863 (tramp-flush-file-property v localname)
864 (unless (tramp-smb-send-command
865 v (format "put %s \"%s\""
866 filename (tramp-smb-get-localname v)))
867 (tramp-error v 'file-error "Cannot rename `%s'" filename)))))
869 (delete-file filename)))
871 (defun tramp-smb-handle-set-file-modes (filename mode)
872 "Like `set-file-modes' for Tramp files."
873 (with-parsed-tramp-file-name filename nil
874 (when (tramp-smb-get-cifs-capabilities v)
875 (tramp-flush-file-property v localname)
876 (unless (tramp-smb-send-command
877 v (format "chmod \"%s\" %s"
878 (tramp-smb-get-localname v)
879 (tramp-compat-decimal-to-octal mode)))
880 (tramp-error
881 v 'file-error "Error while changing file's mode %s" filename)))))
883 (defun tramp-smb-handle-substitute-in-file-name (filename)
884 "Like `handle-substitute-in-file-name' for Tramp files.
885 \"//\" substitutes only in the local filename part. Catches
886 errors for shares like \"C$/\", which are common in Microsoft Windows."
887 (with-parsed-tramp-file-name filename nil
888 ;; Ignore in LOCALNAME everything before "//".
889 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
890 (setq filename
891 (concat (file-remote-p filename)
892 (replace-match "\\1" nil nil localname)))))
893 (condition-case nil
894 (tramp-run-real-handler 'substitute-in-file-name (list filename))
895 (error filename)))
897 (defun tramp-smb-handle-write-region
898 (start end filename &optional append visit lockname confirm)
899 "Like `write-region' for Tramp files."
900 (setq filename (expand-file-name filename))
901 (with-parsed-tramp-file-name filename nil
902 (unless (eq append nil)
903 (tramp-error
904 v 'file-error "Cannot append to file using Tramp (`%s')" filename))
905 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
906 (when (and (not (featurep 'xemacs))
907 confirm (file-exists-p filename))
908 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
909 filename))
910 (tramp-error v 'file-error "File not overwritten")))
911 ;; We must also flush the cache of the directory, because
912 ;; `file-attributes' reads the values from there.
913 (tramp-flush-file-property v (file-name-directory localname))
914 (tramp-flush-file-property v localname)
915 (let ((curbuf (current-buffer))
916 (tmpfile (tramp-compat-make-temp-file filename)))
917 ;; We say `no-message' here because we don't want the visited file
918 ;; modtime data to be clobbered from the temp file. We call
919 ;; `set-visited-file-modtime' ourselves later on.
920 (tramp-run-real-handler
921 'write-region
922 (if confirm ; don't pass this arg unless defined for backward compat.
923 (list start end tmpfile append 'no-message lockname confirm)
924 (list start end tmpfile append 'no-message lockname)))
926 (with-progress-reporter
927 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
928 (unwind-protect
929 (unless (tramp-smb-send-command
930 v (format "put %s \"%s\""
931 tmpfile (tramp-smb-get-localname v)))
932 (tramp-error v 'file-error "Cannot write `%s'" filename))
933 (delete-file tmpfile)))
935 (unless (equal curbuf (current-buffer))
936 (tramp-error
937 v 'file-error
938 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
939 (when (eq visit t)
940 (set-visited-file-modtime)))))
943 ;; Internal file name functions.
945 (defun tramp-smb-get-share (vec)
946 "Returns the share name of LOCALNAME."
947 (save-match-data
948 (let ((localname (tramp-file-name-localname vec)))
949 (when (string-match "^/?\\([^/]+\\)/" localname)
950 (match-string 1 localname)))))
952 (defun tramp-smb-get-localname (vec)
953 "Returns the file name of LOCALNAME.
954 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
955 (save-match-data
956 (let ((localname (tramp-file-name-localname vec)))
957 (setq
958 localname
959 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
960 ;; There is a share, sparated by "/".
961 (if (not (tramp-smb-get-cifs-capabilities vec))
962 (mapconcat
963 (lambda (x) (if (equal x ?/) "\\" (char-to-string x)))
964 (match-string 1 localname) "")
965 (match-string 1 localname))
966 ;; There is just a share.
967 (if (string-match "^/?\\([^/]+\\)$" localname)
968 (match-string 1 localname)
969 "")))
971 ;; Sometimes we have discarded `substitute-in-file-name'.
972 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
973 (setq localname (replace-match "$" nil nil localname 1)))
975 localname)))
977 ;; Share names of a host are cached. It is very unlikely that the
978 ;; shares do change during connection.
979 (defun tramp-smb-get-file-entries (directory)
980 "Read entries which match DIRECTORY.
981 Either the shares are listed, or the `dir' command is executed.
982 Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)."
983 (with-parsed-tramp-file-name (file-name-as-directory directory) nil
984 (setq localname (or localname "/"))
985 (with-file-property v localname "file-entries"
986 (with-current-buffer (tramp-get-buffer v)
987 (let* ((share (tramp-smb-get-share v))
988 (cache (tramp-get-connection-property v "share-cache" nil))
989 res entry)
991 (if (and (not share) cache)
992 ;; Return cached shares.
993 (setq res cache)
995 ;; Read entries.
996 (if share
997 (tramp-smb-send-command
998 v (format "dir \"%s*\"" (tramp-smb-get-localname v)))
999 ;; `tramp-smb-maybe-open-connection' lists also the share names.
1000 (tramp-smb-maybe-open-connection v))
1002 ;; Loop the listing.
1003 (goto-char (point-min))
1004 (if (re-search-forward tramp-smb-errors nil t)
1005 (tramp-error v 'file-error "%s `%s'" (match-string 0) directory)
1006 (while (not (eobp))
1007 (setq entry (tramp-smb-read-file-entry share))
1008 (forward-line)
1009 (when entry (add-to-list 'res entry))))
1011 ;; Cache share entries.
1012 (unless share
1013 (tramp-set-connection-property v "share-cache" res)))
1015 ;; Add directory itself.
1016 (add-to-list 'res '("" "drwxrwxrwx" 0 (0 0)))
1018 ;; There's a very strange error (debugged with XEmacs 21.4.14)
1019 ;; If there's no short delay, it returns nil. No idea about.
1020 (when (featurep 'xemacs) (sleep-for 0.01))
1022 ;; Return entries.
1023 (delq nil res))))))
1025 ;; Return either a share name (if SHARE is nil), or a file name.
1027 ;; If shares are listed, the following format is expected:
1029 ;; Disk| - leading spaces
1030 ;; [^|]+| - share name, 14 char
1031 ;; .* - comment
1033 ;; Entries provided by smbclient DIR aren't fully regular.
1034 ;; They should have the format
1036 ;; \s-\{2,2} - leading spaces
1037 ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound
1038 ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound
1039 ;; \s- - space delimeter
1040 ;; \s-+[0-9]+ - size, 8 chars, right bound
1041 ;; \s-\{2,2\} - space delimeter
1042 ;; \w\{3,3\} - weekday
1043 ;; \s- - space delimeter
1044 ;; \w\{3,3\} - month
1045 ;; \s- - space delimeter
1046 ;; [ 12][0-9] - day
1047 ;; \s- - space delimeter
1048 ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time
1049 ;; \s- - space delimeter
1050 ;; [0-9]\{4,4\} - year
1052 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1053 ;; has function display_finfo:
1055 ;; d_printf(" %-30s%7.7s %8.0f %s",
1056 ;; finfo->name,
1057 ;; attrib_string(finfo->mode),
1058 ;; (double)finfo->size,
1059 ;; asctime(LocalTime(&t)));
1061 ;; in Samba 1.9, there's the following code:
1063 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1064 ;; CNV_LANG(finfo->name),
1065 ;; attrib_string(finfo->mode),
1066 ;; finfo->size,
1067 ;; asctime(LocalTime(&t))));
1069 ;; Problems:
1070 ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't
1071 ;; available in older Emacsen.
1072 ;; * The length of constructs (file name, size) might exceed the default.
1073 ;; * File names might contain spaces.
1074 ;; * Permissions might be empty.
1076 ;; So we try to analyze backwards.
1077 (defun tramp-smb-read-file-entry (share)
1078 "Parse entry in SMB output buffer.
1079 If SHARE is result, entries are of type dir. Otherwise, shares are listed.
1080 Result is the list (LOCALNAME MODE SIZE MTIME)."
1081 ;; We are called from `tramp-smb-get-file-entries', which sets the
1082 ;; current buffer.
1083 (let ((line (buffer-substring (point) (point-at-eol)))
1084 localname mode size month day hour min sec year mtime)
1086 (if (not share)
1088 ;; Read share entries.
1089 (when (string-match "^Disk|\\([^|]+\\)|" line)
1090 (setq localname (match-string 1 line)
1091 mode "dr-xr-xr-x"
1092 size 0))
1094 ;; Real listing.
1095 (block nil
1097 ;; year.
1098 (if (string-match "\\([0-9]+\\)$" line)
1099 (setq year (string-to-number (match-string 1 line))
1100 line (substring line 0 -5))
1101 (return))
1103 ;; time.
1104 (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line)
1105 (setq hour (string-to-number (match-string 1 line))
1106 min (string-to-number (match-string 2 line))
1107 sec (string-to-number (match-string 3 line))
1108 line (substring line 0 -9))
1109 (return))
1111 ;; day.
1112 (if (string-match "\\([0-9]+\\)$" line)
1113 (setq day (string-to-number (match-string 1 line))
1114 line (substring line 0 -3))
1115 (return))
1117 ;; month.
1118 (if (string-match "\\(\\w+\\)$" line)
1119 (setq month (match-string 1 line)
1120 line (substring line 0 -4))
1121 (return))
1123 ;; weekday.
1124 (if (string-match "\\(\\w+\\)$" line)
1125 (setq line (substring line 0 -5))
1126 (return))
1128 ;; size.
1129 (if (string-match "\\([0-9]+\\)$" line)
1130 (let ((length (- (max 10 (1+ (length (match-string 1 line)))))))
1131 (setq size (string-to-number (match-string 1 line)))
1132 (when (string-match "\\([ADHRSV]+\\)" (substring line length))
1133 (setq length (+ length (match-end 0))))
1134 (setq line (substring line 0 length)))
1135 (return))
1137 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1138 (if (string-match "\\([ADHRSV]+\\)?$" line)
1139 (setq
1140 mode (or (match-string 1 line) "")
1141 mode (save-match-data (format
1142 "%s%s"
1143 (if (string-match "D" mode) "d" "-")
1144 (mapconcat
1145 (lambda (x) "") " "
1146 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1147 line (substring line 0 -6))
1148 (return))
1150 ;; localname.
1151 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1152 (setq localname (match-string 1 line))
1153 (return))))
1155 (when (and localname mode size)
1156 (setq mtime
1157 (if (and sec min hour day month year)
1158 (encode-time
1159 sec min hour day
1160 (cdr (assoc (downcase month) tramp-parse-time-months))
1161 year)
1162 '(0 0)))
1163 (list localname mode size mtime))))
1165 (defun tramp-smb-get-cifs-capabilities (vec)
1166 "Check, whether the SMB server supports POSIX commands."
1167 ;; When we are not logged in yet, we return nil.
1168 (if (let ((p (tramp-get-connection-process vec)))
1169 (and p (processp p) (memq (process-status p) '(run open))))
1170 (with-connection-property
1171 (tramp-get-connection-process vec) "cifs-capabilities"
1172 (save-match-data
1173 (when (tramp-smb-send-command vec "posix")
1174 (with-current-buffer (tramp-get-buffer vec)
1175 (goto-char (point-min))
1176 (when
1177 (re-search-forward "Server supports CIFS capabilities" nil t)
1178 (member
1179 "pathnames"
1180 (split-string
1181 (buffer-substring (point) (point-at-eol)) nil t)))))))))
1183 (defun tramp-smb-get-stat-capability (vec)
1184 "Check, whether the SMB server supports the STAT command."
1185 ;; When we are not logged in yet, we return nil.
1186 (if (let ((p (tramp-get-connection-process vec)))
1187 (and p (processp p) (memq (process-status p) '(run open))))
1188 (with-connection-property
1189 (tramp-get-connection-process vec) "stat-capability"
1190 (tramp-smb-send-command vec "stat ."))))
1193 ;; Connection functions.
1195 (defun tramp-smb-send-command (vec command)
1196 "Send the COMMAND to connection VEC.
1197 Returns nil if there has been an error message from smbclient."
1198 (tramp-smb-maybe-open-connection vec)
1199 (tramp-message vec 6 "%s" command)
1200 (tramp-send-string vec command)
1201 (tramp-smb-wait-for-output vec))
1203 (defun tramp-smb-maybe-open-connection (vec)
1204 "Maybe open a connection to HOST, log in as USER, using `tramp-smb-program'.
1205 Does not do anything if a connection is already open, but re-opens the
1206 connection if a previous connection has died for some reason."
1207 (let* ((share (tramp-smb-get-share vec))
1208 (buf (tramp-get-buffer vec))
1209 (p (get-buffer-process buf)))
1211 ;; Check whether we still have the same smbclient version.
1212 ;; Otherwise, we must delete the connection cache, because
1213 ;; capabilities migh have changed.
1214 (unless (processp p)
1215 (let ((default-directory (tramp-compat-temporary-file-directory))
1216 (command (concat tramp-smb-program " -V")))
1218 (unless tramp-smb-version
1219 (unless (executable-find tramp-smb-program)
1220 (tramp-error
1221 vec 'file-error
1222 "Cannot find command %s in %s" tramp-smb-program exec-path))
1223 (setq tramp-smb-version (shell-command-to-string command))
1224 (tramp-message vec 6 command)
1225 (tramp-message vec 6 "\n%s" tramp-smb-version)
1226 (if (string-match "[ \t\n\r]+\\'" tramp-smb-version)
1227 (setq tramp-smb-version
1228 (replace-match "" nil nil tramp-smb-version))))
1230 (unless (string-equal
1231 tramp-smb-version
1232 (tramp-get-connection-property
1233 vec "smbclient-version" tramp-smb-version))
1234 (tramp-flush-directory-property vec "")
1235 (tramp-flush-connection-property vec))
1237 (tramp-set-connection-property
1238 vec "smbclient-version" tramp-smb-version)))
1240 ;; If too much time has passed since last command was sent, look
1241 ;; whether there has been an error message; maybe due to
1242 ;; connection timeout.
1243 (with-current-buffer buf
1244 (goto-char (point-min))
1245 (when (and (> (tramp-time-diff
1246 (current-time)
1247 (tramp-get-connection-property
1248 p "last-cmd-time" '(0 0 0)))
1250 p (processp p) (memq (process-status p) '(run open))
1251 (re-search-forward tramp-smb-errors nil t))
1252 (delete-process p)
1253 (setq p nil)))
1255 ;; Check whether it is still the same share.
1256 (unless
1257 (and p (processp p) (memq (process-status p) '(run open))
1258 (string-equal
1259 share
1260 (tramp-get-connection-property p "smb-share" "")))
1262 (save-match-data
1263 ;; There might be unread output from checking for share names.
1264 (when buf (with-current-buffer buf (erase-buffer)))
1265 (when (and p (processp p)) (delete-process p))
1267 (let* ((user (tramp-file-name-user vec))
1268 (host (tramp-file-name-host vec))
1269 (real-user (tramp-file-name-real-user vec))
1270 (real-host (tramp-file-name-real-host vec))
1271 (domain (tramp-file-name-domain vec))
1272 (port (tramp-file-name-port vec))
1273 args)
1275 (if share
1276 (setq args (list (concat "//" real-host "/" share)))
1277 (setq args (list "-g" "-L" real-host )))
1279 (if (not (zerop (length real-user)))
1280 (setq args (append args (list "-U" real-user)))
1281 (setq args (append args (list "-N"))))
1283 (when domain (setq args (append args (list "-W" domain))))
1284 (when port (setq args (append args (list "-p" port))))
1285 (when tramp-smb-conf
1286 (setq args (append args (list "-s" tramp-smb-conf))))
1288 ;; OK, let's go.
1289 (with-progress-reporter
1290 vec 3
1291 (format "Opening connection for //%s%s/%s"
1292 (if (not (zerop (length user))) (concat user "@") "")
1293 host (or share ""))
1295 (let* ((coding-system-for-read nil)
1296 (process-connection-type tramp-process-connection-type)
1297 (p (let ((default-directory
1298 (tramp-compat-temporary-file-directory)))
1299 (apply #'start-process
1300 (tramp-buffer-name vec) (tramp-get-buffer vec)
1301 tramp-smb-program args))))
1303 (tramp-message
1304 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1305 (tramp-compat-set-process-query-on-exit-flag p nil)
1307 ;; Set variables for computing the prompt for reading password.
1308 (setq tramp-current-method tramp-smb-method
1309 tramp-current-user user
1310 tramp-current-host host)
1312 ;; Play login scenario.
1313 (tramp-process-actions
1314 p vec
1315 (if share
1316 tramp-smb-actions-with-share
1317 tramp-smb-actions-without-share))
1319 ;; Check server version.
1320 (with-current-buffer (tramp-get-connection-buffer vec)
1321 (goto-char (point-min))
1322 (search-forward-regexp
1323 "Domain=\\[[^]]*\\] OS=\\[[^]]*\\] Server=\\[[^]]*\\]" nil t)
1324 (let ((smbserver-version (match-string 0)))
1325 (unless
1326 (string-equal
1327 smbserver-version
1328 (tramp-get-connection-property
1329 vec "smbserver-version" smbserver-version))
1330 (tramp-flush-directory-property vec "")
1331 (tramp-flush-connection-property vec))
1332 (tramp-set-connection-property
1333 vec "smbserver-version" smbserver-version)))
1335 ;; Set chunksize. Otherwise, `tramp-send-string' might
1336 ;; try it itself.
1337 (tramp-set-connection-property p "smb-share" share)
1338 (tramp-set-connection-property
1339 p "chunksize" tramp-chunksize))))))))
1341 ;; We don't use timeouts. If needed, the caller shall wrap around.
1342 (defun tramp-smb-wait-for-output (vec)
1343 "Wait for output from smbclient command.
1344 Returns nil if an error message has appeared."
1345 (with-current-buffer (tramp-get-buffer vec)
1346 (let ((p (get-buffer-process (current-buffer)))
1347 (found (progn (goto-char (point-min))
1348 (re-search-forward tramp-smb-prompt nil t)))
1349 (err (progn (goto-char (point-min))
1350 (re-search-forward tramp-smb-errors nil t))))
1352 ;; Algorithm: get waiting output. See if last line contains
1353 ;; tramp-smb-prompt sentinel or tramp-smb-errors strings.
1354 ;; If not, wait a bit and again get waiting output.
1355 (while (and (not found) (not err))
1357 ;; Accept pending output.
1358 (tramp-accept-process-output p)
1360 ;; Search for prompt.
1361 (goto-char (point-min))
1362 (setq found (re-search-forward tramp-smb-prompt nil t))
1364 ;; Search for errors.
1365 (goto-char (point-min))
1366 (setq err (re-search-forward tramp-smb-errors nil t)))
1368 ;; When the process is still alive, read pending output.
1369 (while (and (not found) (memq (process-status p) '(run open)))
1371 ;; Accept pending output.
1372 (tramp-accept-process-output p)
1374 ;; Search for prompt.
1375 (goto-char (point-min))
1376 (setq found (re-search-forward tramp-smb-prompt nil t)))
1378 ;; Return value is whether no error message has appeared.
1379 (tramp-message vec 6 "\n%s" (buffer-string))
1380 (not err))))
1382 (add-hook 'tramp-unload-hook
1383 (lambda ()
1384 (unload-feature 'tramp-smb 'force)))
1386 (provide 'tramp-smb)
1388 ;;; TODO:
1390 ;; * Error handling in case password is wrong.
1391 ;; * Read password from "~/.netrc".
1392 ;; * Return more comprehensive file permission string.
1393 ;; * Try to remove the inclusion of dummy "" directory. Seems to be at
1394 ;; several places, especially in `tramp-smb-handle-insert-directory'.
1395 ;; * (RMS) Use unwind-protect to clean up the state so as to make the state
1396 ;; regular again.
1397 ;; * Make it multi-hop capable.
1399 ;;; tramp-smb.el ends here