#define _GNU_SOURCE
#include <stdio.h>
+#include <libgen.h>
#include "pts_database.h"
enumerator_t *e;
chunk_t hash;
status_t status = NOT_FOUND;
+ char *path, *dir, *file;
+
+ if (strlen(filename) < 1)
+ {
+ return INVALID_ARG;
+ }
+
+ /* separate filename into directory and basename components */
+ path = strdup(filename);
+ dir = dirname(path);
+ file = 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(path);
+ return FAILED;
+ }
+ dir_found = e->enumerate(e, &did);
+ e->destroy(e);
+
+ if (!dir_found)
+ {
+ free(path);
+ 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(path);
- 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;