Example #1
0
void
pci_id_cache_flush(struct pci_access *a)
{
  int orig_status = a->id_cache_status;
  FILE *f;
  unsigned int h;
  struct id_entry *e, *e2;
  char hostname[256], *tmpname, *name;
  int this_pid;

  a->id_cache_status = 0;
  if (orig_status < 2)
    return;
  name = get_cache_name(a);
  if (!name)
    return;

  this_pid = getpid();
  if (gethostname(hostname, sizeof(hostname)) < 0)
    hostname[0] = 0;
  else
    hostname[sizeof(hostname)-1] = 0;
  tmpname = pci_malloc(a, strlen(name) + strlen(hostname) + 64);
  sprintf(tmpname, "%s.tmp-%s-%d", name, hostname, this_pid);

  f = fopen(tmpname, "wb");
  if (!f)
    {
      a->warning("Cannot write to %s: %s", name, strerror(errno));
      pci_mfree(tmpname);
      return;
    }
  a->debug("Writing cache to %s\n", name);
  fprintf(f, "%s\n", cache_version);

  for (h=0; h<HASH_SIZE; h++)
    for (e=a->id_hash[h]; e; e=e->next)
      if (e->src == SRC_CACHE || e->src == SRC_NET)
	{
	  /* Negative entries are not written */
	  if (!e->name[0])
	    continue;

	  /* Verify that every entry is written at most once */
	  for (e2=a->id_hash[h]; e2 != e; e2=e2->next)
	    if ((e2->src == SRC_CACHE || e2->src == SRC_NET) &&
	        e2->cat == e->cat &&
		e2->id12 == e->id12 && e2->id34 == e->id34)
	    break;
	  if (e2 == e)
	    fprintf(f, "%d %x %x %x %x %s\n",
	            e->cat,
		    pair_first(e->id12), pair_second(e->id12),
		    pair_first(e->id34), pair_second(e->id34),
		    e->name);
	}

  fflush(f);
  if (ferror(f))
    a->warning("Error writing %s", name);
  fclose(f);

  if (rename(tmpname, name) < 0)
    {
      a->warning("Cannot rename %s to %s: %s", tmpname, name, strerror(errno));
      unlink(tmpname);
    }
  pci_mfree(tmpname);
}
Example #2
0
 expr apply(expr const & a) {
     bool sh = false;
     if (is_shared(a)) {
         auto r = m_cache.find(a.raw());
         if (r != m_cache.end())
             return r->second;
         sh = true;
     }
     switch (a.kind()) {
     case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
         return save_result(a, copy(a), sh);
     case expr_kind::App: {
         buffer<expr> new_args;
         for (expr const & old_arg : args(a))
             new_args.push_back(apply(old_arg));
         return save_result(a, mk_app(new_args), sh);
     }
     case expr_kind::HEq:      return save_result(a, mk_heq(apply(heq_lhs(a)), apply(heq_rhs(a))), sh);
     case expr_kind::Pair:     return save_result(a, mk_pair(apply(pair_first(a)), apply(pair_second(a)), apply(pair_type(a))), sh);
     case expr_kind::Proj:     return save_result(a, mk_proj(proj_first(a), apply(proj_arg(a))), sh);
     case expr_kind::Lambda:   return save_result(a, mk_lambda(abst_name(a), apply(abst_domain(a)), apply(abst_body(a))), sh);
     case expr_kind::Pi:       return save_result(a, mk_pi(abst_name(a), apply(abst_domain(a)), apply(abst_body(a))), sh);
     case expr_kind::Sigma:    return save_result(a, mk_sigma(abst_name(a), apply(abst_domain(a)), apply(abst_body(a))), sh);
     case expr_kind::Let:      return save_result(a, mk_let(let_name(a), apply(let_type(a)), apply(let_value(a)), apply(let_body(a))), sh);
     case expr_kind::MetaVar:
         return save_result(a,
                            update_metavar(a, [&](local_entry const & e) -> local_entry {
                                    if (e.is_inst())
                                        return mk_inst(e.s(), apply(e.v()));
                                    else
                                        return e;
                                }),
                            sh);
     }
     lean_unreachable(); // LCOV_EXCL_LINE
 }
Example #3
0
#include "fntest.h"



