aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/internal/platform/efficient_android_directory_copy.sh
blob: 7021109e273e1b45649159405d14e1f27fe4a7fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/system/bin/sh

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Android shell script to make the destination directory identical with the
# source directory, without doing unnecessary copies. This assumes that the
# the destination directory was originally a copy of the source directory, and
# has since been modified.

source=$1
dest=$2
echo copying $source to $dest

delete_extra() {
  # Don't delete symbolic links, since doing so deletes the vital lib link.
  if [ ! -L "$1" ]
  then
    if [ ! -e "$source/$1" ]
    then
      echo rm -rf "$dest/$1"
      rm -rf "$dest/$1"
    elif [ -d "$1" ]
    then
      for f in "$1"/*
      do
       delete_extra "$f"
      done
    fi
  fi
}

copy_if_older() {
  if [ -d "$1" ] && [ -e "$dest/$1" ]
  then
    if [ ! -e "$dest/$1" ]
    then
      echo cp -a "$1" "$dest/$1"
      cp -a "$1" "$dest/$1"
    else
      for f in "$1"/*
      do
        copy_if_older "$f"
      done
    fi
  elif [ ! -e "$dest/$1" ] || [ "$1" -ot "$dest/$1" ] || [ "$1" -nt "$dest/$1" ]
  then
    # dates are different, so either the destination of the source has changed.
    echo cp -a "$1" "$dest/$1"
    cp -a "$1" "$dest/$1"
  fi
}

if [ -e "$dest" ]
then
  echo cd "$dest"
  cd "$dest"
  for f in ./*
  do
    if [ -e "$f" ]
    then
      delete_extra "$f"
    fi
  done
else
  echo mkdir "$dest"
  mkdir "$dest"
fi
echo cd "$source"
cd "$source"
for f in ./*
do
  if [ -e "$f" ]
  then
    copy_if_older "$f"
  fi
done