<commit_before>#!/bin/bash

# Copyright (c) 2011 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.

# Script to enter the chroot environment

SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
. "${SCRIPT_ROOT}/common.sh" || exit 1

# Script must be run outside the chroot and as a regular user.
assert_outside_chroot
assert_not_root_user

# Define command line flags
# See http://code.google.com/p/shflags/wiki/Documentation10x
DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
  "The destination dir for the chroot environment." "d"
DEFINE_string trunk "$GCLIENT_ROOT" \
  "The source trunk to bind mount within the chroot." "s"
DEFINE_string build_number "" \
  "The build-bot build number (when called by buildbot only)." "b"
DEFINE_string chrome_root "" \
  "The root of your chrome browser source. Should contain a 'src' subdir."
DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
  "The mount point of the chrome broswer source in the chroot."

DEFINE_boolean official_build $FLAGS_FALSE \
  "Set CHROMEOS_OFFICIAL=1 for release builds."
DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"

# More useful help
FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]

One or more VAR=value pairs can be specified to export variables into
the chroot environment.  For example:

   $0 FOO=bar BAZ=bel

If [-- command] is present, runs the command inside the chroot,
after changing directory to /$USER/trunk/src/scripts.  Note that neither
the command nor args should include single quotes.  For example:

    $0 -- ./build_platform_packages.sh

Otherwise, provides an interactive shell.
"

# Version of info from common.sh that only echos if --verbose is set.
function debug {
  if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
    info "$*"
  fi
}

# Parse command line flags
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
   CHROMEOS_OFFICIAL=1
fi

# Only now can we die on error.  shflags functions leak non-zero error codes,
# so will die prematurely if 'set -e' is specified before now.
# TODO: replace shflags with something less error-prone, or contribute a fix.
set -e

INNER_CHROME_ROOT=$FLAGS_chrome_root_mount  # inside chroot
CHROME_ROOT_CONFIG="/var/cache/chrome_root"  # inside chroot
INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools"  # inside chroot
FUSE_DEVICE="/dev/fuse"
AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"

# Avoid the sudo call if possible since it is a little slow.
if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
  sudo chmod 0777 "$FLAGS_chroot/var/lock"
fi

LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"


MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
function mount_queue_init {
  MOUNT_QUEUE=()
}

function queue_mount {
  # If necessary, mount $source in the host FS at $target inside the
  # chroot directory with $mount_args.
  local source="$1"
  local mount_args="$2"
  local target="$3"

  local mounted_path="${MOUNTED_PATH}$target"

  case ${MOUNT_CACHE} in
  *" on ${mounted_path} "*)
    # Already mounted!
    ;;
  *)
    MOUNT_QUEUE+=(
      "mkdir -p '${mounted_path}'"
      # The args are left unquoted on purpose.
      "mount ${mount_args} '${source}' '${mounted_path}'"
    )
    ;;
  esac
}

function process_mounts {
  if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
    return 0
  fi
  sudo_multi "${MOUNT_QUEUE[@]}"
  mount_queue_init
}

function env_sync_proc {
  # This function runs and performs periodic updates to the chroot env, if
  # necessary.

  local poll_interval=10
  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.
  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.
  exec 0>&- 1>&- 2>&- 200>&-

  while true; do
    # Sync files
    for file in ${sync_files}; do
      if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
        cp -f /${file} ${FLAGS_chroot}/${file}
      fi
    done

    sleep ${poll_interval}
  done
}

