Does RM corrupt when interrupted?

3

1

So I may have kind of accidentally rm -r'd ~. Not to worry, I have backups! I did a quick file listing diff to copy across files that were removed, all is good. But...

Does GNU rm corrupt files if I interrupt it? Surely it was just unlinking gigabytes of my priceless data a second, rather than removing the file from the disk itself?

Jookia

Posted 2013-01-26T01:14:19.863

Reputation: 133

Answers

4

No. rm just calls unlink() to remove the name. This syscall is atomic, meaning it either succeeds in removing the name, or does not. It can not be interrupted. If you interrupt rm while it is unlink()ing multiple files, then some files will be successfully removed, and some will not be. Housekeeping such as freeing the blocks the file was using if all links have been removed is a separate operation and may be done later, and if this is interrupted ( by a power failure or system crash, not interrupting rm ), the filesystem may require a fsck to finish it, but most filesystems these days are journaled so they do not need this. Either way, the file is either removed, or not, there is no in between.

psusi

Posted 2013-01-26T01:14:19.863

Reputation: 7 195

2

From Wikipedia

To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names), and the objects themselves are discarded only when all references have been removed and no programs still have open handles to the objects.

So yeah, your unlinking analogy is pretty good. If you have multiple hard links to a file, the data won't be marked ask discardable until all links are gone (symlinks don't qualify, hard links only).

If you have backups, it might be a good idea to do some md5 or sha1 hashes on the backups and your home directory, and diff them to spot any changes, but as long as you restored teh missing files, the files that stuck around shouldn't suffer any corruption.

As always, YMMV.

peelman

Posted 2013-01-26T01:14:19.863

Reputation: 4 580

Hmm... To be more clear, would interrupting rm during a huge file delete cause corruption (if only half the file was deleted?) I'd find this unlikely if it only removes references, but I need to be sure. – Jookia – 2013-01-26T01:39:10.147

If you need to be sure, then run a check using hashes. Trust but verify. – peelman – 2013-01-26T01:48:12.240

1Eh, I don't think I have a slow enough computer to test the hypothesis. I'll go read the source code. – Jookia – 2013-01-26T01:50:46.633