当前位置:网站首页>File system and directory operations
File system and directory operations
2022-06-30 04:25:00 【Tra220123】
1.mmap
addr: If addr Parameter is NULL, The kernel selects the appropriate address in the process address space to establish the mapping . If addr No NULL, Then give the kernel a hint , What address should I start mapping from , The kernel will choose addr Start mapping at a suitable address above . After the mapping is established , The real mapping first address can be obtained through the return value .
len: The length of the part of the file to be mapped .off Parameters are mapped from where in the file , Must be an integer multiple of the page size .
filedes: Descriptor of the file .
prot: Yes 4 Species value :①PROT_EXEC: This section of the map is executable , Such as mapping shared libraries ②PROT_READ: This section of the map is readable ③PROT_WRITE: This section of the mapping can be written as ④PROT_NONE: This segment of the map is not accessible .
flag:MAP_SHARED: Multiple processes share the same file mapping ,MAP_PRIVATE: Multiple processes do not share the same file mapping
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
int main(void) {
int fd = open("test.txt", O_RDWR);
if (fd < 0) {
perror("open file");
exit(1);
}
int *p = mmap(NULL, 6, PROT_WRITE, MAP_SHARED, fd, 0);
((int *)((char *)p) + 1)[0] = 0x30313233;
munmap(p, 6);
return 0;
}2. Virtual file system VFS
【1】ext2 file system
The smallest unit of storage in a file system is a block , The size of a block is determined during formatting , Such as :mke2fs Of -b Option to set the block size to 1024,2048 or 4096 byte .
Boot block : Size is 1KB, from PC Standard setting , Used to store disk partition information and startup information , This block cannot be used by any file system occasional reads .
Superblock : Describe the file system information of the entire partition , Such as : Block size , File system version number , The last time mount And so on . A superblock has a copy at the beginning of each block group .
Block group descriptor table : It consists of many block group descriptors , How many block groups the whole partition is divided into corresponds to how many block group descriptors . Each block group descriptor stores the description information of a block group , Include inode Where does the table start , Where does the data block begin , Idle inode And how many data blocks there are . The block group descriptor table has a copy at the beginning of each block group , This information is important , So there are multiple copies .
inode Bitmap : Similar to block bitmap , Itself occupies a block , Each of them bit It means a inode Whether it is free or not .
inode surface : file type ( routine , Catalog , A symbolic link ), jurisdiction , file size , establish / modify / Access time and other information exist inode in , Each file has a inode.
Data blocks :
① General documents : The data of the file has data blocks .
② Catalog : All file names and directories in this directory have data blocks .( The file name is saved in the data block of the directory , Other information is saved in the file inode in ).
③ A symbolic link : If the destination pathname is short, it will be saved directly in inode in , To find... Faster , Otherwise, allocate a data block to save .
④ Device file 、FIFO,socket And so on : No data blocks , The primary and secondary equipment numbers of the equipment file are saved in inode.
【2】stat(2)
Read the file inode, And then put inode Fill in a for various file attributes in struct stat Structure is passed to the caller .
stat(1) The order is based on stat Functionally implemented .
stat You need to find according to the incoming file path inode.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
struct stat st;
if (argc < 2) {
printf("usage:cmd + filename,dirname\n");
exit(1);
}
lstat(argv[1], &st);
switch (st.st_mode & S_IFMT)
{
case S_IFREG:
printf("Is regular file\n");
break;
case S_IFDIR:
printf("Is directory\n");
break;
case S_IFCHR:
printf("Is charactor device\n");
break;
default:
printf("Is other file type\n");
break;
}
return 0;
}
边栏推荐
猜你喜欢

el-upload上传文件(手动上传,自动上传,上传进度)

JS inheritance

SQLyog导入数据库时报错,求帮解决!

Redis cache avalanche, breakdown and penetration

Huawei cloud native - data development and datafactory

FortiGate firewall configuration log uploading regularly

Technology sharing | broadcast function design in integrated dispatching

An error occurs when sqlyog imports the database. Please help solve it!

Blocking queue example

Machine learning notes
随机推荐
Unity 在编辑器中输入字符串时,转义字符的输入
Titanic(POJ2361)
Create thread in callable mode
2021-11-04
Unity échappe à l'entrée de caractères lors de l'entrée de chaînes dans l'éditeur
SQL append field
Basic knowledge of redis
Memorize unfamiliar words at SSM stage and update them from time to time
Huawei cloud native - data development and datafactory
Find the interface and add parameters to the form
errno和perror
How to use div boxes to simulate line triangles
What is the difference between synchronized and lock
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
股票利益【非dp】
How the FortiGate firewall rejects a port by using the local in policy policy
Matlab reads fig file and restores signal
FortiGate firewall configuration log uploading regularly
Salary management system based on servlet+jsp+mysql [source code + database]
Day 10 data saving and loading