summaryrefslogtreecommitdiff
path: root/src/sw-collector/sw_collector_dpkg.c
blob: b5a8582972e14812154ac80a5e9b8c6d7fbcab9d (plain)
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
/*
 * Copyright (C) 2017 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#define _GNU_SOURCE
#include <stdio.h>

#include "sw_collector_dpkg.h"

typedef struct private_sw_collector_dpkg_t private_sw_collector_dpkg_t;

/**
 * Private data of an sw_collector_dpkg_t object.
 */
struct private_sw_collector_dpkg_t {

	/**
	 * Public members of sw_collector_dpkg_state_t
	 */
	sw_collector_dpkg_t public;

};

typedef struct {
	/** public enumerator interface */
	enumerator_t public;
	/** dpkg output stream */
	FILE *file;
	/** current dpkg output line */
	char line[BUF_LEN];
} dpkg_enumerator_t;

METHOD(enumerator_t, enumerate, bool,
	dpkg_enumerator_t *this, va_list args)
{
	char **package, **arch, **version, *state, *pos;

	VA_ARGS_VGET(args, package, arch, version);

	while (TRUE)
	{
		if (!fgets(this->line, BUF_LEN, this->file))
		{
			return FALSE;
		}

		*package = this->line;
		pos = strchr(this->line, '\t');
		if (!pos)
		{
			return FALSE;
		}
		*pos = '\0';

		*arch = ++pos;
		pos = strchr(pos, '\t');
		if (!pos)
		{
			return FALSE;
		}
		*pos = '\0';

		*version = ++pos;
		pos = strchr(pos, '\t');
		if (!pos)
		{
			return FALSE;
		}
		*pos = '\0';

		state = ++pos;
		pos = strchr(pos, '\n');
		if (!pos)
		{
			return FALSE;
		}
		*pos = '\0';

		if (streq(state, "install ok installed"))
		{
			return TRUE;
		}
	}
}

METHOD(enumerator_t, enumerator_destroy, void,
	dpkg_enumerator_t *this)
{
	pclose(this->file);
	free(this);	
}

METHOD(sw_collector_dpkg_t, create_sw_enumerator, enumerator_t*,
	private_sw_collector_dpkg_t *this)
{
	dpkg_enumerator_t *enumerator;
	char cmd[] = "dpkg-query -W -f="
				 "\'${Package}\t${Architecture}\t${Version}\t${Status}\n\'";
	FILE *file;

	file = popen(cmd, "r");
	if (!file)
	{
		DBG1(DBG_IMC, "failed to run dpgk-query command");
		return NULL;
	}

	INIT(enumerator,
		.public = {
			.enumerate = enumerator_enumerate_default,
			.venumerate = _enumerate,
			.destroy = _enumerator_destroy,
		},
		.file = file,
	);

	return &enumerator->public;
}

METHOD(sw_collector_dpkg_t, destroy, void,
	private_sw_collector_dpkg_t *this)
{
	free(this);
}

/**
 * Described in header.
 */
sw_collector_dpkg_t *sw_collector_dpkg_create(void)
{
	private_sw_collector_dpkg_t *this;

	INIT(this,
		.public = {
			.create_sw_enumerator = _create_sw_enumerator,
			.destroy = _destroy,
		},
	);

	return &this->public;
}