Skip to content

Commit

Permalink
Work with filenames that include spaces
Browse files Browse the repository at this point in the history
• Simplified the stat generation line (single exec using only stat)
• Quote variables so that the cache directory can also include spaces
• while/read loop to handle spaces in cached files
  (IFS was causing problems when all the files were saved into a single variable)
  • Loading branch information
jaygooby committed Apr 2, 2019
1 parent 7cbb4c9 commit ddbcec5
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions test/sample_delcache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,60 +34,59 @@ if [ "X$1" = "X-h" -o "X$1" = "X-H" ]; then
fi
if [ "X$1" = "X" -o "X$2" = "X" -o "X$3" = "X" ]; then
func_usage $PRGNAME
exit -1
exit 1
fi

BUCKET=$1
CDIR=$2
CDIR="$2"
LIMIT=$3
SILENT=0
if [ "X$4" = "X-silent" ]; then
SILENT=1
fi
FILES_CDIR=$CDIR/$BUCKET
STATS_CDIR=$CDIR/\.$BUCKET\.stat

FILES_CDIR="${CDIR}/${BUCKET}"
STATS_CDIR="${CDIR}/.${BUCKET}.stat"
CURRENT_CACHE_SIZE=`du -sb "$FILES_CDIR" | awk '{print $1}'`
#
# Check total size
#
if [ $LIMIT -ge `du -sb $FILES_CDIR | awk '{print $1}'` ]; then
if [ $LIMIT -ge $CURRENT_CACHE_SIZE ]; then
if [ $SILENT -ne 1 ]; then
echo "$FILES_CDIR is below allowed $LIMIT"
echo "$FILES_CDIR ($CURRENT_CACHE_SIZE) is below allowed $LIMIT"
fi
exit 0
fi

#
# Make file list by sorted access time
#
ALL_STATS_ATIMELIST=`find $STATS_CDIR -type f -exec echo -n {} \; -exec echo -n " " \; -exec stat -c %X {} \; | awk '{print $2":"$1}' | sort`

#
# Remove loop
#
TMP_ATIME=0
TMP_STATS=""
TMP_CFILE=""
for part in $ALL_STATS_ATIMELIST; do
TMP_ATIME=`echo $part | sed 's/\:/ /' | awk '{print $1}'`
TMP_STATS=`echo $part | sed 's/\:/ /' | awk '{print $2}'`
TMP_CFILE=`echo $TMP_STATS | sed s/\.$BUCKET\.stat/$BUCKET/`

if [ `stat -c %X $TMP_STATS` -eq $TMP_ATIME ]; then
rm -f $TMP_STATS $TMP_CFILE > /dev/null 2>&1
#
# Make file list by sorted access time
#
find "$STATS_CDIR" -type f -exec stat -c "%X:%n" "{}" \; | sort | while read part
do
echo Looking at $part
TMP_ATIME=`echo "$part" | cut -d: -f1`
TMP_STATS="`echo "$part" | cut -d: -f2`"
TMP_CFILE=`echo "$TMP_STATS" | sed s/\.$BUCKET\.stat/$BUCKET/`

if [ `stat -c %X "$TMP_STATS"` -eq $TMP_ATIME ]; then
rm -f "$TMP_STATS" "$TMP_CFILE" > /dev/null 2>&1
if [ $? -ne 0 ]; then
if [ $SILENT -ne 1 ]; then
echo "ERROR: Could not remove files($TMP_STATS,$TMP_CFILE)"
fi
exit -1
exit 1
else
if [ $SILENT -ne 1 ]; then
echo "remove file: $TMP_CFILE $TMP_STATS"
fi
fi
fi

if [ $LIMIT -ge `du -sb $FILES_CDIR | awk '{print $1}'` ]; then
if [ $LIMIT -ge `du -sb "$FILES_CDIR" | awk '{print $1}'` ]; then
if [ $SILENT -ne 1 ]; then
echo "finish removing files"
fi
Expand All @@ -96,7 +95,7 @@ for part in $ALL_STATS_ATIMELIST; do
done

if [ $SILENT -ne 1 ]; then
TOTAL_SIZE=`du -sb $FILES_CDIR | awk '{print $1}'`
TOTAL_SIZE=`du -sb "$FILES_CDIR" | awk '{print $1}'`
echo "Finish: $FILES_CDIR total size is $TOTAL_SIZE"
fi

Expand Down

0 comments on commit ddbcec5

Please sign in to comment.