Fourth chapter-File and directory __unix environment advanced programming

Source: Internet
Author: User
Tags bit set lstat mkdir parent directory readable save file
1, the file's attribute information:

1. Attribute structure body struct stat;

struct Stat {
            mode_t     st_mode;  File type and access permission bit
            ino_t         St_ino;      File I node number
            dev_t        st_dev;      The device number (including main device, minor number) of the file system to which the file belongs
            dev_t        St_rdev;     File is a block device, character equipment when the device number (main equipment, secondary equipment)
            nlink_t      st_nlink;
            uid_t         st_uid;      The user ID of the file owner
            gid_t         St_gid;      The user group ID for the owner of the file
            off_t          st_size;     File size (byte unit)
            time_t       st_atime;  The latest access time for the contents of the file
            time_t       st_mtime//The latest modification time of the contents of the file
            time_t       st_ctime;   The latest modification time for file properties
            blksize_t    st_blksize;//The file system's block size
            blkcnt_t     st_blocks;//The number 
    of blocks that the file occupies in the file system }

2. File type:
1, ordinary documents
2, directory files. The file name and file I node number that the file contains
3, block special files. File is a block device
4. special files of characters. File is a character device
5, FIFO. Named pipes, for interprocess communication
6, Socket. For network communication between processes, the process can be on the same machine or on a different machine.
struct the St_mode field in the stat structure that holds the type of file, and the following macros allow you to view the type of the file:

    S_isreg (Stat.st_mode). If the return result is true, the normal file
    s_isdir (Stat.st_mode). If the return result is true, the directory file
    s_ischr (Stat.st_mode). If the return result is true, the character special file
    s_isblk (Stat.st_mode). If the return result is true, the block special file
    S_isfifo (Stat.st_mode). If the return result is true, the named pipe file
    s_islnk (Stat.st_mode). If the return result is true, then the
    s_issock (Stat.st_mode) is eligible for the linked file. If the return result is true, the socket file

3. function to get file property information

#include <sys/stat.h>
 int stat (const char *restrict pathname, struct stat *restrict);
 int Fstar (int filedes, struct stat *buf);
 int Lstat (const char *restrict pathname, struct stat *restrict);
                                                                         function return value: Successfully returned 0, error return-1;

Pathname for file path, fieldes for file descriptor, buf save file attribute information
The Lstat function, when a file is a symbolic linked file type, returns only the file attributes of the symbolic link file itself, not the file attributes that the symbolic link points to.

Percentage of various file types in the operating system 2, File access rights

The permission information for a file is shown in the following illustration:

1, the first column of the-rwxr-xr-x:
-: Indicates that the file is a normal file
RWX: The permissions that the owner of the file has, where the owner has permission to read, write, execute
R-x: Permissions for all groups in the file, where all groups have the right to operate read, execute
R-x: Files Other users have permissions, where other users have the right to operate read, execute

2, the third to fourth column of Liuxiaowu Liuxiaowu:
The third column represents the file owner, the St_uid field in the struct stat structure
The fourth column represents all groups of files, st_gid fields in the struct stat structure

3.425 of the Fifth column:
Size of file, in bytes

4, 第六、七、八 Nov 11 23:02
The time when the contents of the file were last modified

5, the Nineth column of index.html:
The name of the file

Macros that view permissions information for a file by struct the St_mode field in the stat structure:

S_IRUSR (Stat.st_mode), the result is true, indicating that the user can read
s_iwusr (stat.st_mode), the result is true, that the user can write
s_ixusr (stat.st_mode), the result is true, indicating that the user can perform
s_irgrp (Stat.st_mode), the result is true, that the group
is readable S_IWGRP (Stat.st_mode), the result is true, the group can write
s_ixgrp (stat.st_mode), the result is true, the group executable
S_iroth (stat.st_mode), the result is true, indicating that other readable
S_iwoth (Stat.st_mode), the result is true, indicating other
writable S_ixoth (Stat.st_mode), the result is true, indicating that other executable

the meaning of the Execute permission:

In order to open the/usr/include/stdio.h file, you need to have execute permissions on the directory/,/usr,/usr/include, and then also have the appropriate permissions on the Stdio.h file, depending on the mode in which it is opened (read-only, read-write, etc.)

The difference between the execution and read permissions of a directory:
Read permission for the directory to allow reading of file list information contained in the directory
Permission to execute a directory means that when we want to access a part of the path name of the file, the execution permissions on the directory allow us to pass the directory 3, set the user ID and set the group ID

related IDs

Actual User id: the
    actual group ID of the user who created the process: User group valid User ID of the user who created the process: the
    Active Group ID of the process owner
:
    All Groups
owned by the process Additional group ID: the
    process belongs to an additional group. A process can have a valid group, but can have more than one additional group. An additional group is a supplement to a valid group that can enlarge the operation of a process.
