示例#1
0
gboolean
lr_fastestmirror_sort_internalmirrorlists(GSList *handles,
                                          GError **err)
{
    assert(!err || *err == NULL);

    if (!handles)
        return TRUE;

    GTimer *timer = g_timer_new();
    g_timer_start(timer);

    LrHandle *main_handle = handles->data;  // Network configuration for the
                                            // test is used from the first
                                            // handle

    // Prepare list of hosts
    gchar *fastestmirrorcache = main_handle->fastestmirrorcache;
    GHashTable *hosts_ht = g_hash_table_new_full(g_str_hash,
                                                 g_str_equal,
                                                 g_free,
                                                 NULL);

    for (GSList *ehandle = handles; ehandle; ehandle = g_slist_next(ehandle)) {
        LrHandle *handle = ehandle->data;
        GSList *mirrors = handle->internal_mirrorlist;
        for (GSList *elem = mirrors; elem; elem = g_slist_next(elem)) {
            LrInternalMirror *imirror = elem->data;
            gchar *host = lr_url_without_path(imirror->url);
            g_hash_table_insert(hosts_ht, host, NULL);
        }

        // Cache related warning
        if (fastestmirrorcache) {
            if (handle->fastestmirrorcache
                && g_strcmp0(fastestmirrorcache, handle->fastestmirrorcache))
                g_warning("%s: Multiple fastestmirror caches are specified! "
                          "Used one is %s (%s is ignored)", __func__,
                          fastestmirrorcache, handle->fastestmirrorcache);
        } else {
            if (handle->fastestmirrorcache)
                g_warning("%s: First handle doesn't have a fastestmirror "
                          "cache specified but other one has: %s",
                          __func__, handle->fastestmirrorcache);
        }
    }

    GList *tmp_list_of_urls = g_hash_table_get_keys(hosts_ht);
    GSList *list_of_urls = NULL;
    int number_of_mirrors = 0;
    for (GList *elem = tmp_list_of_urls; elem; elem = g_list_next(elem)) {
        list_of_urls = g_slist_prepend(list_of_urls, elem->data);
        number_of_mirrors++;
    }
    g_list_free(tmp_list_of_urls);

    if (number_of_mirrors <= 1) {
        // Nothing to do
        g_slist_free(list_of_urls);
        g_hash_table_destroy(hosts_ht);
        g_timer_destroy(timer);
        return TRUE;
    }

    // Sort this list by the connection time
    gboolean ret = lr_fastestmirror(main_handle,
                                    &list_of_urls,
                                    err);
    if (!ret) {
        g_debug("%s: lr_fastestmirror failed", __func__);
        g_slist_free(list_of_urls);
        g_hash_table_destroy(hosts_ht);
        g_timer_destroy(timer);
        return FALSE;
    }

    // Apply sorted order to each handle
    for (GSList *ehandle = handles; ehandle; ehandle = g_slist_next(ehandle)) {
        LrHandle *handle = ehandle->data;
        GSList *mirrors = handle->internal_mirrorlist;
        GSList *new_list = NULL;
        for (GSList *elem = list_of_urls; elem; elem = g_slist_next(elem)) {
            gchar *host = elem->data;
            for (GSList *ime = mirrors; ime; ime = g_slist_next(ime)) {
                LrInternalMirror *im = ime->data;
                gchar *im_host = lr_url_without_path(im->url);
                if (!g_strcmp0(im_host, host)) {
                    new_list = g_slist_prepend(new_list, im);
                    // XXX: Maybe convert GSList to GList to make
                    // this delete more efficient
                    mirrors = g_slist_delete_link(mirrors, ime);
                    break;
                }
            }
        }

        // If multiple mirrors with the same lr_url_without_path(url)
        // were present, only the first occurrence was inserted to the
        // the new_list and removed from the mirrors list.
        // The remaining occurences will be moved here.
        for (GSList *elem = mirrors; elem; elem = g_slist_next(elem)) {
            LrInternalMirror *im = elem->data;
            new_list = g_slist_prepend(new_list, im);
        }
        g_slist_free(mirrors);

        // Set sorted list to the handle (reversed, because the items
        // of the new_list were prepended)
        handle->internal_mirrorlist = g_slist_reverse(new_list);
    }

    g_slist_free(list_of_urls);
    g_hash_table_destroy(hosts_ht);

    g_timer_stop(timer);
    g_debug("%s: Duration: %f", __func__, g_timer_elapsed(timer, NULL));
    g_timer_destroy(timer);

    return TRUE;
}
示例#2
0
END_TEST

START_TEST(test_url_without_path)
{
    char *new_url = NULL;

    new_url = lr_url_without_path(NULL);
    fail_if(new_url != NULL);

    new_url = lr_url_without_path("");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, ""));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("hostname");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "hostname"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("hostname/foo/bar/");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "hostname"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("hostname:80");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "hostname:80"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("hostname:80/foo/bar");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "hostname:80"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("http://hostname:80/");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "http://hostname:80"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("http://hostname:80/foo/bar");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "http://hostname:80"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("ftp://foo.hostname:80/foo/bar");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "ftp://foo.hostname:80"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("file:///home/foobar");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "file://"));
    lr_free(new_url);
    new_url = NULL;

    new_url = lr_url_without_path("file:/home/foobar");
    fail_if(new_url == NULL);
    fail_if(strcmp(new_url, "file://"));
    lr_free(new_url);
    new_url = NULL;
}