1993-08-26 01:19:55 +00:00
# ifndef lint
1995-08-06 03:21:04 +00:00
static const char * rcsid = " $Id: pen.c,v 1.13 1995/04/26 07:43:35 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
/*
* 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
{
1994-10-04 16:07:50 +00:00
if ( ! pen ) {
char * cp ;
1995-02-16 12:43:00 +00:00
if ( ( cp = getenv ( " PKG_TMPDIR " ) ) ! = NULL )
1994-10-04 16:07:50 +00:00
sprintf ( Pen , " %s/instmp.XXXXXX " , cp ) ;
else
1995-02-15 03:48:13 +00:00
strcpy ( Pen , " /var/tmp/instmp.XXXXXX " ) ;
1995-08-06 03:21:04 +00:00
PlayPen = Pen ;
1994-10-04 16:07:50 +00:00
}
else
strcpy ( Pen , pen ) ;
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 ) ;
1994-10-14 05:56:15 +00:00
if ( Verbose ) {
if ( ! sz )
fprintf ( stderr , " Free temp space: %d bytes \n " , min_free ( Pen ) ) ;
else
fprintf ( stderr , " Projected package size: %d bytes, free temp space: %d bytes \n " , ( int ) sz , min_free ( Pen ) ) ;
}
1994-10-04 16:07:50 +00:00
if ( min_free ( Pen ) < sz ) {
rmdir ( Pen ) ;
1995-04-21 06:30:41 +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 \n try 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-04-22 13:58:44 +00:00
long
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 ;
}