Make the rebuild of the index use a lockfile.

This prevents corruption of the index when multiple builds happen in
parallel and then try to update the index at the same time.