22long replaceFile(
char *file,
char *replacement) {
23 if (renameRobust(file, replacement, RENAME_OVERWRITE)) {
24 fprintf(stderr,
"unable to rename file %s to %s\n",
32long renameRobust(
char *oldName,
char *newName,
unsigned long flags)
43 if (
fexists(newName) && !(flags & RENAME_OVERWRITE))
47 if (rename(oldName, newName) == 0)
51 sprintf(buffer,
"copy %s %s", oldName, newName);
53 sprintf(buffer,
"cp %s %s", oldName, newName);
57 fprintf(stderr,
"unable to copy %s to %s\n", oldName, newName);
77 backup =
tmalloc(
sizeof(*backup) * (strlen(file) + 2));
78 sprintf(backup,
"%s~", file);
79 if (renameRobust(file, backup, RENAME_OVERWRITE) == 0) {
80 if (renameRobust(replacement, file, RENAME_OVERWRITE)) {
81 fprintf(stderr,
"unable to rename temporary file %s to %s\n",
84 if (renameRobust(backup, file, 0)) {
85 fprintf(stderr,
"unable to rename %s back to %s !\n", backup, file);
88 fprintf(stderr,
"original version of %s restored\n", file);
93 fprintf(stderr,
"unable to replace %s--result stored in %s\n",
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
long fexists(const char *filename)
Checks if a file exists.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.