Pathnames
Each file has a pathname, pointing to an inode.
All inodes count the number of files they are being pointed by.
All inodes have a type.
All inodes have a mode.
All inodes have a reference to the device hosting the filesystem they belong to.
A device ID is a pair of identifiers:
- The major ID identifies the class of the device.
- The minor ID identifies the instance of the class.
All inodes have a numeric id. This id is unique within the filesystem.
Each inode specifies the blocksize for input/output operations.
Stat
struct stat
{
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_uid;
dev_t st_rdev;
off_t st_size;
blksize_t st_blksize;
blkcount_t st_blocks;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
}
Hard Links and Symbolic Links
#include <unistd.h>
int link(const char *oldpath, const char *newpath);
int symlink(const char *oldpath, const char *newpath);
Creates a new link to a file.
A hard link:
- requires the existence of the original file
- garantees the existence of a file even after the original is removed.
A symbolic link:
- does not require the existence of the original file
- if the original path points to nothing, also points to nothing
- can cross file system boundaries, but normal permission checks are made on each component during pathname resolution.
Some system calls in UNIX/Linux systems have a pathname as parameter.
A pathname is a strings that resolves to a file.
A component of a pathname is a sub-string delimited by '/'.
Recall also that:
- All directories have an entry named '.' that refers to the directory itself.
- All direstories have an entry named '..' that referes to the parent directory.
Step 1: determine the starting lookup directory
1.1) Absolute pathnames
An absolute pathname is pathname starts with '/'.
The starting lookup directory is the root directory of the calling process.
1.2) Relative pathnames
A relative pathname is a pathname that does not start with '/'.
The starting lookup directory is the current working directory of the calling process.
Step 2: walk the path components
To walk the path, we start by declaring a current lookup directory.
We initialize the current lookup directory to the starting lookup directory.
Then, we iterate along the components of the pathname.
Unless the current component is the last component, we do the following.
If the current lookup directory is a symbolic link, we replace it with the resolution of the link.
Then if the current lookup directory contains the current component and the current component is a directory, we set the current lookup directory to the current component, otherwise we return an error.
When the current component is the last,