Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 1 | #!/sbin/sh |
| 2 | # |
| 3 | # Functions for backuptool.sh |
| 4 | # |
| 5 | |
Marko Man | 2d21873 | 2017-09-12 02:24:39 +0200 | [diff] [blame] | 6 | DEBUG=0 |
| 7 | |
Gabriele M | 8252162 | 2017-02-27 15:44:17 +0100 | [diff] [blame] | 8 | copy_file() { |
| 9 | cp -dp "$1" "$2" |
| 10 | # symlinks don't have a context |
| 11 | if [ ! -L "$1" ]; then |
| 12 | # it is assumed that every label starts with 'u:object_r' and has no white-spaces |
| 13 | local context=`ls -Z "$1" | grep -o 'u:object_r:[^ ]*' | head -1` |
| 14 | chcon "$context" "$2" |
| 15 | fi |
| 16 | } |
| 17 | |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 18 | backup_file() { |
| 19 | if [ -e "$1" ]; then |
| 20 | local F=`basename "$1"` |
| 21 | local D=`dirname "$1"` |
| 22 | # dont backup any apps that have odex files, they are useless |
| 23 | if ( echo "$F" | grep -q "\.apk$" ) && [ -e `echo "$1" | sed -e 's/\.apk$/\.odex/'` ]; then |
Marko Man | 2d21873 | 2017-09-12 02:24:39 +0200 | [diff] [blame] | 24 | echo "Skipping odexed apk $1" |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 25 | else |
| 26 | mkdir -p "$C/$D" |
Gabriele M | 8252162 | 2017-02-27 15:44:17 +0100 | [diff] [blame] | 27 | copy_file "$1" "$C/$D/$F" |
Marko Man | 2d21873 | 2017-09-12 02:24:39 +0200 | [diff] [blame] | 28 | if [ $DEBUG -eq 1 ]; then |
| 29 | echo backup_file "$1" "$C/$D/$F" |
| 30 | fi |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 31 | fi |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | restore_file() { |
| 36 | local FILE=`basename "$1"` |
| 37 | local DIR=`dirname "$1"` |
| 38 | if [ -e "$C/$DIR/$FILE" ]; then |
| 39 | if [ ! -d "$DIR" ]; then |
Marko Man | 2d21873 | 2017-09-12 02:24:39 +0200 | [diff] [blame] | 40 | mkdir -p "$DIR" |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 41 | fi |
Alessandro Astone | 09bec2c | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 42 | copy_file "$C/$DIR/$FILE" $(get_output_path "$1"); |
Marko Man | 2d21873 | 2017-09-12 02:24:39 +0200 | [diff] [blame] | 43 | if [ $DEBUG -eq 1 ]; then |
| 44 | echo restore_file "$C/$DIR/$FILE" "$1" |
| 45 | fi |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 46 | if [ -n "$2" ]; then |
| 47 | echo "Deleting obsolete file $2" |
Alessandro Astone | 09bec2c | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 48 | rm $(get_output_path "$2"); |
Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame] | 49 | fi |
| 50 | fi |
| 51 | } |
Alessandro Astone | 09bec2c | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 52 | |
| 53 | get_output_path() { |
| 54 | # In recovery we mounted all partitions in the right place, so we can rely on symlinks |
| 55 | echo "$1" |
| 56 | } |