Prevent writing the same file several times (fixes #3753)

Ha, makes sense. It should be safe then :)