Linux unlink function and how to delete files

Linux unlink function and how to delete files

1. unlink function

For hard links, unlink is used to delete the directory entry and reduce the inode reference count by 1. These two steps are also an atomic process. The file will not be actually deleted until the inode reference count reaches 0.

For soft links, unlink directly deletes the soft link without affecting the file pointed to by the soft link.

Function prototype:

int unlink(const char *pathname);

Parameter Description:

pathname: specifies the link file to be removed

Return value description:

Returns 0 if successful; returns -1 if failed and sets errno to the corresponding value

2. Experimental code—myunlink

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){ 
 //Create a directory entry (hard link) for an existing file
 if (link(argv[1], argv[2]) == -1) {
 perror("link error");
 exit(1);
 }
 //Delete the previous file directory entry if (unlink (argv [1]) == -1) {
 perror("unlink error");
 exit(1);
 }
 return 0;
}

When we execute the ./myunlink hellotest command, hellotest will be deleted and the inode reference count will be reduced by 1.

3. Delete files

Needless to say, I believe everyone has used the rm -rf command.

Now let's think about it again. When we deleted files using the rm command before, have you ever questioned whether the files were really deleted?

If it can be deleted, how does the operating system delete the files?

When the operating system is designed, it associates the file's inode index number with the block in the disk, so that we can find the location of the block through the file and see the file's data.

When deleting a file, it is controlled by two system variables: i_link, which indicates the number of hard links to the file, and i_count, which indicates the reference count of the file. The necessary conditions for file deletion are i_link = 0 and i_count = 0.

For files on disk, we just need to set i_link = 0 (clear the number of hard links) to delete the file. If the file is opened in a program, we also need to clear i_count = 0 of the running program to achieve the purpose of deleting the file.

4. The general process of deleting files under Linux

The file deletion process under Linux is roughly as shown below:

這里寫圖片描述

Figure 1- The general process of file deletion under Linux

There is a test file in the /test/file directory on the current disk (i_link = 1), and a hard link file hard_link pointing to the test file (i_link = 1), and the ./test process has opened the test file (i_count = 1). If you want to delete the test.txt file, you must kill the ./test process (i_count = 0), and then delete the hard_link hard link file and the test.txt file in the /test/file directory (that is, let i_link = 0).

That is to say, in Linux, file deletion is controlled by the number of links. When the link of a file = 0, the file will be deleted. Generally, a file has two link counters, i_link and i_count.

i_count is the reference count of the file opened by the current process, and i_link is the number of file links. i_count can be understood as a counter of the file in memory, and i_link is a counter in the disk. For the rm command, it actually sets the i_link count of the file on the disk to 0. If a file is used by a process and the user executes the rm command to delete the file, the program can still execute normally and can still read the correct data from the file. This is because the rm command only sets i_link to 0 (it disconnects the association between the file and the inode, and does not delete the inode and the block data block on the disk. If the process is stopped at this time, the deleted data can be retrieved. If the process is writing data, the data in the disk block will be overwritten by the data written by the process, and the original data cannot be recovered).

The process is still referencing the file i_count = 1. Executing the rm command will not actually delete the file. If you want to delete the file, you must let the process release the reference count of the file, that is, kill the process, so that the file will be truly deleted.

Even so, are the files really deleted? We have said before that the data of a file is stored in blocks on the disk. When we want to find the data in a file, we cannot directly find the blocks on the disk, because there are too many blocks on the disk. How do you know which block your data is stored in?

Suppose you accidentally delete very important data, it will mean that your data will never be recovered, thus causing irreparable loss. This shows the importance of data, so the operating system will not easily delete data from the disk.

Seeing this, I believe you have understood that, in fact, the so-called right-click deletion operation only disconnects the file's inode index number from the block on the disk, but the file data is not actually deleted. If you really want to delete the data, you can either format the disk or delete the original data and then write new data to overwrite it. Of course, you can also choose to format and overwrite the data for double insurance. At this time, it is basically very difficult to recover your data, even if you can, you can only recover part of the data at most.

If you really accidentally delete very important data, you should quickly restore the data and try not to do any other unnecessary operations, so as to minimize data loss during the data recovery process.

5. myunlink2.c program

#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 /*
 The unlink function deletes a dentry
 */
