"Linux/unix System Programming Manual" Reading notes catalogue
In Linux, everything is file. Therefore, the file system occupies an important position in the Linux system. This article is mainly about the properties of the file, just a little mention of the file system, in the future if more in-depth research will be written.
The relationship between disk partitioning and the file system
Files (directories) in the file system have unique records (I-node) on the I-node table. I-node points to a block of data through a block pointer, which is the data for the file corresponding to the I-node.
The relationship between the I-node and the data block is as follows:
Because Linux supports many types of file systems, there are differences in the implementation of each file system. To address this discrepancy, the kernel provides a virtual file system by adding an abstraction layer between the application and the file system to allow programmers to write applications without regard to the type of file system.
Get to know the file system first-----file properties
Because everything is a file, we often need to get information about the file in order to know if it is a normal file or a catalog file, or to know its timestamp. At this point we can get the file information we want through the stat () system call.
1 #include <sys/stat.h>23int stat (constcharstruct stat *statbuf); 4 int lstat (constcharstruct stat *statbuf); 5 int fstat (intstruct stat *statbuf);
The call returns 0 successfully, and the failure returns-1. The file information is stored in the statbuf.
Pathname is the file name, and for Lstat, if the file is a symbolic link, the information returned for that link is not the point.
Fstat is the information that gets the file that corresponds to the open file descriptor.
Then look at the format of the stat structure:
which will st_mode and s_ifmt phase, you can get the file type, and St_mode low 12 bits for file permissions. In addition to the time stamp to note that St_atime represents the last access time, St_mtime represents the last time the file was modified, St_ctime represents the time when the last file state changed.
We often have access to file permissions. The user is divided into 3 categories: The owner of the file, the user group where the file belongs, and other users. There are 3 types of permissions for each class of users: readable, writable, and executable.
Practice:
15-6: Command chmod the role of A+RX file is to grant Read permissions to all categories of users, and when file is a directory or any class of file that has executable permissions, it will grant executable permissions to all categories of users.
1#include"Tlpi_hdr.h"2#include <unistd.h>3#include <stdio.h>4#include <sys/stat.h>5#include <sys/types.h>6 7 #defineREAD (S_irusr | S_irgrp | S_iroth)8 #defineEXECUTE (S_ixusr | S_ixgrp | S_ixoth)9 Ten One intMainintargcChar*argv[]) { A structstat sb; - inti; - the if(ARGC <1|| strcmp (argv[1],"--help") ==0) -Usageerr ("%s filename...\n", argv[0]); - - for(i =1; i < argc; i++){ + if(Stat (Argv[i], &sb) = =-1) -Errexit ("Stat"); + A if((Sb.st_mode & s_ifmt) = =S_ifdir) { at if(Chmod (Argv[i], Sb.st_mode | READ | EXECUTE) = =-1) -Errexit ("chmod"); - } - - if((Sb.st_mode & s_ifmt) = =S_ifreg) { - if(Chmod (Argv[i], Sb.st_mode | READ) = =-1) inErrexit ("chmod"); - if((Sb.st_mode & s_ixusr) | | (Sb.st_mode & s_ixgrp) | | (Sb.st_mode &s_ixoth)) to if(Chmod (Argv[i], Sb.st_mode | EXECUTE) = =-1) +Errexit ("chmod"); - } the } * $ exit (exit_success);Panax Notoginseng}
View Code
Results:
[Email protected] tlpi]$ls-LD dir filePROGD---------2LAN LAN4096Jul - A: to dir-R--------1LAN LAN0Jul - A: to file-r-x------1LAN LAN0Jul - A: toProg[[email protected] tlpi]$./15.6 dir fileProg[[email protected] tlpi]$ls-LD dir filePROGDR-xr-xr-x2LAN LAN4096Jul - A: to dir-r--r--r--1LAN LAN0Jul - A: to file-r-x--x--x1LAN LAN0Jul - A: toProg
-------first to fill out a small pit, long time no blog, this period to find an internship, prepare for the exam ... I'm finally done .... There are too many things to think about .... Intend to write a summary, the end of the six-year thing ....
-------continue to work hard, and then will not regret .... Continue to consolidate Linux system programming, read more, multi-code, think more!!!!!!