Path.pm 5.1 KB

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