source: trunk/base/src/port1.0/portchecksum.tcl @ 1560

Last change on this file since 1560 was 1549, checked in by jkh, 18 years ago

Clean up checksum handling a little by making cosmetic changes to
messages and making it a little less verbose. It's also not strictly
an error if a port author decides not to use a checksum safety belt but
it is worth a warning. Making the car not start if your safety belt isn't
fastened goes a bit too far, whereas a warning buzzer is generally considered
acceptable.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.6 KB
Line 
1# et:ts=4
2# portchecksum.tcl
3#
4# Copyright (c) 2002 Apple Computer, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. Neither the name of Apple Computer, Inc. nor the names of its contributors
16#    may be used to endorse or promote products derived from this software
17#    without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32package provide portchecksum 1.0
33package require portutil 1.0
34
35set com.apple.checksum [target_new com.apple.checksum checksum_main]
36${com.apple.checksum} provides checksum
37${com.apple.checksum} requires main fetch
38${com.apple.checksum} set prerun checksum_start
39
40# define options
41options checksums
42
43set UI_PREFIX "---> "
44
45proc md5 {file} {
46    global distpath UI_PREFIX
47
48    set md5regex "^(MD5)\[ \]\\((.+)\\)\[ \]=\[ \](\[A-Za-z0-9\]+)\n$"
49    set pipe [open "|md5 \"${file}\"" r]
50    set line [read $pipe]
51    if {[regexp $md5regex $line match type filename sum] == 1} {
52        close $pipe
53        if {$filename == $file} {
54            return $sum
55        } else {
56            return -1
57        }
58    } else {
59        # XXX Handle this error beter
60        close $pipe
61        ui_error "$line - md5sum failed!"
62        return -1
63    }
64}
65
66proc dmd5 {file} {
67    global checksums
68
69    foreach {name type sum} $checksums {
70        if {$name == $file} {
71            return $sum
72        }
73    }
74    return -1
75}
76
77proc checksum_start {args} {
78    global UI_PREFIX portname
79
80    ui_msg "$UI_PREFIX Verifying checksum for $portname"
81}
82
83proc checksum_main {args} {
84    global checksums distpath all_dist_files UI_PREFIX
85
86    # If no files have been downloaded there is nothing to checksum
87    if ![info exists all_dist_files] {
88        return 0
89    }
90
91    if ![info exists checksums] {
92        ui_error "No checksums statement in Portfile.  File checksums are:"
93        foreach distfile $all_dist_files {
94            ui_msg "$distfile md5 [md5 $distpath/$distfile]"
95        }
96        return -code error "No checksums statement in Portfile."
97    }
98
99    # Optimization for the 2 argument case for checksums
100    if {[llength $checksums] == 2 && [llength $all_dist_files] == 1} {
101        set checksums [linsert $checksums 0 $all_dist_files]
102    }
103
104    foreach distfile $all_dist_files {
105        set checksum [md5 $distpath/$distfile]
106        set dchecksum [dmd5 $distfile]
107        if {$dchecksum == -1} {
108            ui_warn "No checksum recorded for $distfile"
109        } elseif {$checksum != $dchecksum} {
110            ui_error "Checksum mismatch for $distfile"
111            return -code error "Checksum mismatch for $distfile"
112        }
113    }
114    return 0
115}
Note: See TracBrowser for help on using the repository browser.