当前位置:网站首页>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;
}边栏推荐
- Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
- Project safety and quality
- OneNote software
- idea灰屏问题
- Myrpc version 4
- Iterator of JS
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
- What is the difference between synchronized and lock
- 深度融合云平台,对象存储界的“学霸”ObjectScale来了
- Day 10 data saving and loading
猜你喜欢

尝试链接数据库时出现链接超时报错,如何解决?

I get n offers in two months. I don't have any difficult interviewers here

深度融合云平台,对象存储界的“学霸”ObjectScale来了

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

在大厂外包呆了三年,颠覆了我的认知!

Machine learning notes

基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】

Configure specific source IP in SLA detection of FortiGate sdwan

El upload upload file (manual upload, automatic upload, upload progress)

Use of thread pool
随机推荐
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
Unity 在編輯器中輸入字符串時,轉義字符的輸入
Daily summary of code knowledge
487-3279(POJ1002)
errno和perror
7-3 single source shortest circuit for strange play upgrade
I spent three years in a big factory outsourcing, which subverted my understanding!
知识点滴 - 如何用3个简单的技巧在销售中建立融洽的关系
Myrpc version 3
FortiGate firewall configuration link detection link monitor and status query
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
Indefinite parameters of JS function
QT creator 8 beta2 release
Explain the underlying principles of JVM garbage collection in simple terms
FortiGate firewall modifies the default timeout of a session
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
[从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
Myrpc version 6
Introduction to cloud native + container concept
thinkphp5实现导入功能