2 ms·
In particular, the underlying translation layer can't prevent this without completely breaking the ability to operate on Windows files from the Linux compatibil
by JoshTriplett 10y ago
In particular, the underlying translation layer can't prevent this without completely breaking the ability to operate on Windows files from the Linux compatibility subsystem. "rm -rf" just recurses through the directory tree and calls unlink() on every file and rmdir() on every directory. Making rmdir() refuse to work on /mnt/c wouldn't help; by the time that syscall occurs, rm has removed every other file and directory. "find / -delete" would have the same effect.
And if you prevent unlink() or rmdir() from working on files within /mnt/c or other Windows drives, you would not only break the ability to do normal file management with rm or other Linux tools; you'd also break every application that creates and removes temporary files, or otherwise manages files programmatically. You couldn't, for instance, use "git checkout", because that unlinks files and rmdirs directories present in the current HEAD but not present in the new target to check out.
rm (without --no-preserve-root) specifically checks for an attempt to remove / before it starts recursing. You could potentially improve rm to preserve a few more directories by default; some tools exist to do exactly that, such as safe-rm.
rm's default --preserve-root behavior mostly exists to help catch cases like "rm -rf $VAR/$VAR2", which with VAR and VAR2 unset would turn into "rm -rf /".
(Personally, I'd like to have rm add and default to a --preserve-home option too, to catch things like "rm -rf $HOME/$VAR" or "rm -rf ~/$VAR" or "rm -rf ~ /foo".)