-
Notifications
You must be signed in to change notification settings - Fork 13
/
checkisomd5.c
184 lines (156 loc) · 4.94 KB
/
checkisomd5.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* checkosmd5 - simple program to check implanted md5sum
* Copyright (C) 2001-2013 Red Hat, Inc.
* Michael Fulbright <msf@redhat.com>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <popt.h>
#include <termios.h>
#include "md5.h"
#include "libcheckisomd5.h"
struct progressCBData {
int verbose;
int gauge;
int gaugeat;
};
int user_bailing_out(void) {
struct timeval timev;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
timev.tv_sec = 0;
timev.tv_usec = 0;
if (select(1, &rfds, NULL, NULL, &timev) && getchar() == 27)
return 1;
return 0;
}
static int outputCB(void *const co, const long long offset, const long long total) {
struct progressCBData *const data = co;
int gaugeval = -1;
if (data->verbose) {
printf("\rChecking: %05.1f%%", (100.0 * (double) offset) / (double) total);
fflush(stdout);
}
if (data->gauge) {
gaugeval = (int) ((100.0 * (double) offset) / (double) total);
if (gaugeval != data->gaugeat) {
printf("%d\n", gaugeval);
fflush(stdout);
data->gaugeat = gaugeval;
}
}
return user_bailing_out();
}
static int usage(void) {
fprintf(stderr, "Usage: checkisomd5 [--md5sumonly] [--verbose] [--gauge] <isofilename>|<blockdevice>\n\n");
return 1;
}
/* Process the result code and return the proper exit status value
*
* return 1 for failures, 0 for good checksum and 2 if aborted.
*/
int processExitStatus(const int rc) {
char *result;
int exit_rc;
switch (rc) {
case ISOMD5SUM_CHECK_FAILED:
result = "FAIL.\n\nIt is not recommended to use this media.";
exit_rc = 1;
break;
case ISOMD5SUM_CHECK_ABORTED:
result = "UNKNOWN.\n\nThe media check was aborted.";
exit_rc = 2;
break;
case ISOMD5SUM_CHECK_NOT_FOUND:
result = "NA.\n\nNo checksum information available, unable to verify media.";
exit_rc = 1;
break;
case ISOMD5SUM_FILE_NOT_FOUND:
result = "NA.\n\nFile not found.";
exit_rc = 1;
break;
case ISOMD5SUM_CHECK_PASSED:
result = "PASS.\n\nIt is OK to use this media.";
exit_rc = 0;
break;
default:
result = "checkisomd5 ERROR - bad return value";
exit_rc = 1;
break;
}
fprintf(stderr, "\nThe media check is complete, the result is: %s\n", result);
return exit_rc;
}
int main(int argc, const char **argv) {
struct progressCBData data;
memset(&data, 0, sizeof(data));
data.verbose = 0;
data.gauge = 0;
int md5only = 0;
int help = 0;
struct poptOption options[] = {
{ "md5sumonly", 'o', POPT_ARG_NONE, &md5only, 0 },
{ "verbose", 'v', POPT_ARG_NONE, &data.verbose, 0 },
{ "gauge", 'g', POPT_ARG_NONE, &data.gauge, 0 },
{ "help", 'h', POPT_ARG_NONE, &help, 0 },
{ 0, 0, 0, 0, 0 }
};
poptContext optCon = poptGetContext("checkisomd5", argc, argv, options, 0);
int rc = poptGetNextOpt(optCon);
if (rc < -1) {
fprintf(stderr, "bad option %s: %s\n",
poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
poptStrerror(rc));
poptFreeContext(optCon);
return 1;
}
if (help) {
poptFreeContext(optCon);
return usage();
}
const char **args = poptGetArgs(optCon);
if (!args || !args[0] || !args[0][0]) {
poptFreeContext(optCon);
return usage();
}
if (md5only | data.verbose) {
rc = printMD5SUM(args[0]);
if (rc < 0) {
poptFreeContext(optCon);
return processExitStatus(rc);
}
}
if (md5only) {
poptFreeContext(optCon);
return 0;
}
printf("Press [Esc] to abort check.\n");
static struct termios oldt;
struct termios newt;
tcgetattr(0, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
tcsetattr(0, TCSANOW, &newt);
rc = mediaCheckFile(args[0], outputCB, &data);
tcsetattr(0, TCSANOW, &oldt);
if (data.verbose)
printf("\n");
poptFreeContext(optCon);
return processExitStatus(rc);
}