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