Node:File System, Next:, Previous:Ports and File Descriptors, Up:POSIX

38.3 File System

These procedures allow querying and setting file system attributes (such as owner, permissions, sizes and types of files); deleting, copying, renaming and linking files; creating and removing directories and querying their contents; syncing the file system and creating special files.

access? path how Scheme Procedure
scm_access (path, how) C Function
Return #t if path corresponds to an existing file and the current process has the type of access specified by how, otherwise #f. how should be specified using the values of the variables listed below. Multiple values can be combined using a bitwise or, in which case #t will only be returned if all accesses are granted.

Permissions are checked using the real id of the current process, not the effective id, although it's the effective id which determines whether the access would actually be granted.

R_OK Variable
test for read permission.

W_OK Variable
test for write permission.

X_OK Variable
test for execute permission.

F_OK Variable
test for existence of the file.

stat object Scheme Procedure
scm_stat (object) C Function
Return an object containing various information about the file determined by obj. obj can be a string containing a file name or a port or integer file descriptor which is open on a file (in which case fstat is used as the underlying system call).

The object returned by stat can be passed as a single parameter to the following procedures, all of which return integers:

The device containing the file.
The file serial number, which distinguishes this file from all other files on the same device.
The mode of the file. This includes file type information and the file permission bits. See stat:type and stat:perms below.
The number of hard links to the file.
The user ID of the file's owner.
The group ID of the file.
Device ID; this entry is defined only for character or block special files.
The size of a regular file in bytes.
The last access time for the file.
The last modification time for the file.
The last modification time for the attributes of the file.
The optimal block size for reading or writing the file, in bytes.
The amount of disk space that the file occupies measured in units of 512 byte blocks.

In addition, the following procedures return the information from stat:mode in a more convenient form:

A symbol representing the type of file. Possible values are regular, directory, symlink, block-special, char-special, fifo, socket and unknown
An integer representing the access permission bits.

lstat str Scheme Procedure
scm_lstat (str) C Function
Similar to stat, but does not follow symbolic links, i.e., it will return information about a symbolic link itself, not the file it points to. path must be a string.

readlink path Scheme Procedure
scm_readlink (path) C Function
Return the value of the symbolic link named by path (a string), i.e., the file that the link points to.

chown object owner group Scheme Procedure
scm_chown (object, owner, group) C Function
Change the ownership and group of the file referred to by object to the integer values owner and group. object can be a string containing a file name or, if the platform supports fchown, a port or integer file descriptor which is open on the file. The return value is unspecified.

If object is a symbolic link, either the ownership of the link or the ownership of the referenced file will be changed depending on the operating system (lchown is unsupported at present). If owner or group is specified as -1, then that ID is not changed.

chmod object mode Scheme Procedure
scm_chmod (object, mode) C Function
Changes the permissions of the file referred to by obj. obj can be a string containing a file name or a port or integer file descriptor which is open on a file (in which case fchmod is used as the underlying system call). mode specifies the new permissions as a decimal number, e.g., (chmod "foo" #o755). The return value is unspecified.

utime pathname [actime [modtime]] Scheme Procedure
scm_utime (pathname, actime, modtime) C Function
utime sets the access and modification times for the file named by path. If actime or modtime is not supplied, then the current time is used. actime and modtime must be integer time values as returned by the current-time procedure.
(utime "foo" (- (current-time) 3600))
will set the access time to one hour in the past and the modification time to the current time.

delete-file str Scheme Procedure
scm_delete_file (str) C Function
Deletes (or "unlinks") the file specified by path.

copy-file oldfile newfile Scheme Procedure
scm_copy_file (oldfile, newfile) C Function
Copy the file specified by path-from to path-to. The return value is unspecified.

rename-file oldname newname Scheme Procedure
scm_rename (oldname, newname) C Function
Renames the file specified by oldname to newname. The return value is unspecified.

link oldpath newpath Scheme Procedure
scm_link (oldpath, newpath) C Function
Creates a new name newpath in the file system for the file named by oldpath. If oldpath is a symbolic link, the link may or may not be followed depending on the system.

symlink oldpath newpath Scheme Procedure
scm_symlink (oldpath, newpath) C Function
Create a symbolic link named path-to with the value (i.e., pointing to) path-from. The return value is unspecified.

mkdir path [mode] Scheme Procedure
scm_mkdir (path, mode) C Function
Create a new directory named by path. If mode is omitted then the permissions of the directory file are set using the current umask. Otherwise they are set to the decimal value specified with mode. The return value is unspecified.

rmdir path Scheme Procedure
scm_rmdir (path) C Function
Remove the existing directory named by path. The directory must be empty for this to succeed. The return value is unspecified.

opendir dirname Scheme Procedure
scm_opendir (dirname) C Function
Open the directory specified by path and return a directory stream.

directory-stream? obj Scheme Procedure
scm_directory_stream_p (obj) C Function
Return a boolean indicating whether object is a directory stream as returned by opendir.

readdir port Scheme Procedure
scm_readdir (port) C Function
Return (as a string) the next directory entry from the directory stream stream. If there is no remaining entry to be read then the end of file object is returned.

rewinddir port Scheme Procedure
scm_rewinddir (port) C Function
Reset the directory port stream so that the next call to readdir will return the first directory entry.

closedir port Scheme Procedure
scm_closedir (port) C Function
Close the directory stream stream. The return value is unspecified.

Here is an example showing how to display all the entries in a directory:

(define dir (opendir "/usr/lib"))
(do ((entry (readdir dir) (readdir dir)))
    ((eof-object? entry))
  (display entry)(newline))
(closedir dir)

sync Scheme Procedure
scm_sync () C Function
Flush the operating system disk buffers. The return value is unspecified.

mknod path type perms dev Scheme Procedure
scm_mknod (path, type, perms, dev) C Function
Creates a new special file, such as a file corresponding to a device. path specifies the name of the file. type should be one of the following symbols: regular, directory, symlink, block-special, char-special, fifo, or socket. perms (an integer) specifies the file permissions. dev (an integer) specifies which device the special file refers to. Its exact interpretation depends on the kind of special file being created.


(mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2))

The return value is unspecified.

tmpnam Scheme Procedure
scm_tmpnam () C Function
Return a name in the file system that does not match any existing file. However there is no guarantee that another process will not create the file after tmpnam is called. Care should be taken if opening the file, e.g., use the O_EXCL open flag or use mkstemp! instead.

mkstemp! tmpl Scheme Procedure
scm_mkstemp (tmpl) C Function
Create a new unique file in the file system and returns a new buffered port open for reading and writing to the file. tmpl is a string specifying where the file should be created: it must end with XXXXXX and will be changed in place to return the name of the temporary file.

dirname filename Scheme Procedure
scm_dirname (filename) C Function
Return the directory name component of the file name filename. If filename does not contain a directory component, . is returned.

basename filename [suffix] Scheme Procedure
scm_basename (filename, suffix) C Function
Return the base name of the file name filename. The base name is the file name without any directory components. If suffix is provided, and is equal to the end of basename, it is removed also.
(basename "/tmp/test.xml" ".xml")
=> "test"