int main(void){
 int fd;
 char *p = "test of unlink\n";
 char *p2 = "after write something.\n";
 //When the process opens the temp.txt file, the reference count will be +1
 fd = open("temp.txt", O_RDWR|O_CREAT|O_TRUNC, 0644);
 if(fd < 0){
 perror("open temp error");
 exit(1);
 }
 //The conditions for being released are met int ret = unlink("temp.txt"); 
 if(ret < 0){
 perror("unlink error");
 exit(1);
 }
 //Write the first string to the temp.txt file and determine whether the write operation is successful by the return value ret = write(fd, p, strlen(p));
 if (ret == -1) {
 perror("-----write error");
 }
 printf("hi! I'm printf\n");
 //Write the second string to the temp.txt file and determine whether the write operation is successful by the return value ret = write(fd, p2, strlen(p2));
 if (ret == -1) {
 perror("-----write error");
 }
 printf("Enter anykey continue\n");
getchar();
 //When close closes fd, the process's reference count to the file will be -1, disconnecting the process from the file close(fd);
 return 0;
}

Program running results:

這里寫圖片描述

The result of the program is the same as we expected. When the program is running, the open function is called to create and open the temp.txt file. At this time, the process's reference count i_count to the temp file will increase by 1, and the temp file itself will also have an i_link link count that will increase by 1.

When the unlink function is called to delete the temp file, only the i_link link count is reduced by 1, while the i_count count of the process is still 1, and the association with the temp file is not disconnected. Therefore, the process can call the write function to write data to the temp file, and it will naturally succeed. When the program ends, call close to close the reference to the temp file, and the temp file will be deleted by the operating system.

6. Conclusion

Without understanding the principles of the file system, we usually think that the data has been deleted. In fact, the file data on the disk is still there, but the connection between the dentry directory and the data on the disk is disconnected. If we can't find the data, we will definitely think it has been deleted. However, as long as we find a way to re-establish the connection between the data and the dentry directory, the deleted data can be recovered.

Therefore, when we delete a file, in a sense, we are just making the file ready for release. As for when it is released, it depends on the operating system.

For the unlink function, when clearing a file, if the number of hard links of the file reaches 0 and there is no corresponding dentry, the file will not be released immediately. The system will not release the file until all processes that have opened the file close the file.

7. Don’t use the rm command casually

I believe that after reading this, you should know the importance of data to computers, because once some crucial data is deleted, it is really gone forever. This is why the operating system does not directly delete the data from the disk. But don't let this mean you can use the rm command recklessly, because sometimes deleted data cannot be restored 100%.

Summarize

The above is the Linux unlink function and the operation method of deleting files introduced by the editor. I hope it will be helpful to everyone. Thank you very much for your support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Example explanation of alarm function in Linux
  • PHP executes 6 Linux command function code examples
  • Detailed explanation of the use of stat function and stat command in Linux
  • How to get the current time using time(NULL) function and localtime() in Linux
  • How to add a timeout to a Python function on Linux/Mac
  • Detailed explanation of the use of Linux lseek function
  • A brief analysis of the function calling process under the ARM architecture

<<:  Example analysis of mysql shared lock and exclusive lock usage

>>:  Detailed explanation of primitive values ​​and reference values ​​in ECMAScript

Recommend

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...

Sample code using scss in uni-app

Pitfalls encountered I spent the whole afternoon ...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

In-depth understanding of Vue's method of generating QR codes using vue-qr

Table of contents npm download step (1) Import (2...

How to track users with JS

Table of contents 1. Synchronous AJAX 2. Asynchro...

jQuery implements Table paging effect

This article shares the specific code of jQuery t...

Top 10 useful and important open source tools in 2019

In Black Duck's 2017 open source survey, 77% ...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

Apache Log4j2 reports a nuclear-level vulnerability and a quick fix

Apache Log4j2 reported a nuclear-level vulnerabil...

Nginx rush purchase current limiting configuration implementation analysis

Due to business needs, there are often rush purch...

Solve the problem of setting Chinese language pack for Docker container

If you use docker search centos in Docker Use doc...

Summary of events that browsers can register

Html event list General Events: onClick HTML: Mous...

How to install Docker CE on Ubuntu 18.04 (Community Edition)

Uninstall old versions If you have installed an o...