enter_chroot: optimize env_sync_proc ownership changing

Most of the time the files in the chroot /etc/ are already changed to
the proper owner.  Only on the first run do they need to be changed.
So check the owner before doing the sudo as that is a bit faster.  If
we do end up needing to run sudo, bundle the commands up to avoid having
to execute sudo multiple times.

BUG=None
TEST=`sudo chown root chroot/etc/resolv.conf && cros_sdk --enter`; see /etc/resolv.conf owned by me

Change-Id: Ifd974ace168ab309fdc2e7583d135ee23576a5bb
Reviewed-on: http://gerrit.chromium.org/gerrit/8418
Reviewed-by: David James <davidjames@chromium.org>
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 69f62e5..8da0ab5 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -118,13 +118,14 @@
   # necessary.
 
   local poll_interval=10
-  local sync_files="etc/resolv.conf etc/hosts"
+  local sync_files=( etc/resolv.conf etc/hosts )
 
   # Make sure the synced files are writable by normal user, so that we
   # don't have to sudo inside the loop.
-  for file in ${sync_files}; do
-    sudo chown ${USER} ${FLAGS_chroot}/${file} 1>&2
-  done
+  local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER})
+  if [ -n "${chmods}" ]; then
+    sudo -- chown ${USER} ${chmods} 1>&2
+  fi
 
   # Drop stdin, stderr, stdout, and chroot lock.
   # This is needed for properly daemonizing the process.