private function choose_repository(stdClass $info) {
        global $DB, $CFG;
        require_once($CFG->dirroot.'/repository/lib.php');

        if ($this->task->is_samesite()) {
            // We can rely on repository instance id.

            if (array_key_exists($info->oldfile->repositoryid, $this->cachereposbyid)) {
                return $this->cachereposbyid[$info->oldfile->repositoryid];
            }

            $this->log('looking for repository instance by id', backup::LOG_DEBUG, $info->oldfile->repositoryid, 1);

            try {
                $this->cachereposbyid[$info->oldfile->repositoryid] = repository::get_repository_by_id($info->oldfile->repositoryid, SYSCONTEXTID);
                return $this->cachereposbyid[$info->oldfile->repositoryid];
            } catch (Exception $e) {
                $this->cachereposbyid[$info->oldfile->repositoryid] = null;
                return null;
            }

        } else {
            // We can rely on repository type only.

            if (empty($info->oldfile->repositorytype)) {
                return null;
            }

            if (array_key_exists($info->oldfile->repositorytype, $this->cachereposbytype)) {
                return $this->cachereposbytype[$info->oldfile->repositorytype];
            }

            $this->log('looking for repository instance by type', backup::LOG_DEBUG, $info->oldfile->repositorytype, 1);

            // Both Server files and Legacy course files repositories have a single
            // instance at the system context to use. Let us try to find it.
            if ($info->oldfile->repositorytype === 'local' or $info->oldfile->repositorytype === 'coursefiles') {
                $sql = "SELECT ri.id
                          FROM {repository} r
                          JOIN {repository_instances} ri ON ri.typeid = r.id
                         WHERE r.type = ? AND ri.contextid = ?";
                $ris = $DB->get_records_sql($sql, array($info->oldfile->repositorytype, SYSCONTEXTID));
                if (empty($ris)) {
                    return null;
                }
                $repoids = array_keys($ris);
                $repoid = reset($repoids);
                try {
                    $this->cachereposbytype[$info->oldfile->repositorytype] = repository::get_repository_by_id($repoid, SYSCONTEXTID);
                    return $this->cachereposbytype[$info->oldfile->repositorytype];
                } catch (Exception $e) {
                    $this->cachereposbytype[$info->oldfile->repositorytype] = null;
                    return null;
                }
            }

            $this->cachereposbytype[$info->oldfile->repositorytype] = null;
            return null;
        }
    }