wctype-h: Fix last change.
[gnulib.git] / build-aux / mktempd
1 #!/bin/sh
2 # Create a temporary directory, much like mktemp -d does.
3
4 # Copyright (C) 2007-2011 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 # Written by Jim Meyering.
20
21 # Usage: mktempd /tmp phoey.XXXXXXXXXX
22
23 # First, try to use the mktemp program.
24 # Failing that, we'll roll our own mktemp-like function:
25 #  - try to get random bytes from /dev/urandom
26 #  - failing that, generate output from a combination of quickly-varying
27 #      sources and gzip.  Ignore non-varying gzip header, and extract
28 #      "random" bits from there.
29 #  - given those bits, map to file-name bytes using tr, and try to create
30 #      the desired directory.
31 #  - make only $MAX_TRIES attempts
32
33 ME=`basename "$0"`
34 die() { echo >&2 "$ME: $@"; exit 1; }
35
36 MAX_TRIES=4
37
38 rand_bytes()
39 {
40   n=$1
41
42   chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
43
44   dev_rand=/dev/urandom
45   if test -r "$dev_rand"; then
46     # Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194.
47     dd ibs=$n count=1 if="$dev_rand" | tr -c $chars 01234567$chars$chars$chars
48     return
49   fi
50
51   cmds='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
52   data=` (eval "$cmds") 2>&1 | gzip `
53
54   n_plus_50=`expr $n + 50`
55
56   # Ensure that $data has length at least 50+$n
57   while :; do
58     len=`echo "$data"|wc -c`
59     test $n_plus_50 -le $len && break;
60     data=` (echo "$data"; eval "$cmds") 2>&1 | gzip `
61   done
62
63   echo "$data" \
64     | dd bs=1 skip=50 count=$n 2>/dev/null \
65     | tr -c $chars 01234567$chars$chars$chars
66 }
67
68 mktempd()
69 {
70   case $# in
71   2);;
72   *) die "Usage: $ME DIR TEMPLATE";;
73   esac
74
75   destdir=$1
76   template=$2
77
78   # Disallow any trailing slash on specified destdir:
79   # it would subvert the post-mktemp "case"-based destdir test.
80   case $destdir in
81   /) ;;
82   */) die "invalid destination dir: remove trailing slash(es)";;
83   esac
84
85   case $template in
86   *XXXX) ;;
87   *) die "invalid template: $template (must have a suffix of at least 4 X's)";;
88   esac
89
90   fail=0
91
92   # First, try to use mktemp.
93   d=`env -u TMPDIR mktemp -d -t -p "$destdir" "$template" 2>/dev/null` \
94     || fail=1
95
96   # The resulting name must be in the specified directory.
97   case $d in "$destdir"*);; *) fail=1;; esac
98
99   # It must have created the directory.
100   test -d "$d" || fail=1
101
102   # It must have 0700 permissions.  Handle sticky "S" bits.
103   perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
104   case $perms in drwx------*) ;; *) fail=1;; esac
105
106   test $fail = 0 && {
107     echo "$d"
108     return
109   }
110
111   # If we reach this point, we'll have to create a directory manually.
112
113   # Get a copy of the template without its suffix of X's.
114   base_template=`echo "$template"|sed 's/XX*$//'`
115
116   # Calculate how many X's we've just removed.
117   template_length=`echo "$template" | wc -c`
118   nx=`echo "$base_template" | wc -c`
119   nx=`expr $template_length - $nx`
120
121   err=
122   i=1
123   while :; do
124     X=`rand_bytes $nx`
125     candidate_dir="$destdir/$base_template$X"
126     err=`mkdir -m 0700 "$candidate_dir" 2>&1` \
127       && { echo "$candidate_dir"; return; }
128     test $MAX_TRIES -le $i && break;
129     i=`expr $i + 1`
130   done
131   die "$err"
132 }
133
134 mktempd "$@"