Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The unlink function in Linux and the operation method of deleting files

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the Linux unlink function and delete file operation method, I hope you have something to gain after reading this article, let's discuss it together!

Linux is a free-to-use and freely distributed UNIX-like operating system, is a POSIX-based multi-user, multitasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

1. unlink function

  For hard links, unlink is used to delete directory entries and decrement the inode reference count by 1, which is also an atomic process. Files are not actually deleted until the inode reference count is zero.

  For soft links, unlink removes the soft link directly without affecting the file to which the soft link points.

Function prototype:

int unlink(const char *pathname);

Parameter Description:

  pathname: Specifies the linked file to remove

Return Value Description:

  0 for success;-1 for failure, and errno is set to the corresponding value

2. Experimental code-myunlink

#include #include int main(int argc, char *argv[]){ //Create a directory entry for an existing file (hardlink) 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 perform.../ When myunlink hellotest finishes, hellotest is deleted and the inode reference count is decremented 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 with rm command, did you ever question whether the files were really deleted?

  If it was deleted, how did the operating system delete the file?

  The operating system is designed by associating the inode index number of the file with the block block in the disk, so that we can find the location of the block through the file and see the data of the file.

  When deleting a file, it is controlled by two variables of the system: i_link, which indicates the number of hard links in 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.

  Files on disk can be deleted by killing i_link = 0 (hard link count). If the file is opened in the program, we also need to kill the running program i_count = 0 in order to delete the file.

4. Linux file deletion process

  The file deletion process under linux is roughly as shown in the figure below:

Figure 1-General process of file deletion under linux

  There is a test file (i_link = 1) in the/test/file directory on the current disk, and there is a hard link file hard_link pointing to the test file (i_link = 1), and./ The test process opens the test file again (i_count = 1), and if you want to delete the test.txt file, you must put./ The test process kills (i_count = 0), and then deletes the hard_link file and test.txt file in the/test/file directory (i.e., i_link = 0).

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

  i_count is the reference count of the file open by the current process, i_link is the number of file links, i_count can be understood as a counter of files in memory, and i_link is a counter on disk. For the rm command, this actually sets the i_link count of files on disk to 0. If a file is used by a process and the user deletes the file by executing the rm command, the program can still execute normally and still read the correct data from the file 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 in the disk. At this time, the process is stopped, and the deleted data can be recovered. If the process is writing data, the block data of the disk will be overwritten by the data written by the process, and the original data cannot be recovered.)

  While the process is still referencing the file i_count = 1, executing the rm command system does not actually delete the file. If you want to delete the file, you must let the process dereference the file, that is, kill the process, so that the file will be deleted.

  Even so, was the file really deleted? As we said earlier, the data of the file is stored in the block on the disk. When we want to find the data in the file, we don't directly find the block 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, this will mean that your data will never be found back, resulting in irreparable loss, which shows the importance of data, so the operating system will not easily delete the data from the disk.

  See here, I believe you already understand, in fact, your so-called right-click delete operation just disconnects the inode index number of the file from the block in the disk, but the data of the file is not really deleted. If you really want to delete the data, then either format the disk, or delete the original data, and then write new data to overwrite, of course, you can also choose to format and data overwrite double insurance, this time your data want to recover is basically very difficult, even if you can only recover part of the data.

  If you really accidentally delete very important data, then this time quickly recover the data, any other redundant operations try not to do, so that in the data recovery process to minimize data loss.

5. myunlink2.c program

#include #include /* unlink function is to delete a dentry */int main(void){ int fd; char *p = "test of unlink\n"; char *p2 = "after writing something.\ n"; //When a process opens a temp.txt file, the reference count is +1 fd = open("temp.txt", O_RDWR| O_CREAT| O_TRUNC, 0644); if(fd < 0){ perror ("open temp error"); exit(1); } //met the condition to be released int ret = unlink ("temp.txt"); if(ret < 0){ perror ("unlink error"); exit(1); } //Write the first string to temp.txt file, and judge whether the write operation is successful by returning the value ret = write(fd, p, strlen(p)); if (ret == -1) { perror("----write error"); } printf("hi! I'm printf\n");//Write the second string to temp.txt file, and judge 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 reference count of the process to the file will be-1, breaking the association between the process and the file close(fd); return 0;}

Program Run Results:

  The program runs as expected. When the program runs, the open function is called to create and open the temp.txt file. At this time, the reference count i_count of the temp file will be increased by 1, and the temp file itself will have an i_link link count.

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

6. summary

  Without understanding the principle of file system, we usually think that the data has been deleted. In fact, the file data on the disk is still there. Just disconnect the dentry directory from the data on the disk. We will definitely think that the data has been deleted if we cannot find it. However, as long as we find a way to re-establish the connection between the data and the dentry directory, we can restore the deleted data.

  So we delete files, in a sense, just to make them eligible for release, and when to release depends on the operating system.

  For the unlink function, if the hard link count of the file reaches zero, there is no dentry, but the file is still not released immediately. The system chooses the time to release the file until all processes that open it close it.

7. Do not use the RM command.

  I believe that you should know the importance of data for computers, because once some vital data is deleted, it is really gone forever, which is why the operating system does not directly delete data from disk. But do not because of this, you can use the rm command unscrupulously, because sometimes the data deleted, and not 100% recovery back.

After reading this article, I believe you have a certain understanding of "unlink function and delete file operation method in Linux". If you want to know more related knowledge, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report