tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Change to genassym to use mktemp(1) to create tempdir

"Jeremy C. Reed" <> writes:

> On Mon, 6 Jan 2014, J.T. Conklin wrote:
>> -genassym_temp=/tmp/genassym.$$
>> +genassym_temp=`mktemp -d -q ${TMPDIR-/tmp}/genassym.XXXXXX`
>> -if ! mkdir $genassym_temp; then
>> +if ! test -d $genassym_temp; then
>>      echo "${progname}: unable to create temporary directory" >&2
>>      exit 1
>>  fi
> This seems fine to me.
> But maybe don't use -q and let it fail at same time:
>  genassym_temp=`mktemp -d ${TMPDIR-/tmp}/genassym.XXXXXX` || {
>        echo "${progname}: unable to create temporary directory" >&2 ;
>        exit 1
>  }

The -q option only suppresses mktemp's diagnostics, it doesn't change 
the return value. Having both mktemp and genassym output a diagnostic
may be a bit messy/confusing.

That being said, I've checked other uses of mktemp in the NetBSD tree,
and the most common idiom is more like what you propose.  For example,
following the cue of, the code would be like:

        genassym_temp=`mktemp -d -q ${TMPDIR-/tmp}/genassym.XXXXXX` &&
        test -n "$genassym_temp" &&
        test -d "$genassym_temp"
    } || {
        mkdir $genassym_temp > /dev/null 2>&1
    } || {
        echo "${progname}: unable to create temporary directory" >&2 ;
        exit 1

This is probably much more conservative that we need, as we know that
mktemp(1) is available and that it exits with a non-zero return value
on failure.  Simplifying, it pretty much ends up like your suggestion.
This is also the approach used by the mktemp manpage example (albeit
checking $? instead of using ||), so I'll plan to rework and send a pr
along those lines in the next few days.


J.T. Conklin

Home | Main Index | Thread Index | Old Index