TPCCLIB
Loading...
Searching...
No Matches
backup.c File Reference

Functions for file copying and making backup. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int backupExistingFile (char *filename, char *backup_ext, char *status)
 
int fileCopy (char *filename1, char *filename2, char *status)
 

Detailed Description

Functions for file copying and making backup.

Author
Vesa Oikonen

Definition in file backup.c.

Function Documentation

◆ backupExistingFile()

int backupExistingFile ( char * filename,
char * backup_ext,
char * status )

Check if specified file exists; rename existing file to a backup file. If also backup file exists, then remove that.

Returns
Returns 0, if successful, and <>0 in case of an error.
Parameters
filenameName of file which, if it exists, is renamed to a backup file
backup_extExtension for backup file; NULL will set the default ".bak" extension.
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed

Definition at line 14 of file backup.c.

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}
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

Referenced by dftWrite(), fitWrite(), plot_fit_svg(), plot_fitrange_svg(), plot_svg(), plotdata(), and resWrite().

◆ fileCopy()

int fileCopy ( char * filename1,
char * filename2,
char * status )

Copy file contents to another file. Existing file will be overwritten, to prevent it call backupExistingFile() before calling this function.

Returns
Returns 0 if successfull, otherwise >0.
Parameters
filename1Name of file to be copied
filename2Name of new file
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed

Definition at line 65 of file backup.c.

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}