Fix backup file process 78/246778/4
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 4 Nov 2020 06:29:02 +0000 (15:29 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Wed, 4 Nov 2020 07:38:04 +0000 (16:38 +0900)
Make a flag before backup file.

Change-Id: I040c371b9d5af2e93a7cea94decab7c96a9e7138
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/pkg_upgrade/pkg_upgrade.c

index 679f14e..96d628f 100644 (file)
@@ -1584,12 +1584,54 @@ static int __set_db_permission(const char *path)
        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) {
@@ -1641,17 +1683,26 @@ static int __backup_file(const char *src_path, const char *dest_path)
        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);
@@ -1667,6 +1718,8 @@ catch:
        if (dest)
                fclose(dest);
 
+       __remove_backup_flag(src_path);
+
        return ret;
 }
 
@@ -1674,7 +1727,7 @@ static int __backup_db(const char *src_path, const char *dest_path) {
        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;