def index_torrent(self, torrent):
        """
        Indexes the files in the torrent.
        """
        torrent_name = torrent[b'info'][b'name']
        logger.debug('Handling torrent name %r' % (torrent_name, ))
        torrent_name = self.try_decode(torrent_name)
        if not self.is_legal_path([torrent_name]):
            raise IllegalPathException('That is a dangerous torrent name %r, bailing' % torrent_name)
        
        logger.info('Found name %r for torrent' % torrent_name)
        
        if self.db.exact_mode:
            prefix = 'd' if b'files' in torrent[b'info'] else 'f'
            
            paths = self.db.find_exact_file_path(prefix, torrent_name)
            if paths:
                for path in paths:
                    logger.debug('Checking exact path %r' % path)
                    if prefix == 'f':
                        logger.info('Did an exact match to a file')
                        size = os.path.getsize(path)
                        if torrent[b'info'][b'length'] != size:
                            continue
                        
                        return {'mode': 'exact',
                                'source_path': os.path.dirname(path),
                                'files': [{
                                    'actual_path': path,
                                    'length': size,
                                    'path': [torrent_name],
                                    'completed': True,
                                }]}
                    else:
                        result = []
                        for f in torrent[b'info'][b'files']:
                            orig_path = [self.try_decode(x) for x in f[b'path']]
                            p = os.path.join(path, *orig_path)
                            
                            if not os.path.isfile(p):
                                logger.debug('File %r does not exist' % p)
                                break
                            
                            size = os.path.getsize(p)
                            if size != f[b'length']:
                                logger.debug('File %r did not match, this is not exact (got size %s, expected %s)' % (p, size, f[b'length']))
                                break
                            
                            result.append({
                                'actual_path': p,
                                'length': f[b'length'],
                                'path': orig_path,
                                'completed': True,
                            })
                        else:
                            logger.info('Did an exact match to a path')
                            return {'mode': 'exact',
                                    'source_path': path,
                                    'files': result}
        
        
        result = []
        if b'files' in torrent[b'info']: # multifile torrent
            files_sorted = {}
            files = {}
            if b'files' in torrent[b'info']:
                
                i = 0
                path_files = defaultdict(list)
                for f in torrent[b'info'][b'files']:
                    logger.debug('Handling torrent file %r' % (f, ))
                    orig_path = [self.try_decode(x) for x in f[b'path'] if x] # remove empty fragments
                    if not self.is_legal_path(orig_path):
                        raise IllegalPathException('That is a dangerous torrent path %r, bailing' % orig_path)
                    
                    path = [torrent_name] + orig_path
                    name = path.pop()
                    
                    path_files[os.path.join(*path)].append({
                        'path': orig_path,
                        'length': f[b'length'],
                    })
                    
                    files_sorted['/'.join(orig_path)] = i
                    i += 1
            
            if self.db.unsplitable_mode:
                unsplitable_paths = set()
                for path, files in path_files.items():
                    if is_unsplitable(f['path'][-1] for f in files):
                        path = path.split(os.sep)
                        name = get_root_of_unsplitable(path)
                        if not name:
                            continue
                        
                        while path[-1] != name:
                            path.pop()
                        unsplitable_paths.add(os.path.join(*path))
            
            for path, files in path_files.items():
                if self.db.unsplitable_mode:
                    path = path.split(os.sep)
                    while path and os.path.join(*path) not in unsplitable_paths:
                        path.pop()
                else:
                    path = None
                
                if path:
                    name = path[-1]
                    for f in files:
                        actual_path = self.db.find_unsplitable_file_path(name, f['path'], f['length'])
                        f['actual_path'] = actual_path
                        f['completed'] = actual_path is not None
                    result += files
                else:
                    for f in files:
                        actual_path = self.db.find_file_path(f['path'][-1], f['length'])
                        f['actual_path'] = actual_path
                        f['completed'] = actual_path is not None
                    result += files
            # re-sort the torrent to fit original ordering
            result = sorted(result, key=lambda x:files_sorted['/'.join(x['path'])])
            
        else: # singlefile torrent
            length = torrent[b'info'][b'length']
            actual_path = self.db.find_file_path(torrent_name, length)
            
            result.append({
                'actual_path': actual_path,
                'length': length,
                'path': [torrent_name],
                'completed': actual_path is not None,
            })
        
        mode = 'link'
        if self.db.hash_mode:
            modified_result, result = self.find_hash_checks(torrent, result)
            if modified_result:
                mode = 'hash'
        
        return {'mode': mode, 'files': result}
dataset/ETHPy150Open JohnDoee/autotorrent/autotorrent/at.py/AutoTorrent.index_torrent/original