return 0;
}
+static int __create_backup_flag(const char *path)
+{
+ int fd;
+ char temp_buf[8192] = {'\0', };
+
+ snprintf(temp_buf, sizeof(temp_buf), "%s.bck.flag", path);
+
+ fd = open(temp_buf, O_CREAT | O_WRONLY, 0644);
+ if (fd == -1) {
+ _LOG("failed to create flag file %s, %d", temp_buf, errno);
+ return -1;
+ }
+ close(fd);
+
+ return 0;
+}
+
+static int __check_backup_flag(const char *path)
+{
+ char temp_buf[8192] = {'\0', };
+
+ snprintf(temp_buf, sizeof(temp_buf), "%s.bck.flag", path);
+ if (access(temp_buf, F_OK) != 0)
+ return -1;
+
+ return 0;
+}
+
+static int __remove_backup_flag(const char *path)
+{
+ char temp_buf[8192] = {'\0', };
+
+ snprintf(temp_buf, sizeof(temp_buf), "%s.bck.flag", path);
+ if (remove(temp_buf)) {
+ _LOG("cannot remove flag file(%s): %d", temp_buf, errno);
+ return -1;
+ }
+
+ return 0;
+}
+
static int __check_and_restore_backup(const char *origin_path) {
char backup_path[BUF_SIZE];
char buf[BUF_SIZE];
snprintf(backup_path, BUF_SIZE, "%s.bck", origin_path);
- if (access(backup_path, F_OK))
+ // if backup flag exists, it means the previous backup process aborted.
+ if (access(backup_path, F_OK) && __check_backup_flag(origin_path))
return 0;
if (access(origin_path, F_OK) == 0) {
retvm_if(access(src_path, F_OK) != 0, -1,
"File(%s) is not exist", src_path);
- retvm_if(access(dest_path, F_OK) == 0, -1,
- "File(%s) is already exist", dest_path);
-
- retvm_if(rename(src_path, dest_path) != 0, -1,
- "Fail to move file %s to %s", src_path, dest_path);
+ // if backup flag exists, it means the previous backup process aborted.
+ if (__check_backup_flag(src_path)) {
+ if (access(dest_path, F_OK) == 0) {
+ if (remove(dest_path))
+ _LOG("Failed to remove uncompleted backup file "
+ "%s: %d", dest_path, errno);
+ return -1;
+ }
+ } else {
+ if (__create_backup_flag(src_path)) {
+ _LOG("failed to create backup flag");
+ return -1;
+ }
+ }
- src = fopen(dest_path, "r");
- tryvm_if(src == NULL, ret = -1, "Failed to open : %s\n", dest_path);
+ src = fopen(src_path, "r");
+ tryvm_if(src == NULL, ret = -1, "Failed to open : %s\n", src_path);
- dest = fopen(src_path, "w");
- tryvm_if(dest == NULL, ret = -1, "Failed to open : %s\n", src_path);
+ dest = fopen(dest_path, "w");
+ tryvm_if(dest == NULL, ret = -1, "Failed to open : %s\n", dest_path);
while (!feof(src)) {
rc = fread(temp_buf, size_of_char, size_of_temp_buf, src);
if (dest)
fclose(dest);
+ __remove_backup_flag(src_path);
+
return ret;
}
if (__backup_file(src_path, dest_path) != 0)
return -1;
- if (__set_db_permission(src_path) != 0)
+ if (__set_db_permission(dest_path) != 0)
return -1;
return 0;