]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-smb.el
Speed up keyboard auto-repeat cursor motion under bidi redisplay.
[gnu-emacs] / lisp / net / tramp-smb.el
1 ;;; tramp-smb.el --- Tramp access functions for SMB servers
2
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp.
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl)) ; block, return
31 (require 'tramp)
32
33 ;; Define SMB method ...
34 ;;;###tramp-autoload
35 (defconst tramp-smb-method "smb"
36 "*Method to connect SAMBA and M$ SMB servers.")
37
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)))
42
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))
48
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))
54
55 ;; Add completion function for SMB method.
56 ;;;###tramp-autoload
57 (eval-after-load 'tramp
58 '(tramp-set-completion-function
59 tramp-smb-method
60 '((tramp-parse-netrc "~/.netrc"))))
61
62 (defcustom tramp-smb-program "smbclient"
63 "*Name of SMB client to run."
64 :group 'tramp
65 :type 'string)
66
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."
71 :group 'tramp
72 :type '(choice (const nil) (file :must-match t)))
73
74 (defvar tramp-smb-version nil
75 "*Version string of the SMB client.")
76
77 (defconst tramp-smb-prompt "^smb: .+> \\|^\\s-+Server\\s-+Comment$"
78 "Regexp used as prompt in smbclient.")
79
80 (defconst tramp-smb-errors
81 (mapconcat
82 'identity
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"
89 ,(regexp-opt
90 '(;; Samba.
91 "ERRDOS"
92 "ERRHRD"
93 "ERRSRV"
94 "ERRbadfile"
95 "ERRbadpw"
96 "ERRfilexists"
97 "ERRnoaccess"
98 "ERRnomem"
99 "ERRnosuchshare"
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")))
123 "\\|")
124 "Regexp for possible error strings of SMB servers.
125 Used instead of analyzing error codes of commands.")
126
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.
135
136 See `tramp-actions-before-shell' for more info.")
137
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.
145
146 See `tramp-actions-before-shell' for more info.")
147
148 ;; New handlers should be added here.
149 (defconst tramp-smb-file-name-handler-alist
150 '(
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)
209 )
210 "Alist of handler functions for Tramp SMB method.
211 Operations not mentioned here will be handled by the default Emacs primitives.")
212
213 ;;;###tramp-autoload
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)))
218
219 ;;;###tramp-autoload
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)))
225 (if fn
226 (save-match-data (apply (cdr fn) args))
227 (tramp-run-real-handler operation args))))
228
229 ;;;###tramp-autoload
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)))
233
234
235 ;; File name primitives.
236
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
243 (tramp-error
244 v 'file-error
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)
250 (tramp-error
251 v2 'file-error
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))
256 (y-or-n-p
257 (format
258 "File %s already exists; make it a new name anyway? "
259 newname)))
260 (tramp-error
261 v2 'file-error
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)
267 (unless
268 (tramp-smb-send-command
269 v1
270 (format
271 "%s \"%s\" \"%s\""
272 (if (tramp-smb-get-cifs-capabilities v1) "link" "hardlink")
273 (tramp-smb-get-localname v1)
274 (tramp-smb-get-localname v2)))
275 (tramp-error
276 v2 'file-error
277 "error with add-name-to-file, see buffer `%s' for details"
278 (buffer-name))))))
279
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
288 (cond
289 ;; We must use a local temporary directory.
290 ((and t1 t2)
291 (let ((tmpdir
292 (make-temp-name
293 (expand-file-name
294 tramp-temp-name-prefix
295 (tramp-compat-temporary-file-directory)))))
296 (unwind-protect
297 (progn
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))))
301
302 ;; We can copy recursively.
303 ((or t1 t2)
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))
308 (unwind-protect
309 (unless
310 (and
311 prompt recurse
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)))
316 (if t1
317 (tramp-smb-send-command v "mget *")
318 (tramp-smb-send-command v "mput *")))
319 ;; Error.
320 (with-current-buffer (tramp-get-connection-buffer v)
321 (goto-char (point-min))
322 (search-forward-regexp tramp-smb-errors nil t)
323 (tramp-error
324 v 'file-error
325 "%s `%s'" (match-string 0) (if t1 dirname newname))))
326 ;; Go home.
327 (tramp-smb-send-command
328 v (format
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")))))
333
334 ;; We must do it file-wise.
335 (t
336 (tramp-run-real-handler
337 'copy-directory (list dirname newname keep-date parents)))))))
338
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)
350
351 (let ((tmpfile (file-local-copy filename)))
352
353 (if tmpfile
354 ;; Remote filename.
355 (condition-case err
356 (rename-file tmpfile newname ok-if-already-exists)
357 ((error quit)
358 (delete-file tmpfile)
359 (signal (car err) (cdr err))))
360
361 ;; Remote newname.
362 (when (file-directory-p newname)
363 (setq newname
364 (expand-file-name (file-name-nondirectory filename) newname)))
365
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))
370
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)
376 (tramp-error
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))))))
382
383 ;; KEEP-DATE handling.
384 (when keep-date (set-file-times newname (nth 5 (file-attributes filename)))))
385
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)
390 (if recursive
391 (mapc
392 (lambda (file)
393 (if (file-directory-p file)
394 (tramp-compat-delete-directory file recursive)
395 (delete-file file)))
396 ;; We do not want to delete "." and "..".
397 (directory-files
398 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
399
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
406 v (format
407 "%s \"%s\""
408 (if (tramp-smb-get-cifs-capabilities v) "posix_rmdir" "rmdir")
409 (tramp-smb-get-localname v)))
410 ;; Error.
411 (with-current-buffer (tramp-get-connection-buffer v)
412 (goto-char (point-min))
413 (search-forward-regexp tramp-smb-errors nil t)
414 (tramp-error
415 v 'file-error "%s `%s'" (match-string 0) directory))))))
416
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
427 v (format
428 "%s \"%s\""
429 (if (tramp-smb-get-cifs-capabilities v) "posix_unlink" "rm")
430 (tramp-smb-get-localname v)))
431 ;; Error.
432 (with-current-buffer (tramp-get-connection-buffer v)
433 (goto-char (point-min))
434 (search-forward-regexp tramp-smb-errors nil t)
435 (tramp-error
436 v 'file-error "%s `%s'" (match-string 0) filename))))))
437
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.
444 (when match
445 (setq result
446 (delete nil
447 (mapcar (lambda (x) (when (string-match match x) x))
448 result))))
449 ;; Append directory.
450 (when full
451 (setq result
452 (mapcar
453 (lambda (x) (expand-file-name x directory))
454 result)))
455 ;; Sort them if necessary.
456 (unless nosort (setq result (sort result 'string-lessp)))
457 ;; That's it.
458 result))
459
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))
470 ;; Dissect NAME.
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)
475 (setq localname
476 (replace-match
477 (if (zerop (length (match-string 1 localname)))
478 (tramp-file-name-real-user v)
479 (match-string 1 localname))
480 nil nil 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
487 method user host
488 (tramp-run-real-handler 'expand-file-name (list localname))))))
489
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)))
510
511 ;; Check result.
512 (when entry
513 (list (and (string-match "d" (nth 1 entry))
514 t) ;0 file type
515 -1 ;1 link count
516 uid ;2 uid
517 gid ;3 gid
518 '(0 0) ;4 atime
519 (nth 3 entry) ;5 mtime
520 '(0 0) ;6 ctime
521 (nth 2 entry) ;7 size
522 (nth 1 entry) ;8 mode
523 nil ;9 gid weird
524 inode ;10 inode number
525 device))))))) ;11 file system number
526
527 (defun tramp-smb-do-file-attributes-with-stat (vec &optional id-format)
528 "Implement `file-attributes' for Tramp files using stat command."
529 (tramp-message
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)))
535
536 ;; Loop the listing.
537 (goto-char (point-min))
538 (unless (re-search-forward tramp-smb-errors nil t)
539 (while (not (eobp))
540 (cond
541 ((looking-at
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)) ""))))
546 ((looking-at
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))))
550 ((looking-at
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)))))
557 ((looking-at
558 "Access:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
559 (setq atime
560 (encode-time
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
567 ((looking-at
568 "Modify:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
569 (setq mtime
570 (encode-time
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
577 ((looking-at
578 "Change:\\s-+\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)\\s-+\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)")
579 (setq ctime
580 (encode-time
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
587 (forward-line))
588 ;; Return the result.
589 (list id link uid gid atime mtime ctime size mode nil inode
590 (tramp-get-device vec)))))))
591
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))))
596
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)
601 (tramp-error
602 v 'file-error
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)
612 (tramp-error
613 v 'file-error "Cannot make local copy of file `%s'" filename)))
614 tmpfile)))
615
616 ;; This function should return "foo/" for directories and "bar" for
617 ;; files.
618 (defun tramp-smb-handle-file-name-all-completions (filename directory)
619 "Like `file-name-all-completions' for Tramp files."
620 (all-completions
621 filename
622 (with-parsed-tramp-file-name directory nil
623 (with-file-property v localname "file-name-all-completions"
624 (save-match-data
625 (let ((entries (tramp-smb-get-file-entries directory)))
626 (mapcar
627 (lambda (x)
628 (list
629 (if (string-match "d" (nth 1 x))
630 (file-name-as-directory (nth 0 x))
631 (nth 0 x))))
632 entries)))))))
633
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)))))
641
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))
646 (if full-directory-p
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
651 (save-match-data
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)))))
657
658 (when wildcard
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)))
665
666 ;; Filter entries.
667 (setq entries
668 (delq
669 nil
670 (if (or wildcard (zerop (length base)))
671 ;; Check for matching entries.
672 (mapcar
673 (lambda (x)
674 (when (string-match
675 (format "^%s" base) (nth 0 x))
676 x))
677 entries)
678 ;; We just need the only and only entry FILENAME.
679 (list (assoc base entries)))))
680
681 ;; Sort entries.
682 (setq entries
683 (sort
684 entries
685 (lambda (x y)
686 (if (string-match "t" switches)
687 ;; Sort by date.
688 (tramp-time-less-p (nth 3 y) (nth 3 x))
689 ;; Sort by name.
690 (string-lessp (nth 0 x) (nth 0 y))))))
691
692 ;; Handle "-F" switch.
693 (when (string-match "F" switches)
694 (mapc
695 (lambda (x)
696 (when (not (zerop (length (car x))))
697 (cond
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) "*"))))))
702 entries))
703
704 ;; Print entries.
705 (mapc
706 (lambda (x)
707 (when (not (zerop (length (nth 0 x))))
708 (let ((attr
709 (when (tramp-smb-get-stat-capability v)
710 (ignore-errors
711 (file-attributes filename 'string)))))
712 (insert
713 (format
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
720 (format-time-string
721 (if (tramp-time-less-p
722 (tramp-time-subtract (current-time) (nth 3 x))
723 tramp-half-a-year)
724 "%b %e %R"
725 "%b %e %Y")
726 (nth 3 x)))) ; date
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)))
731 (insert
732 (format
733 "%s\n"
734 (file-relative-name
735 (expand-file-name
736 (nth 0 x) (file-name-directory filename)))))
737 (put-text-property start (1- (point)) 'dired-filename t))
738 (forward-line)
739 (beginning-of-line))))
740 entries)))))
741
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
748 (save-match-data
749 (let* ((ldir (file-name-directory dir)))
750 ;; Make missing directory parts.
751 (when (and parents
752 (tramp-smb-get-share v)
753 (not (file-directory-p ldir)))
754 (make-directory ldir parents))
755 ;; Just do it.
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))))))
760
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
767 (save-match-data
768 (let* ((file (tramp-smb-get-localname v)))
769 (when (file-directory-p (file-name-directory directory))
770 (tramp-smb-send-command
771 v
772 (if (tramp-smb-get-cifs-capabilities v)
773 (format
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)
782 (tramp-error
783 v 'file-error "Couldn't make directory %s" directory))))))
784
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.
791
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
799 (tramp-error
800 v 'file-error
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)
806 (tramp-error
807 v2 'file-error
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))
812 (y-or-n-p
813 (format
814 "File %s already exists; make it a new name anyway? "
815 linkname)))
816 (tramp-error
817 v2 'file-error
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)
825 (unless
826 (tramp-smb-send-command
827 v1
828 (format
829 "symlink \"%s\" \"%s\""
830 (tramp-smb-get-localname v1)
831 (tramp-smb-get-localname v2)))
832 (tramp-error
833 v2 'file-error
834 "error with make-symbolic-link, see buffer `%s' for details"
835 (buffer-name))))))
836
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)
845
846 (let ((tmpfile (file-local-copy filename)))
847
848 (if tmpfile
849 ;; Remote filename.
850 (condition-case err
851 (rename-file tmpfile newname ok-if-already-exists)
852 ((error quit)
853 (delete-file tmpfile)
854 (signal (car err) (cdr err))))
855
856 ;; Remote newname.
857 (when (file-directory-p newname)
858 (setq newname (expand-file-name
859 (file-name-nondirectory filename) newname)))
860
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)))))
873
874 (delete-file filename)))
875
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)))
885 (tramp-error
886 v 'file-error "Error while changing file's mode %s" filename)))))
887
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))
895 (setq filename
896 (concat (file-remote-p filename)
897 (replace-match "\\1" nil nil localname)))))
898 (condition-case nil
899 (tramp-run-real-handler 'substitute-in-file-name (list filename))
900 (error filename)))
901
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)
908 (tramp-error
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? "
914 filename))
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
926 'write-region
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)))
930
931 (tramp-with-progress-reporter
932 v 3 (format "Moving tmp file %s to %s" tmpfile filename)
933 (unwind-protect
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)))
939
940 (unless (equal curbuf (current-buffer))
941 (tramp-error
942 v 'file-error
943 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))
944 (when (eq visit t)
945 (set-visited-file-modtime)))))
946
947
948 ;; Internal file name functions.
949
950 (defun tramp-smb-get-share (vec)
951 "Returns the share name of LOCALNAME."
952 (save-match-data
953 (let ((localname (tramp-file-name-localname vec)))
954 (when (string-match "^/?\\([^/]+\\)/" localname)
955 (match-string 1 localname)))))
956
957 (defun tramp-smb-get-localname (vec)
958 "Returns the file name of LOCALNAME.
959 If VEC has no cifs capabilities, exchange \"/\" by \"\\\\\"."
960 (save-match-data
961 (let ((localname (tramp-file-name-localname vec)))
962 (setq
963 localname
964 (if (string-match "^/?[^/]+\\(/.*\\)" localname)
965 ;; There is a share, sparated by "/".
966 (if (not (tramp-smb-get-cifs-capabilities vec))
967 (mapconcat
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)
974 "")))
975
976 ;; Sometimes we have discarded `substitute-in-file-name'.
977 (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" localname)
978 (setq localname (replace-match "$" nil nil localname 1)))
979
980 localname)))
981
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))
994 res entry)
995
996 (if (and (not share) cache)
997 ;; Return cached shares.
998 (setq res cache)
999
1000 ;; Read entries.
1001 (if share
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))
1006
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)
1011 (while (not (eobp))
1012 (setq entry (tramp-smb-read-file-entry share))
1013 (forward-line)
1014 (when entry (add-to-list 'res entry))))
1015
1016 ;; Cache share entries.
1017 (unless share
1018 (tramp-set-connection-property v "share-cache" res)))
1019
1020 ;; Add directory itself.
1021 (add-to-list 'res '("" "drwxrwxrwx" 0 (0 0)))
1022
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))
1026
1027 ;; Return entries.
1028 (delq nil res))))))
1029
1030 ;; Return either a share name (if SHARE is nil), or a file name.
1031 ;;
1032 ;; If shares are listed, the following format is expected:
1033 ;;
1034 ;; Disk| - leading spaces
1035 ;; [^|]+| - share name, 14 char
1036 ;; .* - comment
1037 ;;
1038 ;; Entries provided by smbclient DIR aren't fully regular.
1039 ;; They should have the format
1040 ;;
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
1051 ;; [ 12][0-9] - day
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
1056 ;;
1057 ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html)
1058 ;; has function display_finfo:
1059 ;;
1060 ;; d_printf(" %-30s%7.7s %8.0f %s",
1061 ;; finfo->name,
1062 ;; attrib_string(finfo->mode),
1063 ;; (double)finfo->size,
1064 ;; asctime(LocalTime(&t)));
1065 ;;
1066 ;; in Samba 1.9, there's the following code:
1067 ;;
1068 ;; DEBUG(0,(" %-30s%7.7s%10d %s",
1069 ;; CNV_LANG(finfo->name),
1070 ;; attrib_string(finfo->mode),
1071 ;; finfo->size,
1072 ;; asctime(LocalTime(&t))));
1073 ;;
1074 ;; Problems:
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.
1080 ;;
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
1087 ;; current buffer.
1088 (let ((line (buffer-substring (point) (point-at-eol)))
1089 localname mode size month day hour min sec year mtime)
1090
1091 (if (not share)
1092
1093 ;; Read share entries.
1094 (when (string-match "^Disk|\\([^|]+\\)|" line)
1095 (setq localname (match-string 1 line)
1096 mode "dr-xr-xr-x"
1097 size 0))
1098
1099 ;; Real listing.
1100 (block nil
1101
1102 ;; year.
1103 (if (string-match "\\([0-9]+\\)$" line)
1104 (setq year (string-to-number (match-string 1 line))
1105 line (substring line 0 -5))
1106 (return))
1107
1108 ;; time.
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))
1114 (return))
1115
1116 ;; day.
1117 (if (string-match "\\([0-9]+\\)$" line)
1118 (setq day (string-to-number (match-string 1 line))
1119 line (substring line 0 -3))
1120 (return))
1121
1122 ;; month.
1123 (if (string-match "\\(\\w+\\)$" line)
1124 (setq month (match-string 1 line)
1125 line (substring line 0 -4))
1126 (return))
1127
1128 ;; weekday.
1129 (if (string-match "\\(\\w+\\)$" line)
1130 (setq line (substring line 0 -5))
1131 (return))
1132
1133 ;; size.
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)))
1140 (return))
1141
1142 ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID.
1143 (if (string-match "\\([ADHRSV]+\\)?$" line)
1144 (setq
1145 mode (or (match-string 1 line) "")
1146 mode (save-match-data (format
1147 "%s%s"
1148 (if (string-match "D" mode) "d" "-")
1149 (mapconcat
1150 (lambda (x) "") " "
1151 (concat "r" (if (string-match "R" mode) "-" "w") "x"))))
1152 line (substring line 0 -6))
1153 (return))
1154
1155 ;; localname.
1156 (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line)
1157 (setq localname (match-string 1 line))
1158 (return))))
1159
1160 (when (and localname mode size)
1161 (setq mtime
1162 (if (and sec min hour day month year)
1163 (encode-time
1164 sec min hour day
1165 (cdr (assoc (downcase month) tramp-parse-time-months))
1166 year)
1167 '(0 0)))
1168 (list localname mode size mtime))))
1169
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"
1177 (save-match-data
1178 (when (tramp-smb-send-command vec "posix")
1179 (with-current-buffer (tramp-get-buffer vec)
1180 (goto-char (point-min))
1181 (when
1182 (re-search-forward "Server supports CIFS capabilities" nil t)
1183 (member
1184 "pathnames"
1185 (split-string
1186 (buffer-substring (point) (point-at-eol)) nil t)))))))))
1187
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 ."))))
1196
1197
1198 ;; Connection functions.
1199
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))
1207
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)))
1215
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")))
1222
1223 (unless tramp-smb-version
1224 (unless (executable-find tramp-smb-program)
1225 (tramp-error
1226 vec 'file-error
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))))
1234
1235 (unless (string-equal
1236 tramp-smb-version
1237 (tramp-get-connection-property
1238 vec "smbclient-version" tramp-smb-version))
1239 (tramp-flush-directory-property vec "")
1240 (tramp-flush-connection-property vec))
1241
1242 (tramp-set-connection-property
1243 vec "smbclient-version" tramp-smb-version)))
1244
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
1251 (current-time)
1252 (tramp-get-connection-property
1253 p "last-cmd-time" '(0 0 0)))
1254 60)
1255 p (processp p) (memq (process-status p) '(run open))
1256 (re-search-forward tramp-smb-errors nil t))
1257 (delete-process p)
1258 (setq p nil)))
1259
1260 ;; Check whether it is still the same share.
1261 (unless
1262 (and p (processp p) (memq (process-status p) '(run open))
1263 (string-equal
1264 share
1265 (tramp-get-connection-property p "smb-share" "")))
1266
1267 (save-match-data
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))
1271
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))
1278 args)
1279
1280 (if share
1281 (setq args (list (concat "//" real-host "/" share)))
1282 (setq args (list "-g" "-L" real-host )))
1283
1284 (if (not (zerop (length real-user)))
1285 (setq args (append args (list "-U" real-user)))
1286 (setq args (append args (list "-N"))))
1287
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))))
1292
1293 ;; OK, let's go.
1294 (tramp-with-progress-reporter
1295 vec 3
1296 (format "Opening connection for //%s%s/%s"
1297 (if (not (zerop (length user))) (concat user "@") "")
1298 host (or share ""))
1299
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))))
1307
1308 (tramp-message
1309 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1310 (tramp-compat-set-process-query-on-exit-flag p nil)
1311
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)
1316
1317 ;; Play login scenario.
1318 (tramp-process-actions
1319 p vec nil
1320 (if share
1321 tramp-smb-actions-with-share
1322 tramp-smb-actions-without-share))
1323
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)))
1330 (unless
1331 (string-equal
1332 smbserver-version
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)))
1339
1340 ;; Set chunksize. Otherwise, `tramp-send-string' might
1341 ;; try it itself.
1342 (tramp-set-connection-property p "smb-share" share)
1343 (tramp-set-connection-property
1344 p "chunksize" tramp-chunksize))))))))
1345
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))))
1356
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))
1361
1362 ;; Accept pending output.
1363 (tramp-accept-process-output p)
1364
1365 ;; Search for prompt.
1366 (goto-char (point-min))
1367 (setq found (re-search-forward tramp-smb-prompt nil t))
1368
1369 ;; Search for errors.
1370 (goto-char (point-min))
1371 (setq err (re-search-forward tramp-smb-errors nil t)))
1372
1373 ;; When the process is still alive, read pending output.
1374 (while (and (not found) (memq (process-status p) '(run open)))
1375
1376 ;; Accept pending output.
1377 (tramp-accept-process-output p)
1378
1379 ;; Search for prompt.
1380 (goto-char (point-min))
1381 (setq found (re-search-forward tramp-smb-prompt nil t)))
1382
1383 ;; Return value is whether no error message has appeared.
1384 (tramp-message vec 6 "\n%s" (buffer-string))
1385 (not err))))
1386
1387 (add-hook 'tramp-unload-hook
1388 (lambda ()
1389 (unload-feature 'tramp-smb 'force)))
1390
1391 (provide 'tramp-smb)
1392
1393 ;;; TODO:
1394
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
1401 ;; regular again.
1402 ;; * Make it multi-hop capable.
1403
1404 ;;; tramp-smb.el ends here