File functions in Linux (i)

Source: Internet
Author: User
Tags bitwise chmod function prototype lstat

I. Stat, FSTAT, Fstatat, Lstat functions

The prototype of the function is:

#include <sys/stat.h>intStatConst Char*restrict Pathname,structStat *restrict buf);intFstat (intFdstructStat *buf);intLstatConst Char*restrict Pathname,structStat *restrict buf);intFstatat (intFdConst Char*restrict Pathname,structStat *restrict buf,intFlag);

The Pathname,stat function is given to return the information structure associated with this named file. The Fstat function obtains information about opening on the descriptor FD.

Lstat returns information about the symbolic link, rather than the file referenced by the symbolic link. The Fstatat function is a relative to the current open

The path name of the directory (which is pointed to by the FD parameter) returns the file statistics, and the flag parameter controls whether to follow a symbolic link. When At_symlink_n

When the Ofollow flag is set, Fstatat does not follow the symbolic link, but instead returns information about the symbolic link itself. Otherwise, by default, the return

Is the information for the actual file that the symbolic link points to. If the value of the FD parameter is AT_FDCWD, and the pathname parameter is a relative pathname, FS

Tatat calculates the pathname parameter relative to the current directory. If pathname is an absolute path, the FD parameter is ignored. In both cases

, the Fstatat function is the same as stat or Lstat, depending on the value of flag. The 2nd parameter, buf, is a pointer that points to a node that must be supplied

Frame. function to populate the structure pointed to by BUF.

Second, access, Faccessat functions

When a file is opened with the open function, the kernel performs its access test based on the process's valid user ID and valid group ID. Sometimes, the process

You also want to test their access capabilities by their actual user ID and actual group ID. Even though a process may have been run with superuser privileges by setting the user ID

, it may still want to verify that its actual user can access a given file. The access and FACCESSAT functions are based on the actual user ID and the actual group ID

The access rights test. The function prototype is:

#include <unistd.h>int access (constchar *pathname,int  mode); int faccessat (int fd,constchar *pathname,int mode,int flag) ;

If the test file already exists, mode is F_OK; otherwise mode is the bitwise OR of these constants: R_OK, test read permission; W_OK

Write permission; x_ok, test execution permissions. The Faccessat function is the same as the Access function in the following two cases: one is the pathname parameter

Absolute path, the other is the FD parameter value is AT_FDCWD and the pathname parameter is a relative path. Otherwise, the FACCESSAT calculation is relative to the Open directory (by

The pathname of the FD parameter point. The flag parameter can be used to change the behavior of the Faccessat, and if flag is set to At_eaccess, the access check is

The valid user ID and valid group ID of the calling process, not the actual user ID and the actual group ID.

Three, umask function

The Umask function creates a mask word for the process settings file mode and returns the previous value. The prototype of this function is:

#include <sys/stat.h>mode_t umask (mode_t cmask);

Where the parameter cmask is a bitwise OR of the 9 constants. S_IRUSR: User reads; S_IWUSR: User writes; S_IXUSR:

User execution; S_IRGRP: group read; S_IWGRP: Group write; S_IXGRP: Group execution; S_iroth: other read; S_iwoth: other write; S_ixoth:

Other execution. When a process creates a new file or a new directory, it is bound to use the file mode to create the mask word. 1 in the File Mode creation screen Word

bit, the corresponding bit in the file mode must be closed.

Iv. chmod, Fchmod, Fchmodat functions

The 3 functions of chmod, Fchmod, and Fchmodat allow us to change the access rights of existing files. The prototype of the function is:

#include <sys/stat.h>int chmod (constChar *pathname,mode_t mode); int fchmod (int  fd,mode_t mode); int fchmodat (int fd,constchar *pathname,mode_t mode,int flag );

The CHMOD function operates on the specified file, while the FCHMOD function operates on the file that is already open. The Fchmodat function and the chmod function are

The following two cases are the same: one is the pathname parameter is the absolute path, the other is the FD parameter value is AT_FDCWD and the pathname parameter is the phase

to the path. Otherwise, Fchmodat calculates the pathname relative to the directory (which is pointed to by the FD parameter). The flag parameter can be used to change the behavior of Fchmodat,

When the At_symlink_nofollow flag is set, Fchmodat does not follow the symbolic link. In order to change the permission bit of a file, the effective use of the process

The user ID must be equal to the owner ID of the file, or the process must have superuser privileges.

Five, Chown, Fchown, Fchownat, Lchown functions

The following several chown functions can be used to change the user ID and group ID of a file. If the two parameter owner or any of the group is-1, the corresponding

The ID does not change. The prototype of the function is:

#include <unistd.h>int chown (constChar *pathname,uid_t owner,gid_t Group); int fchown (int  fd,uid_t owner,gid_t Group); int fchownat (int fd,constchar *pathname,uid_t owner,gid_t Group,int  flag); int lchown (constchar *pathname,uid_t owner,gid_t Group);

These 4 functions work similarly, except that the referenced file is a symbolic link. In the case of symbolic links, Lchown and Fchownat (set the At_sym

The LINK_NOFOLLOW flag) changes the owner of the symbolic link itself, not the owner of the file to which the symbolic link points. Fchown function Changes FD parameters

Point to the owner of the open file, and since it operates on an open file, it cannot be used to change the owner of the symbolic link.

The Fchownat function is the same as the Chown or Lchown function in the following two cases: one is the pathname parameter is the absolute path, the other is the FD parameter

The count value is AT_FDCWD and the pathname parameter is a relative path. In both cases, if the AT_SYMLINK_NOFOLLOW flag is set in the flag parameter,

The Fchownat behaves the same as the lchown behavior, and the Fchownat behaves the same as Chown if the At_symlink_nofollow flag is cleared in the flag parameter. If

The FD parameter is set to open the file descriptor of the directory, and the pathname parameter is a relative pathname, and the Fchownat function calculates the PA with respect to the Open Directory

Thname.

File functions in Linux (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.