File: parted_devices.c

package info (click to toggle)
partman-base 208
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 1,996 kB
  • sloc: sh: 2,287; ansic: 2,238; makefile: 35
file content (125 lines) | stat: -rw-r--r-- 2,383 bytes parent folder | download | duplicates (2)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>

#ifdef __linux__
/* from <linux/cdrom.h> */
#define CDROM_GET_CAPABILITY	0x5331	/* get capabilities */
#endif /* __linux__ */

#ifdef __FreeBSD_kernel__
#include <sys/cdio.h>
#include <errno.h>
#endif

#include <parted/parted.h>

#if defined(__linux__)
static int
is_cdrom(const char *path)
{
	int fd;
	int ret;

	fd = open(path, O_RDONLY | O_NONBLOCK);
	ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
	close(fd);

	if (ret >= 0)
		return 1;
	else
		return 0;
}
#elif defined(__FreeBSD_kernel__) /* !__linux__ */
static int
is_cdrom(const char *path)
{
	int fd;

	fd = open(path, O_RDONLY | O_NONBLOCK);
	ioctl(fd, CDIOCCAPABILITY, NULL);	
	close(fd);

	if (errno != EBADF && errno != ENOTTY)
		return 1;
	else
		return 0;
}
#else /* !__linux__ && !__FreeBSD_kernel__ */
#define is_cdrom(path) 0
#endif

#if defined(__linux__)
static int
is_floppy(const char *path)
{
	return (strstr(path, "/dev/floppy") != NULL ||
		strstr(path, "/dev/fd") != NULL);
}
#elif defined(__FreeBSD_kernel__) /* !__linux__ */
static int
is_floppy(const char *path)
{
	return (strstr(path, "/dev/fd") != NULL);
}
#else /* !__linux__ && !__FreeBSD_kernel__ */
#define is_floppy(path) 0
#endif

void
process_device(PedDevice *dev)
{
	PedDisk *disk;
	if (dev->read_only)
		return;
	if (is_cdrom(dev->path) || is_floppy(dev->path))
		return;
	/* Exclude compcache (http://code.google.com/p/compcache/) */
	if (strstr(dev->path, "/dev/ramzswap") != NULL ||
	    strstr(dev->path, "/dev/zram") != NULL)
		return;
	disk = ped_disk_new(dev);
	printf("%s\t%lli\t%s\t%s\n",
	       dev->path,
	       dev->length * dev->sector_size,
	       dev->model,
	       disk && disk->type && disk->type->name ?
	       disk->type->name : "unknown");
}

int
main(int argc, char *argv[])
{
	PedDevice *dev;
	ped_exception_fetch_all();
	ped_device_probe_all();
	if (argc > 1) {
		int i;
		for (i = 1; i < argc; ++i) {
			dev = ped_device_get(argv[i]);
			if (dev) {
				process_device(dev);
				ped_device_destroy(dev);
			}
		}
	} else {
		for (dev = NULL; NULL != (dev = ped_device_get_next(dev));)
			process_device(dev);
	}
	return 0;
}

/*
Local variables:
indent-tabs-mode: nil
c-file-style: "linux"
c-font-lock-extra-types: ("Ped\\sw+")
End:
*/