TPCCLIB
Loading...
Searching...
No Matches
backup.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcmisc.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
16 char *filename,
18 char *backup_ext,
21 char *status
22) {
23 char bakfile[FILENAME_MAX];
24 int ret;
25
26 // Check the input
27 if(filename==NULL || strlen(filename)<1) {
28 if(status!=NULL) sprintf(status, "invalid filename");
29 return 1;
30 }
31
32 // Check if file exists; if not then no need to make any backup
33 if(access(filename, 0) == -1) {
34 if(status!=NULL) sprintf(status, "file does not pre-exist");
35 return 0;
36 }
37 // Create filename for the backup file
38 strlcpy(bakfile, filename, FILENAME_MAX);
39 if(backup_ext==NULL) strlcat(bakfile, ".bak", FILENAME_MAX);
40 else strlcat(bakfile, backup_ext, FILENAME_MAX);
41 // If also backup file exists, then just delete it
42 if(access(bakfile, 0) != -1) {
43 ret=remove(bakfile);
44 if(ret!=0) {
45 if(status!=NULL) sprintf(status, "cannot delete previous backup file");
46 return 3;
47 }
48 }
49 // Rename file
50 ret=rename(filename, bakfile);
51 if(ret!=0) {
52 if(status!=NULL) sprintf(status, "cannot rename file as backup");
53 return 5;
54 }
55 if(status!=NULL) sprintf(status, "file renamed as backup");
56 return 0;
57}
58/*****************************************************************************/
59
60/*****************************************************************************/
67 char *filename1,
69 char *filename2,
72 char *status
73) {
74 FILE *from, *to;
75 char c;
76
77 // Check the input
78 if(filename1==NULL || filename2==NULL) {
79 if(status!=NULL) sprintf(status, "invalid filename");
80 return 1;
81 }
82 // Open the file1 for reading
83 if((from=fopen(filename1, "rb"))==NULL) {
84 if(status!=NULL) sprintf(status, "cannot open file for read");
85 return 2;
86 }
87 // Open file2 for writing
88 if((to=fopen(filename2, "wb"))==NULL) {
89 if(status!=NULL) sprintf(status, "cannot open file for write");
90 fclose(from); return 3;
91 }
92 // Copy the file
93 while(!feof(from)) {
94 c=fgetc(from);
95 if(ferror(from)) {
96 if(status!=NULL) sprintf(status, "cannot read from file");
97 fclose(from); fclose(to); (void)remove(filename2); return 4;
98 }
99 if(!feof(from)) fputc(c, to);
100 if(ferror(to)) {
101 if(status!=NULL) sprintf(status, "cannot write to file");
102 fclose(from); fclose(to); (void)remove(filename2); return 6;
103 }
104 }
105 // Close files
106 if(fclose(from)==EOF) {
107 if(status!=NULL) sprintf(status, "cannot close file");
108 fclose(to); return 7;
109 }
110 if(fclose(to)==EOF) {
111 if(status!=NULL) sprintf(status, "cannot close file");
112 return 8;
113 }
114 return 0;
115}
116/*****************************************************************************/
117
118/*****************************************************************************/
119
int fileCopy(char *filename1, char *filename2, char *status)
Definition backup.c:65
int backupExistingFile(char *filename, char *backup_ext, char *status)
Definition backup.c:14
Header file for libtpcmisc.
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition strext.c:206