Ejemplo n.º 1
0
static struct status *
status_fromrd(char *path, struct stream *file)
{
	struct status *st;
	char *id, *line;
	time_t scantime;
	int error, ver;

	/* Get the first line of the file and validate it. */
	line = stream_getln(file, NULL);
	if (line == NULL) {
		stream_close(file);
		return (NULL);
	}
	id = proto_get_ascii(&line);
	error = proto_get_int(&line, &ver, 10);
	if (error) {
		stream_close(file);
		return (NULL);
	}
	error = proto_get_time(&line, &scantime);
	if (error || line != NULL) {
		stream_close(file);
		return (NULL);
	}

	if (strcmp(id, "F") != 0 || ver != STATUS_VERSION) {
		stream_close(file);
		return (NULL);
	}

	st = status_new(path, scantime, file);
	st->linenum = 1;
	return (st);
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]){
	char*gcf_name;
	archive_t gcf=NULL;
	RTinitPopt(&argc,&argv,options,1,-1,&gcf_name,NULL,"([-c] <gcf> (<dir>|<file>)*) | (-x <gcf> [<dir>|<pattern>])",
		"Tool for creating and extracting GCF archives\n\nOptions");
	compression_policy_compile(policy);
	if (operation==GCF_EXTRACT) {
		char *out_name=RTinitNextArg();
		archive_t dir;
		if(out_name){
			if(RTinitNextArg()){
				Fatal(1,error,"extraction uses gcf -x <gcf> [<dir>|<pattern>]");
			}
			if (strstr(out_name,"%s")) {
				dir=arch_fmt(out_name,file_input,file_output,blocksize);
			} else {
				dir=arch_dir_create(out_name,blocksize,force?DELETE_ALL:DELETE_NONE);
			}
		} else {
			dir=arch_dir_open(".",blocksize);
		}
		if (is_a_dir(gcf_name)){
			gcf=arch_dir_open(gcf_name,blocksize);
		} else {
			gcf=arch_gcf_read(raf_unistd(gcf_name));
		}
		archive_copy(gcf,"auto",dir,NULL,blocksize);
		arch_close(&dir);
		arch_close(&gcf);
	} else {
		if (operation==GCF_FILE){
			gcf=arch_gcf_create(raf_unistd(gcf_name),blocksize,blocksize*blockcount,0,1);
		} else {
			gcf=arch_dir_create(gcf_name,blocksize,force?DELETE_ALL:DELETE_NONE);
		}
		for(;;){
			char *input_name=RTinitNextArg();
			if (!input_name) break;
			if (is_a_dir(input_name)){
				Warning(info,"copying contents of %s",input_name);
				archive_t dir=arch_dir_open(input_name,blocksize);
				archive_copy(dir,NULL,gcf,get_compression,blocksize);
				arch_close(&dir);
			} else {
				Warning(info,"copying %s",input_name);
				stream_t is=file_input(input_name);
				stream_t os=arch_write(gcf,input_name,get_compression(input_name),1);
				char buf[blocksize];
				for(;;){
					int len=stream_read_max(is,buf,blocksize);
					if (len) stream_write(os,buf,len);
					if(len<blocksize) break;
				}
				stream_close(&is);
				stream_close(&os);	
			}
		}
		arch_close(&gcf);
	}
}
Ejemplo n.º 3
0
static void gcf_compress(){
    char*source;
    char target[LTSMIN_PATHNAME_MAX];
    while((source=HREnextArg())){
        if (is_a_file(source)){
            sprintf(target,"%s.gzf",source);
            stream_t is=file_input(source);
            stream_t os=file_output(target);
            char *code=SSMcall(compression_policy,source);
            DSwriteS(os,code);
            os=stream_add_code(os,code);
            char buf[blocksize];
            for(;;){
                int len=stream_read_max(is,buf,blocksize);
                if (len) stream_write(os,buf,len);
                if(len<blocksize) break;
            }
            stream_close(&is);
            stream_close(&os);
            if (!keep) recursive_erase(source);
        } else if (is_a_dir(source)){
            sprintf(target,"%s.gcf",source);
            archive_t arch_in=arch_dir_open(source,blocksize);
            archive_t arch_out=arch_gcf_create(raf_unistd(target),blocksize,blocksize*blockcount,0,1);
            archive_copy(arch_in,arch_out,compression_policy,blocksize,NULL);
            arch_close(&arch_in);
            arch_close(&arch_out);
            if (!keep) recursive_erase(source);
        } else {
            Abort("source %s is neither a file nor a directory",source);
        }
    }
}
/* Model terminate function */
void omni_interface_terminate(void)
{
  /* S-Function Block: omni_interface/HIL Initialize (hil_initialize_block) */
  {
    t_boolean is_switching;
    t_int result;
    hil_task_stop_all(omni_interface_DWork.HILInitialize_Card);
    hil_task_delete_all(omni_interface_DWork.HILInitialize_Card);
    is_switching = false;
    if ((omni_interface_P.HILInitialize_POTerminate && !is_switching) ||
        (omni_interface_P.HILInitialize_POExit && is_switching)) {
      omni_interface_DWork.HILInitialize_POValues[0] =
        omni_interface_P.HILInitialize_POFinal;
      omni_interface_DWork.HILInitialize_POValues[1] =
        omni_interface_P.HILInitialize_POFinal;
      omni_interface_DWork.HILInitialize_POValues[2] =
        omni_interface_P.HILInitialize_POFinal;
      result = hil_write_pwm(omni_interface_DWork.HILInitialize_Card,
        &omni_interface_P.HILInitialize_POChannels[0], 3U,
        &omni_interface_DWork.HILInitialize_POValues[0]);
      if (result < 0) {
        msg_get_error_messageA(NULL, result, _rt_error_message, sizeof
          (_rt_error_message));
        rtmSetErrorStatus(omni_interface_M, _rt_error_message);
      }
    }

    hil_close(omni_interface_DWork.HILInitialize_Card);
    omni_interface_DWork.HILInitialize_Card = NULL;
  }

  /* S-Function Block: omni_interface/Stream Answer (stream_answer_block) */
  {
    if (omni_interface_DWork.StreamAnswer_Client != NULL) {
      stream_close(omni_interface_DWork.StreamAnswer_Client);
      omni_interface_DWork.StreamAnswer_Client = NULL;
    }

    if (omni_interface_DWork.StreamAnswer_Listener != NULL) {
      stream_close(omni_interface_DWork.StreamAnswer_Listener);
      omni_interface_DWork.StreamAnswer_Listener = NULL;
    }
  }

  /* S-Function Block: omni_interface/Stream Answer1 (stream_answer_block) */
  {
    if (omni_interface_DWork.StreamAnswer1_Client != NULL) {
      stream_close(omni_interface_DWork.StreamAnswer1_Client);
      omni_interface_DWork.StreamAnswer1_Client = NULL;
    }

    if (omni_interface_DWork.StreamAnswer1_Listener != NULL) {
      stream_close(omni_interface_DWork.StreamAnswer1_Listener);
      omni_interface_DWork.StreamAnswer1_Listener = NULL;
    }
  }

  /* External mode */
  rtExtModeShutdown(1);
}
Ejemplo n.º 5
0
static void gcf_create(){
    char *gcf_name=HREnextArg();
    if (gcf_name==NULL) {
        Abort("missing <gcf archive> argument");
    }
    archive_t arch=arch_gcf_create(raf_unistd(gcf_name),blocksize,blocksize*blockcount,0,1);
    char*file;
    while((file=HREnextArg())){
        if (is_a_file(file)){
            stream_t is=file_input(file);
            stream_t os=arch_write_apply(arch,file,SSMcall(compression_policy,file));
            char buf[blocksize];
            for(;;){
                int len=stream_read_max(is,buf,blocksize);
                if (len) stream_write(os,buf,len);
                if(len<blocksize) break;
            }
            stream_close(&is);
            stream_close(&os);
        } else {
            Abort("cannot add %s because it is not a file",file);
        }
    }
    arch_close(&arch);
}
Ejemplo n.º 6
0
static int
auth_lookuprecord(char *server, struct srvrecord *auth)
{
	char *home, *line, authfile[FILENAME_MAX];
	struct stream *s;
	int linenum = 0, error;

	home = getenv("HOME");
	if (home == NULL) {
		lprintf(-1, "Environment variable \"HOME\" is not set\n");
		return (STATUS_FAILURE);
	}
	snprintf(authfile, sizeof(authfile), "%s/%s", home, AUTHFILE);
	s = stream_open_file(authfile, O_RDONLY);
	if (s == NULL) {
		lprintf(-1, "Could not open file %s\n", authfile);
		return (STATUS_FAILURE);
	}

	while ((line = stream_getln(s, NULL)) != NULL) {
		linenum++;
		if (line[0] == '#' || line[0] == '\0')
			continue;
		error = auth_parsetoken(&line, auth->server,
		    sizeof(auth->server));
		if (error != STATUS_SUCCESS) {
			lprintf(-1, "%s:%d Missing client name\n", authfile, linenum);
			goto close;
		}
		/* Skip the rest of this line, it isn't what we are looking for. */
		if (strcasecmp(auth->server, server) != 0)
			continue;
		error = auth_parsetoken(&line, auth->client,
		    sizeof(auth->client));
		if (error != STATUS_SUCCESS) {
			lprintf(-1, "%s:%d Missing password\n", authfile, linenum);
			goto close;
		}
		error = auth_parsetoken(&line, auth->password,
		    sizeof(auth->password));
		if (error != STATUS_SUCCESS) {
			lprintf(-1, "%s:%d Missing comment\n", authfile, linenum);
			goto close;
		}
		stream_close(s);
		lprintf(2, "Found authentication record for server \"%s\"\n",
		    server);
		return (STATUS_SUCCESS);
	}
	lprintf(-1, "Unknown server \"%s\". Fix your %s\n", server , authfile);
	memset(auth->password, 0, sizeof(auth->password));
close:
	stream_close(s);
	return (STATUS_FAILURE);
}
Ejemplo n.º 7
0
static void vrt_rdev_close_superblock_streams(vrt_realdev_t *rdev)
{
    int i;

    for (i = 0; i < 2; i++)
    {
        stream_close(rdev->checksum_sb_streams[i]);
        stream_close(rdev->sb_data_streams[i]);
    }

    stream_close(rdev->raw_sb_stream);
}
Ejemplo n.º 8
0
alsa_midi_t* alsa_rawmidi_new(jack_client_t *jack)
{
    alsa_rawmidi_t *midi = calloc(1, sizeof(alsa_rawmidi_t));
    if (!midi)
        goto fail_0;
    midi->client = jack;
    if (pipe(midi->scan.wake_pipe)==-1) {
        error_log("pipe() in alsa_midi_new failed: %s", strerror(errno));
        goto fail_1;
    }

    if (stream_init(&midi->in, midi, "in"))
        goto fail_2;
    midi->in.mode = POLLIN;
    midi->in.port_size = sizeof(input_port_t);
    midi->in.port_init = input_port_init;
    midi->in.port_close = input_port_close;
    midi->in.process_jack = do_jack_input;
    midi->in.process_midi = do_midi_input;

    if (stream_init(&midi->out, midi, "out"))
        goto fail_3;
    midi->out.mode = POLLOUT;
    midi->out.port_size = sizeof(output_port_t);
    midi->out.port_init = output_port_init;
    midi->out.port_close = output_port_close;
    midi->out.process_jack = do_jack_output;
    midi->out.process_midi = do_midi_output;

    midi->ops.destroy = alsa_rawmidi_delete;
    midi->ops.attach = alsa_rawmidi_attach;
    midi->ops.detach = alsa_rawmidi_detach;
    midi->ops.start = alsa_rawmidi_start;
    midi->ops.stop = alsa_rawmidi_stop;
    midi->ops.read = alsa_rawmidi_read;
    midi->ops.write = alsa_rawmidi_write;
    midi->midi_in_cnt = 0;
    midi->midi_out_cnt = 0;

    return &midi->ops;
fail_3:
    stream_close(&midi->out);
fail_2:
    stream_close(&midi->in);
    close(midi->scan.wake_pipe[1]);
    close(midi->scan.wake_pipe[0]);
fail_1:
    free(midi);
fail_0:
    return NULL;
}
Ejemplo n.º 9
0
static
void alsa_rawmidi_delete(alsa_midi_t *m)
{
    alsa_rawmidi_t *midi = (alsa_rawmidi_t*)m;

    alsa_rawmidi_detach(m);

    stream_close(&midi->out);
    stream_close(&midi->in);
    close(midi->scan.wake_pipe[0]);
    close(midi->scan.wake_pipe[1]);

    free(midi);
}
Ejemplo n.º 10
0
void cdc_acmd_destroy(CDC_ACMD* cdc_acmd)
{
    io_destroy(cdc_acmd->notify);

    io_destroy(cdc_acmd->rx);
    stream_close(cdc_acmd->rx_stream_handle);
    stream_destroy(cdc_acmd->rx_stream);

    io_destroy(cdc_acmd->tx);
    stream_close(cdc_acmd->tx_stream_handle);
    stream_destroy(cdc_acmd->tx_stream);

    free(cdc_acmd);
}
Ejemplo n.º 11
0
static void read_test(void *v_t)
{
	struct read_test *t = v_t;
	enum stream_result sres;
	ssize_t res;

	switch (t->step) {
	case 0:
		t->stream = delim_read_next(t->dr);
		t->pos = 0;

	STEP:
		res = read_some(t, 8);
		if (res == STREAM_WAITING)
			return;

		assert(res == 7);
		assert(!memcmp(t->buf, "hello, ", 7));

	STEP:
		sres = stream_close(t->stream, &t->tasklet, &t->err);
		if (sres == STREAM_WAITING)
			return;

		assert(sres == STREAM_OK);
		stream_destroy(t->stream);
		t->stream = delim_read_next(t->dr);
		t->pos = 0;

	STEP:
		res = read_some(t, 7);
		if (res == STREAM_WAITING)
			return;

		assert(res == 6);
		assert(!memcmp(t->buf, "world!", 6));

	STEP:
		sres = stream_close(t->stream, &t->tasklet, &t->err);
		if (sres == STREAM_WAITING)
			return;

		assert(sres == STREAM_OK);
		stream_destroy(t->stream);
	}

	tasklet_stop(&t->tasklet);
	application_stop();
}
Ejemplo n.º 12
0
ut_cleanup()
{
    int i;

    stream_close(stream);
    stream_close(memory_stream);

    rain1_group_free(rxg, sto);

    storage_free(sto);
    for (i = 0; i < NUM_SPOF_GROUPS; i++)
        os_free(rdevs[i]);

    os_random_cleanup();
}
Ejemplo n.º 13
0
int config_parse_file(server *srv, config_t *context, const char *fn) {
    tokenizer_t t;
    stream s;
    int ret;
    buffer *filename;

    if (buffer_is_empty(context->basedir) ||
            (fn[0] == '/' || fn[0] == '\\') ||
            (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
        filename = buffer_init_string(fn);
    } else {
        filename = buffer_init_buffer(context->basedir);
        buffer_append_string(filename, fn);
    }

    if (0 != stream_open(&s, filename)) {
        if (s.size == 0) {
            /* the file was empty, nothing to parse */
            ret = 0;
        } else {
            log_error_write(srv, __FILE__, __LINE__, "sbss",
                            "opening configfile ", filename, "failed:", strerror(errno));
            ret = -1;
        }
    } else {
        tokenizer_init(&t, filename, s.start, s.size);
        ret = config_parse(srv, context, &t);
    }

    stream_close(&s);
    buffer_free(filename);
    return ret;
}
Ejemplo n.º 14
0
int downloadData()
{
  char device[255];
  sprintf(device, "/dev/tcp/%s/80", "netdata.be");
  char headers[255];
  sprintf(headers, "GET /loxone/test2.php HTTP/1.1\r\nHost: %s\r\nUser-Agent: LoxLIVE [en]\r\nContent-Type: text/html; charset=utf-8\r\n\r\n",  "netdata.be");
  STREAM* tcpStream = stream_create(device, 0, 0);
  stream_write(tcpStream, headers, strlen(headers));
  stream_flush(tcpStream);

  char block[RD_BLOCK_SIZE];
  int count;
  int i = 0;
  // read stream
  do
  {
    count = stream_read(tcpStream, block, RD_BLOCK_SIZE, 4000);
    if (count > 0) strncpy((char*)buffer + i * RD_BLOCK_SIZE, block, count);
    i++;
    if (i >= ( ( MAX_BUFF - 1 ) / RD_BLOCK_SIZE )) count=0; // avoid buffer overflows

  }
  while (count > 0);
  stream_close(tcpStream);
  buffer[MAX_BUFF] = 0; //put null character or end of string at the end.
  return 0;
}
Ejemplo n.º 15
0
static void ti85b_file_exit(imgtool_image * img)
{
	ti85b_file *file=(ti85b_file*)img;
	stream_close(file->file_handle);
	free(file->data);
	free(file);
}
Ejemplo n.º 16
0
static imgtoolerr_t ascii_writefile(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *sourcef, option_resolution *opts)
{
	imgtoolerr_t err;
	imgtool_stream *mem_stream = NULL;
	const char *eoln;

	/* create a stream */
	mem_stream = stream_open_mem(NULL, 0);
	if (!mem_stream)
	{
		err = IMGTOOLERR_OUTOFMEMORY;
		goto done;
	}

	eoln = imgtool_partition_get_info_string(partition, IMGTOOLINFO_STR_EOLN);

	err = convert_stream_eolns(sourcef, mem_stream, eoln);
	if (err)
		goto done;
	stream_seek(mem_stream, SEEK_SET, 0);

	err = imgtool_partition_write_file(partition, filename, fork, mem_stream, opts, NULL);
	if (err)
		goto done;

done:
	if (mem_stream)
		stream_close(mem_stream);
	return err;
}
Ejemplo n.º 17
0
static imgtoolerr_t ascii_readfile(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *destf)
{
	imgtoolerr_t err;
	imgtool_stream *mem_stream;

	mem_stream = stream_open_mem(NULL, 0);
	if (!mem_stream)
	{
		err = IMGTOOLERR_OUTOFMEMORY;
		goto done;
	}

	err = imgtool_partition_read_file(partition, filename, fork, mem_stream, NULL);
	if (err)
		goto done;

	stream_seek(mem_stream, SEEK_SET, 0);
	err = convert_stream_eolns(mem_stream, destf, EOLN);
	if (err)
		goto done;

done:
	if (mem_stream)
		stream_close(mem_stream);
	return err;
}
Ejemplo n.º 18
0
/* -1: connection got closed, 0: nothing to read, n: read n bytes */
gssize stream_read(server *srv, stream *s, char *buf, gssize bufsize) {
	gssize len;
	while (-1 == (len = read(s->fd, buf, bufsize))) {
		switch (errno) {
		case EAGAIN:
#if EWOULDBLOCK != EAGAIN
		case EWOULDBLOCK:
#endif
			/* nothing to read */
			return 0;
		case ECONNRESET:
			return stream_closed(srv, s);
		case EINTR: /* try again */
			break;
		default:
			g_message("read error: %s", g_strerror(errno));
			stream_close(srv, s, s->other);
			return -1;
		}
	}
	if (0 == len) { /* connection closed */
		return stream_closed(srv, s);
	}
	return len;
}
Ejemplo n.º 19
0
static void vmsx_gm2_image_exit(imgtool_image *img)
{
    GM2_IMAGE *image=(GM2_IMAGE*)img;
    stream_close(image->file_handle);
    free(image->data);
    free(image);
}
Ejemplo n.º 20
0
static void
status_free(struct status *st)
{

	if (st->previous != NULL)
		statusrec_fini(st->previous);
	if (st->rd != NULL)
		stream_close(st->rd);
	if (st->wr != NULL)
		stream_close(st->wr);
	if (st->tempfile != NULL)
		free(st->tempfile);
	free(st->path);
	pathcomp_free(st->pc);
	free(st);
}
Ejemplo n.º 21
0
static imgtoolerr_t write_fork(imgtool_partition *partition, const char *filename, const char *fork,
	imgtool_stream *sourcef, UINT64 pos, UINT64 fork_len, option_resolution *opts)
{
	imgtoolerr_t err = IMGTOOLERR_SUCCESS;
	imgtool_stream *mem_stream = NULL;
	size_t len;

	if (fork_len > 0)
	{
		mem_stream = stream_open_mem(NULL, 0);
		if (!mem_stream)
		{
			err = IMGTOOLERR_OUTOFMEMORY;
			goto done;
		}

		stream_seek(sourcef, pos, SEEK_SET);
		len = stream_transfer(mem_stream, sourcef, fork_len);
		if (len < fork_len)
			stream_fill(mem_stream, 0, fork_len);

		stream_seek(mem_stream, 0, SEEK_SET);
		err = imgtool_partition_write_file(partition, filename, fork, mem_stream, opts, NULL);
		if (err)
			goto done;
	}

done:
	if (mem_stream)
		stream_close(mem_stream);
	return err;
}
Ejemplo n.º 22
0
int main(int argc, char* argv[])
{
  size_t n = 0x200000;
  double rate = 1;
  zfp_field* field;
  uint insize;
  zfp_stream* zfp;
  bitstream* stream;
  void* buffer;
  size_t bytes;
  clock_t c;
  double time;
  uint i;

  switch (argc) {
    case 3:
      sscanf(argv[2], "%zu", &n);
      /* FALLTHROUGH */
    case 2:
      sscanf(argv[1], "%lf", &rate);
      break;
  }

  /* declare array to compress */
  field = zfp_field_3d(NULL, zfp_type_double, 4, 4, 4 * n);
  insize = n * sizeof(block);

  /* allocate storage for compressed bit stream */
  zfp = zfp_stream_open(NULL);
  zfp_stream_set_rate(zfp, rate, zfp_field_type(field), zfp_field_dimensionality(field), 0);
  bytes = zfp_stream_maximum_size(zfp, field);
  buffer = malloc(bytes);
  stream = stream_open(buffer, bytes);
  zfp_stream_set_bit_stream(zfp, stream);
  zfp_field_free(field);

  /* compress */
  c = clock();
  for (i = 0; i < n; i++)
    zfp_encode_block_double_3(zfp, (const double*)block);
  zfp_stream_flush(zfp);
  time = (double)(clock() - c) / CLOCKS_PER_SEC;
  printf("encode in=%u out=%u %.0f MB/s\n", insize, (uint)stream_size(stream), insize / (1024 * 1024 * time));

  /* decompress */
  zfp_stream_rewind(zfp);
  c = clock();
  for (i = 0; i < n; i++) {
    double a[64];
    zfp_decode_block_double_3(zfp, a);
  }
  time = (double)(clock() - c) / CLOCKS_PER_SEC;
  printf("decode in=%u out=%u %.0f MB/s\n", (uint)stream_size(stream), insize, insize / (1024 * 1024 * time));

  zfp_stream_close(zfp);
  stream_close(stream);
  free(buffer);

  return 0;
}
Ejemplo n.º 23
0
int main(int argc, char **argv){
	int ret;

	/* validate arguments */
	if ( argc != 2 ){
		fprintf(stderr, "usage: %s FILENAME\n", argv[0]);
		return 1;
	}

	/* load tracefile address */
	const char* filename = argv[1];
	stream_addr_t addr = STREAM_ADDR_INITIALIZER;
	stream_addr_str(&addr, filename, 0);

	/* open stream */
	stream_t stream;
	if ( (ret=stream_open(&stream, &addr, NULL, 0)) != 0 ){
		fprintf(stderr, "%s: %s\n", filename, caputils_error_string(ret));
		return ret;
	}

	/* read packets */
	while ( stream_read_cb(stream, handle_packet, NULL, NULL) == 0 );

	/* close stream */
	stream_close(stream);

	return 0;
}
Ejemplo n.º 24
0
// Update the tranfer state with new information
static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t size)
{
    error_t status;
    vfs_mngr_printf("vfs_manager transfer_stream_data(sector=%i, size=%i)\r\n", sector, size);
    vfs_mngr_printf("    size processed=0x%x, data=%x,%x,%x,%x,...\r\n",
                    file_transfer_state.size_processed, data[0], data[1], data[2], data[3]);

    if (file_transfer_state.stream_finished) {
        util_assert(0);
        return;
    }

    util_assert(size % VFS_SECTOR_SIZE == 0);
    util_assert(file_transfer_state.stream_open);
    status = stream_write((uint8_t *)data, size);
    vfs_mngr_printf("    stream_write ret=%i\r\n", status);

    if (ERROR_SUCCESS_DONE == status) {
        // Override status so ERROR_SUCCESS_DONE
        // does not get passed into transfer_update_state
        status = stream_close();
        vfs_mngr_printf("    stream_close ret=%i\r\n", status);
        file_transfer_state.stream_open = false;
        file_transfer_state.stream_finished = true;
        file_transfer_state.stream_optional_finish = true;
    } else if (ERROR_SUCCESS_DONE_OR_CONTINUE == status) {
        status = ERROR_SUCCESS;
        file_transfer_state.stream_optional_finish = true;
    } else {
        file_transfer_state.stream_optional_finish = false;
    }

    file_transfer_state.size_processed += size;
    transfer_update_state(status);
}
Ejemplo n.º 25
0
Archivo: file.c Proyecto: nielssp/ctodo
TODOLIST *parse_todolist(char *source, size_t length) {
  TODOLIST *list = NULL;
  STREAM *input = stream_buffer(source, length);
  list = read_todolist(input);
  stream_close(input);
  return list;
}
Ejemplo n.º 26
0
Archivo: zurl.c Proyecto: deltheil/zurl
int
main(void)
{
  struct stream *s;
  CURL *curl;
  CURLcode res;

  s = stream_new(ZOUT);
  stream_open(s, Z_BEST_COMPRESSION);

  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();

  curl_easy_setopt(curl, CURLOPT_URL, ZURL);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) s);

  res = curl_easy_perform(curl);
  if (res != CURLE_OK)
    fprintf(stderr, "error: %s\n", curl_easy_strerror(res));

  stream_close(s);
  if (s->error)
    fprintf(stderr, "error: stream is invalid\n");

  curl_easy_cleanup(curl);
  curl_global_cleanup();

  stream_del(s);

  return 0;
}
Ejemplo n.º 27
0
void stream_deinit(struct _libstream_stream* stream) {
  stream_close(stream);
  sem_destroy(&stream->download);
  curl_easy_cleanup(stream->handle);
  queue_deinit(&stream->buffer);
  return;
}
Ejemplo n.º 28
0
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_byte_t stream_next_byte, mp_lexer_stream_close_t stream_close) {
    mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t);

    // check for memory allocation error
    if (lex == NULL) {
        if (stream_close) {
            stream_close(stream_data);
        }
        return NULL;
    }

    lex->source_name = src_name;
    lex->stream_data = stream_data;
    lex->stream_next_byte = stream_next_byte;
    lex->stream_close = stream_close;
    lex->line = 1;
    lex->column = 1;
    lex->emit_dent = 0;
    lex->nested_bracket_level = 0;
    lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
    lex->num_indent_level = 1;
    lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level);
    vstr_init(&lex->vstr, 32);

    // check for memory allocation error
    if (lex->indent_level == NULL || vstr_had_error(&lex->vstr)) {
        mp_lexer_free(lex);
        return NULL;
    }

    // store sentinel for first indentation level
    lex->indent_level[0] = 0;

    // preload characters
    lex->chr0 = stream_next_byte(stream_data);
    lex->chr1 = stream_next_byte(stream_data);
    lex->chr2 = stream_next_byte(stream_data);

    // if input stream is 0, 1 or 2 characters long and doesn't end in a newline, then insert a newline at the end
    if (lex->chr0 == MP_LEXER_EOF) {
        lex->chr0 = '\n';
    } else if (lex->chr1 == MP_LEXER_EOF) {
        if (lex->chr0 == '\r') {
            lex->chr0 = '\n';
        } else if (lex->chr0 != '\n') {
            lex->chr1 = '\n';
        }
    } else if (lex->chr2 == MP_LEXER_EOF) {
        if (lex->chr1 == '\r') {
            lex->chr1 = '\n';
        } else if (lex->chr1 != '\n') {
            lex->chr2 = '\n';
        }
    }

    // preload first token
    mp_lexer_next_token_into(lex, true);

    return lex;
}
Ejemplo n.º 29
0
/* Connects to a fake_pvconn with vconn_open(), accepts that connection and
 * closes it immediately, and verifies that vconn_connect() reports
 * 'expected_error'. */
