Update rw-update-macro.inc 49/165249/2 submit/tizen_4.0/20180103.005612
authorSunmin Lee <sunm.lee@samsung.com>
Wed, 27 Dec 2017 08:16:05 +0000 (17:16 +0900)
committerSunmin Lee <sunm.lee@samsung.com>
Tue, 2 Jan 2018 06:44:51 +0000 (15:44 +0900)
The options of restore_backup_file is changed to simplify the usage
 -r (deprecated): for now, always restore recursively if the target is directory
 -d (deprecated): for now, destination is always root(/)
 -f (new): the option for overwriting existing file

Change-Id: I473182a8be5de46c65970eba11677b725a52a7de
Signed-off-by: Sunmin Lee <sunm.lee@samsung.com>
upgrade/rw-update-macro.inc [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index d3fe5a9..31e1f1e
@@ -30,54 +30,97 @@ write_version_info() {
        echo "OLD_VER=$NEW_VER" > $OLD_VER_INFO
 }
 
+BACKUP_ZIP="/usr/system/RestoreDir/opt.zip"
+
+restore_from_zip() {
+
+       TARGET_FILE=$1
+       TARGET_DIR=$(dirname /$TARGET_FILE)
+
+       echo "restore_from_zip: $TARGET_FILE"
+       if [ ! -d $TARGET_DIR ]; then
+               mkdir -p $TARGET_DIR
+       fi
+
+       # The attributes of directory can not be overwritten by unzip.
+       # Unzip the directory into temp path and copy it to original path.
+       if [ "z${TARGET_FILE: -1}" = "z/" ]; then
+               RESTORE_DIR_PATH="/tmp/restored_dir"
+               mkdir -p $RESTORE_DIR_PATH
+               unzip -oX $BACKUP_ZIP $TARGET_FILE -d $RESTORE_DIR_PATH > /dev/null
+               cp -af $RESTORE_DIR_PATH/$TARGET_FILE $TARGET_DIR
+               rm -rf $RESTORE_DIR_PATH
+       else
+               unzip -oX $BACKUP_ZIP $TARGET_FILE -d / > /dev/null
+       fi
+
+       # Restore smack label
+       TMP=$(mktemp /tmp/smackinfo.XXXXXX)
+       PATH_FOR_SMACK=$(echo $TARGET_FILE | sed -e "s/\/$//")
+       SMACK_VAL=$(grep "$PATH_FOR_SMACK " /usr/system/RestoreDir/smack_label.txt | \
+                       { read FILE SMACK; echo $SMACK; })
+       if [ "z$SMACK_VAL" = "z" ]; then
+               echo "No smack label for $PATH_FOR_SMACK"
+       else
+               echo "/$PATH_FOR_SMACK $SMACK_VAL" > $TMP
+               rstsmack $TMP
+       fi
+       rm $TMP
+}
+
 restore_backup_file() {
 
-       BACKUP_ZIP="/usr/system/RestoreDir/opt.zip"
-       REC=0
-       DEST=
+       OVERWRITE=
+       RESTORE_PATH=
 
-       while [ "$1" != "" ]; do
+       while [ "z$1" != "z" ]; do
                case $1 in
-               -r )
-                       REC=1
-                       ;;
-               -d )
-                       shift
-                       DEST=$1
+               -f )
+                       OVERWRITE=$1
                        ;;
-               *)
+               * )
                        RESTORE_PATH=$1
                        ;;
                esac
                shift
        done
 
-       MOD_PATH=$(echo $RESTORE_PATH | sed -e "s/^\///")
+       if [ "z$RESTORE_PATH" = "z" ]; then
+               echo "There is no file to restore"
+               return
+       fi
 
-       if [ "$REC" = "1" ]; then
-               REC_FILES=$(unzip -l $BACKUP_ZIP | grep $MOD_PATH | awk '{print $4}')
-               for REC_FILE in $REC_FILES; do
-                       if [ "z$DEST" = "z" ]; then
-                               restore_backup_file $REC_FILE
-                       else
-                               restore_backup_file $REC_FILE -d $DEST
-                       fi
-               done
+       if [ ! "z${RESTORE_PATH:0:1}" = "z/" ]; then
+               echo "Full path of file is required"
                return
        fi
 
-       unzip -nX $BACKUP_ZIP $MOD_PATH -d $DEST/
-       TMP=$(mktemp /tmp/smackinfo.XXXXXX)
-       PATH_FOR_SMACK=$(echo $MOD_PATH | sed -e "s/\/$//")
+       if [ -e "$RESTORE_PATH" ]; then
+               if [ ! "z$OVERWRITE" = "z" ]; then
+                       echo "Warning: $RESTORE_PATH already exists. It will be overwritten"
+               else
+                       echo "Error: $RESTORE_PATH already exists"
+                       return
+               fi
+       fi
 
-       FILE_PATH="$DEST/$PATH_FOR_SMACK"
-       SMACK_VAL=$(grep $PATH_FOR_SMACK'\ ' /usr/system/RestoreDir/smack_label.txt | \
-                       { read FILE SMACK; echo $SMACK; })
-       if [ "z$SMACK_VAL" = "z" ]; then
-               echo "No smack label for $PATH_FOR_SMACK"
+       # Check if the target file is backed up
+       PATH_FOR_ZIP=$(echo $RESTORE_PATH | sed -e "s/^\///")
+       FOUND_FILES=$(unzip -l $BACKUP_ZIP | awk '{print $4}' | \
+                       grep "^$PATH_FOR_ZIP")
+       FOUND_FILE=$(echo "$FOUND_FILES" | \
+                       grep -E "$PATH_FOR_ZIP$|$PATH_FOR_ZIP/$")
+       if [ "z$FOUND_FILE" = "z" ]; then
+               echo "Error: $RESTORE_PATH was not backed up"
+               return
+       fi
+
+       echo "restore_backup_file: $RESTORE_PATH"
+       if [ ! "z${FOUND_FILE: -1}" = "z/" ]; then
+               restore_from_zip $FOUND_FILE
        else
-               echo "$FILE_PATH $SMACK_VAL" > $TMP
-               rstsmack $TMP
+               for FILE in $FOUND_FILES; do
+                       restore_from_zip $FILE
+               done
        fi
-       rm $TMP
 }