CuTmpdir.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package CuTmpdir;
  2. # create, then chdir into a temporary sub-directory
  3. # Copyright (C) 2007-2018 Free Software Foundation, Inc.
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. use strict;
  15. use warnings;
  16. use File::Temp;
  17. use File::Find;
  18. our $ME = $0 || "<???>";
  19. my $dir;
  20. sub skip_test($)
  21. {
  22. warn "$ME: skipping test: unsafe working directory name: '$_[0]'\n";
  23. exit 77;
  24. }
  25. sub chmod_1
  26. {
  27. my $name = $_;
  28. # Skip symlinks and non-directories.
  29. -l $name || !-d _
  30. and return;
  31. chmod 0700, $name;
  32. }
  33. sub chmod_tree
  34. {
  35. # When tempdir fails, it croaks, which leaves $dir undefined.
  36. defined $dir
  37. or return;
  38. # Perform the equivalent of find "$dir" -type d -print0|xargs -0 chmod -R 700.
  39. my $options = {untaint => 1, wanted => \&chmod_1};
  40. find ($options, $dir);
  41. }
  42. sub import {
  43. my $prefix = $_[1];
  44. $ME eq '-' && defined $prefix
  45. and $ME = $prefix;
  46. if ($prefix !~ /^\//)
  47. {
  48. eval 'use Cwd';
  49. my $cwd = $@ ? '.' : Cwd::getcwd();
  50. $prefix = "$cwd/$prefix";
  51. }
  52. # Untaint for the upcoming mkdir.
  53. $prefix =~ m!^([-+\@\w./]+)$!
  54. or skip_test $prefix;
  55. $prefix = $1;
  56. my $original_pid = $$;
  57. my $on_sig_remove_tmpdir = sub {
  58. my ($sig) = @_;
  59. if ($$ == $original_pid and defined $dir)
  60. {
  61. chmod_tree;
  62. # Older versions of File::Temp lack this method.
  63. exists &File::Temp::cleanup
  64. and &File::Temp::cleanup;
  65. }
  66. $SIG{$sig} = 'DEFAULT';
  67. kill $sig, $$;
  68. };
  69. foreach my $sig (qw (INT TERM HUP))
  70. {
  71. $SIG{$sig} = $on_sig_remove_tmpdir;
  72. }
  73. $dir = File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );
  74. chdir $dir
  75. or warn "$ME: failed to chdir to $dir: $!\n";
  76. }
  77. END {
  78. # Move cwd out of the directory we're about to remove.
  79. # This is required on some systems, and by some versions of File::Temp.
  80. chdir '..'
  81. or warn "$ME: failed to chdir to .. from $dir: $!\n";
  82. my $saved_errno = $?;
  83. chmod_tree;
  84. $? = $saved_errno;
  85. }
  86. 1;