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