User ID: The process saves a
    valid user ID in the EXEC function when it is executing the
set group ID of the saved setting:
    when the process executes the EXEC function, Saves a valid group ID in this

set User ID for file and set user group ID

When executing a program file, the valid user ID of the process is usually the actual user ID, and the valid group ID is usually the actual group ID. However, you can set a special flag in the St_mode field of a file, meaning "when executing this file, set the process's valid user ID to the file owner's user ID", which is the file's set user ID (same as setting the group ID).
With this flag, once the program file owner is superuser and the file's St_mode field is set to set the user ID flag, the process is executing the program file, the process's valid user ID is the file owner, the Superuser, so the process has the power user's execution rights. If a passwd file is executed, allowing any user to modify his or her password, that is the principle used.

permissions Check when a process is operating on a file:

1. If the active user ID of the process is 0 (superuser), access is allowed.
2. If the active user ID of the process is equal to the owner ID of the file, access is allowed if the file owner has the appropriate permissions on the file. The appropriate permission is if the process opens the file for reading, the owner's read bit should be 1, and if the process opens the file for writing, the owner write bit should be 1, and if the process will execute the file, the owner's execution bit should be 1
3, or one of the valid group IDs of the process or an additional group ID equals all the file group IDs. If all groups of files have appropriate permissions on the file, access is allowed.
4, if other users have the appropriate permissions on the file, then allow access to

When you perform a file operation, the above four steps are executed sequentially. If a valid user ID for a process is equal to the owner ID of the file, then the 3rd step is not checked if the file owner does not have the appropriate permissions on the file. By analogy, only one step is checked from top to bottom. 4 owner ID and all group IDs for new files

When a process creates a new file, the owner of the file defaults to the valid user ID of the process. And all of the files in the group, under the Linux system is the following two kinds of situations:
1. When the setting group ID bit of the file-owning directory is set, all group IDs for the new file are all group IDs of the owning directory
2. Otherwise valid group ID 5 Access function for the process

When a program file is executed, if the set user ID (the set group ID) bit is set, the active user ID of the executing program file's process is the owner ID of the file, and the process's operation permission becomes the file's owner ID. At this point, if the process wants to determine whether its own permissions have permission to manipulate the file, you can use the Access function to get it. The Access function uses the actual user ID of the process to determine whether there is permission to manipulate the program files.

#include <sys/unistd.h>
int access (const char *name, int mode)
    return value: Successfully returned 0, error returned-1
    mode range, R_ok: readable, W _OK: Writable, X_OK: Executable, F_OK: Test file exists
#include <sys/unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include < fcntl.h>

int main (int argc, char *argv[]) {for
    (int i = 1; i < argc; i++) {
        if Access (argv[i), R_OK) = = 0) {
            printf ("Shiji can read\n");//actual user readable
        }

        if (open (argv[i), o_rdonly) > 0) {
            printf ("Youxiao Can read\n ")//valid user readable
        }}
}

Test:
1. Compile the file:

Gcc-std=c99 Testaccess.c-o testaccess 

2. Permission information for program Files:

-rwxrwxr-x 1 root Liu  7415 November 21:55 testaccess*

Permission information for 3./etc/shadow file:

-rw-r-----1 root Shadow 1220  July  2 21:59/etc/shadow

4. Test the process of executing program files for/etc/shadow Read permissions:

$./testaccess/etc/shadow

You can see that nothing is output, the process does not have the appropriate operation rights

5. Set the owner of the program file as root and set the user ID bit:

sudo chown root testaccess
sudo chmod u+s testaccess

6. Display permission information for program Files:

-rwsrwxr-x 1 root Liu  7415 November 21:55 testaccess*

7. Test the process of executing program files for/etc/shadow Read permissions:

$./testaccess/etc/shadow
Youxiao can read

You can see that the process can open the file with Read permissions, which indicates that the actual user ID of the process does not have/etc/shadow permissions on the file, and that the valid user ID for the process has permissions because the valid user ID for the process is the owner of the program file root 6 umask function

The umask value is called a file mode to create a masked value. When you create a new file, you specify the appropriate action permissions for all groups/groups/other users of the file. The final operational permissions for the new file are based on the specified permissions, minus the Umask setting value. For example, when Umask is 002, if the permission to specify a new file is 777, the new file has the final permission of 775

#include <sys/stat.h>

int umask (mode_t cmask);
    Returns a value, the previous file pattern creates a masked value
7 chmod and Fchmod functions
#include <sys/stat.h>

int chmod (const char *name, mode_t mode);
int fchmod (int fiedes, mode_t mode);
    return value, return 0 successfully, error return-1

Chmod/fchmod function, which is used to change the permission bits of a file.
Modifications can only be made if the process's valid user ID equals the owner ID of the file, or if the process has superuser privileges.
The chmod function only modifies the permission information and does not modify the file contents. This means that it updates only the I node information.

