aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/tmpfile
blob: 371f5f8360b7007d861045e6bc719dc050c8751d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash

# Setup a temporary file
function hydra_set_tmpfile {
  if [ -z "$BASEDIR" ]; then
    BASEDIR="/tmp"
  fi

  if [ -z "$1" ]; then
    template="$BASEDIR/tmp/hydra.XXXXXXXXXX"
  else
    template="$BASEDIR/tmp/$1.XXXXXXXXXX"
  fi

  mkdir -p $BASEDIR/tmp
  hydra_git_ignore 'tmp/*'

  if [ "$2" == "-d" ]; then
    TMPWORK="`mktemp -d $template`"
  else
    TMPWORK="`mktemp $template`"
  fi
  
  if [ "$?" != "0" ]; then
    echo "Error: can't set TMPWORK $TMPWORK"
    exit 1
  fi

  trap "hydra_unset_tmpfile $TMPWORK; exit" INT TERM EXIT
}

# Remove a temporary file
function hydra_unset_tmpfile {
  if [ -z "$1" ]; then
    echo "No tmp file set"
  fi

  rm -rf $1

  if [ "$?" != "0" ]; then
    echo "Warning: could not delete file $1. Please delete it manually as it might have sensitive information."
    exit 1
  fi

  rmdir $BASEDIR/tmp &> /dev/null
}