Commit | Line | Data |
---|---|---|
4007ba5b KG |
1 | ;;; tramp-smb.el --- Tramp access functions for SMB servers -*- coding: iso-8859-1; -*- |
2 | ||
8daea7fc | 3 | ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
4007ba5b KG |
4 | |
5 | ;; Author: Michael Albinus <Michael.Albinus@alcatel.de> | |
6 | ;; Keywords: comm, processes | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Access functions for SMB servers like SAMBA or M$ Windows from Tramp. | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (require 'tramp) | |
32 | ||
33 | ;; Pacify byte-compiler | |
34 | (eval-when-compile | |
35 | (require 'cl) | |
36 | (require 'custom) | |
37 | ;; Emacs 19.34 compatibility hack -- is this needed? | |
38 | (or (>= emacs-major-version 20) | |
39 | (load "cl-seq"))) | |
40 | ||
41 | ;; Define SMB method ... | |
42 | (defcustom tramp-smb-method "smb" | |
43 | "*Method to connect SAMBA and M$ SMB servers." | |
44 | :group 'tramp | |
45 | :type 'string) | |
46 | ||
47 | ;; ... and add it to the method list. | |
48 | (add-to-list 'tramp-methods (cons tramp-smb-method nil)) | |
49 | ||
50 | ;; Add a default for `tramp-default-method-alist'. Rule: If there is | |
51 | ;; a domain in USER, it must be the SMB method. | |
52 | (add-to-list 'tramp-default-method-alist | |
8daea7fc | 53 | (list "%" "" tramp-smb-method)) |
4007ba5b KG |
54 | |
55 | ;; Add completion function for SMB method. | |
56 | (tramp-set-completion-function | |
57 | tramp-smb-method | |
58 | '((tramp-parse-netrc "~/.netrc"))) | |
59 | ||
60 | (defcustom tramp-smb-program "smbclient" | |
61 | "*Name of SMB client to run." | |
62 | :group 'tramp | |
63 | :type 'string) | |
64 | ||
65 | (defconst tramp-smb-prompt "^smb: \\S-+> " | |
66 | "Regexp used as prompt in smbclient.") | |
67 | ||
68 | (defconst tramp-smb-errors | |
69 | (mapconcat | |
70 | 'identity | |
71 | '(; Connection error | |
72 | "Connection to \\S-+ failed" | |
73 | ; Samba | |
74 | "ERRSRV" | |
75 | "ERRDOS" | |
76 | "ERRbadfile" | |
77 | "ERRbadpw" | |
78 | "ERRfilexists" | |
79 | "ERRnoaccess" | |
80 | "ERRnomem" | |
81 | "ERRnosuchshare" | |
82 | ; Windows NT 4.0, Windows 5.0 (Windows 2000), Windows 5.1 (Windows XP) | |
83 | "NT_STATUS_ACCESS_DENIED" | |
84 | "NT_STATUS_BAD_NETWORK_NAME" | |
85 | "NT_STATUS_CANNOT_DELETE" | |
86 | "NT_STATUS_LOGON_FAILURE" | |
87 | "NT_STATUS_NO_SUCH_FILE" | |
88 | "NT_STATUS_OBJECT_NAME_INVALID" | |
89 | "NT_STATUS_OBJECT_NAME_NOT_FOUND" | |
90 | "NT_STATUS_SHARING_VIOLATION") | |
91 | "\\|") | |
92 | "Regexp for possible error strings of SMB servers. | |
93 | Used instead of analyzing error codes of commands.") | |
94 | ||
95 | (defvar tramp-smb-share nil | |
96 | "Holds the share name for the current buffer. | |
97 | This variable is local to each buffer.") | |
98 | (make-variable-buffer-local 'tramp-smb-share) | |
99 | ||
100 | (defvar tramp-smb-share-cache nil | |
101 | "Caches the share names accessible to host related to the current buffer. | |
102 | This variable is local to each buffer.") | |
103 | (make-variable-buffer-local 'tramp-smb-share-cache) | |
104 | ||
105 | (defvar tramp-smb-process-running nil | |
106 | "Flag whether a corresponding process is still running. | |
107 | Will be changed by corresponding `process-sentinel'. | |
108 | This variable is local to each buffer.") | |
109 | (make-variable-buffer-local 'tramp-smb-process-running) | |
110 | ||
8daea7fc KG |
111 | (defvar tramp-smb-inodes nil |
112 | "Keeps virtual inodes numbers for SMB files.") | |
113 | ||
4007ba5b KG |
114 | ;; New handlers should be added here. |
115 | (defconst tramp-smb-file-name-handler-alist | |
116 | '( | |
117 | ;; `access-file' performed by default handler | |
118 | (add-name-to-file . tramp-smb-handle-copy-file) ;; we're on Windows, honey. | |
119 | ;; `byte-compiler-base-file-name' performed by default handler | |
120 | (copy-file . tramp-smb-handle-copy-file) | |
121 | (delete-directory . tramp-smb-handle-delete-directory) | |
122 | (delete-file . tramp-smb-handle-delete-file) | |
123 | ;; `diff-latest-backup-file' performed by default handler | |
8daea7fc | 124 | (directory-file-name . tramp-handle-directory-file-name) |
4007ba5b KG |
125 | (directory-files . tramp-smb-handle-directory-files) |
126 | (directory-files-and-attributes . tramp-smb-handle-directory-files-and-attributes) | |
127 | (dired-call-process . tramp-smb-not-handled) | |
128 | (dired-compress-file . tramp-smb-not-handled) | |
129 | ;; `dired-uncache' performed by default handler | |
130 | ;; `expand-file-name' not necessary because we cannot expand "~/" | |
131 | (file-accessible-directory-p . tramp-smb-handle-file-directory-p) | |
132 | (file-attributes . tramp-smb-handle-file-attributes) | |
133 | (file-directory-p . tramp-smb-handle-file-directory-p) | |
134 | (file-executable-p . tramp-smb-handle-file-exists-p) | |
135 | (file-exists-p . tramp-smb-handle-file-exists-p) | |
136 | (file-local-copy . tramp-smb-handle-file-local-copy) | |
137 | (file-modes . tramp-handle-file-modes) | |
138 | (file-name-all-completions . tramp-smb-handle-file-name-all-completions) | |
139 | ;; `file-name-as-directory' performed by default handler | |
140 | (file-name-completion . tramp-handle-file-name-completion) | |
141 | (file-name-directory . tramp-handle-file-name-directory) | |
142 | (file-name-nondirectory . tramp-handle-file-name-nondirectory) | |
143 | ;; `file-name-sans-versions' performed by default handler | |
144 | (file-newer-than-file-p . tramp-smb-handle-file-newer-than-file-p) | |
145 | (file-ownership-preserved-p . tramp-smb-not-handled) | |
146 | (file-readable-p . tramp-smb-handle-file-exists-p) | |
147 | (file-regular-p . tramp-handle-file-regular-p) | |
148 | (file-symlink-p . tramp-smb-not-handled) | |
149 | ;; `file-truename' performed by default handler | |
150 | (file-writable-p . tramp-smb-handle-file-writable-p) | |
151 | ;; `find-backup-file-name' performed by default handler | |
152 | ;; `find-file-noselect' performed by default handler | |
153 | ;; `get-file-buffer' performed by default handler | |
154 | (insert-directory . tramp-smb-handle-insert-directory) | |
155 | (insert-file-contents . tramp-handle-insert-file-contents) | |
156 | (load . tramp-handle-load) | |
157 | (make-directory . tramp-smb-handle-make-directory) | |
158 | (make-directory-internal . tramp-smb-handle-make-directory-internal) | |
159 | (make-symbolic-link . tramp-smb-not-handled) | |
160 | (rename-file . tramp-smb-handle-rename-file) | |
161 | (set-file-modes . tramp-smb-not-handled) | |
162 | (set-visited-file-modtime . tramp-smb-not-handled) | |
163 | (shell-command . tramp-smb-not-handled) | |
164 | ;; `substitute-in-file-name' performed by default handler | |
165 | (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory) | |
166 | (vc-registered . tramp-smb-not-handled) | |
167 | (verify-visited-file-modtime . tramp-smb-not-handled) | |
168 | (write-region . tramp-smb-handle-write-region) | |
169 | ) | |
170 | "Alist of handler functions for Tramp SMB method. | |
171 | Operations not mentioned here will be handled by the default Emacs primitives.") | |
172 | ||
173 | (defun tramp-smb-file-name-p (filename) | |
174 | "Check if it's a filename for SMB servers." | |
175 | (let ((v (tramp-dissect-file-name filename))) | |
176 | (string= | |
177 | (tramp-find-method | |
178 | (tramp-file-name-multi-method v) | |
179 | (tramp-file-name-method v) | |
180 | (tramp-file-name-user v) | |
181 | (tramp-file-name-host v)) | |
182 | tramp-smb-method))) | |
183 | ||
184 | (defun tramp-smb-file-name-handler (operation &rest args) | |
185 | "Invoke the SMB related OPERATION. | |
186 | First arg specifies the OPERATION, second arg is a list of arguments to | |
187 | pass to the OPERATION." | |
188 | (let ((fn (assoc operation tramp-smb-file-name-handler-alist))) | |
189 | (if fn | |
190 | (if (eq (cdr fn) 'tramp-smb-not-handled) | |
191 | (apply (cdr fn) operation args) | |
192 | (save-match-data (apply (cdr fn) args))) | |
193 | (tramp-run-real-handler operation args)))) | |
194 | ||
195 | (add-to-list 'tramp-foreign-file-name-handler-alist | |
196 | (cons 'tramp-smb-file-name-p 'tramp-smb-file-name-handler)) | |
197 | ||
198 | ||
199 | ;; File name primitives | |
200 | ||
201 | (defun tramp-smb-not-handled (operation &rest args) | |
202 | "Default handler for all functions which are disrecarded." | |
203 | (tramp-message 10 "Won't be handled: %s %s" operation args) | |
204 | nil) | |
205 | ||
206 | (defun tramp-smb-handle-copy-file | |
207 | (filename newname &optional ok-if-already-exists keep-date) | |
208 | "Like `copy-file' for tramp files. | |
209 | KEEP-DATE is not handled in case NEWNAME resides on an SMB server." | |
210 | (setq filename (expand-file-name filename) | |
211 | newname (expand-file-name newname)) | |
212 | ||
213 | (let ((tmpfile (file-local-copy filename))) | |
214 | ||
215 | (if tmpfile | |
216 | ;; remote filename | |
217 | (rename-file tmpfile newname ok-if-already-exists) | |
218 | ||
219 | ;; remote newname | |
220 | (when (file-directory-p newname) | |
221 | (setq newname (expand-file-name | |
222 | (file-name-nondirectory filename) newname))) | |
223 | (when (and (not ok-if-already-exists) | |
224 | (file-exists-p newname)) | |
225 | (error "copy-file: file %s already exists" newname)) | |
226 | ||
227 | ; (with-parsed-tramp-file-name newname nil | |
7432277c | 228 | (let (user host localname) |
4007ba5b | 229 | (with-parsed-tramp-file-name newname l |
7432277c | 230 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 231 | (save-excursion |
7432277c KG |
232 | (let ((share (tramp-smb-get-share localname)) |
233 | (file (tramp-smb-get-localname localname t))) | |
4007ba5b KG |
234 | (unless share |
235 | (error "Target `%s' must contain a share name" filename)) | |
236 | (tramp-smb-maybe-open-connection user host share) | |
237 | (tramp-message-for-buffer | |
238 | nil tramp-smb-method user host | |
239 | 5 "Copying file %s to file %s..." filename newname) | |
240 | (if (tramp-smb-send-command | |
241 | user host (format "put %s \"%s\"" filename file)) | |
242 | (tramp-message-for-buffer | |
243 | nil tramp-smb-method user host | |
244 | 5 "Copying file %s to file %s...done" filename newname) | |
245 | (error "Cannot copy `%s'" filename)))))))) | |
246 | ||
247 | (defun tramp-smb-handle-delete-directory (directory) | |
248 | "Like `delete-directory' for tramp files." | |
249 | (setq directory (directory-file-name (expand-file-name directory))) | |
250 | (unless (file-exists-p directory) | |
251 | (error "Cannot delete non-existing directory `%s'" directory)) | |
252 | ; (with-parsed-tramp-file-name directory nil | |
7432277c | 253 | (let (user host localname) |
4007ba5b | 254 | (with-parsed-tramp-file-name directory l |
7432277c | 255 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 256 | (save-excursion |
7432277c KG |
257 | (let ((share (tramp-smb-get-share localname)) |
258 | (dir (tramp-smb-get-localname (file-name-directory localname) t)) | |
259 | (file (file-name-nondirectory localname))) | |
4007ba5b KG |
260 | (tramp-smb-maybe-open-connection user host share) |
261 | (if (and | |
262 | (tramp-smb-send-command user host (format "cd \"%s\"" dir)) | |
263 | (tramp-smb-send-command user host (format "rmdir \"%s\"" file))) | |
264 | ;; Go Home | |
265 | (tramp-smb-send-command user host (format "cd \\")) | |
266 | ;; Error | |
267 | (tramp-smb-send-command user host (format "cd \\")) | |
268 | (error "Cannot delete directory `%s'" directory)))))) | |
269 | ||
270 | (defun tramp-smb-handle-delete-file (filename) | |
271 | "Like `delete-file' for tramp files." | |
272 | (setq filename (expand-file-name filename)) | |
273 | (unless (file-exists-p filename) | |
274 | (error "Cannot delete non-existing file `%s'" filename)) | |
275 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 276 | (let (user host localname) |
4007ba5b | 277 | (with-parsed-tramp-file-name filename l |
7432277c | 278 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 279 | (save-excursion |
7432277c KG |
280 | (let ((share (tramp-smb-get-share localname)) |
281 | (dir (tramp-smb-get-localname (file-name-directory localname) t)) | |
282 | (file (file-name-nondirectory localname))) | |
4007ba5b KG |
283 | (unless (file-exists-p filename) |
284 | (error "Cannot delete non-existing file `%s'" filename)) | |
285 | (tramp-smb-maybe-open-connection user host share) | |
286 | (if (and | |
287 | (tramp-smb-send-command user host (format "cd \"%s\"" dir)) | |
288 | (tramp-smb-send-command user host (format "rm \"%s\"" file))) | |
289 | ;; Go Home | |
290 | (tramp-smb-send-command user host (format "cd \\")) | |
291 | ;; Error | |
292 | (tramp-smb-send-command user host (format "cd \\")) | |
293 | (error "Cannot delete file `%s'" directory)))))) | |
294 | ||
295 | (defun tramp-smb-handle-directory-files | |
296 | (directory &optional full match nosort) | |
297 | "Like `directory-files' for tramp files." | |
298 | (setq directory (directory-file-name (expand-file-name directory))) | |
299 | ; (with-parsed-tramp-file-name directory nil | |
7432277c | 300 | (let (user host localname) |
4007ba5b | 301 | (with-parsed-tramp-file-name directory l |
7432277c | 302 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 303 | (save-excursion |
7432277c KG |
304 | (let* ((share (tramp-smb-get-share localname)) |
305 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
306 | (entries (tramp-smb-get-file-entries user host share file))) |
307 | ;; Just the file names are needed | |
308 | (setq entries (mapcar 'car entries)) | |
309 | ;; Discriminate with regexp | |
310 | (when match | |
311 | (setq entries | |
312 | (delete nil | |
313 | (mapcar (lambda (x) (when (string-match match x) x)) | |
314 | entries)))) | |
7432277c | 315 | ;; Make absolute localnames if necessary |
4007ba5b KG |
316 | (when full |
317 | (setq entries | |
318 | (mapcar (lambda (x) | |
319 | (concat (file-name-as-directory directory) x)) | |
320 | entries))) | |
321 | ;; Sort them if necessary | |
322 | (unless nosort (setq entries (sort entries 'string-lessp))) | |
323 | ;; That's it | |
324 | entries)))) | |
325 | ||
326 | (defun tramp-smb-handle-directory-files-and-attributes | |
327 | (directory &optional full match nosort) | |
328 | "Like `directory-files-and-attributes' for tramp files." | |
329 | (mapcar | |
330 | (lambda (x) | |
331 | (cons x (file-attributes | |
332 | (if full x (concat (file-name-as-directory directory) x))))) | |
333 | (directory-files directory full match nosort))) | |
7432277c | 334 | |
4007ba5b KG |
335 | (defun tramp-smb-handle-file-attributes (filename &optional nonnumeric) |
336 | "Like `file-attributes' for tramp files. | |
337 | Optional argument NONNUMERIC means return user and group name | |
338 | rather than as numbers." | |
339 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 340 | (let (user host localname) |
4007ba5b | 341 | (with-parsed-tramp-file-name filename l |
7432277c | 342 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 343 | (save-excursion |
7432277c KG |
344 | (let* ((share (tramp-smb-get-share localname)) |
345 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
346 | (entries (tramp-smb-get-file-entries user host share file)) |
347 | (entry (and entries | |
8daea7fc KG |
348 | (assoc (file-name-nondirectory file) entries))) |
349 | (inode (tramp-smb-get-inode share file)) | |
350 | (device (tramp-get-device nil tramp-smb-method user host))) | |
351 | ||
4007ba5b KG |
352 | ; check result |
353 | (when entry | |
354 | (list (and (string-match "d" (nth 1 entry)) | |
355 | t) ;0 file type | |
356 | -1 ;1 link count | |
357 | -1 ;2 uid | |
358 | -1 ;3 gid | |
8daea7fc | 359 | '(0 0) ;4 atime |
4007ba5b | 360 | (nth 3 entry) ;5 mtime |
8daea7fc | 361 | '(0 0) ;6 ctime |
4007ba5b KG |
362 | (nth 2 entry) ;7 size |
363 | (nth 1 entry) ;8 mode | |
364 | nil ;9 gid weird | |
8daea7fc KG |
365 | inode ;10 inode number |
366 | device)))))) ;11 file system number | |
4007ba5b KG |
367 | |
368 | (defun tramp-smb-handle-file-directory-p (filename) | |
369 | "Like `file-directory-p' for tramp files." | |
370 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 371 | (let (user host localname) |
4007ba5b | 372 | (with-parsed-tramp-file-name filename l |
7432277c | 373 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 374 | (save-excursion |
7432277c KG |
375 | (let* ((share (tramp-smb-get-share localname)) |
376 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
377 | (entries (tramp-smb-get-file-entries user host share file)) |
378 | (entry (and entries | |
379 | (assoc (file-name-nondirectory file) entries)))) | |
380 | (and entry | |
381 | (string-match "d" (nth 1 entry)) | |
382 | t))))) | |
383 | ||
384 | (defun tramp-smb-handle-file-exists-p (filename) | |
385 | "Like `file-exists-p' for tramp files." | |
386 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 387 | (let (user host localname) |
4007ba5b | 388 | (with-parsed-tramp-file-name filename l |
7432277c | 389 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 390 | (save-excursion |
7432277c KG |
391 | (let* ((share (tramp-smb-get-share localname)) |
392 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
393 | (entries (tramp-smb-get-file-entries user host share file))) |
394 | (and entries | |
395 | (member (file-name-nondirectory file) (mapcar 'car entries)) | |
396 | t))))) | |
397 | ||
398 | (defun tramp-smb-handle-file-local-copy (filename) | |
399 | "Like `file-local-copy' for tramp files." | |
400 | (with-parsed-tramp-file-name filename nil | |
401 | (save-excursion | |
7432277c KG |
402 | (let ((share (tramp-smb-get-share localname)) |
403 | (file (tramp-smb-get-localname localname t)) | |
4007ba5b KG |
404 | (tmpfil (tramp-make-temp-file))) |
405 | (unless (file-exists-p filename) | |
406 | (error "Cannot make local copy of non-existing file `%s'" filename)) | |
407 | (tramp-message-for-buffer | |
408 | nil tramp-smb-method user host | |
409 | 5 "Fetching %s to tmp file %s..." filename tmpfil) | |
410 | (tramp-smb-maybe-open-connection user host share) | |
411 | (if (tramp-smb-send-command | |
412 | user host (format "get \"%s\" %s" file tmpfil)) | |
413 | (tramp-message-for-buffer | |
414 | nil tramp-smb-method user host | |
415 | 5 "Fetching %s to tmp file %s...done" filename tmpfil) | |
416 | (error "Cannot make local copy of file `%s'" filename)) | |
417 | tmpfil)))) | |
418 | ||
419 | ;; This function should return "foo/" for directories and "bar" for | |
420 | ;; files. | |
421 | (defun tramp-smb-handle-file-name-all-completions (filename directory) | |
422 | "Like `file-name-all-completions' for tramp files." | |
423 | ; (with-parsed-tramp-file-name directory nil | |
7432277c | 424 | (let (user host localname) |
4007ba5b | 425 | (with-parsed-tramp-file-name directory l |
7432277c | 426 | (setq user l-user host l-host localname l-localname)) |
4007ba5b KG |
427 | (save-match-data |
428 | (save-excursion | |
7432277c KG |
429 | (let* ((share (tramp-smb-get-share localname)) |
430 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
431 | (entries (tramp-smb-get-file-entries user host share file))) |
432 | ||
433 | (all-completions | |
434 | filename | |
435 | (mapcar | |
436 | (lambda (x) | |
437 | (list | |
438 | (if (string-match "d" (nth 1 x)) | |
439 | (file-name-as-directory (nth 0 x)) | |
440 | (nth 0 x)))) | |
441 | entries))))))) | |
442 | ||
443 | (defun tramp-smb-handle-file-newer-than-file-p (file1 file2) | |
444 | "Like `file-newer-than-file-p' for tramp files." | |
445 | (cond | |
446 | ((not (file-exists-p file1)) nil) | |
447 | ((not (file-exists-p file2)) t) | |
448 | (t (tramp-smb-time-less-p (file-attributes file2) | |
449 | (file-attributes file1))))) | |
450 | ||
451 | (defun tramp-smb-handle-file-writable-p (filename) | |
452 | "Like `file-writable-p' for tramp files." | |
453 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 454 | (let (user host localname) |
4007ba5b | 455 | (with-parsed-tramp-file-name filename l |
7432277c | 456 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 457 | (save-excursion |
7432277c KG |
458 | (let* ((share (tramp-smb-get-share localname)) |
459 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
460 | (entries (tramp-smb-get-file-entries user host share file)) |
461 | (entry (and entries | |
462 | (assoc (file-name-nondirectory file) entries)))) | |
463 | (and entry | |
464 | (string-match "w" (nth 1 entry)) | |
465 | t))))) | |
466 | ||
467 | (defun tramp-smb-handle-insert-directory | |
468 | (filename switches &optional wildcard full-directory-p) | |
469 | "Like `insert-directory' for tramp files. | |
470 | WILDCARD and FULL-DIRECTORY-P are not handled." | |
471 | (setq filename (expand-file-name filename)) | |
472 | (when (file-directory-p filename) | |
473 | ;; This check is a little bit strange, but in `dired-add-entry' | |
474 | ;; this function is called with a non-directory ... | |
475 | (setq filename (file-name-as-directory filename))) | |
476 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 477 | (let (user host localname) |
4007ba5b | 478 | (with-parsed-tramp-file-name filename l |
7432277c | 479 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 480 | (save-match-data |
7432277c KG |
481 | (let* ((share (tramp-smb-get-share localname)) |
482 | (file (tramp-smb-get-localname localname nil)) | |
4007ba5b KG |
483 | (entries (tramp-smb-get-file-entries user host share file))) |
484 | ||
485 | ;; Delete dummy "" entry, useless entries | |
7432277c | 486 | (setq entries |
4007ba5b KG |
487 | (if (file-directory-p filename) |
488 | (delq (assoc "" entries) entries) | |
489 | ;; We just need the only and only entry FILENAME. | |
490 | (list (assoc (file-name-nondirectory filename) entries)))) | |
491 | ||
492 | ;; Sort entries | |
493 | (setq entries | |
494 | (sort | |
495 | entries | |
496 | (lambda (x y) | |
497 | (if (string-match "t" switches) | |
498 | ; sort by date | |
499 | (tramp-smb-time-less-p (nth 3 y) (nth 3 x)) | |
500 | ; sort by name | |
501 | (string-lessp (nth 0 x) (nth 0 y)))))) | |
502 | ||
503 | ;; Print entries | |
504 | (mapcar | |
505 | (lambda (x) | |
506 | (insert | |
507 | (format | |
508 | "%10s %3d %-8s %-8s %8s %s %s\n" | |
509 | (nth 1 x) ; mode | |
510 | 1 "nobody" "nogroup" | |
511 | (nth 2 x) ; size | |
512 | (format-time-string | |
513 | (if (tramp-smb-time-less-p | |
514 | (tramp-smb-time-subtract (current-time) (nth 3 x)) | |
515 | tramp-smb-half-a-year) | |
516 | "%b %e %R" | |
517 | "%b %e %Y") | |
518 | (nth 3 x)) ; date | |
519 | (nth 0 x))) ; file name | |
520 | (forward-line) | |
521 | (beginning-of-line)) | |
522 | entries))))) | |
523 | ||
524 | (defun tramp-smb-handle-make-directory (dir &optional parents) | |
525 | "Like `make-directory' for tramp files." | |
526 | (setq dir (directory-file-name (expand-file-name dir))) | |
527 | (unless (file-name-absolute-p dir) | |
528 | (setq dir (concat default-directory dir))) | |
529 | ; (with-parsed-tramp-file-name dir nil | |
7432277c | 530 | (let (user host localname) |
4007ba5b | 531 | (with-parsed-tramp-file-name dir l |
7432277c | 532 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 533 | (save-match-data |
7432277c | 534 | (let* ((share (tramp-smb-get-share localname)) |
4007ba5b KG |
535 | (ldir (file-name-directory dir))) |
536 | ;; Make missing directory parts | |
537 | (when (and parents share (not (file-directory-p ldir))) | |
538 | (make-directory ldir parents)) | |
539 | ;; Just do it | |
540 | (when (file-directory-p ldir) | |
8daea7fc | 541 | (make-directory-internal dir)) |
4007ba5b KG |
542 | (unless (file-directory-p dir) |
543 | (error "Couldn't make directory %s" dir)))))) | |
544 | ||
545 | (defun tramp-smb-handle-make-directory-internal (directory) | |
546 | "Like `make-directory-internal' for tramp files." | |
547 | (setq directory (directory-file-name (expand-file-name directory))) | |
548 | (unless (file-name-absolute-p directory) | |
549 | (setq ldir (concat default-directory directory))) | |
550 | ; (with-parsed-tramp-file-name directory nil | |
7432277c | 551 | (let (user host localname) |
4007ba5b | 552 | (with-parsed-tramp-file-name directory l |
7432277c | 553 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 554 | (save-match-data |
7432277c KG |
555 | (let* ((share (tramp-smb-get-share localname)) |
556 | (file (tramp-smb-get-localname localname nil))) | |
4007ba5b KG |
557 | (when (file-directory-p (file-name-directory directory)) |
558 | (tramp-smb-maybe-open-connection user host share) | |
559 | (tramp-smb-send-command user host (format "mkdir \"%s\"" file))) | |
560 | (unless (file-directory-p directory) | |
561 | (error "Couldn't make directory %s" directory)))))) | |
562 | ||
563 | (defun tramp-smb-handle-rename-file | |
564 | (filename newname &optional ok-if-already-exists) | |
565 | "Like `rename-file' for tramp files." | |
566 | (setq filename (expand-file-name filename) | |
567 | newname (expand-file-name newname)) | |
568 | ||
569 | (let ((tmpfile (file-local-copy filename))) | |
570 | ||
571 | (if tmpfile | |
572 | ;; remote filename | |
573 | (rename-file tmpfile newname ok-if-already-exists) | |
574 | ||
575 | ;; remote newname | |
576 | (when (file-directory-p newname) | |
577 | (setq newname (expand-file-name | |
578 | (file-name-nondirectory filename) newname))) | |
579 | (when (and (not ok-if-already-exists) | |
580 | (file-exists-p newname)) | |
581 | (error "rename-file: file %s already exists" newname)) | |
582 | ||
583 | ; (with-parsed-tramp-file-name newname nil | |
7432277c | 584 | (let (user host localname) |
4007ba5b | 585 | (with-parsed-tramp-file-name newname l |
7432277c | 586 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 587 | (save-excursion |
7432277c KG |
588 | (let ((share (tramp-smb-get-share localname)) |
589 | (file (tramp-smb-get-localname localname t))) | |
4007ba5b KG |
590 | (tramp-smb-maybe-open-connection user host share) |
591 | (tramp-message-for-buffer | |
592 | nil tramp-smb-method user host | |
593 | 5 "Copying file %s to file %s..." filename newname) | |
594 | (if (tramp-smb-send-command | |
595 | user host (format "put %s \"%s\"" filename file)) | |
596 | (tramp-message-for-buffer | |
597 | nil tramp-smb-method user host | |
598 | 5 "Copying file %s to file %s...done" filename newname) | |
599 | (error "Cannot rename `%s'" filename))))))) | |
600 | ||
601 | (delete-file filename)) | |
602 | ||
603 | (defun tramp-smb-handle-write-region | |
604 | (start end filename &optional append visit lockname confirm) | |
605 | "Like `write-region' for tramp files." | |
606 | (unless (eq append nil) | |
607 | (error "Cannot append to file using tramp (`%s')" filename)) | |
608 | (setq filename (expand-file-name filename)) | |
609 | ;; XEmacs takes a coding system as the seventh argument, not `confirm' | |
610 | (when (and (not (featurep 'xemacs)) | |
611 | confirm (file-exists-p filename)) | |
612 | (unless (y-or-n-p (format "File %s exists; overwrite anyway? " | |
613 | filename)) | |
614 | (error "File not overwritten"))) | |
615 | ; (with-parsed-tramp-file-name filename nil | |
7432277c | 616 | (let (user host localname) |
4007ba5b | 617 | (with-parsed-tramp-file-name filename l |
7432277c | 618 | (setq user l-user host l-host localname l-localname)) |
4007ba5b | 619 | (save-excursion |
7432277c KG |
620 | (let ((share (tramp-smb-get-share localname)) |
621 | (file (tramp-smb-get-localname localname t)) | |
4007ba5b KG |
622 | (curbuf (current-buffer)) |
623 | ;; We use this to save the value of `last-coding-system-used' | |
624 | ;; after writing the tmp file. At the end of the function, | |
625 | ;; we set `last-coding-system-used' to this saved value. | |
626 | ;; This way, any intermediary coding systems used while | |
627 | ;; talking to the remote shell or suchlike won't hose this | |
628 | ;; variable. This approach was snarfed from ange-ftp.el. | |
629 | coding-system-used | |
630 | tmpfil) | |
631 | ;; Write region into a tmp file. | |
632 | (setq tmpfil (tramp-make-temp-file)) | |
633 | ;; We say `no-message' here because we don't want the visited file | |
634 | ;; modtime data to be clobbered from the temp file. We call | |
635 | ;; `set-visited-file-modtime' ourselves later on. | |
636 | (tramp-run-real-handler | |
637 | 'write-region | |
638 | (if confirm ; don't pass this arg unless defined for backward compat. | |
639 | (list start end tmpfil append 'no-message lockname confirm) | |
640 | (list start end tmpfil append 'no-message lockname))) | |
641 | ;; Now, `last-coding-system-used' has the right value. Remember it. | |
642 | (when (boundp 'last-coding-system-used) | |
643 | (setq coding-system-used last-coding-system-used)) | |
644 | ||
645 | (tramp-smb-maybe-open-connection user host share) | |
646 | (tramp-message-for-buffer | |
647 | nil tramp-smb-method user host | |
648 | 5 "Writing tmp file %s to file %s..." tmpfil filename) | |
649 | (if (tramp-smb-send-command | |
650 | user host (format "put %s \"%s\"" tmpfil file)) | |
651 | (tramp-message-for-buffer | |
652 | nil tramp-smb-method user host | |
653 | 5 "Writing tmp file %s to file %s...done" tmpfil filename) | |
654 | (error "Cannot write `%s'" filename)) | |
655 | ||
656 | (delete-file tmpfil) | |
657 | (unless (equal curbuf (current-buffer)) | |
658 | (error "Buffer has changed from `%s' to `%s'" | |
659 | curbuf (current-buffer))) | |
660 | (when (eq visit t) | |
661 | (set-visited-file-modtime)) | |
662 | ;; Make `last-coding-system-used' have the right value. | |
663 | (when (boundp 'last-coding-system-used) | |
664 | (setq last-coding-system-used coding-system-used)))))) | |
665 | ||
666 | ||
667 | ;; Internal file name functions | |
668 | ||
7432277c KG |
669 | (defun tramp-smb-get-share (localname) |
670 | "Returns the share name of LOCALNAME." | |
4007ba5b | 671 | (save-match-data |
7432277c KG |
672 | (when (string-match "^/?\\([^/]+\\)/" localname) |
673 | (match-string 1 localname)))) | |
4007ba5b | 674 | |
7432277c KG |
675 | (defun tramp-smb-get-localname (localname convert) |
676 | "Returns the file name of LOCALNAME. | |
4007ba5b KG |
677 | If CONVERT is non-nil exchange \"/\" by \"\\\\\"." |
678 | (save-match-data | |
7432277c | 679 | (let ((res localname)) |
4007ba5b KG |
680 | |
681 | (setq | |
682 | res (if (string-match "^/?[^/]+/\\(.*\\)" res) | |
683 | (if convert | |
684 | (mapconcat | |
685 | (lambda (x) (if (equal x ?/) "\\" (char-to-string x))) | |
686 | (match-string 1 res) "") | |
687 | (match-string 1 res)) | |
688 | (if (string-match "^/?\\([^/]+\\)$" res) | |
689 | (match-string 1 res) | |
690 | ""))) | |
691 | ||
692 | ;; Sometimes we have discarded `substitute-in-file-name' | |
693 | (when (string-match "\\(\\$\\$\\)\\(/\\|$\\)" res) | |
694 | (setq res (replace-match "$" nil nil res 1))) | |
695 | ||
696 | res))) | |
697 | ||
698 | ;; Share names of a host are cached. It is very unlikely that the | |
699 | ;; shares do change during connection. | |
7432277c KG |
700 | (defun tramp-smb-get-file-entries (user host share localname) |
701 | "Read entries which match LOCALNAME. | |
4007ba5b | 702 | Either the shares are listed, or the `dir' command is executed. |
7432277c KG |
703 | Only entries matching the localname are returned. |
704 | Result is a list of (LOCALNAME MODE SIZE MONTH DAY TIME YEAR)." | |
4007ba5b KG |
705 | (save-excursion |
706 | (save-match-data | |
7432277c KG |
707 | (let ((base (or (and (> (length localname) 0) |
708 | (string-match "\\([^/]+\\)$" localname) | |
709 | (regexp-quote (match-string 1 localname))) | |
4007ba5b KG |
710 | "")) |
711 | res entry) | |
712 | (set-buffer (tramp-get-buffer nil tramp-smb-method user host)) | |
713 | (if (and (not share) tramp-smb-share-cache) | |
714 | ;; Return cached shares | |
715 | (setq res tramp-smb-share-cache) | |
716 | ;; Read entries | |
717 | (tramp-smb-maybe-open-connection user host share) | |
718 | (when share | |
719 | (tramp-smb-send-command | |
720 | user host | |
721 | (format "dir %s" | |
7432277c | 722 | (if (zerop (length localname)) "" (concat "\"" localname "*\""))))) |
4007ba5b KG |
723 | (goto-char (point-min)) |
724 | ;; Loop the listing | |
725 | (unless (re-search-forward tramp-smb-errors nil t) | |
726 | (while (not (eobp)) | |
727 | (setq entry (tramp-smb-read-file-entry share)) | |
728 | (forward-line) | |
729 | (when entry (add-to-list 'res entry)))) | |
730 | (unless share | |
731 | ;; Cache share entries | |
732 | (setq tramp-smb-share-cache res))) | |
733 | ||
734 | ||
735 | ;; Add directory itself | |
736 | (add-to-list 'res '("" "dr-xr-xr-x" 0 (0 0))) | |
737 | ||
738 | ;; Check for matching entries | |
739 | (delq nil (mapcar | |
740 | (lambda (x) (and (string-match base (nth 0 x)) x)) | |
741 | res)))))) | |
742 | ||
743 | ;; Return either a share name (if SHARE is nil), or a file name | |
744 | ;; | |
745 | ;; If shares are listed, the following format is expected | |
746 | ;; | |
747 | ;; \s-\{8,8} - leading spaces | |
748 | ;; \S-\(.*\S-\)\s-* - share name, 14 char | |
749 | ;; \s- - space delimeter | |
750 | ;; \S-+\s-* - type, 8 char, "Disk " expected | |
751 | ;; \(\s-\{2,2\}.*\)? - space delimeter, comment | |
752 | ;; | |
753 | ;; Entries provided by smbclient DIR aren't fully regular. | |
754 | ;; They should have the format | |
755 | ;; | |
756 | ;; \s-\{2,2} - leading spaces | |
b1a2b924 KG |
757 | ;; \S-\(.*\S-\)\s-* - file name, 30 chars, left bound |
758 | ;; \s-+[ADHRSV]* - permissions, 7 chars, right bound | |
4007ba5b | 759 | ;; \s- - space delimeter |
b1a2b924 | 760 | ;; \s-+[0-9]+ - size, 8 chars, right bound |
4007ba5b KG |
761 | ;; \s-\{2,2\} - space delimeter |
762 | ;; \w\{3,3\} - weekday | |
763 | ;; \s- - space delimeter | |
b1a2b924 KG |
764 | ;; \w\{3,3\} - month |
765 | ;; \s- - space delimeter | |
4007ba5b KG |
766 | ;; [ 19][0-9] - day |
767 | ;; \s- - space delimeter | |
768 | ;; [0-9]\{2,2\}:[0-9]\{2,2\}:[0-9]\{2,2\} - time | |
769 | ;; \s- - space delimeter | |
770 | ;; [0-9]\{4,4\} - year | |
771 | ;; | |
b1a2b924 KG |
772 | ;; samba/src/client.c (http://samba.org/doxygen/samba/client_8c-source.html) |
773 | ;; has function display_finfo: | |
774 | ;; | |
775 | ;; d_printf(" %-30s%7.7s %8.0f %s", | |
776 | ;; finfo->name, | |
777 | ;; attrib_string(finfo->mode), | |
778 | ;; (double)finfo->size, | |
779 | ;; asctime(LocalTime(&t))); | |
780 | ;; | |
781 | ;; in Samba 1.9, there's the following code: | |
782 | ;; | |
783 | ;; DEBUG(0,(" %-30s%7.7s%10d %s", | |
784 | ;; CNV_LANG(finfo->name), | |
785 | ;; attrib_string(finfo->mode), | |
786 | ;; finfo->size, | |
787 | ;; asctime(LocalTime(&t)))); | |
788 | ;; | |
4007ba5b KG |
789 | ;; Problems: |
790 | ;; * Modern regexp constructs, like spy groups and counted repetitions, aren't | |
791 | ;; available in older Emacsen. | |
792 | ;; * The length of constructs (file name, size) might exceed the default. | |
793 | ;; * File names might contain spaces. | |
794 | ;; * Permissions might be empty. | |
795 | ;; | |
796 | ;; So we try to analyze backwards. | |
797 | (defun tramp-smb-read-file-entry (share) | |
798 | "Parse entry in SMB output buffer. | |
799 | If SHARE is result, entries are of type dir. Otherwise, shares are listed. | |
7432277c | 800 | Result is the list (LOCALNAME MODE SIZE MTIME)." |
4007ba5b | 801 | (let ((line (buffer-substring (point) (tramp-point-at-eol))) |
7432277c | 802 | localname mode size month day hour min sec year mtime) |
4007ba5b KG |
803 | |
804 | (if (not share) | |
805 | ||
806 | ; Read share entries | |
807 | (when (string-match "^\\s-+\\(\\S-+\\)\\s-+Disk" line) | |
7432277c | 808 | (setq localname (match-string 1 line) |
4007ba5b KG |
809 | mode "dr-xr-xr-x" |
810 | size 0)) | |
811 | ||
812 | ; Real listing | |
813 | (block nil | |
814 | ||
815 | ;; year | |
816 | (if (string-match "\\([0-9]+\\)$" line) | |
817 | (setq year (string-to-number (match-string 1 line)) | |
818 | line (substring line 0 -5)) | |
819 | (return)) | |
820 | ||
821 | ;; time | |
822 | (if (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)$" line) | |
823 | (setq hour (string-to-number (match-string 1 line)) | |
824 | min (string-to-number (match-string 2 line)) | |
825 | sec (string-to-number (match-string 3 line)) | |
826 | line (substring line 0 -9)) | |
827 | (return)) | |
828 | ||
829 | ;; day | |
830 | (if (string-match "\\([0-9]+\\)$" line) | |
831 | (setq day (string-to-number (match-string 1 line)) | |
832 | line (substring line 0 -3)) | |
833 | (return)) | |
834 | ||
835 | ;; month | |
836 | (if (string-match "\\(\\w+\\)$" line) | |
837 | (setq month (match-string 1 line) | |
838 | line (substring line 0 -4)) | |
839 | (return)) | |
840 | ||
841 | ;; weekday | |
842 | (if (string-match "\\(\\w+\\)$" line) | |
843 | (setq line (substring line 0 -5)) | |
844 | (return)) | |
845 | ||
846 | ;; size | |
847 | (if (string-match "\\([0-9]+\\)$" line) | |
b1a2b924 KG |
848 | (let ((length (- (max 10 (1+ (length (match-string 1 line))))))) |
849 | (setq size (string-to-number (match-string 1 line))) | |
850 | (when (string-match "\\([ADHRSV]+\\)" (substring line length)) | |
851 | (setq length (+ length (match-end 0)))) | |
852 | (setq line (substring line 0 length))) | |
4007ba5b KG |
853 | (return)) |
854 | ||
b1a2b924 KG |
855 | ;; mode: ARCH, DIR, HIDDEN, RONLY, SYSTEM, VOLID |
856 | (if (string-match "\\([ADHRSV]+\\)?$" line) | |
4007ba5b | 857 | (setq |
b1a2b924 | 858 | mode (or (match-string 1 line) "") |
4007ba5b KG |
859 | mode (save-match-data (format |
860 | "%s%s" | |
861 | (if (string-match "D" mode) "d" "-") | |
862 | (mapconcat | |
863 | (lambda (x) "") " " | |
864 | (concat "r" (if (string-match "R" mode) "-" "w") "x")))) | |
b1a2b924 | 865 | line (substring line 0 -7)) |
4007ba5b KG |
866 | (return)) |
867 | ||
7432277c | 868 | ;; localname |
b1a2b924 | 869 | (if (string-match "^\\s-+\\(\\S-\\(.*\\S-\\)?\\)\\s-*$" line) |
7432277c | 870 | (setq localname (match-string 1 line)) |
4007ba5b KG |
871 | (return)))) |
872 | ||
7432277c | 873 | (when (and localname mode size) |
4007ba5b KG |
874 | (setq mtime |
875 | (if (and sec min hour day month year) | |
876 | (encode-time | |
877 | sec min hour day | |
878 | (cdr (assoc (downcase month) tramp-smb-parse-time-months)) | |
879 | year) | |
880 | '(0 0))) | |
7432277c | 881 | (list localname mode size mtime)))) |
4007ba5b | 882 | |
8daea7fc KG |
883 | ;; Inodes don't exist for SMB files. Therefore we must generate virtual ones. |
884 | ;; Used in `find-buffer-visiting'. | |
885 | ;; The method applied might be not so efficient (Ange-FTP uses hashes). But | |
886 | ;; performance isn't the major issue given that file transfer will take time. | |
887 | ||
888 | (defun tramp-smb-get-inode (share file) | |
889 | "Returns the virtual inode number. | |
890 | If it doesn't exist, generate a new one." | |
891 | (let ((string (concat share "/" (directory-file-name file)))) | |
892 | (unless (assoc string tramp-smb-inodes) | |
893 | (add-to-list 'tramp-smb-inodes | |
894 | (list string (length tramp-smb-inodes)))) | |
895 | (nth 1 (assoc string tramp-smb-inodes)))) | |
896 | ||
4007ba5b KG |
897 | |
898 | ;; Connection functions | |
899 | ||
900 | (defun tramp-smb-send-command (user host command) | |
901 | "Send the COMMAND to USER at HOST (logged into an SMB session). | |
902 | Erases temporary buffer before sending the command. Returns nil if | |
903 | there has been an error message from smbclient." | |
904 | (save-excursion | |
905 | (set-buffer (tramp-get-buffer nil tramp-smb-method user host)) | |
906 | (erase-buffer) | |
907 | (tramp-send-command nil tramp-smb-method user host command nil t) | |
908 | (tramp-smb-wait-for-output user host))) | |
909 | ||
910 | (defun tramp-smb-maybe-open-connection (user host share) | |
911 | "Maybe open a connection to HOST, logging in as USER, using `tramp-smb-program'. | |
912 | Does not do anything if a connection is already open, but re-opens the | |
913 | connection if a previous connection has died for some reason." | |
914 | (let ((p (get-buffer-process | |
915 | (tramp-get-buffer nil tramp-smb-method user host)))) | |
916 | (save-excursion | |
917 | (set-buffer (tramp-get-buffer nil tramp-smb-method user host)) | |
918 | ;; Check whether it is still the same share | |
919 | (unless (and p (processp p) (string-equal tramp-smb-share share)) | |
920 | (when (and p (processp p)) | |
921 | (delete-process p) | |
922 | (setq p nil))) | |
923 | ;; If too much time has passed since last command was sent, look | |
924 | ;; whether process is still alive. If it isn't, kill it. | |
925 | (when (and tramp-last-cmd-time | |
926 | (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60) | |
927 | p (processp p) (memq (process-status p) '(run open))) | |
928 | (unless (and p (processp p) (memq (process-status p) '(run open))) | |
929 | (delete-process p) | |
930 | (setq p nil)))) | |
931 | (unless (and p (processp p) (memq (process-status p) '(run open))) | |
932 | (when (and p (processp p)) | |
933 | (delete-process p)) | |
934 | (tramp-smb-open-connection user host share)))) | |
935 | ||
936 | (defun tramp-smb-open-connection (user host share) | |
937 | "Open a connection using `tramp-smb-program'. | |
938 | This starts the command `smbclient //HOST/SHARE -U USER', then waits | |
939 | for a remote password prompt. It queries the user for the password, | |
940 | then sends the password to the remote host. | |
941 | ||
942 | Domain names in USER and port numbers in HOST are acknowledged." | |
943 | ||
944 | (save-match-data | |
945 | (let* ((buffer (tramp-get-buffer nil tramp-smb-method user host)) | |
946 | (real-user user) | |
947 | (real-host host) | |
948 | domain port args) | |
949 | ||
950 | ; Check for domain ("user%domain") and port ("host#port") | |
951 | (when (and user (string-match "\\(.+\\)%\\(.+\\)" user)) | |
952 | (setq real-user (or (match-string 1 user) user) | |
953 | domain (match-string 2 user))) | |
954 | ||
955 | (when (and host (string-match "\\(.+\\)#\\(.+\\)" host)) | |
956 | (setq real-host (or (match-string 1 host) host) | |
957 | port (match-string 2 host))) | |
958 | ||
959 | (if share | |
960 | (setq args (list (concat "//" real-host "/" share))) | |
961 | (setq args (list "-L" real-host ))) | |
962 | ||
963 | (if real-user | |
964 | (setq args (append args (list "-U" real-user))) | |
965 | (setq args (append args (list "-N")))) | |
966 | ||
967 | (when domain (setq args (append args (list "-W" domain)))) | |
968 | (when port (setq args (append args (list "-p" port)))) | |
969 | ||
970 | ; OK, let's go | |
971 | (tramp-pre-connection nil tramp-smb-method user host) | |
972 | (tramp-message 7 "Opening connection for //%s@%s/%s..." | |
973 | user host (or share "")) | |
974 | ||
975 | (let* ((default-directory (tramp-temporary-file-directory)) | |
976 | ;; If we omit the conditional here, then we would use | |
977 | ;; `undecided-dos' in some cases. With the conditional, | |
978 | ;; we use nil in these cases. Which one is right? | |
979 | (coding-system-for-read (unless (and (not (featurep 'xemacs)) | |
980 | (> emacs-major-version 20)) | |
981 | tramp-dos-coding-system)) | |
982 | (p (apply #'start-process (buffer-name buffer) buffer | |
983 | tramp-smb-program args))) | |
984 | ||
985 | (tramp-message 9 "Started process %s" (process-command p)) | |
986 | (process-kill-without-query p) | |
987 | (set-buffer buffer) | |
988 | (set-process-sentinel | |
989 | p (lambda (proc str) (setq tramp-smb-process-running nil))) | |
990 | ; If no share is given, the process will terminate | |
991 | (setq tramp-smb-process-running share | |
992 | tramp-smb-share share) | |
993 | ||
994 | ; send password | |
995 | (when real-user | |
996 | (let ((pw-prompt "Password:")) | |
997 | (tramp-message 9 "Sending password") | |
998 | (tramp-enter-password p pw-prompt))) | |
999 | ||
1000 | (unless (tramp-smb-wait-for-output user host) | |
1001 | (error "Cannot open connection //%s@%s/%s" | |
1002 | user host (or share ""))))))) | |
1003 | ||
1004 | ;; We don't use timeouts. If needed, the caller shall wrap around. | |
1005 | (defun tramp-smb-wait-for-output (user host) | |
1006 | "Wait for output from smbclient command. | |
1007 | Sets position to begin of buffer. | |
1008 | Returns nil if an error message has appeared." | |
1009 | (save-excursion | |
1010 | (let ((proc (get-buffer-process (current-buffer))) | |
1011 | (found (progn (goto-char (point-max)) | |
1012 | (beginning-of-line) | |
1013 | (looking-at tramp-smb-prompt))) | |
1014 | err) | |
1015 | (save-match-data | |
1016 | ;; Algorithm: get waiting output. See if last line contains | |
1017 | ;; tramp-smb-prompt sentinel, or process has exited. | |
1018 | ;; If not, wait a bit and again get waiting output. | |
1019 | (while (and (not found) tramp-smb-process-running) | |
1020 | (accept-process-output proc) | |
1021 | (goto-char (point-max)) | |
1022 | (beginning-of-line) | |
1023 | (setq found (looking-at tramp-smb-prompt))) | |
1024 | ||
1025 | ;; There might be pending output. If tramp-smb-prompt sentinel | |
1026 | ;; hasn't been found, the process has died already. We should | |
1027 | ;; give it a chance. | |
1028 | (when (not found) (accept-process-output nil 1)) | |
1029 | ||
1030 | ;; Search for errors. | |
1031 | (goto-char (point-min)) | |
1032 | (setq err (re-search-forward tramp-smb-errors nil t))) | |
1033 | ||
1034 | ;; Add output to debug buffer if appropriate. | |
1035 | (when tramp-debug-buffer | |
1036 | (append-to-buffer | |
1037 | (tramp-get-debug-buffer nil tramp-smb-method user host) | |
1038 | (point-min) (point-max)) | |
1039 | (when (and (not found) tramp-smb-process-running) | |
1040 | (save-excursion | |
1041 | (set-buffer | |
1042 | (tramp-get-debug-buffer nil tramp-smb-method user host)) | |
1043 | (goto-char (point-max)) | |
1044 | (insert (format "[[Remote prompt `%s' not found]]\n" | |
1045 | tramp-smb-prompt))))) | |
1046 | (goto-char (point-min)) | |
1047 | ;; Return value is whether no error message has appeared. | |
1048 | (not err)))) | |
1049 | ||
1050 | ||
1051 | ;; Snarfed code from time-date.el and parse-time.el | |
1052 | ||
1053 | (defconst tramp-smb-half-a-year '(241 17024) | |
1054 | "Evaluated by \"(days-to-time 183)\".") | |
1055 | ||
1056 | (defconst tramp-smb-parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3) | |
1057 | ("apr" . 4) ("may" . 5) ("jun" . 6) | |
1058 | ("jul" . 7) ("aug" . 8) ("sep" . 9) | |
1059 | ("oct" . 10) ("nov" . 11) ("dec" . 12)) | |
1060 | "Alist mapping month names to integers.") | |
1061 | ||
1062 | (defun tramp-smb-time-less-p (t1 t2) | |
1063 | "Say whether time value T1 is less than time value T2." | |
1064 | (unless t1 (setq t1 '(0 0))) | |
1065 | (unless t2 (setq t2 '(0 0))) | |
1066 | (or (< (car t1) (car t2)) | |
1067 | (and (= (car t1) (car t2)) | |
1068 | (< (nth 1 t1) (nth 1 t2))))) | |
1069 | ||
1070 | (defun tramp-smb-time-subtract (t1 t2) | |
1071 | "Subtract two time values. | |
1072 | Return the difference in the format of a time value." | |
1073 | (unless t1 (setq t1 '(0 0))) | |
1074 | (unless t2 (setq t2 '(0 0))) | |
1075 | (let ((borrow (< (cadr t1) (cadr t2)))) | |
1076 | (list (- (car t1) (car t2) (if borrow 1 0)) | |
1077 | (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))) | |
1078 | ||
1079 | ||
1080 | ;; `PC-do-completion' touches the returning "$$" by `substitute-in-file-name'. | |
1081 | ;; Must be corrected. | |
1082 | ||
1083 | (defadvice PC-do-completion (around tramp-smb-advice-PC-do-completion activate) | |
1084 | "Changes \"$\" back to \"$$\" in minibuffer." | |
1085 | (if (funcall PC-completion-as-file-name-predicate) | |
1086 | ||
1087 | (progn | |
1088 | ;; Substitute file names | |
1089 | (let* ((beg (or (and (functionp 'minibuffer-prompt-end) ; Emacs 21 | |
1090 | (funcall 'minibuffer-prompt-end)) | |
1091 | (point-min))) | |
1092 | (end (point-max)) | |
1093 | (str (substitute-in-file-name (buffer-substring beg end)))) | |
1094 | (delete-region beg end) | |
1095 | (insert str) | |
1096 | (ad-set-arg 2 (point))) | |
1097 | ||
1098 | ;; Do `PC-do-completion' without substitution | |
1099 | (let* (save) | |
1100 | (fset 'save (symbol-function 'substitute-in-file-name)) | |
1101 | (fset 'substitute-in-file-name (symbol-function 'identity)) | |
1102 | ad-do-it | |
1103 | (fset 'substitute-in-file-name (symbol-function 'save))) | |
1104 | ||
1105 | ;; Expand "$" | |
1106 | (let* ((beg (or (and (functionp 'minibuffer-prompt-end) ; Emacs 21 | |
1107 | (funcall 'minibuffer-prompt-end)) | |
1108 | (point-min))) | |
1109 | (end (point-max)) | |
1110 | (str (buffer-substring beg end))) | |
1111 | (delete-region beg end) | |
1112 | (insert (if (string-match "\\(\\$\\)\\(/\\|$\\)" str) | |
1113 | (replace-match "$$" nil nil str 1) | |
1114 | str)))) | |
1115 | ||
1116 | ;; No file names. Behave unchanged. | |
1117 | ad-do-it)) | |
1118 | ||
1119 | (provide 'tramp-smb) | |
1120 | ||
1121 | ;;; TODO: | |
1122 | ||
1123 | ;; * Provide a local smb.conf. The default one might not be readable. | |
1124 | ;; * Error handling in case password is wrong. | |
1125 | ;; * Read password from "~/.netrc". | |
1126 | ;; * Use different buffers for different shares. By this, the password | |
1127 | ;; won't be requested again when changing shares on the same host. | |
1128 | ;; * Return more comprehensive file permission string. Think whether it is | |
1129 | ;; possible to implement `set-file-modes'. | |
1130 | ;; * Handle WILDCARD and FULL-DIRECTORY-P in | |
1131 | ;; `tramp-smb-handle-insert-directory'. | |
1132 | ;; * Handle links (FILENAME.LNK). | |
1133 | ;; * Maybe local tmp files should have the same extension like the original | |
1134 | ;; files. Strange behaviour with jka-compr otherwise? | |
1135 | ;; * Copy files in dired from SMB to another method doesn't work. | |
1136 | ;; * Try to remove the inclusion of dummy "" directory. Seems to be at | |
1137 | ;; several places, especially in `tramp-smb-handle-insert-directory'. | |
1138 | ;; * Provide variables for debug. | |
1139 | ;; * (RMS) Use unwind-protect to clean up the state so as to make the state | |
1140 | ;; regular again. | |
1141 | ||
ab5796a9 | 1142 | ;;; arch-tag: fcc9dbec-7503-4d73-b638-3c8aa59575f5 |
4007ba5b | 1143 | ;;; tramp-smb.el ends here |