SDDSlib
Loading...
Searching...
No Matches
replacefile.c File Reference

Provides functions to replace files with options for backup and robust renaming. More...

#include "mdb.h"

Go to the source code of this file.

Functions

long replaceFile (char *file, char *replacement)
 
long renameRobust (char *oldName, char *newName, unsigned long flags)
 
long replaceFileAndBackUp (char *file, char *replacement)
 Replaces a file with a replacement file and creates a backup of the original.
 

Detailed Description

Provides functions to replace files with options for backup and robust renaming.

This file contains the implementation of the replaceFile(), replaceFileAndBackUp(), and renameRobust() functions, which handle file replacement with error checking and backup capabilities.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file replacefile.c.

Function Documentation

◆ renameRobust()

long renameRobust ( char * oldName,
char * newName,
unsigned long flags )

Definition at line 32 of file replacefile.c.

34{
35 char buffer[1024];
36 /*
37 if (fexists(newName) && flags&RENAME_OVERWRITE) {
38 remove(newName);
39 }
40 if (fexists(newName))
41 return 1;
42*/
43 if (fexists(newName) && !(flags & RENAME_OVERWRITE))
44 return 1;
45
46 /* try using the system-provided version first */
47 if (rename(oldName, newName) == 0)
48 return 0;
49 /* do a copy-and-delete operation */
50#if defined(_WIN32)
51 sprintf(buffer, "copy %s %s", oldName, newName);
52#else
53 sprintf(buffer, "cp %s %s", oldName, newName);
54#endif
55 system(buffer);
56 if (!fexists(newName)) {
57 fprintf(stderr, "unable to copy %s to %s\n", oldName, newName);
58 return 1;
59 }
60 remove(oldName); /* ignore return value */
61 return 0;
62}
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27

◆ replaceFile()

long replaceFile ( char * file,
char * replacement )

Definition at line 22 of file replacefile.c.

22 {
23 if (renameRobust(file, replacement, RENAME_OVERWRITE)) {
24 fprintf(stderr, "unable to rename file %s to %s\n",
25 replacement, file);
26 perror(NULL);
27 return 0;
28 }
29 return 1;
30}

◆ replaceFileAndBackUp()

long replaceFileAndBackUp ( char * file,
char * replacement )

Replaces a file with a replacement file and creates a backup of the original.

Creates a backup of the original file by renaming it with a "~" suffix, then replaces it with the replacement file. If the replacement fails, the function attempts to restore the original file from the backup. Error messages are printed to stderr in case of failures.

Parameters
fileThe name of the file to be replaced.
replacementThe name of the replacement file.
Returns
Returns 1 on success, 0 on failure.

Definition at line 75 of file replacefile.c.

75 {
76 char *backup;
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",
82 replacement, file);
83 perror(NULL);
84 if (renameRobust(backup, file, 0)) {
85 fprintf(stderr, "unable to rename %s back to %s !\n", backup, file);
86 perror(NULL);
87 } else
88 fprintf(stderr, "original version of %s restored\n", file);
89 free(backup);
90 return 0;
91 }
92 } else {
93 fprintf(stderr, "unable to replace %s--result stored in %s\n",
94 file, replacement);
95 perror(NULL);
96 free(backup);
97 return 0;
98 }
99 free(backup);
100 return 1;
101}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59