Previous: Lisp Pathnames, Up: Pathnames


9.2 Native Filenames

In some circumstances, what is wanted is a Lisp pathname object which corresponds to a string produced by the Operating System. In this case, some of the default parsing rules are inappropriate: most filesystems do not have a native understanding of wild pathnames; such functionality is often provided by shells above the OS, often in mutually-incompatible ways.

To allow the user to deal with this, the following functions are provided: parse-native-namestring and native-pathname return the closest equivalent Lisp pathname to a given string (appropriate for the Operating System), while native-namestring converts a non-wild pathname designator to the equivalent native namestring, if possible. Some Lisp pathname concepts (such as the :back directory component) have no direct equivalents in most Operating Systems; the behaviour of native-namestring is unspecified if an inappropriate pathname designator is passed to it. Additionally, note that conversion from pathname to native filename and back to pathname should not be expected to preserve equivalence under equal.

— Function: sb-ext:parse-native-namestring thing &optional host defaults &key start end junk-allowed as-directory

Convert thing into a pathname, using the native conventions appropriate for the pathname host host, or if not specified the host of defaults. If thing is a string, the parse is bounded by start and end, and error behaviour is controlled by junk-allowed, as with parse-namestring. For file systems whose native conventions allow directories to be indicated as files, if as-directory is true, return a pathname denoting thing as a directory.

— Function: sb-ext:native-pathname pathspec

Convert pathspec (a pathname designator) into a pathname, assuming the operating system native pathname conventions.

— Function: sb-ext:native-namestring pathname &key as-file

Construct the full native (name)string form of pathname. For file systems whose native conventions allow directories to be indicated as files, if as-file is true and the name, type, and version components of pathname are all nil or :unspecific, construct a string that names the directory according to the file system's syntax for files.

Because some file systems permit the names of directories to be expressed in multiple ways, it is occasionally necessary to parse a native file name “as a directory name” or to produce a native file name that names a directory “as a file”. For these cases, parse-native-namestring accepts the keyword argument as-directory to force a filename to parse as a directory, and native-namestring accepts the keyword argument as-file to force a pathname to unparse as a file. For example,

     ; On Unix, the directory "/tmp/" can be denoted by "/tmp/" or "/tmp".
     ; Under the default rules for native filenames, these parse and
     ; unparse differently.
     (defvar *p*)
     (setf *p* (parse-native-namestring "/tmp/")) ⇒ #P"/tmp/"
     (pathname-name *p*) ⇒ NIL
     (pathname-directory *p*) ⇒ (:ABSOLUTE "tmp")
     (native-namestring *p*) ⇒ "/tmp/"
     
     (setf *p* (parse-native-namestring "/tmp")) ⇒ #P"/tmp"
     (pathname-name *p*) ⇒ "tmp"
     (pathname-directory *p*) ⇒ (:ABSOLUTE)
     (native-namestring *p*) ⇒ "/tmp"
     
     ; A non-NIL AS-DIRECTORY argument to PARSE-NATIVE-NAMESTRING forces
     ; both the second string to parse the way the first does.
     (setf *p* (parse-native-namestring "/tmp"
                                        nil *default-pathname-defaults*
                                        :as-directory t)) ⇒ #P"/tmp/"
     (pathname-name *p*) ⇒ NIL
     (pathname-directory *p*) ⇒ (:ABSOLUTE "tmp")
     
     ; A non-NIL AS-FILE argument to NATIVE-NAMESTRING forces the pathname
     ; parsed from the first string to unparse as the second string.
     (setf *p* (parse-native-namestring "/tmp/")) ⇒ #P"/tmp/"
     (native-namestring *p* :as-file t) ⇒ "/tmp"