1 # Copyright © 2007-2011 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2011 Linaro Limited
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.
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.
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/>.
22 our $VERSION = '1.04';
26 check_files_are_the_same
35 use Exporter qw(import);
39 use Dpkg::Arch qw(get_host_arch debarch_to_debtuple);
46 Dpkg::Path - some common path handling functions
50 It provides some functions to handle various path.
56 =item get_pkg_root_dir($file)
58 This function will scan upwards the hierarchy of directory to find out
59 the directory which contains the "DEBIAN" sub-directory and it will return
60 its path. This directory is the root directory of a package being built.
62 If no DEBIAN subdirectory is found, it will return undef.
66 sub get_pkg_root_dir($) {
69 $file =~ s{/+[^/]+$}{} if not -d $file;
71 return $file if -d "$file/DEBIAN";
72 last if $file !~ m{/};
73 $file =~ s{/+[^/]+$}{};
78 =item relative_to_pkg_root($file)
80 Returns the filename relative to get_pkg_root_dir($file).
84 sub relative_to_pkg_root($) {
86 my $pkg_root = get_pkg_root_dir($file);
87 if (defined $pkg_root) {
89 return $file if ($file =~ s/^\Q$pkg_root\E//);
94 =item guess_pkg_root_dir($file)
96 This function tries to guess the root directory of the package build tree.
97 It will first use get_pkg_root_dir(), but it will fallback to a more
98 imprecise check: namely it will use the parent directory that is a
99 sub-directory of the debian directory.
101 It can still return undef if a file outside of the debian sub-directory is
106 sub guess_pkg_root_dir($) {
108 my $root = get_pkg_root_dir($file);
109 return $root if defined $root;
112 $file =~ s{/+[^/]+$}{} if not -d $file;
115 $parent =~ s{/+[^/]+$}{};
116 last if not -d $parent;
117 return $file if check_files_are_the_same('debian', $parent);
119 last if $file !~ m{/};
124 =item check_files_are_the_same($file1, $file2, $resolve_symlink)
126 This function verifies that both files are the same by checking that the device
127 numbers and the inode numbers returned by stat()/lstat() are the same. If
128 $resolve_symlink is true then stat() is used, otherwise lstat() is used.
132 sub check_files_are_the_same($$;$) {
133 my ($file1, $file2, $resolve_symlink) = @_;
134 return 0 if ((! -e $file1) || (! -e $file2));
136 if ($resolve_symlink) {
137 @stat1 = stat($file1);
138 @stat2 = stat($file2);
140 @stat1 = lstat($file1);
141 @stat2 = lstat($file2);
143 my $result = ($stat1[0] == $stat2[0]) && ($stat1[1] == $stat2[1]);
148 =item canonpath($file)
150 This function returns a cleaned path. It simplifies double //, and remove
151 /./ and /../ intelligently. For /../ it simplifies the path only if the
152 previous element is not a symlink. Thus it should only be used on real
159 $path = File::Spec->canonpath($path);
160 my ($v, $dirs, $file) = File::Spec->splitpath($path);
161 my @dirs = File::Spec->splitdir($dirs);
163 foreach my $d (@dirs) {
165 if (scalar(@new) > 0 and $new[-1] ne '..') {
166 next if $new[-1] eq ''; # Root directory has no parent
167 my $parent = File::Spec->catpath($v,
168 File::Spec->catdir(@new), '');
169 if (not -l $parent) {
181 return File::Spec->catpath($v, File::Spec->catdir(@new), $file);
184 =item $newpath = resolve_symlink($symlink)
186 Return the filename of the file pointed by the symlink. The new name is
187 canonicalized by canonpath().
191 sub resolve_symlink($) {
193 my $content = readlink($symlink);
194 return unless defined $content;
195 if (File::Spec->file_name_is_absolute($content)) {
196 return canonpath($content);
198 my ($link_v, $link_d, $link_f) = File::Spec->splitpath($symlink);
199 my ($cont_v, $cont_d, $cont_f) = File::Spec->splitpath($content);
200 my $new = File::Spec->catpath($link_v, $link_d . '/' . $cont_d, $cont_f);
201 return canonpath($new);
206 =item $cmdpath = find_command($command)
208 Return the path of the command if defined and available on an absolute or
209 relative path or on the $PATH, undef otherwise.
213 sub find_command($) {
218 return "$cmd" if -x "$cmd";
220 foreach my $dir (split(/:/, $ENV{PATH})) {
221 return "$dir/$cmd" if -x "$dir/$cmd";
227 =item $control_file = get_control_path($pkg, $filetype)
229 Return the path of the control file of type $filetype for the given
232 =item @control_files = get_control_path($pkg)
234 Return the path of all available control files for the given package.
238 sub get_control_path($;$) {
239 my ($pkg, $filetype) = @_;
241 my @exec = ('dpkg-query', '--control-path', $pkg);
242 push @exec, $filetype if defined $filetype;
243 spawn(exec => \@exec, wait_child => 1, to_string => \$control_file);
244 chomp($control_file);
245 if (defined $filetype) {
246 return if $control_file eq '';
247 return $control_file;
249 return () if $control_file eq '';
250 return split(/\n/, $control_file);
253 =item $file = find_build_file($basename)
255 Selects the right variant of the given file: the arch-specific variant
256 ("$basename.$arch") has priority over the OS-specific variant
257 ("$basename.$os") which has priority over the default variant
258 ("$basename"). If none of the files exists, then it returns undef.
260 =item @files = find_build_file($basename)
262 Return the available variants of the given file. Returns an empty
263 list if none of the files exists.
267 sub find_build_file($) {
269 my $host_arch = get_host_arch();
270 my ($abi, $libc, $host_os, $cpu) = debarch_to_debtuple($host_arch);
272 foreach my $f ("$base.$host_arch", "$base.$host_os", "$base") {
273 push @files, $f if -f $f;
275 return @files if wantarray;
276 return $files[0] if scalar @files;
284 =head2 Version 1.04 (dpkg 1.17.11)
286 Update semantics: find_command() now handles an empty or undef argument.
288 =head2 Version 1.03 (dpkg 1.16.1)
290 New function: find_build_file()
292 =head2 Version 1.02 (dpkg 1.16.0)
294 New function: get_control_path()
296 =head2 Version 1.01 (dpkg 1.15.8)
298 New function: find_command()
300 =head2 Version 1.00 (dpkg 1.15.6)
302 Mark the module as public.