static void
test_accept_then_close(struct ovs_cmdl_context *ctx)
{
    const char *type = ctx->argv[1];
    struct fake_pvconn fpv;
    struct vconn *vconn;
    int error;

    fpv_create(type, &fpv);
    CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
    vconn_run(vconn);
    stream_close(fpv_accept(&fpv));
    fpv_close(&fpv);

    error = vconn_connect_block(vconn);
    if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
        if (error != ECONNRESET && error != EPIPE
#ifdef _WIN32
            && error != WSAECONNRESET
#endif
            ) {
            ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
                      error, ovs_strerror(error));
        }
    } else {
        CHECK_ERRNO(error, EPROTO);
    }

    vconn_close(vconn);
    fpv_destroy(&fpv);
}
Ejemplo n.º 30
0
/* -1: connection closed, n: wrote n bytes */
gssize stream_write(server *srv, stream *s) {
	gssize len;
	while (-1 == (len = write(s->fd, s->buffer->str, s->buffer->len))) {
		switch (errno) {
		case EAGAIN:
#if EWOULDBLOCK != EAGAIN
		case EWOULDBLOCK:
#endif
			/* try again later */
			return 0;
		case ECONNRESET:
		case EPIPE:
			return stream_closed(srv, s);
		case EINTR: /* try again */
			break;
		default:
			g_message("write error: %s", g_strerror(errno));
			stream_close(srv, s, s->other);
			return -1;
		}
	}
	g_string_erase(s->buffer, 0, len);
	if (s->buffer->len == 0) {
		if (s->other->closed) return stream_closed(srv, s);
		ev_io_rem_events(srv->loop, &s->watcher, EV_WRITE);
	}
	if (s->buffer->len < MAX_STREAM_BUF_SIZE && !s->other->closed)
		ev_io_add_events(srv->loop, &s->other->watcher, EV_READ);
	return 0;
}