def scan_virus(self, scan_task): try: sroot_id = None hroot_id = None if scan_task.scan_commit_id: sroot_id = commit_mgr.get_commit_root_id( scan_task.repo_id, 1, scan_task.scan_commit_id) if scan_task.head_commit_id: hroot_id = commit_mgr.get_commit_root_id( scan_task.repo_id, 1, scan_task.head_commit_id) differ = CommitDiffer(scan_task.repo_id, 1, sroot_id, hroot_id) scan_files = differ.diff() if len(scan_files) == 0: logger.debug('No change occur for repo %.8s, skip virus scan.', scan_task.repo_id) self.db_oper.update_vscan_record(scan_task.repo_id, scan_task.head_commit_id) return else: logger.info('Start to scan virus for repo %.8s.', scan_task.repo_id) vnum = 0 nvnum = 0 nfailed = 0 vrecords = [] for scan_file in scan_files: fpath, fid, fsize = scan_file if not self.should_scan_file(fpath, fsize): continue ret = self.scan_file_virus(scan_task.repo_id, fid, fpath) if ret == 0: logger.debug('File %s virus scan by %s: OK.', fpath, self.settings.scan_cmd) nvnum += 1 elif ret == 1: logger.info('File %s virus scan by %s: Found virus.', fpath, self.settings.scan_cmd) vnum += 1 fpath = fpath if isinstance( fpath, unicode) else fpath.decode('utf-8') vrecords.append( (scan_task.repo_id, scan_task.head_commit_id, fpath)) else: logger.debug('File %s virus scan by %s: Failed.', fpath, self.settings.scan_cmd) nfailed += 1 if nfailed == 0: ret = 0 if len(vrecords) > 0: ret = self.db_oper.add_virus_record(vrecords) if ret == 0 and self.settings.enable_send_mail: self.send_email(vrecords) if ret == 0: self.db_oper.update_vscan_record(scan_task.repo_id, scan_task.head_commit_id) logger.info( 'Virus scan for repo %.8s finished: %d virus, %d non virus, %d failed.', scan_task.repo_id, vnum, nvnum, nfailed) except Exception as e: logger.warning('Failed to scan virus for repo %.8s: %s.', scan_task.repo_id, e)
def get_repo_root_seafdir(repo): root_id = commit_mgr.get_commit_root_id(repo.id, repo.version, repo.head_cmmt_id) return fs_mgr.load_seafdir(repo.store_id, repo.version, root_id)