Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | magnus | 1 | package DebPool::Release; |
2 | |||
3 | ### |
||
4 | # |
||
5 | # DebPool::Release - Module for generating and installing Release files |
||
6 | # |
||
7 | # Copyright 2003-2004 Joel Aelwyn. All rights reserved. |
||
8 | # |
||
9 | # Redistribution and use in source and binary forms, with or without |
||
10 | # modification, are permitted provided that the following conditions |
||
11 | # are met: |
||
12 | # 1. Redistributions of source code must retain the above copyright |
||
13 | # notice, this list of conditions and the following disclaimer. |
||
14 | # 2. Redistributions in binary form must reproduce the above copyright |
||
15 | # notice, this list of conditions and the following disclaimer in the |
||
16 | # documentation and/or other materials provided with the distribution. |
||
17 | # 3. Neither the name of the Author nor the names of any contributors |
||
18 | # may be used to endorse or promote products derived from this software |
||
19 | # without specific prior written permission. |
||
20 | # |
||
21 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
||
22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
||
25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
31 | # SUCH DAMAGE. |
||
32 | # |
||
33 | # $Id: Release.pm 27 2004-11-07 03:06:59Z joel $ |
||
34 | # |
||
35 | ### |
||
36 | |||
37 | # We use 'our', so we must have at least Perl 5.6 |
||
38 | |||
39 | require 5.006_000; |
||
40 | |||
41 | # Always good ideas. |
||
42 | |||
43 | use strict; |
||
44 | use warnings; |
||
45 | |||
46 | use POSIX; # strftime |
||
47 | use File::Temp qw(tempfile); |
||
48 | |||
49 | # We need the Digest modules so that we can calculate the proper checksums. |
||
50 | |||
51 | use Digest::MD5; |
||
52 | use Digest::SHA1; |
||
53 | |||
54 | ### Module setup |
||
55 | |||
56 | BEGIN { |
||
57 | use Exporter (); |
||
58 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); |
||
59 | |||
60 | # Version checking |
||
61 | $VERSION = '0.1.5'; |
||
62 | |||
63 | @ISA = qw(Exporter); |
||
64 | |||
65 | @EXPORT = qw( |
||
66 | ); |
||
67 | |||
68 | @EXPORT_OK = qw( |
||
69 | &Generate_Release_Triple |
||
70 | &Install_Release |
||
71 | ); |
||
72 | |||
73 | %EXPORT_TAGS = ( |
||
74 | 'functions' => [qw(&Generate_Release_Triple &Install_Release)], |
||
75 | 'vars' => [qw()], |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | ### Exported package globals |
||
80 | |||
81 | ### Non-exported package globals |
||
82 | |||
83 | # Thread-safe? What's that? Package global error value. We don't export |
||
84 | # this directly, because it would conflict with other modules. |
||
85 | |||
86 | our($Error); |
||
87 | |||
88 | # Magic filenames - these are files we want to include hashes for in a |
||
89 | # Release file. |
||
90 | |||
91 | my(@SigFiles) = ( |
||
92 | 'Packages', |
||
93 | 'Sources', |
||
94 | 'Packages.gz', |
||
95 | 'Sources.gz', |
||
96 | ); |
||
97 | |||
98 | ### File lexicals |
||
99 | |||
100 | # None |
||
101 | |||
102 | ### Constant functions |
||
103 | |||
104 | # None |
||
105 | |||
106 | ### Meaningful functions |
||
107 | |||
8 | magnus | 108 | # Compute_Checksums($basepath, $filename) |
109 | # |
||
110 | # Compute MD5 and SHA1 checksums of a file. The checksums are stored, |
||
111 | # together with the filename and the file size, in a hash, a reference |
||
112 | # to which is returned. |
||
113 | # |
||
114 | # Returns undef on error. |
||
115 | |||
116 | sub Compute_Checksums { |
||
117 | my($basepath, $filename) = @_; |
||
118 | my($fh); |
||
119 | |||
120 | # Open the file and read in the contents. This could be a very |
||
121 | # large amount of data, but unfortunately, both Digets routines |
||
122 | # require the entire thing at once. |
||
123 | |||
124 | unless (open($fh, '<', "$basepath/$filename") && binmode($fh)) { |
||
125 | $Error = "Couldn't open file '$basepath/$filename' for reading."; |
||
126 | return undef; |
||
127 | } |
||
128 | |||
129 | # Grab the filesize from stat() |
||
130 | my(@stat) = stat($fh); |
||
131 | my $ret = { |
||
132 | 'File' => $filename, |
||
133 | 'Size' => $stat[7], |
||
134 | }; |
||
135 | |||
136 | my %digesters = ('MD5Sum' => new Digest::MD5, |
||
137 | 'SHA1' => new Digest::SHA1); |
||
138 | |||
139 | # Now calculate the checksums and put them into the hashes. |
||
140 | while (my ($algo, $dig) = each %digesters) { |
||
141 | eval { |
||
142 | seek $fh, 0, 0 or die $!; |
||
143 | $ret->{$algo} = $dig->addfile($fh)->hexdigest; |
||
144 | }; |
||
145 | if ($@) { |
||
146 | $Error = "Failed to compute $algo for '$basepath/$filename': $@"; |
||
147 | close $fh; |
||
148 | return undef; |
||
149 | } |
||
150 | } |
||
151 | return $ret; |
||
152 | } |
||
153 | |||
1 | magnus | 154 | # Generate_Release_Triple($archive, $component, $architecture, $version) |
155 | # |
||
156 | # Generate a Release file for a specific dist/component/arch, in the |
||
157 | # temp/working area, and return the filename. |
||
158 | # |
||
159 | # Returns undef (and sets $Error) on error. |
||
160 | |||
161 | sub Generate_Release_Triple { |
||
162 | use DebPool::Config qw(:vars); |
||
163 | use DebPool::Dirs qw(:functions); |
||
164 | |||
165 | my($archive, $component, $architecture, $version) = @_; |
||
166 | |||
7 | magnus | 167 | my(@Checksums); |
1 | magnus | 168 | |
169 | # Before we bother to do much else, generate the MD5 and SHA1 checksums |
||
170 | # we'll need later. This is mostly so that we can catch errors before |
||
171 | # ever bothering to open a tempfile. |
||
172 | |||
173 | # First, grab a list of files from the directory. |
||
174 | |||
175 | my($dirpath) = "${Options{'dists_dir'}}/"; |
||
176 | $dirpath .= Archfile($archive, $component, $architecture, 1); |
||
177 | |||
178 | if (!opendir(RELDIR, $dirpath)) { |
||
179 | $Error = "Couldn't open directory '$dirpath'."; |
||
180 | return undef; |
||
181 | } |
||
182 | |||
183 | my(@dirfiles) = readdir(RELDIR); |
||
184 | close(RELDIR); |
||
185 | |||
186 | # Now, for each file, generate MD5 and SHA1 checksums, and put them |
||
187 | # into Checksums for later use (assuming it's a file we care about). |
||
188 | |||
7 | magnus | 189 | foreach my $ck_file (@dirfiles) { |
1 | magnus | 190 | if (0 == grep(/^$ck_file$/, @SigFiles)) { # We don't care about it. |
191 | next; |
||
192 | } |
||
8 | magnus | 193 | my $checksum = Compute_Checksums($dirpath, $ck_file) or return undef; |
194 | push @Checksums, $checksum; |
||
1 | magnus | 195 | } |
196 | |||
197 | # Open a secure tempfile, and write the headers to it. |
||
198 | |||
199 | my($tmpfile_handle, $tmpfile_name) = tempfile(); |
||
200 | |||
201 | print $tmpfile_handle "Archive: $archive\n"; |
||
202 | print $tmpfile_handle "Component: $component\n"; |
||
203 | print $tmpfile_handle "Version: $version\n"; |
||
204 | print $tmpfile_handle "Origin: $Options{'release_origin'}\n"; |
||
205 | print $tmpfile_handle "Label: $Options{'release_label'}\n"; |
||
206 | print $tmpfile_handle "Architecture: $architecture\n"; |
||
207 | |||
208 | # If the archive (aka distribution) appears in release_noauto, print |
||
209 | # the appropriate directive. |
||
210 | |||
211 | if (0 != grep(/^$archive$/, @{$Options{'release_noauto'}})) { |
||
212 | print $tmpfile_handle "NotAutomatic: yes\n"; |
||
213 | } |
||
214 | |||
215 | print $tmpfile_handle "Description: $Options{'release_description'}\n"; |
||
216 | |||
217 | # Now print MD5 and SHA1 checksum lists. |
||
218 | |||
8 | magnus | 219 | foreach my $algo ('MD5Sum', 'SHA1') { |
220 | print $tmpfile_handle "$algo:\n"; |
||
1 | magnus | 221 | |
8 | magnus | 222 | foreach my $checksum (@Checksums) { |
223 | printf $tmpfile_handle " %s %8d %s\n", $checksum->{$algo}, |
||
7 | magnus | 224 | $checksum->{'Size'}, $checksum->{'File'}; |
8 | magnus | 225 | } |
1 | magnus | 226 | } |
227 | |||
228 | close($tmpfile_handle); |
||
229 | |||
230 | return $tmpfile_name; |
||
231 | } |
||
232 | |||
233 | # Generate_Release_Dist($archive, $version, @files) |
||
234 | # |
||
235 | # Generate top-level Release file for a specific distribution, covering the |
||
236 | # given files, in the temp/working area, and return the filename. |
||
237 | # |
||
238 | # Filenames in @files should be relative to <dists_dir>/<archive>, with no |
||
239 | # leading slash (ie, main/binary-i386/Packages). |
||
240 | # |
||
241 | # Returns undef (and sets $Error) on error. |
||
242 | |||
243 | sub Generate_Release_Dist { |
||
244 | use DebPool::Config qw(:vars); |
||
245 | |||
246 | my($archive) = shift(@_); |
||
247 | my($version) = shift(@_); |
||
248 | my(@files) = @_; |
||
249 | |||
7 | magnus | 250 | my(@Checksums); |
1 | magnus | 251 | my($dists_dir) = $Options{'dists_dir'}; |
252 | |||
253 | # Before we bother to do much else, generate the MD5 and SHA1 checksums |
||
254 | # we'll need later. This is mostly so that we can catch errors before |
||
255 | # ever bothering to open a tempfile. |
||
256 | |||
257 | my($file); |
||
258 | for $file (@files) { |
||
259 | # Now, for each file, generate MD5 and SHA1 checksums, and put them |
||
260 | # into Checksums for later use (assuming it's a file we care about). |
||
8 | magnus | 261 | my $checksum = Compute_Checksums("${dists_dir}/${archive}", $file) or return undef; |
262 | push @Checksums, $checksum; |
||
1 | magnus | 263 | } |
264 | |||
265 | # Open a secure tempfile, and set up some variables. |
||
266 | |||
267 | my($tmpfile_handle, $tmpfile_name) = tempfile(); |
||
268 | |||
269 | my($now_822) = strftime('%a, %d %b %Y %H:%M:%S %Z', localtime()); |
||
270 | my(@archs) = grep(!/^source$/, @{$Options{'archs'}}); |
||
271 | my($suite) = $Options{'reverse_dists'}->{$archive}; |
||
272 | |||
273 | # Write the headers into the Release tempfile |
||
274 | |||
275 | print $tmpfile_handle "Origin: ${Options{'release_origin'}}\n"; |
||
276 | print $tmpfile_handle "Label: ${Options{'release_label'}}\n"; |
||
277 | print $tmpfile_handle "Suite: ${suite}\n"; |
||
278 | print $tmpfile_handle "Codename: ${archive}\n"; |
||
279 | print $tmpfile_handle "Date: ${now_822}\n"; |
||
280 | print $tmpfile_handle "Architectures: " . join(' ', @archs) . "\n"; |
||
281 | print $tmpfile_handle "Components: " . join(' ', @{$Options{'sections'}}) . "\n"; |
||
282 | print $tmpfile_handle "Description: $Options{'release_description'}\n"; |
||
283 | |||
284 | # Now print MD5 and SHA1 checksum lists. |
||
285 | |||
8 | magnus | 286 | foreach my $algo ('MD5Sum', 'SHA1') { |
287 | print $tmpfile_handle "$algo:\n"; |
||
1 | magnus | 288 | |
8 | magnus | 289 | foreach my $checksum (@Checksums) { |
290 | printf $tmpfile_handle " %s %8d %s\n", $checksum->{$algo}, |
||
291 | $checksum->{'Size'}, $checksum->{'File'}; |
||
292 | } |
||
1 | magnus | 293 | } |
294 | close($tmpfile_handle); |
||
295 | |||
296 | return $tmpfile_name; |
||
297 | } |
||
298 | |||
299 | # Install_Release($archive, $component, $architecture, $release, $signature) |
||
300 | # |
||
301 | # Installs a release file and an optional signature file to the |
||
302 | # distribution directory specified by the ($archive, $component, |
||
303 | # $architecture) triple, or $archive if $component and $architecture are |
||
304 | # undefined. Returns 0 (and sets $Error) on failure, 1 on |
||
305 | # success. |
||
306 | |||
307 | sub Install_Release { |
||
308 | use DebPool::Config qw(:vars); |
||
309 | use DebPool::Util qw(:functions); |
||
310 | |||
311 | my($archive, $component, $architecture, $release, $signature) = @_; |
||
312 | |||
313 | my($dists_file_mode) = $Options{'dists_file_mode'}; |
||
314 | |||
315 | my($inst_dir); |
||
316 | if (defined($architecture) && defined($component)) { |
||
317 | $inst_dir = "${Options{'dists_dir'}}/"; |
||
318 | $inst_dir .= Archfile($archive, $component, $architecture, 1); |
||
319 | } else { |
||
320 | $inst_dir = "${Options{'dists_dir'}}/${archive}"; |
||
321 | } |
||
322 | |||
323 | # Now install the file(s) into the appropriate place(s). |
||
324 | |||
325 | if (!Move_File($release, "${inst_dir}/Release", $dists_file_mode)) { |
||
326 | $Error = "Couldn't install Release file '${release}' to "; |
||
327 | $Error .= "'${inst_dir}': ${DebPool::Util::Error}"; |
||
328 | return 0; |
||
329 | } |
||
330 | |||
331 | if (defined($signature) && !Move_File($signature, "${inst_dir}/Release.gpg", |
||
332 | $dists_file_mode)) { |
||
333 | $Error = "Couldn't install Signature file '${signature}' to "; |
||
334 | $Error .= "'${inst_dir}': ${DebPool::Util::Error}"; |
||
335 | return 0; |
||
336 | } |
||
337 | |||
338 | return 1; |
||
339 | } |
||
340 | |||
341 | END {} |
||
342 | |||
343 | 1; |
||
344 | |||
345 | __END__ |
||
346 | |||
347 | # vim:set tabstop=4 expandtab: |