Path.pm 7.4 KB

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