NEXT UP previous
Next: Questions

File Hierarchy

In order to create, modify and maintain the Linux file and directory hierarchy it is necessary to be able to create and remove directories, and create and remove links in directories to files. All of these things are easy to achieve, especially since they all have their own system calls.

mkdir and rmdir System Calls

A Linux process can create a directory with the mkdir() system call. The prototype for the call is:

	#include <sys/types.h> 
	#include <fcntl.h>
	#include <unistd.h>

	int mkdir(char *pathname, mode_t mode);

assuming your process has appropriate permissions, a call to mkdir() will create a new directory named pathname with its permission bits set to mode. The value of the mode parameter is modified by the current umask value in the usual way. The new directory will be owned by the effective user ID of the calling process and it will automatically be initialized to contain the dot and dot-dot entries.

If a directory is empty (apart from the dot and dot-dot entries) then it can be removed from the directory hierarchy with the rmdir() system call:

	#include <unistd.h>

	int rmdir(char *pathname);

Directory Access

Because of the special importance of directories in the file system hierarchy, special system calls are required to access their contents. To open a directory you need to use the opendir() system call which has the prototype:

	#include <dirent.h>

	DIR *opendir(char *pathname);

This call opens the specified directory and returns a directory pointer if successful or zero on error. The directory pointer is passed into the related system calls, the main ones being readdir() and closedir().

The readdir() system call has the prototype:

	#include <dirent.h>

	struct dirent *readdir(DIR *dirptr);

This call returns a pointer to a struct dirent which contains the details of the next link in the specified directory. The name of the file in the link is stored in the d_name element of the structure, which is a char array. Calling readdir() repeatedly will result in a sequential walk throught the links in the directory. A zero value is returned when there are no more links left to read.

Once you have finished with a directory, it can be closed with the closedir() system call. This call has the prototype:

	#include <dirent.h>

	int closedir(DIR *dirptr);

link and unlink System Calls

A directory entry for a file is called a link. A link to a file is automatically generated from its given pathname when the file is first created. Thereafter, extra links to the file can be generated by the link() system call:

	#include <unistd.h>

	int link(char *pathname1, char *pathname2);

A new directory link, called pathname2, is created to point to the existing file pathname1. In order for the link() call to work, both pathname1 and pathname2 must be on the same filesystem and the calling process must have appropriate permission to write to the target directory.

Ordinary file links can be removed with the unlinkO system call.

	#include <unistd.h>

	int unlink(char *pathname)

This call removes the specified directory link and decrements the link count to the file in its mode. After this, if the link count has gone to zero then the mode and the file's data blocks are all freed back to the system. If some process should happen to have the file open when it is scheduled for removal then the recovery of the mode and data blocks will be delayed until the process terminates, though the directory link will still be removed so that no other process can open() the file and further prolong its life.


NEXT UP previous
Next: Questions