Mode takes value

Owner reads, writes, executes, reads and writes the execution
s_irusr
s_iwusr
s_ixusr
s_irwxu
//All groups reads, writes, executes, reads and writes executes
s_irgrp
S_ Iwgrp
s_ixgrp
s_irwxg
//Other read, write, execute, read-write execution
s_iroth
s_iwoth
s_ixoth S_irwxo Set the user ID bit, set the user group ID bit, set the save body (stuck bit)
s_isuid
s_isgid
s_isvtx

When a process creates a new file, the If this happens when all the group IDs for a file are not a valid group ID for the process or one of the additional group IDs (which is possible when the parent directory sets the group ID bit), the set group ID bit is automatically closed if the new file sets the group ID bit. This prevents the user from creating a set group ID file that is owned by a group other than the one to which the user belongs. Prevents a user from creating a new file by setting the group ID bit, and then performing the appropriate action with the permissions of the parent directory group of the file. 8 Sticking bit

Sticking bit means that once the file is loaded into memory, its information is kept in the swap area without being replaced. So the next time the file needs to be executed, it can be loaded quickly.

the sticking bit of the catalogue:
When the directory has a sticky bit set, if the user has write permission to the directory, then when the file in that directory is deleted or renamed, one of the following conditions must be met:
1. The user is a super user
2. User as Directory Owner
3. The user is the file owner
such as/tmp directory, whose permission information is:

DRWXRWXRWT.  9 root  4096 November 23:13 tmp

As you can see, the owner is root, all groups are root, and the other user's permissions are read/write execution (+ stuck bit). Each user can create their own files in the directory, but each user can only delete or rename files that belong to them. 9 Chown,fchown,lchown function

#include <sys/stat.h>

int chown (const char *name, uid_t owner, gid_t GID);
int fchown (int fiedes, uid_t owner, gid_t GID);
int Lchown (const char *name, uid_t owner, gid_t GID);
        return value, return 0 successfully, error return-1

If the file is a symbolic link, the Chown,fchown function modifies the file that the symbolic link points to, and the Lchown function modifies the symbolic link file itself.
When a function executes, if the UID or GID has a-1, the owner or all group IDs are unchanged.

Limit rule:
1. Only the Superuser process can modify the owner ID of the file
2. When you invoke the above function to perform the owner modification on a file, you must meet the following two conditions:

1. The group ID of a file can be set when the valid user ID of the process equals the file's owner ID of
2.owner to 1 or the owner ID of the file, and one of the process's valid group IDs or additional group IDs equals GID

When a non-superuser modifies the owner ID or all group IDs of a file, the set user ID bit and the set group ID bit of the file are cleared. This is to prevent the owner ID or all group IDs of the files from being modified, and to perform the permissions of other users by using the Set user ID bit of the file or by setting the group ID bit. 10 file Length

The st_size in the struct stat structure represents the byte length of the file's content, St_blksize represents the storage system in which the file is stored, the most appropriate buffer block size, and the st_blocks represents the number of blocks of 512 bytes of the file.

void:
A hole is an uninitialized data in a file, for example, when the data is written directly at the end of the end of the file, the 5 bytes after the end of the file are empty, and it does not occupy the actual storage space.
But when you read the data in the file and write the new file, the hole fills in 0, so that the original empty data in the new file takes up the actual storage space.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h >
#include <string.h>

int main (int argc, char * argv[]) {
        int fd;
        if (FD = open ("Kongdong.txt", O_wronly | O_creat, S_irwxu | S_irwxg | S_IRWXO)) < 0) {
                printf ("Open error:%s\n", Strerror (errno));
        }

        Write (FD, "1234", 4);

        Lseek (FD, 100000, seek_end);

        Write (FD, "5678", 4);

        return 0;

}

When the above program executes, it creates a new file Kongdong.txt with a file size of 10008 bytes:

-rwxrwxr-x 1 Liuxiaowu Liuxiaowu 10008 Dec  2 17:53 kongdong.txt 

The du-s command displays the number of blocks that the file occupies:

8       Kongdong.txt

The cat kongdong.txt > Kongdong_copy.txt, which shows the size of the Kongdong_copy.txtx file is also 10008 bytes, but the du-s command shows that the number of blocks that the file occupies is:

-rw-rw-r--1 Liuxiaowu Liuxiaowu 10008 Dec  2 17:54 kongdong_copy.txt
a      kongdong_copy.txt

This indicates that the file generated by the above program Kongdong.txt is empty. 11 file Truncation

#include <unistd.h>
#include <sys/types.h>

int truncate (const char *path, off_t length);
int ftruncate (int fd, off_t length);

