1 ;;;; pathname parsing for Win32 filesystems
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 (def!struct
(win32-host
15 (:make-load-form-fun make-host-load-form
)
17 (parse #'parse-win32-namestring
)
18 (parse-native #'parse-native-win32-namestring
)
19 (unparse #'unparse-win32-namestring
)
20 (unparse-native #'unparse-native-win32-namestring
)
21 (unparse-host #'unparse-win32-host
)
22 (unparse-directory #'unparse-win32-directory
)
23 (unparse-file #'unparse-win32-file
)
24 (unparse-enough #'unparse-win32-enough
)
25 (unparse-directory-separator "\\")
26 (simplify-namestring #'simplify-win32-namestring
)
27 (customary-case :lower
))))
29 (defvar *physical-host
* (make-win32-host))
32 (define-symbol-macro +long-file-name-prefix
+ (quote "\\\\?\\"))
33 (define-symbol-macro +unc-file-name-prefix
+ (quote "\\\\?\\UNC"))
35 (defun extract-device (namestr start end
)
36 (declare (type simple-string namestr
)
37 (type index start end
))
38 (if (>= end
(+ start
2))
39 (let ((c0 (char namestr start
))
40 (c1 (char namestr
(1+ start
))))
41 (cond ((and (eql c1
#\
:) (alpha-char-p c0
))
42 ;; "X:" style, saved as X
43 (values (string (char namestr start
)) (+ start
2)))
44 ((and (member c0
'(#\
/ #\\)) (eql c0 c1
) (>= end
(+ start
3)))
45 ;; "//UNC" style, saved as :UNC device, with host and share
46 ;; becoming directory components.
47 (values :unc
(+ start
1)))
52 (defun split-at-slashes-and-backslashes (namestr start end
)
53 (declare (type simple-string namestr
)
54 (type index start end
))
55 ;; FIXME: There is a fundamental brokenness in using the same
56 ;; character as escape character and directory separator in
57 ;; non-native pathnames. (PATHNAME-DIRECTORY #P"\\*/") should
58 ;; probably be (:RELATIVE "*") everywhere, but on Windows it's
59 ;; (:ABSOLUTE :WILD)! See lp#673625.
60 (let ((absolute (and (/= start end
)
61 (or (char= (schar namestr start
) #\
/)
62 (char= (schar namestr start
) #\\)))))
65 ;; Next, split the remainder into slash-separated chunks.
68 (let ((slash (position-if (lambda (c)
71 namestr
:start start
:end end
)))
72 (pieces (cons start
(or slash end
)))
75 (setf start
(1+ slash
))))
76 (values absolute
(pieces)))))
78 (defun parse-win32-namestring (namestring start end
)
79 (declare (type simple-string namestring
)
80 (type index start end
))
81 (setf namestring
(coerce namestring
'simple-string
))
82 (multiple-value-bind (device new-start
)
83 (extract-device namestring start end
)
84 (multiple-value-bind (absolute pieces
)
85 (split-at-slashes-and-backslashes namestring new-start end
)
86 (multiple-value-bind (name type version
)
87 (let* ((tail (car (last pieces
)))
88 (tail-start (car tail
))
89 (tail-end (cdr tail
)))
90 (unless (= tail-start tail-end
)
91 (setf pieces
(butlast pieces
))
92 (extract-name-type-and-version namestring tail-start tail-end
#\^
)))
95 (let ((position (position-if (lambda (char)
96 (or (char= char
(code-char 0))
100 (error 'namestring-parse-error
101 :complaint
"can't embed #\\Nul or #\\/ in Windows namestring"
102 :namestring namestring
106 ;; Deal with ~ and ~user.
108 (destructuring-bind (start . end
) (car pieces
)
109 (when (and (not absolute
)
110 (not (eql start end
))
111 (string= namestring
"~"
115 (if (> end
(1+ start
))
116 (setf home
(list :home
(subseq namestring
(1+ start
) end
)))
120 ;; Now we have everything we want. So return it.
121 (values nil
; no host for Win32 namestrings
124 (dolist (piece pieces
)
125 (let ((piece-start (car piece
))
126 (piece-end (cdr piece
)))
127 (unless (= piece-start piece-end
)
128 (cond ((string= namestring
".."
132 ((string= namestring
"**"
135 (dirs :wild-inferiors
))
137 (dirs (maybe-make-pattern namestring
143 (list* :absolute home
(dirs))
144 (cons :absolute
(dirs))))
146 (cons :relative
(dirs)))
153 (defun parse-native-win32-namestring (namestring start end as-directory
)
154 (declare (type simple-string namestring
)
155 (type index start end
))
156 (setf namestring
(coerce namestring
'simple-string
))
157 (multiple-value-bind (device new-start
)
158 (cond ((= (length +unc-file-name-prefix
+)
159 (mismatch +unc-file-name-prefix
+ namestring
161 (values :unc
(+ start
(length +unc-file-name-prefix
+))))
162 ((= (length +long-file-name-prefix
+)
163 (mismatch +long-file-name-prefix
+ namestring
165 (extract-device namestring
166 (+ start
(length +long-file-name-prefix
+))
168 (t (extract-device namestring start end
)))
169 (multiple-value-bind (absolute ranges
)
170 (split-at-slashes-and-backslashes namestring new-start end
)
171 (let* ((components (loop for
((start . end
) . rest
) on ranges
172 for piece
= (subseq namestring start end
)
173 collect
(if (and (string= piece
"..") rest
)
176 (directory (if (and as-directory
177 (string/= "" (car (last components
))))
179 (butlast components
)))
182 (let* ((end (first (last components
)))
183 (dot (position #\. end
:from-end t
)))
184 ;; FIXME: can we get this dot-interpretation knowledge
185 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
186 ;; does slightly more work than that.
191 (list (subseq end
0 dot
) (subseq end
(1+ dot
))))
196 (cons (if absolute
:absolute
:relative
) directory
)
197 (first name-and-type
)
198 (second name-and-type
)
203 (defun unparse-win32-host (pathname)
204 (declare (type pathname pathname
)
206 ;; FIXME: same as UNPARSE-UNIX-HOST. That's probably not good.
209 (defun unparse-win32-device (pathname &optional native
)
210 (declare (type pathname pathname
))
211 (let ((device (pathname-device pathname
))
212 (directory (pathname-directory pathname
)))
213 (cond ((or (null device
) (eq device
:unspecific
))
216 (if native
"\\" "/"))
217 ((and (= 1 (length device
)) (alpha-char-p (char device
0)))
218 (concatenate 'simple-string device
":"))
219 ((and (consp directory
) (eq :relative
(car directory
)))
220 (error "No printed representation for a relative UNC pathname."))
223 (concatenate 'simple-string
"\\\\" device
)
224 (concatenate 'simple-string
"//" device
))))))
226 (defun unparse-win32-directory (pathname)
227 (unparse-physical-directory pathname
#\^
))
229 (defun unparse-win32-file (pathname)
230 (declare (type pathname pathname
))
232 (let* ((name (%pathname-name pathname
))
233 (type (%pathname-type pathname
))
234 (type-supplied (not (or (null type
) (eq type
:unspecific
)))))
235 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
236 ;; translating logical pathnames to a filesystem without
237 ;; versions (like Win32).
239 (when (and (null type
)
242 (position #\. name
:start
1))
243 (error "too many dots in the name: ~S" pathname
))
244 (when (and (typep name
'string
)
246 (error "name is of length 0: ~S" pathname
))
247 (strings (unparse-physical-piece name
#\^
)))
250 (error "cannot specify the type without a file: ~S" pathname
))
251 (when (typep type
'simple-string
)
252 (when (position #\. type
)
253 (error "type component can't have a #\. inside: ~S" pathname
)))
255 (strings (unparse-physical-piece type
#\^
))))
256 (apply #'concatenate
'simple-string
(strings))))
258 (defun unparse-win32-namestring (pathname)
259 (declare (type pathname pathname
))
260 (concatenate 'simple-string
261 (unparse-win32-device pathname
)
262 (unparse-physical-directory pathname
#\^
)
263 (unparse-win32-file pathname
)))
265 (defun unparse-native-win32-namestring (pathname as-file
)
266 (declare (type pathname pathname
))
267 (let* ((device (pathname-device pathname
))
268 (directory (pathname-directory pathname
))
269 (name (pathname-name pathname
))
270 (name-present-p (typep name
'(not (member nil
:unspecific
))))
271 (name-string (if name-present-p name
""))
272 (type (pathname-type pathname
))
273 (type-present-p (typep type
'(not (member nil
:unspecific
))))
274 (type-string (if type-present-p type
""))
275 (absolutep (and device
(eql :absolute
(car directory
)))))
278 (when (and absolutep
(member :up directory
))
279 ;; employ merge-pathnames to parse :BACKs into which we turn :UPs
283 (make-pathname :defaults pathname
:directory
'(:relative
))
284 (make-pathname :defaults pathname
285 :directory
(substitute :back
:up directory
))))))
287 (with-simple-output-to-string (s)
289 (write-string (case device
290 (:unc
+unc-file-name-prefix
+)
291 (otherwise +long-file-name-prefix
+)) s
))
292 (when (or (not absolutep
) (not (member device
'(:unc nil
))))
293 (write-string (unparse-win32-device pathname t
) s
))
295 (ecase (pop directory
)
297 (let ((next (pop directory
)))
298 ;; Don't use USER-HOMEDIR-NAMESTRING, since
299 ;; it can be specified as C:/User/user
300 ;; and (native-namestring (user-homedir-pathname))
301 ;; will be not equal to it, because it's parsed first.
302 (cond ((eq :home next
)
303 (write-string (native-namestring (user-homedir-pathname))
305 ((and (consp next
) (eq :home
(car next
)))
306 (let ((where (user-homedir-pathname (second next
))))
308 (write-string (native-namestring where
) s
)
309 (error "User homedir unknown for: ~S."
311 ;; namestring of user-homedir-pathname already has
315 (push next directory
))
317 (write-char #\\ s
)))))
319 (loop for
(piece . subdirs
) on directory
321 ((member :up
:back
) (write-string ".." s
))
322 (string (write-string piece s
))
323 (t (error "Bad directory segment in NATIVE-NAMESTRING: ~S."
325 if
(or subdirs
(stringp name
))
326 do
(write-char #\\ s
)
332 (unless (stringp name-string
) ;some kind of wild field
333 (error "Bad name component in NATIVE-NAMESTRING: ~S." name
))
334 (write-string name-string s
)
336 (unless (stringp type-string
) ;some kind of wild field
337 (error "Bad type component in NATIVE-NAMESTRING: ~S." type
))
339 (write-string type-string s
)))
342 "Type component without a name component in NATIVE-NAMESTRING: ~S."
345 (let ((string (get-output-stream-string s
)))
346 (return-from unparse-native-win32-namestring
347 (cond ((< (- 260 12) (length string
))
348 ;; KLUDGE: account for additional length of 8.3 name to make
349 ;; directories always accessible
350 (coerce string
'simple-string
))
353 (subseq string
(1- (length +unc-file-name-prefix
+)))
355 (t (subseq string
(length +long-file-name-prefix
+))))))))
359 (defun unparse-win32-enough (pathname defaults
)
360 (declare (type pathname pathname defaults
))
362 (error "~S cannot be represented relative to ~S."
365 (let* ((pathname-directory (%pathname-directory pathname
))
366 (defaults-directory (%pathname-directory defaults
))
367 (prefix-len (length defaults-directory
))
369 (cond ((null pathname-directory
) '(:relative
))
370 ((eq (car pathname-directory
) :relative
)
372 ((and (> prefix-len
0)
373 (>= (length pathname-directory
) prefix-len
)
374 (compare-component (subseq pathname-directory
377 ;; Pathname starts with a prefix of default. So
378 ;; just use a relative directory from then on out.
379 (cons :relative
(nthcdr prefix-len pathname-directory
)))
380 ((eq (car pathname-directory
) :absolute
)
381 ;; We are an absolute pathname, so we can just use it.
384 (bug "Bad fallthrough in ~S" 'unparse-unix-enough
)))))
385 (strings (unparse-physical-directory-list result-directory
#\^
)))
386 (let* ((pathname-type (%pathname-type pathname
))
387 (type-needed (and pathname-type
388 (not (eq pathname-type
:unspecific
))))
389 (pathname-name (%pathname-name pathname
))
390 (name-needed (or type-needed
392 (not (compare-component pathname-name
396 (unless pathname-name
(lose))
397 (when (and (null pathname-type
)
398 (typep pathname-name
'simple-string
)
399 (position #\. pathname-name
:start
1))
400 (error "too many dots in the name: ~S" pathname
))
401 (strings (unparse-physical-piece pathname-name
#\^
)))
403 (when (or (null pathname-type
) (eq pathname-type
:unspecific
))
405 (when (typep pathname-type
'simple-string
)
406 (when (position #\. pathname-type
)
407 (error "type component can't have a #\. inside: ~S" pathname
)))
409 (strings (unparse-physical-piece pathname-type
#\^
))))
410 (apply #'concatenate
'simple-string
(strings)))))
412 ;; FIXME: This has been converted rather blindly from the Unix
413 ;; version, with no reference to any Windows docs what so ever.
414 (defun simplify-win32-namestring (src)
415 (declare (type simple-string src
))
416 (let* ((src-len (length src
))
417 (dst (make-string src-len
:element-type
'character
))
421 (flet ((deposit (char)
422 (setf (schar dst dst-len
) char
)
426 (dotimes (src-index src-len
)
427 (let ((char (schar src src-index
)))
428 (cond ((char= char
#\.
)
435 ;; either ``/...' or ``...//...'
437 (setf last-slash dst-len
)
440 ;; either ``./...'' or ``..././...''
445 ((and last-slash
(not (zerop last-slash
)))
446 ;; There is something before this ..
447 (let ((prev-prev-slash
448 (position-if #'slashp dst
:end last-slash
:from-end t
)))
449 (cond ((and (= (+ (or prev-prev-slash
0) 2)
451 (char= (schar dst
(- last-slash
2)) #\.
)
452 (char= (schar dst
(1- last-slash
)) #\.
))
453 ;; The something before this .. is another ..
455 (setf last-slash dst-len
))
457 ;; The something is some directory or other.
462 (setf last-slash prev-prev-slash
)))))
464 ;; There is nothing before this .., so we need to keep it
465 (setf last-slash dst-len
)
468 ;; something other than a dot between slashes
469 (setf last-slash dst-len
)
474 (setf (schar dst dst-len
) char
)
477 (when (and last-slash
(not (zerop last-slash
)))
480 ;; We've got ``foobar/.''
483 ;; We've got ``foobar/..''
484 (unless (and (>= last-slash
2)
485 (char= (schar dst
(1- last-slash
)) #\.
)
486 (char= (schar dst
(- last-slash
2)) #\.
)
488 (slashp (schar dst
(- last-slash
3)))))
489 (let ((prev-prev-slash
490 (position-if #'slashp dst
:end last-slash
:from-end t
)))
492 (setf dst-len
(1+ prev-prev-slash
))
493 (return-from simplify-win32-namestring
494 (coerce ".\\" 'simple-string
)))))))))
495 (cond ((zerop dst-len
)
500 (subseq dst
0 dst-len
)))))