Exemplo n.º 1
0
// place ->fd at offset off
int kftp_connect_file(knetFile *fp)
{
	int ret;
	long long file_size;
	if (fp->fd != -1) {
		netclose(fp->fd);
		if (fp->no_reconnect) kftp_get_response(fp);
	}
	kftp_pasv_prep(fp);
    kftp_send_cmd(fp, fp->size_cmd, 1);
#ifndef _WIN32
    if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 )
    {
        if(!knetsilent)
        {
            fprintf(stderr,"[kftp_connect_file] %s\n", fp->response);
        }
        return -1;
    }
#else
	const char *p = fp->response;
	while (*p != ' ') ++p;
	while (*p < '0' || *p > '9') ++p;
	file_size = strtoint64(p);
#endif
	fp->file_size = file_size;
	if (fp->offset>=0) {
		char tmp[32];
#ifndef _WIN32
		sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
#else
		strcpy(tmp, "REST ");
		int64tostr(tmp + 5, fp->offset);
		strcat(tmp, "\r\n");
#endif
		kftp_send_cmd(fp, tmp, 1);
	}
	kftp_send_cmd(fp, fp->retr, 0);
	kftp_pasv_connect(fp);
	ret = kftp_get_response(fp);
	if (ret != 150) {
            if(!knetsilent)
            {
		fprintf(stderr, "[kftp_connect_file] %s\n", fp->response);
            }
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	fp->is_ready = 1;
	return 0;
}
Exemplo n.º 2
0
static inline int strtowhatever(char *cur_pos, char **end_pos, void *data, enum parsetype type) {
  float valf;
  double vald;
  int32_t vali32;
  int64_t vali64;
  char *new_pos = cur_pos;
  char *data_pos = (char *)data;
  switch (type) {
  case PARSE_FLOAT32:
    valf = strtofloat(cur_pos, end_pos);
    if (cur_pos == *end_pos) return 0;
    *((float *)data) = valf;
    return 1;

  case PARSE_INT32:
    vali32 = strtoint32(cur_pos, end_pos);
    if (cur_pos == *end_pos) return 0;
    *((int32_t *)data) = vali32;
    return 1;

  case PARSE_FLOAT64:
    vald = strtodouble(cur_pos, end_pos);
    if (cur_pos == *end_pos) return 0;
    *((double *)data) = vald;
    return 1;

  case PARSE_INT64:
    vali64 = strtoint64(cur_pos, end_pos);
    if (cur_pos == *end_pos) return 0;
    *((int64_t *)data) = vali64;
    return 1;
   
  case PARSE_STRING:
    while (*new_pos && !(*new_pos==' ' || *new_pos=='\t' || *new_pos=='\n')) {
      *data_pos = *new_pos;
      data_pos++;
      new_pos++;
    }
    if (new_pos==cur_pos) return 0;
    *data_pos = 0;
    *end_pos = new_pos;
    return 1;

  case PARSE_SKIP:
    while (*new_pos && !(*new_pos==' ' || *new_pos=='\t' || *new_pos=='\n'))
      new_pos++;
    if (new_pos==cur_pos) return 0;
    *end_pos = new_pos;
    return 1;
  }
  return 0;
}
Exemplo n.º 3
0
// place ->fd at offset off
int kftp_connect_file(knetFile *fp)
{
	int ret;
	long long file_size;
	if (fp->fd != -1) {
		netclose(fp->fd);
		if (fp->no_reconnect) kftp_get_response(fp);
	}
	kftp_pasv_prep(fp);
    kftp_send_cmd(fp, fp->size_cmd, 1);
#ifndef _WIN32
    // If the file does not exist, the response will be "550 Could not get file
    // size". Be silent on failure, hts_idx_load can be trying the existence of .csi or .tbi.
    if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 ) return -1;
#else
	const char *p = fp->response;
	while (*p != ' ') ++p;
	while (*p < '0' || *p > '9') ++p;
	file_size = strtoint64(p);