When length is shorter than the original length of the file, the file is truncated and the contents of the file after length are no longer accessible.
When length is longer than the original length of the file, the file is extended, and the expanded file content is read as 0, which is empty. 12 File System

The diagram above is the structure diagram of the file system. A disk is divided into multiple partitions, each of which is a file system, including autonomous blocks, super blocks, and cylinder group information. Each cylinder includes a copy of the Super block, the I node, and the data block.

I node in memory next to the storage, in the I node includes file type, file current location, file size, file access rights, point to the actual content of the file in the data block pointers and other information, struct STAT structure in addition to the I node number, file name, The rest of the information is from the I node.

The data Block is the place that holds the actual content of the file, depending on the file type, it can be subdivided into the catalog block and the data block. The directory block holds the directory information, the contents of the file that the data block holds. The format of the catalog block is shown in the figure, including the I node number and the file name.

Each i node has a reference link number that indicates how many directory entries are pointing to the I node.

For a file, the data space occupied by the file can be freed only if the reference count of the I node is 0 o'clock. This is also why the function name of the delete file is unlink, not delete the directory

entry that does not contain any subdirectories for the directory, with a reference link number of at least 2, One is that its directory block itself has a directory entry that points to itself (called.), and a directory block with its parent directory has a directory entry that points to the directory entry it contains. There is a directory entry that contains subdirectories, the number of reference links is at least 3, in addition to the two above, and the directory block of its subdirectories, there is a directory entry that points to its parent directory (called. )。

As I node 1278 in the figure above, the figure shows two reference links, one in the directory itself (.) and one in the directory block of its parent directory.

As can be seen from the above, for MV Command, the operation process does not move the file content address, only in the current directory to delete a directory entry, and in another directory added a directory entry.

I node number is for a file system, the number of I nodes in different file systems may be duplicated. unlink, link, rename, remove functions

#include <unistd.h>

int link (const char *oldpath, const char *newpath);
int unlink (const char *pathname);

#include <stdio.h>

int rename (const char *oldpath, const char *newpath);
int remove (const char *pathname);

link function:
A new directory entry NewPath is created, and the I node number in the directory entry is the same as the I node in the OldPath directory entry.

Because the I node number is valid only within the same file system, the link function cannot establish links across the file system.

If NewPath is a directory, you can create a success only if you have superuser privileges. Because of the hard links to the directory, it is possible to generate an infinite loop.

unlink function
unlink function to reduce the number of reference links in the pathname directory entry to I node minus one.

Its file space can be freed only if a file has a reference link number of 0 and no process opens it. So many temporary files are created as follows: Open a nonexistent file and immediately unlink it, at which point the number of reference links to the file is 0, but the process also opens it. When the process finishes executing, the kernel checks whether the file has a process open number of 0, and if 0, continues to check the number of reference links to the file, and if the number of links is 0, frees the storage space occupied by the file.

Remove function
If pathname is a file, the function works just like unlink. If pathname is a directory, the function acts like the RmDir function.

From its header file stdio.h, this function is defined by ISO C.

Rename function
Rename the OldPath file to NewPath. For files and directories, the effect is as follows:

OldPath and NewPath are all files:

If the NewPath already exists, the NewPath is deleted first.
deletes the OldPath entry and creates a new NewPath directory entry.
If the file is a symbolic link, the symbolic link itself is manipulated.  

OldPath and NewPath are directories:

If the NewPath already exists, the NewPath is deleted first, which can only be an empty directory.
deletes the OldPath entry and creates a new NewPath directory entry. The
path to the NewPath directory entry, which cannot be a subdirectory of the OldPath directory entry. If you cannot rename/usr/foo to/usr/foo/foo1 because it deletes the/usr/foo directory and then creates a new/usr/foo/foo1 directory, it will not succeed because the Foo directory no longer exists

Because you need to delete the OldPath directory entry and create a new NewPath directory entry, you need to have write and execute permissions on the OldPath parent directory, NewPath parent directory.

And if NewPath already exists, the process also needs to have write permission to NewPath. ( still don't understand why you need to have write permission ) 14 Symbolic Links

Hard links have the following two disadvantages:
1. Cannot Cross file system
2. Only super users can create hard links to directories (because directory hard links can cause infinite loops)
Based on this, the addition of symbolic links. A symbolic link is a file type whose content is the file path to the actual file that points to.
Generally, the function of the operation of the file is to the symbolic link to the actual file operation, in addition to a few special functions of the symbolic link itself to operate:

#include <sys/stat.h>

 int lstat (const char *restrict path, struct stat *restrict buf);
#include <unistd.h>

ssize_t readlink (const char *restrict path, char *restrict buf, size_t bufsize);

Symbolic links introduce loops in the file system:

CD apue/4 mkdir testsymbol cd testsymbol mkdir foo ln-s foo foo/testdir 

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.