xref: /dragonfly/contrib/lvm2/dist/test/mkdtemp (revision cf89a63b)
1#!/bin/sh
2# Create a temporary directory, sort of like mktemp -d does.
3
4# Copyright (C) 2007 Red Hat, Inc. All rights reserved.
5#
6# This copyrighted material is made available to anyone wishing to use,
7# modify, copy, or redistribute it subject to the terms and conditions
8# of the GNU General Public License v.2.
9#
10# You should have received a copy of the GNU General Public License
11# along with this program; if not, write to the Free Software Foundation,
12# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13
14# Written by Jim Meyering.
15
16# Usage: mkdtemp /tmp phoey.XXXXXXXXXX
17
18# First, try to use the mktemp program.
19# Failing that, we'll roll our own mktemp-like function:
20#  - try to get random bytes from /dev/urandom
21#  - failing that, generate output from a combination of quickly-varying
22#      sources and gzip.  Ignore non-varying gzip header, and extract
23#      "random" bits from there.
24#  - given those bits, map to file-name bytes using tr, and try to create
25#      the desired directory.
26#  - make only $MAX_TRIES attempts
27
28ME=$(basename "$0")
29die() { echo >&2 "$ME: $@"; exit 1; }
30
31MAX_TRIES=4
32
33rand_bytes()
34{
35  n=$1
36
37  chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
38
39  dev_rand=/dev/urandom
40  if test -r "$dev_rand"; then
41    # Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194.
42    head -c$n "$dev_rand" | tr -c $chars 01234567$chars$chars$chars
43    return
44  fi
45
46  cmds='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
47  data=$( (eval "$cmds") 2>&1 | gzip )
48
49  n_plus_50=$(expr $n + 50)
50
51  # Ensure that $data has length at least 50+$n
52  while :; do
53    len=$(echo "$data"|wc -c)
54    test $n_plus_50 -le $len && break;
55    data=$( (echo "$data"; eval "$cmds") 2>&1 | gzip )
56  done
57
58  echo "$data" \
59    | dd bs=1 skip=50 count=$n 2>/dev/null \
60    | tr -c $chars 01234567$chars$chars$chars
61}
62
63mkdtemp()
64{
65  case $# in
66  2);;
67  *) die "Usage: $ME DIR TEMPLATE";;
68  esac
69
70  destdir=$1
71  template=$2
72
73  case $template in
74  *XXXX) ;;
75  *) die "invalid template: $template (must have a suffix of at least 4 X's)";;
76  esac
77
78  fail=0
79
80  # First, try to use mktemp.
81  d=$(env -u TMPDIR mktemp -d -t -p "$destdir" "$template" 2>/dev/null) \
82    || fail=1
83
84  # The resulting name must be in the specified directory.
85  case $d in "$destdir"*);; *) fail=1;; esac
86
87  # It must have created the directory.
88  test -d "$d" || fail=1
89
90  # It must have 0700 permissions.
91  perms=$(ls -dgo "$d" 2>/dev/null) || fail=1
92  case $perms in drwx------*) ;; *) fail=1;; esac
93
94  test $fail = 0 && {
95    echo "$d"
96    return
97  }
98
99  # If we reach this point, we'll have to create a directory manually.
100
101  # Get a copy of the template without its suffix of X's.
102  base_template=$(echo "$template"|sed 's/XX*$//')
103
104  # Calculate how many X's we've just removed.
105  nx=$(expr length "$template" - length "$base_template")
106
107  err=
108  i=1
109  while :; do
110    X=$(rand_bytes $nx)
111    candidate_dir="$destdir/$base_template$X"
112    err=$(mkdir -m 0700 "$candidate_dir" 2>&1) \
113      && { echo "$candidate_dir"; return; }
114    test $MAX_TRIES -le $i && break;
115    i=$(expr $i + 1)
116  done
117  die "$err"
118}
119
120mkdtemp "$@"
121