当前位置:网站首页>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;
}边栏推荐
- Tea mall system based on SSM framework [project source code + database script + report]
- A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
- The same node code will cause duplicate data
- Anonymous pipeline for interprocess communication
- [cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
- Read / write lock example
- 什么是光耦电路,实际使用中应该注意些什么?
- idea灰屏问题
- Myrpc version 6
- 找到接口在表单里加参数
猜你喜欢

Day 10 data saving and loading

MySQL DDL change

Myrpc version 2

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

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

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

Junior students summarize JS advanced interview questions

El upload Upload file (Manual upload, Automatic upload, upload progress)

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

Sql语句遇到的错误,求解
随机推荐
Collinearity problem
Day 11 script and game AI
输入输出及中断技术——微机第六章学习笔记
internship:接口案例实现
工程安全和工程质量
QT 6.3.1conan software package release
Project safety and quality
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
MySQL updates JSON string in array form
How to write a conditional statement to obtain the value of the maximum time in a table using a MySQL statement
Unity 在编辑器中输入字符串时,转义字符的输入
Array of small C
尝试链接数据库时出现链接超时报错,如何解决?
Modifier of JS regular expression
An error occurs when sqlyog imports the database. Please help solve it!
With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
FortiGate firewall filters the specified session and cleans it up
Salary management system based on servlet+jsp+mysql [source code + database]
Internship: interface case implementation
Default value of JS parameter