Unix/linux file attributes and their system calls

  

TABLE 1. Get file attributes (stat, fstat, lstat)

2. File type 2.1 File types include the following 2.2 file type test macros 2.3 File Type Test Example 3. Permissions for Files 3.2 Small Details on Permissions 3.3 Setting User ID and Setting Group ID 3.4 System Call for File Permission Operation 3.4.1 umask Function 3.4.2 chmod and fchmod 3.4.3 chown fchown and lchown 4. The time of the file 4.1 utime function

The file has its attributes, such as type, permission, length, status, etc. This article will summarize this and learn related system calls.

1. Get file attributes (stat, fstat, lstat)

Three stat functions can return information about files.

[code] #include

int stat(const char *pathname, struct stat *buf);int fstat(int filedes, struct stat *buf);int lstat(const Char *pathname, struct stat *buf); return value: success 0 failure -1 [/code]

The difference between the three functions is that stat first parameter is the file name, fstat first parameter file Descriptor, lstat The first parameter is the file name, and he returns the information of the symbolic link, not the information of the symbolic link reference file.

The second parameter, the buf structure pointer, is a structure we must provide. Its basic form: struct stat { mode_t st_mode; /* file type and file permissions */ino_t st_ino; /* associated with the file inode */dev_t st_dev; /* saved file device */nlink_t st_nlink; /* The number of hard links on the file */uid_t st_uid; /* The file owner UID */gid_t st_gid; /* The file owner GID */off_t st_size; /* The number of file bytes, the normal file is valid */time_t st_atime; /* The last time the file was accessed */time_t st_mtime; /* The last time the file content was modified */time_t st_ctime; /* The time the file permission, owner, group, or content was last modified */}Use stat The most likely function is the `ls -l` command to get all the information about a file. Below we will follow the contents of stat to understand the file attribute information.

2. The file type structure stat has file type information in st_mode (st_mode contains both file type and file permissions).

2.1 File types include the following 1. Normal file /regular file. The longest file type. The data can be either text or binary, no difference for the Unix kernel.

2. Directory file /directory file. This file contains the names of other files and pointers to information about these files.

3. block special file. This file type provides buffered access to the device. Each access is done in fixed length units.

4. Character special file This type of file provides unbuffered access to the device. The length of each visit is variable.

5. FIFO This type of file is used for interprocess communication and also becomes a named pipe.

6. Socket /socket This file type is used for network communication between processes.

7. Symbolic link /sysbolic link This type of file points to another file.

2.2 Macros for File Type Testing For a file, the following macros can be used to determine the file type. The parameters for these macros are the st_mode members of stat.

S_ISREG() Tests if it is a normal file S_ISDIR() Tests whether it is a directory file S_ISBLK() Tests whether it is a block special file S_ISCHR() Tests whether it is a character special file S_ISFIFO() Tests whether the pipe S_ISSOCK() test Is the socket S_ISLNK() test whether it is a link file

2.3 File type test instance [code] filename: file_type.c #include #include #include #include

int main(int argc , char *argv[])

{ int ret;int i;struct stat stat_buf;char *p_file_type;

for (i = 1; i < argc; i++) { if ( (ret = lstat(argv[i], &stat_buf)) == -1) { fprintf(stderr, "%s ——> lstat error!\ ", argv[i]);continue; } if (S_ISREG(stat_buf.st_mode))

p_file_type = "regular";else if (S_ISDIR(stat_buf.st_mode))

p_file_type = "dir";else if (S_ISCHR (stat_buf.st_mode))

p_file_type = "character special";else if (S_ISBLK(stat_buf.st_mode))

p _file_type = "block special";else if (S_ISLNK(stat_buf.st_mode))

p_file_type = "symbolic link";else if (S_ISFIFO(stat_buf.st_mode))

p_file_type = "fifo";else if (S_ISSOCK(stat_buf.st_mode))

p_file_type = "socket";else p_file_type = "unknow";printf("%s ——> %s \ ",argv[i], p_file_type);}

return 0;}

[/code]test[code] [test@test stat_code]$ ./file_type /dev/Initctl /etc/passwd /dev/sda /dev/tty /dev/log /dev/cdrom /dev/initctl ——> fifo /etc/passwd ——> regular /dev/sda —&mdash ;> block special /dev/tty ——> character special /dev/log ——> socket /dev/cdrom ——> symbolic link [test@test stat_code]$ [/code ]




Copyright © Windows knowledge All Rights Reserved