2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "imv_os_database.h"
18 #include <utils/debug.h>
20 typedef struct private_imv_os_database_t private_imv_os_database_t
;
23 * Private data of a imv_os_database_t object.
26 struct private_imv_os_database_t
{
29 * Public imv_os_database_t interface.
31 imv_os_database_t
public;
40 METHOD(imv_os_database_t
, check_packages
, status_t
,
41 private_imv_os_database_t
*this, char *os_info
,
42 enumerator_t
*package_enumerator
)
44 char *product
, *package
, *release
, *cur_release
, *pos
;
46 int pid
, gid
, security
, i
;
47 int count
= 0, count_ok
= 0, count_no_match
= 0, count_not_found
= 0;
49 chunk_t name
, version
;
50 status_t status
= SUCCESS
;
58 /* looking for appended platform info */
59 for (i
= 0; i
< countof(platform
); i
++)
61 pos
= strstr(os_info
, platform
[i
]);
69 /* Remove platform info, leaving OS name and version only */
70 len
= pos
- os_info
- 1;
71 product
= malloc(len
+ 1);
72 memcpy(product
, os_info
, len
);
77 product
= strdup(os_info
);
80 /* Get primary key of product */
81 e
= this->db
->query(this->db
,
82 "SELECT id FROM products WHERE name = ?",
83 DB_TEXT
, product
, DB_INT
);
89 if (!e
->enumerate(e
, &pid
))
97 DBG1(DBG_IMV
, "'%s': pid = %d", product
, pid
);
99 while (package_enumerator
->enumerate(package_enumerator
, &name
, &version
))
101 /* Convert package name chunk to a string */
102 package
= malloc(name
.len
+ 1);
103 memcpy(package
, name
.ptr
, name
.len
);
104 package
[name
.len
] = '\0';
107 /* Get primary key of package */
108 e
= this->db
->query(this->db
,
109 "SELECT id FROM packages WHERE name = ?",
110 DB_TEXT
, package
, DB_INT
);
117 if (!e
->enumerate(e
, &gid
))
119 /* not found in database vor any product - skip */
126 /* Convert package version chunk to a string */
127 release
= malloc(version
.len
+ 1);
128 memcpy(release
, version
.ptr
, version
.len
);
129 release
[version
.len
] = '\0';
131 /* Enumerate over all acceptable versions */
132 e
= this->db
->query(this->db
,
133 "SELECT release, security FROM versions "
134 "WHERE product = ? AND package = ?",
135 DB_INT
, pid
, DB_INT
, gid
, DB_TEXT
, DB_INT
);
146 while (e
->enumerate(e
, &cur_release
, &security
))
149 if (streq(release
, cur_release
))
161 DBG2(DBG_IMV
, "package '%s' (%s)%s is ok", package
, release
,
162 security ?
" [s]" : "");
167 DBG1(DBG_IMV
, "package '%s' (%s) no match", package
, release
);
169 status
= VERIFY_ERROR
;
181 DBG1(DBG_IMV
, "processed %d packages: %d ok, %d no match, %d not found",
182 count
, count_ok
, count_no_match
, count_not_found
);
187 METHOD(imv_os_database_t
, destroy
, void,
188 private_imv_os_database_t
*this)
190 this->db
->destroy(this->db
);
197 imv_os_database_t
*imv_os_database_create(char *uri
)
199 private_imv_os_database_t
*this;
203 .check_packages
= _check_packages
,
206 .db
= lib
->db
->create(lib
->db
, uri
),
212 "failed to connect to OS database '%s'", uri
);
217 return &this->public;