Scheme 48 Manual | Contents | In Chapter: Access to POSIX
Previous: OS and machine identification | Next: Time

Files and directories

These procedures are in structures posix-files and posix.

Directory streams

Directory streams are like input ports, with each read operation returning the next name in the directory.

Open-directory-stream opens a new directory stream. Directory-stream? is a predicate that recognizes directory streams. Read-directory-stream returns the next name in the directory or #f if all names have been read. Close-directory-stream closes a directory stream. This is the obvious utility; it returns a list of the names in directory name.

Working directory

These return and set the working directory.

File creation and removal

Open-file opens a port to the file named by string path. The file-options argument determines various aspects of the returned port. The optional file-mode argument is used only if the file to be opened does not already exist. The returned port is an input port if file-options includes read-only; otherwise it returns an output port. Dup-switching-mode can be used to open an input port for output ports opened with the read/write option. The syntax file-options returns a file-option with the indicated options set. File-options-on? returns true if its first argument includes all of the options listed in the second argument. The following file options may be used with open-file.
create create file if it does not already exist; a file-mode argument is required with this option
exclusive an error will be raised if this option and create are both set and the file already exists
no-controlling-tty if path is a terminal device this option causes the terminal to not become the controlling terminal of the process
truncate file is truncated
append writes are appended to existing contents
nonblocking read and write operations do not block
read-only port may not be written
read-write file descriptor may be read or written
write-only port may not be read
Only one of the last three options may be used.

For example

(open-file "some-file.txt"
           (file-options create write-only)
           (file-mode read owner-write))
returns an output port that writes to a newly-created file that can be read by anyone and written only by the owner. Once the file exists,
(open-file "some-file.txt"
           (file-options append write-only))
will open an output port that appends to the file.

The append and nonblocking options and the read/write nature of the port can be read using i/o-flags. The append and nonblocking options can be set using set-i/o-flags!.

To keep port operations from blocking the Scheme 48 process, output ports are set to be nonblocking at the time of creation (input ports are managed using select()). You can use set-i/o-flags! to make an output port blocking, for example just before a fork, but care should be exercised. The Scheme 48 runtime code may get confused if an I/O operation blocks.

Sets the file creation mask to be file-mode. Bits set in file-mode are cleared in the modes of any files or directories created by the current process. Link makes path new be a new link to the file pointed to by path existing. The two paths must be in the same file system. These two procedures make new directories and fifo files. Unlink removes the link indicated by path. Remove-directory removes the indicated (empty) directory. Rename moves the file pointed to by old-path to the location pointed to by new-path (the two paths must be in the same file system). Any other links to the file remain unchanged. Accessible? returns true if path is a file that can be accessed in the listed mode. If more than one mode is specified accessible? returns true if all of the specified modes are permitted. The mode-names are: read, write, execute, exists.

File information

Get-file-info and get-file/link-info both return a file info record for the named file. Get-file-info follows symbolic links while get-file/link-info does not. Get-port-info returns a file info record for the file which port reads from or writes to. An error is raised if fd-port does not read from or write to a file descriptor. File-info? is a predicate for file-info records. File-info-name is the name which was used to get file-info, either as passed to get-file-info or get-file/link-info, or used to open the port passed to get-port-info. File-info-type returns the type of the file, as a file-type object File types may be compared using eq?. The valid file types are:
regular
directory
character-device
block-device
fifo
symbolic-link
socket
other

Symbolic-link and socket are not required by POSIX.

The device and inode numbers uniquely determine a file. These return the number of links to a file and the file size in bytes. The size is only meaningful for regular files. These return the owner, group, and access mode of a file. These return the time the file was last read, modified, or had its status modified

File modes

A file mode is a boxed integer representing a file protection mask.

File-mode is syntax for creating file modes. The mode-names are listed below. File-mode? is a predicate for file modes. File-mode+ returns a mode that contains all of permissions of its arguments. File-mode- returns a mode that has all of the permissions of file-mode0 that are not in file-mode1. File-mode=? returns true if the two modes are exactly the same. File-mode<=? returns true if file-mode0 has a subset of the permissions of file-mode1. File-mode>=? is file-mode<=? with the arguments reversed. Integer->file-mode and file-mode->integer translate file modes to and from the classic Unix file mode masks. These may not be the masks used by the underlying OS.
Permission name Bit mask
set-uid #o4000 set user id when executing
set-gid #o2000 set group id when executing
owner-read #o0400 read by owner
owner-write #o0200 write by owner
owner-exec #o0100 execute (or search) by owner
group-read #o0040 read by group
group-write #o0020 write by group
group-exec #o0010 execute (or search) by group
other-read #o0004 read by others
other-write #o0002 write by others
other-exec #o0001 execute (or search) by others
Names for sets of permissions
owner #o0700 read, write, and execute by owner
group #o0070 read, write, and execute by group
other #o0007 read, write, and execute by others
read #o0444 read by anyone
write #o0222 write by anyone
exec #o0111 execute by anyone
all #o0777 anything by anyone

Previous: OS and machine identification | Next: Time