Use lock-files in the way git does with auto-cleanup being the most notable feature.