From b05cf0346fbfb0a29c81fb94845b08720fe863e7 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 25 Jan 2024 17:21:33 +0100 Subject: [PATCH] Turn exercise about number of path into regular text --- Unix_Concepts.m4 | 91 +++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 52 deletions(-) diff --git a/Unix_Concepts.m4 b/Unix_Concepts.m4 index be51cc9..ab6cdbb 100644 --- a/Unix_Concepts.m4 +++ b/Unix_Concepts.m4 @@ -638,6 +638,24 @@ directory contains two subdirectories named . and .. which refer to the directory itself and its parent directory, respectively.

+

One consequence of the special meaning of the path components +. and .. is that there are infinitely +many paths which refer to the same file. Indeed, given the path +/foo/bar, one may construct different paths which +refer to the same file by inserting any number of /. +or ../foo after the first component. For example, +/foo/./bar and /foo/../foo/bar both refer +to the same file. If relative paths have to be taken into account as +well, even more paths can be constructed.

+ +

This illustrates the fundamental difference between a path and a +file. Paths can be mapped to files, but not the other way around. In +particular, there is no such thing like "the list of paths which +have changed since yesterday". Also, it is incorrect to assume +that two files are different just because their paths are different. +The concept of hard- and soft links discussed below further complicates +the relationship between paths and files.

+ SUBSECTION(«Globbing»)

Globbing, also known as pathname expansion, is a pattern @@ -663,7 +681,6 @@ the prompt. However, POSIX also demands system functions which make globbing available to other applications. These are implemented as part of libc.

-

There are a few quirks related to globbing which are worth to point out. First, if no valid path matches the given pattern, the expansion of the pattern is, by definition according to POSIX, the @@ -1005,6 +1022,27 @@ to traverse the whole hierarchy of files. This may be prohibitive for large filesystems, and the result is unreliable anyway unless the filesystems are mounted read-only.

+

It is possible to choose among all possible paths which refer +to the same file a canonical path, that is, a shortest +(counting characters) absolute path which does not contain any soft +links. In fact, the POSIX standard requires each Unix system library +to provide the realpath() library function which performs +the following substitutions on the given path: First, the path to +the current working directory is prepended if the given path is +relative (does not begin with a slash). Second, symbolic links are +replaced by their targets. Third, any occurrences of /. +and foo/.. are removed. The thusly transformed path is +returned by the function as the canonical path.

+ +

Each path can be canonicalized in this way, but not all paths +which refer to the same file give rise to the same canonical path. For +example, /tmp/foo and /tmp/bar could refer +to regular files which are hard links of each other. In this case the +paths refer to the same file, yet the paths are different and already +canonicalized. The same can happen when a file system (or a subtree +of it) is bind mounted. That is, the file system tree is +visible at two or more locations in the global directory tree.

+ EXERCISES() -HOMEWORK(« -How many paths are there that refer to the same file? -», « -Given the path /foo/bar, one may construct different paths -which refer to the same file by inserting any number of /. -or ../foo after the first component. For example, -/foo/./bar and /foo/../foo/bar both refer -to the same file. If relative paths have to be taken into account as -well, even more paths can be constructed easily. Hence the answer is: -arbitrary many. - -This illustrates the fundamental difference between a path and a -file. Paths can be mapped to files, but not the other way around. In -particular, there is no such thing like "the list of paths which have -changed since yesterday". - -The concept of hard- and soft links complicates the situation further. -») - HOMEWORK(« Given two paths, how can one tell if they refer to the same file? », « @@ -1305,38 +1324,6 @@ interpretations of .. does bash apply when you type ») -HOMEWORK(« - -Is it possible to choose among all possible paths which refer to the -same file a canonical path? That is, a shortest (counting -characters) absolute path which does not contain any soft links? - -», « - -

The POSIX standard requires each Unix system library to provide -the realpath() function which performs the following -substitutions on the given path: First, the path to the current -working directory is prepended if the given path is relative -(does not begin with a slash). Second, symbolic links are replaced -by their targets. Third, any occurrences of /. and -foo/.. are removed. The thusly transformed path is -returned by the function as the canonical path.

- -

Although each path can be canonicalized in this way, not all paths -which refer to the same file give rise to the same canonical path. For -example, /tmp/foo and /tmp/bar could refer -to regular files which are hard links of each other. In this case the -paths refer to the same file, yet the paths are different and already -canonicalized. The same can happen when a file system (or a subtree -of it) is bind mounted. That is, the file system tree is -visible at two or more locations in the global directory tree.

- -The message of this exercise is to convince the reader that it is -incorrect to assume that two files are different because their paths -are different. - -») - SECTION(«Processes»)

A program consists of instructions and data stored in -- 2.39.5