1 | /* libmondo-mountlist.c subroutines for handling mountlist |
---|
2 | $Id: libmondo-mountlist.c 1909 2008-04-14 11:18:58Z bruno $ |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | 08/01 |
---|
7 | - when evaluating mountlist, skip drive entirely if it does not exist |
---|
8 | |
---|
9 | 07/14 |
---|
10 | - always exclude /devpts, /proc, /sys from mountlist |
---|
11 | |
---|
12 | 06/29/2004 |
---|
13 | - changed some char[] to *char |
---|
14 | - drivelist is struct now, not char[][] |
---|
15 | |
---|
16 | 10/19/2003 |
---|
17 | - format_device() --- contract /dev/md/N to /dev/mdN to |
---|
18 | overcome devfs problems |
---|
19 | |
---|
20 | 06/11 |
---|
21 | - added support for 5th column in mountlist.txt, for labels |
---|
22 | |
---|
23 | 05/08 |
---|
24 | - added save_mountlist_to_disk() and load_mountlist() |
---|
25 | and misc other funcs from mondorestore.c |
---|
26 | |
---|
27 | 05/05 |
---|
28 | - added Joshua Oreman's FreeBSD patches |
---|
29 | |
---|
30 | 04/24 |
---|
31 | - added some assert()'s |
---|
32 | |
---|
33 | 04/23 |
---|
34 | - cleaned up evaluate_drive_within_mountlist() a bit |
---|
35 | |
---|
36 | 01/15/2003 |
---|
37 | - added code for LVM (Brian Borgeson) |
---|
38 | |
---|
39 | 10/19/2002 |
---|
40 | - added some comments |
---|
41 | |
---|
42 | 07/24/2002 |
---|
43 | - created |
---|
44 | */ |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | * @file |
---|
49 | * Functions which manipulate the mountlist. |
---|
50 | */ |
---|
51 | |
---|
52 | #include "my-stuff.h" |
---|
53 | #include "mondostructures.h" |
---|
54 | #include "libmondo-mountlist.h" |
---|
55 | #include "lib-common-externs.h" |
---|
56 | #include "libmondo-raid-EXT.h" |
---|
57 | #include "libmondo-devices-EXT.h" |
---|
58 | #include "libmondo-tools-EXT.h" |
---|
59 | #include "libmondo-string-EXT.h" |
---|
60 | #include "newt-specific-EXT.h" |
---|
61 | |
---|
62 | /*@unused@*/ |
---|
63 | //static char cvsid[] = "$Id: libmondo-mountlist.c 1909 2008-04-14 11:18:58Z bruno $"; |
---|
64 | |
---|
65 | /* Reference to global bkpinfo */ |
---|
66 | extern struct s_bkpinfo *bkpinfo; |
---|
67 | |
---|
68 | /** |
---|
69 | * @addtogroup mountlistGroup |
---|
70 | * @{ |
---|
71 | */ |
---|
72 | /** |
---|
73 | * Evaluate a drive within the mountlist for flaws. For example, too many |
---|
74 | * primary partitions, the first logical isn't 5, duplicate partitions, |
---|
75 | * ovar-allocated or under-allocated, unsupported format, silly size, or |
---|
76 | * silly mountpoint. Under FreeBSD, this checks the disklabel too, for the above-mentioned |
---|
77 | * errors as well as too many BSD partitions (more than 'h'). |
---|
78 | * @param mountlist The mountlist to check. |
---|
79 | * @param drive The drive to check (e.g. @c /dev/hda). |
---|
80 | * @param flaws_str Where to put the found flaws (human-readable). |
---|
81 | * @return The number of flaws found (0 for success). |
---|
82 | * @see evaluate_mountlist |
---|
83 | */ |
---|
84 | int evaluate_drive_within_mountlist(struct mountlist_itself *mountlist, |
---|
85 | char *drive, char *flaws_str) |
---|
86 | #ifdef __FreeBSD__ |
---|
87 | { |
---|
88 | // FreeBSD-specific version of evaluate_drive_within_mountlist() |
---|
89 | /*@ int ************************************************************* */ |
---|
90 | int prev_part_no = 0; |
---|
91 | int curr_part_no = 0; |
---|
92 | int pos = 0, npos = 0; |
---|
93 | int res = 0; |
---|
94 | int mountpoint_copies = 0; |
---|
95 | int device_copies = 0; |
---|
96 | int i = 0; |
---|
97 | int cur_sp_no = 0; |
---|
98 | int prev_sp_no = 0; |
---|
99 | int foundsome = FALSE; |
---|
100 | |
---|
101 | /*@ buffers ******************************************************** */ |
---|
102 | char tmp[MAX_STR_LEN]; |
---|
103 | char device[MAX_STR_LEN]; |
---|
104 | char mountpoint[MAX_STR_LEN]; |
---|
105 | |
---|
106 | /*@ long *********************************************************** */ |
---|
107 | long physical_drive_size = 0; |
---|
108 | long amount_allocated = 0; |
---|
109 | |
---|
110 | /*@ pointers ******************************************************* */ |
---|
111 | char *part_table_fmt; |
---|
112 | |
---|
113 | /*@ initialize ***************************************************** */ |
---|
114 | flaws_str[0] = '\0'; |
---|
115 | prev_part_no = 0; |
---|
116 | tmp[0] = '\0'; |
---|
117 | |
---|
118 | |
---|
119 | physical_drive_size = get_phys_size_of_drive(drive); |
---|
120 | |
---|
121 | if (physical_drive_size < 0) { |
---|
122 | sprintf(tmp, " %s does not exist.", drive); |
---|
123 | strcat(flaws_str, tmp); |
---|
124 | } else { |
---|
125 | sprintf(tmp, "%s is %ld MB", drive, physical_drive_size); |
---|
126 | } |
---|
127 | log_it(tmp); |
---|
128 | |
---|
129 | |
---|
130 | /* check DD */ |
---|
131 | for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) { |
---|
132 | sprintf(device, "%s%c", drive, cur_sp_no); |
---|
133 | if (find_device_in_mountlist(mountlist, device) >= 0) |
---|
134 | foundsome = TRUE; |
---|
135 | } |
---|
136 | if (foundsome) { |
---|
137 | for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) { |
---|
138 | sprintf(device, "%s%c", drive, cur_sp_no); |
---|
139 | pos = find_device_in_mountlist(mountlist, device); |
---|
140 | if (pos < 0) { |
---|
141 | continue; |
---|
142 | } |
---|
143 | strcpy(mountpoint, mountlist->el[pos].mountpoint); |
---|
144 | /* is it too big? */ |
---|
145 | if (curr_part_no > 'h') { |
---|
146 | sprintf(tmp, " Can only have up to 'h' in disklabel."); |
---|
147 | log_it(tmp); |
---|
148 | strcat(flaws_str, tmp); |
---|
149 | res++; |
---|
150 | } |
---|
151 | /* does partition /dev/adXsYZ exist more than once in the mountlist? */ |
---|
152 | for (i = 0, mountpoint_copies = 0, device_copies = 0; |
---|
153 | i < mountlist->entries; i++) { |
---|
154 | if (!strcmp(device, mountlist->el[i].device)) { |
---|
155 | device_copies++; |
---|
156 | } |
---|
157 | } |
---|
158 | if (device_copies > 1) { |
---|
159 | sprintf(tmp, " %s %s's.", number_to_text(device_copies), |
---|
160 | device); |
---|
161 | if (!strstr(flaws_str, tmp)) { |
---|
162 | log_it(tmp); |
---|
163 | strcat(flaws_str, tmp); |
---|
164 | res++; |
---|
165 | } |
---|
166 | } |
---|
167 | /* silly partition size? */ |
---|
168 | if (mountlist->el[pos].size < 8192 |
---|
169 | && strcmp(mountlist->el[pos].mountpoint, "lvm")) { |
---|
170 | sprintf(tmp, " %s is tiny!", device); |
---|
171 | log_it(tmp); |
---|
172 | strcat(flaws_str, tmp); |
---|
173 | res++; |
---|
174 | } |
---|
175 | /* mountpoint should begin with / unless it is swap, lvm or raid */ |
---|
176 | if (strcmp(mountlist->el[pos].mountpoint, "swap") |
---|
177 | && strcmp(mountlist->el[pos].mountpoint, "lvm") |
---|
178 | && strcmp(mountlist->el[pos].mountpoint, "raid") |
---|
179 | && strcmp(mountlist->el[pos].mountpoint, "image") |
---|
180 | && strcmp(mountlist->el[pos].mountpoint, "none") |
---|
181 | && mountlist->el[pos].mountpoint[0] != '/') { |
---|
182 | sprintf(tmp, " %s has a weird mountpoint.", device); |
---|
183 | log_it(tmp); |
---|
184 | strcat(flaws_str, tmp); |
---|
185 | res++; |
---|
186 | } |
---|
187 | /* is format sensible? */ |
---|
188 | if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) { |
---|
189 | sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format); |
---|
190 | log_it(tmp); |
---|
191 | strcat(flaws_str, tmp); |
---|
192 | res++; |
---|
193 | } |
---|
194 | amount_allocated += mountlist->el[pos].size / 1024; |
---|
195 | prev_sp_no = cur_sp_no; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | npos = pos = 0; |
---|
200 | for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) { |
---|
201 | build_partition_name(device, drive, curr_part_no); |
---|
202 | pos = find_device_in_mountlist(mountlist, device); |
---|
203 | npos = 0; |
---|
204 | for (cur_sp_no = 'a'; cur_sp_no <= 'h'; cur_sp_no++) { |
---|
205 | sprintf(device, "%ss%i%c", device, curr_part_no, cur_sp_no); |
---|
206 | if (find_device_in_mountlist(mountlist, device) >= 0) |
---|
207 | npos++; |
---|
208 | } |
---|
209 | if (((pos >= 0) || npos) && foundsome) { |
---|
210 | sprintf(flaws_str + strlen(flaws_str), |
---|
211 | " %s has both DD and PC-style partitions.", drive); |
---|
212 | return ++res; // fatal error |
---|
213 | } |
---|
214 | |
---|
215 | build_partition_name(device, drive, curr_part_no); |
---|
216 | strcpy(mountpoint, mountlist->el[pos].mountpoint); |
---|
217 | if (pos > 0 && !npos) { |
---|
218 | /* gap in the partition list? */ |
---|
219 | if (curr_part_no - prev_part_no > 1) { |
---|
220 | if (prev_part_no == 0) { |
---|
221 | sprintf(tmp, " Gap prior to %s.", device); |
---|
222 | log_it(tmp); |
---|
223 | strcat(flaws_str, tmp); |
---|
224 | res++; |
---|
225 | } else if (curr_part_no > 5 |
---|
226 | || (curr_part_no <= 4 && prev_part_no > 0)) { |
---|
227 | sprintf(tmp, " Gap between %ss%d and %d.", drive, |
---|
228 | prev_part_no, curr_part_no); |
---|
229 | log_it(tmp); |
---|
230 | strcat(flaws_str, tmp); |
---|
231 | res++; |
---|
232 | } |
---|
233 | } |
---|
234 | /* GPT allows more than 4 primary partitions */ |
---|
235 | part_table_fmt = which_partition_format(drive); |
---|
236 | /* no spare primary partitions to help accommodate the logical(s)? */ |
---|
237 | if ((curr_part_no >= 5 && prev_part_no == 4) |
---|
238 | && (strcmp(part_table_fmt, "MBR") == 0)) { |
---|
239 | sprintf(tmp, " Partition %ss4 is occupied.", drive); |
---|
240 | log_it(tmp); |
---|
241 | strcat(flaws_str, tmp); |
---|
242 | res++; |
---|
243 | } |
---|
244 | /* does partition /dev/adXsY exist more than once in the mountlist? */ |
---|
245 | for (i = 0, mountpoint_copies = 0, device_copies = 0; |
---|
246 | i < mountlist->entries; i++) { |
---|
247 | if (!strcmp(device, mountlist->el[i].device)) { |
---|
248 | device_copies++; |
---|
249 | } |
---|
250 | } |
---|
251 | if (device_copies > 1) { |
---|
252 | sprintf(tmp, " %s %s's.", number_to_text(device_copies), |
---|
253 | device); |
---|
254 | if (!strstr(flaws_str, tmp)) { |
---|
255 | log_it(tmp); |
---|
256 | strcat(flaws_str, tmp); |
---|
257 | res++; |
---|
258 | } |
---|
259 | } |
---|
260 | /* silly partition size? */ |
---|
261 | if (mountlist->el[pos].size < 8192 |
---|
262 | && strcmp(mountlist->el[pos].mountpoint, "lvm")) { |
---|
263 | sprintf(tmp, " %s is tiny!", device); |
---|
264 | log_it(tmp); |
---|
265 | strcat(flaws_str, tmp); |
---|
266 | res++; |
---|
267 | } |
---|
268 | /* mountpoint should begin with / unless it is swap, lvm or raid */ |
---|
269 | if (strcmp(mountlist->el[pos].mountpoint, "swap") |
---|
270 | && strcmp(mountlist->el[pos].mountpoint, "lvm") |
---|
271 | && strcmp(mountlist->el[pos].mountpoint, "raid") |
---|
272 | && strcmp(mountlist->el[pos].mountpoint, "image") |
---|
273 | && strcmp(mountlist->el[pos].mountpoint, "none") |
---|
274 | && mountlist->el[pos].mountpoint[0] != '/') { |
---|
275 | sprintf(tmp, " %s has a weird mountpoint.", device); |
---|
276 | log_it(tmp); |
---|
277 | strcat(flaws_str, tmp); |
---|
278 | res++; |
---|
279 | } |
---|
280 | /* is format sensible? */ |
---|
281 | if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) { |
---|
282 | sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format); |
---|
283 | log_it(tmp); |
---|
284 | strcat(flaws_str, tmp); |
---|
285 | res++; |
---|
286 | } |
---|
287 | } else { |
---|
288 | /* Check subpartitions */ |
---|
289 | for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) { |
---|
290 | sprintf(device, "%ss%d%c", drive, curr_part_no, cur_sp_no); |
---|
291 | pos = find_device_in_mountlist(mountlist, device); |
---|
292 | if (pos < 0) { |
---|
293 | continue; |
---|
294 | } |
---|
295 | strcpy(mountpoint, mountlist->el[pos].mountpoint); |
---|
296 | /* is it too big? */ |
---|
297 | if (curr_part_no > 'h') { |
---|
298 | sprintf(tmp, " Can only have up to 'h' in disklabel."); |
---|
299 | log_it(tmp); |
---|
300 | strcat(flaws_str, tmp); |
---|
301 | res++; |
---|
302 | } |
---|
303 | /* does partition /dev/adXsYZ exist more than once in the mountlist? */ |
---|
304 | for (i = 0, mountpoint_copies = 0, device_copies = 0; |
---|
305 | i < mountlist->entries; i++) { |
---|
306 | if (!strcmp(device, mountlist->el[i].device)) { |
---|
307 | device_copies++; |
---|
308 | } |
---|
309 | } |
---|
310 | if (device_copies > 1) { |
---|
311 | sprintf(tmp, " %s %s's.", |
---|
312 | number_to_text(device_copies), device); |
---|
313 | if (!strstr(flaws_str, tmp)) { |
---|
314 | log_it(tmp); |
---|
315 | strcat(flaws_str, tmp); |
---|
316 | res++; |
---|
317 | } |
---|
318 | } |
---|
319 | /* silly partition size? */ |
---|
320 | if (mountlist->el[pos].size < 8192 |
---|
321 | && strcmp(mountlist->el[pos].mountpoint, "lvm")) { |
---|
322 | sprintf(tmp, " %s is tiny!", device); |
---|
323 | log_it(tmp); |
---|
324 | strcat(flaws_str, tmp); |
---|
325 | res++; |
---|
326 | } |
---|
327 | /* mountpoint should begin with / unless it is swap, lvm or raid */ |
---|
328 | if (strcmp(mountlist->el[pos].mountpoint, "swap") |
---|
329 | && strcmp(mountlist->el[pos].mountpoint, "lvm") |
---|
330 | && strcmp(mountlist->el[pos].mountpoint, "raid") |
---|
331 | && strcmp(mountlist->el[pos].mountpoint, "image") |
---|
332 | && strcmp(mountlist->el[pos].mountpoint, "none") |
---|
333 | && mountlist->el[pos].mountpoint[0] != '/') { |
---|
334 | sprintf(tmp, " %s has a weird mountpoint.", device); |
---|
335 | log_it(tmp); |
---|
336 | strcat(flaws_str, tmp); |
---|
337 | res++; |
---|
338 | } |
---|
339 | /* is format sensible? */ |
---|
340 | if (!is_this_a_valid_disk_format |
---|
341 | (mountlist->el[pos].format)) { |
---|
342 | sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format); |
---|
343 | log_it(tmp); |
---|
344 | strcat(flaws_str, tmp); |
---|
345 | res++; |
---|
346 | } |
---|
347 | amount_allocated += mountlist->el[pos].size / 1024; |
---|
348 | prev_sp_no = cur_sp_no; |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | /* OK, continue with main loop */ |
---|
353 | amount_allocated += mountlist->el[pos].size / 1024; |
---|
354 | prev_part_no = curr_part_no; |
---|
355 | } |
---|
356 | |
---|
357 | /* Over-allocated the disk? Unallocated space on disk? */ |
---|
358 | if (amount_allocated > physical_drive_size) // Used to be +1, but what if you're 1 MB too high? |
---|
359 | { |
---|
360 | sprintf(tmp, " %ld MB over-allocated on %s.", |
---|
361 | amount_allocated - physical_drive_size, drive); |
---|
362 | log_it(tmp); |
---|
363 | strcat(flaws_str, tmp); |
---|
364 | res++; |
---|
365 | } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */ |
---|
366 | sprintf(tmp, " %ld MB unallocated on %s.", |
---|
367 | physical_drive_size - amount_allocated, drive); |
---|
368 | log_it(tmp); |
---|
369 | strcat(flaws_str, tmp); |
---|
370 | } |
---|
371 | if (res) { |
---|
372 | return (FALSE); |
---|
373 | } else { |
---|
374 | return (TRUE); |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | #else |
---|
379 | // Linux-specific version of evaluate_drive_within_mountlist() |
---|
380 | { |
---|
381 | |
---|
382 | /*@ int ************************************************************* */ |
---|
383 | int prev_part_no = 0; |
---|
384 | int curr_part_no = 0; |
---|
385 | int pos = 0; |
---|
386 | int res = 0; |
---|
387 | int mountpoint_copies = 0; |
---|
388 | int device_copies = 0; |
---|
389 | int i = 0; |
---|
390 | |
---|
391 | /*@ buffers ******************************************************** */ |
---|
392 | char *tmp; |
---|
393 | char *device; |
---|
394 | |
---|
395 | /*@ long *********************************************************** */ |
---|
396 | long physical_drive_size = 0L; |
---|
397 | long amount_allocated = 0L; |
---|
398 | |
---|
399 | /*@ pointers ******************************************************* */ |
---|
400 | char *part_table_fmt; |
---|
401 | |
---|
402 | /*@ initialize ***************************************************** */ |
---|
403 | assert_string_is_neither_NULL_nor_zerolength(drive); |
---|
404 | assert(mountlist != NULL); |
---|
405 | assert(flaws_str != NULL); |
---|
406 | |
---|
407 | malloc_string(tmp); |
---|
408 | malloc_string(device); |
---|
409 | flaws_str[0] = '\0'; |
---|
410 | prev_part_no = 0; |
---|
411 | tmp[0] = '\0'; |
---|
412 | |
---|
413 | |
---|
414 | physical_drive_size = get_phys_size_of_drive(drive); |
---|
415 | |
---|
416 | if (physical_drive_size < 0) { |
---|
417 | sprintf(tmp, " %s does not exist.", drive); |
---|
418 | strcat(flaws_str, tmp); |
---|
419 | res++; |
---|
420 | log_msg(1, tmp); |
---|
421 | goto endoffunc; |
---|
422 | } else { |
---|
423 | sprintf(tmp, "%s is %ld MB", drive, physical_drive_size); |
---|
424 | log_it(tmp); |
---|
425 | } |
---|
426 | |
---|
427 | for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) { |
---|
428 | build_partition_name(device, drive, curr_part_no); |
---|
429 | pos = find_device_in_mountlist(mountlist, device); |
---|
430 | if (pos < 0) { |
---|
431 | continue; |
---|
432 | } |
---|
433 | /* gap in the partition list? */ |
---|
434 | if (curr_part_no - prev_part_no > 1) { |
---|
435 | if (prev_part_no == 0) { |
---|
436 | sprintf(tmp, " Gap prior to %s.", device); |
---|
437 | log_it(tmp); |
---|
438 | strcat(flaws_str, tmp); |
---|
439 | res++; |
---|
440 | } else if (curr_part_no > 5 |
---|
441 | || (curr_part_no <= 4 && prev_part_no > 0)) { |
---|
442 | sprintf(tmp, " Gap on %s between %d and %d.", drive, |
---|
443 | prev_part_no, curr_part_no); |
---|
444 | log_it(tmp); |
---|
445 | strcat(flaws_str, tmp); |
---|
446 | res++; |
---|
447 | } |
---|
448 | } |
---|
449 | /* GPT allows more than 4 primary partitions */ |
---|
450 | part_table_fmt = which_partition_format(drive); |
---|
451 | /* no spare primary partitions to help accommodate the logical(s)? */ |
---|
452 | if ((curr_part_no >= 5 && prev_part_no == 4) |
---|
453 | && (strcmp(part_table_fmt, "MBR") == 0)) { |
---|
454 | sprintf(tmp, " Partition 4 of %s is occupied.", drive); |
---|
455 | log_it(tmp); |
---|
456 | strcat(flaws_str, tmp); |
---|
457 | res++; |
---|
458 | } |
---|
459 | /* does partition /dev/hdNX exist more than once in the mountlist? */ |
---|
460 | for (i = 0, mountpoint_copies = 0, device_copies = 0; |
---|
461 | i < mountlist->entries; i++) { |
---|
462 | if (!strcmp(device, mountlist->el[i].device)) { |
---|
463 | device_copies++; |
---|
464 | } |
---|
465 | } |
---|
466 | if (device_copies > 1) { |
---|
467 | sprintf(tmp, " %s %s's.", number_to_text(device_copies), |
---|
468 | device); |
---|
469 | if (!strstr(flaws_str, tmp)) { |
---|
470 | log_it(tmp); |
---|
471 | strcat(flaws_str, tmp); |
---|
472 | res++; |
---|
473 | } |
---|
474 | } |
---|
475 | /* silly partition size? */ |
---|
476 | if (mountlist->el[pos].size < 8192 |
---|
477 | && strcmp(mountlist->el[pos].mountpoint, "lvm")) { |
---|
478 | sprintf(tmp, " %s is tiny!", device); |
---|
479 | log_it(tmp); |
---|
480 | strcat(flaws_str, tmp); |
---|
481 | res++; |
---|
482 | } |
---|
483 | /* mountpoint should begin with / unless it is swap, lvm or raid */ |
---|
484 | if (strcmp(mountlist->el[pos].mountpoint, "swap") |
---|
485 | && strcmp(mountlist->el[pos].mountpoint, "lvm") |
---|
486 | && strcmp(mountlist->el[pos].mountpoint, "raid") |
---|
487 | && strcmp(mountlist->el[pos].mountpoint, "image") |
---|
488 | && mountlist->el[pos].mountpoint[0] != '/') { |
---|
489 | sprintf(tmp, " %s has a weird mountpoint.", device); |
---|
490 | log_it(tmp); |
---|
491 | strcat(flaws_str, tmp); |
---|
492 | res++; |
---|
493 | } |
---|
494 | /* is format sensible? */ |
---|
495 | if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) { |
---|
496 | sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format); |
---|
497 | log_it(tmp); |
---|
498 | strcat(flaws_str, tmp); |
---|
499 | res++; |
---|
500 | } |
---|
501 | /* OK, continue with main loop */ |
---|
502 | amount_allocated += mountlist->el[pos].size / 1024; |
---|
503 | prev_part_no = curr_part_no; |
---|
504 | } |
---|
505 | |
---|
506 | /* Over-allocated the disk? Unallocated space on disk? */ |
---|
507 | if (amount_allocated > physical_drive_size + 1) { |
---|
508 | sprintf(tmp, " %ld MB over-allocated on %s.", |
---|
509 | amount_allocated - physical_drive_size, drive); |
---|
510 | log_it(tmp); |
---|
511 | strcat(flaws_str, tmp); |
---|
512 | res++; |
---|
513 | } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */ |
---|
514 | sprintf(tmp, " %ld MB unallocated on %s.", |
---|
515 | physical_drive_size - amount_allocated, drive); |
---|
516 | log_it(tmp); |
---|
517 | strcat(flaws_str, tmp); |
---|
518 | } |
---|
519 | |
---|
520 | endoffunc: |
---|
521 | paranoid_free(tmp); |
---|
522 | paranoid_free(device); |
---|
523 | |
---|
524 | if (res) { |
---|
525 | return (FALSE); |
---|
526 | } else { |
---|
527 | return (TRUE); |
---|
528 | } |
---|
529 | } |
---|
530 | #endif |
---|
531 | |
---|
532 | |
---|
533 | |
---|
534 | /** |
---|
535 | * Evaluate a whole mountlist for flaws. Calls evaluate_drive_within_mountlist() |
---|
536 | * for each drive, and then spreads the flaws across three lines. |
---|
537 | * @param mountlist The mountlist to evaluate. |
---|
538 | * @param flaws_str_A Where to put the first line listing errors found. |
---|
539 | * @param flaws_str_B Where to put the second line listing errors found. |
---|
540 | * @param flaws_str_C Where to put the third line listing errors found. |
---|
541 | * @return The number of flaws found (0 for success). |
---|
542 | * @see evaluate_drive_within_mountlist |
---|
543 | */ |
---|
544 | int |
---|
545 | evaluate_mountlist(struct mountlist_itself *mountlist, char *flaws_str_A, |
---|
546 | char *flaws_str_B, char *flaws_str_C) |
---|
547 | { |
---|
548 | |
---|
549 | /*@ buffer *********************************************************** */ |
---|
550 | struct list_of_disks *drivelist; |
---|
551 | char *tmp; |
---|
552 | char *flaws_str; |
---|
553 | |
---|
554 | /*@ int ************************************************************** */ |
---|
555 | int i = 0; |
---|
556 | int res = 0; |
---|
557 | |
---|
558 | /*@ initialize ******************************************************* */ |
---|
559 | |
---|
560 | drivelist = malloc(sizeof(struct list_of_disks)); |
---|
561 | malloc_string(tmp); |
---|
562 | malloc_string(flaws_str); |
---|
563 | assert(mountlist != NULL); |
---|
564 | assert(flaws_str_A != NULL); |
---|
565 | assert(flaws_str_B != NULL); |
---|
566 | assert(flaws_str_C != NULL); |
---|
567 | flaws_str[0] = '\0'; |
---|
568 | |
---|
569 | make_list_of_drives_in_mountlist(mountlist, drivelist); |
---|
570 | |
---|
571 | log_it("Evaluating mountlist..."); |
---|
572 | |
---|
573 | for (i = 0; i < drivelist->entries; i++) { |
---|
574 | if (strstr |
---|
575 | (drivelist->el[i].device, |
---|
576 | DONT_KNOW_HOW_TO_EVALUATE_THIS_DEVICE_TYPE)) { |
---|
577 | sprintf(tmp, " Not evaluating %s (I don't know how yet)", |
---|
578 | drivelist->el[i].device); |
---|
579 | log_it(tmp); |
---|
580 | tmp[0] = '\0'; |
---|
581 | } else { |
---|
582 | if (!evaluate_drive_within_mountlist |
---|
583 | (mountlist, drivelist->el[i].device, tmp)) { |
---|
584 | res++; |
---|
585 | } |
---|
586 | } |
---|
587 | strcat(flaws_str, tmp); |
---|
588 | } |
---|
589 | res += look_for_duplicate_mountpoints(mountlist, flaws_str); |
---|
590 | /* res+=look_for_weird_formats(mountlist,flaws_str); .. not necessary, now that we can check to see |
---|
591 | which formarts are actually _supported_ by the kernel */ |
---|
592 | /* log_it(flaws_str); */ |
---|
593 | return (spread_flaws_across_three_lines |
---|
594 | (flaws_str, flaws_str_A, flaws_str_B, flaws_str_C, res)); |
---|
595 | } |
---|
596 | |
---|
597 | |
---|
598 | /** |
---|
599 | * Find the index number of @p device in the mountlist. |
---|
600 | * The device given must match @p mountlist->el[N].device exactly, case-sensitive. |
---|
601 | * @param mountlist The mountlist to search in. |
---|
602 | * @param device The device to search for. |
---|
603 | * @return The zero-based index of the device, or -1 if it could not be found. |
---|
604 | */ |
---|
605 | int |
---|
606 | find_device_in_mountlist(struct mountlist_itself *mountlist, char *device) |
---|
607 | { |
---|
608 | |
---|
609 | /*@ int ************************************************************** */ |
---|
610 | int i = 0; |
---|
611 | char *tmp; |
---|
612 | char *flaws_str; |
---|
613 | |
---|
614 | malloc_string(tmp); |
---|
615 | malloc_string(flaws_str); |
---|
616 | |
---|
617 | assert(mountlist != NULL); |
---|
618 | assert_string_is_neither_NULL_nor_zerolength(device); |
---|
619 | for (i = 0; |
---|
620 | i < mountlist->entries |
---|
621 | && strcmp(mountlist->el[i].device, device) != 0; i++); |
---|
622 | |
---|
623 | paranoid_free(tmp); |
---|
624 | paranoid_free(flaws_str); |
---|
625 | |
---|
626 | if (i == mountlist->entries) { |
---|
627 | return (-1); |
---|
628 | } else { |
---|
629 | return (i); |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | |
---|
634 | |
---|
635 | |
---|
636 | |
---|
637 | /** |
---|
638 | * Look for duplicate mountpoints in @p mountlist. |
---|
639 | * @param mountlist The mountlist to check. |
---|
640 | * @param flaws_str The flaws string to append the results to. |
---|
641 | * @return The number of mountpoints that have duplicates, or 0 for success. |
---|
642 | */ |
---|
643 | int |
---|
644 | look_for_duplicate_mountpoints(struct mountlist_itself *mountlist, |
---|
645 | char *flaws_str) |
---|
646 | { |
---|
647 | |
---|
648 | /*@ int ************************************************************* */ |
---|
649 | int res = 0; |
---|
650 | int currline = 0; |
---|
651 | int i = 0; |
---|
652 | int copies = 0; |
---|
653 | int last_copy = 0; |
---|
654 | |
---|
655 | /*@ buffetr ********************************************************* */ |
---|
656 | char *curr_mountpoint; |
---|
657 | char *tmp; |
---|
658 | |
---|
659 | malloc_string(curr_mountpoint); |
---|
660 | malloc_string(tmp); |
---|
661 | assert(mountlist != NULL); |
---|
662 | assert(flaws_str != NULL); |
---|
663 | for (currline = 0; currline < mountlist->entries; currline++) { |
---|
664 | strcpy(curr_mountpoint, mountlist->el[currline].mountpoint); |
---|
665 | for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries; |
---|
666 | i++) { |
---|
667 | if (!strcmp(mountlist->el[i].mountpoint, curr_mountpoint) |
---|
668 | && strcmp(mountlist->el[i].mountpoint, "lvm") |
---|
669 | && strcmp(mountlist->el[i].mountpoint, "swap")) { |
---|
670 | last_copy = i; |
---|
671 | copies++; |
---|
672 | } |
---|
673 | } |
---|
674 | if (copies > 1 && last_copy == currline |
---|
675 | && strcmp(curr_mountpoint, "raid")) { |
---|
676 | sprintf(tmp, " %s %s's.", number_to_text(copies), |
---|
677 | curr_mountpoint); |
---|
678 | strcat(flaws_str, tmp); |
---|
679 | log_it(tmp); |
---|
680 | res++; |
---|
681 | } |
---|
682 | } |
---|
683 | paranoid_free(curr_mountpoint); |
---|
684 | paranoid_free(tmp); |
---|
685 | return (res); |
---|
686 | } |
---|
687 | |
---|
688 | /** |
---|
689 | * Look for strange formats. Does not respect /proc/filesystems. |
---|
690 | * @param mountlist The mountlist to check. |
---|
691 | * @param flaws_str The flaws string to append the results to. |
---|
692 | * @return The number of weird formats found, or 0 for success. |
---|
693 | * @bug Seems orphaned; please remove. |
---|
694 | */ |
---|
695 | int |
---|
696 | look_for_weird_formats(struct mountlist_itself *mountlist, char *flaws_str) |
---|
697 | { |
---|
698 | |
---|
699 | /*@ int ************************************************************* */ |
---|
700 | int i = 0; |
---|
701 | int res = 0; |
---|
702 | |
---|
703 | /*@ buffers ********************************************************* */ |
---|
704 | char *tmp; |
---|
705 | char *format_sz; |
---|
706 | |
---|
707 | malloc_string(tmp); |
---|
708 | malloc_string(format_sz); |
---|
709 | |
---|
710 | assert(mountlist != NULL); |
---|
711 | assert(flaws_str != NULL); |
---|
712 | |
---|
713 | for (i = 0; i < mountlist->entries; i++) { |
---|
714 | sprintf(format_sz, " %s ", mountlist->el[i].format); |
---|
715 | if (!strstr(SANE_FORMATS, format_sz) |
---|
716 | && strcmp(mountlist->el[i].mountpoint, "image") != 0) { |
---|
717 | sprintf(tmp, " %s has unknown format.", |
---|
718 | mountlist->el[i].device); |
---|
719 | log_it(tmp); |
---|
720 | strcat(flaws_str, tmp); |
---|
721 | res++; |
---|
722 | } else if ((!strcmp(mountlist->el[i].format, "swap") |
---|
723 | && strcmp(mountlist->el[i].mountpoint, "swap") |
---|
724 | && strcmp(mountlist->el[i].mountpoint, "none")) |
---|
725 | || (strcmp(mountlist->el[i].format, "swap") |
---|
726 | && !strcmp(mountlist->el[i].mountpoint, "swap") |
---|
727 | && !strcmp(mountlist->el[i].mountpoint, "none"))) { |
---|
728 | sprintf(tmp, " %s is half-swap.", mountlist->el[i].device); |
---|
729 | log_it(tmp); |
---|
730 | strcat(flaws_str, tmp); |
---|
731 | res++; |
---|
732 | } |
---|
733 | } |
---|
734 | paranoid_free(tmp); |
---|
735 | paranoid_free(format_sz); |
---|
736 | return (res); |
---|
737 | } |
---|
738 | |
---|
739 | |
---|
740 | |
---|
741 | /** |
---|
742 | * Make a list of the drives mentioned in the mountlist. |
---|
743 | * @param mountlist The mountlist to examine. |
---|
744 | * @param drivelist Where to put the list of drives found. |
---|
745 | * @return The number of physical (non-RAID non-LVM) drives found, or \<= 0 for error. |
---|
746 | */ |
---|
747 | int |
---|
748 | make_list_of_drives_in_mountlist(struct mountlist_itself *mountlist, |
---|
749 | struct list_of_disks *drivelist) |
---|
750 | { |
---|
751 | |
---|
752 | /*@ int ************************************************************* */ |
---|
753 | int lino; |
---|
754 | int noof_drives; |
---|
755 | int j; |
---|
756 | |
---|
757 | /*@ buffers ********************************************************* */ |
---|
758 | char *drive; |
---|
759 | char *tmp; |
---|
760 | |
---|
761 | long long size; |
---|
762 | |
---|
763 | malloc_string(drive); |
---|
764 | malloc_string(tmp); |
---|
765 | assert(mountlist != NULL); |
---|
766 | assert(drivelist != NULL); |
---|
767 | log_it("Making list of drives"); |
---|
768 | for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) { |
---|
769 | |
---|
770 | strcpy(drive, mountlist->el[lino].device); |
---|
771 | if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) { |
---|
772 | sprintf(tmp, |
---|
773 | "Not putting %s in list of drives: it's a virtual drive", |
---|
774 | drive); |
---|
775 | log_msg(8, tmp); |
---|
776 | continue; |
---|
777 | } |
---|
778 | |
---|
779 | size = mountlist->el[lino].size; |
---|
780 | if (size == 0) { |
---|
781 | sprintf(tmp, |
---|
782 | "Not putting %s in list of drives: it has zero size (maybe an LVM volume)", |
---|
783 | drive); |
---|
784 | log_msg(8, tmp); |
---|
785 | continue; |
---|
786 | } |
---|
787 | |
---|
788 | /* |
---|
789 | for (i = strlen (drive); isdigit (drive[i - 1]); i--); |
---|
790 | drive[i] = '\0'; |
---|
791 | if (get_phys_size_of_drive (drive) <= 0 && drive[i - 1] == 'p') |
---|
792 | { |
---|
793 | i--; |
---|
794 | drive[i] = '\0'; |
---|
795 | } |
---|
796 | for (j = 0; j < noof_drives && strcmp (drivelist[j], drive) != 0; j++); |
---|
797 | */ |
---|
798 | |
---|
799 | sprintf(tmp, |
---|
800 | "Putting %s with size %lli in list of drives", |
---|
801 | drive, size); |
---|
802 | log_msg(8, tmp); |
---|
803 | |
---|
804 | (void) truncate_to_drive_name(drive); |
---|
805 | for (j = 0; |
---|
806 | j < noof_drives |
---|
807 | && strcmp(drivelist->el[j].device, drive) != 0; j++) |
---|
808 | continue; |
---|
809 | if (j == noof_drives) { |
---|
810 | strcpy(drivelist->el[noof_drives++].device, drive); |
---|
811 | } |
---|
812 | } |
---|
813 | drivelist->entries = noof_drives; |
---|
814 | log_msg(8, "Made list of drives"); |
---|
815 | paranoid_free(drive); |
---|
816 | paranoid_free(tmp); |
---|
817 | |
---|
818 | return (noof_drives); |
---|
819 | } |
---|
820 | |
---|
821 | |
---|
822 | |
---|
823 | |
---|
824 | |
---|
825 | |
---|
826 | /** |
---|
827 | * Make a list of RAID partitions not currently associated with any RAID device. |
---|
828 | * The user may add any of these partitions to the RAID device. |
---|
829 | * @param output_list Where to put the list of unallocated RAID partitions. |
---|
830 | * @param mountlist The mountlist to examine. |
---|
831 | * @param raidlist The raidlist to examine. |
---|
832 | */ |
---|
833 | void make_list_of_unallocated_raid_partitions(struct mountlist_itself |
---|
834 | *output_list, |
---|
835 | struct mountlist_itself |
---|
836 | *mountlist, |
---|
837 | struct raidlist_itself |
---|
838 | *raidlist) |
---|
839 | { |
---|
840 | |
---|
841 | /*@ int ************************************************************* */ |
---|
842 | int items = 0; |
---|
843 | int i = 0; |
---|
844 | int used_by = 0; |
---|
845 | |
---|
846 | /*@ buffers ********************************************************* */ |
---|
847 | char *tmp; |
---|
848 | |
---|
849 | malloc_string(tmp); |
---|
850 | assert(output_list != NULL); |
---|
851 | assert(mountlist != NULL); |
---|
852 | assert(raidlist != NULL); |
---|
853 | log_it("MLOURP -- starting"); |
---|
854 | items = 0; |
---|
855 | |
---|
856 | |
---|
857 | for (i = 0; i < mountlist->entries; i++) { |
---|
858 | if (strstr(mountlist->el[i].mountpoint, "raid")) { |
---|
859 | used_by = |
---|
860 | which_raid_device_is_using_this_partition(raidlist, |
---|
861 | mountlist->el[i]. |
---|
862 | device); |
---|
863 | if (used_by < 0) { |
---|
864 | memcpy((void *) &output_list->el[items++], |
---|
865 | (void *) &mountlist->el[i], |
---|
866 | sizeof(struct mountlist_line)); |
---|
867 | sprintf(tmp, |
---|
868 | "%s is available; user may choose to add it to raid device", |
---|
869 | output_list->el[items - 1].device); |
---|
870 | log_it(tmp); |
---|
871 | } |
---|
872 | } |
---|
873 | } |
---|
874 | output_list->entries = items; |
---|
875 | log_it("MLUORP -- ending"); |
---|
876 | paranoid_free(tmp); |
---|
877 | } |
---|
878 | |
---|
879 | |
---|
880 | |
---|
881 | |
---|
882 | |
---|
883 | /** |
---|
884 | * Get the size of a mountlist entry by the @c device field. |
---|
885 | * @param mountlist The mountlist to search in. |
---|
886 | * @param device The device to search for |
---|
887 | * @return The size of the device (in KB), or -1 if it could not be found. |
---|
888 | */ |
---|
889 | long long |
---|
890 | size_of_specific_device_in_mountlist(struct mountlist_itself *mountlist, |
---|
891 | char *device) |
---|
892 | { |
---|
893 | /*@ int ************************************************************** */ |
---|
894 | int i = 0; |
---|
895 | |
---|
896 | |
---|
897 | assert(mountlist != NULL); |
---|
898 | assert_string_is_neither_NULL_nor_zerolength(device); |
---|
899 | |
---|
900 | for (i = 0; |
---|
901 | i < mountlist->entries && strcmp(mountlist->el[i].device, device); |
---|
902 | i++); |
---|
903 | if (i == mountlist->entries) { |
---|
904 | return (-1); |
---|
905 | } else { |
---|
906 | return (mountlist->el[i].size); |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | |
---|
911 | |
---|
912 | |
---|
913 | /** |
---|
914 | * Load a file on disk into @p mountlist. |
---|
915 | * The file on disk should consist of multiple lines, each containing 4 or 5 |
---|
916 | * columns: the device, the mountpoint, the filesystem type, the size in kilobytes, and optionally the filesystem label. |
---|
917 | * Comments begin with a '#' without any leading whitespace. Any duplicate |
---|
918 | * entries are renamed. |
---|
919 | * @param mountlist The mountlist to load into. |
---|
920 | * @param fname The name of the file to load the mountlist from. |
---|
921 | * @return 0 for success, 1 for failure. |
---|
922 | */ |
---|
923 | int load_mountlist(struct mountlist_itself *mountlist, char *fname) |
---|
924 | { |
---|
925 | FILE *fin; |
---|
926 | /* malloc ** */ |
---|
927 | char *incoming; |
---|
928 | char *siz; |
---|
929 | char *tmp; |
---|
930 | char *p; |
---|
931 | |
---|
932 | int items; |
---|
933 | int j; |
---|
934 | |
---|
935 | assert(mountlist != NULL); |
---|
936 | assert_string_is_neither_NULL_nor_zerolength(fname); |
---|
937 | malloc_string(incoming); |
---|
938 | malloc_string(siz); |
---|
939 | malloc_string(tmp); |
---|
940 | if (!(fin = fopen(fname, "r"))) { |
---|
941 | log_it("Unable to open mountlist - '%s'", fname); |
---|
942 | log_to_screen("Cannot open mountlist"); |
---|
943 | paranoid_free(incoming); |
---|
944 | paranoid_free(siz); |
---|
945 | paranoid_free(tmp); |
---|
946 | return (1); |
---|
947 | } |
---|
948 | items = 0; |
---|
949 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
950 | log_it("Loading mountlist..."); |
---|
951 | while (!feof(fin)) { |
---|
952 | #if linux |
---|
953 | sscanf(incoming, |
---|
954 | "%s %s %s %s %s", |
---|
955 | mountlist->el[items].device, |
---|
956 | mountlist->el[items].mountpoint, |
---|
957 | mountlist->el[items].format, |
---|
958 | siz, mountlist->el[items].label); |
---|
959 | #elif __FreeBSD__ |
---|
960 | sscanf(incoming, |
---|
961 | "%s %s %s %s", |
---|
962 | mountlist->el[items].device, |
---|
963 | mountlist->el[items].mountpoint, |
---|
964 | mountlist->el[items].format, siz); |
---|
965 | strcpy(mountlist->el[items].label, ""); |
---|
966 | #endif |
---|
967 | |
---|
968 | if (!strcmp(mountlist->el[items].device, "/proc") || |
---|
969 | !strcmp(mountlist->el[items].device, "proc") || |
---|
970 | !strcmp(mountlist->el[items].device, "/sys") || |
---|
971 | !strcmp(mountlist->el[items].device, "sys") || |
---|
972 | !strcmp(mountlist->el[items].device, "/devpts") || |
---|
973 | !strcmp(mountlist->el[items].device, "devpts") |
---|
974 | ) { |
---|
975 | log_msg(1, |
---|
976 | "Ignoring %s in mountlist - not loading that line :) ", |
---|
977 | mountlist->el[items].device); |
---|
978 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
979 | continue; |
---|
980 | } |
---|
981 | mountlist->el[items].size = atoll(siz); |
---|
982 | if (mountlist->el[items].device[0] != '\0' |
---|
983 | && mountlist->el[items].device[0] != '#') { |
---|
984 | if (items >= ARBITRARY_MAXIMUM) { |
---|
985 | log_to_screen("Too many lines in mountlist.. ABORTING"); |
---|
986 | finish(1); |
---|
987 | } |
---|
988 | for (j = 0; |
---|
989 | j < items |
---|
990 | && strcmp(mountlist->el[j].device, |
---|
991 | mountlist->el[items].device); j++); |
---|
992 | if (j < items) { |
---|
993 | strcat(mountlist->el[items].device, "_dup"); |
---|
994 | sprintf(tmp, |
---|
995 | "Duplicate entry in mountlist - renaming to %s", |
---|
996 | mountlist->el[items].device); |
---|
997 | log_it(tmp); |
---|
998 | } |
---|
999 | strcpy(tmp, mountlist->el[items].device); |
---|
1000 | if (strstr(tmp, "/dev/md/")) { |
---|
1001 | log_it("format_device() --- Contracting %s", tmp); |
---|
1002 | p = strrchr(tmp, '/'); |
---|
1003 | if (p) { |
---|
1004 | *p = *(p + 1); |
---|
1005 | *(p + 1) = *(p + 2); |
---|
1006 | *(p + 2) = *(p + 3); |
---|
1007 | } |
---|
1008 | log_it("It was %s; it is now %s", |
---|
1009 | mountlist->el[items].device, tmp); |
---|
1010 | strcpy(mountlist->el[items].device, tmp); |
---|
1011 | } |
---|
1012 | |
---|
1013 | sprintf(tmp, |
---|
1014 | "%s %s %s %lld %s", |
---|
1015 | mountlist->el[items].device, |
---|
1016 | mountlist->el[items].mountpoint, |
---|
1017 | mountlist->el[items].format, |
---|
1018 | mountlist->el[items].size, |
---|
1019 | mountlist->el[items].label); |
---|
1020 | |
---|
1021 | log_it(tmp); |
---|
1022 | items++; |
---|
1023 | } |
---|
1024 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
1025 | } |
---|
1026 | paranoid_fclose(fin); |
---|
1027 | mountlist->entries = items; |
---|
1028 | |
---|
1029 | log_it("Mountlist loaded successfully."); |
---|
1030 | sprintf(tmp, "%d entries in mountlist", items); |
---|
1031 | log_it(tmp); |
---|
1032 | paranoid_free(incoming); |
---|
1033 | paranoid_free(siz); |
---|
1034 | paranoid_free(tmp); |
---|
1035 | return (0); |
---|
1036 | } |
---|
1037 | |
---|
1038 | |
---|
1039 | |
---|
1040 | /** |
---|
1041 | * Save @p mountlist to a file on disk. |
---|
1042 | * @param mountlist The mountlist to save. |
---|
1043 | * @param fname The file to save it to. |
---|
1044 | * @return 0 for success, 1 for failure. |
---|
1045 | * @see load_mountlist |
---|
1046 | */ |
---|
1047 | int save_mountlist_to_disk(struct mountlist_itself *mountlist, char *fname) |
---|
1048 | { |
---|
1049 | FILE *fout; |
---|
1050 | int i; |
---|
1051 | |
---|
1052 | assert(mountlist != NULL); |
---|
1053 | assert_string_is_neither_NULL_nor_zerolength(fname); |
---|
1054 | |
---|
1055 | log_it("save_mountlist_to_disk() --- saving to %s", fname); |
---|
1056 | if (!(fout = fopen(fname, "w"))) { |
---|
1057 | log_OS_error("WMTD - Cannot openout mountlist"); |
---|
1058 | return (1); |
---|
1059 | } |
---|
1060 | for (i = 0; i < mountlist->entries; i++) { |
---|
1061 | fprintf(fout, |
---|
1062 | "%-15s %-15s %-15s %-15lld %-15s\n", |
---|
1063 | mountlist->el[i].device, mountlist->el[i].mountpoint, |
---|
1064 | mountlist->el[i].format, mountlist->el[i].size, |
---|
1065 | mountlist->el[i].label); |
---|
1066 | } |
---|
1067 | paranoid_fclose(fout); |
---|
1068 | return (0); |
---|
1069 | } |
---|
1070 | |
---|
1071 | |
---|
1072 | |
---|
1073 | /** |
---|
1074 | * Sort the mountlist alphabetically by device. |
---|
1075 | * The sorting is done in-place. |
---|
1076 | * @param mountlist The mountlist to sort. |
---|
1077 | */ |
---|
1078 | void sort_mountlist_by_device(struct mountlist_itself *mountlist) |
---|
1079 | { |
---|
1080 | int diff; |
---|
1081 | int lino = -999; |
---|
1082 | |
---|
1083 | assert(mountlist != NULL); |
---|
1084 | |
---|
1085 | while (lino < mountlist->entries) { |
---|
1086 | for (lino = 1; lino < mountlist->entries; lino++) { |
---|
1087 | diff = |
---|
1088 | strcmp_inc_numbers(mountlist->el[lino - 1].device, |
---|
1089 | mountlist->el[lino].device); |
---|
1090 | if (diff > 0) { |
---|
1091 | swap_mountlist_entries(mountlist, lino - 1, lino); |
---|
1092 | break; |
---|
1093 | } |
---|
1094 | } |
---|
1095 | } |
---|
1096 | } |
---|
1097 | |
---|
1098 | /** |
---|
1099 | * Sort the mountlist alphabetically by mountpoint. |
---|
1100 | * The sorting is done in-place. |
---|
1101 | * @param mountlist The mountlist to sort. |
---|
1102 | * @param reverse If TRUE, then do a reverse sort. |
---|
1103 | */ |
---|
1104 | void |
---|
1105 | sort_mountlist_by_mountpoint(struct mountlist_itself *mountlist, |
---|
1106 | bool reverse) |
---|
1107 | { |
---|
1108 | int diff; |
---|
1109 | int lino = -999; |
---|
1110 | |
---|
1111 | assert(mountlist != NULL); |
---|
1112 | |
---|
1113 | while (lino < mountlist->entries) { |
---|
1114 | for (lino = 1; lino < mountlist->entries; lino++) { |
---|
1115 | diff = |
---|
1116 | strcmp(mountlist->el[lino - 1].mountpoint, |
---|
1117 | mountlist->el[lino].mountpoint); |
---|
1118 | if ((diff > 0 && !reverse) || ((diff < 0 && reverse))) { |
---|
1119 | swap_mountlist_entries(mountlist, lino - 1, lino); |
---|
1120 | break; |
---|
1121 | } |
---|
1122 | } |
---|
1123 | } |
---|
1124 | } |
---|
1125 | |
---|
1126 | |
---|
1127 | /** |
---|
1128 | * Swap two entries in the mountlist in-place. |
---|
1129 | * @param mountlist The mountlist to swap the entries in. |
---|
1130 | * @param a The index number of the first entry. |
---|
1131 | * @param b The index number of the second entry. |
---|
1132 | */ |
---|
1133 | void |
---|
1134 | swap_mountlist_entries(struct mountlist_itself *mountlist, int a, int b) |
---|
1135 | { |
---|
1136 | /*@ mallocs *** */ |
---|
1137 | char device[64]; |
---|
1138 | char mountpoint[256]; |
---|
1139 | char format[64]; |
---|
1140 | |
---|
1141 | long long size; |
---|
1142 | |
---|
1143 | assert(mountlist != NULL); |
---|
1144 | assert(a >= 0); |
---|
1145 | assert(b >= 0); |
---|
1146 | |
---|
1147 | strcpy(device, mountlist->el[a].device); |
---|
1148 | strcpy(mountpoint, mountlist->el[a].mountpoint); |
---|
1149 | strcpy(format, mountlist->el[a].format); |
---|
1150 | |
---|
1151 | size = mountlist->el[a].size; |
---|
1152 | |
---|
1153 | strcpy(mountlist->el[a].device, mountlist->el[b].device); |
---|
1154 | strcpy(mountlist->el[a].mountpoint, mountlist->el[b].mountpoint); |
---|
1155 | strcpy(mountlist->el[a].format, mountlist->el[b].format); |
---|
1156 | |
---|
1157 | mountlist->el[a].size = mountlist->el[b].size; |
---|
1158 | |
---|
1159 | strcpy(mountlist->el[b].device, device); |
---|
1160 | strcpy(mountlist->el[b].mountpoint, mountpoint); |
---|
1161 | strcpy(mountlist->el[b].format, format); |
---|
1162 | |
---|
1163 | mountlist->el[b].size = size; |
---|
1164 | } |
---|
1165 | |
---|
1166 | /* @} - end of mountlistGroup */ |
---|