当前位置:网站首页>Interview question: is the space released after RM deleted the file? Do you know the answer?

Interview question: is the space released after RM deleted the file? Do you know the answer?

2020-12-06 15:49:55 Official account programmer Bai Nannan

  • Generate a random content file of a specified size

  • When will a file be deleted ?

  • How to free the space occupied by deleted files ?

  • summary

stay Linux, Did you ever think that , Use rm Delete a file , The space is free ? Things may not always be what you want .

Generate a random content file of a specified size

Let's take a look at the current space size of each mounted directory :

$ df -h
/dev/sda11      454M  280M  147M  66% /boot
 

I picked one of the results here ( You can choose any mount Directory ), Next, we are going to /boot Next generate a file .

First we have a 50M Size file :

$ dd if=/dev/urandom of=/boot/test.txt bs=50M count=1
 

thus , We produced a 50M Size file , Look again boot Next :

$ df -h
/dev/sda11      454M  312M  115M  74% /boot
 

You don't have to worry about how much more , You just need to pay attention ,/boot There are more files under .

The test program :

#include<stdio.h>
#include<unistd.h>
int main(void)
{
    FILE *fp = NULL;
    fp = fopen("/boot/test.txt", "rw+");
    if(NULL == fp)
    {
       perror("open file failed");
       return -1;
    }
    while(1)
    {
       //do nothing
       sleep(1);
    }
    fclose(fp);
    return 0;
}
 

As for the program itself , I didn't do anything practical , Just open a file , And then it goes on and on . Compile and run :

$ gcc -o openFile openFile.c
$ ./openFile
 

Open another window , Delete test.txt:

$ rm /boot/test.txt
 

Look again. boot Space :

$ df -h
dev/sda11      454M  312M  115M  74% /boot
 

Why ? The size of the space hasn't changed at all !! Clearly use rm Delete it ?

We put openFile The program stops , I want to see others :

$$ df -h
/dev/sda11      454M  280M  147M  66% /boot
 

Darling , The space was immediately released , That is, as expected , Our files have been deleted .

When will a file be deleted ?

actually , Only when the reference count of a file is 0( Including the number of hard links ) When , It's possible to call unlink Delete , As long as it's not 0, Then it won't be deleted . The so-called deletion , It's just a file name inode Link to delete , As long as the data is not rewritten , On disk block Data blocks are not deleted , therefore , You'll see , Even if the deletion library ran away , Some data can still be recovered . let me put it another way , When a program opens a file ( Get the file descriptor ), Its reference count will be +1,rm Although the file seems to be deleted , Actually, it just subtracts the reference count 1, But because the reference count is not 0, So the file will not be deleted .

struct inode {
struct hlist_node   i_hash; /* hash The pointer to the linked list  */
struct list_head    i_list; /* backing dev IO list */
struct list_head    i_sb_list; /*  Super block inode Linked list  */
struct list_head    i_dentry; /*  quote inode Directory entry object chain header of  */
unsigned long    i_ino; /*  Inode number  */
atomic_t         i_count; /*  Reference count  */
unsigned int     i_nlink; /*  Number of hard links  */
 

About the details inside , There's a lot more ( For example, the number of hard links will also affect whether the file is deleted ), It's not going to unfold one by one .

How to free the space occupied by deleted files ?

About releasing , As I said before , Restart the process of opening the file . But is there any way to find out which files have been deleted , But it was opened by some process ?

Nature has a way :

$ lsof |grep deleted

 

Where is marked as deleted The file of , These are some of these documents .

In fact, in the previous example , We can also easily observe (openFile The program runs ,test.txt File deleted ):

$ ls -al /proc/`pidof openFile`/fd
total 0
lrwx------ 1 root root 64 5 month    4 09:27 0 -> /dev/pts/25
lrwx------ 1 root root 64 5 month    4 09:27 1 -> /dev/pts/25
lrwx------ 1 root root 64 5 month    4 09:27 2 -> /dev/pts/25
lrwx------ 1 root root 64 5 month    4 09:27 3 -> /boot/test.txt (deleted)

 

See ,test.txt There is something deleted word .

Now that we've all said , In this case, the file is not deleted , So can we recover ? It's actually readable .

summary

In fact, this kind of file has been deleted , It often appears in program log files , Maybe you have a regular task to clean up the log files generated by the program , But if the program itself forgets to close the handle , Will cause the disk space not to be free , In the end, you think the files have been deleted , But the disk is still occupied . therefore , Develop good habits , After opening the file , When not in use , Remember to close the file descriptor .

If it is found that a large number of files have been deleted , But space doesn't return to normal , Well, let's see if there's a program that opens these files .

The editor summed up 2020 Interview questions , This interview question contains modules which are divided into 19 A module , Namely : Java Basics 、 Containers 、 Multithreading 、 Reflection 、 Object Copy 、Java Web 、 abnormal 、 The Internet 、 Design patterns 、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM . Official account : Programmer Bai Nannan

 

版权声明
本文为[Official account programmer Bai Nannan]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206154755397b.html