Fixed the building
[strongswan.git] / src / libpts / pts / pts_database.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "pts_database.h"
17
18 #include <debug.h>
19 #include <crypto/hashers/hasher.h>
20
21
22 typedef struct private_pts_database_t private_pts_database_t;
23
24 /**
25 * Private data of a pts_database_t object.
26 *
27 */
28 struct private_pts_database_t {
29
30 /**
31 * Public pts_database_t interface.
32 */
33 pts_database_t public;
34
35 /**
36 * database instance
37 */
38 database_t *db;
39
40 };
41
42 METHOD(pts_database_t, create_file_meas_enumerator, enumerator_t*,
43 private_pts_database_t *this, char *product)
44 {
45 enumerator_t *e;
46
47 /* look for all entries belonging to a product in the files table */
48 e = this->db->query(this->db,
49 "SELECT f.id, f.type, f.path FROM files AS f "
50 "JOIN product_file AS pf ON f.id = pf.file "
51 "JOIN products AS p ON p.id = pf.product "
52 "WHERE p.name = ? AND f.measurement = 1",
53 DB_TEXT, product, DB_INT, DB_INT, DB_TEXT);
54 return e;
55 }
56
57 METHOD(pts_database_t, create_file_meta_enumerator, enumerator_t*,
58 private_pts_database_t *this, char *product)
59 {
60 enumerator_t *e;
61
62 /* look for all entries belonging to a product in the files table */
63 e = this->db->query(this->db,
64 "SELECT f.type, f.path FROM files AS f "
65 "JOIN product_file AS pf ON f.id = pf.file "
66 "JOIN products AS p ON p.id = pf.product "
67 "WHERE p.name = ? AND f.metadata = 1",
68 DB_TEXT, product, DB_INT, DB_TEXT);
69 return e;
70 }
71
72 METHOD(pts_database_t, create_comp_evid_enumerator, enumerator_t*,
73 private_pts_database_t *this, char *product)
74 {
75 enumerator_t *e;
76
77 /* look for all entries belonging to a product in the components table */
78 e = this->db->query(this->db,
79 "SELECT c.vendor_id, c.name, c.qualifier FROM components AS c "
80 "JOIN product_component AS pc ON c.id = pc.component "
81 "JOIN products AS p ON p.id = pc.product "
82 "WHERE p.name = ? ORDER BY pf.order",
83 DB_TEXT, product, DB_INT, DB_INT, DB_INT);
84 return e;
85 }
86
87
88 METHOD(pts_database_t, create_file_hash_enumerator, enumerator_t*,
89 private_pts_database_t *this, char *product, pts_meas_algorithms_t algo,
90 int id, bool is_dir)
91 {
92 enumerator_t *e;
93
94 if (is_dir)
95 {
96 e = this->db->query(this->db,
97 "SELECT f.path, fh.hash FROM file_hashes AS fh "
98 "JOIN files AS f ON fh.file = f.id "
99 "JOIN products AS p ON fh.product = p.id "
100 "WHERE p.name = ? AND fh.directory = ? AND fh.algo = ? "
101 "ORDER BY f.path",
102 DB_TEXT, product, DB_INT, id, DB_INT, algo, DB_TEXT, DB_BLOB);
103 }
104 else
105 {
106 e = this->db->query(this->db,
107 "SELECT f.path, fh.hash FROM file_hashes AS fh "
108 "JOIN files AS f ON fh.file = f.id "
109 "JOIN products AS p ON fh.product = p.id "
110 "WHERE p.name = ? AND fh.file = ? AND fh.algo = ?",
111 DB_TEXT, product, DB_INT, id, DB_INT, algo, DB_TEXT, DB_BLOB);
112 }
113 return e;
114 }
115
116 METHOD(pts_database_t, create_comp_hash_enumerator, enumerator_t*,
117 private_pts_database_t *this, char *product, pts_meas_algorithms_t algo,
118 pts_comp_func_name_t *comp_name)
119 {
120 enumerator_t *e;
121
122 e = this->db->query(this->db,
123 "SELECT ch.hash FROM component_hashes AS ch "
124 "JOIN components AS c ON ch.component = c.id "
125 "JOIN products AS p ON ch.product = p.id "
126 "WHERE p.name = ? AND c.vendor_id = ? "
127 "AND c.name = ? AND c.qualifier = ? AND ch.algo = ? ",
128 DB_TEXT, product, DB_INT, comp_name->get_vendor_id(comp_name),
129 DB_INT, comp_name->get_name(comp_name),
130 DB_INT, comp_name->get_qualifier(comp_name),
131 DB_INT, algo, DB_BLOB);
132
133 return e;
134 }
135
136 METHOD(pts_database_t, destroy, void,
137 private_pts_database_t *this)
138 {
139 this->db->destroy(this->db);
140 free(this);
141 }
142
143 /**
144 * See header
145 */
146 pts_database_t *pts_database_create(char *uri)
147 {
148 private_pts_database_t *this;
149
150 INIT(this,
151 .public = {
152 .create_file_meas_enumerator = _create_file_meas_enumerator,
153 .create_file_meta_enumerator = _create_file_meta_enumerator,
154 .create_comp_evid_enumerator = _create_comp_evid_enumerator,
155 .create_file_hash_enumerator = _create_file_hash_enumerator,
156 .create_comp_hash_enumerator = _create_comp_hash_enumerator,
157 .destroy = _destroy,
158 },
159 .db = lib->db->create(lib->db, uri),
160 );
161
162 if (!this->db)
163 {
164 DBG1(DBG_PTS,
165 "failed to connect to PTS file measurement database '%s'", uri);
166 free(this);
167 return NULL;
168 }
169
170 return &this->public;
171 }
172