SDDSlib
Loading...
Searching...
No Matches
replacefile.c
Go to the documentation of this file.
1/**
2 * @file replacefile.c
3 * @brief Provides functions to replace files with options for backup and robust renaming.
4 *
5 * This file contains the implementation of the replaceFile(), replaceFileAndBackUp(),
6 * and renameRobust() functions, which handle file replacement with error checking and
7 * backup capabilities.
8 *
9 * @copyright
10 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
11 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
12 *
13 * @license
14 * This file is distributed under the terms of the Software License Agreement
15 * found in the file LICENSE included with this distribution.
16 *
17 * @author M. Borland, C. Saunders, R. Soliday
18 */
19
20#include "mdb.h"
21
22long replaceFile(char *file, char *replacement) {
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}
31
32long renameRobust(char *oldName, char *newName, unsigned long flags)
33/* returns 0 on success, 1 on failure, just like ANSI rename() */
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}
63
64/**
65 * @brief Replaces a file with a replacement file and creates a backup of the original.
66 *
67 * Creates a backup of the original file by renaming it with a "~" suffix, then replaces it
68 * with the replacement file. If the replacement fails, the function attempts to restore the
69 * original file from the backup. Error messages are printed to stderr in case of failures.
70 *
71 * @param file The name of the file to be replaced.
72 * @param replacement The name of the replacement file.
73 * @return Returns 1 on success, 0 on failure.
74 */
75long replaceFileAndBackUp(char *file, char *replacement) {
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
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
Definition replacefile.c:75