The principle of file deletion under linux

  

Linux controls the file deletion by the number of links. This file will be deleted only when there is no link in a file. In general, each file has two link counters: i_count and i_nlink. The meaning of i_count is the number of current file consumers (or called). The meaning of i_nlink is the number of media connections (the number of hard links); it can be understood that i_count is the memory reference counter, and i_nlink is the reference counter of the disk. When a file is referenced by a process, the corresponding i_count number will increase; when a hard link to the file is created, the corresponding i_nlink number will increase. For the delete command rm, the actual reduction of the disk reference count i_nlink. There will be a problem here. If a file is being called by a process and the user performs an rm operation to delete the file, what happens? After the user performs the rm operation to delete the file, and then executes ls or other file management commands, the file cannot be found again, but the process of calling the deleted file continues to execute normally, and can still read and write correctly from the file. Into the content. This is why? This is because the rm operation only reduces the i_nlink of the file. If there is no other link i_nlink, it is 0; but since the file is still referenced by the process, the i_count corresponding to the file is not 0, so even if rm is executed Operation, but the system does not actually delete this file, when only i_nlink and i_count are 0, the file will be deleted. In other words, you also need to undo the process's call to the file. The above mentioned i_nlink and i_count are the real conditions for file deletion, but when the file is not called, can you still retrieve the deleted file after executing the rm operation to delete the file? As mentioned earlier, the rm operation only reduces the i_nlink of the file, or sets it to 0. The actual link is to delete the file name to the inode. At this time, there is no entity that deletes the file (block data block). If the machine is stopped in time, the data can be retrieved. If the data is continuously written at this time, then the new data may be allocated to the block data block of the deleted data. At this time, the file will be real. Recycling, there is no way for the gods at that time

Copyright © Windows knowledge All Rights Reserved