예제 #1
0
/****************  main  *******************/
int main(int argc, char** argv)
{
  PGconn* pgConn;
  PGresult* result;
  psqlCopy_t pCopy;
  char* TestTable = "TestsqlCopy";
  char col_vc[40] = "This is \n\r column vc[40] 123456789\r";
  char* col_text;
  char* DataBuf;
  int datasize;
  char sql[2048];
  int NumColumns = 3;
  int CopyBufSize;
  int RowsToTest;
  int NumTextBytes;
  int RowNum;
  clock_t StartTime, EndTime;
  char* DBConfFile = NULL;  /* use default Db.conf */
  char* ErrorBuf;

  if (argc != 4)
  {
    printf("Usage: %s RowsToTest NumTextBytes CopyDataBufferSize\n", argv[0]);
    exit(-1);
  }

  /* first argument is the number of rows to test, 
   * the second is the number of bytes to use for col_text
   * third is the Copy data buffer size
   */
  RowsToTest = atoi(argv[1]);
  NumTextBytes = atoi(argv[2]);
  CopyBufSize = atoi(argv[3]);

  /* Populate test data */
  col_text = GetTextCol(NumTextBytes);
  datasize = NumTextBytes + 8 + 40 + 1;
  DataBuf = calloc(datasize, sizeof(char));
  if (!DataBuf)
  {
    ERROR_RETURN("Allocating test data buffer failed.")
    exit(-2);
  }

  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);

  /* Create a test table to populate */
  snprintf(sql, sizeof(sql), "create table %s (col_int integer, col_text text, col_vc varchar(40))", TestTable);
  result = PQexec(pgConn, sql);
  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);

  /* Start timer */
  StartTime = clock();

  /* Create the pCopy */
  pCopy = fo_sqlCopyCreate(pgConn, TestTable, CopyBufSize, NumColumns,
    "col_int", "col_text", "col_vc");
  if (!pCopy) exit(1);  /* CopyCreate prints errors to stdout */

  /* Add data */
  for (RowNum = 0; RowNum < RowsToTest; RowNum++)
  {
    snprintf(DataBuf, datasize, "%d\t%s\t%s\n", RowNum, col_text, col_vc);
    fo_sqlCopyAdd(pCopy, DataBuf);
  }
  free(col_text);

  /* Destroy - flushes remaining data and frees */
  fo_sqlCopyDestroy(pCopy, 1);

  /* Print run time for the load (whole Create/Add/Destroy cycle). */
  EndTime = clock();
  printf("%.6f Seconds to load.\n", ((double) (EndTime - StartTime)) / CLOCKS_PER_SEC);

  /* Verify that the right number of records were loaded */
  snprintf(sql, sizeof(sql), "select count(*) from %s", TestTable);
  result = PQexec(pgConn, sql);
  fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__);
  printf("%d records inserted, %d expected\n",
    atoi(PQgetvalue(result, 0, 0)),
    RowsToTest);
  PQclear(result);

  /* Remove the test table */
/*
  snprintf(sql, sizeof(sql), "drop table %s", TestTable);
  result = PQexec(pgConn, sql);
  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);
*/

  PQfinish(pgConn);
  return (0);
}
예제 #2
0
파일: main.c 프로젝트: gerv/fossology
/**
 * @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);
}