Path.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Dpkg::Path;
  13. use strict;
  14. use warnings;
  15. use base qw(Exporter);
  16. use File::Spec;
  17. use Cwd qw(realpath);
  18. our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
  19. guess_pkg_root_dir check_files_are_the_same
  20. resolve_symlink canonpath);
  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, $resolve_symlink)
  81. This function verifies that both files are the same by checking that the device
  82. numbers and the inode numbers returned by stat()/lstat() are the same. If
  83. $resolve_symlink is true then stat() is used, otherwise lstat() is used.
  84. =cut
  85. sub check_files_are_the_same($$;$) {
  86. my ($file1, $file2, $resolve_symlink) = @_;
  87. return 0 if ((! -e $file1) || (! -e $file2));
  88. my (@stat1, @stat2);
  89. if ($resolve_symlink) {
  90. @stat1 = stat($file1);
  91. @stat2 = stat($file2);
  92. } else {
  93. @stat1 = lstat($file1);
  94. @stat2 = lstat($file2);
  95. }
  96. my $result = ($stat1[0] == $stat2[0]) && ($stat1[1] == $stat2[1]);
  97. return $result;
  98. }
  99. =item canonpath($file)
  100. This function returns a cleaned path. It simplifies double //, and remove
  101. /./ and /../ intelligently. For /../ it simplifies the path only if the
  102. previous element is not a symlink. Thus it should only be used on real
  103. filenames.
  104. =cut
  105. sub canonpath($) {
  106. my $path = shift;
  107. $path = File::Spec->canonpath($path);
  108. my ($v, $dirs, $file) = File::Spec->splitpath($path);
  109. my @dirs = File::Spec->splitdir($dirs);
  110. my @new;
  111. foreach my $d (@dirs) {
  112. if ($d eq '..') {
  113. if (scalar(@new) > 0 and $new[-1] ne "..") {
  114. next if $new[-1] eq ""; # Root directory has no parent
  115. my $parent = File::Spec->catpath($v,
  116. File::Spec->catdir(@new), '');
  117. if (not -l $parent) {
  118. pop @new;
  119. } else {
  120. push @new, $d;
  121. }
  122. } else {
  123. push @new, $d;
  124. }
  125. } else {
  126. push @new, $d;
  127. }
  128. }
  129. return File::Spec->catpath($v, File::Spec->catdir(@new), $file);
  130. }
  131. =item $newpath = resolve_symlink($symlink)
  132. Return the filename of the file pointed by the symlink. The new name is
  133. canonicalized by canonpath().
  134. =cut
  135. sub resolve_symlink($) {
  136. my $symlink = shift;
  137. my $content = readlink($symlink);
  138. return undef unless defined $content;
  139. if (File::Spec->file_name_is_absolute($content)) {
  140. return canonpath($content);
  141. } else {
  142. my ($link_v, $link_d, $link_f) = File::Spec->splitpath($symlink);
  143. my ($cont_v, $cont_d, $cont_f) = File::Spec->splitpath($content);
  144. my $new = File::Spec->catpath($link_v, $link_d . "/" . $cont_d, $cont_f);
  145. return canonpath($new);
  146. }
  147. }
  148. =back
  149. =head1 AUTHOR
  150. Raphael Hertzog <hertzog@debian.org>.
  151. =cut
  152. 1;