database.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:assetsweeper 作者: guardian 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号