Path.pm 5.0 KB

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