TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcfileutil
backup.c
Go to the documentation of this file.
1
4
/*****************************************************************************/
5
#include "tpcclibConfig.h"
6
/*****************************************************************************/
7
#include "
tpcextensions.h
"
8
/*****************************************************************************/
9
#include "
tpcfileutil.h
"
10
/*****************************************************************************/
11
12
/*****************************************************************************/
17
int
fileBackup
(
19
const
char
*filename,
21
const
char
*backup_ext,
24
char
*status
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
}
61
/*****************************************************************************/
62
63
/*****************************************************************************/
fileBackup
int fileBackup(const char *filename, const char *backup_ext, char *status)
Definition
backup.c:17
strlcpy
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition
stringext.c:630
strlcat
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition
stringext.c:590
tpcextensions.h
Header file for library libtpcextensions.
tpcfileutil.h
Header file for libtpcfileutil.
Generated on
for TPCCLIB by
1.17.0