-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscentgather_connecttodb.py
More file actions
147 lines (138 loc) · 4.67 KB
/
scentgather_connecttodb.py
File metadata and controls
147 lines (138 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import scentgather_getfilemeta
import scentgather_logging
import sys
def connectodb(hostname='localhost',schema='scentgather',username='root',password='',dbtype='sqlite'):
if dbtype=='sqlite':
import sqlite3
conn=sqlite3.connect(schema+'.db')
c=conn.cursor()
c.execute("PRAGMA foreign_keys=1")
c.executescript('''
CREATE TABLE IF NOT EXISTS exif
(
exifID INTEGER PRIMARY KEY,
exifname text UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS platforms
(
systemID INTEGER PRIMARY KEY,
architecture text,
machine text,
node text,
platform text,
processor text,
system text
);
CREATE TABLE IF NOT EXISTS files
(
fileID INTEGER PRIMARY KEY,
filename text NOT NULL,
pathname text UNIQUE NOT NULL,
inode INT,
systemID INT,
lastmodDate text,
lastaccessDate text,
apparentcreationDate text,
discovereddeletionDate text,
insertiondate text DEFAULT CURRENT_TIMESTAMP,
sizeInBytes INT,
detectedformatID INT,
ext INT,
md5sum text,
FOREIGN KEY (systemID) REFERENCES platforms(systemID)
);
CREATE TABLE IF NOT EXISTS files_exif
(
fileID INTEGER NOT NULL,
exifID INTEGER NOT NULL,
value text,
PRIMARY KEY (exifID,fileID),
FOREIGN KEY (fileID) REFERENCES files(fileID),
FOREIGN KEY (exifID) REFERENCES exif(exifID)
);
''');
return conn
else:
return None
def savesysinfotodb(dbconnect,sysinfo):
import sqlite3
if type(dbconnect) is sqlite3.Connection:
c=dbconnect.cursor()
res=c.execute('''
SELECT systemID FROM platforms
WHERE platform=?
''',[sysinfo[3]])
sysid=res.fetchall()
if sysid is None or type(sysid) is not int and len(sysid)==0:
c.execute('''
INSERT INTO platforms
(architecture,machine,node,platform,
processor,system)
VALUES (?,?,?,?,?,?)
''',sysinfo)
sysid=c.lastrowid
dbconnect.commit()
if type(sysid) is not int and len(sysid)==0:
return False
elif type(sysid) is not int:
sysid=sysid[0][0]
else:
return False
return sysid
def getsysid(dbconnect):
return savesysinfotodb(dbconnect,scentgather_getfilemeta.getsysinfo())
def saveinfotodb(dbconnect,fileinfos):
import sqlite3
if type(dbconnect) is sqlite3.Connection:
c=dbconnect.cursor()
for f in fileinfos:
f[0]=buffer(f[0])
f[1]=buffer(f[1])
c.execute('''
INSERT OR IGNORE INTO files
(filename,pathname,inode,systemID,
lastmodDate,lastaccessDate,apparentcreationDate,
discovereddeletionDate,insertiondate,sizeInBytes,
detectedformatID,ext,md5sum)
VALUES (CAST(? AS TEXT),CAST(? AS TEXT),?,?,?,?,?,?,?,?,?,?,?)
''',f[:-1])
if type(c.lastrowid) is int:
fid=c.lastrowid
for k,v in list(f[-1].items()):
c.execute('''
INSERT OR IGNORE INTO exif
(exifName)
VALUES (?)
''',[unicode(k)])
c.execute('''
INSERT OR IGNORE INTO files_exif
(fileID,exifID,value)
VALUES (?,(select exifID from exif where exifname=?),CAST(? AS TEXT))
''',[fid,unicode(k),unicode(v)])
else:
dbconnect.rollback()
return False
dbconnect.commit()
return True
else:
return False
def checkfileexistsindb(dbconnect,fileinfo,quick):
import sqlite3
if type(dbconnect) is sqlite3.Connection:
c=dbconnect.cursor()
pathname=fileinfo['pathname']
checksum=scentgather_getfilemeta.hashfile(pathname)
if quick:
res=c.execute('''
SELECT 1 FROM files
WHERE pathname=CAST(? AS TEXT)''', [buffer(pathname)])
else:
res=c.execute('''
SELECT 1 FROM files
WHERE pathname=CAST(? AS TEXT) and md5sum=?''', [buffer(pathname),checksum])
fileexists=res.fetchall()
if fileexists:
return True
return False
else:
return None