[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

General Programming Concepts:
Writing and Debugging Programs

JFS2 Directories

Directories provide a hierarchical structure to the file system and link file and subdirectory names to i-nodes. There is no limit on the depth of nested directories. Disk space is allocated for directories in file system blocks.

Processes can read directories as regular files. However, the kernel reserves the right to write directories. For this reason, directories are created and maintained by a set of subroutines unique to them.

JFS2 Directory Structures

A directory contains entries which indicate the objects contained in the directory. A directory entry has a fixed length. It contains the i-node number, the name up to 22 bytes long, a name length field, and a field to continue the entry if the name won't fit completely.

The directory entries are stored in a B+ tree sorted by name. The self (.) and parent (..) information will be contained in the i-node instead of a directory entry.

Access modes for directories have the following meanings:

read Allows a process to read directory entries
write Allows a process to create new directory entries or remove old ones by using the creat, mknod, link, and unlink subroutines
execute Allows a process to use the directory as a current working directory or to search below the directory in the file tree

Working with Directories (Programming)

The mkdir and rmdir subroutines create and remove directories, respectively.

The opendir, readdir, telldir, seekdir, rewinddir and closedir subroutines manipulate directories. The opendir subroutine returns a structure pointer that is used by the readdir subroutine to obtain the next directory entry, by rewinddir to reset the read position to the beginning, and by closedir to close the directory. The seekdir subroutine returns to a position previously obtained with the telldir subroutine. In earlier versions, programs treated directories as regular files and used the open, read, lseek, and close subroutines to access them. This is no longer recommended.

Changing Current Directory of a Process

When the system is booted, the first process uses the root directory of the root file system as its current directory. New processes created with the fork subroutine inherit the current directory used by the parent process. The chdir subroutine changes the current directory of a process.

The chdir subroutine parses the path name to ensure that the target file is a directory and that the process owner has permissions to the directory. After the chdir subroutine is run, the process uses the new current directory to search all path names that do not begin with a / (slash).

Changing the Root Directory of a Process

Processes can change their understanding of the root directory through the chroot subroutine. Child processes of the calling process consider the directory indicated by the chroot subroutine as the logical root directory of the file system.

Processes use the global file system root directory for all path names starting with a / (slash).All path name searches beginning with a / (slash) begin at this new root directory.

Subroutines That Control Directories

Due to the unique nature of directory files, directories are controlled by a special set of subroutines. The following subroutines are designed to control directories:

chdir Changes the current working directory
chroot Changes the effective root directory
opendir, readdir, telldir, seekdir, rewinddir, or closedir
  Perform various actions on directories
getcwd or getwd Gets path to current directory
mkdir Creates a directory
rename Renames a directory
rmdir Removes a directory

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]