Posix.FileSys
structure
signature POSIX_FILE_SYS
structure FileSys
: POSIX_FILE_SYS
The structure Posix.FileSys
provides access to file system operations as described in Section 5 of the POSIX standard 1003.1,1996[CITE].
eqtype uid
eqtype gid
eqtype file_desc
val fdToWord : file_desc -> SysWord.word
val wordToFD : SysWord.word -> file_desc
val fdToIOD : file_desc -> OS.IO.iodesc
val iodToFD : OS.IO.iodesc -> file_desc option
type dirstream
val opendir : string -> dirstream
val readdir : dirstream -> string option
val rewinddir : dirstream -> unit
val closedir : dirstream -> unit
val chdir : string -> unit
val getcwd : unit -> string
val stdin : file_desc
val stdout : file_desc
val stderr : file_desc
structure S : sig
eqtype mode
include BIT_FLAGS
where type flags = mode
val irwxu : mode
val irusr : mode
val iwusr : mode
val ixusr : mode
val irwxg : mode
val irgrp : mode
val iwgrp : mode
val ixgrp : mode
val irwxo : mode
val iroth : mode
val iwoth : mode
val ixoth : mode
val isuid : mode
val isgid : mode
end
structure O : sig
include BIT_FLAGS
val append : flags
val excl : flags
val noctty : flags
val nonblock : flags
val sync : flags
val trunc : flags
end
datatype open_mode
= O_RDONLY
| O_WRONLY
| O_RDWR
val openf : string * open_mode * O.flags -> file_desc
val createf : string * open_mode * O.flags * S.mode
-> file_desc
val creat : string * S.mode -> file_desc
val umask : S.mode -> S.mode
val link : {old : string, new : string} -> unit
val mkdir : string * S.mode -> unit
val mkfifo : string * S.mode -> unit
val unlink : string -> unit
val rmdir : string -> unit
val rename : {old : string, new : string} -> unit
val symlink : {old : string, new : string} -> unit
val readlink : string -> string
eqtype dev
val wordToDev : SysWord.word -> dev
val devToWord : dev -> SysWord.word
eqtype ino
val wordToIno : SysWord.word -> ino
val inoToWord : ino -> SysWord.word
structure ST : sig
type stat
val isDir : stat -> bool
val isChr : stat -> bool
val isBlk : stat -> bool
val isReg : stat -> bool
val isFIFO : stat -> bool
val isLink : stat -> bool
val isSock : stat -> bool
val mode : stat -> S.mode
val ino : stat -> ino
val dev : stat -> dev
val nlink : stat -> int
val uid : stat -> uid
val gid : stat -> gid
val size : stat -> Position.int
val atime : stat -> Time.time
val mtime : stat -> Time.time
val ctime : stat -> Time.time
end
val stat : string -> ST.stat
val lstat : string -> ST.stat
val fstat : file_desc -> ST.stat
datatype access_mode = A_READ | A_WRITE | A_EXEC
val access : string * access_mode list -> bool
val chmod : string * S.mode -> unit
val fchmod : file_desc * S.mode -> unit
val chown : string * uid * gid -> unit
val fchown : file_desc * uid * gid -> unit
val utime : string
* {actime : Time.time, modtime : Time.time} option
-> unit
val ftruncate : file_desc * Position.int -> unit
val pathconf : string * string -> SysWord.word option
val fpathconf : file_desc * string -> SysWord.word option
eqtype uid
Posix.ProcEnv.uid
.
eqtype gid
Posix.ProcEnv.gid
.
eqtype file_desc
val fdToWord : file_desc -> SysWord.word
val wordToFD : SysWord.word -> file_desc
file_desc
value that is not accessible, but it cannot do this for any file_desc
that has ever been made concrete by fdToWord
. Also, there is no validation that the file descriptor created by wordToFD
corresponds to an actually open file.
val fdToIOD : file_desc -> OS.IO.iodesc
val iodToFD : OS.IO.iodesc -> file_desc option
iodToFD
returns an option
type because, on certain systems, some open I/O devices are not associated with an underlying open file descriptor.
type dirstream
OS.FileSys.dirstream
.
opendir dirName
readdir dir
NONE
is returned. Entries for "."
(current directory) and ".."
(parent directory) are never returned.
Rationale:
The reason for filtering out the current and parent directory entries is that it makes recursive walks of a directory tree easier.
rewinddir d
closedir d
dirstream
does not raise an exception.
chdir s
val getcwd : unit -> string
val stdin : file_desc
val stdout : file_desc
val stderr : file_desc
structure S
eqtype mode
mode
is a set of (read, write, execute) permissions for the owner of the file, members of the file's group, and others.
val irwxu : mode
val irusr : mode
val iwusr : mode
val ixusr : mode
val irwxg : mode
val irgrp : mode
val iwgrp : mode
val ixgrp : mode
val irwxo : mode
val iroth : mode
val iwoth : mode
val ixoth : mode
val isuid : mode
val isgid : mode
structure O
Posix.FileSys.O
contains file status flags used in calls to openf
.
val append : flags
val excl : flags
val noctty : flags
val nonblock : flags
val sync : flags
writeVec
, writeArr
, ftruncate
, openf
with trunc
), the calling process is assured that all data for the file has been written to permanent storage, even if the file is also open for deferred update.
val trunc : flags
datatype open_mode
= O_RDONLY
| O_WRONLY
| O_RDWR
openf (s, om, f)
createf (s, om, f, m)
These calls open a file named s for reading, writing, or both (depending on the open mode om). The flags f specify the state of the open file. If the file does not exist, openf
raises the OS.SysErr
exception whereas createf
creates the file, setting its protection mode to m (as modified by the umask
).
Note that, in C, the roles of openf
and createf
are combined in the function open
. The first acts like open
without the O_CREAT
flag; the second acts like open
with the O_CREAT
flag and the specified permission mode. Also, the createf
function should not be confused with the creat
function below, which behaves like its C namesake.
creat (s, m)
umask
). This is equivalent to the expression:
createf(s,O_WRONLY,O.trunc,m)
umask cmask
Whenever a file is created (by openf
, creat
, mkdir
, etc.), all file permission set in the file mode creation mask are removed from the mode of the created file. This clearing allows users to restrict the default access to their files.
The mask is inherited by child processes.
link {old, new}
Both old and new must reside on the same file system. A hard link to a directory cannot be created.
Upon successful completion, link
updates the file status change time of the old file, and updates the file status change and modification times of the directory containing the new entry. (See Posix.FileSys.ST
.)
mkdir (s, m)
umask
).
mkfifo (s, m)
umask
).
unlink path
When all links to a file are removed and no process has the file open or mapped, all resources associated with the file are reclaimed, and the file is no longer accessible. If one or more processes have the file open or mapped when the last link is removed, the link is removed before unlink
returns, but the removal of the file contents is postponed until all open or map references to the file are removed. If the path parameter names a symbolic link, the symbolic link itself is removed.
rmdir s
rename {old, new}
symlink {old, new}
readlink s
eqtype dev
ino
) uniquely identify a file.
val wordToDev : SysWord.word -> dev
val devToWord : dev -> SysWord.word
dev
values and words by which the operating system identifies a device. There is no verification that a value created by wordToDev
corresponds to a to a valid device identifier.
eqtype ino
val wordToIno : SysWord.word -> ino
val inoToWord : ino -> SysWord.word
ino
values and words by which the operating system identifies an inode. There is no verification that a value created by wordToIno
corresponds to a to a valid inode.
structure ST
type stat
val isDir : stat -> bool
val isChr : stat -> bool
val isBlk : stat -> bool
val isReg : stat -> bool
val isFIFO : stat -> bool
val isLink : stat -> bool
val isSock : stat -> bool
true
if the file described by the parameter is, respectively, a directory, a character special device, a block special device, a regular file, a FIFO, a symbolic link, or a socket.
mode st
val ino : stat -> ino
val dev : stat -> dev
nlink st
val uid : stat -> uid
val gid : stat -> gid
size st
val atime : stat -> Time.time
val mtime : stat -> Time.time
val ctime : stat -> Time.time
val stat : string -> ST.stat
val lstat : string -> ST.stat
val fstat : file_desc -> ST.stat
stat
and lstat
, the object is specified by its pathname. Note that an empty string causes an exception. For fstat
, an open file descriptor is supplied.
lstat
differs from stat
in that, if the pathname argument is a symbolic link, the information concerns the link itself, not the file to which the link points.
datatype access_mode = A_READ | A_WRITE | A_EXEC
OS.FileSys.access_mode
.
access (s, l)
A_READ
, it checks for the readability of s based on the real user and group IDs of the process; and so on.
The value returned depends only the appropriate privileges of the process and the permissions of the file. A directory may be indicated as writable by access
, but an attempt to open it for writing will fail (although files may be created there). A file's permissions may indicate that it is executable, but the exec
can fail if the file is not in the proper format. Conversely, if the process has appropriate privileges, access
will return true
if none of the appropriate file permissions are set.
chmod (s, mode)
fchmod (fd, mode)
chown (s, uid, gid)
fchown (fd, uid, gid)
utime (f, SOME{actime,modtime})
utime (f, NONE)
ftruncate (fd, n)
pathconf (s, p)
fpathconf (fd, p)
NONE
is returned. If the value is bounded, SOME
(v)
is returned, where v is the value. For boolean-value properties, if the value is true, SOME
(1)
is returned; otherwise, SOME
(0)
or NONE
is returned. The OS.SysErr
exception is raised if something goes wrong, including when p is not a valid property or when the implementation does not associate the property with the file.
In the case of pathconf
, read, write, or execute permission of the named file is not required, but all directories in the path leading to the file must be searchable.
The properties required by POSIX are described below. A given implementation may support additional properties.
"CHOWN_RESTRICTED"
chown
on any files (other than directories) in the specified directory is restricted to processes with appropriate privileges. This property only applies to directories.
"LINK_MAX"
ST.nlink
function.
"MAX_CANON"
"MAX_INPUT"
"NAME_MAX"
"NO_TRUNC"
"NAME_MAX"
causes an error; false if long filenames are truncated. This property only applies to directories.
"PATH_MAX"
"PIPE_BUF"
"VDISABLE"
ord(c)
of the character c
which can be used to disable the terminal special characters specified in Posix.TTY.V
. This property only applies to terminal devices.
"ASYNC_IO"
"SYNC_IO"
"PRIO_IO"
Implementation note:
An implementation can call the operating system's
pathconf
orfpathconf
functions, which return an integer. If the returned value is -1 anderrno
has been set, an exception is raised. Otherwise, a returned value of -1 should be mapped toNONE
, and other values should be wrapped inSOME
and returned.
Rationale:
The encoding of boolean values as
int option
, with false having two values, is an unpleasant choice. It would be preferable to split these two functions into four, with one pair handling integer-valued properties, with the present return type, and the other pair handling boolean-valued properties, returning values of typebool
. Unfortunately, the nature of the POSIXpathconf
andfpathconf
functions would make this a nightmare for the implementor.First, the specification of these functions provides a non-negative integer return value for both booleans and numbers. System include files provide no inherent information as to the type of a property. Although the basic properties specified by POSIX have fixed types, each system is allowed to add its own non-standard properties. Thus, for an SML implementation to make the distinction, it would have to rely on somehow gleaning the information from, e.g., system-specific manual pages.
In addition, the POSIX specification is unclear on how boolean values are encoded. Some systems return 0 for false; others appear to return -1 without setting
errno
. Technically, the latter value may be interpreted as meaning that the property value is unknown or unspecified. From the programmer's point of view, this means that the property is not usable.This situation probably precludes automatically generating these functions on a per system basis. Given this, the current return types and values appear to be the only reasonable choice.
BIT_FLAGS
,OS.FileSys
,Posix
,Posix.IO
,Posix.ProcEnv
,Posix.Process
Generated October 02, 2003
Last Modified July 1, 2002
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 © 2003 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). |