This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: Redirecting $TEMP or a certain directory to /tmp
- From: Lee Rothstein <lee at veritech dot com>
- To: cygwin at cygwin dot com
- Date: Tue, 12 Jan 2010 06:17:01 -0500
- Subject: Re: Redirecting $TEMP or a certain directory to /tmp
- References: <hih84d$2eo$1@ger.gmane.org> <hihadl$70l$1@ger.gmane.org>
- Reply-to: lee at veritech dot com
Thorsten Kampe wrote:
> Thomas Wiedmann (Tue, 12 Jan 2010 08:24:57 +0100) wrote
>> how can in Cygwin the directory, which is in Windows configured as
$TEMP, or
>> a certain other directory, be redirected to /tmp?
> man mount
My approach is different, I solve the problem on the Windows side for all
apps, Cygwin, or Windows.
My system is Visa 64b; others use a slightly different "menu"
sequence, but:
Control Panel -> System -> Advanced system settings ->
Environment Variables -> System Variables ->
TEMP == %TMPDIR%
and ... ->
TMPDIR == c:\_0\tmp
and (because some pseudo *nix apps for Windows use $TMP, instead of $TMPDIR:
... ->
TMP == %TMPDIR%
(since Cygwin root on my system is at 'c:\_0')
My approach means that both Windows and Cygwin apps use the same
directory for temporary files. This means I only have to "clean up"
or search in one temporary directory.
I also use the attached script, 'tmpdir', when I'm using the temporary
directory with unknown *NIX configs.
It has it's own help (-h|--help).
As always, YMMV.
#!/bin/bash
# tmpdir: resilient TeMPorary Directory Inquisition Reporter
# 'tmpdir' "finds" a master temporary directory (MTD) -- the directory in
# which temporary files and directories of an appication's session are
# stored. With certain options, it will create the directory.
# By Lee Rothstein, 2009-02-07 11:56
ProgName=tmpdir
Version="0.4.0"
UpDate="2009-05-10"
HelpEm () {
less << !!!EOF!!!
NAME
tmpdir - *resilient* TeMPorary Directory Intantiator/Reporter
DESCRIPTION
'tmpdir' "finds" a master temporary directory (MTD) -- the
directory in which temporary files and directories of an
application's session are stored. With certain options it will
create (recursively, i.e., 'mkdir - p') the directory.
In most GNU, UNIX, BSD, Linux, Cygwin and Posix systems, the
MTD is either /tmp, or the directory referenced by $TMPDIR. In
Cygwin, a likely additional possibility is the directory
referenced by $TEMP.
Normally, 'tmpdir' will be used by another script. Practical
uses for this utility include:
* If you're writing scripts for unknown users and are unsure of
their environment, 'tmpdir' might be one element of a
"foolproof" approach. Alas, however, fools are such clever
creatures; witness, George W. Bush (or, at least, George W.
Bush with Karl Rove's help).
* Creating a more private MTD for a specific application or
session.
SYNOPSIS
tmpdir -h
tmpdir [ [-c] [-f TEMPDIRECTORY] [-l TEMPDIRECTORY] ]
OPTIONS
Each option is mutually exclusive of the others. (That is,
only one can be used per invocation of 'tmpdir'.) Every option
besides '-h' requires TEMPDIRECTORY. Any option must be
specified before a TEMPDIRECTORY is provided.
-h | --help -- Help -- Provide this help. Take no other
actions. Exit code = '99'. 'tmpdir' does not use nor
check for any other arguments, when '-h', or '--help' is
specified.
Note: no other "long option names are supported."
-c -- Create a specified temporary directory. Abort if
directory already exists.
-f -- Force -- Force the use of the specified directory
whether or not it pre-exists. (This option will not
delete a file that prevents the creation of a directory
by the same name. Ergo, even "The Force" is limited in
its power ;-) )
-l -- Last resort -- Go through the normal sequence of
temporary directory investigation. If none is found,
try TEMPDIRECTORY, if it doesn't exist create it
(recursively, i.e., 'mkdir -p').
With the exception of help, long option syntax is not
supported!
USAGE
Rules:
* Almost any name acceptable to the file system is acceptable
as TEMPDIRECTORY, except one beginning with a dash. This is
done for four reasons:
> It makes option sanity checking easier.
> The user is very likely making a mistake about options
usage.
> It's very bad form. MTDs beginning with '-' would likely
cause problems later when 'tmpdir' was used in a script.
> If you don't like it, you can always change it, it's just
a script.
* Success is finding or creating an MTD -- return value == 0.
* Whenever it succeeds, and returns '0', it 'stdouts' the name
of that directory.
* Failure for any operational reason returns '1'. 'stdout' is
null. Failure means there is not a temporary directory, of
any kind, to be found.
The help option exits with a "return code" of '99'. Errors
in command line specification also return 90 series numbers.
Help is directed to 'stdout' piped through 'less'.
* Error messages are limited in size and always directed to
'stderr'.
* Directories are only created if one of the options is used.
* '-c' will create a directory (recursively, i.e., 'mkdir -p')
if one doesn't exist, but will fail if the specified
directory already exists.
* '-f' will use the pre-existing specified directory and
create (recursively, i.e., 'mkdir -p') and use it
otherwise.
* '-l' will cause 'tmpdir' to go through all the remaining
steps, using TEMPDIRECTORY only as a penultimate
alternative; creating it (recursively, i.e., 'mkdir -p') and
using it, only if it doesn't already exist.
* Barring an MTD command line specification, 'tmpdir' will
search for valid directories, in order, in the following
"name space", until it achieve a success, or exhausts all
alternatives:
> '\$TMPDIR', if defined, and exists.
> '/tmp'
> '\$TEMP', if defined and it exists.
* If step last step (just above) fails, and '-l' has been
specified, then and only then will TEMPDIRECTORY be
tried/created (recursively, i.e., 'mkdir -p').
* Any path name, of the temporary directory, is sent to
'stdout' in Posix format.
Typically, 'tmpdir' will be used in an execute in place command
where the output of 'tmpdir will assigned to an environment
variable, or files will be stored.
EXAMPLES
--
# 1
# Here, the user can select an alternative directory for the MTD
# by removing the '#' from the following 2nd 'TmpDirArgs=' line
...
TmpdirArgs=""
#TmpdirArgs="-f Tmp.dir"
...
TmpPath=\$(tmpdir \$TmpdirArgs)
...
any_command> \$TmpPath/\$0-\$\$
...
--
# 2
# Here if all of \$TMPDIR, '/tmp', and \$TEMP , don't exist 'tmpdir'
# will create an MTD, '/tmp'
...
TmpPath="\$(tmpdir -l /tmp)"
...
--
# 3
# Even if '\$TMPDIR' is defined and exists, '/tmp' will be used
# whether or not it existed prior to the 'tmpdir'
...
TmpPath="\$(tmpdir -f /tmp)"
if [[ "\$?" -ne 0 || "\$TmpPath" == "" ]] ; them
echo "\$(basename \$0): Execution aborted."
echo " Temporary directory not found"
exit 3
fi
...
--
# 4
# The user has decided that s/he wishes to save the results in
# a subdirectory of the current directory, named '_Saves'. The
# '-c' option plus the if test guarantee, that past saved data
# will not be overwritten.
...
# date +%Y-%m-%d+%H-%M-%S
# e.g., '2010-12-20+16-52' for 20 Dec 2010, 4:52 PM
date1=\$(date +%Y-%m-%d+%H-%M-%S)
TmpPath="\$(tmpdir -c _Saves/\$date1)"
if [[ "\$?" -ne 0 || "\$TmpPath" == "" ]] ; them
echo "\$(basename \$0): Execution aborted."
echo " A new temporary directory could not be created."
exit 3
fi
...
--
VERSION
${Version}; ${UpDate}.
BUGS AND LIMITATIONS
Currently there is no way to set the permissions on a created
MST, inside of 'tmpdir'.
!!!EOF!!!
exit 99
}
# GraceExit: GRACEfully stdout the temporary directory path (in Posix format)
# & 'EXIT 0'
GraceExit () {
echo "$(cygpath -u $1)"
exit 0
}
# WarnAbort: Issue WARNing ($2) to stderr
# "Issue" null temporary directory path to stdout
# ABort & exit with code $1
WarnAbort () {
{
echo "${ProgName}: ** error **:"
echo "$2"|fmt -50|xargs --max-lines=1 echo " "
echo ""
echo " For help, type \"${ProgName} -h\""
exit $1
} 1>&2
}
if [[ "$1" == "-h" || "$1" == "--help" ]] ; then HelpEm ; fi
case $# in
0) Option=""
TmpPath=""
;;
1) Option=""
if [[ "$1" =~ -[-a-zA-Z0-9] ]] ; then
WarnAbort 96 "Directory ('$1') starts with '-', or option misused."
fi
TmpPath="$1"
;;
2) Option="$1"
TmpPath="$2"
case "$Option" in
"-c"|"-f"|"-l")
;;
*) WarnAbort 97 "Unrecognized option -- '$Option'!"
;;
esac
;;
*) WarnAbort 98 "Can't have more than 2 arguments"
;;
esac
# Has an MTD been specified, but we're not using the "Last Resort" option?
if [[ -n "$TmpPath" && "$Option" != "-l" ]] ; then
# Does the specified MTD exist?
if [[ -e "$TmpPath" ]] ; then
# Is the specified MTD really a directory?
if [[ -d "$TmpPath" ]] ; then
# No option, or the Force option?
if [[ -z "$Option" || "$Option" == "-f" ]] ; then
GraceExit "$TmpPath"
# Is "Create" option (which only uses the MTD, if it can create it) operative?
elif [[ "$Option" == "-c" ]] ; then
WarnAbort 1 "Specified dir exists, but must create; can't do with '-c' option."
fi
# Specified MTD exists and is not a directory
else
WarnAbort 1 "$TmpPath exists but is not a directory."
fi
# Specified MTD does not exist. Can we create it?
elif [[ "$Option" == "-c" || "$Option" == "-f" ]] ; then
mkdir -p $TmpPath
MkDirCode="$?"
# Did we successfully create the MTD?
if [[ "$MkDirCode" == 0 ]] ; then
GraceExit "$TmpPath"
# Attempt to create the MTD failed.
else
WarnAbort 1 "Couldn't 'mkdir -p $TmpPath' . 'mkdir' exit code was '$MkdirCode' ."
fi
fi
# For the situation of a path being specified and the possibility of
# creating an MTD (except for the Last resort option), all other
# alternatives have either succeeded or failed and in either case
# exited.
WarnAbort 1 "Specified path -- $TmpPath -- doesn't exist!"
# No options or Last resort (-l) option. Ergo, must try to find existing MTD.
# Does the variable "$TMPDIR" exist; does it reference a directory?
elif [[ -n "$TMPDIR" && -d "$TMPDIR" ]] ; then
GraceExit "$TMPDIR"
# How about good ol' '/tmp'?
elif [[ -d /tmp ]] ; then
GraceExit "/tmp"
# Windows $TEMP directory?
elif [[ -n "$TEMP" && -d "$(cygpath -u $TEMP)" ]] ; then
GraceExit "$TEMP"
# We're down to the Last resort?
elif [[ "$Option" == "-l" ]] ; then
# Does the specified MTD already exist?
if [[ -e "$TmpPath" ]] ; then
# If it's a directory use it.
if [[ -d "$TmpPath" ]] ; then
GraceExit "$TmpPath"
# No luck, here, either
else
WarnAbort 1 "Last resort temp path--'$TmpPath'--exists & is not dir!"
fi
# Try creating the directory
else
mkdir -p $TmpPath
MkDirCode="$?"
if [[ "$MkDirCode" == 0 ]] ; then
GraceExit "$TmpPath"
else
WarnAbort 1 "Couldn't 'mkdir -p $TmpPath'. 'mkdir' exit code was $MkdirCode."
fi
fi
else
WarnAbort 1 "No viable temporary directory could be found!"
# At this point, either the author of this script, or the invoker needs a brain implant. ;-)
fi
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple