File: part_info.c

package info (click to toggle)
noflushd 2.7.5-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 792 kB
  • ctags: 302
  • sloc: ansic: 2,537; sh: 1,142; makefile: 85
file content (351 lines) | stat: -rw-r--r-- 7,742 bytes parent folder | download | duplicates (4)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * <part_info.c>
 *
 * Copyright (C) 2001 Daniel Kobras
 *
 * 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
 * 
 * $Id: part_info.c,v 1.5 2002/04/13 14:48:04 nold Exp $
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "part_info.h"
#include "state.h"
#include "util.h"
#include "diskhelper.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>	/* for major()/minor() macros */
#include <sys/types.h>

struct part_info_s {
	FILE *fp;	/* filp of /proc/partitions */
	int pos_major;	/* number of major entry in /proc/partitions */
	int pos_minor;	/* number of minor entry in /proc/partitions */
	int pos_name;	/* number of name entry in /proc/partitions */
	int major;	/* dev major of current entry */
	int minor;	/* dev minor of current entry */
	char *line;	/* next line to parse */
	char *name;	/* name of current entry */
};

/* The following functions determine whether a device name looks like a
 * name for a full disk (as opposed to a partition on a disk). They return
 * 1 for disks, and 0 for partitions.
 */
static int (*is_disk)(char *);

static int devfs_is_disk(char *name)
{
	int len;
	
	if (!name)
		return 0;
	
	len = strlen(name);
	
	if (len < 5)
		return 0;

	if (strcmp(name+len-5, "/disc"))
		return 0;

	return 1;
}

static int oldstyle_is_disk(char *name)
{
	int len;
	
	if (!name)
		return 0;

	len = strlen(name);
	
	if (len < 1)
		return 0;
	

	if (isdigit(name[strlen(name)-1]))
		return 0;

	return 1;
}

/* Check whether a device name looks devfs-like or oldstyle.
 */
static int name_is_devfs(char *name)
{
	return strchr(name, (int) '/') ? 1 : 0;
}

/* Check whether device names in /proc/partitions are devfs or oldstyle.
 * Set virtual function is_disk() accordingly.
 * Note: Calling check_devfs() will peek through /proc/partitions for a
 * valid device entry and thus clobber filepos in part->fp, and part->line!
 */
static void check_devfs(part_info_t part)
{

	char *major, *minor;
	char *name = NULL;
	
	goto start;
	
	while ((part->line=get_line(part->fp))) {

		release_entry(major);
		release_entry(minor);
		
	start:
		minor = NULL;
		
		if (!(major = get_entry(part->line, part->pos_major)) ||
		    !atoi(major) ||
		    !(minor = get_entry(part->line, part->pos_minor)))
			continue;
		
		/* XXX Hack!  Some meta devices like ataraid nowadays use
		 * a devfs-ish '/' in the name field.  Luckily they are
		 * accompanied by an ordinary IDE device, and we can simply
		 * ignore the meta devices.
		 */
		if (IS_META(atoi(major), atoi(minor)))
			continue;
		
		if ((name = get_entry(part->line, part->pos_name)))
			break;
	}

	if (!name)
		BUG("No valid entries found in /proc/partitions");
	
	if (name_is_devfs(name)) {
		DEBUG("Kernel talks devfs");
		is_disk = devfs_is_disk;
	} else {
		DEBUG("Kernel talks oldstyle dev");
		is_disk = oldstyle_is_disk;
	}
	
	release_entry(major);
	release_entry(minor);
	release_entry(name);
}	

/* Call to mark current line as treated. Fetches next line. */
static char *eat_line(part_info_t part)
{
	if (part->line)
		release_line(part->line);

	return (part->line=get_line(part->fp));
}

part_info_t part_info_init(void)
{
	char *entry;
	int pos;
	part_info_t part;

	part = malloc(sizeof(struct part_info_s));
	if (!part) {
		ERR("Could not alloc part struct");
		return NULL;
	}
	
	part->major = part->minor = 0;
	part->name = NULL;

	part->fp = fopen("/proc/partitions", "r");
	if (!part->fp) {
		ERR("Unable to open /proc/partitions");
		return NULL;
	}

	if (!(part->line=get_line(part->fp))) {
		ERR("Unable to read from /proc/partitions");
		part_info_release(part);
		return NULL;
	}

	/* Set up mapping between name tag and number of entry. */

	part->pos_major = part->pos_minor = part->pos_name = -1;
	
	for (pos=0; (entry=get_entry(part->line, pos)); pos++) {
		if (!strcmp(entry, "major"))
			part->pos_major=pos;
		else if (!strcmp(entry, "minor"))
			part->pos_minor=pos;
		else if (!strcmp(entry, "name"))
			part->pos_name=pos;
		release_entry(entry);
	}

	eat_line(part);
	
	if (part->pos_major==-1 || part->pos_minor==-1 || part->pos_name==-1) {
		ERR("Unknown format of /proc/partitions (%d, %d, %d)",
			part->pos_major, part->pos_minor, part->pos_name);
		part_info_release(part);
		return NULL;
	}

	check_devfs(part);
	part_info_reset(part);	/* check_devfs() clobbers filepos and line. */
	
	return part;
}

void part_info_release(part_info_t part)
{
	if (!part)
		return;

	if (part->line)
		release_line(part->line);
	if (part->name)
		free(part->name);
	if (part->fp)
		fclose(part->fp);
}

void part_info_reset(part_info_t part)
{
	if (!part || !part->fp)
		BUG("part_info_reset called on uninitialised part");

	fflush(part->fp);
	rewind(part->fp);

	/* Skip first line (tag line). */
	part->line = get_line(part->fp);
	eat_line(part);
}

/* part_info_next() handles both looking for partitions and whole disks.
 * Two bits in the flag parameter toggle the media type we are looking for,
 * and the behaviour upon unsuccessful lookups. The defines are overkill but
 * hopefully help clarify what's going on.
 */

#define NFD_PI_GET_PART		0x00	/* Looking for partition */
#define NFD_PI_GET_DISK		0x01	/* Looking for whole disk */
#define NFD_PI_MEDIA_MASK	0x0f
#define NFD_PI_FAIL_GO_ON	0x00	/* Go on searching if wrong media */
#define NFD_PI_FAIL_RETURN	0x10	/* Stop searching if wrong media */

static int part_info_next(part_info_t part, int flag)
{
	char *s_major, *s_minor, *name;
	int major, minor;

	if (!part || !part->fp)
		BUG("part_info_disk_next called on uninitialised part");

	if (!part->line)
		return 0;	/* EOF reached. */

	do {
		
		s_major=get_entry(part->line, part->pos_major);
		s_minor=get_entry(part->line, part->pos_minor);
		name=get_entry(part->line, part->pos_name);
		
		if (!s_major || !s_minor || !name) {
			release_entry(s_major);
			release_entry(s_minor);
			release_entry(name);
			continue;
		}

		major = atoi(s_major);
		minor = atoi(s_minor);
		release_entry(s_major);
		release_entry(s_minor);
		
		if (!major) {
			release_entry(name);
			continue;
		}

		if (!!is_disk(name) == !!(flag & NFD_PI_MEDIA_MASK))
			goto found;
		
		release_entry(name);
		
		if (flag & NFD_PI_FAIL_RETURN)
			return 0;
		
	} while (eat_line(part)); 

	/* End of file reached -> signal there are no more disks. */
	return 0;

found:
	if (part->name)
		free(part->name);

	part->name = strdup(name);
	part->major = major;
	part->minor = minor;

	release_entry(name);

	eat_line(part);
	return 1;
}

int part_info_disk_next(part_info_t part)
{
	return part_info_next(part, NFD_PI_GET_DISK | NFD_PI_FAIL_GO_ON);
}

int part_info_part_next(part_info_t part)
{
	return part_info_next(part, NFD_PI_GET_PART | NFD_PI_FAIL_RETURN);
}

char *part_info_get_name(part_info_t part)
{
	return part->name;
}

int part_info_get_major(part_info_t part)
{
	return part->major;
}
	
int part_info_get_minor(part_info_t part)
{
	return part->minor;
}
	
int part_info_is_devfs(void)
{
	return (is_disk == devfs_is_disk);
}