add product and file entries to database

This commit is contained in:
Andreas Steffen
2011-11-28 14:39:53 +01:00
parent 3cd6077b75
commit f4159ff816
3 changed files with 115 additions and 28 deletions
+26 -9
View File
@@ -48,7 +48,9 @@ static void do_args(int argc, char *argv[])
OP_FILES, OP_FILES,
OP_PRODUCTS, OP_PRODUCTS,
OP_HASHES, OP_HASHES,
} operation = OP_UNDEF; OP_ADD,
OP_DEL,
} op = OP_UNDEF;
/* reinit getopt state */ /* reinit getopt state */
optind = 0; optind = 0;
@@ -60,9 +62,12 @@ static void do_args(int argc, char *argv[])
struct option long_opts[] = { struct option long_opts[] = {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "files", no_argument, NULL, 'f' }, { "files", no_argument, NULL, 'f' },
{ "add", no_argument, NULL, 'a' },
{ "del", no_argument, NULL, 'd' },
{ "products", no_argument, NULL, 'p' }, { "products", no_argument, NULL, 'p' },
{ "hashes", no_argument, NULL, 'H' }, { "hashes", no_argument, NULL, 'H' },
{ "directory", required_argument, NULL, 'D' }, { "directory", required_argument, NULL, 'D' },
{ "dir", required_argument, NULL, 'D' },
{ "file", required_argument, NULL, 'F' }, { "file", required_argument, NULL, 'F' },
{ "product", required_argument, NULL, 'P' }, { "product", required_argument, NULL, 'P' },
{ "sha1", no_argument, NULL, '1' }, { "sha1", no_argument, NULL, '1' },
@@ -80,31 +85,37 @@ static void do_args(int argc, char *argv[])
case EOF: case EOF:
break; break;
case 'h': case 'h':
operation = OP_USAGE; op = OP_USAGE;
break; break;
case 'f': case 'f':
operation = OP_FILES; op = OP_FILES;
continue; continue;
case 'p': case 'p':
operation = OP_PRODUCTS; op = OP_PRODUCTS;
continue; continue;
case 'H': case 'H':
operation = OP_HASHES; op = OP_HASHES;
continue;
case 'a':
op = OP_ADD;
continue;
case 'd':
op = OP_DEL;
continue; continue;
case 'D': case 'D':
if (!attest->set_directory(attest, optarg)) if (!attest->set_directory(attest, optarg, op == OP_ADD))
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
continue; continue;
case 'F': case 'F':
if (!attest->set_file(attest, optarg)) if (!attest->set_file(attest, optarg, op == OP_ADD))
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
continue; continue;
case 'P': case 'P':
if (!attest->set_product(attest, optarg)) if (!attest->set_product(attest, optarg, op == OP_ADD))
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -140,7 +151,7 @@ static void do_args(int argc, char *argv[])
break; break;
} }
switch (operation) switch (op)
{ {
case OP_USAGE: case OP_USAGE:
usage(); usage();
@@ -154,6 +165,12 @@ static void do_args(int argc, char *argv[])
case OP_HASHES: case OP_HASHES:
attest->list_hashes(attest); attest->list_hashes(attest);
break; break;
case OP_ADD:
attest->add(attest);
break;
case OP_DEL:
attest->delete(attest);
break;
default: default:
usage(); usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
+70 -13
View File
@@ -85,7 +85,7 @@ struct private_attest_db_t {
}; };
METHOD(attest_db_t, set_product, bool, METHOD(attest_db_t, set_product, bool,
private_attest_db_t *this, char *product) private_attest_db_t *this, char *product, bool create)
{ {
enumerator_t *e; enumerator_t *e;
@@ -104,12 +104,26 @@ METHOD(attest_db_t, set_product, bool,
{ {
this->product_set = TRUE; this->product_set = TRUE;
} }
else e->destroy(e);
}
if (this->product_set)
{
return TRUE;
}
if (!create)
{ {
printf("product '%s' not found in database\n", product); printf("product '%s' not found in database\n", product);
} }
e->destroy(e);
} /* Add a new database entry */
this->product_set = this->db->execute(this->db, &this->pid,
"INSERT INTO products (name) VALUES (?)",
DB_TEXT, product);
printf("product '%s' %sinserted into database\n", product,
this->product_set ? "" : "could not be ");
return this->product_set; return this->product_set;
} }
@@ -145,7 +159,7 @@ METHOD(attest_db_t, set_pid, bool,
} }
METHOD(attest_db_t, set_file, bool, METHOD(attest_db_t, set_file, bool,
private_attest_db_t *this, char *file) private_attest_db_t *this, char *file, bool create)
{ {
enumerator_t *e; enumerator_t *e;
@@ -164,12 +178,26 @@ METHOD(attest_db_t, set_file, bool,
{ {
this->file_set = TRUE; this->file_set = TRUE;
} }
else e->destroy(e);
}
if (this->file_set)
{
return TRUE;
}
if (!create)
{ {
printf("file '%s' not found in database\n", file); printf("file '%s' not found in database\n", file);
} }
e->destroy(e);
} /* Add a new database entry */
this->file_set = this->db->execute(this->db, &this->fid,
"INSERT INTO files (type, path) VALUES (0, ?)",
DB_TEXT, file);
printf("file '%s' %sinserted into database\n", file,
this->file_set ? "" : "could not be ");
return this->file_set; return this->file_set;
} }
@@ -205,7 +233,7 @@ METHOD(attest_db_t, set_fid, bool,
} }
METHOD(attest_db_t, set_directory, bool, METHOD(attest_db_t, set_directory, bool,
private_attest_db_t *this, char *dir) private_attest_db_t *this, char *dir, bool create)
{ {
enumerator_t *e; enumerator_t *e;
@@ -217,7 +245,8 @@ METHOD(attest_db_t, set_directory, bool,
free(this->dir); free(this->dir);
this->dir = strdup(dir); this->dir = strdup(dir);
e = this->db->query(this->db, "SELECT id FROM files WHERE path = ?", e = this->db->query(this->db,
"SELECT id FROM files WHERE type = 1 AND path = ?",
DB_TEXT, dir, DB_INT); DB_TEXT, dir, DB_INT);
if (e) if (e)
{ {
@@ -225,12 +254,26 @@ METHOD(attest_db_t, set_directory, bool,
{ {
this->dir_set = TRUE; this->dir_set = TRUE;
} }
else e->destroy(e);
}
if (this->dir_set)
{
return TRUE;
}
if (!create)
{ {
printf("directory '%s' not found in database\n", dir); printf("directory '%s' not found in database\n", dir);
} }
e->destroy(e);
} /* Add a new database entry */
this->dir_set = this->db->execute(this->db, &this->did,
"INSERT INTO files (type, path) VALUES (1, ?)",
DB_TEXT, dir);
printf("directory '%s' %sinserted into database\n", dir,
this->dir_set ? "" : "could not be ");
return this->dir_set; return this->dir_set;
} }
@@ -519,6 +562,18 @@ METHOD(attest_db_t, list_hashes, void,
free(dir); free(dir);
} }
METHOD(attest_db_t, add, bool,
private_attest_db_t *this)
{
return FALSE;
}
METHOD(attest_db_t, delete, bool,
private_attest_db_t *this)
{
return FALSE;
}
METHOD(attest_db_t, destroy, void, METHOD(attest_db_t, destroy, void,
private_attest_db_t *this) private_attest_db_t *this)
{ {
@@ -548,6 +603,8 @@ attest_db_t *attest_db_create(char *uri)
.list_products = _list_products, .list_products = _list_products,
.list_files = _list_files, .list_files = _list_files,
.list_hashes = _list_hashes, .list_hashes = _list_hashes,
.add = _add,
.delete = _delete,
.destroy = _destroy, .destroy = _destroy,
}, },
.dir = strdup(""), .dir = strdup(""),
@@ -37,9 +37,10 @@ struct attest_db_t {
* Set software product to be queried * Set software product to be queried
* *
* @param product software product * @param product software product
* @param create if TRUE create database entry if it doesn't exist
* @return TRUE if successful * @return TRUE if successful
*/ */
bool (*set_product)(attest_db_t *this, char *product); bool (*set_product)(attest_db_t *this, char *product, bool create);
/** /**
* Set primary key of the software product to be queried * Set primary key of the software product to be queried
@@ -53,9 +54,10 @@ struct attest_db_t {
* Set measurement file to be queried * Set measurement file to be queried
* *
* @param file measurement file * @param file measurement file
* @param create if TRUE create database entry if it doesn't exist
* @return TRUE if successful * @return TRUE if successful
*/ */
bool (*set_file)(attest_db_t *this, char *file); bool (*set_file)(attest_db_t *this, char *file, bool create);
/** /**
* Set primary key of the measurement file to be queried * Set primary key of the measurement file to be queried
@@ -69,9 +71,10 @@ struct attest_db_t {
* Set directory of the measurement file to be queried * Set directory of the measurement file to be queried
* *
* @param directory directory containing the measurement file * @param directory directory containing the measurement file
* @param create if TRUE create database entry if it doesn't exist
* @return TRUE if successful * @return TRUE if successful
*/ */
bool (*set_directory)(attest_db_t *this, char *dir); bool (*set_directory)(attest_db_t *this, char *dir, bool create);
/** /**
* Set primary key of the directory to be queried * Set primary key of the directory to be queried
@@ -103,6 +106,16 @@ struct attest_db_t {
*/ */
void (*list_hashes)(attest_db_t *this); void (*list_hashes)(attest_db_t *this);
/**
* Add an entry to the database
*/
bool (*add)(attest_db_t *this);
/**
* Delete an entry from the database
*/
bool (*delete)(attest_db_t *this);
/** /**
* Destroy attest_db_t object * Destroy attest_db_t object
*/ */