1993-08-26 01:19:55 +00:00
# ifndef lint
1995-08-26 18:36:27 +00:00
static const char * rcsid = " $Id: pen.c,v 1.16 1995/08/26 10:15:18 jkh Exp $ " ;
1993-08-26 01:19:55 +00:00
# endif
/*
* FreeBSD install - a package for the installation and maintainance
* of non - core utilities .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
*
* Jordan K . Hubbard
* 18 July 1993
*
* Routines for managing the " play pen " .
*
*/
# include "lib.h"
1995-04-26 07:43:35 +00:00
# include <sys/signal.h>
1994-10-04 16:07:50 +00:00
# include <sys/param.h>
# include <sys/mount.h>
1993-08-26 01:19:55 +00:00
/* For keeping track of where we are */
static char Cwd [ FILENAME_MAX ] ;
static char Pen [ FILENAME_MAX ] ;
1995-08-06 03:21:04 +00:00
extern char * PlayPen ;
1993-08-26 01:19:55 +00:00
1995-08-17 00:36:06 +00:00
/* Find a good place to play. */
static char *
find_play_pen ( size_t sz )
{
char * cp ;
struct stat sb ;
if ( ( cp = getenv ( " PKG_TMPDIR " ) ) ! = NULL & & stat ( cp , & sb ) ! = FAIL & & ( min_free ( cp ) > = sz ) )
sprintf ( Pen , " %s/instmp.XXXXXX " , cp ) ;
else if ( ( cp = getenv ( " TMPDIR " ) ) ! = NULL & & stat ( cp , & sb ) ! = FAIL & & ( min_free ( cp ) > = sz ) )
sprintf ( Pen , " %s/instmp.XXXXXX " , cp ) ;
else if ( stat ( " /var/tmp " , & sb ) ! = FAIL & & min_free ( " /var/tmp " ) > = sz )
strcpy ( Pen , " /var/tmp/instmp.XXXXXX " ) ;
else if ( stat ( " /tmp " , & sb ) ! = FAIL & & min_free ( " /tmp " ) > = sz )
strcpy ( Pen , " /tmp/instmp.XXXXXX " ) ;
1995-08-26 18:36:27 +00:00
else if ( ( stat ( " /usr/tmp " , & sb ) = = SUCCESS | mkdir ( " /usr/tmp " , 01777 ) = = SUCCESS ) & & min_free ( " /usr/tmp " ) > = sz )
strcpy ( Pen , " /usr/tmp/instmp.XXXXXX " ) ;
1995-08-17 00:36:06 +00:00
else barf ( " Can't find enough temporary space to extract the files, please set \n your PKG_TMPDIR environment variable to a location with at least %d bytes \n free. " , sz ) ;
return Pen ;
}
1993-08-26 01:19:55 +00:00
/*
* Make a temporary directory to play in and chdir ( ) to it , returning
* pathname of previous working directory .
*/
char *
1994-10-04 16:07:50 +00:00
make_playpen ( char * pen , size_t sz )
1993-08-26 01:19:55 +00:00
{
1995-08-17 00:36:06 +00:00
if ( ! pen )
PlayPen = find_play_pen ( sz ) ;
else {
strcpy ( Pen , pen ) ;
1995-08-06 03:21:04 +00:00
PlayPen = Pen ;
1994-10-04 16:07:50 +00:00
}
1993-08-26 01:19:55 +00:00
if ( ! getcwd ( Cwd , FILENAME_MAX ) )
upchuck ( " getcwd " ) ;
if ( ! mktemp ( Pen ) )
barf ( " Can't mktemp '%s'. " , Pen ) ;
if ( mkdir ( Pen , 0755 ) = = FAIL )
barf ( " Can't mkdir '%s'. " , Pen ) ;
1995-08-17 00:36:06 +00:00
if ( Verbose )
{
1994-10-14 05:56:15 +00:00
if ( ! sz )
1995-08-17 00:36:06 +00:00
fprintf ( stderr , " Free temp space: %d bytes in %s \n " , min_free ( Pen ) , Pen ) ;
1994-10-14 05:56:15 +00:00
else
1995-08-17 00:36:06 +00:00
fprintf ( stderr , " Projected package size: %d bytes,
free temp space : % d bytes in % s \ n " , (int)sz, min_free(Pen), Pen);
1994-10-14 05:56:15 +00:00
}
1994-10-04 16:07:50 +00:00
if ( min_free ( Pen ) < sz ) {
rmdir ( Pen ) ;
1995-08-17 00:36:06 +00:00
barf ( " Not enough free space to create `%s'. \n Please set your PKG_TMPDIR
environment variable to a location with more space and \ ntry the command
again . " , Pen);
1995-08-06 03:21:04 +00:00
PlayPen = NULL ;
1994-10-04 16:07:50 +00:00
}
1993-08-26 01:19:55 +00:00
if ( chdir ( Pen ) = = FAIL )
barf ( " Can't chdir to '%s'. " , Pen ) ;
return Cwd ;
}
/* Convenience routine for getting out of playpen */
void
leave_playpen ( void )
{
1995-04-26 07:43:35 +00:00
void ( * oldsig ) ( int ) ;
/* Don't interrupt while we're cleaning up */
oldsig = signal ( SIGINT , SIG_IGN ) ;
1993-08-26 01:19:55 +00:00
if ( Cwd [ 0 ] ) {
if ( chdir ( Cwd ) = = FAIL )
barf ( " Can't chdir back to '%s'. " , Cwd ) ;
if ( vsystem ( " rm -rf %s " , Pen ) )
fprintf ( stderr , " Couldn't remove temporary dir '%s' \n " , Pen ) ;
Cwd [ 0 ] = ' \0 ' ;
}
1995-04-26 07:43:35 +00:00
signal ( SIGINT , oldsig ) ;
1993-08-26 01:19:55 +00:00
}
1993-09-05 04:54:24 +00:00
/* Accessor function for telling us where the pen is */
char *
where_playpen ( void )
{
if ( Cwd [ 0 ] )
return Pen ;
else
return NULL ;
}
1994-10-04 16:07:50 +00:00
1995-08-26 10:15:18 +00:00
size_t
1995-04-22 13:58:44 +00:00
min_free ( char * tmpdir )
1994-10-04 16:07:50 +00:00
{
struct statfs buf ;
1995-04-22 13:58:44 +00:00
if ( ! tmpdir )
tmpdir = Pen ;
1994-10-04 16:07:50 +00:00
if ( statfs ( tmpdir , & buf ) ! = 0 ) {
perror ( " Error in statfs " ) ;
return - 1 ;
}
return buf . f_bavail * buf . f_bsize ;
}