讲解:300698、Systems Programming、c/
Workshop DOperating Systems Programming – 3006981 IntroductionIn this workshop you will investigate file I/O and file copy operations.2 SpecificationFollowing on from the demonstration program cp1.c presented in the lecture, we will make a series of modifi-cations to the program.Firstly, what happens when the cp1.c program is asked to copy a file onto itself, i.e. cp1 input input?Is this what you expect? Modify the program to do something more sensible! As a hint, two files are the same ifthey are on the same device and have the same i-node number (which stat() can give you), simply comparingthe names is not enough.Secondly, a real copy program will assign the same file permissions to the destination as were on the source,modify your answer to the last part to do this.Thirdly, real copy programs allow the second argument to be a directory, modify the answer to the last partto include this functionality. You should allocate the space for the new name dynamically.13 Sample Code3.1 cp1.c#include #include #include #define BUFFERSIZE 4096#define COPYMODE 0644void oops(char *, char *);main(int ac, char *av[]){int in_fd, out_fd, n_chars;char buf[BUFFERSIZE];if ( ac != 3 ){fprintf( stderr, usage: %s source destination\n, *av);exit(1);}if ( (in_fd=open(av[1], O_RDONLY)) == -1 )oops(Cannot open , av[1]);if ( (out_fd=creat( av[2], COPYMODE)) == -1 )oops( Cannot creat, av[2]);while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )if ( write( out_fd, buf, n_chars ) != n_chars )oops(Write error to , av[2]);if ( n_chars == -1 )oops(Read error from , av[1]);if ( close(in_fd) == -1 || close(out_fd) == -1 )oops(Error closing files,);}void oops(char *s1, char *s2){fprintf(stderr,Error: %s , s1);perror(s2);exit(1);}24 Supplementary MaterialsThe material on the following pages is an extract of the linux system documentation and may prove useful inimplementing this Workshop. These manual pages are taken from the Linux man-pages Project available at:http://www.kernel.org/doc/man-pages/.3OPEN(2) Linux Programmer’s Manual OPEN(2)NAMEopen, creat ? open and possibly create a file or deviceSYNOPSIS#include #include #include int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);int creat(const char * pathname, mode_t mode);DESCRIPTIONGiven a pathname for a file, open() returns a file descriptor,asmall, non-negative integer for use in subsequentsystem calls (read(2), write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a successfulcall will be the lowest-numbered file descriptor not currently open for the process.The new file descriptor is set to remain open across an execve(2) (i.e., the FD_CLOEXEC file descriptorflag described in fcntl(2) is initially disabled). The file offset is set to the beginning of the file (seelseek(2)).A call to open() creates a new open file description, an entry in the system-wide table of open files. Thisentry records the file offset and the file status flags (modifiable via the fcntl() F_SETFL operation). A filedescriptor is a reference to one of these entries; this reference is unaffected if pathname is subsequentlyremoved or modified to refer to a different file. The new open file description is initially not shared withany other process, but sharing may arise via fork(2).The parameter flags must include one of the following access modes: O_RDONLY, O_WRONLY, orO_RDWR. These request opening the file read-only, write-only, or read/write, respectively.In addition, zero or more file creation flags and file status flags can be bitwise-or’d in flags. The file creationflags are O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC. The file status flags are all of theremaining flags listed below. The distinction between these two groups of flags is that the file status flagscan be retrieved and (in some cases) modified using fcntl(2). The full list of file creation flags and file statusflags is as follows:O_APPENDThe file is opened in append mode. Before each write(), the file offset is positioned at the end ofthe file, as if with lseek(). O_APPEND may lead to corrupted files on NFS file systems if morethan one process appends data to a file at once. This is because NFS does not support appendingto a file, so the client kernel has to simulate it, which can’t be done without a race condition.O_ASYNCEnable signal-driven I/O: generate a signal (SIGIO by default, but this can be changed viafcntl(2)) when input or output becomes possible on this file descriptor. This feature is only availablefor terminals, pseudo-terminals, sockets, and (since Linux 2.6) pipes and FIFOs. See fcntl(2)for further details.O_CREATIf the file does not exist it will be created. The owner (user ID) of the file is set to the effectiveuser ID of the process. The group ownership (group ID) is set either to the effective group ID ofthe process or to the group ID of the parent directory (depending on filesystem type and mountoptions, and the mode of the parent directory, see, e.g., the mount options bsdgroups and sysvgroupsof the ext2 filesystem, as described in mount(8)).O_DIRECTTry to minimize cache effects of the I/O to and from this file. In general this will degrade performance,but it is useful in special situations, such as when applications do their own caching. FileI/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of aread(2) or write(2), data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes,Linux 2.6.12 2005-06-22 1OPEN(2) Linux Programmer’s Manual OPEN(2)and the alignment of user buffer and file offset must all be multiples of the logical block size of thefile system. Under Linux 2.6 alignment must fit the block size of the device.A semantically similar (but deprecated) interface for block devices is described in raw(8).O_DIRECTORYIf pathname is not a directory, cause the open to fail. This flag is Linux-specific, and was added inkernel version 2.1.126, to avoid denial-of-service problems if opendir(3) is called on a FIFO ortape device, but should not be used outside of the implementation of opendir.O_EXCLWhen used with O_CREAT, if the file already exists it is an error and the open() will fail. In thiscontext, a symbolic link exists, regardless of where it points to. O_EXCL is broken on NFS filesystems; programs which rely on it for performing locking tasks will contain a race condition.The solution for performing atomic file locking using a lockfile is to create a unique file on thesame file system (e.g., incorporating hostname and pid), use link(2) to makealink to the lockfile.If link() returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if itslink count has increased to 2, in which case the lock is also successful.O_LARGEFILE(LFS) Allow files whose sizes cannot be represented in an off_t (but can be represented in anoff64_t) to be opened.O_NOATIME(Since Linux 2.6.8) Do not update the file last access time (st_atime in the inode) when the file isread(2). This flag is intended for use by indexing or backup programs, where its use can signifi-cantly reduce the amount of disk activity. This flag may not be effective on all filesystems. Oneexample is NFS, where the server maintains the access time.O_NOCTTYIf pathname refers to a terminal device — see tty(4) — it will not become the process’s controllingterminal even if the process does not have one.O_NOFOLLOWIf pathname is a symbolic link, then the open fails. This is a FreeBSD extension, which was addedto Linux in version 2.1.126. Symbolic links in earlier components of the pathname will still befollowed.O_NONBLOCK or O_NDELAYWhen possible, the file is opened in non-blocking mode. Neither the open() nor any subsequentoperations on the file descriptor which is returned will cause the calling process to wait. For thehandling of FIFOs (named pipes), see also fifo(7). For a discussion of the effect of O_NONBLOCKin conjunction with mandatory file locks and with file leases, see fcntl(2).O_SYNCThe file is opened for synchronous I/O. Any write()s on the resulting file descriptor will block thecalling process until the data has been physically written to the underlying hardware. But seeRESTRICTIONS below.O_TRUNCIf the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR orO_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, theO_TRUNC flag is ignored. Otherwise the effect of O_TRUNC is unspecified.Some of these optional flags can be altered using fcntl() after the file has been opened.The argument mode specifies the permissions to use in case a new file is created. It is modified by the process’sumask in the usual way: the permissions of the created file are (mode & ?umask). Note that thismode only applies to future accesses of the newly created file; the open() call that creates a read-only fileLinux 2.6.12 2005-06-22 2OPEN(2) Linux Programmer’s Manual OPEN(2)may well return a read/write file descriptor.The following symbolic constants are provided for mode:S_IRWXU00700 user (file owner) has read, write and execute permissionS_IRUSR00400 user has read permissionS_IWUSR00200 user has write permissionS_IXUSR00100 user has execute permissionS_IRWXG00070 group has read, write and execute permissionS_IRGRP00040 group has read permissionS_IWGRP00020 group has write permissionS_IXGRP00010 group has execute permissionS_IRWXO00007 others have read, write and execute permissionS_IROTH00004 others have read permissionS_IWOTH00002 others have write permissionS_IXOTH00001 others have execute permissionmode must be specified when O_CREAT is in the flags, and is ignored otherwise.creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.RETURN VALUEopen() and creat() return the new file descriptor, or ?1 if an error occurred (in which case, errno is setappropriately).NOTESNote that open() can open device special files, but creat() cannot create them; use mknod(2) instead.On NFS file systems with UID mapping enabled, open() may return a file descriptor but e.g. read(2)requests are denied with EACCES. This is because the client performs open() by checking the permissions,but UID mapping is performed by the server upon read and write requests.If the file is newly created, its st_atime, st_ctime, st_mtime fields (respectively, time of last access, time oflast status change, and time of last modification; see stat(2)) are set to the current time, and so are thest_ctime and st_mtime fields of the parent directory. Otherwise, if the file is modified because of theO_TRUNC flag, its st_ctime and st_mtime fields are set to the current time.ERRORSEACCESThe requested access to the file is not allowed, or search permission is denied for one of the directoriesin the path prefix of pathname, or the file did not exist yet and write access to the parentdirectory is not allowed. (See also path_resolution(2).)Linux 2.6.12 2005-06-22 3OPEN(2) Linux Programmer’s Manual OPEN(2)EEXISTpathname already exists and O_CREAT and O_EXCL were used.EFAULTpathname points outside your accessible address space.EISDIRpathname refers to a directory and the access requested involved writing (that is, O_WRONLY orO_RDWR is set).ELOOPToo many symbolic links were encountered in resolving pathname, or O_NOFOLLOW wasspecified but pathname wasasymbolic link.EMFILEThe process already has the maximum number of files open.ENAMETOOLONGpathname was too long.ENFILEThe system limit on the total number of open files has been reached.ENODEVpathname refers to a device special file and no corresponding device exists. (This is a Linux kernelbug; in this situation ENXIO must be returned.)ENOENTO_CREAT is not set and the named file does not exist. Or,adirectory component in pathnamedoes not exist or is a dangling symbolic link.ENOMEMInsufficient kernel memory was available.ENOSPCpathname was to be created but the device containing pathname has no room for the new file.ENOTDIRA component used as a directory in pathname is not, in fact, a directory, or O_DIRECTORY wasspecified and pathname was not a directory.ENXIOO_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process has the file openfor reading. Or, the file is a device special file and no corresponding device exists.EOVERFLOWpathname refers to a regular file, too large to be opened; see O_LARGEFILE above.EPERMThe O_NOATIME flag was specified, but the effective user ID of the caller did not match theowner of the file and the caller was not privileged (CAP_FOWNER).EROFSpathname refers to a file on a read-only filesystem and write access was requested.ETXTBSYpathname refers to an executable image which is currently being executed and write access wasrequested.EWOULDBLOCKThe O_NONBLOCK flag was specified, and an incompatible lease was held on the file (seefcntl(2)).Linux 2.6.12 2005-06-22 4OPEN(2) Linux Programmer’s Manual OPEN(2)NOTEUnder Linux, the O_NONBLOCK flag indicates that one wants to open but does not necessarily have theintention to read or write. This is typically used to open devices in order to get a file descriptor for use withioctl(2).CONFORMING TOSVr4, 4.3BSD, POSIX.1-2001. The O_NOATIME, O_NOFOLLOW, and O_DIRECTORY flags areLinux-specific. One may have to define the _GNU_SOURCE macro to get their definitions.The (undefined) effect of O_RDONLY|O_TRUNC varies among implementations. On many systems thefile is actually truncated.The O_DIRECT flag was introduced in SGI IRIX, where it has alignment restrictions similar to those ofLinux 2.4. IRIX has also a fcntl(2) call to query appropriate alignments, and sizes. FreeBSD 4.x introduceda flag of same name, but without alignment restrictions. Support was added under Linux in kernelversion 2.4.10. Older Linux kernels simply ignore this flag. One may have to define the _GNU_SOURCEmacro to get its definition.BUGSThe thing that has always disturbed me about O_DIRECT is that the whole interface is just stupid, andwas probably designed by a deranged monkey on some serious mind-controlling substances. — LinusCurrently, it is not possible to enable signal-driven I/O by specifying O_ASYNC when calling open(); usefcntl(2) to enable this flag.RESTRICTIONSThere are many infelicities in the protocol underlying NFS, affecting amongst others O_SYNC andO_NDELAY.POSIX provides for three different variants of synchronised I/O, corresponding to the flags O_SYNC,O_DSYNC and O_RSYNC. Currently (2.1.130) these are all synonymous under Linux.SEE ALSOclose(2), dup(2), fcntl(2), link(2), lseek(2), mknod(2), mount(2), mmap(2), openat(2), path_resolution(2),read(2), socket(2), stat(2), umask(2), unlink(2), write(2), fopen(3), fifo(7), feature_test_macros(7)Linux 2.6.12 2005-06-22 5CLOSE(2) Linux Programmer’s Manual CLOSE(2)NAMEclose ? close a file descriptorSYNOPSIS#include int close(int fd);DESCRIPTIONclose() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks(see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless ofthe file descriptor that was used to obta300698留学生作业代写、代做Systems Programming作业、c/c++语言作业代做、代写c/c++编程作in the lock).If fd is the last copy of a particular file descriptor the resources associated with it are freed; if the descriptorwas the last reference to a file which has been removed using unlink(2) the file is deleted.RETURN VALUEclose() returns zero on success. On error, ?1 is returned, and errno is set appropriately.ERRORSEBADFfd isn’tavalid open file descriptor.EINTRThe close() call was interrupted by a signal.EIO An I/O error occurred.CONFORMING TOSVr4, 4.3BSD, POSIX.1-2001.NOTESNot checking the return value of close() is a common but nevertheless serious programming error. It isquite possible that errors on a previous write(2) operation are first reported at the final close(). Not checkingthe return value when closing the file may lead to silent loss of data. This can especially be observedwith NFS and with disk quota.A successful close does not guarantee that the data has been successfully saved to disk, as the kernel deferswrites. It is not common for a filesystem to flush the buffers when the stream is closed. If you need to besure that the data is physically stored use fsync(2). (It will depend on the disk hardware at this point.)It is probably unwise to close file descriptors while they may be in use by system calls in other threads inthe same process. Since a file descriptor may be re-used, there are some obscure race conditions that maycause unintended side effects.When dealing with sockets, you have to be sure that there is no recv(2) still blocking on it on anotherthread, otherwise it might block forever, since no more messages will be sent via the socket. Be sure to useshutdown(2) to shut down all parts the connection before closing the socket.SEE ALSOfcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3)Linux 2001-12-13 1READ(2) Linux Programmer’s Manual READ(2)NAMEread ? read from a file descriptorSYNOPSIS#include ssize_t read(int fd, void *buf, size_t count);DESCRIPTIONread() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, theresult is unspecified.RETURN VALUEOn success, the number of bytes read is returned (zero indicates end of file), and the file position isadvanced by this number. It is not an error if this number is smaller than the number of bytes requested;this may happen for example because fewer bytes are actually available right now (maybe because we wereclose to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() wasinterrupted by a signal. On error, ?1 is returned, and errno is set appropriately. In this case it is left unspecifiedwhether the file position (if any) changes.ERRORSEAGAINNon-blocking I/O has been selected using O_NONBLOCK and no data was immediately availablefor reading.EBADFfd is not a valid file descriptor or is not open for reading.EFAULTbuf is outside your accessible address space.EINTRThe call was interrupted by a signal before any data was read.EINVALfd is attached to an object which is unsuitable for reading; or the file was opened with theO_DIRECT flag, and either the address specified in buf, the value specified in count, or the currentfile offset is not suitably aligned.EIO I/O error. This will happen for example when the process is in a background process group, tries toread from its controlling tty, and either it is ignoring or blocking SIGTTIN or its process group isorphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.EISDIRfd refers to a directory.Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interruptedafter reading some data to return ?1 (with errno set to EINTR) or to return the number of bytes alreadyread.CONFORMING TOSVr4, 4.3BSD, POSIX.1-2001.RESTRICTIONSOn NFS file systems, reading small amounts of data will only update the time stamp the first time, subsequentcalls may not do so. This is caused by client side attribute caching, because most if not all NFSclients leave st_atime (last file access time) updates to the server and client side reads satisfied from theclient’s cache will not cause st_atime updates on the server as there are no server side reads. UNIX semanticscan be obtained by disabling client side attribute caching, but in most situations this will substantiallyincrease server load and decrease performance.Linux 2.0.32 1997-07-12 1READ(2) Linux Programmer’s Manual READ(2)Many filesystems and disks were considered to be fast enough that the implementation of O_NONBLOCKwas deemed unnecessary. So, O_NONBLOCK may not be available on files and/or disks.SEE ALSOclose(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2), readlink(2), readv(2), select(2),write(2), fread(3)Linux 2.0.32 1997-07-12 2WRITE(2) Linux Programmer’s Manual WRITE(2)NAMEwrite write to a file descriptorSYNOPSIS#include ssize_t write(int fd, const void *buf, size_t count);DESCRIPTIONwrite() writes up to count bytes to the file referenced by the file descriptor fd from the buffer starting atbuf. POSIX requires that a read() which can be proved to occur after a write() has returned returns thenew data. Note that not all file systems are POSIX conforming.RETURN VALUEOn success, the number of bytes written are returned (zero indicates nothing was written). On error, 1 isreturned, and errno is set appropriately. If count is zero and the file descriptor refers to a regular file, 0 maybe returned, or an error could be detected. Foraspecial file, the results are not portable.ERRORSEAGAINNon-blocking I/O has been selected using O_NONBLOCK and the write would block.EBADFfd is not a valid file descriptor or is not open for writing.EFAULTbuf is outside your accessible address space.EFBIGAn attempt was made to write a file that exceeds the implementation-defined maximum file size orthe process’ file size limit, or to write at a position past the maximum allowed offset.EINTRThe call was interrupted by a signal before any data was written.EINVALfd is attached to an object which is unsuitable for writing; or the file was opened with theO_DIRECT flag, and either the address specified in buf, the value specified in count, or the currentfile offset is not suitably aligned.EIO A low-level I/O error occurred while modifying the inode.ENOSPCThe device containing the file referred to by fd has no room for the data.EPIPE fd is connected to a pipe or socket whose reading end is closed. When this happens the writingprocess will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the programcatches, blocks or ignores this signal.)Other errors may occur, depending on the object connected to fd.CONFORMING TOSVr4, 4.3BSD, POSIX.1-2001.Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.NOTESA successful return from write() does not make any guarantee that data has been committed to disk. Infact, on some buggy implementations, it does not even guarantee that space has successfully been reservedfor the data. The only way to be sure is to call fsync(2) after you are done writing all your data.SEE ALSOclose(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2), read(2), select(2), writev(3), fwrite(3)Linux 2.0.32 2001-12-13 1STAT(2) Linux Programmer’s Manual STAT(2)NAMEstat, fstat, lstat ? get file statusSYNOPSIS#include #include #include int stat(const char * path, struct stat *buf );int fstat(int filedes, struct stat *buf );int lstat(const char *path, struct stat *buf );DESCRIPTIONThese functions return information about a file. No permissions are required on the file itself, but — in thecase of stat() and lstat() — execute (search) permission is required on all of the directories in path thatlead to the file.stat() stats the file pointed to by path and fills in buf.lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the filethat it refers to.fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor filedes.All of these system calls return a stat structure, which contains the following fields:struct stat {dev_t st_dev; /* ID of device containing file */ino_t st_ino; /* inode number */mode_t st_mode; /* protection */nlink_t st_nlink; /* number of hard links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */dev_t st_rdev; /* device ID (if special file) */off_t st_size; /* total size, in bytes */blksize_t st_blksize; /* blocksize for filesystem I/O */blkcnt_t st_blocks; /* number of blocks allocated */time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */time_t st_ctime; /* time of last status change */};The st_dev field describes the device on which this file resides.The st_rdev field describes the device that this file (inode) represents.The st_size field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of asymlink is the length of the pathname it contains, without a trailing null byte.The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. (This may besmaller than st_size/512, for example, when the file has holes.)The st_blksize field gives the preferred blocksize for efficient file system I/O. (Writing to a file in smallerchunks may cause an inefficient read-modify-rewrite.)Not all of the Linux filesystems implement all of the time fields. Some file system types allow mounting insuch a way that file accesses do not cause an update of the st_atime field. (See ‘noatime’ in mount(8).)The field st_atime is changed by file accesses, e.g. by execve(2), mknod(2), pipe(2), utime(2) and read(2)Linux 2.6.7 2004-06-23 1STAT(2) Linux Programmer’s Manual STAT(2)(of more than zero bytes). Other routines, like mmap(2), may or may not update st_atime.The field st_mtime is changed by file modifications, e.g. by mknod(2), truncate(2), utime(2) and write(2)(of more than zero bytes). Moreover, st_mtime of a directory is changed by the creation or deletion of filesin that directory. The st_mtime field is not changed for changes in owner, group, hard link count, or mode.The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count,mode, etc.).The following POSIX macros are defined to check the file type using the st_mode field:S_ISREG(m) is it a regular file?S_ISDIR(m) directory?S_ISCHR(m) character device?S_ISBLK(m) block device?S_ISFIFO(m) FIFO (named pipe)?S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)S_ISSOCK(m) socket? (Not in POSIX.1-1996.)The following flags are defined for the st_mode field:S_IFMT 0170000 bitmask for the file type bitfieldsS_IFSOCK 0140000 socketS_IFLNK 0120000 symbolic linkS_IFREG 0100000 regular fileS_IFBLK 0060000 block deviceS_IFDIR 0040000 directoryS_IFCHR 0020000 character deviceS_IFIFO 0010000 FIFOS_ISUID 0004000 set UID bitS_ISGID 0002000 set-group-ID bit (see below)S_ISVTX 0001000 sticky bit (see below)S_IRWXU 00700 mask for file owner permissionsS_IRUSR 00400 owner has read permissionS_IWUSR 00200 owner has write permissionS_IXUSR 00100 owner has execute permissionS_IRWXG 00070 mask for group permissionsS_IRGRP 00040 group has read permissionS_IWGRP 00020 group has write permissionS_IXGRP 00010 group has execute permissionS_IRWXO 00007 mask for permissions for others (not in group)S_IROTH 00004 others have read permissionS_IWOTH 00002 others have write permissionS_IXOTH 00001 others have execute permissionThe set-group-ID bit (S_ISGID) has several special uses. Foradirectory it indicates that BSD semantics isto be used for that directory: files created there inherit their group ID from the directory, not from the effectivegroup ID of the creating process, and directories created there will also get the S_ISGID bit set. For afile that does not have the group execution bit (S_IXGRP) set, the set-group-ID bit indicates mandatoryfile/record locking.The ‘sticky’ bit (S_ISVTX) on a directory means that a file in that directory can be renamed or deleted onlyby the owner of the file, by the owner of the directory, and by a privileged process.Linux 2.6.7 2004-06-23 2STAT(2) Linux Programmer’s Manual STAT(2)LINUX NOTESSince kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields.Glibc exposes the nanosecond component of each field using names either of the form st_atim.tv_nsec, ifthe _BSD_SOURCE or _SVID_SOURCE feature test macro is defined, or of the form st_atimensec, if neitherof these macros is defined. On file systems that do not support sub-second timestamps, these nanosecondfields are returned with the value 0.For most files under the /proc directory, stat() does not return the file size in the st_size field; instead thefield is returned with the value 0.RETURN VALUEOn success, zero is returned. On error, ?1 is returned, and errno is set appropriately.ERRORSEACCESSearch permission is denied for one of the directories in the path prefix of path. (See alsopath_resolution(2).)EBADFfiledes is bad.EFAULTBad address.ELOOPToo many symbolic links encountered while traversing the path.ENAMETOOLONGFile name too long.ENOENTA component of the path path does not exist, or the path is an empty string.ENOMEMOut of memory (i.e. kernel memory).ENOTDIRA component of the path is not a directory.CONFORMING TOThese system calls conform to SVr4, 4.3BSD, POSIX.1-2001.Use of the st_blocks and st_blksize fields may be less portable. (They were introduced in BSD. The interpretationdiffers between systems, and possibly on a single system when NFS mounts are involved.)POSIX does not describe the S_IFMT, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFDIR, S_IFCHR,S_IFIFO, S_ISVTX bits, but instead demands the use of the macros S_ISDIR(), etc. The S_ISLNK andS_ISSOCK macros are not in POSIX.1-1996, but both are present in POSIX.1-2001; the former is fromSVID 4, the latter from SUSv2.Unix V7 (and later systems) had S_IREAD, S_IWRITE, S_IEXEC, where POSIX prescribes the synonymsS_IRUSR, S_IWUSR, S_IXUSR.OTHER SYSTEMSValues that have been (or are) in use on various systems:hex name ls octal descriptionf000 S_IFMT 170000 mask for file type0000 000000 SCO out-of-service inode, BSD unknown typeSVID-v2 and XPG2 have both 0 and 0100000 for ordinary file1000 S_IFIFO p| 010000 FIFO (named pipe)Linux 2.6.7 2004-06-23 3STAT(2) Linux Programmer’s Manual STAT(2)2000 S_IFCHR c 020000 character special (V7)3000 S_IFMPC 030000 multiplexed character special (V7)4000 S_IFDIR d/ 040000 directory (V7)5000 S_IFNAM 050000 XENIX named special filewith two subtypes, distinguished by st_rdev values 1, 2:0001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM0002 S_INSHD m 000002 XENIX shared data subtype of IFNAM6000 S_IFBLK b 060000 block special (V7)7000 S_IFMPB 070000 multiplexed block special (V7)8000 S_IFREG - 100000 regular (V7)9000 S_IFCMP 110000 VxFS compressed9000 S_IFNWK n 110000 network special (HP-UX)a000 S_IFLNK l@ 120000 symbolic link (BSD)b000 S_IFSHAD 130000 Solaris shadow inode for ACL (not 转自:http://www.7daixie.com/2019051813954957.html