rules.c revision 8156
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $Id$
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <string.h>
17#include <sys/types.h>
18#include <sys/diskslice.h>
19#include <sys/disklabel.h>
20#include <err.h>
21#include "libdisk.h"
22
23/*
24 *  Rule#0:
25 *	Chunks of type 'whole' can have max NDOSPART children.
26 */
27void
28Rule_000(struct disk *d, struct chunk *c, char *msg)
29{
30	int i;
31	struct chunk *c1;
32
33	if (c->type != whole)
34		return;
35	for (i=0, c1=c->part; c1; c1=c1->next)
36		if (c1->type != unused)
37			i++;
38	if (i <= NDOSPART)
39		return;
40	sprintf(msg+strlen(msg),
41		"%d is too many children of the 'whole' chunk.  Max is %d\n",
42		i, NDOSPART);
43}
44
45/*
46 * Rule#1:
47 *	All children of 'whole' must be track-aligned
48 */
49void
50Rule_001(struct disk *d, struct chunk *c, char *msg)
51{
52	int i;
53	struct chunk *c1;
54
55	if (c->type != whole)
56		return;
57	for (i=0, c1=c->part; c1; c1=c1->next) {
58		if (c1->type == reserved)
59			continue;
60		if (c1->type == unused)
61			continue;
62		if (!Aligned(d,c1->offset))
63			sprintf(msg+strlen(msg),
64		    "chunk '%s' [%ld..%ld] does not start on a track boundary\n",
65				c1->name,c1->offset,c1->end);
66		if (!Aligned(d,c1->end+1))
67			sprintf(msg+strlen(msg),
68		    "chunk '%s' [%ld..%ld] does not end on a track boundary\n",
69				c1->name,c1->offset,c1->end);
70	}
71}
72
73void
74Check_Chunk(struct disk *d, struct chunk *c, char *msg)
75{
76	Rule_000(d,c,msg);
77	Rule_001(d,c,msg);
78	if (c->part)
79		Check_Chunk(d,c->part,msg);
80	if (c->next)
81		Check_Chunk(d,c->next,msg);
82	return;
83}
84
85char *
86CheckRules(struct disk *d)
87{
88	char msg[BUFSIZ];
89
90	*msg = '\0';
91	Check_Chunk(d,d->chunks,msg);
92	if (*msg)
93		return strdup(msg);
94	return 0;
95}
96