每天看点英文文档:APUE第三版
Posted 看,未来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天看点英文文档:APUE第三版相关的知识,希望对你有一定的参考价值。
1.4 Files and Directories(文件和目录)
File System(文件系统)
The UNIX file system is a hierarchical arrangement(层次排列) of directories and files. Everything starts in the directory called root, whose name is the single character /. A directory is a file that contains directory entries. Logically, we can think of each directory entry as containing a filename along with a structure of information describing the attributes of the file. The attributes of a file are such things as the type of file (regular file, directory), the size of the file, the owner of the file, permissions for the file (whether other users may access this file), and when the file was last modified. The stat and fstat functions return a structure of information containing all the attributes of a file. In Chapter 4, we’ll examine all the attributes of a file in great detail.
Unix系统由目录和文件分层排列,根目录称为root,写法:‘/’。目录是包含目录项的文件,理论上来说,我们可以认为每条目录项上都包含了一个文件名和一个描述文件属性的结构。
这个结构包括:文件种类、文件大小、文件拥有者、文件权限、最后修改时间。
使用 stat 和 fstat 函数可以获取这个结构中的信息。
在第四章我们将详细的讲解关于文件的细节。
We make a distinction between the logical view of a directory entry and the way it is actually stored(存储) on disk. Most implementations of UNIX file systems don’t store attributes in the directory entries themselves, because of the difficulty of keeping them in synch when a file has multiple hard links.
我们在目录项的物理视图和它实际在磁盘中的存储方式做了一个区分,大部分Unix文件系统不会将属性存储在目录项本身,因为一旦硬链接过多,这种做法将会很难保持数据的一致性。
Filename(文件名)
The names in a directory are called filenames. The only two characters that cannot appear in a filename are the slash character (/) and the null character. The slash separates the filenames that form a pathname (described next) and the null character terminates a pathname. Nevertheless, it’s good practice to restrict(限制) the characters in a filename to a subset of the normal printing characters. (If we use some of the shell’s special characters in the filename, we have to use the shell’s quoting mechanism to reference the filename, and this can get complicated.) Indeed, for portability, POSIX.1 recommends restricting filenames to consist of the following characters: letters (a-z,A-Z), numbers (0-9), period (.), dash (-), and underscore ( _ ).
Two filenames are automatically created whenever a new directory is created: .(called dot) and … (called dot-dot). Dot refers to the current directory, and dot-dot refers to the parent directory. In the root directory, dot-dot is the same as dot.
目录中的名称称为文件名,文件名中仅不能存在两种字符:斜杠字符(/)和空字符。斜杠分隔形成路径名的文件名,空字符终止路径名。
不过,最好将文件名的长度限制在打印长度之内(如果我们使用的文件名跟shell命令名冲突了,会有点麻烦)。为了简便,POSIX1标准限制了文件名包含字符范围为:a-z、A-Z、0-9、.-_
当创建新目录时,将自动创建两个文件名:. 和 … 。点表示当前目录,点点表示父目录。在根目录中,这俩没差。
The Research UNIX System and some older UNIX System V file systems restricted a filename to 14 characters. BSD versions extended this limit to 255 characters. Today, almost all commercial UNIX file systems support at least 255-character filenames.
Research UNIX系统和一些较旧的UNIX System V文件系统将文件名限制为14个字符。BSD版本将此限制扩展到255个字符。如今,几乎所有商用UNIX文件系统都支持至少255个字符的文件名。
Pathname(路径名)
A sequence of one or more filenames, separated by slashes and optionally starting with a slash, forms a pathname. A pathname that begins with a slash is called an absolute pathname; otherwise, it’s called a relative pathname. Relative pathnames refer to files relative to the current directory. The name for the root of the file system (/) is a special-case absolute pathname that has no filename component(组件).
路径名由一个或多个文件名序列组成,可以由‘/’开头,由‘/’开头的路径名称为绝对路径,其余称为相对路径。相对路径名指的是相对于当前目录的文件。
文件系统的根目录名(/)是一个特殊的绝对路径名,没有文件名组件。
Example(示例:列出目录中所有文件的名称)
Listing the names of all the files in a directory is not difficult. Figure 1.3 shows a bare-bones implementation of the ls(1) command.
#include "apue.h"
#include <dirent.h>
int main(int argc, char *argv[]){
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can’t open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\\n", dirp->d_name);
closedir(dp);
exit(0);
}
(此处省去一堆将man文档的,有点乱)
There are many details to consider in this 20-line program.
• First, we include a header of our own: apue.h. We include this header in almost every program in this text. This header includes some standard system headers and defines numerous constants and function prototypes that we use throughout the examples in the text. A listing of this header is in Appendix B.
• Next, we include a system header, dirent.h, to pick up the function prototypes for opendir and readdir, in addition to the definition of the dirent structure. On some systems, the definitions are split into multiple header files. For example, in the Ubuntu 12.04 Linux distribution, /usr/include/dirent.h declares the function prototypes and includes bits/dirent.h, which defines the dirent structure (and is actually stored in /usr/include/x86_64-linux-gnu/bits).
• Because the actual format of directory entries varies from one UNIX system to another, we use the functions opendir, readdir, and closedir to manipulate the directory.
• The opendir function returns a pointer to a DIR structure, and we pass this pointer to the readdir function. We don’t care what’s in the DIR structure. We then call readdir in a loop, to read each directory entry. The readdir function returns a pointer to a dirent structure or, when it’s finished with the directory,anull pointer.
All we examine in the dirent structure is the name of each directory entry (d_name). Using this name, we could then call the stat function (Section 4.2) to determine all the attributes of the file.
• When the program is done, it calls the function exit with an argument of 0. The function exit terminates a program. By convention, an argument of 0 means OK, and an argument between 1 and 255 means that an error occurred. In Section 8.5, we show how any program, such as a shell or a program that we write, can obtain the exit status of a program that it executes.
在这20行代码中有很多细节:
1、我们使用了一个自己的头文件:apue.h。在本书中几乎每个程序中都包了这个投。这个头里面包了一些标准系统头,并定义了我们在本文示例中使用的许多常量和函数原型。附录B中列出了该头文件。
2、除了dirent结构的定义之外,我们还包括一个系统头dirent.h,用于获取opendir和readdir的函数原型。在某些系统上,定义被拆分为多个头文件。例如,在Ubuntu 12.04 Linux发行版中,/usr/include/dirent.h声明函数原型并包含bits/dirent.h,它定义了dirent结构(实际上存储在/usr/include/x86_64-Linux-gnu/bits中)。
3、由于目录项的实际格式因UNIX系统而异,因此我们使用函数opendir、readdir和closedir来操作目录。
4、opendir函数返回指向DIR结构的指针,我们将该指针传递给readdir函数。我们不关心DIR结构中的内容。然后我们在循环中调用readdir来读取每个目录条目。readdir函数返回一个指向dirent结构的指针,或者在使用完目录后返回一个all指针。
我们在dirent结构中只检查每个目录项的名称(d_name)。使用这个名称,我们可以调用stat函数(第4.2节)来确定文件的所有属性。
5、当程序完成时,它调用参数为0的函数exit。函数exit终止程序。按照惯例,参数0表示OK,参数1到255表示出错。在第8.5节中,我们将展示任何程序(如shell或我们编写的程序)如何获得其执行的程序的退出状态。
Working Directory(工作目录)
Every process has a working directory, sometimes called the current working directory.
This is the directory from which all relative pathnames are interpreted. A process can change its working directory with the chdir function.
For example, the relative pathname doc/memo/joe refers to the file or directory joe, in the directory memo, in the directory doc, which must be a directory within the working directory. From looking just at this pathname, we know that both doc and memo have to be directories, but we can’t tell whether joe is a file or a directory. The pathname /usr/lib/lint is an absolute pathname that refers to the file or directory lint in the directory lib, in the directory usr, which is in the root directory.
每个进程都有一个工作目录,有时称为当前工作目录。
这是解释所有相对路径名的目录。进程可以使用chdir函数更改其工作目录。
例如,相对路径名doc/memo/joe指的是目录memo中目录doc中的文件或目录joe,它必须是工作目录中的目录。通过查看这个路径名,我们知道doc和memo都必须是目录,但我们无法区分joe是文件还是目录。路径名/usr/lib/lint是一个绝对路径名,它引用根目录usr中目录lib中的文件或目录lint。
Home Directory
When we log in, the working directory is set to our home directory. Our home directory is obtained from our entry in the password file (Section 1.3).
以上是关于每天看点英文文档:APUE第三版的主要内容,如果未能解决你的问题,请参考以下文章