Path.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Path;
  14. use strict;
  15. use warnings;
  16. use Exporter;
  17. use Cwd qw(realpath);
  18. our @ISA = qw(Exporter);
  19. our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
  20. guess_pkg_root_dir check_files_are_the_same);
  21. =head1 NAME
  22. Dpkg::Path - some common path handling functions
  23. =head1 DESCRIPTION
  24. It provides some functions to handle various path.
  25. =head1 METHODS
  26. =over 8
  27. =item get_pkg_root_dir($file)
  28. This function will scan upwards the hierarchy of directory to find out
  29. the directory which contains the "DEBIAN" sub-directory and it will return
  30. its path. This directory is the root directory of a package being built.
  31. If no DEBIAN subdirectory is found, it will return undef.
  32. =cut
  33. sub get_pkg_root_dir($) {
  34. my $file = shift;
  35. $file =~ s{/+$}{};
  36. $file =~ s{/+[^/]+$}{} if not -d $file;
  37. while ($file) {
  38. return $file if -d "$file/DEBIAN";
  39. last if $file !~ m{/};
  40. $file =~ s{/+[^/]+$}{};
  41. }
  42. return undef;
  43. }
  44. =item relative_to_pkg_root($file)
  45. Returns the filename relative to get_pkg_root_dir($file).
  46. =cut
  47. sub relative_to_pkg_root($) {
  48. my $file = shift;
  49. my $pkg_root = get_pkg_root_dir($file);
  50. if (defined $pkg_root) {
  51. $pkg_root .= "/";
  52. return $file if ($file =~ s/^\Q$pkg_root\E//);
  53. }
  54. return undef;
  55. }
  56. =item guess_pkg_root_dir($file)
  57. This function tries to guess the root directory of the package build tree.
  58. It will first use get_pkg_root_dir(), but it will fallback to a more
  59. imprecise check: namely it will use the parent directory that is a
  60. sub-directory of the debian directory.
  61. It can still return undef if a file outside of the debian sub-directory is
  62. provided.
  63. =cut
  64. sub guess_pkg_root_dir($) {
  65. my $file = shift;
  66. my $root = get_pkg_root_dir($file);
  67. return $root if defined $root;
  68. $file =~ s{/+$}{};
  69. $file =~ s{/+[^/]+$}{} if not -d $file;
  70. my $parent = $file;
  71. while ($file) {
  72. $parent =~ s{/+[^/]+$}{};
  73. last if not -d $parent;
  74. return $file if check_files_are_the_same("debian", $parent);
  75. $file = $parent;
  76. last if $file !~ m{/};
  77. }
  78. return undef;
  79. }
  80. =item check_files_are_the_same($file1, $file2)
  81. This function verifies that both files are the same by checking that the device
  82. numbers and the inode numbers returned by lstat() are the same.
  83. =cut
  84. sub check_files_are_the_same($$) {
  85. my ($file1, $file2) = @_;
  86. return 0 if ((! -e $file1) || (! -e $file2));
  87. my @stat1 = lstat($file1);
  88. my @stat2 = lstat($file2);
  89. my $result = ($stat1[0] == $stat2[0]) && ($stat1[1] == $stat2[1]);
  90. return $result;
  91. }
  92. =back
  93. =head1 AUTHOR
  94. Raphael Hertzog <hertzog@debian.org>.
  95. =cut
  96. 1;