diff -r -du trunk/PAR-Packer/ChangeLog /h/src/ThirdParty/main/par/PAR-Packer/ChangeLog --- trunk/PAR-Packer/ChangeLog 2008-03-10 10:27:51.941283000 -0700 +++ /h/src/ThirdParty/main/par/PAR-Packer/ChangeLog 2008-03-10 10:59:11.897557000 -0700 @@ -1,3 +1,15 @@ + - Don't remove the top level par- directory for PAR_CLEAN=1 because + doing so introduces a race condition with other applications trying to + create the directory on startup. (Scott Stanton) + - When creating the temporary directory for PAR_CLEAN=1, ensure that + the directory does not already exist to avoid picking up stale files + from a prior invocation of a different par application. (Scott Stanton) + - Changed so get_username_from_getpwuid works for uid=0. (Scott Stanton) + - Fixed crash in get_username_from_getpwuid when getpwuid fails. + (Scott Stanton) + - Eliminated the need to spawn a background cleanup process on Windows, + relying instead on the parent process's call to par_cleanup. + (Scott Stanton) [Changes for 0.978 - Feb 28, 2008] diff -r -du trunk/PAR-Packer/myldr/mktmpdir.c /h/src/ThirdParty/main/par/PAR-Packer/myldr/mktmpdir.c --- trunk/PAR-Packer/myldr/mktmpdir.c 2008-01-08 10:48:33.274371000 -0800 +++ /h/src/ThirdParty/main/par/PAR-Packer/myldr/mktmpdir.c 2008-03-10 09:55:45.000000000 -0700 @@ -11,6 +11,17 @@ #ifndef P_tmpdir #define P_tmpdir "/tmp" #endif +static int isWritableDir(const char* val) +{ +#ifndef PL_statbuf +struct stat PL_statbuf; +#endif + + return ( par_lstat(val, &PL_statbuf) == 0 && + ( S_ISDIR(PL_statbuf.st_mode) || + S_ISLNK(PL_statbuf.st_mode) ) && + access(val, W_OK) == 0 ); +} void par_setup_libpath( const char * stmpdir ) { @@ -46,7 +57,7 @@ char *c; const char *tmpdir = NULL; const char *key = NULL , *val = NULL; - const char *temp_dirs[4] = { "C:\\TEMP", P_tmpdir, ".", "" }; + const char *temp_dirs[4] = { "C:\\TEMP", P_tmpdir, ".", "", }; const char *temp_keys[6] = { "PAR_TMPDIR", "TMPDIR", "TEMPDIR", "TEMP", "TMP", "" }; const char *user_keys[3] = { "USER", "USERNAME", "" }; @@ -61,10 +72,6 @@ unsigned char buf[32768]; unsigned char sha_data[20]; -#ifndef PL_statbuf -struct stat PL_statbuf; -#endif - if ( (val = (char *)par_getenv(PAR_TEMP)) && strlen(val) ) { par_setup_libpath(val); return strdup(val); @@ -103,21 +110,30 @@ } } + /* Try temp environment variables */ for ( i = 0 ; tmpdir == NULL && strlen(key = temp_keys[i]) > 0 ; i++ ) { if ( (val = (char *)par_getenv(key)) && - par_lstat(val, &PL_statbuf) == 0 && - ( S_ISDIR(PL_statbuf.st_mode) || - S_ISLNK(PL_statbuf.st_mode) ) && - access(val, W_OK) == 0 ) { + isWritableDir(val) ) { tmpdir = strdup(val); } } +#ifdef WIN32 + /* Try the windows temp directory */ + if ( tmpdir == NULL && (val = (char*)par_getenv("WinDir")) ) { + char* buf = (char*)(malloc(strlen(val) + 6)); + sprintf(buf, "%s\\temp", val); + if (isWritableDir(buf)) { + tmpdir = buf; + } else { + free(buf); + } + } +#endif + + /* Try default locations */ for ( i = 0 ; tmpdir == NULL && strlen(val = temp_dirs[i]) > 0 ; i++ ) { - if ( par_lstat(val, &PL_statbuf) == 0 && - ( S_ISDIR(PL_statbuf.st_mode) || - S_ISLNK(PL_statbuf.st_mode) ) && - access(val, W_OK) == 0 ) { + if ( isWritableDir(val) ) { tmpdir = strdup(val); } } @@ -178,6 +194,9 @@ } } else { + int i = 0; + size_t len = strlen(stmpdir); + /* "$TEMP/par-$USER/temp-$PID" */ par_setenv("PAR_CLEAN", "1"); @@ -186,6 +205,21 @@ "%s%stemp-%u%s", stmpdir, dir_sep, getpid(), subdirbuf_suffix ); + + /* Ensure we pick an unused directory each time. If the directory + already exists when we try to create it, bump a counter and try + "$TEMP/par-$USER/temp-$PID-$i". This will guard against cases where + a prior invocation crashed leaving garbage in a temp directory that + might interfere. */ + + while (my_mkdir(stmpdir, 0755) == -1 && errno == EEXIST) { + stmpdir[len] = 0; + sprintf( + stmpdir, + "%s%stemp-%u-%u%s", + stmpdir, dir_sep, getpid(), ++i, subdirbuf_suffix + ); + } } /* set dynamic loading path */ @@ -198,7 +232,7 @@ #ifdef WIN32 -void par_rmtmpdir ( char *stmpdir, int recurse ) { +static void par_rmtmpdir ( char *stmpdir ) { struct _finddata_t cur_file; int subsub_len; char *subsubdir; @@ -217,27 +251,7 @@ hFile = _findfirst( subsubdir, &cur_file ); if ( hFile == -1 ) return; - if (!strstr(cur_file.name, "\\")) { - sprintf(subsubdir, "%s\\%s", stmpdir, cur_file.name); - } - else { - sprintf(subsubdir, "%s", cur_file.name); - } - - if (!(slashdot = strstr(subsubdir, "\\.")) || (strcmp(slashdot,"\\.") && strcmp(slashdot,"\\.."))) { - if ((cur_file.attrib & _A_SUBDIR) && recurse) { - par_rmtmpdir( subsubdir, 1 ); - } - /* if (!(cur_file.attrib & _A_SUBDIR)) fprintf(stderr, "unlinking %s\n", subsubdir); */ - else { - dll = GetModuleHandle(cur_file.name); - tries = 0; - while ( _unlink(subsubdir) && ( tries++ < 10 ) ) { - if ( dll ) FreeLibrary(dll); - }; - } - } - while ( _findnext( hFile, &cur_file ) == 0 ) { + do { if (!strstr(cur_file.name, "\\")) { sprintf(subsubdir, "%s\\%s", stmpdir, cur_file.name); } @@ -246,10 +260,9 @@ } if (!(slashdot = strstr(subsubdir, "\\.")) || (strcmp(slashdot,"\\.") && strcmp(slashdot,"\\.."))) { - if ((cur_file.attrib & _A_SUBDIR) && recurse) { - par_rmtmpdir( subsubdir, 1 ); + if ((cur_file.attrib & _A_SUBDIR)) { + par_rmtmpdir( subsubdir ); } - /* if (!(cur_file.attrib & _A_SUBDIR)) fprintf(stderr, "unlinking %s\n", subsubdir); */ else { dll = GetModuleHandle(cur_file.name); tries = 0; @@ -258,14 +271,14 @@ }; } } - } + } while ( _findnext( hFile, &cur_file ) == 0 ); _findclose(hFile); _rmdir(stmpdir); } #else -void par_rmtmpdir ( char *stmpdir, int recurse ) { +static void par_rmtmpdir ( char *stmpdir ) { DIR *partmp_dirp; Direntry_t *dp; char *subsubdir = NULL; @@ -273,19 +286,20 @@ struct stat stbuf; /* remove temporary PAR directory */ + if (!stmpdir || !*stmpdir) return; + partmp_dirp = opendir(stmpdir); if ( partmp_dirp == NULL ) return; - /* fprintf(stderr, "%s: removing private temporary subdirectory %s.\n", argv[0], stmpdir); */ while ( ( dp = readdir(partmp_dirp) ) != NULL ) { if ( strcmp (dp->d_name, ".") != 0 && strcmp (dp->d_name, "..") != 0 ) { subsub_len = strlen(stmpdir) + strlen(dp->d_name) + 2; subsubdir = malloc( subsub_len); sprintf(subsubdir, "%s/%s", stmpdir, dp->d_name); - if (stat(subsubdir, &stbuf) != -1 && S_ISDIR(stbuf.st_mode) && recurse) { - par_rmtmpdir(subsubdir, 1); + if (stat(subsubdir, &stbuf) != -1 && S_ISDIR(stbuf.st_mode)) { + par_rmtmpdir(subsubdir); } else { unlink(subsubdir); @@ -296,7 +310,7 @@ } closedir(partmp_dirp); - if (stmpdir) rmdir(stmpdir); + rmdir(stmpdir); } #endif @@ -305,8 +319,9 @@ char *basename = par_basename(dirname); if ( par_env_clean() && stmpdir != NULL && strlen(stmpdir)) { if ( strstr(basename, "par-") == basename ) { - par_rmtmpdir(stmpdir, 1); - par_rmtmpdir(dirname, 0); + par_rmtmpdir(stmpdir); + /* Don't try to remove dirname because this will introduce a race + with other applications that are trying to start. */ } } } diff -r -du trunk/PAR-Packer/myldr/static.c /h/src/ThirdParty/main/par/PAR-Packer/myldr/static.c --- trunk/PAR-Packer/myldr/static.c 2008-01-08 10:48:33.198369000 -0800 +++ /h/src/ThirdParty/main/par/PAR-Packer/myldr/static.c 2008-01-20 11:37:07.503649000 -0800 @@ -61,7 +61,7 @@ stmpdir = (char *)par_getenv("PAR_TEMP"); if ( stmpdir != NULL ) { i = my_mkdir(stmpdir, 0755); - if ( (i != 0) && (i != EEXIST) && (i != -1) ) { + if ( i == -1 && errno != EEXIST) { fprintf(stderr, "%s: creation of private temporary subdirectory %s failed - aborting with %i.\n", argv[0], stmpdir, errno); return 2; } diff -r -du trunk/PAR-Packer/myldr/usernamefrompwuid.c /h/src/ThirdParty/main/par/PAR-Packer/myldr/usernamefrompwuid.c --- trunk/PAR-Packer/myldr/usernamefrompwuid.c 2008-01-08 10:48:32.953368000 -0800 +++ /h/src/ThirdParty/main/par/PAR-Packer/myldr/usernamefrompwuid.c 2008-01-20 12:06:42.869330000 -0800 @@ -15,17 +15,13 @@ */ char *get_username_from_getpwuid () { -#ifdef I_PWD char *username = NULL; +#ifdef I_PWD struct passwd *userdata = NULL; - uid_t uid; - uid = getuid(); - if (uid) { - userdata = getpwuid(uid); + userdata = getpwuid(getuid()); + if (userdata) { username = userdata->pw_name; } -#else - char *username = NULL; #endif return(username); } diff -r -du trunk/PAR-Packer/script/par.pl /h/src/ThirdParty/main/par/PAR-Packer/script/par.pl --- trunk/PAR-Packer/script/par.pl 2008-03-10 10:27:48.821984000 -0700 +++ /h/src/ThirdParty/main/par/PAR-Packer/script/par.pl 2008-04-17 15:14:58.492004000 -0700 @@ -166,13 +166,20 @@ outs(qq{Removing files in "$par_temp"}); File::Find::finddepth(sub { ( -d ) ? rmdir : unlink }, $par_temp); rmdir $par_temp; - rmdir $topdir; - if (-d $par_temp) { + outs("Unable to remove $par_temp: $!"); + } else { + outs("Removed $par_temp"); + } + # Don't remove topdir because this causes a race with other apps + # that are trying to start. + if (-d $par_temp && $^O ne 'MSWin32') { # Something went wrong unlinking the temporary directory. This # typically happens on platforms that disallow unlinking shared # libraries and executables that are in use. Unlink with a background # shell command so the files are no longer in use by this process. + # Don't do anything on Windows because our parent process will + # take care of cleaning things up. my $tmp = new File::Temp( TEMPLATE => 'tmpXXXXX', @@ -181,11 +188,11 @@ UNLINK => 0, ); - # Because the par_temp directory is going to be deleted in a - # background process, the parent process id may be reused before the - # background process completes. To ensure that the temporary - # directory does not get reused while it is being deleted, try to - # rename it to a name that is related to the temporary script file. + # Because the par_temp directory is going to be deleted in a background + # process, the parent process id may be reused before the background + # process completes. To ensure that the temporary directory does not + # get reused while it is being deleted, try to rename it to a name that + # is related to the temporary script file. my $tmpname = (File::Spec->splitpath($tmp->filename))[2]; # filename $tmpname =~ s/\.[^.]*$/.dir/; @@ -194,37 +201,26 @@ if (rename $par_temp, $newDir) { outs("Renamed $par_temp to $newDir"); $par_temp = $newDir; - } - - if ($^O =~ m/win32/i) { - print $tmp " -:loop -rmdir /q /s \"$par_temp\" -if exist \"$par_temp\" goto loop -rmdir \"$topdir\" -rm \"" . $tmp->filename . "\" -"; - close $tmp; - my $proc; - Win32::Process::Create( - $proc, $ENV{COMSPEC}, - "$ENV{COMSPEC} /c \"" . $tmp->filename . "\" >nul 2>nul ", - 1, NORMAL_PRIORITY_CLASS, "." - ); - } else { print $tmp "#!/bin/sh -while [ -d '$par_temp' ]; do +x=1; while [ \$x -lt 10 ]; do rm -rf '$par_temp' + if [ \! -d '$par_temp' ]; then + break + fi + sleep 1 + x=`expr \$x + 1` done -rmdir '$topdir' rm '" . $tmp->filename . "' "; chmod 0700,$tmp->filename; my $cmd = $tmp->filename . ' >/dev/null 2>&1 &'; close $tmp; system($cmd); + outs(qq(Spawned background process to perform cleanup: ) + . $tmp->filename); + } else { + outs("Rename of $par_temp failed: $!"); } - outs(qq(Spawned background process to perform cleanup)); } } } @@ -289,38 +285,40 @@ outs(qq(Unpacking file "$fullname"...)); my $crc = ( $fullname =~ s|^([a-f\d]{8})/|| ) ? $1 : undef; my ($basename, $ext) = ($buf =~ m|(?:.*/)?(.*)(\..*)|); + my $isLibrary = (defined($ext) and $ext !~ /\.(?:pm|pl|ix|al)$/i); + my $isShlib = ($fullname =~ m|^/?shlib/|); read _FH, $buf, 4; - read _FH, $buf, unpack("N", $buf); + my $size = unpack("N", $buf); - if (defined($ext) and $ext !~ /\.(?:pm|pl|ix|al)$/i) { + if ($isLibrary || $isShlib) { my ($out, $filename) = _tempfile($ext, $crc); + if ($isLibrary) { + $PAR::Heavy::FullCache{$fullname} = $filename; + $PAR::Heavy::FullCache{$filename} = $fullname; + } if ($out) { - binmode($out); + read _FH, $buf, $size; + binmode($out); print $out $buf; close $out; chmod 0755, $filename; + outs(qq(Unpacked "$fullname" to "$filename")); + } else { + seek _FH, $size, 1; + outs(qq(Skipped "$fullname" as it already exists in "$filename")); } - $PAR::Heavy::FullCache{$fullname} = $filename; - $PAR::Heavy::FullCache{$filename} = $fullname; - } - elsif ( $fullname =~ m|^/?shlib/| and defined $ENV{PAR_TEMP} ) { - # should be moved to _tempfile() - my $filename = "$ENV{PAR_TEMP}/$basename$ext"; - outs("SHLIB: $filename\n"); - open my $out, '>', $filename or die $!; - binmode($out); - print $out $buf; - close $out; - } - else { + } else { + read _FH, $buf, $size; $require_list{$fullname} = $PAR::Heavy::ModuleCache{$fullname} = { buf => $buf, crc => $crc, name => $fullname, }; + outs(qq(Unpacked "$fullname" to memory)) } + read _FH, $buf, 4; } # }}} @@ -339,10 +337,22 @@ delete $require_list{$key} if defined($key); } or return; + my $havePerlIOScalar = defined($INC{"PerlIO/scalar.pm"}); $INC{$module} = "/loader/$filename/$module"; + if ($^O eq "MSWin32" + && $havePerlIOScalar + && defined($filename->{buf})) + { + outs("Loading module '$module' from memory"); + open my $fh, '<', \$filename->{buf}; + binmode($fh); + return $fh; + } + if ($ENV{PAR_CLEAN} and defined(&IO::File::new)) { - my $fh = IO::File->new_tmpfile or die $!; + my ($fh, $name) = IO::File->new_tmpfile or die $!; + outs("Loading module '$module' from a temp file"); binmode($fh); print $fh $filename->{buf}; seek($fh, 0, 0); @@ -355,6 +365,7 @@ print $out $filename->{buf}; close $out; } + outs("Loading module '$module' from '$name'"); open my $fh, '<', $name or die $!; binmode($fh); return $fh; @@ -371,6 +382,8 @@ require Carp::Heavy; require Exporter::Heavy; PAR::Heavy::_init_dynaloader(); + require PerlIO; + require PerlIO::scalar; # now let's try getting helper modules from within require IO::File; @@ -703,8 +716,9 @@ if (-f $dest_name && -s _ == $member->uncompressedSize()) { outs(qq(Skipping "$member_name" since it already exists at "$dest_name")); } else { - $member->extractToFileNamed($dest_name); outs(qq(Extracting "$member_name" to "$dest_name")); + $member->extractToFileNamed($dest_name); + chmod(0555, $dest_name) if $^O eq "hpux"; } } } @@ -759,9 +773,10 @@ require PAR::Heavy; require PAR::Dist; require PAR::Filter::PodStrip; + require PerlIO; + require PerlIO::scalar; eval { require Cwd }; eval { require Win32 }; - eval { require Win32::Process }; eval { require Scalar::Util }; eval { require Archive::Unzip::Burst }; } --- trunk/lib/PAR.pm 2008-01-08 10:53:18.693713000 -0800 +++ /h/src/ThirdParty/main/par/PAR/lib/PAR.pm 2007-11-27 19:43:38.332834000 -0800 @@ -367,7 +367,7 @@ # XXX - handle META.yml here! push @PAR_INC, unpar($progname, undef, undef, 1); - _extract_inc($progname) unless $ENV{PAR_CLEAN}; +# _extract_inc($progname) unless $ENV{PAR_CLEAN}; my $zip = $LibCache{$progname}; my $member = _first_member( $zip, @@ -472,7 +472,7 @@ PAR::Heavy::_init_dynaloader(); # XXX - handle META.yml here! - _extract_inc($opt->{file}) unless $ENV{PAR_CLEAN}; +# _extract_inc($opt->{file}) unless $ENV{PAR_CLEAN}; my $zip = $LibCache{$opt->{file}}; my $member = _first_member( $zip, @@ -865,14 +865,9 @@ return $member if $member_only; - my ($fh, $is_new); - ($fh, $is_new, $LastTempFile) = _tempfile($member->crc32String . ".pm"); - die "Bad Things Happened..." unless $fh; - - if ($is_new) { - $member->extractToFileHandle($fh); - seek ($fh, 0, 0); - } + my ($fh, $contents); + $contents = $member->contents(); + open($fh, "<", \$contents) || die "Bad Things Happened..."; return $fh; }