Open -3.3-

3.3. open Function
               open 函数

A file is opened or created by calling the open function.
打开或创建一个文件调用open函数
open#include <fcntl.h>

      int open(const char * pathname, int oflag, ... /* mode_t mode   */ );
                                                                                                    Returns: file descriptor if OK, 1 on error
                                                                                                       返回值:成功返回文件描述符,错误返回1



           We show the third argument as ..., which is the ISO C way to specify that the number and types of the remaining arguments may vary. For this function, the third argument is used only when a new file is being created, as we describe later. We show this argument as a comment in the prototype.
           我们看到第三个参数是...,这里是ISO C指定的不同的参数的数目和类型的方法.对于这个函数,第三个参数仅在创建一个新文件的时候使用,我们稍后进行说明.我们看到这个参数的原型在注释中.

           The pathname is the name of the file to open or create. This function has a multitude of options, which are specified by the oflag argument. This argument is formed by ORing together one or more of the following constants from the <fcntl.h> header:
          pathname 是将要打开或创建的文件名字.这个函数有多个不同选项,由参数 oflag指定.此参数的形式由下列常数的一个或多个进行或运算构成,这些常数在<fcntl.h>中:

O_RDONLY   Open for reading only
                      只读打开
O_WRONLY   Open for writing only
                       只写打开
O_RDWR     Open for reading and writing
                      读写打开

          Most implementations define O_RDONLY as 0, O_WRONLY as 1, and O_RDWR as 2, for compatibility with older programs.
         多数的实现第一O_RDONLY为0,O_WRONLY为1,O_RDWR为2,为了实现与早期程序的兼容.
         One and only one of these three constants must be specified. The following constants are optional

          这三个常数仅能指定一个.下面的常数是可选的:

 

O_APPEND    Append to the end of file on each write. We describe this option in  detail in Section 3.11.:

                       每次write附加到文件末尾.我们将在3.11节进行详细的说明

 

O_CREAT     Create the file if it doesn't exist. This option requires a third argument to the open function, the mode, which specifies the access           permission bits of the new file. (When we describe a file's access permission bits in Section 4.5, we'll see how to specify the mode and how it can be modified by the umask value of a process.)

                       如果文件不存在则创建.这个选项需要在调用open函数时填写第三个参数mode.用来指定新文件的许可权为(在4.5节指定文件的许可权位时可以看到怎样指定    mode和怎样用进程的umask值修改它.)


O_EXCL      Generate an error if O_CREAT is also specified and the file already exists. This test for whether the file already exists and the creation of the file if it doesn't exist is an atomic operation. We describe  atomic operations in more detail in Section 3.11.

                    当O_CREAT指定的文件已经存在时产生一个错误.它测试文件是否存在或则在文件不存在将文件的创建作为一个原子操作.3.11节将详细说明原子操作

 

O_TRUNC     If the file exists and if it is successfully opened for either write-only or readwrite, truncate its length to 0.

                     当以只写或读写方式成功打开一个现存文件,则将它的长度截短为0

 

O_NOCTTY     If the pathname refers to a terminal device, do not allocate the device as the controlling terminal for this process. We talk about controlling terminals in Section 9.6.
                       如果pathname指定的是一个终端设备,则不分配此设备作为此进程的控制终端.我们将在9.6节讨论控制终端

O_NONBLOCK    If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the nonblocking mode for both the opening of the file and subsequent I/O. We describe this mode in Section 14.2.

                            如果pathname指定的是一个FIFO,一个块特殊文件,或者一个自己特殊文件,这一选项将打开文件和后续的I/O操作都设置为非阻塞模式.我们将在14.2节说明 这一模式


            In earlier releases of System V, the O_NDELAY (no delay) flag was introduced. This option is similar to the O_NONBLOCK (nonblocking) option, but an ambiguity was introduced in the return value from a read operation. The no-delay option causes a read to return 0 if there is no data to be read from a pipe, FIFO, or device, but this conflicts with a return value of 0, indicating an end of file. SVR4-based systems still support the no-delay option, with the old semantics, but new applications should use the nonblocking option instead.
            在早期的SysetemV中,引入了O_NDELAY(无延迟)标志.这个选项与O_NONBLOCK(非阻塞)模式选项很类似,但是与read操作的返回值上存在二义性.如果不能从管道、 FIFO或设备读得数据,则不延迟选择项使read返回0,这与表示已读到文件尾端的返回值0相冲突。SVR 4仍支持这种语义的不延迟选择项,但是新的应用程序应当使用不阻塞选择项以代替之。
            The following three flags are also optional. They are part of the synchronized input and output option of the Single UNIX Specification (and thus POSIX.1):

            下面的3个标志也是可选的.他们是UNIX标准规范和POSIX.1同步输入输出的一部分:

 

O_DSYNC   Have each write wait for physical I/O to complete, but don't wait for file attributes to be updated if they don't affect the ability to read
                     the data just written.

                     每次写操作都等到物理I/O结束,如果它不影响数据的读取,则不等待文件属性的更新

O_RSYNC   Have each read operation on the file descriptor wait until any pending writes for the same portion of the file are complete.

                    每次读操作要等到对同一文件的还未完成的写操作完成后

