Path.pm 5.0 KB

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