Path.pm 5.1 KB

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