Пример #1
0
static void BuildList( void ) {
    int numDirs, numDemos;
    void **dirlist, **demolist;
    char *cache, *p;
    unsigned flags;
    size_t len;
    int i;

    // this can be a lengthy process
    S_StopAllSounds();
    m_demos.menu.status = "Building list...";
    SCR_UpdateScreen();

    // list files
    flags = ui_listalldemos->integer ? 0 : FS_TYPE_REAL | FS_PATH_GAME;
    dirlist = FS_ListFiles( m_demos.browse, NULL, flags |
        FS_SEARCH_DIRSONLY, &numDirs );
    demolist = FS_ListFiles( m_demos.browse, DEMO_EXTENSIONS, flags |
        FS_SEARCH_EXTRAINFO, &numDemos );

    // alloc entries
    m_demos.list.items = UI_Malloc( sizeof( demoEntry_t * ) * ( numDirs + numDemos + 1 ) );
    m_demos.list.numItems = 0;
    m_demos.list.curvalue = 0;
    m_demos.list.prestep = 0;

    m_demos.widest_map = 3;
    m_demos.widest_pov = 3;
    m_demos.total_bytes = 0;

    // start with minimum size
    m_demos.menu.size( &m_demos.menu );

    if( m_demos.browse[0] ) {
        BuildDir( "..", ENTRY_UP );
    }

    // add directories
    if( dirlist ) {
        for( i = 0; i < numDirs; i++ ) {
            BuildDir( dirlist[i], ENTRY_DN );
        }
        FS_FreeList( dirlist );
    }    

    m_demos.numDirs = m_demos.list.numItems;

    // add demos
    if( demolist ) {
        CalcHash( demolist );
        if( ( cache = LoadCache( demolist ) ) != NULL ) {
            p = cache + 32 + 1;
            for( i = 0; i < numDemos; i++ ) {
                BuildName( demolist[i], &p );
            }
            FS_FreeFile( cache );
        } else {
            for( i = 0; i < numDemos; i++ ) {
                BuildName( demolist[i], NULL );
                if( ( i & 7 ) == 0 ) {
                    m_demos.menu.size( &m_demos.menu );
                    SCR_UpdateScreen();
                }
            }
        }
        WriteCache();
        FS_FreeList( demolist );
    }

    // update status line and sort
    if( m_demos.list.numItems ) {
        Change( &m_demos.list.generic );
        if( m_demos.list.sortdir ) {
            m_demos.list.sort( &m_demos.list, m_demos.list.sortcol );
        }
    }

    // resize columns
    m_demos.menu.size( &m_demos.menu );

    // format our extra status line
    i = m_demos.list.numItems - m_demos.numDirs;
    len = Q_scnprintf( m_demos.status, sizeof( m_demos.status ),
        "%d demo%s, ", i, i == 1 ? "" : "s" );
    Com_FormatSizeLong( m_demos.status + len, sizeof( m_demos.status ) - len,
        m_demos.total_bytes );
        
    SCR_UpdateScreen();
}
Пример #2
0
// A download finished, find out what it was, whether there were any errors and
// if so, how severe. If none, rename file and other such stuff.
static qboolean finish_download(void)
{
    int         msgs_in_queue;
    CURLMsg     *msg;
    CURLcode    result;
    dlhandle_t  *dl;
    CURL        *curl;
    long        response;
    double      sec, bytes;
    char        size[16], speed[16];
    char        temp[MAX_OSPATH];
    qboolean    fatal_error = qfalse;
    const char  *err;
    print_type_t level;

    do {
        msg = curl_multi_info_read(curl_multi, &msgs_in_queue);
        if (!msg)
            break;

        if (msg->msg != CURLMSG_DONE)
            continue;

        curl = msg->easy_handle;
        dl = find_handle(curl);

        cls.download.current = NULL;
        cls.download.percent = 0;

        //filelist processing is done on read
        if (dl->file) {
            fclose(dl->file);
            dl->file = NULL;
        }

        curl_handles--;

        result = msg->data.result;

        switch (result) {
            //for some reason curl returns CURLE_OK for a 404...
        case CURLE_HTTP_RETURNED_ERROR:
        case CURLE_OK:
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
            if (result == CURLE_OK && response == 200) {
                //success
                break;
            }

            err = http_strerror(response);

            //404 is non-fatal
            if (response == 404) {
                level = PRINT_ALL;
                goto fail1;
            }

            //every other code is treated as fatal
            //not marking download as done since
            //we are falling back to UDP
            level = PRINT_ERROR;
            fatal_error = qtrue;
            goto fail2;

        case CURLE_COULDNT_RESOLVE_HOST:
        case CURLE_COULDNT_CONNECT:
        case CURLE_COULDNT_RESOLVE_PROXY:
            //connection problems are fatal
            err = curl_easy_strerror(result);
            level = PRINT_ERROR;
            fatal_error = qtrue;
            goto fail2;

        default:
            err = curl_easy_strerror(result);
            level = PRINT_WARNING;
fail1:
            //we mark download as done even if it errored
            //to prevent multiple attempts.
            CL_FinishDownload(dl->queue);
fail2:
            Com_LPrintf(level,
                        "[HTTP] %s [%s] [%d remaining file%s]\n",
                        dl->queue->path, err, cls.download.pending,
                        cls.download.pending == 1 ? "" : "s");
            if (dl->path[0]) {
                remove(dl->path);
                dl->path[0] = 0;
            }
            if (dl->buffer) {
                Z_Free(dl->buffer);
                dl->buffer = NULL;
            }
            curl_multi_remove_handle(curl_multi, curl);
            continue;
        }

        //mark as done
        CL_FinishDownload(dl->queue);

        //show some stats
        curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &sec);
        curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &bytes);
        if (sec < 0.001)
            sec = 0.001;
        Com_FormatSizeLong(size, sizeof(size), bytes);
        Com_FormatSizeLong(speed, sizeof(speed), bytes / sec);

        //FIXME:
        //technically i shouldn't need to do this as curl will auto reuse the
        //existing handle when you change the url. however, the curl_handles goes
        //all weird when reusing a download slot in this way. if you can figure
        //out why, please let me know.
        curl_multi_remove_handle(curl_multi, curl);

        Com_Printf("[HTTP] %s [%s, %s/sec] [%d remaining file%s]\n",
                   dl->queue->path, size, speed, cls.download.pending,
                   cls.download.pending == 1 ? "" : "s");

        if (dl->path[0]) {
            //rename the temp file
            Q_snprintf(temp, sizeof(temp), "%s/%s", fs_gamedir, dl->queue->path);

            if (rename(dl->path, temp))
                Com_EPrintf("[HTTP] Failed to rename '%s' to '%s': %s\n",
                            dl->path, dl->queue->path, strerror(errno));
            dl->path[0] = 0;

            //a pak file is very special...
            if (dl->queue->type == DL_PAK) {
                CL_RestartFilesystem(qfalse);
                rescan_queue();
            }
        } else if (!fatal_error) {
            parse_file_list(dl);
        }
    } while (msgs_in_queue > 0);

    //fatal error occured, disable HTTP
    if (fatal_error) {
        abort_downloads();
        return qfalse;
    }

    // see if we have more to dl
    CL_RequestNextDownload();
    return qtrue;
}