📄 filesys.texi
字号:
This macro returns nonzero if the file is a block special file (a devicelike a disk).@end deftypefn@comment sys/stat.h@comment POSIX@deftypefn Macro int S_ISREG (mode_t @var{m})This macro returns nonzero if the file is a regular file.@end deftypefn@comment sys/stat.h@comment POSIX@deftypefn Macro int S_ISFIFO (mode_t @var{m})This macro returns nonzero if the file is a FIFO special file, or apipe. @xref{Pipes and FIFOs}.@end deftypefn@comment sys/stat.h@comment GNU@deftypefn Macro int S_ISLNK (mode_t @var{m})This macro returns nonzero if the file is a symbolic link.@xref{Symbolic Links}.@end deftypefn@comment sys/stat.h@comment GNU@deftypefn Macro int S_ISSOCK (mode_t @var{m})This macro returns nonzero if the file is a socket. @xref{Sockets}.@end deftypefnAn alterate non-POSIX method of testing the file type is supported forcompatibility with BSD. The mode can be bitwise ANDed with@code{S_IFMT} to extract the file type code, and compared to theappropriate type code constant. For example,@smallexampleS_ISCHR (@var{mode})@end smallexample@noindentis equivalent to:@smallexample((@var{mode} & S_IFMT) == S_IFCHR)@end smallexample@comment sys/stat.h@comment BSD@deftypevr Macro int S_IFMTThis is a bit mask used to extract the file type code portion of a modevalue.@end deftypevrThese are the symbolic names for the different file type codes:@table @code@comment sys/stat.h@comment BSD@item S_IFDIR@vindex S_IFDIRThis macro represents the value of the file type code for a directory file.@comment sys/stat.h@comment BSD@item S_IFCHR@vindex S_IFCHRThis macro represents the value of the file type code for acharacter-oriented device file.@comment sys/stat.h@comment BSD@item S_IFBLK@vindex S_IFBLKThis macro represents the value of the file type code for a block-orienteddevice file.@comment sys/stat.h@comment BSD@item S_IFREG@vindex S_IFREGThis macro represents the value of the file type code for a regular file.@comment sys/stat.h@comment BSD@item S_IFLNK@vindex S_IFLNKThis macro represents the value of the file type code for a symbolic link.@comment sys/stat.h@comment BSD@item S_IFSOCK@vindex S_IFSOCKThis macro represents the value of the file type code for a socket.@comment sys/stat.h@comment BSD@item S_IFIFO@vindex S_IFIFOThis macro represents the value of the file type code for a FIFO or pipe.@end table@node File Owner@subsection File Owner@cindex file owner@cindex owner of a file@cindex group owner of a fileEvery file has an @dfn{owner} which is one of the registered user namesdefined on the system. Each file also has a @dfn{group}, which is oneof the defined groups. The file owner can often be useful for showingyou who edited the file (especially when you edit with GNU Emacs), butits main purpose is for access control.The file owner and group play a role in determining access because thefile has one set of access permission bits for the user that is theowner, another set that apply to users who belong to the file's group,and a third set of bits that apply to everyone else. @xref{AccessPermission}, for the details of how access is decided based on thisdata.When a file is created, its owner is set from the effective user ID ofthe process that creates it (@pxref{Process Persona}). The file's groupID may be set from either effective group ID of the process, or thegroup ID of the directory that contains the file, depending on thesystem where the file is stored. When you access a remote file system,it behaves according to its own rule, not according to the system yourprogram is running on. Thus, your program must be prepared to encountereither kind of behavior, no matter what kind of system you run it on.@pindex chown@pindex chgrpYou can change the owner and/or group owner of an existing file usingthe @code{chown} function. This is the primitive for the @code{chown}and @code{chgrp} shell commands.@pindex unistd.hThe prototype for this function is declared in @file{unistd.h}.@comment unistd.h@comment POSIX.1@deftypefun int chown (const char *@var{filename}, uid_t @var{owner}, gid_t @var{group})The @code{chown} function changes the owner of the file @var{filename} to@var{owner}, and its group owner to @var{group}.Changing the owner of the file on certain systems clears the set-user-IDand set-group-ID bits of the file's permissions. (This is because thosebits may not be appropriate for the new owner.) The other filepermission bits are not changed.The return value is @code{0} on success and @code{-1} on failure.In addition to the usual file name syntax errors (@pxref{File Name Errors}), the following @code{errno} error conditions are defined for this function:@table @code@item EPERMThis process lacks permission to make the requested change.Only privileged users or the file's owner can change the file's group.On most file systems, only privileged users can change the file owner;some file systems allow you to change the owner if you are currently theowner. When you access a remote file system, the behavior you encounteris determined by the system that actually holds the file, not by thesystem your program is running on.@xref{Options for Files}, for information about the@code{_POSIX_CHOWN_RESTRICTED} macro.@item EROFSThe file is on a read-only file system.@end table@end deftypefun@comment unistd.h@comment BSD@deftypefun int fchown (int @var{filedes}, int @var{owner}, int @var{group})This is like @code{chown}, except that it changes the owner of the filewith open file descriptor @var{filedes}.The return value from @code{fchown} is @code{0} on success and @code{-1}on failure. The following @code{errno} error codes are defined for thisfunction:@table @code@item EBADFThe @var{filedes} argument is not a valid file descriptor.@item EINVALThe @var{filedes} argument corresponds to a pipe or socket, not an ordinaryfile.@item EPERMThis process lacks permission to make the requested change. Fordetails, see @code{chmod}, above.@item EROFSThe file resides on a read-only file system.@end table@end deftypefun@node Permission Bits@subsection The Mode Bits for Access PermissionThe @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 accesspermission bits, which control who can read or write the file.@xref{Testing File Type}, for information about the file type code.All of the symbols listed in this section are defined in the header file@file{sys/stat.h}.@pindex sys/stat.h@cindex file permission bitsThese symbolic constants are defined for the file mode bits that controlaccess permission for the file:@table @code@comment sys/stat.h@comment POSIX.1@item S_IRUSR@vindex S_IRUSR@comment sys/stat.h@comment BSD@itemx S_IREAD@vindex S_IREADRead permission bit for the owner of the file. On many systems, thisbit is 0400. @code{S_IREAD} is an obsolete synonym provided for BSDcompatibility.@comment sys/stat.h@comment POSIX.1@item S_IWUSR@vindex S_IWUSR@comment sys/stat.h@comment BSD@itemx S_IWRITE@vindex S_IWRITEWrite permission bit for the owner of the file. Usually 0200.@code{S_IWRITE} is an obsolete synonym provided for BSD compatibility.@comment sys/stat.h@comment POSIX.1@item S_IXUSR@vindex S_IXUSR@comment sys/stat.h@comment BSD@itemx S_IEXEC@vindex S_IEXECExecute (for ordinary files) or search (for directories) permission bitfor the owner of the file. Usually 0100. @code{S_IEXEC} is an obsoletesynonym provided for BSD compatibility.@comment sys/stat.h@comment POSIX.1@item S_IRWXU@vindex S_IRWXUThis is equivalent to @samp{(S_IRUSR | S_IWUSR | S_IXUSR)}.@comment sys/stat.h@comment POSIX.1@item S_IRGRP@vindex S_IRGRPRead permission bit for the group owner of the file. Usually 040.@comment sys/stat.h@comment POSIX.1@item S_IWGRP@vindex S_IWGRPWrite permission bit for the group owner of the file. Usually 020.@comment sys/stat.h@comment POSIX.1@item S_IXGRP@vindex S_IXGRPExecute or search permission bit for the group owner of the file.Usually 010.@comment sys/stat.h@comment POSIX.1@item S_IRWXG@vindex S_IRWXGThis is equivalent to @samp{(S_IRGRP | S_IWGRP | S_IXGRP)}.@comment sys/stat.h@comment POSIX.1@item S_IROTH@vindex S_IROTHRead permission bit for other users. Usually 04.@comment sys/stat.h@comment POSIX.1@item S_IWOTH@vindex S_IWOTHWrite permission bit for other users. Usually 02.@comment sys/stat.h@comment POSIX.1@item S_IXOTH@vindex S_IXOTHExecute or search permission bit for other users. Usually 01.@comment sys/stat.h@comment POSIX.1@item S_IRWXO@vindex S_IRWXOThis is equivalent to @samp{(S_IROTH | S_IWOTH | S_IXOTH)}.@comment sys/stat.h@comment POSIX@item S_ISUID@vindex S_ISUIDThis is the set-user-ID on execute bit, usually 04000. @xref{How Change Persona}.@comment sys/stat.h@comment POSIX@item S_ISGID@vindex S_ISGIDThis is the set-group-ID on execute bit, usually 02000.@xref{How Change Persona}.@cindex sticky bit@comment sys/stat.h@comment BSD@item S_ISVTX@vindex S_ISVTXThis is the @dfn{sticky} bit, usually 01000.On a directory, it gives permission to delete a file in the directoryonly if you own that file. Ordinarily, a user either can delete all thefiles in the directory or cannot delete any of them (based on whetherthe user has write permission for the directory). The same restrictionapplies---you must both have write permission for the directory and ownthe file you want to delete. The one exception is that the owner of thedirectory can delete any file in the directory, no matter who owns it(provided the owner has given himself write permission for thedirectory). This is commonly used for the @file{/tmp} directory, whereanyone may create files, but not delete files created by other users.Originally the sticky bit on an executable file modified the swappingpolicies of the system. Normally, when a program terminated, its pagesin core were immediately freed and reused. If the sticky bit was set onthe executable file, the system kept the pages in core for a while as ifthe program were still running. This was advantageous for a programlikely to be run many times in succession. This usage is obsolete inmodern systems. When a program terminates, its pages always remain incore as long as there is no shortage of memory in the system. When theprogram is next run, its pages will still be in core if no shortagearose since the last run.On some modern systems where the sticky bit has no useful meaning for anexecutable file, you cannot set the bit at all for a non-directory.If you try, @code{chmod} fails with @code{EFTYPE}; @pxref{Setting Permissions}.Some systems (particularly SunOS) have yet another use for the stickybit. If the sticky bit is set on a file that is @emph{not} executable,it means the opposite: never cache the pages of this file at all. Themain use of this is for the files on an NFS server machine which areused as the swap area of diskless client machines. The idea is that thepages of the file will be cached in the client's memory, so it is awaste of the server's memory to cache them a second time. In this usethe sticky bit also says that the filesystem may fail to record thefile's modification time onto disk reliably (the idea being that noonecares for a swap file).@end tableThe actual bit values of the symbols are listed in the table aboveso you can decode file mode values when debugging your programs.These bit values are correct for most systems, but they are notguaranteed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -