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