<commit_before>#!/usr/bin/env bash

#Thanks to http://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/
if [ $# -eq 0 ] || [ $# -eq 1 ]; then

  echo "Not correct arguments supplied. Usage: ./add-folder.sh <host-path> <container-path> <stop-nginx>" && \
  exit 1
fi

set -e
HOSTPATH=$1
CONTPATH=$2
TO_STOP=$3
TMPDIR='/tmpmnt'
REALPATH=$(readlink --canonicalize $HOSTPATH)
FILESYS=$(df -P $REALPATH | tail -n 1 | awk '{print $6}')

while read DEV MOUNT JUNK
do [ $MOUNT = $FILESYS ] && break
done < /proc/mounts
[ $MOUNT = $FILESYS ] # Sanity check!

while read A B C SUBROOT MOUNT JUNK
do [ $MOUNT = $FILESYS ] && break
done < /proc/self/mountinfo
[ $MOUNT = $FILESYS ] # Moar sanity check!

SUBPATH=$(echo $REALPATH | sed s,^$FILESYS,,)
DEVDEC=$(printf "%d %d" $(stat --format "0x%t 0x%T" $DEV))

if [[ $TO_STOP ]]; then

  echo "Stopping nginx inside the container" && \
  docker exec -it nginx nginx -s stop
fi

docker exec -it nginx bash -c "[ -b $DEV ] || mknod --mode 0600 $DEV b $DEVDEC"
docker exec -it nginx bash -c "mkdir -p $TMPDIR"
docker exec -it nginx bash -c "mount $DEV $TMPDIR"
docker exec -it nginx bash -c "mkdir -p /tmp/swap_folder"

if [[ $(find $REALPATH -maxdepth 0 -type d -empty 2>/dev/null) ]]; then

  #empty
  docker exec -it nginx bash -c "[ $(find $CONTPATH -maxdepth 0 -type d -empty 2>/dev/null) ] && mkdir -p $CONTPATH || mv $CONTPATH/* /tmp/swap_folder"
else

  #not empty
  docker exec -it nginx bash -c "[ $(find $CONTPATH -maxdepth 0 -type d -empty 2>/dev/null) ] && mkdir -p $CONTPATH || rm -Rf $CONTPATH/*"
fi

docker exec -it nginx bash -c "mount -o bind $TMPDIR/$SUBROOT/$SUBPATH $CONTPATH"
docker exec -it nginx bash -c "[ $(find /tmp/swap_folder -maxdepth 0 -type d -empty 2>/dev/null) ] || mv /tmp/swap_folder/* $CONTPATH 2>/dev/null"

docker exec -it nginx bash -c "rm -Rf /tmp/swap_folder && umount $TMPDIR && rmdir $TMPDIR"
<commit_msg>dunno why<commit_after>#!/usr/bin/env bash

#Thanks to http://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/
if [ $# -eq 0 ] || [ $# -eq 1 ]; then

  echo "Not correct arguments supplied. Usage: ./add-folder.sh <host-path> <container-path> <stop-nginx>" && \
  exit 1
fi

set -e
HOSTPATH=$1
CONTPATH=$2
TO_STOP=$3
TMPDIR='/tmpmnt'
REALPATH=$(readlink --canonicalize $HOSTPATH)
FILESYS=$(df -P $REALPATH | tail -n 1 | awk '{print $6}')

while read DEV MOUNT JUNK
do [ $MOUNT = $FILESYS ] && break
done < /proc/mounts
[ $MOUNT = $FILESYS ] # Sanity check!

while read A B C SUBROOT MOUNT JUNK
do [ $MOUNT = $FILESYS ] && break
done < /proc/self/mountinfo
[ $MOUNT = $FILESYS ] # Moar sanity check!

SUBPATH=$(echo $REALPATH | sed s,^$FILESYS,,)
DEVDEC=$(printf "%d %d" $(stat --format "0x%t 0x%T" $DEV))

if [[ $TO_STOP ]]; then

  echo "Stopping nginx inside the container" && \
  docker exec -it nginx nginx -s stop
fi

docker exec -it nginx bash -c "[ -b $DEV ] || mknod --mode 0600 $DEV b $DEVDEC"
docker exec -it nginx bash -c "mkdir -p $TMPDIR"
docker exec -it nginx bash -c "mount $DEV $TMPDIR"
docker exec -it nginx bash -c "mkdir -p /tmp/swap_folder"

if [[ $(find $REALPATH -maxdepth 0 -type d -empty 2>/dev/null) ]]; then

  #empty
  docker exec -it nginx bash -c "[ $(find $CONTPATH -maxdepth 0 -type d -empty 2>/dev/null) ] && mkdir -p $CONTPATH || mv $CONTPATH/* /tmp/swap_folder"
else

  #not empty
  docker exec -it nginx bash -c "[ $(find $CONTPATH -maxdepth 0 -type d -empty 2>/dev/null) ] && mkdir -p $CONTPATH || rm -Rf $CONTPATH/*"
fi

docker exec -it nginx bash -c "mount -o bind $TMPDIR/$SUBROOT/$SUBPATH $CONTPATH"
docker exec -it nginx bash -c "[ $(find /tmp/swap_folder -maxdepth 0 -type d -empty 2>/dev/null) ] || mv /tmp/swap_folder/* $CONTPATH"

docker exec -it nginx bash -c "rm -Rf /tmp/swap_folder && umount $TMPDIR && rmdir $TMPDIR"

{'fraction_non_alphanumeric': 0.16790450928381964, 'fraction_numerical': 0.016180371352785147, 'mean_word_length': 3.369640787949015, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, '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': '563957', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1470, 'n_words': 486}