Blame tests/CuTmpdir.pm

Packit 709fb3
package CuTmpdir;
Packit 709fb3
# create, then chdir into a temporary sub-directory
Packit 709fb3
Packit 709fb3
# Copyright (C) 2007-2015, 2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
# This program is free software: you can redistribute it and/or modify
Packit 709fb3
# it under the terms of the GNU General Public License as published by
Packit 709fb3
# the Free Software Foundation, either version 3 of the License, or
Packit 709fb3
# (at your option) any later version.
Packit 709fb3
Packit 709fb3
# This program is distributed in the hope that it will be useful,
Packit 709fb3
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
# GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
# You should have received a copy of the GNU General Public License
Packit 709fb3
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 709fb3
Packit 709fb3
use strict;
Packit 709fb3
use warnings;
Packit 709fb3
Packit 709fb3
use File::Temp;
Packit 709fb3
use File::Find;
Packit 709fb3
Packit 709fb3
our $ME = $0 || "";
Packit 709fb3
Packit 709fb3
my $dir;
Packit 709fb3
Packit 709fb3
sub skip_test($)
Packit 709fb3
{
Packit 709fb3
  warn "$ME: skipping test: unsafe working directory name: '$_[0]'\n";
Packit 709fb3
  exit 77;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
sub chmod_1
Packit 709fb3
{
Packit 709fb3
  my $name = $_;
Packit 709fb3
Packit 709fb3
  # Skip symlinks and non-directories.
Packit 709fb3
  -l $name || !-d _
Packit 709fb3
    and return;
Packit 709fb3
Packit 709fb3
  chmod 0700, $name;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
sub chmod_tree
Packit 709fb3
{
Packit 709fb3
  # When tempdir fails, it croaks, which leaves $dir undefined.
Packit 709fb3
  defined $dir
Packit 709fb3
    or return;
Packit 709fb3
Packit 709fb3
  # Perform the equivalent of find "$dir" -type d -print0|xargs -0 chmod -R 700.
Packit 709fb3
  my $options = {untaint => 1, wanted => \&chmod_1};
Packit 709fb3
  find ($options, $dir);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
sub import {
Packit 709fb3
  my $prefix = $_[1];
Packit 709fb3
Packit 709fb3
  $ME eq '-' && defined $prefix
Packit 709fb3
    and $ME = $prefix;
Packit 709fb3
Packit 709fb3
  if ($prefix !~ /^\//)
Packit 709fb3
    {
Packit 709fb3
      eval 'use Cwd';
Packit 709fb3
      my $cwd = $@ ? '.' : Cwd::getcwd();
Packit 709fb3
      $prefix = "$cwd/$prefix";
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  # Untaint for the upcoming mkdir.
Packit 709fb3
  $prefix =~ m!^([-+\@\w./]+)$!
Packit 709fb3
    or skip_test $prefix;
Packit 709fb3
  $prefix = $1;
Packit 709fb3
Packit 709fb3
  my $original_pid = $$;
Packit 709fb3
Packit 709fb3
  my $on_sig_remove_tmpdir = sub {
Packit 709fb3
    my ($sig) = @_;
Packit 709fb3
    if ($$ == $original_pid and defined $dir)
Packit 709fb3
      {
Packit 709fb3
        chmod_tree;
Packit 709fb3
        # Older versions of File::Temp lack this method.
Packit 709fb3
        exists &File::Temp::cleanup
Packit 709fb3
          and &File::Temp::cleanup;
Packit 709fb3
      }
Packit 709fb3
    $SIG{$sig} = 'DEFAULT';
Packit 709fb3
    kill $sig, $$;
Packit 709fb3
  };
Packit 709fb3
Packit 709fb3
  foreach my $sig (qw (INT TERM HUP))
Packit 709fb3
    {
Packit 709fb3
      $SIG{$sig} = $on_sig_remove_tmpdir;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  $dir = File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );
Packit 709fb3
  chdir $dir
Packit 709fb3
    or warn "$ME: failed to chdir to $dir: $!\n";
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
END {
Packit 709fb3
  # Move cwd out of the directory we're about to remove.
Packit 709fb3
  # This is required on some systems, and by some versions of File::Temp.
Packit 709fb3
  chdir '..'
Packit 709fb3
    or warn "$ME: failed to chdir to .. from $dir: $!\n";
Packit 709fb3
Packit 709fb3
  my $saved_errno = $?;
Packit 709fb3
  chmod_tree;
Packit 709fb3
  $? = $saved_errno;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
1;