def upsert_file_record(self,filepath,filename,statinfo,mimetype,ignore=None):
cursor=self.conn.cursor()
self.conn.commit()
safe_filepath = filepath.decode('utf-8', 'strict')
safe_filename = filename.decode('utf-8', 'strict')
try:
cursor.execute("insert into files (filename,filepath,last_seen) values (%s,%s,now()) returning id", (safe_filename,safe_filepath))
except psycopg2.IntegrityError as e:
self.conn.rollback()
cursor.execute("update files set last_seen=now() where filename=%s and filepath=%s returning id,ignore", (safe_filename, safe_filepath))
result=cursor.fetchone()
id=result[0]
try:
if result[1] == True:
ignore = True
except Exception as e:
logging.warning("An error occurred: " + str(e) + " trying to get ignore flag")
sqlcmd="update files set mtime={mt}, atime={at}, ctime={ct}, size=%s, owner=%s, gid=%s, mime_type=%s where id=%s".format(
mt="(SELECT TIMESTAMP WITH TIME ZONE 'epoch' + "+str(statinfo.st_mtime)+" * INTERVAL '1 second')",
at="(SELECT TIMESTAMP WITH TIME ZONE 'epoch' + "+str(statinfo.st_atime)+" * INTERVAL '1 second')",
ct="(SELECT TIMESTAMP WITH TIME ZONE 'epoch' + "+str(statinfo.st_ctime)+" * INTERVAL '1 second')",
)
cursor.execute(sqlcmd, (statinfo.st_size,statinfo.st_uid,statinfo.st_gid,mimetype,id))
if ignore is not None:
cursor.execute("update files set ignore={ign} where id={id}".format(
ign=ignore,
id=id
))
self.conn.commit()
评论列表
文章目录