Read file metadata

An application can retrieve information about a File (also called File Metadata) by calling the stat and fstat functions.

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

int stat(const char* filename, struct stat *buf);
int fstat(int fd, struct stat *buf); Returns on success0, returns if there is an error- 1
Copy the code

Among them,struct stat *bufTo return the queried file metadata. forstatFunction, the first argument is the file name; forfstatFunction. The first argument is the file descriptor.

struct stat {
               dev_tst_dev; Device number of the fileino_tst_ino; Index node numbermode_tst_mode; File type and permissionnlink_tst_nlink; Number of hard linksuid_tst_uid; The user IDgid_tst_gid; Group IDdev_tst_rdev; Device type (If the file is a device file, it is the device numberoff_tst_size; The file sizeblksize_tst_blksize; I/O buffer size of the file systemblkcnt_tst_blocks; blockstime_tst_atime; Access timetime_tst_mtime; Modify the timetime_tst_ctime; Change the time};Copy the code

According tostatFunction to implement aLinuxthestatCommand similar to the following figure. LinuxThere are onstatThis command:

Reading directory contents

Applications can read directory contents with the readdir family of functions. If there is an error, readdir returns NULL and sets errno. Unfortunately, the only way to tell the difference between an error and an end-of-stream condition is to check if errno has changed since readdir was called.

// With the path name argument, returns a pointer to the directory stream, which is a extract of an ordered list of items.
DIR *opendir(const char *name);
// Returns a pointer to the next directory entry of the stream dirp. If no more directory entries return NULL, each directory entry is a structure
struct dirent *readdir(DIR *dirp);
// Although some Linux versions include other data members, both are standard on all systems. D_name is the file name,
D_ino is the file location
struct dirent{
    ino_t d_ino;
    char d_name[256];
Copy the code

The function Closedir closes and frees all resources.

The Shared file

  • Descriptor table: Each process has its own Descriptor table, indexed by the file Descriptor. Each open Descriptor entry points to an entry in the open file table.

  • Open File table: A collection of Open files is represented by a file table that is shared by all processes. Each file table contains the current file location, reference count, and a pointer to the corresponding entry in the V-Node table. Closing a descriptor reduces the reference count in the corresponding file table entry. The kernel does not remove this file table entry until its reference count is zero.

  • V-node table: This V-Node table is shared by all processes. Each entry contains most of the information in stat, including ST_mode and ST_size.

  • If the same process opens the same file twice, it will generate two different open file tables, with two independent file descriptors, so that the two open files can be read and written completely independently. (But obviously, it is not recommended to write both at the same time, which can easily make the file very messy)

  • forkThe child will create a copy of the same descriptor table as the parent, and the refcnT for the open file table will become 2. The parent and child will share the same open file table and, therefore, the file locationFile posIs shared, so that when one process reads in, another process reads in the new file location. (Of course parent and child processes can open and close files themselves, but this can get messy, so reference counting is necessary.)

  • ** Open file table close ** :closeFor a file descriptor, all it does is perform the refCNT of the open file table corresponding to the file descriptor- 1If refCNt becomes 0, the open file table will be deleted. If forked, all processes need to be closed to actually close it from an operating system perspective.

The I/O redirection

include <unistd.h>

int dup2(int oldfd, int newfd); Returns a nonnegative descriptor on success, or an error1.
Copy the code

Dup2 function: Copies descriptor entry oldfd to descriptor entry newfd, overwriting the previous description of descriptor entry new-fd.