SDDS ToolKit Programs and Libraries for C and Python
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 {
56 int ret = system(buffer);
57 (void)ret;
58 }
59 if (!fexists(newName)) {
60 fprintf(stderr, "unable to copy %s to %s\n", oldName, newName);
61 return 1;
62 }
63 remove(oldName); /* ignore return value */
64 return 0;
65}
66
67/**
68 * @brief Replaces a file with a replacement file and creates a backup of the original.
69 *
70 * Creates a backup of the original file by renaming it with a "~" suffix, then replaces it
71 * with the replacement file. If the replacement fails, the function attempts to restore the
72 * original file from the backup. Error messages are printed to stderr in case of failures.
73 *
74 * @param file The name of the file to be replaced.
75 * @param replacement The name of the replacement file.
76 * @return Returns 1 on success, 0 on failure.
77 */
78long replaceFileAndBackUp(char *file, char *replacement) {
79 char *backup;
80 backup = tmalloc(sizeof(*backup) * (strlen(file) + 2));
81 sprintf(backup, "%s~", file);
82 if (renameRobust(file, backup, RENAME_OVERWRITE) == 0) {
83 if (renameRobust(replacement, file, RENAME_OVERWRITE)) {
84 fprintf(stderr, "unable to rename temporary file %s to %s\n",
85 replacement, file);
86 perror(NULL);
87 if (renameRobust(backup, file, 0)) {
88 fprintf(stderr, "unable to rename %s back to %s !\n", backup, file);
89 perror(NULL);
90 } else
91 fprintf(stderr, "original version of %s restored\n", file);
92 free(backup);
93 return 0;
94 }
95 } else {
96 fprintf(stderr, "unable to replace %s--result stored in %s\n",
97 file, replacement);
98 perror(NULL);
99 free(backup);
100 return 0;
101 }
102 free(backup);
103 return 1;
104}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:65
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:78