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

Functions for file backup. More...

#include "tpcclibConfig.h"
#include "tpcextensions.h"
#include "tpcfileutil.h"

Go to the source code of this file.

Functions

int fileBackup (const char *filename, const char *backup_ext, char *status)
 

Detailed Description

Functions for file backup.

Definition in file backup.c.

Function Documentation

◆ fileBackup()

int fileBackup ( const char * filename,
const 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 17 of file backup.c.

25 {
26 char bakfile[FILENAME_MAX];
27 int ret;
28
29 // Check the input
30 if(filename==NULL || strlen(filename)<1) {
31 if(status!=NULL) sprintf(status, "invalid filename");
32 return 1;
33 }
34
35 // Check if file exists; if not then no need to make any backup
36 if(access(filename, 0) == -1) {
37 if(status!=NULL) sprintf(status, "file does not pre-exist");
38 return 0;
39 }
40 // Create filename for the backup file
41 strlcpy(bakfile, filename, FILENAME_MAX);
42 if(backup_ext==NULL) strlcat(bakfile, ".bak", FILENAME_MAX);
43 else strlcat(bakfile, backup_ext, FILENAME_MAX);
44 // If also backup file exists, then just delete it
45 if(access(bakfile, 0) != -1) {
46 ret=remove(bakfile);
47 if(ret!=0) {
48 if(status!=NULL) sprintf(status, "cannot delete previous backup file");
49 return 3;
50 }
51 }
52 // Rename file
53 ret=rename(filename, bakfile);
54 if(ret!=0) {
55 if(status!=NULL) sprintf(status, "cannot rename file as backup");
56 return 5;
57 }
58 if(status!=NULL) sprintf(status, "file renamed as backup");
59 return 0;
60}
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592