O_SYNC      Have each write wait for physical I/O to complete, including I/O necessary to update file attributes modified as a result of the write.
                    We use this option in Section 3.14.

                    每次写操作都等待物理I/O完成,同时对文件属性的修改进行更新.

 

          The O_DSYNC and O_SYNC flags are similar, but subtly different. The O_DSYNC flag affects a file's attributes only when they need to be updated to reflect a change in the file's data (for example, update the file's size to reflect more data). With the O_SYNC flag, data and attributes are always updated synchronously. When overwriting an existing part of a file opened with the O_DSYNC flag, the file times wouldn't be updated synchronously. In contrast, if we had opened the file with the O_SYNC flag, every write to the file would update the file's times before the write returns, regardless of whether we were writing over existing bytes or appending to the file.
           O_DSYNC和O_SYNC标志很相似,但是有些微小的区别.O_DSYNC标志影响文件属性,当文件数据变化时(例如修改了文件的大小).O_SYNC标志,数据和文件属性总是同步的更新.当覆盖一个标志为O_DSYNC打开的现存文件,文件的时间将不同步更新.在对比O_SYNC,如果我们打开文件时标志为O_SYNC,则每次对文件的写操作都将同步更新文件的时间,无论时候覆盖已经存在的数据或者附加到末尾.
            Solaris 9 supports all three flags. FreeBSD 5.2.1 and Mac OS X 10.3 have a separate flag (O_FSYNC) that does the same thing as O_SYNC. Because the two flags are equivalent, FreeBSD 5.2.1 defines them to have the same value (but curiously, Mac OS X 10.3 doesn't define O_SYNC). FreeBSD 5.2.1 and Mac OS X 10.3 don't support the O_DSYNC or O_RSYNC flags. Linux 2.4.22 treats both flags the same as O_SYNC.
           Solaris 9这三个标志都支持. FreeBSD 5.2.1 和Mac OS X 10.3有一个不同的标志(O_FSYNC)跟O_SYNC具有同样作用.FreeBSD5.2.1对它们定义了相同的值(但是很奇怪,Mac OS X10.3 确没有定义O_SYNC).FreeBSD 5.2.1 和 Mac OS X 10.3 不支持O_DSYNC 和O_RSYNC标志.Linux 2.4.22 把这些标志都看作O_SYNC.
           The file descriptor returned by open is guaranteed to be the lowest-numbered unused descriptor. This fact is used by some applications to open a new file on standard input, standard output, or standard error. For example, an application might close standard outputnormally, file descriptor 1 and then open another file, knowing that it will be opened on file descriptor 1. We'll see a better way to guarantee that a file is open on a given descriptor in Section 3.12 with the dup2 function.
           由open返回的文件描述符一定是最小的未用描述符数字。这一点被很多应用程序用来在标准输入、标准输出或标准出错输出上打开一个新的文件。例如,一个应用程序可以先关闭标准输出(通常是文件描述符1 ),然后打开另一个文件,事先就能了解到该文件一定会在文件描述符1上打开。在3 . 1 2节说明dup2函数时,可以了解到有更好的方法来保证在一个给定的描述符上打开一个文件。

Filename and Pathname Truncation
文件名和路径名截短

            What happens if NAME_MAX is 14 and we try to create a new file in the current directory with a filename containing 15 characters? Traditionally, early releases of System V, such as SVR2, allowed this to happen, silently truncating the filename beyond the 14th character. BSD-derived systems returned an error status, with errno set to ENAMETOOLONG. Silently truncating the filename presents a problem that affects more than simply the creation of new files. If NAME_MAX is 14 and a file exists whose name is exactly 14 characters, any function that accepts a pathname argument, such as open or stat, has no way to determine what the original name of the file was, as the original name might have been truncated.

            如果NAME_MAX是14,当我在要在当前目录下创建一个文件名由15的字节构成的文件时会发生什么?早期的SystemV版本,如SVR2允许这种情况发生,之后默默地将文件名截短为14个字节.BSD系统则返回出错ENAMETOOLONG.这一问题不仅仅与创建新文件有关。如果NAME_MAX是14,而存在一个其文件名恰恰就是14个字符的文件,那么以pathname作为其参数的任一函数( open , stat等)都会遇到这一问题。

            With POSIX.1, the constant _POSIX_NO_TRUNC determines whether long filenames and long pathnames are truncated or whether an error is returned. As we saw in Chapter 12, this value can vary based on the type of the file system.

           在POSIX.1中,常数_ POSIX_NO_TRUNC决定了是否要截短过长的文件名或路径名,或者返回一个出错。第12章将说明此值可以针对各个不同的文件系统进行变更。
          Whether or not an error is returned is largely historical. For example, SVR4-based systems do not generate an error for the traditional System V file system, S5. For the BSD-style file system (known as UFS), however, SVR4-based systems do generate an error.
          S V R 4对传统的系统V文件系统( S 5 )并不保证返回出错,但是对BSD风格的文件系统( UFS ),SVR 4保证返回出错.
          As another example, see Figure 2.19. Solaris will return an error for UFS, but not for PCFS, the DOS-compatible file system, as DOS silently truncates filenames that don't fit in an 8.3 format.
          如图2.19的例子.solaris将返回出错(UFS),但是PCFS则不返回出错,而DOS则会截短不适合8.3格式的文件名.
          BSD-derived systems and Linux always return an error.
          BSD系统和linux总是返回出错
          If _POSIX_NO_TRUNC is in effect, errno is set to ENAMETOOLONG, and an error status is returned if the entire pathname exceeds PATH_MAX or any filename component of the pathname exceeds NAME_MAX.
        如果 _POSIX_NO_TRUNC 有效,则在整个路径名超过PATH _ MAX,或路径名中的任一文件名超过NAME _ MAX时,返回出错ENAMETOOLONG。

你可能感兴趣的:(open)