<commit_before>#!/bin/bash

create_volume_file() {
  local volume_file=$1

  <% if p('grootfs.store_size_bytes') <= 0 %>
  volume_size="$(( $(df --output=size  /var/vcap/data | sed 1d) * 1024 ))"
  <% else %>
  volume_size="<%= p('grootfs.store_size_bytes') %>"
  <% end %>

  # Do no recreate the volume file if it already exists
  if [ ! -f "$volume_file" ]
  then
    echo "creating volume..."
    truncate -s $volume_size  $volume_file
  fi
}

create_loop_devices() {
  echo "creating loop devices..."
  for i in {0..255}
  do
    mknod -m 0660 /dev/loop${i} b 7 $i || true
  done
}

# copied from https://github.com/concourse/concourse/blob/master/jobs/baggageclaim/templates/baggageclaim_ctl.erb#L54
# break out of bosh-lite device limitations
permit_device_control() {
  local devices_mount_info=$(cat /proc/self/cgroup | grep devices)

  if [ -z "$devices_mount_info" ]; then
    # cgroups not set up; must not be in a container
    return
  fi

  local devices_subsytems=$(echo $devices_mount_info | cut -d: -f2)
  local devices_subdir=$(echo $devices_mount_info | cut -d: -f3)

  if [ "$devices_subdir" = "/" ]; then
    # we're in the root devices cgroup; must not be in a container
    return
  fi

  if [ ! -e /tmp/devices-cgroup ]; then
    # mount our container's devices subsystem somewhere
    mkdir /tmp/devices-cgroup
    mount -t cgroup -o $devices_subsytems none /tmp/devices-cgroup
  fi

  # permit our cgroup to do everything with all devices
  echo a > /tmp/devices-cgroup${devices_subdir}/devices.allow || true

  umount /tmp/devices-cgroup || true
}

setup_unprivileged_store() {
  local store_path=$1

  maximus_uid=$(/var/vcap/packages/idmapper/bin/maximus)
  chown $maximus_uid:$maximus_uid $store_path
  chmod 0700 $store_path
}

delete_store() {
  local config_path=$1
  local store_path=$2
  local volume_file=$3

  if [ -d "$store_path" ] || [ -f "$volume_file ]; then
    # Allow grootfs delete-store and umount to fail, because we could
    # have an malformed store.
    grootfs --config $config_path delete-store || true
    umount --force $store_path || true

    # Ensure that the store is deleted, or fail
    rm -rf $store_path

    rm -rf $volume_file
  fi
}<commit_msg>Add retry logic to umount volumes<commit_after>#!/bin/bash

create_volume_file() {
  local volume_file=$1

  <% if p('grootfs.store_size_bytes') <= 0 %>
  volume_size="$(( $(df --output=size  /var/vcap/data | sed 1d) * 1024 ))"
  <% else %>
  volume_size="<%= p('grootfs.store_size_bytes') %>"
  <% end %>

  # Do no recreate the volume file if it already exists
  if [ ! -f "$volume_file" ]
  then
    echo "creating volume..."
    truncate -s $volume_size  $volume_file
  fi
}

create_loop_devices() {
  echo "creating loop devices..."
  for i in {0..255}
  do
    mknod -m 0660 /dev/loop${i} b 7 $i || true
  done
}

# copied from https://github.com/concourse/concourse/blob/master/jobs/baggageclaim/templates/baggageclaim_ctl.erb#L54
# break out of bosh-lite device limitations
permit_device_control() {
  local devices_mount_info=$(cat /proc/self/cgroup | grep devices)

  if [ -z "$devices_mount_info" ]; then
    # cgroups not set up; must not be in a container
    return
  fi

  local devices_subsytems=$(echo $devices_mount_info | cut -d: -f2)
  local devices_subdir=$(echo $devices_mount_info | cut -d: -f3)

  if [ "$devices_subdir" = "/" ]; then
    # we're in the root devices cgroup; must not be in a container
    return
  fi

  if [ ! -e /tmp/devices-cgroup ]; then
    # mount our container's devices subsystem somewhere
    mkdir /tmp/devices-cgroup
    mount -t cgroup -o $devices_subsytems none /tmp/devices-cgroup
  fi

  # permit our cgroup to do everything with all devices
  echo a > /tmp/devices-cgroup${devices_subdir}/devices.allow || true

  umount /tmp/devices-cgroup || true
}

setup_unprivileged_store() {
  local store_path=$1

  maximus_uid=$(/var/vcap/packages/idmapper/bin/maximus)
  chown $maximus_uid:$maximus_uid $store_path
  chmod 0700 $store_path
}

delete_store() {
  local config_path=$1
  local store_path=$2
  local volume_file=$3

  if [ -d "$store_path" ] || [ -f "$volume_file" ]; then
    # Allow grootfs delete-store and umount to fail, because we could
    # have an malformed store.
    grootfs --config $config_path delete-store || true
    try_umount $store_path || true

    # Ensure that the store is deleted, or fail
    rm -rf $store_path

    rm -rf $volume_file
  fi
}

try_umount() {
  local mount_point=$1

  if ! mountpoint $mount_point
  then
    return
  fi

  local n=0
   until [ $n -ge 5 ]
   do
      umount $mount_point && return
      n=$[$n+1]
      sleep 2
   done

   echo "Failed to unmount $mount_point"
}
{'fraction_non_alphanumeric': 0.12185769066893908, 'fraction_numerical': 0.012995313165743502, 'mean_word_length': 3.2450271247739604, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '2120714', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1639, 'n_words': 464}