Beispiel #1
0
static int
load_runs(struct rldb_mysql_cnts *cs)
{
  struct rldb_mysql_state *state = cs->plugin_state;
  struct common_mysql_iface *mi = state->mi;
  struct common_mysql_state *md = state->md;
  struct runlog_state *rls = cs->rl_state;
  struct run_entry_internal ri;
  struct run_entry *re;
  int i, mime_type;
  ruint32_t sha1[5];
  ej_uuid_t run_uuid;

  memset(&ri, 0, sizeof(ri));
  if (mi->fquery(md, RUNS_ROW_WIDTH,
                 "SELECT * FROM %sruns WHERE contest_id=%d ORDER BY run_id ;",
                 md->table_prefix, cs->contest_id) < 0)
    goto fail;
  if (!md->row_count) {
    mi->free_res(md);
    return 0;
  }
  for (i = 0; i < md->row_count; i++) {
    memset(&ri, 0, sizeof(ri));
    memset(sha1, 0, sizeof(sha1));
    memset(&run_uuid, 0, sizeof(run_uuid));
    if (mi->next_row(md) < 0) goto fail;
    mime_type = 0;
    if (mi->parse_spec(md, md->field_count, md->row, md->lengths,
                       RUNS_ROW_WIDTH, runs_spec, &ri) < 0)
      goto fail;
    if (ri.run_id < 0) db_error_inv_value_fail(md, "run_id");
    if (ri.size < 0) db_error_inv_value_fail(md, "size");
    /* FIXME: check ordering on create_time/create_nsec */
    if (ri.create_nsec < 0 || ri.create_nsec > NSEC_MAX)
      db_error_inv_value_fail(md, "create_nsec");
    if (!run_is_valid_status(ri.status))
      db_error_inv_value_fail(md, "status");
    if (ri.status == RUN_EMPTY) {
      xfree(ri.hash); ri.hash = 0;
      xfree(ri.mime_type); ri.mime_type = 0;
      xfree(ri.run_uuid); ri.run_uuid = 0;

      expand_runs(rls, ri.run_id);
      re = &rls->runs[ri.run_id];
      memset(re, 0, sizeof(*re));

      re->run_id = ri.run_id;
      re->time = ri.create_time;
      re->nsec = ri.create_nsec;
      re->status = ri.status;
      continue;
    }
    if (ri.user_id <= 0) db_error_inv_value_fail(md, "user_id");
    if (ri.prob_id < 0) db_error_inv_value_fail(md, "prob_id");
    if (ri.lang_id < 0) db_error_inv_value_fail(md, "lang_id");
    if (ri.hash && parse_sha1(sha1, ri.hash) < 0)
      db_error_inv_value_fail(md, "hash");
    if (ri.run_uuid) {
#if CONF_HAS_LIBUUID - 0 != 0
      uuid_parse(ri.run_uuid, (void*) &run_uuid);
#endif
    }
    //if (ri.ip_version != 4) db_error_inv_value_fail(md, "ip_version");
    if (ri.mime_type && (mime_type = mime_type_parse(ri.mime_type)) < 0)
      db_error_inv_value_fail(md, "mime_type");
    xfree(ri.hash); ri.hash = 0;
    xfree(ri.mime_type); ri.mime_type = 0;
    xfree(ri.run_uuid); ri.run_uuid = 0;

    expand_runs(rls, ri.run_id);
    re = &rls->runs[ri.run_id];

    re->run_id = ri.run_id;
    re->size = ri.size;
    re->time = ri.create_time;
    re->nsec = ri.create_nsec;
    re->user_id = ri.user_id;
    re->prob_id = ri.prob_id;
    re->lang_id = ri.lang_id;
    ipv6_to_run_entry(&ri.ip, re);
    memcpy(re->sha1, sha1, sizeof(re->sha1));
    memcpy(&re->run_uuid, &run_uuid, sizeof(re->run_uuid));
    re->score = ri.score;
    re->test = ri.test_num;
    re->score_adj = ri.score_adj;
    re->locale_id = ri.locale_id;
    re->judge_id = ri.judge_id;
    re->status = ri.status;
    re->is_imported = ri.is_imported;
    re->variant = ri.variant;
    re->is_hidden = ri.is_hidden;
    re->is_readonly = ri.is_readonly;
    re->pages = ri.pages;
    re->ssl_flag = ri.ssl_flag;
    re->mime_type = mime_type;
    re->is_marked = ri.is_marked;
    re->is_saved = ri.is_saved;
    re->saved_status = ri.saved_status;
    re->saved_score = ri.saved_score;
    re->saved_test = ri.saved_test;
    re->passed_mode = ri.passed_mode;
    re->eoln_type = ri.eoln_type;
    re->store_flags = ri.store_flags;
    re->token_flags = ri.token_flags;
    re->token_count = ri.token_count;
  }
  return 1;

 fail:
  xfree(ri.hash);
  xfree(ri.mime_type);
  xfree(ri.run_uuid);
  mi->free_res(md);
  return -1;
}
Beispiel #2
0
void
write_runs_dump(const serve_state_t state, FILE *f, const unsigned char *url,
                unsigned char const *charset)
{
  int total_runs, i, j;
  struct run_entry re;
  struct tm *pts;
  time_t start_time, dur;
  unsigned char *s;
  unsigned char statstr[128];
  time_t tmp_time;

  if (url && *url) {
    fprintf(f, "Content-type: text/plain; charset=%s\n\n", charset);
  }

  fprintf(f,
          "Run_Id"
          ";Time;Nsec;Time2;Date;Year;Mon;Day;Hour;Min;Sec"
          ";Dur;Dur_Day;Dur_Hour;Dur_Min;Dur_Sec"
          ";Size"
          ";IPV6_Flag;IP;SSL_Flag"
          ";Sha1"
          ";User_Id;User_Login;User_Name"
          ";User_Inv;User_Ban;User_Lock"
          ";Prob;Variant"
          ";Lang;Content_Type"
          ";Stat_Short;Status;Score;Score_Adj;Test"
          ";Import_Flag;Hidden_Flag;RO_Flag;Locale_Id;Pages;Judge_Id"
          "\n");

  total_runs = run_get_total(state->runlog_state);
  start_time = run_get_start_time(state->runlog_state);
  for (i = 0; i < total_runs; i++) {
    if (run_get_entry(state->runlog_state, i, &re) < 0) {
      fprintf(f, "%d;Cannot read entry!\n", i);
      continue;
    }
    if (!run_is_valid_status(re.status)) {
      fprintf(f, "%d;Invalid status %d!\n", i, re.status);
      continue;
    }
    if (re.status == RUN_EMPTY) continue;

    fprintf(f, "%d;", i);
    fprintf(f, "%lld;%09d;", re.time, re.nsec);
    tmp_time = re.time;
    pts = localtime(&tmp_time);
    fprintf(f, "%04d%02d%02d%02d%02d%02d;",
            pts->tm_year + 1900,
            pts->tm_mon + 1,
            pts->tm_mday,
            pts->tm_hour,
            pts->tm_min,
            pts->tm_sec);
    fprintf(f, "%04d%02d%02d;",
            pts->tm_year + 1900,
            pts->tm_mon + 1,
            pts->tm_mday);
    fprintf(f, "%04d;%02d;%02d;%02d;%02d;%02d;",
            pts->tm_year + 1900,
            pts->tm_mon + 1,
            pts->tm_mday,
            pts->tm_hour,
            pts->tm_min,
            pts->tm_sec);
    if (state->global->is_virtual) {
      start_time = run_get_virtual_start_time(state->runlog_state, re.user_id);
    }

    dur = re.time - start_time;
    if (dur < 0) dur = 0;
    fprintf(f, "%ld;", dur);
    pts->tm_sec = dur % 60;
    dur /= 60;
    pts->tm_min = dur % 60;
    dur /= 60;
    pts->tm_hour = dur % 24;
    dur /= 24;
    fprintf(f, "%ld;%02d;%02d;%02d;",
            dur, pts->tm_hour, pts->tm_min, pts->tm_sec);

    fprintf(f, "%u;", re.size);
    fprintf(f, "%d;%s;%d;", re.ipv6_flag, xml_unparse_ip(re.a.ip), re.ssl_flag);

    s = (unsigned char*) re.sha1;
    for (j = 0; j < 20; j++) fprintf(f, "%02x", *s++);
    fprintf(f, ";");

    fprintf(f, "%d;", re.user_id);
    if (!(s = teamdb_get_login(state->teamdb_state, re.user_id))) {
      fprintf(f, "!INVALID TEAM!;");
    } else {
      fprintf(f, "%s;", s);
    }
    if (!(s = teamdb_get_name(state->teamdb_state, re.user_id))) {
      fprintf(f, "!INVALID TEAM!;");
    } else {
      fprintf(f, "%s;", s);
    }
    j = teamdb_get_flags(state->teamdb_state, re.user_id);
    s = "";
    if ((j & TEAM_INVISIBLE)) s = "I";
    fprintf(f, "%s;", s);
    s = "";
    if ((j & TEAM_BANNED)) s = "B";
    fprintf(f, "%s;", s);
    s = "";
    if ((j & TEAM_LOCKED)) s = "L";
    fprintf(f, "%s;", s);

    if (re.status == RUN_VIRTUAL_START || re.status == RUN_VIRTUAL_STOP) {
      //fprintf(f, "<problem>;<variant>;<lang_short>;<mime_type>;<short_status>;<status>;<score>;<score_adj>;<test>;<is_imported>;<is_hidden>;<is_readonly>;<locale_id>;<pages>;<judge_id>\n");
      fprintf(f, ";;;;");
      run_status_to_str_short(statstr, sizeof(statstr), re.status);
      fprintf(f, "%s;", statstr);
      run_status_str(re.status, statstr, sizeof(statstr), 0, 0);
      fprintf(f, "%s;", statstr);
      fprintf(f, ";;;;;;;;\n");
      continue;
    }

    if (re.prob_id > 0 && re.prob_id <= state->max_prob
        && state->probs[re.prob_id] && state->probs[re.prob_id]->short_name) {
      fprintf(f, "%s;", state->probs[re.prob_id]->short_name);
    } else {
      fprintf(f, "!INVALID PROBLEM %d!;", re.prob_id);
    }
    fprintf(f, "%d;", re.variant);

    if (!re.lang_id) {
      fprintf(f, ";%s;", mime_type_get_type(re.mime_type));
    } else if (re.lang_id > 0 && re.lang_id <= state->max_lang
        && state->langs[re.lang_id] && state->langs[re.lang_id]->short_name) {
      fprintf(f, "%s;;", state->langs[re.lang_id]->short_name);
    } else {
      fprintf(f, "!INVALID LANGUAGE %d!;", re.lang_id);
    }

    run_status_to_str_short(statstr, sizeof(statstr), re.status);
    fprintf(f, "%s;", statstr);
    run_status_str(re.status, statstr, sizeof(statstr), 0, 0);
    fprintf(f, "%s;", statstr);
    fprintf(f, "%d;%d;", re.score, re.score_adj);
    fprintf(f, "%d;", re.test);
    fprintf(f, "%d;", (re.passed_mode > 0));
    fprintf(f, "%d;", re.is_imported);
    fprintf(f, "%d;", re.is_hidden);
    fprintf(f, "%d;", re.is_readonly);
    fprintf(f, "%d;%d;%d", re.locale_id, re.pages, re.judge_id);

    fprintf(f, "\n");
  }
}