Path.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Copyright © 2007-2011 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2011 Linaro Limited
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package Dpkg::Path;
  17. use strict;
  18. use warnings;
  19. our $VERSION = "1.02";
  20. use base qw(Exporter);
  21. use File::Spec;
  22. use Cwd qw(realpath);
  23. use Dpkg::Arch qw(get_host_arch debarch_to_debtriplet);
  24. use Dpkg::IPC;
  25. our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
  26. guess_pkg_root_dir check_files_are_the_same
  27. resolve_symlink canonpath find_command
  28. get_control_path find_build_file);
  29. =encoding utf8
  30. =head1 NAME
  31. Dpkg::Path - some common path handling functions
  32. =head1 DESCRIPTION
  33. It provides some functions to handle various path.
  34. =head1 METHODS
  35. =over 8
  36. =item get_pkg_root_dir($file)
  37. This function will scan upwards the hierarchy of directory to find out
  38. the directory which contains the "DEBIAN" sub-directory and it will return
  39. its path. This directory is the root directory of a package being built.
  40. If no DEBIAN subdirectory is found, it will return undef.
  41. =cut
  42. sub get_pkg_root_dir($) {
  43. my $file = shift;
  44. $file =~ s{/+$}{};
  45. $file =~ s{/+[^/]+$}{} if not -d $file;
  46. while ($file) {
  47. return $file if -d "$file/DEBIAN";
  48. last if $file !~ m{/};
  49. $file =~ s{/+[^/]+$}{};
  50. }
  51. return;
  52. }
  53. =item relative_to_pkg_root($file)
  54. Returns the filename relative to get_pkg_root_dir($file).
  55. =cut
  56. sub relative_to_pkg_root($) {
  57. my $file = shift;
  58. my $pkg_root = get_pkg_root_dir($file);
  59. if (defined $pkg_root) {
  60. $pkg_root .= "/";
  61. return $file if ($file =~ s/^\Q$pkg_root\E//);
  62. }
  63. return;
  64. }
  65. =item guess_pkg_root_dir($file)
  66. This function tries to guess the root directory of the package build tree.
  67. It will first use get_pkg_root_dir(), but it will fallback to a more
  68. imprecise check: namely it will use the parent directory that is a
  69. sub-directory of the debian directory.
  70. It can still return undef if a file outside of the debian sub-directory is
  71. provided.
  72. =cut
  73. sub guess_pkg_root_dir($) {
  74. my $file = shift;
  75. my $root = get_pkg_root_dir($file);
  76. return $root if defined $root;
  77. $file =~ s{/+$}{};
  78. $file =~ s{/+[^/]+$}{} if not -d $file;
  79. my $parent = $file;
  80. while ($file) {
  81. $parent =~ s{/+[^/]+$}{};
  82. last if not -d $parent;
  83. return $file if check_files_are_the_same("debian", $parent);
  84. $file = $parent;
  85. last if $file !~ m{/};
  86. }
  87. return;
  88. }
  89. =item check_files_are_the_same($file1, $file2, $resolve_symlink)
  90. This function verifies that both files are the same by checking that the device
  91. numbers and the inode numbers returned by stat()/lstat() are the same. If
  92. $resolve_symlink is true then stat() is used, otherwise lstat() is used.
  93. =cut
  94. sub check_files_are_the_same($$;$) {
  95. my ($file1, $file2, $resolve_symlink) = @_;
  96. return 0 if ((! -e $file1) || (! -e $file2));
  97. my (@stat1, @stat2);
  98. if ($resolve_symlink) {
  99. @stat1 = stat($file1);
  100. @stat2 = stat($file2);
  101. } else {
  102. @stat1 = lstat($file1);
  103. @stat2 = lstat($file2);
  104. }
  105. my $result = ($stat1[0] == $stat2[0]) && ($stat1[1] == $stat2[1]);
  106. return $result;
  107. }
  108. =item canonpath($file)
  109. This function returns a cleaned path. It simplifies double //, and remove
  110. /./ and /../ intelligently. For /../ it simplifies the path only if the
  111. previous element is not a symlink. Thus it should only be used on real
  112. filenames.
  113. =cut
  114. sub canonpath($) {
  115. my $path = shift;
  116. $path = File::Spec->canonpath($path);
  117. my ($v, $dirs, $file) = File::Spec->splitpath($path);
  118. my @dirs = File::Spec->splitdir($dirs);
  119. my @new;
  120. foreach my $d (@dirs) {
  121. if ($d eq '..') {
  122. if (scalar(@new) > 0 and $new[-1] ne "..") {
  123. next if $new[-1] eq ""; # Root directory has no parent
  124. my $parent = File::Spec->catpath($v,
  125. File::Spec->catdir(@new), '');
  126. if (not -l $parent) {
  127. pop @new;
  128. } else {
  129. push @new, $d;
  130. }
  131. } else {
  132. push @new, $d;
  133. }
  134. } else {
  135. push @new, $d;
  136. }
  137. }
  138. return File::Spec->catpath($v, File::Spec->catdir(@new), $file);
  139. }
  140. =item $newpath = resolve_symlink($symlink)
  141. Return the filename of the file pointed by the symlink. The new name is
  142. canonicalized by canonpath().
  143. =cut
  144. sub resolve_symlink($) {
  145. my $symlink = shift;
  146. my $content = readlink($symlink);
  147. return unless defined $content;
  148. if (File::Spec->file_name_is_absolute($content)) {
  149. return canonpath($content);
  150. } else {
  151. my ($link_v, $link_d, $link_f) = File::Spec->splitpath($symlink);
  152. my ($cont_v, $cont_d, $cont_f) = File::Spec->splitpath($content);
  153. my $new = File::Spec->catpath($link_v, $link_d . "/" . $cont_d, $cont_f);
  154. return canonpath($new);
  155. }
  156. }
  157. =item my $cmdpath = find_command($command)
  158. Return the path of the command if available on an absolute or relative
  159. path or on the $PATH, undef otherwise.
  160. =cut
  161. sub find_command($) {
  162. my $cmd = shift;
  163. if ($cmd =~ m{/}) {
  164. return "$cmd" if -x "$cmd";
  165. } else {
  166. foreach my $dir (split(/:/, $ENV{'PATH'})) {
  167. return "$dir/$cmd" if -x "$dir/$cmd";
  168. }
  169. }
  170. return;
  171. }
  172. =item my $control_file = get_control_path($pkg, $filetype)
  173. Return the path of the control file of type $filetype for the given
  174. package.
  175. =item my @control_files = get_control_path($pkg)
  176. Return the path of all available control files for the given package.
  177. =cut
  178. sub get_control_path($;$) {
  179. my ($pkg, $filetype) = @_;
  180. my $control_file;
  181. my @exec = ("dpkg-query", "--control-path", $pkg);
  182. push @exec, $filetype if defined $filetype;
  183. spawn(exec => \@exec, wait_child => 1, to_string => \$control_file);
  184. chomp($control_file);
  185. if (defined $filetype) {
  186. return if $control_file eq "";
  187. return $control_file;
  188. }
  189. return () if $control_file eq "";
  190. return split(/\n/, $control_file);
  191. }
  192. =item my $file = find_build_file($basename)
  193. Selects the right variant of the given file: the arch-specific variant
  194. ("$basename.$arch") has priority over the OS-specific variant
  195. ("$basename.$os") which has priority over the default variant
  196. ("$basename"). If none of the files exists, then it returns undef.
  197. =item my @files = find_build_file($basename)
  198. Return the available variants of the given file. Returns an empty
  199. list if none of the files exists.
  200. =cut
  201. sub find_build_file($) {
  202. my $base = shift;
  203. my $host_arch = get_host_arch();
  204. my ($abi, $host_os, $cpu) = debarch_to_debtriplet($host_arch);
  205. my @files;
  206. foreach my $f ("$base.$host_arch", "$base.$host_os", "$base") {
  207. push @files, $f if -f $f;
  208. }
  209. return @files if wantarray;
  210. return $files[0] if scalar @files;
  211. return;
  212. }
  213. =back
  214. =head1 CHANGES
  215. =head2 Version 1.03
  216. New function: find_build_file()
  217. =head2 Version 1.02
  218. New function: get_control_path()
  219. =head2 Version 1.01
  220. New function: find_command()
  221. =head1 AUTHOR
  222. Raphaël Hertzog <hertzog@debian.org>.
  223. =cut
  224. 1;