Example #1
0
int write_record(FILE *f, DataRecord *r) 
{
    int size;
    char *data = encode_record(r, &size);
    if (fwrite(data, 1, size, f) < size){
        fprintf(stderr, "write %d byte failed\n", size);
        free(data);
        return -1;
    }
    free(data);
    return 0;
}
Example #2
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeT01(EmbPattern* pattern, const char* fileName)
{
	EmbRect boundingRect;
	EmbFile* file = 0;
	int xx, yy, dx, dy, flags;
	int co = 1, st = 0;
	int ax, ay, mx, my;
	EmbStitchList* pointer = 0;
	
	if (!embStitchList_count(pattern->stitchList))
	{
		embLog_error("format-t01.c writeDst(), pattern contains no stitches\n");
		return 0;
	}

	/* Check for an END stitch and add one if it is not present */
	if (pattern->lastStitch->stitch.flags != END)
		embPattern_addStitchRel(pattern, 0, 0, END, 1);

	file = embFile_open(fileName, "wb");
	if (!file)
	{
		embLog_error("format-t01.c writet01(), cannot open %s for writing\n", fileName);
		return 0;
	}

	embPattern_correctForMaxStitchLength(pattern, 12.1, 12.1);

	xx = yy = 0;
	co = 1;
	co = embThreadList_count(pattern->threadList);
	st = 0;
	st = embStitchList_count(pattern->stitchList);
	flags = NORMAL;
	boundingRect = embPattern_calcBoundingBox(pattern);
	ax = ay = mx = my = 0;
	xx = yy = 0;
	pointer = pattern->stitchList;
	while (pointer)
	{
		/* convert from mm to 0.1mm for file format */
		dx = roundDouble(pointer->stitch.xx * 10.0) - xx;
		dy = roundDouble(pointer->stitch.yy * 10.0) - yy;
		xx = roundDouble(pointer->stitch.xx * 10.0);
		yy = roundDouble(pointer->stitch.yy * 10.0);
		flags = pointer->stitch.flags;
		encode_record(file, dx, dy, flags);
		pointer = pointer->next;
	}
	embFile_close(file);
	return 1;
}
Example #3
0
void encode_message(stru_message *message, unsigned char *data)
{
    // assert sizeof(data) >= getEncodedSize()
    unsigned char* data_ptr = &data[0];

    for(int i = 0; i < message->recordCount; i++)
    {
    	encode_record(message->records[i], data_ptr, i == 0, (i + 1) == message->recordCount);

        // can NdefRecord.encode return the record size?
    	data_ptr += get_encoded_record_size(message->records[i]);
    }
}
Example #4
0
static int
store_record(glite_renewal_core_context ctx, char *basename, proxy_record *record)
{
   int stored = 0;
   FILE *fd = NULL;
   int temp;
   char *new_line = NULL;
   int ret, i;
   char *p;
   proxy_record tmp_record;
   char tmp_file[FILENAME_MAX];
   char meta_file[FILENAME_MAX];
   int line_num = 0;
   char *line = ctx->buffer;

   assert (record != NULL);

   memset(&tmp_record, 0, sizeof(tmp_record));

   snprintf(meta_file, sizeof(meta_file), "%s.data", basename);
   snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", meta_file);

   temp = mkstemp(tmp_file);
   if (temp < 0)
      return errno;

   fd = fopen(meta_file, "r");
   if (fd == NULL) {
      ret = errno;
      goto end;
   }
   while (fgets(line, ctx->bufsize, fd) != NULL) {
      line_num++;
      free_record(ctx, &tmp_record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &tmp_record);
      if (ret) {
	 edg_wlpr_Log(ctx, LOG_WARNING, "Removing invalid entry at line %d in %s", line_num, basename);
	 continue;
      }
      if (record->suffix == tmp_record.suffix &&
	  record->unique == tmp_record.unique) {
	 free_record(ctx, &tmp_record);
	 tmp_record = *record;
	 tmp_record.myproxy_server = strdup(record->myproxy_server);
	 tmp_record.jobids.len = 0;
	 tmp_record.jobids.val = NULL;
	 for (i = 0; i < record->jobids.len; i++) {
	    realloc_prd_list(ctx, &tmp_record.jobids);
	    tmp_record.jobids.val[tmp_record.jobids.len - 1] = 
	       strdup(record->jobids.val[i]);
	 }
	 if (record->fqans)
	     tmp_record.fqans = strdup(record->fqans);
	 stored = 1;
      }
      ret = encode_record(ctx, &tmp_record, &new_line);
      if (ret)
	 goto end;
      dprintf(temp, "%s\n", new_line);
      save_jobids(ctx, basename, &tmp_record);
      free(new_line);
      new_line = NULL;
   }
   if (! stored) {
      ret = encode_record(ctx, record, &new_line);
      if (ret)
	 goto end;
      ret = dprintf(temp, "%s\n", new_line);
      save_jobids(ctx, basename, record);
      free(new_line);
      new_line = NULL;
   }
   fclose(fd); fd = NULL;
   close(temp);

   ret = rename(tmp_file, meta_file);
   if (ret)
      ret = errno;

end:
   free_record(ctx, &tmp_record);
   if (fd)
      fclose(fd);
   close(temp);
   return ret;
}
Example #5
0
void
update_db(glite_renewal_core_context ctx, edg_wlpr_Request *request, edg_wlpr_Response *response)
{
   FILE *fd = NULL;
   int tmp_fd = -1;
   int suffix = -1;
   char tmp_file[FILENAME_MAX];
   char cur_proxy[FILENAME_MAX];
   char datafile[FILENAME_MAX];
   char *new_line = NULL;
   char *basename, *proxy = NULL;
   char **entry;
   proxy_record record;
   int ret;
   char *p;
   time_t current_time;
   char *line = ctx->buffer;

   memset(&record, 0, sizeof(record));

   edg_wlpr_Log(ctx, LOG_DEBUG, "UPDATE_DB request for %s", request->proxy_filename);

   chdir(repository);
   basename = request->proxy_filename;

   snprintf(datafile, sizeof(datafile), "%s.data", basename);
   fd = fopen(datafile, "r");
   if (fd == NULL) {
      edg_wlpr_Log(ctx, LOG_ERR, "Cannot open meta file %s (%s)",
	           datafile, strerror(errno));
      ret = errno;
      return;
   }

   snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", datafile);
   tmp_fd = mkstemp(tmp_file);
   if (tmp_fd < 0) {
      edg_wlpr_Log(ctx, LOG_ERR, "Cannot create temporary file (%s)",
	           strerror(errno));
      ret = errno;
      goto end;
   }

   entry = request->entries;
   if (entry) {
      p = strchr(*entry, ':');
      *p = '\0';
      suffix = atoi(*entry);
      proxy = p+1;
   }

   current_time = time(NULL);

   while (fgets(line, ctx->bufsize, fd) != NULL) {
      free_record(ctx, &record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &record);
      if (ret)
	 goto end;
      
      if (record.suffix > suffix && entry && *entry) {
	 do {
	    entry++;
	    if (entry == NULL || *entry == NULL) {
	       suffix = -1;
	       break;
	    }
	    
	    p = strchr(*entry, ':');
	    suffix = atoi(*entry);
	    proxy = p+1;
	 } while (record.suffix > suffix);
      }

      if (record.suffix == suffix) {
	 snprintf(cur_proxy, sizeof(cur_proxy), "%s.%d", basename, suffix);
	 if (proxy == NULL || *proxy == '\0') {
	    /* if proxy isn't specified use file registered currently and
	     * reschedule renewal */
	    if (record.end_time < current_time) {
	       char *server;
	       char jobids[FILENAME_MAX];
	       /* remove file with expired proxy and clean the record in db */
	       unlink(cur_proxy);
	       server = strdup(record.myproxy_server);
	       snprintf(jobids, sizeof(jobids), "%s.%u.jobs", basename, record.suffix);
	       unlink(jobids);
	       free_record(ctx, &record);
	       record.suffix = suffix;
	       record.myproxy_server = server;
	       edg_wlpr_Log(ctx, LOG_WARNING, "Removed expired proxy %s", cur_proxy);
	    } else
	       set_renewal_times(ctx, cur_proxy, &record);
	 } else {
	    ret = set_renewal_times(ctx, proxy, &record);
	    (ret == 0) ? rename(proxy, cur_proxy) : unlink(proxy);
	 }
      }
      glite_renewal_core_reset_err(ctx);
      
      ret = encode_record(ctx, &record, &new_line);
      if (ret)
	 goto end;

      dprintf(tmp_fd, "%s\n", new_line);
      free(new_line);
      new_line = NULL;
   }
   free_record(ctx, &record);

   close(tmp_fd);
   fclose(fd);

   rename(tmp_file, datafile);

   return;

end:
   if (fd)
      fclose(fd);
   unlink(tmp_file);
   if (tmp_fd > 0)
      close(tmp_fd);
   free_record(ctx, &record);

   return;
}
Example #6
0
bool bc_set(Bitcask *bc, const char* key, char* value, int vlen, int flag, int version)
{
    if (version < 0 && vlen > 0 || vlen > MAX_RECORD_SIZE)
    {
        fprintf(stderr, "invalid set cmd \n");
        return false;
    }

    bool suc = false;
    pthread_mutex_lock(&bc->write_lock);

    int oldv = 0, ver = version;
    Item *it = ht_get(bc->tree, key);
    if (it != NULL)
    {
        oldv = it->ver;
    }

    if (version == 0 && oldv > 0)  // replace
    {
        ver = oldv + 1;
    }
    else if (version == 0 && oldv <= 0)    // add
    {
        ver = -oldv + 1;
    }
    else if (version < 0 && oldv <= 0)     // delete, not exist
    {
        goto SET_FAIL;
    }
    else if (version == -1)     // delete
    {
        ver = - abs(oldv) - 1;
    }
    else if (abs(version) <= abs(oldv))     // sync
    {
        goto SET_FAIL;
    }
    else     // sync
    {
        ver = version;
    }

    uint16_t hash = gen_hash(value, vlen);
    if (ver < 0) hash = 0;

    if (NULL != it && hash == it->hash)
    {
        DataRecord *r = bc_get(bc, key);
        if (r != NULL && r->flag == flag && vlen  == r->vsz
                && memcmp(value, r->value, vlen) == 0)
        {
            if (version != 0)
            {
                // update version
                if (it->pos & 0xff == bc->curr)
                {
                    ht_add(bc->curr_tree, key, it->pos, it->hash, ver);
                }
                ht_add(bc->tree, key, it->pos, it->hash, ver);
            }
            suc = true;
            free_record(r);
            goto SET_FAIL;
        }
        if (r != NULL) free_record(r);
    }

    int klen = strlen(key);
    DataRecord *r = (DataRecord*)malloc(sizeof(DataRecord) + klen);
    r->ksz = klen;
    memcpy(r->key, key, klen);
    r->vsz = vlen;
    r->value = value;
    r->free_value = false;
    r->flag = flag;
    r->version = ver;
    r->tstamp = time(NULL);

    int rlen;
    char *rbuf = encode_record(r, &rlen);
    if (rbuf == NULL || (rlen & 0xff) != 0)
    {
        fprintf(stderr, "encode_record() failed with %d\n", rlen);
        if (rbuf != NULL) free(rbuf);
        goto SET_FAIL;
    }

    pthread_mutex_lock(&bc->buffer_lock);
    // record maybe larger than buffer
    if (bc->wbuf_curr_pos + rlen > bc->wbuf_size)
    {
        pthread_mutex_unlock(&bc->buffer_lock);
        bc_flush(bc, 0, 0);
        pthread_mutex_lock(&bc->buffer_lock);

        while (rlen > bc->wbuf_size)
        {
            bc->wbuf_size *= 2;
            free(bc->write_buffer);
            bc->write_buffer = (char*)malloc(bc->wbuf_size);
        }
        if (bc->wbuf_start_pos + bc->wbuf_size > MAX_BUCKET_SIZE)
        {
            bc_rotate(bc);
        }
    }
    memcpy(bc->write_buffer + bc->wbuf_curr_pos, rbuf, rlen);
    int pos = (bc->wbuf_start_pos + bc->wbuf_curr_pos) | bc->curr;
    bc->wbuf_curr_pos += rlen;
    pthread_mutex_unlock(&bc->buffer_lock);

    ht_add(bc->curr_tree, key, pos, hash, ver);
    ht_add(bc->tree, key, pos, hash, ver);
    suc = true;
    free(rbuf);
    free_record(r);

SET_FAIL:
    pthread_mutex_unlock(&bc->write_lock);
    if (it != NULL) free(it);
    return suc;
}
Example #7
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeDst(EmbPattern* pattern, const char* fileName)
{
    EmbRect boundingRect;
    FILE* file = 0;
    int xx, yy, dx, dy, flags;
    int i;
    int co = 1, st = 0;
    int ax, ay, mx, my;
    char* pd = 0;
    EmbStitchList* pointer = 0;

    if(!pattern) { embLog_error("format-dst.c writeDst(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-dst.c writeDst(), fileName argument is null\n"); return 0; }

    file = fopen(fileName, "wb");
    if(!file)
    {
        embLog_error("format-dst.c writeDst(), cannot open %s for writing\n", fileName);
        return 0;
    }
    embPattern_correctForMaxStitchLength(pattern, 12.1, 12.1);

    xx = yy = 0;
    co = 1;
    co = embThreadList_count(pattern->threadList);
    st = 0;
    st = embStitchList_count(pattern->stitchList);
    flags = NORMAL;
    boundingRect = embPattern_calcBoundingBox(pattern);
    /* TODO: review the code below
    if (pattern->get_variable("design_name") != NULL)
    {
    char *la = stralloccopy(pattern->get_variable("design_name"));
    if (strlen(la)>16) la[16]='\0';

    fprintf(file,"LA:%-16s\x0d",la);
    free (la);
    }
    else
    {
    */
    fprintf(file, "LA:%-16s\x0d", "Untitled");
    /*} */
    fprintf(file, "ST:%7d\x0d", st);
    fprintf(file, "CO:%3d\x0d", co - 1); /* number of color changes, not number of colors! */
    fprintf(file, "+X:%5d\x0d", (int)(boundingRect.right * 10.0));
    fprintf(file, "-X:%5d\x0d", (int)(fabs(boundingRect.left) * 10.0));
    fprintf(file, "+Y:%5d\x0d", (int)(boundingRect.bottom * 10.0));
    fprintf(file, "-Y:%5d\x0d", (int)(fabs(boundingRect.top) * 10.0));


    ax = ay = mx = my = 0;
    /* TODO: review the code below */
    /*ax=pattern->get_variable_int("ax"); */ /* will return 0 if not defined */
    /*ay=pattern->get_variable_int("ay"); */
    /*mx=pattern->get_variable_int("mx"); */
    /*my=pattern->get_variable_int("my"); */

    /*pd=pattern->get_variable("pd");*/ /* will return null pointer if not defined */
    pd = 0;
    if(pd == 0 || strlen(pd) != 6)
    {
        /* pd is not valid, so fill in a default consisting of "******" */
        pd = "******";
    };
    fprintf(file, "AX:+%5d\x0d", ax);
    fprintf(file, "AY:+%5d\x0d", ay);
    fprintf(file, "MX:+%5d\x0d", mx);
    fprintf(file, "MY:+%5d\x0d", my);
    fprintf(file, "PD:%6s\x0d", pd);
    binaryWriteByte(file, 0x1a); /* 0x1a is the code for end of section. */

    /* pad out header to proper length */
    for(i = 125; i < 512; i++)
    {
        fprintf(file, " ");
    }

    /* write stitches */
    xx = yy = 0;
    pointer = pattern->stitchList;
    while(pointer)
    {
        /* convert from mm to 0.1mm for file format */
        dx = roundDouble(pointer->stitch.xx * 10.0) - xx;
        dy = roundDouble(pointer->stitch.yy * 10.0) - yy;
        xx = roundDouble(pointer->stitch.xx * 10.0);
        yy = roundDouble(pointer->stitch.yy * 10.0);
        flags = pointer->stitch.flags;
        encode_record(file, dx, dy, flags);
        pointer = pointer->next;
    }
    binaryWriteByte(file, 0xA1); /* finish file with a terminator character */
    binaryWriteShort(file, 0);
    fclose(file);
    return 1;
}
Example #8
0
gint wr_subsume_cl(glb* g, gptr cl1, gptr cl2, int uniquestrflag) {
  void* db=g->db;
  int cllen1,cllen2;
  int i2;
  gint meta1,meta2;
  gint lit1,lit2;
  int vc_tmp;
  int mres;
  
#ifdef DEBUG  
  printf("wr_subsume_cl called on %d %d \n",(int)cl1,(int)cl2);
  wr_print_clause(g,cl1);
  wr_print_clause(g,cl2);
#endif

  ++(g->stat_clsubs_attempted);  

  // check fact clause cases first
  if (!wg_rec_is_rule_clause(db,cl1)) {
    if (!wg_rec_is_rule_clause(db,cl2)) {
#ifdef DEBUG  
       printf("both clauses are facts \n");
#endif            
      ++(g->stat_clsubs_unit_attempted);
      if (wr_equal_term(g,encode_record(db,cl1),encode_record(db,cl2),uniquestrflag))
        return 1;
      else
        return 0;
    } else {
      cllen2=wg_count_clause_atoms(db,cl2);
      lit1=encode_record(db,cl1);      
      ++(g->stat_clsubs_unit_attempted);
      for(i2=0;i2<cllen2;i2++) {
        meta2=wg_get_rule_clause_atom_meta(db,cl2,i2);
        lit2=wg_get_rule_clause_atom(db,cl2,i2);
        if (!wg_atom_meta_is_neg(db,meta2) && wr_equal_term(g,lit1,lit2,uniquestrflag)) return 1;        
      }  
      return 0;
    }      
  } 
  cllen1=wg_count_clause_atoms(db,cl1);
  
  if (!wg_rec_is_rule_clause(db,cl2)) {
    // unit rule clause subsuming a unit fact clause case
    ++(g->stat_clsubs_unit_attempted);    
    cllen2=1;
    if (cllen1>1) return 0;
    meta1=wg_get_rule_clause_atom_meta(db,cl1,0);
    if (wg_atom_meta_is_neg(d,meta1)) return 0;
    lit1=wg_get_rule_clause_atom(db,cl1,0);    
    lit2=rpto(g,cl2);
    vc_tmp=2;
    mres=wr_match_term(g,lit1,lit2,uniquestrflag);
    if (vc_tmp!=*((g->varstack)+1)) wr_clear_varstack(g,g->varstack);
    return mres;      
  } else {
    // cl2 is a rule clause
    cllen2=wg_count_clause_atoms(db,cl2); 
  }    
  // now both clauses are rule clauses   

#ifdef DEBUG  
  printf("cllen1 %d cllen2 %d\n",cllen1,cllen2);
#endif   
  // check unit rule clause case
  if (cllen1==1) { 
#ifdef DEBUG  
    printf("unit clause subsumption case \n");
#endif        
    ++(g->stat_clsubs_unit_attempted);    
    ++(g->stat_clsubs_unit_attempted);
    meta1=wg_get_rule_clause_atom_meta(db,cl1,0);
    lit1=wg_get_rule_clause_atom(db,cl1,0);
    vc_tmp=2;
    *((g->varstack)+1)=vc_tmp; // zero varstack pointer 
    for(i2=0;i2<cllen2;i2++) {
        meta2=wg_get_rule_clause_atom_meta(db,cl2,i2);
        lit2=wg_get_rule_clause_atom(db,cl2,i2);
        if (!litmeta_negpolarities(meta1,meta2)) {  
          mres=wr_match_term(g,lit1,lit2,uniquestrflag);
          if (vc_tmp!=*((g->varstack)+1)) wr_clear_varstack(g,g->varstack);
          if (mres) return 1;
        }                    
    }  
    return 0;
  }    
  if (cllen1>cllen2) return 0;
  
  // now both clauses are nonunit rule clauses and we do full subsumption
  // prepare for subsumption: set globals etc 
#ifdef DEBUG  
  printf("general subsumption case \n");
#endif  
  g->tmp_unify_vc=(g->varstack)+1;  // var counter for varstack   
  // clear lit information vector (0 pos holds vec len)
  for(i2=1;i2<=cllen2;i2++) (g->tmp_litinf_vec)=wr_vec_store(g,g->tmp_litinf_vec,i2,0);
  ++(g->stat_clsubs_full_attempted);
  mres=wr_subsume_cl_aux(g,cl1,cl2,
                   cl1+RECORD_HEADER_GINTS+CLAUSE_EXTRAHEADERLEN,
                   cl2+RECORD_HEADER_GINTS+CLAUSE_EXTRAHEADERLEN,
                   0,0,
                   cllen1,cllen2,
                   uniquestrflag);
  wr_clear_varstack(g,g->varstack);
  return mres;  
}