Path.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  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. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Path;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use base qw(Exporter);
  20. use File::Spec;
  21. use Cwd qw(realpath);
  22. our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
  23. guess_pkg_root_dir check_files_are_the_same
  24. resolve_symlink canonpath);
  25. =head1 NAME
  26. Dpkg::Path - some common path handling functions
  27. =head1 DESCRIPTION
  28. It provides some functions to handle various path.
  29. =head1 METHODS
  30. =over 8
  31. =item get_pkg_root_dir($file)
  32. This function will scan upwards the hierarchy of directory to find out
  33. the directory which contains the "DEBIAN" sub-directory and it will return
  34. its path. This directory is the root directory of a package being built.
  35. If no DEBIAN subdirectory is found, it will return undef.
  36. =cut
  37. sub get_pkg_root_dir($) {
  38. my $file = shift;
  39. $file =~ s{/+$}{};
  40. $file =~ s{/+[^/]+$}{} if not -d $file;
  41. while ($file) {
  42. return $file if -d "$file/DEBIAN";
  43. last if $file !~ m{/};
  44. $file =~ s{/+[^/]+$}{};
  45. }
  46. return undef;
  47. }
  48. =item relative_to_pkg_root($file)
  49. Returns the filename relative to get_pkg_root_dir($file).
  50. =cut
  51. sub relative_to_pkg_root($) {
  52. my $file = shift;
  53. my $pkg_root = get_pkg_root_dir($file);
  54. if (defined $pkg_root) {
  55. $pkg_root .= "/";
  56. return $file if ($file =~ s/^\Q$pkg_root\E//);
  57. }
  58. return undef;
  59. }
  60. =item guess_pkg_root_dir($file)
  61. This function tries to guess the root directory of the package build tree.
  62. It will first use get_pkg_root_dir(), but it will fallback to a more
  63. imprecise check: namely it will use the parent directory that is a
  64. sub-directory of the debian directory.
  65. It can still return undef if a file outside of the debian sub-directory is
  66. provided.
  67. =cut
  68. sub guess_pkg_root_dir($) {
  69. my $file = shift;
  70. my $root = get_pkg_root_dir($file);
  71. return $root if defined $root;
  72. $file =~ s{/+$}{};
  73. $file =~ s{/+[^/]+$}{} if not -d $file;
  74. my $parent = $file;
  75. while ($file) {
  76. $parent =~ s{/+[^/]+$}{};
  77. last if not -d $parent;
  78. return $file if check_files_are_the_same("debian", $parent);
  79. $file = $parent;
  80. last if $file !~ m{/};
  81. }
  82. return undef;
  83. }
  84. =item check_files_are_the_same($file1, $file2, $resolve_symlink)
  85. This function verifies that both files are the same by checking that the device
  86. numbers and the inode numbers returned by stat()/lstat() are the same. If
  87. $resolve_symlink is true then stat() is used, otherwise lstat() is used.
  88. =cut
  89. sub check_files_are_the_same($$;$) {
  90. my ($file1, $file2, $resolve_symlink) = @_;
  91. return 0 if ((! -e $file1) || (! -e $file2));
  92. my (@stat1, @stat2);
  93. if ($resolve_symlink) {
  94. @stat1 = stat($file1);
  95. @stat2 = stat($file2);
  96. } else {
  97. @stat1 = lstat($file1);
  98. @stat2 = lstat($file2);
  99. }
  100. my $result = ($stat1[0] == $stat2[0]) && ($stat1[1] == $stat2[1]);
  101. return $result;
  102. }
  103. =item canonpath($file)
  104. This function returns a cleaned path. It simplifies double //, and remove
  105. /./ and /../ intelligently. For /../ it simplifies the path only if the
  106. previous element is not a symlink. Thus it should only be used on real
  107. filenames.
  108. =cut
  109. sub canonpath($) {
  110. my $path = shift;
  111. $path = File::Spec->canonpath($path);
  112. my ($v, $dirs, $file) = File::Spec->splitpath($path);
  113. my @dirs = File::Spec->splitdir($dirs);
  114. my @new;
  115. foreach my $d (@dirs) {
  116. if ($d eq '..') {
  117. if (scalar(@new) > 0 and $new[-1] ne "..") {
  118. next if $new[-1] eq ""; # Root directory has no parent
  119. my $parent = File::Spec->catpath($v,
  120. File::Spec->catdir(@new), '');
  121. if (not -l $parent) {
  122. pop @new;
  123. } else {
  124. push @new, $d;
  125. }
  126. } else {
  127. push @new, $d;
  128. }
  129. } else {
  130. push @new, $d;
  131. }
  132. }
  133. return File::Spec->catpath($v, File::Spec->catdir(@new), $file);
  134. }
  135. =item $newpath = resolve_symlink($symlink)
  136. Return the filename of the file pointed by the symlink. The new name is
  137. canonicalized by canonpath().
  138. =cut
  139. sub resolve_symlink($) {
  140. my $symlink = shift;
  141. my $content = readlink($symlink);
  142. return undef unless defined $content;
  143. if (File::Spec->file_name_is_absolute($content)) {
  144. return canonpath($content);
  145. } else {
  146. my ($link_v, $link_d, $link_f) = File::Spec->splitpath($symlink);
  147. my ($cont_v, $cont_d, $cont_f) = File::Spec->splitpath($content);
  148. my $new = File::Spec->catpath($link_v, $link_d . "/" . $cont_d, $cont_f);
  149. return canonpath($new);
  150. }
  151. }
  152. =back
  153. =head1 AUTHOR
  154. Raphael Hertzog <hertzog@debian.org>.
  155. =cut
  156. 1;