use rename() instead of link()

so that a previous file would be overwritten (if the key is being overwritten as well)

also check the return value and output an error message if rename() fails