File Attributes in Linux

Source: Internet
Author: User

++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/6844692

++ ++

 

When we use ls-l filename, this shell command will print the detailed information of the file, such:

The detailed information of these files is stored in a struct stat. When the file system runs, it loads the detailed information of the files from the disk to the kernel space memory, the user space can obtain the struct information by calling the stat () and fstat () functions.

Stat struct:

Struct stat {

Dev_t st_dev;/* file system device Number */

Ino_t st_ino;/* I node number */

Mode_t st_mode;/* file type, permission bit */

Nlink_t st_nlink;/* Number of hard links */

Uid_t st_uid;/* master user ID */

Gid_t st_gid;/* master group ID */

Dev_t st_rdev;/* Special file device Number */

Off_t st_size;/* file byte size */

Blksize_t st_blksize;/* block size */

Blkcnt_t st_blocks;/* number of blocks occupied by files */

Time_t st_atime;/* last access time */

Time_t st_mtime;/* last modification time */

Time_t st_ctime;/* Last file state modification time */

};

First, its type has been passed by typedef, so it cannot be seen as its type. You can use the grep command to view it (for specific operations, refer to section 1). We can compare the file information, basically, this struct contains the basic information of the entire file. by reading this struct, You can implement a simple ls-l shell command.

 

Stat Function

# Include <sys/types. h>

# Include <sys/stat. h>

# Include <unistd. h>

Int stat (const char * path, struct stat * buf );

Function: view file or directory Properties

Parameter: path is the file path, and buf is the pointer of the stat struct.

Returned value: 0 is returned for success, and-1 is returned for error.

Now we can use this function to obtain the stat struct, so we can get the same effect of ls-l through the read struct.

Let's look at an example:

# Include <stdio. h>

# Include <sys/stat. h>

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

{

If (argc! = 2 ){

Printf ("Usage: <pathname> \ n ");

}

Int I = 0;

Struct stat buf;

If (stat (argv [1], & buf) <0 ){

Perror ("stat ");

}

Printf ("% d % s \ n", buf. st_mode,

Buf. st_nlink, buf. st_uid, buf. st_gid,

Buf. st_size, buf. st_atime, argv [1]);

Return 0;

}

Running result:

We found that, except for the number of hard links, the file size and file name, all others are different.

It seems that there are still many things to deal with. Let's analyze them.

The first is st_mode, which is a member of the int type, and ls-l shows that it is a string.

St_uid and st_gid are also int-type members, while ls is the user name.

The date is completely different. We solve these problems one by one.

 

First, let's look at st_mode.

S_ISUID settings during execution-user-I D

S_ISGID: set when execution-group-I D

S_ISVTX

 

S_IRWXU user (owner) reads, writes, and executes

S_IRUSR user (owner) read

S_IWUSR user (owner) Write

S_IXUSR user (owner) Execution

 

S_IRWXG Group read, write, and execute

S_IRGRP Group read

S_IWGRP group write

S_IXGRP group execution

 

S_IRWXO other reads, writes, and executions

S_IROTH other reads

S_IWOTH other writes

S_IXOTH other executions

These permissions have been learned before the open, and they all use a set of macro definitions.

We use grep to get its value and convert it to binary:

 

 

From this figure, we can easily see that each permission bit corresponds to a bit. When this bit is 1, it proves that the permission is 0 and there is no permission, which can also be explained, at that time, why did we use chomod to modify the user permission? Add 777 to indicate that all permissions are granted.

Therefore, if we want to implement the ls-l file display permission function, we only need to check whether the mode Value and the corresponding token have the permission.

If (buf. st_mode & S_IRUSR)

Putchar ('R ');

Else

Putchar ('-');

 

Note that if the ls-l directory name is used, the first permission bit is d, and the first permission bit is d, we also need to determine the type of a file. The principle is very similar to the above file permissions.

 

 

Therefore, we can use a similar method to obtain its type:

If (buf. st_mode & S_IFREG)

Putchar ('-');

If (buf. st_mode & S_IFDIR)

Putchar ('D ');

However, the system has provided several macros for us, as shown below:

 

 

Therefore, we can use these macros to replace our operations:

If (S_ISREG (buf. st_mode)

Putchar ('-');

If (S_ISDIR (buf. st_mode)

Putchar ('D ');

 

Through the analysis of st_mode members, we can conclude that st_mode uses 16 bits to indicate the type and permissions of a file, which saves memory resources, bitwise operations can speed up the operation. Summary:

 

Now let's see how to get the user name.

We can get the master user ID and group ID through st_uid and st_gid. We should remember that when we first started learning basic linux operations and shell programming, /etc/passwd stores the user's account information as follows:

Root: x: 0: 0: root:/bin/bash

Bin: x: 1: 1: bin:/sbin/nologin

Daemon: x: 2: 2: daemon:/sbin/nologin

They are all separated. The first is the user name, and the third is the corresponding user ID. We can retrieve the corresponding user name by reading the content of the passwd file. Obtain the group name by reading the/etc/group file.

If you do not want to write this function like this, our system call interface also provides such a function to retrieve the user name and group name by ID:

Getpwuid ();

Getgrgid ();

The specific usage of these two functions should be done by man, or the passwd method should be used by myself. Hey!

 

Last, display time.

In this case, we need to understand the time mechanism in Linux. The time in the system is represented by a large number, which is from January 1, January 1, 1970 (UTC) the number of seconds from the start time to the current time, so it is a large number. Since it is a number of seconds, we can convert it to the current time, you can use the library function directly. Because there are many time display formats, You need to convert the converted date and time.

Localtime () converts the time in seconds into a time struct in the local time zone, and then prints the time in the format of the date through strftime. Here, you can also go to man.

 

Advanced

We can write the simplest ls-l command through the above, but there are still a lot of details not taken into account, let's look at the following:

When the user permission of the test.txt file has the execution permission and sets its s bit, it is displayed in lower case s. When the user permission does not have the execution permission, its s bit is capitalized, so we need to further modify our ls-l here. T-bits are displayed in the same way.

 

When we modify the user master, we can specify a uid. For example, below, the user who sets the file master as the 9999 master but uses 9999 as the user ID does not exist, the system ls will be shown as follows, and its uid will not be displayed directly.

 

When the details of a file are displayed, if this is a common file, there is no problem, but if the current file is a linked file, the system's ls will be shown as follows:

 

It prints the details of the link file, followed by the file to which the link file points (the link file in Linux is similar to the shortcut in windows) here, we need to consider a little more. If the current file is a link file, we need to print out the file pointed to by this link file. The readlink system call is used here. Let's go to man and very easily.

 

When multiple files are displayed, note that the system ls-l is always neatly arranged. As you can see, when printing information, the longest character length in the current column is printed, and its file names are sorted in descending order.

 

++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/6844692

++ ++

 

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.