当前位置:网站首页>Directory operations and virtual file systems
Directory operations and virtual file systems
2022-06-30 04:25:00 【Tra220123】
1.stat
Suppose a path is /opt/file, Then the search order is :
① read out inode No 2 term ( root directory inode), Find the root directory data block location
② Find the file name from the root directory data block opt The record of , Be alone from the record inode Number
③ read out opt The directory inode, Find out its data block location
④ from opt Find the file name in the data block of the directory file The record of , Read from the record inode Number
⑤ read out file Of documents inode
2.opendir(3)/readdir(3)/closedir(3)
Used to traverse the records in the directory data block .
opendir: Open a directory , Return to one Dir * The pointer represents the directory , similar FILE * Handle to pointer .
closedir: Close this handle , hold Dir * The pointer is passed to readdir Read the records in the directory data block , Return one point at a time struct dirent The pointer to , Repeated reading can traverse all records , After all the records are traversed readdir return NULL.
Structure struct dirent Definition :

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
void printDir(char *dirname) {
char pathname[1024];
DIR *dir;
struct dirent *dp;
struct stat st;
if (!(dir=opendir(dirname))) {
perror("opendir");
exit(1);
}
while (dp = readdir(dir))
{
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
continue;
}
sprintf(pathname, "%s/%s", dirname, dp->d_name);
if (stat(pathname, &st) < 0) {
perror("stat");
exit(1);
}
if (S_ISDIR(st.st_mode)) {
printDir(pathname);
}
printf("%s\t", dp->d_name);
}
putchar(10);
closedir(dir);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("usage:cmd + path");
return 1;
}
printDir(argv[1]);
return 0;
}3.VFS
Virtual Filesystem
Linux Supports a wide variety of file system formats , Then these file systems can mount To a directory , Let's see a unified directory tree , Directories and files on various file systems ls The orders look the same , Read and write operations are the same , How this is done ?
Linux The kernel makes an abstraction layer on top of various file system formats , Make the document , Catalog , Concepts such as read / write access are called abstract layer concepts , So the systems look the same , This abstraction layer is the virtual file system .

4.dup and dup2
Can be used to assign an existing file descriptor , Make two file descriptors point to the same file Structure .
If two file descriptors point to the same file Structure ,File,Status,Flag And read and write locations are only saved in file Structure , also file The reference count of the structure is 2.
If 2 Time open The same file gets two file descriptors , Then each descriptor corresponds to a different file Structure , There can be different ones File,Status,Flag And reading and writing positions .
Distinguish between these two situations :


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
int main(void) {
int fd, save_fd;
if ((fd = open("test.txt", O_RDWR)) < 0) {
perror("open");
exit(1);
}
save_fd = dup(1);
dup2(fd, 1);
close(fd);
write(1, "123456789", 9);
dup2(save_fd, 1);
write(1, "123456789", 9);
close(save_fd);
return 0;
}边栏推荐
- 《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
- 7-3 打怪升级 单源最短路
- Errno and PERROR
- Cloud native -- websocket of Web real-time communication technology
- 2021-07-14
- el-upload上傳文件(手動上傳,自動上傳,上傳進度)
- Day 11 script and game AI
- Mongodb learning
- JS file block to Base64 text
- Explain the underlying principles of JVM garbage collection in simple terms
猜你喜欢

OneNote software

Threejs realizes the simulation of river, surface flow, pipe flow and sea surface

Anonymous pipeline for interprocess communication

Threejs实现模拟河流,水面水流,水管水流,海面

FortiGate firewall quick initialization administrator password

Interview topic of MySQL

Sql语句遇到的错误,求解

管道实现进程间通信之命名管道

Share an example of a simple MapReduce method using a virtual machine

基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
随机推荐
网络层详解
Unity 在編輯器中輸入字符串時,轉義字符的輸入
Cloud native -- websocket of Web real-time communication technology
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
Ora-00907: missing right parenthesis problem supplement
Internship: interface case implementation
Modifier of JS regular expression
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
An error occurs when sqlyog imports the database. Please help solve it!
Anonymous pipeline for interprocess communication
FortiGate firewall filters the specified session and cleans it up
mysql更新数组形式的json串
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
JS generator
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
2021-11-04
Configure specific source IP in SLA detection of FortiGate sdwan
base64.c
输入输出及中断技术——微机第六章学习笔记