summaryrefslogtreecommitdiffstats
path: root/loader/mediacheck.c
blob: d6a0c1d57e9def2e3c93c1f5fd8e2d0f2b6f6ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * simple program to check implanted md5sum in an iso 9660 image
 *
 * Copyright (C) 2001, 2005  Red Hat, Inc.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Michael Fulbright <msf@redhat.com>
 *            Dustin Kirkland <dustin.kirkland@gmail.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <newt.h>
#include <libcheckisomd5.h>

#include "lang.h"
#include "log.h"

struct progressCBdata {
    newtComponent scale;
    newtComponent label;
};

static void readCB(void *co, long long pos, long long total) {
    struct progressCBdata *data = co;
    char tickmark[2] = "-";
    char * ticks = "-\\|/";

    newtScaleSet(data->scale, pos * 100.0 / total);
    *tickmark = ticks[(total / (pos + 1)) % 5];

    newtLabelSetText(data->label, tickmark);
    newtRefresh();
}

int doMediaCheck(char *file, char *descr) {
    struct progressCBdata data;
    newtComponent t, f, scale, label;
    int rc;
    int dlen;
    int llen;
    char tmpstr[1024];

    if (access(file, R_OK) < 0) {
	newtWinMessage(_("Error"), _("OK"), _("Unable to find install image "
					      "%s"), file);
	return -1;
    }

    if (descr)
	snprintf(tmpstr, sizeof(tmpstr), _("Checking \"%s\"..."), descr);
    else
	snprintf(tmpstr, sizeof(tmpstr), _("Checking media now..."));

    dlen = strlen(tmpstr);
    if (dlen > 65)
	dlen = 65;

    newtCenteredWindow(dlen+8, 6, _("Media Check"));
    t = newtTextbox(1, 1, dlen+4, 3, NEWT_TEXTBOX_WRAP);

    newtTextboxSetText(t, tmpstr);
    llen = strlen(tmpstr);

    label = newtLabel(llen+1, 1, "-");
    f = newtForm(NULL, NULL, 0);
    newtFormAddComponent(f, t);
    scale = newtScale(3, 3, dlen, 100);
    newtFormAddComponent(f, scale);

    newtDrawForm(f);
    newtRefresh();

    data.scale = scale;
    data.label = label;

    rc = mediaCheckFile(file, readCB, &data);

    newtFormDestroy(f);
    newtPopWindow();

    if (rc == -1) {
	logMessage(WARNING, "mediacheck: %s (%s) has no checksum info", file, descr);
	newtWinMessage(_("Error"), _("OK"),
		       _("Unable to read the disc checksum from the "
			 "primary volume descriptor.  This probably "
			 "means the disc was created without adding the "
			 "checksum."));
    } else if (rc == 0) {
        logMessage(ERROR, "mediacheck: %s (%s) FAILED", file, descr);
        newtWinMessage(_("Error"), _("OK"),
                       _("The image which was just tested has errors. "
                         "This could be due to a "
                         "corrupt download or a bad disc.  "
                         "If applicable, please clean the disc "
                         "and try again.  If this test continues to fail you "
                         "should not continue the install."));
    } else if (rc > 0) {
        logMessage(INFO, "mediacheck: %s (%s) PASSED", file, descr);
        newtWinMessage(_("Success"), _("OK"),
                       _("The image which was just tested was successfully "
                         "verified.  It should be OK to install from this "
                         "media.  Note that not all media/drive errors can "
                         "be detected by the media check."));
    }


    return rc;
}