write to tmp and atomically rename