merge sdk & board distdirs

We've merged these two dirs to avoid double fetching, so add some
hooks to clean up the old paths as they can take up many GB of data.

BUG=None
TEST=CQ passes

Cq-Depend: chromium:2277139
Change-Id: I3285b9de3373d7bf222c638de8b8e8de742b5c3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2276308
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/chroot_version_hooks.d/178_merge_distdir b/chroot_version_hooks.d/178_merge_distdir
new file mode 100644
index 0000000..f0598a2
--- /dev/null
+++ b/chroot_version_hooks.d/178_merge_distdir
@@ -0,0 +1,46 @@
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# We changed the distfile download dirs to a combined one, so move the old
+# content up to the common location.
+
+# Delete the old symlinks.
+sudo rm -f /var/lib/portage/distfiles /var/lib/portage/distfiles-target
+
+# Merge the content.
+echo "Merging old distfiles paths"
+cd /var/cache/distfiles
+for srcdir in target host; do
+  if [[ ! -d "${srcdir}" ]]; then
+    continue
+  fi
+
+  echo "  ${srcdir} ..."
+  # Merge the two git subdirs.
+  for gitdir in egit-src git3-src; do
+    mkdir -p "${gitdir}"
+    if [[ -d "${srcdir}/${gitdir}" ]]; then
+      for d in $(find "${srcdir}/${gitdir}/" -mindepth 1 -maxdepth 1 -type d \
+                   -printf '%f\n'); do
+        if [[ ! -d "${gitdir}/${d}" ]]; then
+          mv "${srcdir}/${gitdir}/${d}" "${gitdir}/" || exit 1
+        fi
+      done
+    fi
+  done
+
+  # Clear all the subdirs now that they're migrated.
+  sudo rm -rf "${srcdir}"/*/
+
+  # Merge the files.
+  rm -f "${srcdir}"/*._checksum_failure_.*
+  for f in "${srcdir}"/*; do
+    if [[ ! -e "${f##*/}" ]]; then
+      mv "${f}" ./ || exit 1
+    fi
+  done
+
+  # Delete the dir.
+  sudo rm -rf "${srcdir}" || exit 1
+done
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index ab52b36..5199de5 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -476,7 +476,7 @@
 
     chroot_cache='/var/cache/chromeos-cache'
     debug "Setting up shared cache dir directory."
-    user_mkdir "${FLAGS_cache_dir}"/distfiles/{target,host}
+    user_mkdir "${FLAGS_cache_dir}"/distfiles
     user_mkdir "${FLAGS_chroot}/${chroot_cache}"
     setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
     # TODO(build): remove this as of 12/01/12.
diff --git a/sdk_lib/make_chroot.sh b/sdk_lib/make_chroot.sh
index 61879f0..037b190 100755
--- a/sdk_lib/make_chroot.sh
+++ b/sdk_lib/make_chroot.sh
@@ -280,14 +280,6 @@
    # these are defined as w/in the chroot.
    bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
 
-   # These are created for compatibility while transitioning
-   # make.conf and friends over to the new location.
-   # TODO(ferringb): remove this 01/13 or so.
-   ln -s ../../cache/chromeos-cache/distfiles/host \
-     "${FLAGS_chroot}/var/lib/portage/distfiles"
-   ln -s ../../cache/chromeos-cache/distfiles/target \
-     "${FLAGS_chroot}/var/lib/portage/distfiles-target"
-
    # Add chromite/bin and depot_tools into the path globally; note that the
    # chromite wrapper itself might also be found in depot_tools.
    # We rely on 'env-update' getting called below.