blob: d180c7c2435c528565da6eeaa77018867cb27e97 [file] [log] [blame]
Andrew Dodd0c209012013-09-28 15:34:25 -04001#!/sbin/sh
2#
3# Functions for backuptool.sh
4#
5
Marko Man2d218732017-09-12 02:24:39 +02006DEBUG=0
7
Gabriele M82521622017-02-27 15:44:17 +01008copy_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 Dodd0c209012013-09-28 15:34:25 -040018backup_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 Man2d218732017-09-12 02:24:39 +020024 echo "Skipping odexed apk $1"
Andrew Dodd0c209012013-09-28 15:34:25 -040025 else
26 mkdir -p "$C/$D"
Gabriele M82521622017-02-27 15:44:17 +010027 copy_file "$1" "$C/$D/$F"
Marko Man2d218732017-09-12 02:24:39 +020028 if [ $DEBUG -eq 1 ]; then
29 echo backup_file "$1" "$C/$D/$F"
30 fi
Andrew Dodd0c209012013-09-28 15:34:25 -040031 fi
32 fi
33}
34
35restore_file() {
36 local FILE=`basename "$1"`
37 local DIR=`dirname "$1"`
38 if [ -e "$C/$DIR/$FILE" ]; then
39 if [ ! -d "$DIR" ]; then
Marko Man2d218732017-09-12 02:24:39 +020040 mkdir -p "$DIR"
Andrew Dodd0c209012013-09-28 15:34:25 -040041 fi
Alessandro Astone09bec2c2020-12-29 18:38:28 +010042 copy_file "$C/$DIR/$FILE" $(get_output_path "$1");
Marko Man2d218732017-09-12 02:24:39 +020043 if [ $DEBUG -eq 1 ]; then
44 echo restore_file "$C/$DIR/$FILE" "$1"
45 fi
Andrew Dodd0c209012013-09-28 15:34:25 -040046 if [ -n "$2" ]; then
47 echo "Deleting obsolete file $2"
Alessandro Astone09bec2c2020-12-29 18:38:28 +010048 rm $(get_output_path "$2");
Andrew Dodd0c209012013-09-28 15:34:25 -040049 fi
50 fi
51}
Alessandro Astone09bec2c2020-12-29 18:38:28 +010052
53get_output_path() {
54 # In recovery we mounted all partitions in the right place, so we can rely on symlinks
55 echo "$1"
56}