START_TEST(test_pairfunc)
{
    pair_t *pair = pair_new(int, double);
    int vali;
    double vald;
    assert(_PAIR_STNODE(pair)->type == _get_type_bystr(_PAIR_TYPE_NAME));
    ck_assert_ptr_eq(_PAIR_CTYPE(pair, 0), _get_type_bystr("int"));
    ck_assert_ptr_eq(_PAIR_CTYPE(pair, 1), _get_type_bystr("double"));

    pair_make(pair, 1, 3.5);
    ck_assert_int_eq(*(int*)pair_first(pair), 1);
    ck_assert_int_eq(*(double*)pair_second(pair) == 3.5, true);

    pair_make(pair, 2, 4.5);
    ck_assert_int_eq(*(int*)pair_first(pair), 2);
    ck_assert_int_eq(*(double*)pair_second(pair) == 4.5, true);

    pair_first_value(pair, &vali);
    pair_second_value(pair, &vald);
    ck_assert_int_eq(vali, 2);
    ck_assert_int_eq(vald == 4.5, true);

    pair_t *pair2 = pair_new(int, double);
    pair_assign(pair2, pair);
    ck_assert_int_eq(*(int*)pair_first(pair2), 2);
    ck_assert_int_eq(*(double*)pair_second(pair2) == 4.5, true);
Example #4
0
/**
 * @brief perform the analysis of a given list of files
 *
 * loops over the file_list given and performs the analysis of all the files,
 * when finished simply check if the results should be entered into the
 * database, this is indicated by the second element of the pair not being NULL
 *
 * @param pgConn the connection to the database
 * @param copy the copyright instance to use to perform the analysis
 * @param current_file the file and the pfile_pk that is currently being analyzed
 * @param agent_pk the primary key for this agent, use to enter info into the database
 * @param mout a logging file to used for debugging
 * @param type report_type binary number xyz, 0x1(bit z): statement, 0x2(bit y): url, 0x4(bit x): email
 */
void perform_analysis(PGconn* pgConn, copyright copy, pair current_file, long agent_pk, FILE* mout, int report_type)
{
  /* locals */
  char sql[2048];               // buffer to hold the sql commands
  char buf[2048];               // buffer to hold string that have been escaped for sql
  char hash[256];               // holds the hash of the copyright string for entry into database
  char* file_name;              // holds the name of the file to open
  copyright_iterator finds;     // an iterator to access the copyrights
  FILE* input_fp;               // the file that will be analyzed

  /* initialize memory */
  memset(sql, 0, sizeof(sql));
  file_name = NULL;
  finds = NULL;
  input_fp = NULL;

  /* find the correct path to the file */
  if(*(int*)pair_second(current_file) >= 0)
  {
    file_name = fo_RepMkPath("files", (char*)pair_first(current_file));
  }
  else
  {
    file_name = (char*)pair_first(current_file);
  }

  /* attempt to open the file */
  input_fp = fopen(file_name, "rb");
  if(!input_fp)
  {
    fprintf(cerr, "ERROR %s.%d Failure to open file %s\n", __FILE__, __LINE__, file_name);
    fprintf(cerr, "ERROR %s\n", strerror(errno));
    fflush(cerr);
    copyright_clear(copy);
    return;
  }

  /* only free temp if running as an agent */
  if(*(int*)pair_second(current_file) >= 0)
  {
    free(file_name);
  }

  /* perform the actual analysis */
  copyright_analyze(copy, input_fp, report_type);

  /* if running command line, print file name */
  if(*(int*)pair_second(current_file) < 0)
  {
    fprintf(cout, "%s ::\n", (char*)pair_first(current_file));
  }

  /* loop across the found copyrights */
  if(copyright_size(copy) > 0)
  {
    for(finds = copyright_begin(copy); finds != copyright_end(copy); finds++)
    {
      copy_entry entry = *finds;

      if(verbose)
      {
        fprintf(mout, "=== %s ==============================================\n",
            (char*)pair_first(current_file));
        fprintf(mout, "DICT: %s\nNAME: %s\nTEXT[%s]\n",
            copy_entry_dict(entry),
            copy_entry_name(entry),
            copy_entry_text(entry));
      }

      if(*(int*)pair_second(current_file) >= 0)
      {
        /* ensure legal sql */
        escape_string(pgConn, buf, copy_entry_text(entry), sizeof(buf));

        /* get the hash for the string */
        sprintf(hash, "0x%lx", hash_string(copy_entry_text(entry)));

        /* place the copyright in the table */
        memset(sql, '\0', sizeof(sql));
        snprintf(sql, sizeof(sql), "%ld\t%d\t%d\t%d\t%s\t%s\t%s\n",
            agent_pk,
            *(int*)pair_second(current_file),
            copy_entry_start(entry),
            copy_entry_end(entry),
            buf,
            hash,
            copy_entry_type(entry));

        fo_sqlCopyAdd(sqlcpy, sql);
      }
      else
      {
        fprintf(cout, "\t[%d:%d:%s] '%s'",
            copy_entry_start(entry), copy_entry_end(entry),
            copy_entry_type(entry), copy_entry_text(entry));
        if(copy_entry_text(entry)[strlen(copy_entry_text(entry)) - 1] != '\n')
        {
          fprintf(cout, "\n");
        }
      }
    }
  }

  fclose(input_fp);
  fo_scheduler_heart(1);
}