#endif
	fp->file_size = file_size;
	if (fp->offset>=0) {
		char tmp[32];
#ifndef _WIN32
		sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
#else
		strcpy(tmp, "REST ");
		int64tostr(tmp + 5, fp->offset);
		strcat(tmp, "\r\n");
#endif
		kftp_send_cmd(fp, tmp, 1);
	}
	kftp_send_cmd(fp, fp->retr, 0);
	kftp_pasv_connect(fp);
	ret = kftp_get_response(fp);
	if (ret != 150) {
		fprintf(stderr, "[kftp_connect_file] %s\n", fp->response);
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	fp->is_ready = 1;
	return 0;
}
Exemplo n.º 4
0
// contract: out is already zeroed and contains only name
void get_package_contents(pkgconfig* cfg, stringptr* packagename, pkgdata* out) {
	ini_section sec;
	char buf[256];
	ulz_snprintf(buf, sizeof(buf), "%s/pkg/%s", cfg->pkgroot.ptr, packagename->ptr);
	
	stringptr* fc = stringptr_fromfile(buf);
	stringptr val;
	
	if(!fc) goto err;
	stringptrlist* ini = stringptr_linesplit(fc);
	size_t start = 0;
	
	stringptr* tmp;
	
	sec = iniparser_get_section(ini, SPL("mirrors"));
	out->mirrors = stringptrlist_new(sec.linecount);
	
	for(start = sec.startline; start < sec.startline + sec.linecount; start++) {
		tmp = stringptrlist_get(ini, start);
		if(tmp->size) stringptrlist_add(out->mirrors, stringptr_strdup(tmp), tmp->size);
	}
	
	
	sec = iniparser_get_section(ini, SPL("main"));
	
	iniparser_getvalue(ini, &sec, SPL("tardir"), &val);
	if(val.size)
		out->tardir = stringptr_copy(&val);
	else {
		// must run after mirrors!
		stringptr fe;
		if(get_tarball_filename(out, buf, sizeof(buf))) {
			stringptr_fromchar(buf, &fe);
			strip_fileext(&fe);
		} else {
			fe.size = 0;
			fe.ptr = NULL;
		}
		out->tardir = stringptr_copy(&fe);
	}
	
	iniparser_getvalue(ini, &sec, SPL("sha512"), &val);
	if(val.size)
		out->sha512 = stringptr_copy(&val);
	else
		out->sha512 = NULL;
	
	iniparser_getvalue(ini, &sec, SPL("filesize"), &val);
	if(val.size)
		out->filesize = strtoint64(val.ptr, val.size);
	else
		out->filesize = 0;
	
	sec = iniparser_get_section(ini, SPL("deps"));
	out->deps = stringptrlist_new(sec.linecount);
	
	for(start = sec.startline; start < sec.startline + sec.linecount; start++) {
		tmp = stringptrlist_get(ini, start);
		if(tmp->size) stringptrlist_add(out->deps, stringptr_strdup(tmp), tmp->size);
	}
	
	sec = iniparser_get_section(ini, SPL("build")); // the build section has always to come last
	if(sec.startline || sec.linecount) {
		start = sec.startline;
		sec = iniparser_file_as_section(ini); // iniparser may disinterpret lines starting with [
		// so be sure to use the entire rest of the file
		sec.startline = start;
		sec.linecount -= start;
		
		out->buildscript = stringptrlist_new(sec.linecount);
		
		for(start = sec.startline; start < sec.startline + sec.linecount; start++) {
			tmp = stringptrlist_get(ini, start);
			stringptrlist_add(out->buildscript, stringptr_strdup(tmp), tmp->size);
		}
	} else 
		out->buildscript = stringptrlist_new(1);
	
	stringptrlist_free(ini);
	stringptr_free(fc);
	return;
	err:
	log_perror(packagename->ptr);
	die(SPL("package not existing"));
}
Exemplo n.º 5
0
int
_pmi_stuff_value(pmi_context *current, pmi_handle *hp, const char *value)
{
    pmResult	*rp;
    int		i;
    pmID	pmid;
    pmValueSet	*vsp;
    pmValue	*vp;
    pmi_metric	*mp;
    char	*end;
    int		dsize;
    void	*data;
    __int64_t	ll;
    __uint64_t	ull;
    float	f;
    double	d;

    mp = &current->metric[hp->midx];

    if (current->result == NULL) {
	/* first time */
	current->result = (pmResult *)malloc(sizeof(pmResult));
	if (current->result == NULL) {
	    __pmNoMem("_pmi_stuff_value: result malloc:", sizeof(pmResult), PM_FATAL_ERR);
	}
	current->result->numpmid = 0;
	current->result->timestamp.tv_sec = 0;
	current->result->timestamp.tv_usec = 0;
    }
    rp = current->result;

    pmid = current->metric[hp->midx].pmid;
    for (i = 0; i < rp->numpmid; i++) {
	if (pmid == rp->vset[i]->pmid) {
	    if (mp->desc.indom == PM_INDOM_NULL)
		/* singular metric, cannot have more than one value */
		return PMI_ERR_DUPVALUE;
	    break;
	}
    }
    if (i == rp->numpmid) {
	rp->numpmid++;
	rp = current->result = (pmResult *)realloc(current->result, sizeof(pmResult) + (rp->numpmid - 1)*sizeof(pmValueSet *));
	if (current->result == NULL) {
	    __pmNoMem("_pmi_stuff_value: result realloc:", sizeof(pmResult) + (rp->numpmid - 1)*sizeof(pmValueSet *), PM_FATAL_ERR);
	}
	rp->vset[rp->numpmid-1] = (pmValueSet *)malloc(sizeof(pmValueSet));
	if (rp->vset[rp->numpmid-1] == NULL) {
	    __pmNoMem("_pmi_stuff_value: vset alloc:", sizeof(pmValueSet), PM_FATAL_ERR);
	}
	vsp = rp->vset[rp->numpmid-1];
	vsp->pmid = pmid;
	vsp->numval = 1;
    }
    else {
	int		j;
	for (j = 0; j < rp->vset[i]->numval; j++) {
	    if (rp->vset[i]->vlist[j].inst == hp->inst)
		/* each metric-instance can appear at most once per pmResult */
		return PMI_ERR_DUPVALUE;
	}
	rp->vset[i]->numval++;
	vsp = rp->vset[i] = (pmValueSet *)realloc(rp->vset[i], sizeof(pmValueSet) + (rp->vset[i]->numval-1)*sizeof(pmValue));
	if (rp->vset[i] == NULL) {
	    __pmNoMem("_pmi_stuff_value: vset realloc:", sizeof(pmValueSet) + (rp->vset[i]->numval-1)*sizeof(pmValue), PM_FATAL_ERR);
	}
    }
    vp = &vsp->vlist[vsp->numval-1];
    vp->inst = hp->inst;
    dsize = -1;
    switch (mp->desc.type) {
	case PM_TYPE_32:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_INSITU;
	    vp->value.lval = (__int32_t)strtol(value, &end, 10);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    break;

	case PM_TYPE_U32:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_INSITU;
	    vp->value.lval = (__uint32_t)strtoul(value, &end, 10);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    break;

	case PM_TYPE_64:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_DPTR;
	    ll = strtoint64(value, &end, 10);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    dsize = sizeof(ll);
	    data = (void *)&ll;
	    break;

	case PM_TYPE_U64:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_DPTR;
	    ull = strtouint64(value, &end, 10);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    dsize = sizeof(ull);
	    data = (void *)&ull;
	    break;

	case PM_TYPE_FLOAT:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_DPTR;
	    f = strtof(value, &end);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    dsize = sizeof(f);
	    data = (void *)&f;
	    break;

	case PM_TYPE_DOUBLE:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_DPTR;
	    d = strtod(value, &end);
	    if (*end != '\0') {
		vsp->numval = PM_ERR_CONV;
		return PM_ERR_CONV;
	    }
	    dsize = sizeof(d);
	    data = (void *)&d;
	    break;

	case PM_TYPE_STRING:
	    if (vsp->numval == 1) vsp->valfmt = PM_VAL_DPTR;
	    dsize = strlen(value)+1;
	    data = (void *)value;
	    break;

	default:
	    vsp->numval = PM_ERR_TYPE;
	    return PM_ERR_TYPE;
    }

    if (dsize != -1) {
	/* logic copied from stuffvalue.c in libpcp */
	int	need = dsize + PM_VAL_HDR_SIZE;

	vp->value.pval = (pmValueBlock *)malloc(need < sizeof(pmValueBlock) ? sizeof(pmValueBlock) : need);
	if (vp->value.pval == NULL) {
	    __pmNoMem("_pmi_stuff_value: pmValueBlock:", need < sizeof(pmValueBlock) ? sizeof(pmValueBlock) : need, PM_FATAL_ERR);
	}
	vp->value.pval->vlen = (int)need;
	vp->value.pval->vtype = mp->desc.type;
	memcpy((void *)vp->value.pval->vbuf, data, dsize);
    }

    return 0;
}