function copy_ssh_config {
  # Copy user .ssh/config into the chroot filtering out strings not supported
  # by the chroot ssh. The chroot .ssh directory is passed in as the first
  # parameter.

  # ssh options to filter out. The entire strings containing these substrings
  # will be deleted before copying.
  local bad_options=(
    'UseProxyIf='
    'GSSAPIAuthentication no'
  )
  local sshc="${HOME}/.ssh/config"
  local chroot_ssh_dir="${1}"
  local filter
  local option

  if [ ! -f "${sshc}" ]; then
    return # Nothing to copy.
  fi

  for option in "${bad_options[@]}"
  do<commit_msg>Update the ssh/config filters in enter_chroot.sh<commit_after>#!/bin/bash

# Copyright (c) 2011 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.

# Script to enter the chroot environment

SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
. "${SCRIPT_ROOT}/common.sh" || exit 1

# Script must be run outside the chroot and as a regular user.
assert_outside_chroot
assert_not_root_user

# Define command line flags
# See http://code.google.com/p/shflags/wiki/Documentation10x
DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
  "The destination dir for the chroot environment." "d"
DEFINE_string trunk "$GCLIENT_ROOT" \
  "The source trunk to bind mount within the chroot." "s"
DEFINE_string build_number "" \
  "The build-bot build number (when called by buildbot only)." "b"
DEFINE_string chrome_root "" \
  "The root of your chrome browser source. Should contain a 'src' subdir."
DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
  "The mount point of the chrome broswer source in the chroot."

DEFINE_boolean official_build $FLAGS_FALSE \
  "Set CHROMEOS_OFFICIAL=1 for release builds."
DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"

# More useful help
FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]

One or more VAR=value pairs can be specified to export variables into
the chroot environment.  For example:

   $0 FOO=bar BAZ=bel

If [-- command] is present, runs the command inside the chroot,
after changing directory to /$USER/trunk/src/scripts.  Note that neither
the command nor args should include single quotes.  For example:

    $0 -- ./build_platform_packages.sh

Otherwise, provides an interactive shell.
"

# Version of info from common.sh that only echos if --verbose is set.
function debug {
  if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
    info "$*"
  fi
}

# Parse command line flags
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
   CHROMEOS_OFFICIAL=1
fi

# Only now can we die on error.  shflags functions leak non-zero error codes,
# so will die prematurely if 'set -e' is specified before now.
# TODO: replace shflags with something less error-prone, or contribute a fix.
set -e

INNER_CHROME_ROOT=$FLAGS_chrome_root_mount  # inside chroot
CHROME_ROOT_CONFIG="/var/cache/chrome_root"  # inside chroot
INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools"  # inside chroot
FUSE_DEVICE="/dev/fuse"
AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"

# Avoid the sudo call if possible since it is a little slow.
if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
  sudo chmod 0777 "$FLAGS_chroot/var/lock"
fi

LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"


MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
function mount_queue_init {
  MOUNT_QUEUE=()
}

function queue_mount {
  # If necessary, mount $source in the host FS at $target inside the
  # chroot directory with $mount_args.
  local source="$1"
  local mount_args="$2"
  local target="$3"

  local mounted_path="${MOUNTED_PATH}$target"

  case ${MOUNT_CACHE} in
  *" on ${mounted_path} "*)
    # Already mounted!
    ;;
  *)
    MOUNT_QUEUE+=(
      "mkdir -p '${mounted_path}'"
      # The args are left unquoted on purpose.
      "mount ${mount_args} '${source}' '${mounted_path}'"
    )
    ;;
  esac
}

function process_mounts {
  if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
    return 0
  fi
  sudo_multi "${MOUNT_QUEUE[@]}"
  mount_queue_init
}

function env_sync_proc {
  # This function runs and performs periodic updates to the chroot env, if
  # necessary.

  local poll_interval=10
  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.
  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.
  exec 0>&- 1>&- 2>&- 200>&-

  while true; do
    # Sync files
    for file in ${sync_files}; do
      if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
        cp -f /${file} ${FLAGS_chroot}/${file}
      fi
    done

    sleep ${poll_interval}
  done
}

function copy_ssh_config {
  # Copy user .ssh/config into the chroot filtering out strings not supported
  # by the chroot ssh. The chroot .ssh directory is passed in as the first
  # parameter.

  # ssh options to filter out. The entire strings containing these substrings
  # will be deleted before copying.
  local bad_options=(
    'UseProxyIf'
    'GSSAPIAuthentication'
    'GSSAPIKeyExchange'
    'ProxyUseFdpass'
  )
  local sshc="${HOME}/.ssh/config"
  local chroot_ssh_dir="${1}"
  local filter
  local option

  if [ ! -f "${sshc}" ]; then
    return # Nothing to copy.
  fi

  for option in "${bad_options[@]}"
  do
{'fraction_non_alphanumeric': 0.1125, 'fraction_numerical': 0.007442748091603053, 'mean_word_length': 3.850069412309116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476240', 'n_tokens_mistral': 3590, 'n_tokens_neox': 3409, 'n_words': 1165}