/[d-i]/trunk/tools/cdrom-checker/main.c
ViewVC logotype

Contents of /trunk/tools/cdrom-checker/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5925 - (show annotations) (download)
Mon Nov 10 14:04:11 2003 UTC (9 years, 6 months ago) by tsauter
File MIME type: text/plain
File size: 6202 byte(s)
fix template typo
1 /* cdrom-checker - verify debian cdrom's
2 * Copyright (C) 2003 Thorsten Sauter <tsauter@gmx.net>
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20
21 #include "main.h"
22
23
24 void detect_cdrom() {
25 FILE *mount;
26 char line[1024];
27
28 mount = fopen("/proc/mounts", "r");
29 while(fgets(line, sizeof(line), mount) != 0) {
30 char device[1024], mpoint[1024];
31 sscanf(line, "%s %s %*s", device, mpoint);
32 if(strstr(mpoint, TARGET)) {
33 asprintf(&cdrom_device, "%s", device);
34 break;
35 }
36 }
37 fclose(mount);
38
39 if(cdrom_device == NULL) {
40 di_log(DI_LOG_LEVEL_WARNING, "Unable to detect cdrom device!");
41 exit(EXIT_SUCCESS);
42 }
43 }
44
45 int mount_cdrom() {
46 int status = 0;
47 char *cmd = NULL;
48
49 asprintf(&cmd, "/bin/grep -q '^%s' /proc/mounts",
50 cdrom_device);
51 status = di_exec_shell(cmd);
52 di_free(cmd);
53 if(WIFEXITED(status) && WEXITSTATUS(status) == 0)
54 return(EXIT_SUCCESS);
55
56 /* ask if we should mount the device */
57 debconf_fset(debconf, "cdrom-checker/askmount", "seen", "false");
58 debconf_set(debconf, "cdrom-checker/askmount", "false");
59 debconf_input(debconf, "high", "cdrom-checker/askmount");
60 debconf_go(debconf);
61 debconf_get(debconf, "cdrom-checker/askmount");
62
63 asprintf(&cmd, "mount -t iso9660 %s %s -o ro 1>/dev/null 2>&1",
64 cdrom_device, TARGET);
65 status = di_exec_shell(cmd);
66 di_free(cmd);
67 if(WIFEXITED(status) && WEXITSTATUS(status) == 0)
68 return(EXIT_SUCCESS);
69
70 /* unable to mount the cdrom device */
71 debconf_fset(debconf, "cdrom-checker/mntfailed", "seen", "false");
72 debconf_subst(debconf,"cdrom-checker/mntfailed", "CDROM", cdrom_device);
73 debconf_input(debconf, "high", "cdrom-checker/mntfailed");
74 debconf_go(debconf);
75 return(EXIT_FAILURE);
76 }
77
78 int md5file_getlines(FILE *md5file) {
79 char line[1024];
80 int lines = 0;
81
82 rewind(md5file);
83 while(fgets(line, sizeof(line), md5file) != 0)
84 lines++;
85
86 return(lines);
87 }
88
89 int valid_cdrom() {
90 struct stat dir;
91
92 /* test ".disk" directory */
93 if(stat(".disk", &dir) != 0)
94 return(EXIT_FAILURE);
95 if(!S_ISDIR(dir.st_mode))
96 return(EXIT_FAILURE);
97
98 /* test ".disk" directory */
99 if(stat("debian", &dir) != 0)
100 return(EXIT_FAILURE);
101 if(!S_ISDIR(dir.st_mode))
102 return(EXIT_FAILURE);
103
104 /* test "md5sum.txt" directory */
105 if(stat("md5sum.txt", &dir) != 0)
106 return(EXIT_FAILURE);
107 if(!S_ISREG(dir.st_mode))
108 return(EXIT_FAILURE);
109
110 return(EXIT_SUCCESS);
111 }
112
113 int check_cdrom() {
114 FILE *md5file = NULL;
115 int lines, status = 0;
116 char line[1024];
117 char filename[1024];
118
119 md5file = fopen("md5sum.txt", "r");
120 if(md5file == NULL) {
121 debconf_input(debconf, "high", "cdrom-checker/md5file_failed");
122 debconf_go(debconf);
123 exit(EXIT_FAILURE);
124 }
125
126 lines = md5file_getlines(md5file);
127 debconf_progress_start(debconf, 0, lines, "cdrom-checker/progress_title");
128
129 rewind(md5file);
130 while(fgets(line, sizeof(line), md5file) != 0) {
131 char *cmd;
132
133 status = 0;
134 sscanf(line, "%*s %s", filename);
135 debconf_subst(debconf, "cdrom-checker/progress_step", "FILE", filename);
136 debconf_progress_info(debconf, "cdrom-checker/progress_step");
137 asprintf(&cmd, "echo '%s' | md5sum -c 1>/dev/null 2>&1", line);
138 if(system(cmd) != 0) {
139 status = 1;
140 break;
141 }
142 debconf_progress_step(debconf, 1);
143 }
144
145 fclose(md5file);
146 debconf_progress_stop(debconf);
147
148 /* print the result of the command */
149 if(status != 0) {
150 debconf_fset(debconf,"cdrom-checker/mismatch", "seen", "false");
151 debconf_subst(debconf, "cdrom-checker/mismatch", "FILE", filename);
152 debconf_input(debconf, "high", "cdrom-checker/mismatch");
153 } else {
154 debconf_fset(debconf, "cdrom-checker/passed", "seen", "false");
155 debconf_input(debconf, "high", "cdrom-checker/passed");
156 }
157 debconf_go(debconf);
158
159 return(status);
160 }
161
162 int main(int argc, char **argv) {
163 system("touch /tmp/test");
164 system("logger init");
165 di_system_init(basename(argv[0]));
166 /* initialize the debconf frontend */
167 debconf = debconfclient_new();
168 system("logger after");
169
170 /* ask if we should proceed the checking */
171 debconf_fset(debconf, "cdrom-checker/start", "seen", "false");
172 debconf_set(debconf, "cdrom-checker/start", "false");
173 debconf_input(debconf, "high", "cdrom-checker/start");
174 debconf_go(debconf);
175 debconf_get(debconf, "cdrom-checker/start");
176 if(!strstr(debconf->value, "true")) { /* return to main-menu */
177 return(EXIT_SUCCESS);
178 }
179 system("logger after-quest");
180
181 detect_cdrom();
182
183 /* goeing into main-loop */
184 while(1) {
185 /* try to mount the cdrom and exit on error */
186 if(mount_cdrom() != EXIT_SUCCESS)
187 break;
188
189 /* go to cdrom mount-point */
190 chdir(TARGET);
191
192 /* is this a valid Debian cdrom? */
193 if(valid_cdrom() != EXIT_SUCCESS) {
194 debconf_fset(debconf, "cdrom-checker/wrongcd", "seen", "false");
195 debconf_set(debconf, "cdrom-checker/wrongcd", "false");
196 debconf_input(debconf, "high", "cdrom-checker/wrongcd");
197 debconf_go(debconf);
198 break;
199 }
200
201 check_cdrom();
202
203 /* make sure cdrom isn't bussy anymore, then umount it */
204 chdir("/");
205 di_exec_shell_log("umount /cdrom");
206
207 debconf_fset(debconf, "cdrom-checker/nextcd", "seen", "false");
208 debconf_set(debconf, "cdrom-checker/nextcd", "false");
209 debconf_input(debconf, "high", "cdrom-checker/nextcd");
210 debconf_go(debconf);
211 debconf_get(debconf,"cdrom-checker/nextcd");
212 if(!strstr(debconf->value, "true"))
213 break;
214 }
215
216 debconf_fset(debconf,"cdrom-checker/firstcd", "seen", "false");
217 debconf_set(debconf, "cdrom-checker/firstcd", "false");
218 debconf_input(debconf, "high", "cdrom-checker/firstcd");
219 debconf_go(debconf);
220 di_exec_shell_log("udpkg --force-configure --configure cdrom-detect");
221
222 return(EXIT_SUCCESS);
223 }
224

  ViewVC Help
Powered by ViewVC 1.1.5