~learn.
0:00 / 1:12
LLD · File System

One mv cuts your home folder off the tree

transcript

One move command. And your whole home folder drops off the tree. Nothing stops it. It's the move candidates miss.

A move has a few outcomes. To a new name? A rename. The whole subtree moves in one pointer change. Onto a directory? It moves inside. Name already taken? Refuse, and say so. Like move dash n.

Now move home into a folder inside home. Into asha's old. That name doesn't exist yet. So it looks like a rename. Do it, and home detaches from the root. The whole subtree, cut off from the tree.

The fix: every node points at its parent. Start at the destination's parent: old. Walk up. Asha. Then home. That's the very folder being moved. Refused: can't move home inside itself. Nothing has changed.

The parent pointers were already there. They also give every node its full path. So the check costs one walk up the tree. Meet the source on the way up? Refuse the move.

Full breakdown in the lecture.