⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesys.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@table @code@item EACCESOne of the directories containing @var{newname} or @var{oldname}refuses write permission; or @var{newname} and @var{oldname} aredirectories and write permission is refused for one of them.@item EBUSYA directory named by @var{oldname} or @var{newname} is being used bythe system in a way that prevents the renaming from working.  This includesdirectories that are mount points for filesystems, and directoriesthat are the current working directories of processes.@item ENOTEMPTY@itemx EEXISTThe directory @var{newname} isn't empty.  The GNU system always returns@code{ENOTEMPTY} for this, but some other systems return @code{EEXIST}.@item EINVALThe @var{oldname} is a directory that contains @var{newname}.@item EISDIRThe @var{newname} names a directory, but the @var{oldname} doesn't.@item EMLINKThe parent directory of @var{newname} would have too many links.Well-designed file systems never report this error, because they permitmore links than your disk could possibly hold.  However, you must stilltake account of the possibility of this error, as it could result fromnetwork access to a file system on another machine.@item ENOENTThe file named by @var{oldname} doesn't exist.@item ENOSPCThe directory that would contain @var{newname} has no room for anotherentry, and there is no space left in the file system to expand it.@item EROFSThe operation would involve writing to a directory on a read-only filesystem.@item EXDEVThe two file names @var{newname} and @var{oldnames} are on differentfile systems.@end table@end deftypefun@node Creating Directories@section Creating Directories@cindex creating a directory@cindex directories, creating@pindex mkdirDirectories are created with the @code{mkdir} function.  (There is alsoa shell command @code{mkdir} which does the same thing.)@comment sys/stat.h@comment POSIX.1@deftypefun int mkdir (const char *@var{filename}, mode_t @var{mode})The @code{mkdir} function creates a new, empty directory whose name is@var{filename}.The argument @var{mode} specifies the file permissions for the newdirectory file.  @xref{Permission Bits}, for more information aboutthis.A return value of @code{0} indicates successful completion, and@code{-1} indicates failure.  In addition to the usual file name syntaxerrors (@pxref{File Name Errors}), the following @code{errno} errorconditions are defined for this function:@table @code@item EACCESWrite permission is denied for the parent directory in which the newdirectory is to be added.@item EEXISTA file named @var{filename} already exists.@item EMLINKThe parent directory has too many links.Well-designed file systems never report this error, because they permitmore links than your disk could possibly hold.  However, you must stilltake account of the possibility of this error, as it could result fromnetwork access to a file system on another machine.@item ENOSPCThe file system doesn't have enough room to create the new directory.@item EROFSThe parent directory of the directory being created is on a read-onlyfile system, and cannot be modified.@end tableTo use this function, your program should include the header file@file{sys/stat.h}.@pindex sys/stat.h@end deftypefun@node File Attributes@section File Attributes@pindex lsWhen you issue an @samp{ls -l} shell command on a file, it gives youinformation about the size of the file, who owns it, when it was lastmodified, and the like.  This kind of information is called the@dfn{file attributes}; it is associated with the file itself and not aparticular one of its names.This section contains information about how you can inquire about andmodify these attributes of files.@menu* Attribute Meanings::          The names of the file attributes,                                  and what their values mean.* Reading Attributes::          How to read the attributes of a file.* Testing File Type::           Distinguishing ordinary files,                                 directories, links... * File Owner::                  How ownership for new files is determined,			         and how to change it.* Permission Bits::             How information about a file's access                                 mode is stored. * Access Permission::           How the system decides who can access a file.* Setting Permissions::         How permissions for new files are assigned,			         and how to change them.* Testing File Access::         How to find out if your process can                                 access a file. * File Times::                  About the time attributes of a file.@end menu@node Attribute Meanings@subsection What the File Attribute Values Mean@cindex status of a file@cindex attributes of a file@cindex file attributesWhen you read the attributes of a file, they come back in a structurecalled @code{struct stat}.  This section describes the names of theattributes, their data types, and what they mean.  For the functionsto read the attributes of a file, see @ref{Reading Attributes}.The header file @file{sys/stat.h} declares all the symbols definedin this section.@pindex sys/stat.h@comment sys/stat.h@comment POSIX.1@deftp {Data Type} {struct stat}The @code{stat} structure type is used to return information about theattributes of a file.  It contains at least the following members:@table @code@item mode_t st_modeSpecifies the mode of the file.  This includes file type information(@pxref{Testing File Type}) and the file permission bits(@pxref{Permission Bits}).@item ino_t st_inoThe file serial number, which distinguishes this file from all otherfiles on the same device.@item dev_t st_devIdentifies the device containing the file.  The @code{st_ino} and@code{st_dev}, taken together, uniquely identify the file.  The@code{st_dev} value is not necessarily consistent across reboots orsystem crashes, however.@item nlink_t st_nlinkThe number of hard links to the file.  This count keeps track of how manydirectories have entries for this file.  If the count is everdecremented to zero, then the file itself is discarded.  Symbolic linksare not counted in the total.@item uid_t st_uidThe user ID of the file's owner.  @xref{File Owner}.@item gid_t st_gidThe group ID of the file.  @xref{File Owner}.@item off_t st_sizeThis specifies the size of a regular file in bytes.  For files thatare really devices and the like, this field isn't usually meaningful.@item time_t st_atimeThis is the last access time for the file.  @xref{File Times}.@item unsigned long int st_atime_usecThis is the fractional part of the last access time for the file.@xref{File Times}.@item time_t st_mtimeThis is the time of the last modification to the contents of the file.@xref{File Times}.@item unsigned long int st_mtime_usecThis is the fractional part of the time of last modification to thecontents of the file.  @xref{File Times}.@item time_t st_ctimeThis is the time of the last modification to the attributes of the file.@xref{File Times}.@item unsigned long int st_ctime_usecThis is the fractional part of the time of last modification to theattributes of the file.  @xref{File Times}.@item unsigned int st_nblocksThis is the amount of disk space that the file occupies, measured inunits of 512-byte blocks.The number of disk blocks is not strictly proportional to the size ofthe file, for two reasons: the file system may use some blocks forinternal record keeping; and the file may be sparse---it may have``holes'' which contain zeros but do not actually take up space on thedisk.You can tell (approximately) whether a file is sparse by comparing thisvalue with @code{st_size}, like this:@smallexample(st.st_blocks * 512 < st.st_size)@end smallexampleThis test is not perfect because a file that is just slightly sparsemight not be detected as sparse at all.  For practical applications,this is not a problem.@item unsigned int st_blksizeThe optimal block size for reading of writing this file, in bytes.  Youmight use this size for allocating the buffer space for reading ofwriting the file.  (This is unrelated to @code{st_blocks}.)@end table@end deftp  Some of the file attributes have special data type names which existspecifically for those attributes.  (They are all aliases for well-knowninteger types that you know and love.)  These typedef names are definedin the header file @file{sys/types.h} as well as in @file{sys/stat.h}.Here is a list of them.@comment sys/types.h@comment POSIX.1@deftp {Data Type} mode_tThis is an integer data type used to represent file modes.  In theGNU system, this is equivalent to @code{unsigned int}.@end deftp@cindex inode number@comment sys/types.h@comment POSIX.1@deftp {Data Type} ino_tThis is an arithmetic data type used to represent file serial numbers.(In Unix jargon, these are sometimes called @dfn{inode numbers}.)In the GNU system, this type is equivalent to @code{unsigned long int}.@end deftp@comment sys/types.h@comment POSIX.1@deftp {Data Type} dev_tThis is an arithmetic data type used to represent file device numbers.In the GNU system, this is equivalent to @code{int}.@end deftp@comment sys/types.h@comment POSIX.1@deftp {Data Type} nlink_tThis is an arithmetic data type used to represent file link counts.In the GNU system, this is equivalent to @code{unsigned short int}.@end deftp@node Reading Attributes@subsection Reading the Attributes of a FileTo examine the attributes of files, use the functions @code{stat},@code{fstat} and @code{lstat}.  They return the attribute information ina @code{struct stat} object.  All three functions are declared in theheader file @file{sys/stat.h}.@comment sys/stat.h@comment POSIX.1@deftypefun int stat (const char *@var{filename}, struct stat *@var{buf})The @code{stat} function returns information about the attributes of thefile named by @w{@var{filename}} in the structure pointed at by @var{buf}.If @var{filename} is the name of a symbolic link, the attributes you getdescribe the file that the link points to.  If the link points to anonexistent file name, then @code{stat} fails, reporting a nonexistentfile.The return value is @code{0} if the operation is successful, and @code{-1}on failure.  In addition to the usual file name syntax errors(@pxref{File Name Errors}, the following @code{errno} error conditionsare defined for this function:@table @code@item ENOENTThe file named by @var{filename} doesn't exist.@end table@end deftypefun@comment sys/stat.h@comment POSIX.1@deftypefun int fstat (int @var{filedes}, struct stat *@var{buf})The @code{fstat} function is like @code{stat}, except that it takes anopen file descriptor as an argument instead of a file name.@xref{Low-Level I/O}.Like @code{stat}, @code{fstat} returns @code{0} on success and @code{-1}on failure.  The following @code{errno} error conditions are defined for@code{fstat}:@table @code@item EBADFThe @var{filedes} argument is not a valid file descriptor.@end table@end deftypefun@comment sys/stat.h@comment BSD@deftypefun int lstat (const char *@var{filename}, struct stat *@var{buf})The @code{lstat} function is like @code{stat}, except that it does notfollow symbolic links.  If @var{filename} is the name of a symboliclink, @code{lstat} returns information about the link itself; otherwise,@code{lstat} works like @code{stat}.  @xref{Symbolic Links}.@end deftypefun@node Testing File Type@subsection Testing the Type of a FileThe @dfn{file mode}, stored in the @code{st_mode} field of the fileattributes, contains two kinds of information: the file type code, andthe access permission bits.  This section discusses only the type code,which you can use to tell whether the file is a directory, whether it isa socket, and so on.  For information about the access permission,@ref{Permission Bits}.There are two predefined ways you can access the file type portion ofthe file mode.  First of all, for each type of file, there is a @dfn{predicate macro} which examines a file mode value and returnstrue or false---is the file of that type, or not.  Secondly, you canmask out the rest of the file mode to get just a file type code.You can compare this against various constants for the supported filetypes.All of the symbols listed in this section are defined in the header file@file{sys/stat.h}.@pindex sys/stat.hThe following predicate macros test the type of a file, given the value@var{m} which is the @code{st_mode} field returned by @code{stat} onthat file:@comment sys/stat.h@comment POSIX@deftypefn Macro int S_ISDIR (mode_t @var{m})This macro returns nonzero if the file is a directory.@end deftypefn@comment sys/stat.h@comment POSIX@deftypefn Macro int S_ISCHR (mode_t @var{m})This macro returns nonzero if the file is a character special file (adevice like a terminal).@end deftypefn@comment sys/stat.h@comment POSIX@deftypefn Macro int S_ISBLK (mode_t @var{m})

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -