From ddf03e1969d549b76b195213ed2092c35aeea84b Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 22 Feb 2016 12:16:13 -0800 Subject: [PATCH] MOD: modified the index open and create. index is NOT created on open. -idx command will create an index --- dbfp.py | 19 +++++++++++++++---- docs/README_DEV | 1 + libs/fingerprint_index.py | 37 +++++++++++++++++++++++++++---------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/dbfp.py b/dbfp.py index 3c78ca4..5d1ebb8 100644 --- a/dbfp.py +++ b/dbfp.py @@ -45,10 +45,21 @@ def createFingerprint(file_in, app_name, app_ver, notes): # def indexFingerprints(fp_dir): - logging.info("fp_dir=={}".format(fp_dir)) db = FingerprintDB() fp = FingerprintIndex() - fp.openIndex(fp_dir) + try: + fp.openIndex(fp_dir) + print "WARN: index already exists. remove, then reindex\n" + except: + try: + retVal = fp.createIndex(fp_dir) + print "Index created:" + print " {} fingerprints processed.".format(retVal[0]) + print " {} fingprint processing errors.".format(retVal[1]) + print " {} files skipped (N/A, not a fingerprint file)".format(retVal[2]) + print + except Exception as ex: + print "ERROR: issue creating the index\n{}\n".format(ex) # def compareFingerprintDir(file_in, fp_dir): @@ -197,11 +208,11 @@ def parseArgs(): print '***** ***** ***** *****\n' parser = argparse.ArgumentParser(description="Fingerprint a sqlite database based on its schema") parser.add_argument('-db', '--database', required=False, help="path to file to be fingerprinted") - parser.add_argument('-fd', '--fpdir', required=False, help="path to directory of fingerprint files") + parser.add_argument('-fd', '--fpdir', required=False, help="path to directory of fingerprint files, compare each file") parser.add_argument('-fp', '--fingerprint', required=False, help="fingerprint file to use in comparison") parser.add_argument('-ad', '--android_dir', required=False, help="path to a directory with android folder structure sqlite files") parser.add_argument('-dd', '--data_dir', required=False, help="path to a directory to search for sqlite files") - parser.add_argument('-idx', '--index_fingerprints', required=False, help="path to a directory with sqlite files") + parser.add_argument('-idx', '--index_fingerprints', required=False, help="path to a directory with sqlite files, index fingerprints if no other args given") parser.add_argument('-md5', required=False, help="md5 hash to query the index`") parser.add_argument('-an', '--app_name', required=False) parser.add_argument('-av', '--app_version', required=False) diff --git a/docs/README_DEV b/docs/README_DEV index 68dbae3..d24a4f1 100644 --- a/docs/README_DEV +++ b/docs/README_DEV @@ -68,3 +68,4 @@ fp.openIndex(fp_dir) # return: list of fingerprints to open and compare fp_list = fp.findFP(md5_db, md5_tables) + diff --git a/libs/fingerprint_index.py b/libs/fingerprint_index.py index d52cdcf..67cfb59 100644 --- a/libs/fingerprint_index.py +++ b/libs/fingerprint_index.py @@ -40,21 +40,37 @@ class FingerprintIndex: self.cur = self.db_conn.cursor() logging.info("DB Open SUCCESSFUL") else: - logging.info("No index file found, creating index now...") - self.__createIndex(fp_dir) - try: - self.cur = self.db_conn.cursor() - self.__populateIndex(fp_dir) - except Exception as ex: - raise FingerprintIndexWrite("Error populating index file\n{}".format(ex)) - logging.info("Successfully populated the index") - self.db_conn = sql.connect(fq_fpidx) + raise FingerprintIndexOpen("Error opening an index file\n") except Exception as ex: if self.db_conn: self.db_conn.close() self.db_conn = None logging.error(ex) - raise FingerprintIndexOpen("Error opening/creating an index file\n") + raise FingerprintIndexOpen("Error opening an index file\n") + + # + def createIndex(self, fp_dir): + retVal = None + try: + if not os.path.isdir(fp_dir): + raise FingerprintIndexWrite("Error opening directory: {}".format(fp_dir)) + + self.__createIndex(fp_dir) + try: + self.cur = self.db_conn.cursor() + retVal = self.__populateIndex(fp_dir) + fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME + self.db_conn = sql.connect(fq_fpidx) + logging.info("Successfully populated the index") + except Exception as ex: + raise FingerprintIndexWrite("Error populating index file\n{}".format(ex)) + except Exception as ex: + if self.db_conn: + self.db_conn.close() + self.db_conn = None + logging.error(ex) + raise FingerprintIndexWrite("Error creating an index file\n") + return retVal # def findFP(self, md5_db, md5_tables): @@ -166,6 +182,7 @@ class FingerprintIndex: finally: self.db_conn.commit() logging.info("Completed populating the index. Completed: {} Failed: {} NA: {}".format(str(finCount), str(failCount), str(naCount))) + return (finCount, failCount, naCount) # def __insertMod_md5_all(self, md5_db, md5_list, filename):