This is the 25th day of my participation in the August Challenge
Xv6 file system interface
Reference: xv6-RISCV-book 1.4 File System
File system call
System call | Description |
---|---|
int chdir(char *dir) | Change the current directory |
int mkdir(char *dir) | Creating a new directory |
int open(char *file, O_CREATE) | Create a new file |
int mknod(char *file, int, int) | Create a new device file (the last two parameters areThe main equipment,,A device number, which uniquely identify a device in the kernel), reads and writes to device files are forwarded to the device, not the file system. |
int link(char *file1, char *file2) | Create a new link (name) file2 for file1 |
int unlink(char *file) | Remove a file (nlink -= 1). When nlink == 0 and no file descriptor points to the file, the disk space of the file is released |
int fstat(int fd, struct stat *st) | Put information about an open file (FD) into st |
int stat(char *file, struct stat *st) | Writes information about the specified file name to st |
Call the instance
Here is a simple example of trying to call the system call interface of various file systems:
// ufilesystem.c
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fcntl.h"
void print_stat(char *msg, struct stat *st);
int
main(a)
{
int fd;
struct stat st;
mkdir("/dir");
chdir("/dir");
fd = open("file", O_CREATE|O_RDWR);
stat("file", &st);
print_stat("file", &st);
link("/dir/file"."file_link");
stat("file_link", &st);
print_stat("fiel_link", &st);
unlink("/dir/file");
unlink("/dir/file_link");
fstat(fd, &st);
print_stat("fd", &st);
close(fd);
exit(0);
}
void
print_stat(char *msg, struct stat *st)
{
printf("%s: dev=%d ino=%d type=%d nlink=%d size=%d\n",
msg, st->dev, st->ino, st->type, st->nlink, st->size);
}
Copy the code
The result is as follows:
$ ufilesystem
file: dev=1 ino=30 type=2 nlink=1 size=0 # type 2 就是 file
fiel_link: dev=1 ino=30 type=2 nlink=2 size=0
fd: dev=1 ino=30 type=2 nlink=0 size=0 $ ls dir . 1 29 64 .. 1, 1, 1024Copy the code
The file is not deleted until the unlink file reaches nlink==0 and all file descriptors pointing to the file are released. Using this feature, we have the following custom implementation for temporary files:
Note: A practical tip
“Create a temporary file to be deleted when file descriptors are closed or the process exits” is a common requirement, which can be implemented elegantly with unlink==0 and all file descriptors are released:
fd = open("/tmp/xyz", O_CREATE|O_RDWR);
unlink("/tmp/xyz"); // nlink == 0.// temp file will be removed when:
close(fd);
// or:
exit(a);Copy the code
EOF
By CDFMLR 2021-02-21
Echo “See you 🚰”
The picture at the top is from xiaowei API, which is randomly selected and only used to test the mechanical and photoelectric performance of the screen. It has nothing to do with any content or opinion of the article, and does not mean that I agree with, support or oppose any content or opinion in part or in whole. If there is infringement, contact delete.