diff options
Diffstat (limited to 'src/libpts/pts/pts_database.c')
-rw-r--r-- | src/libpts/pts/pts_database.c | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/src/libpts/pts/pts_database.c b/src/libpts/pts/pts_database.c index e5a06cc8d..07e8ae1da 100644 --- a/src/libpts/pts/pts_database.c +++ b/src/libpts/pts/pts_database.c @@ -15,6 +15,7 @@ #define _GNU_SOURCE #include <stdio.h> +#include <libgen.h> #include "pts_database.h" @@ -248,13 +249,62 @@ METHOD(pts_database_t, check_file_measurement, status_t, enumerator_t *e; chunk_t hash; status_t status = NOT_FOUND; + char *dir, *file; + + if (strlen(filename) < 1) + { + return INVALID_ARG; + } + + /* separate filename into directory and basename components */ + dir = path_dirname(filename); + file = path_basename(filename); + + if (*dir == '.') + { /* relative pathname */ + e = this->db->query(this->db, + "SELECT fh.hash FROM file_hashes AS fh " + "JOIN files AS f ON f.id = fh.file " + "JOIN products AS p ON p.id = fh.product " + "WHERE p.name = ? AND f.name = ? AND fh.algo = ?", + DB_TEXT, product, DB_TEXT, file, DB_INT, algo, DB_BLOB); + } + else + { /* absolute pathname */ + bool dir_found; + int did; + + /* find directory entry first */ + e = this->db->query(this->db, + "SELECT id FROM directories WHERE path = ?", + DB_TEXT, dir, DB_INT); + if (!e) + { + free(file); + free(dir); + return FAILED; + } + dir_found = e->enumerate(e, &did); + e->destroy(e); + + if (!dir_found) + { + free(file); + free(dir); + return NOT_FOUND; + } + + e = this->db->query(this->db, + "SELECT fh.hash FROM file_hashes AS fh " + "JOIN files AS f ON f.id = fh.file " + "JOIN products AS p ON p.id = fh.product " + "WHERE p.name = ? AND f.dir = ? AND f.name = ? AND fh.algo = ?", + DB_TEXT, product, DB_INT, did, DB_TEXT, file, DB_INT, algo, + DB_BLOB); + } + free(file); + free(dir); - e = this->db->query(this->db, - "SELECT fh.hash FROM file_hashes AS fh " - "JOIN files AS f ON f.id = fh.file " - "JOIN products AS p ON p.id = fh.product " - "WHERE p.name = ? AND f.path = ? AND fh.algo = ?", - DB_TEXT, product, DB_TEXT, filename, DB_INT, algo, DB_BLOB); if (!e) { return FAILED; |