OS.FileSys
structure
signature OS_FILE_SYS
structure FileSys
: OS_FILE_SYS
The OS.FileSys
structure provides facilities for accessing and operating on the file system. These functions are designed to be portable across operating systems. They raise OS.SysErr
with an argument in case of errors.
Except for fullPath
and realPath
, functions taking a string argument will raise the OS.SysErr
exception if the argument string is empty.
It is expected that all functions taking a pathname as an argument (e.g., modTime
or OS.Process.system
) will resolve any components corresponding to symbolic links. The obvious exceptions to this rule are isLink
and readLink
, where only symbolic links appearing as directory components of the pathname are resolved.
Question:
We need a general discussion of dirstreams, working directory, directory structure, etc. The introduction should say something about the model of a file system that these functions use; what features they support and what examples of features that require an OS-specific library. We should also note that the particular semantics, especially concerning errors, is OS dependent.
type dirstream
val openDir : string -> dirstream
val readDir : dirstream -> string option
val rewindDir : dirstream -> unit
val closeDir : dirstream -> unit
val chDir : string -> unit
val getDir : unit -> string
val mkDir : string -> unit
val rmDir : string -> unit
val isDir : string -> bool
val isLink : string -> bool
val readLink : string -> string
val fullPath : string -> string
val realPath : string -> string
val modTime : string -> Time.time
val fileSize : string -> Position.int
val setTime : string * Time.time option -> unit
val remove : string -> unit
val rename : {old : string, new : string} -> unit
datatype access_mode = A_READ | A_WRITE | A_EXEC
val access : string * access_mode list -> bool
val tmpName : unit -> string
eqtype file_id
val fileId : string -> file_id
val hash : file_id -> word
val compare : file_id * file_id -> order
openDir path
readDir
, rewindDir
, and closeDir
. The stream reads the directory entries off the file system in some unspecified order. It raises SysErr
if, for example, the directory does not exist or is not accessible.
readDir dir
NONE
is returned. readDir
filters out the names corresponding to the current and parent arcs.
rewindDir dir
SysErr
in case of an operating system error, though, since the directory stream has already been opened, an error should not be likely.
closeDir dir
SysErr
. Closing a closed directory stream, however, has no effect.
chDir s
TextIO.openIn
and TextIO.openOut
, and functions defined in this structure. It raises SysErr
if, for example, the directory does not exist or is not readable.
The chDir
function will also change the current volume (on systems with volumes) if one is specified. This function does not allow the user to change the current working directory of another volume than the current volume, even on systems where this concept is otherwise supported.
val getDir : unit -> string
mkDir s
mkDir
raises SysErr
if, for example, the directory in which s is to be created does not exist or is not writable.
rmDir s
SysErr
if, for example, s does not exist or if the directory in which s resides is not writable, or if the directory is not empty.
isDir s
SysErr
if, for example, s does not exist or if the directory in which s resides is not accessible.
isLink s
true
if s names a symbolic link. It raises SysErr
if, for example, s does not exist or there is an access violation. On operating systems without symbolic links, it will always return false
unless an exception is raised first.
readLink s
SysErr
if, for example, s does not exist or is not a symbolic link, or there is an access violation. On operating systems without symbolic links, it raises SysErr
unconditionally.
The precise form of the returned string, in particular, whether it corresponds to an absolute or relative path, is system-dependent.
fullPath path
"."
. It raises SysErr
if, for example, a directory on the path, or the file or directory named, does not exist or is not accessible or if there is a link loop.
realPath path
realPath
acts like fullPath
. If path is relative and on the same volume as the current working directory, then it returns a path that is relative to the current working directory, but in which the symbolic links have been expanded. Otherwise, it raises OS.Path.Path
.
Implementation note:
This function can be implemented as follows:
fun realPath p = if OS.Path.isAbsolute p then fullPath p else OS.Path.mkRelative{ path=fullPath p, relativeTo=fullPath(getDir()) }
modTime path
SysErr
if, for example, path does not exist or if the directory in which path resides is not accessible.
fileSize path
SysErr
if, for example, path does not exist or if the directory in which path resides is not accessible.
setTime (path, opt)
SOME
(t)
, then the time t is used; otherwise the current time (i.e., Time.now
()
) is used. It raises SysErr
if path does not exist, the directory in which path resides is not accessible, or the user does not have the appropriate permission.
remove path
SysErr
if, path does not exist or is not writable, if the directory in which path resides is not writable, or file is a directory. Use the rmDir
function to delete directories.
If one removes a file that has been opened for reading or writing, the behavior of subsequent reads and writes is undefined. For example, removing the file may close all existing streams or generate an exception. The Unix idiom of opening a file and then removing it is not portable.
rename {old, new}
rename
does nothing. If a file called new exists, it is removed. It raises SysErr
if, for example, old does not exist, or if one of the directories in which old or new reside is not writable. This may also fail if old refers to an open file, or if old and new are on different file systems, i.e., if a copy is required.
access (path, accs)
A_READ
, A_WRITE
, or A_EXEC
, respectively, it tests whether the user process has read, write, or execute permission for the file, testing their conjunction if more than one are present. Note that access
is also implicitly testing the user's access to the parent directories of the file. The function will only raise OS.SysErr
for errors unrelated to resolving the pathname and the related permissions, such as being interrupted by a signal during the system call.
Implementation note:
On systems that do not support a notion of execution permissions, the
access
should accept but ignore theA_EXEC
value.
val tmpName : unit -> string
This function raises SysErr
if it cannot create the unique file or filename.
eqtype file_id
file_id
value should not be confused with the open file identifier OS.IO.iodesc
.
fileId path
file_id
value associated with the file system object designated by the pathname path. In particular, if fileId
p = fileId
p'
, then the paths p and p' refer to the same file system object. Note that if p is a symbolic link, then fileId
p =
fileId
(readLink
p)
.
hash fid
Implementation note:
hash
must have the property that values produced are well distributed when taken modulo 2(n) for any n.
compare (fid, fid')
LESS
, EQUAL
, or GREATER
when fid is less than, equal to, or greater than fid', respectively, in some underlying linear ordering on file_id
values.
BinIO
,OS
,OS.Path
,TextIO
For functions dealing with file attributes, such as fileSize
or rename
, the arguments can be directories as well as ordinary files.
Generated April 12, 2004
Last Modified July 14, 2003
Comments to John Reppy.
This document may be distributed freely over the internet as long as the copyright notice and license terms below are prominently displayed within every machine-readable copy.
Copyright © 2004 AT&T and Lucent Technologies. All rights reserved.
Permission is granted for internet users to make one paper copy for their
own personal use. Further hardcopy reproduction is strictly prohibited.
Permission to distribute the HTML document electronically on any medium
other than the internet must be requested from the copyright holders by
contacting the editors.
Printed versions of the SML Basis Manual are available from Cambridge
University Press.
To order, please visit
www.cup.org (North America) or
www.cup.cam.ac.uk (outside North America). |