Functions.pm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package Dpkg::Source::Functions;
  2. use strict;
  3. use warnings;
  4. use Exporter;
  5. our @ISA = qw(Exporter);
  6. our @EXPORT_OK = qw(erasedir fixperms);
  7. use Dpkg::ErrorHandling qw(syserr subprocerr failure);
  8. use Dpkg::Gettext;
  9. use POSIX;
  10. sub erasedir {
  11. my ($dir) = @_;
  12. if (not lstat($dir)) {
  13. return if $! == ENOENT;
  14. syserr(_g("cannot stat directory %s (before removal)"), $dir);
  15. }
  16. system 'rm','-rf','--',$dir;
  17. subprocerr("rm -rf $dir") if $?;
  18. if (not stat($dir)) {
  19. return if $! == ENOENT;
  20. syserr(_g("unable to check for removal of dir `%s'"), $dir);
  21. }
  22. failure(_g("rm -rf failed to remove `%s'"), $dir);
  23. }
  24. sub fixperms {
  25. my ($dir) = @_;
  26. my ($mode, $modes_set, $i, $j);
  27. # Unfortunately tar insists on applying our umask _to the original
  28. # permissions_ rather than mostly-ignoring the original
  29. # permissions. We fix it up with chmod -R (which saves us some
  30. # work) but we have to construct a u+/- string which is a bit
  31. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  32. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  33. $mode = 0777 & ~umask;
  34. for ($i = 0; $i < 9; $i += 3) {
  35. $modes_set .= ',' if $i;
  36. $modes_set .= qw(u g o)[$i/3];
  37. for ($j = 0; $j < 3; $j++) {
  38. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  39. $modes_set .= qw(r w X)[$j];
  40. }
  41. }
  42. system('chmod', '-R', $modes_set, '--', $dir);
  43. subprocerr("chmod -R $modes_set $dir") if $?;
  44. }
  45. # vim: set et sw=4 ts=8
  46. 1;