示例#1
0
static void menu_cardview_open_cb(GtkWidget *w, gpointer user_data)
{
    char **select_info;
    a_string_t *command;
    char *filename;
    UNUSED(w);
    UNUSED(user_data);

    select_info = ui_select_file("Load xml card description",path_config_get_string(PATH_CONFIG_FOLDER_WORKING),NULL);

    if (select_info[1])
    {
        path_config_set_string(PATH_CONFIG_FOLDER_WORKING,select_info[0]);
        filename = luax_escape_string(select_info[1]);
        command=a_strnew(NULL);
        a_sprintf(command,"ui.load_view(\"%s\")",filename);
        luax_run_command(a_strval(command));
        a_strfree(command);
        g_free(select_info[0]);
        g_free(select_info[1]);
        g_free(filename);
    }
}
int cardpeek_update_perform(void)
{
    const char* cardpeek_update_file = path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_UPDATE);
    a_string_t *contents;
    update_t *update;
    int remove;
    update_item_t *item;
    time_t now = time(NULL);
    int updated = 0;
    char *url = NULL;
    char *local_file;
    char *local_dnld;
    unsigned char digest[SHA256_DIGEST_LENGTH];
    a_string_t *url_request;
    unsigned first_update;

    first_update = (unsigned)luax_variable_get_integer("cardpeek.updates.first_update");
    
    /* STEP 1: get cardpeek.update file */

    url=luax_variable_get_strdup("cardpeek.updates.url");

    if (url==NULL)
        url = g_strdup(DEFAULT_UPDATE_URL);

    log_printf(LOG_INFO,"Fetching '%s'",url);

    url_request = a_strnew(NULL);
    a_sprintf(url_request,"%s?u=%x&v=%s",url,first_update,VERSION);

    if (http_download(a_strval(url_request),cardpeek_update_file)==0)
    {
        g_free(url);
        return 0;
    }
    g_free(url);
    a_strfree(url_request);


    /* STEP 2: parse file */

    if ((contents=file_get_contents(cardpeek_update_file))==NULL)
    {
        log_printf(LOG_ERROR,"failed to read update file information.");
        unlink(cardpeek_update_file);
        return 0;
    }

    update = update_new();

    if ((update_load(update,a_strval(contents),a_strlen(contents)))==0)
    {
        unlink(cardpeek_update_file);
        a_strfree(contents);
        update_free(update);
        return 0;
    }
    a_strfree(contents);

    /* log_printf(LOG_DEBUG,"Updates correctly loaded from '%s'",cardpeek_update_file); */
    if ((remove = update_filter_version(update,VERSION))>0)
        log_printf(LOG_WARNING,"%d updates will not be installed because they require a newer version of Cardpeek.");
    
    remove = update_filter_files(update);

    if (update->item_count)
        log_printf(LOG_INFO,"A total of %d files will be updated, %d files are kept unchanged.",update->item_count,remove);
    else
        log_printf(LOG_INFO,"No files will be updated, %d files are kept unchanged.",remove);

    item = update->items;

    while (item)
    {
        local_dnld = new_path(PATH_CONFIG_FOLDER_CARDPEEK,item->file,".download");
        local_file = new_path(PATH_CONFIG_FOLDER_CARDPEEK,item->file,NULL);

        if (http_download(item->url,local_dnld)!=0)
        {        
            if (sha256sum(local_dnld,digest))
            {
                if (memcmp(digest,bytestring_get_data(&item->digest),SHA256_DIGEST_LENGTH)==0)
                {
                    unlink(local_file);

                    if (rename(local_dnld,local_file)==0)
                    {
                        log_printf(LOG_INFO,"Successfuly updated %s", local_file);
                        updated++;
                    }
                    else
                    {
                        log_printf(LOG_ERROR,"Failed to copy %s to %s: %s", 
                                local_dnld,
                                local_file, 
                                strerror(errno));
                    }
                }
                else
                {
                    log_printf(LOG_WARNING,"File %s was not updated: authentication failed.",local_file);
                }
            }
            unlink(local_dnld);
        }
        g_free(local_dnld);
        g_free(local_file);
        item =  item->next; 
    }

    if (updated == update->item_count)
    {    
        luax_variable_set_integer("cardpeek.updates.next_update",(int)(now+7*(24*3600)));  
        luax_config_table_save();
    }

    unlink(cardpeek_update_file);
    update_free(update);

    /* STEP 3: finish */

    return 1;

}
示例#3
0
static int install_dot_file(void)
{
    const char* cardpeek_dir = path_config_get_string(PATH_CONFIG_FOLDER_CARDPEEK);
    const char* old_replay_dir = path_config_get_string(PATH_CONFIG_FOLDER_OLD_REPLAY);
    const char* new_replay_dir = path_config_get_string(PATH_CONFIG_FOLDER_REPLAY);
    const char* version_file = path_config_get_string(PATH_CONFIG_FILE_VERSION);
    GStatBuf sbuf;
    FILE* f;
    int status;
    a_string_t* astr;
    unsigned dot_version=0;
    int response;
    GResource* cardpeek_resources;
    GBytes* dot_cardpeek_tar_gz;
    unsigned char *dot_cardpeek_tar_gz_start;
    gsize dot_cardpeek_tar_gz_size;

    if (g_stat(cardpeek_dir,&sbuf)==0)
    {
        log_printf(LOG_DEBUG,"Found directory '%s'",cardpeek_dir);

        if ((f = g_fopen(version_file,"r"))!=NULL)
        {
            if (fscanf(f,"%u",&dot_version)!=1)
		dot_version=0;
            fclose(f);
            if (dot_version>=SCRIPT_VERSION)
            {
                log_printf(LOG_DEBUG,"Scripts are up to date.");
                return 1;
            }
        }
        astr = a_strnew(NULL);

        if (dot_version==0 && f==NULL)
            a_sprintf(astr,"This seems to be the first time you run Cardpeek, because '%s' does not exist\n"
                      "Do you want to install the necessary files in '%s'?",version_file,cardpeek_dir);
        else
            a_sprintf(astr,"Some scripts in '%s' seem to be outdated or missing\n"
                      "Do you want to upgrade these scripts?",cardpeek_dir);

        if ((response = gui_question(a_strval(astr),"Yes","No","No, don't ask me again",NULL))!=0)
        {
            log_printf(LOG_DEBUG,"The files in '%s' will not be upgraded.",cardpeek_dir);
            a_strfree(astr);

            if (response==2)
            {
                if ((f=g_fopen(version_file,"w"))!=NULL)
                {
                    fprintf(f,"%u\n",SCRIPT_VERSION);
                    fclose(f);
                }
            }
            return 0;
        }
        a_strfree(astr);
    }
    else
    {
        astr = a_strnew(NULL);
        a_sprintf(astr,"It seems this is the first time you run Cardpeek, because \n'%s' does not exit (%s).\n"
                  "Do you want to create '%s'?",cardpeek_dir,strerror(errno),cardpeek_dir);

        if (gui_question(a_strval(astr),"Yes","No",NULL)!=0)
        {
            log_printf(LOG_DEBUG,"'%s' will not be created",cardpeek_dir);
            a_strfree(astr);

            return 0;
        }

        a_strfree(astr);

#ifndef _WIN32
        if (mkdir(cardpeek_dir,0770)!=0)
#else
        if (mkdir(cardpeek_dir)!=0)
#endif
	{
            astr = a_strnew(NULL);
            a_sprintf(astr,"'%s' could not be created: %s",cardpeek_dir,strerror(errno));
            log_printf(LOG_ERROR,a_strval(astr));
            gui_question(a_strval(astr),"OK",NULL);
            a_strfree(astr);
            return 0;
        }
    }

    if (g_stat(old_replay_dir,&sbuf)==0)
    {
        if (rename(old_replay_dir,new_replay_dir)==0)
        {
            log_printf(LOG_INFO,"Renamed %s to %s.",
                       old_replay_dir, new_replay_dir);
        }
        else
        {
            log_printf(LOG_WARNING,"Failed to rename %s to %s: %s",
                       old_replay_dir, new_replay_dir, strerror(errno));
        }
    }

    cardpeek_resources = cardpeek_resources_get_resource();
    if (cardpeek_resources == NULL)
    {
        log_printf(LOG_ERROR,"Could not load cardpeek internal resources. This is not good.");
        return -1;
    }
    dot_cardpeek_tar_gz = g_resources_lookup_data("/cardpeek/dot_cardpeek.tar.gz",G_RESOURCE_LOOKUP_FLAGS_NONE,NULL);
    if (dot_cardpeek_tar_gz == NULL)
    {
        log_printf(LOG_ERROR,"Could not load .cardpeek.tar.gz");
        return -1;
    }
    dot_cardpeek_tar_gz_start = (unsigned char *)g_bytes_get_data(dot_cardpeek_tar_gz,&dot_cardpeek_tar_gz_size);

    if (chdir(cardpeek_dir)==-1)
    {
	log_printf(LOG_ERROR,"Could not change directory to '%s'",cardpeek_dir);
	return 0;
    }

    if ((f = g_fopen("dot_cardpeek.tar.gz","wb"))==NULL)
    {
        log_printf(LOG_ERROR,"Could not create dot_cardpeek.tar.gz in %s (%s)", cardpeek_dir, strerror(errno));
        gui_question("Could not create dot_cardpeek.tar.gz, aborting.","Ok",NULL);
        return 0;
    }

    if (fwrite(dot_cardpeek_tar_gz_start,dot_cardpeek_tar_gz_size,1,f)!=1)
    {
        log_printf(LOG_ERROR,"Could not write to dot_cardpeek.tar.gz in %s (%s)", cardpeek_dir, strerror(errno));
        gui_question("Could not write to dot_cardpeek.tar.gz, aborting.","Ok",NULL);
        fclose(f);
        return 0;
    }
    log_printf(LOG_DEBUG,"Wrote %i bytes to dot_cardpeek.tar.gz",dot_cardpeek_tar_gz_size);
    fclose(f);

    g_bytes_unref(dot_cardpeek_tar_gz);

    log_printf(LOG_INFO,"Created dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"Creating files in %s", cardpeek_dir);
    status = system("tar xzvf dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"'tar xzvf dot_cardpeek.tar.gz' returned %i",status);
    if (status!=0)
    {
        gui_question("Extraction of dot_cardpeek.tar.gz failed, aborting.","Ok",NULL);
        return 0;
    }
    status = system("rm dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"'rm dot_cardpeek.tar.gz' returned %i",status);

    gui_question("Note: The files have been created.\nIt is recommended that you quit and restart cardpeek, for changes to take effect.","Ok",NULL);
    return 1;
}