Ejemplo n.º 1
0
int chirp_stream_printf(struct chirp_stream *s, time_t stoptime, const char *fmt, ...)
{
	va_list args;
	int result;

	if(s->mode != CHIRP_STREAM_WRITE) {
		errno = EINVAL;
		return -1;
	}

	while(1) {
		int avail = s->buffer_length - s->buffer_valid;

		va_start(args, fmt);
		result = vsnprintf(&s->buffer[s->buffer_valid], avail, fmt, args);
		va_end(args);

		if(result < 0) {
			return result;
		} else if(result <= avail) {
			s->buffer_valid += result;
			break;
		} else {
			chirp_stream_flush(s, stoptime);
			if(result > s->buffer_length) {
				s->buffer = realloc(s->buffer, result + 1);
				s->buffer_length = result + 1;
			}
		}
	}

	return result;
}
Ejemplo n.º 2
0
int chirp_stream_close(struct chirp_stream *s, time_t stoptime)
{
	chirp_stream_flush(s, stoptime);
	chirp_client_disconnect(s->client);
	free(s->buffer);
	free(s);
	return 0;
}
Ejemplo n.º 3
0
int chirp_stream_write(struct chirp_stream *s, const void *data, int length, time_t stoptime)
{
	if(s->mode != CHIRP_STREAM_WRITE) {
		errno = EINVAL;
		return -1;
	}

	int avail = s->buffer_length - s->buffer_valid;

	if(length > avail) {
		chirp_stream_flush(s, stoptime);
	}

	if(length >= s->buffer_length) {
		return chirp_client_putstream_write(s->client, data, length, stoptime);
	}

	memcpy(&s->buffer[s->buffer_valid], data, length);
	s->buffer_valid += length;

	return length;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	int did_explicit_auth = 0;
	time_t stoptime;
	char c;
	int i, srcindex, nstreams;
	FILE *localfile;
	struct chirp_stream *stream[argc - 2];
	const char *localmode;
	int remotemode;

	debug_config(argv[0]);

	while((c = getopt(argc, argv, "a:b:d:t:vh")) != (char) -1) {
		switch (c) {
		case 'a':
			auth_register_byname(optarg);
			did_explicit_auth = 1;
			break;
		case 'b':
			buffer_size = atoi(optarg);
			break;
		case 'd':
			debug_flags_set(optarg);
			break;
		case 't':
			timeout = string_time_parse(optarg);
			break;
		case 'v':
			show_version(argv[0]);
			exit(0);
			break;
		case 'h':
			show_help(argv[0]);
			exit(0);
			break;

		}
	}

	if(!did_explicit_auth)
		auth_register_all();

	if((argc - optind) < 4) {
		show_help(argv[0]);
		return 1;
	}

	if(!strcmp(argv[optind], "split")) {
		stream_mode = MODE_SPLIT;
		localmode = "r";
		remotemode = CHIRP_STREAM_WRITE;
	} else if(!strcmp(argv[optind], "copy")) {
		stream_mode = MODE_COPY;
		localmode = "r";
		remotemode = CHIRP_STREAM_WRITE;
	} else if(!strcmp(argv[optind], "join")) {
		stream_mode = MODE_JOIN;
		localmode = "w";
		remotemode = CHIRP_STREAM_READ;
	} else {
		fprintf(stderr, "unknown operation: %s\n", argv[0]);
		show_help(argv[0]);
		return 1;
	}

	char *buffer = malloc(buffer_size);

	srcindex = optind + 1;
	nstreams = (argc - optind - 2) / 2;
	stoptime = time(0) + timeout;

	localfile = fopen(argv[srcindex], localmode);
	if(!localfile) {
		fprintf(stderr, "couldn't open %s: %s\n", argv[srcindex], strerror(errno));
		return 1;
	}

	char **hostname = malloc(sizeof(*hostname) * nstreams);
	char **filename = malloc(sizeof(*filename) * nstreams);

	for(i = 0; i < nstreams; i++) {
		hostname[i] = argv[srcindex + (2 * i) + 1];
		filename[i] = argv[srcindex + (2 * i) + 2];

		stream[i] = chirp_stream_open(hostname[i], filename[i], remotemode, stoptime);
		if(!stream[i]) {
			fprintf(stderr, "couldn't open %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
			return 1;
		}
	}


	if(stream_mode == MODE_SPLIT) {
		i = 0;
		while(fgets(buffer, buffer_size, localfile)) {
			int length = strlen(buffer);
			int actual = chirp_stream_write(stream[i], buffer, length, stoptime);
			if(actual != length) {
				fprintf(stderr, "couldn't write to %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
				return 1;
			}
			i = (i + 1) % nstreams;
		}
	} else if(stream_mode == MODE_COPY) {

		while(fgets(buffer, buffer_size, localfile)) {
			int length = strlen(buffer);
			for(i = 0; i < nstreams; i++) {
				int actual = chirp_stream_write(stream[i], buffer, length, stoptime);
				if(actual != length) {
					fprintf(stderr, "couldn't write to %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
					return 1;
				}
			}
		}

	} else {
		int streams_left = nstreams;
		while(streams_left > 0) {
			for(i = 0; i < nstreams; i++) {
				if(!stream[i])
					continue;
				int length = chirp_stream_readline(stream[i], buffer, buffer_size, stoptime);
				if(length > 0) {
					length = strlen(buffer);
					fprintf(localfile, "%s\n", buffer);
				} else {
					streams_left--;
				}
			}
		}
	}

	for(i = 0; i < nstreams; i++) {
		chirp_stream_flush(stream[i], stoptime);
		chirp_stream_close(stream[i], stoptime);
	}

	return 0;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
	int did_explicit_auth = 0;
	time_t stoptime;
	signed char c;
	int i, srcindex, nstreams;
	FILE *localfile;
	struct chirp_stream *stream[argc - 2];
	const char *localmode;
	int remotemode;
	char *tickets = NULL;

	debug_config(argv[0]);

	static struct option long_options[] = {
		{"auth", required_argument, 0, 'a'},
		{"block-size", required_argument, 0, 'b'},
		{"debug", required_argument, 0, 'd'},
		{"tickets", required_argument, 0, 'i'},
		{"timeout", required_argument, 0, 't'},
		{"version", no_argument, 0, 'v'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	while((c = getopt_long(argc, argv, "a:b:d:i:t:vh", long_options, NULL)) > -1) {
		switch (c) {
		case 'a':
			auth_register_byname(optarg);
			did_explicit_auth = 1;
			break;
		case 'b':
			buffer_size = atoi(optarg);
			break;
		case 'd':
			debug_flags_set(optarg);
			break;
		case 'i':
			tickets = strdup(optarg);
			break;
		case 't':
			timeout = string_time_parse(optarg);
			break;
		case 'v':
			cctools_version_print(stdout, argv[0]);
			exit(0);
			break;
		case 'h':
			show_help(argv[0]);
			exit(0);
			break;

		}
	}

	cctools_version_debug(D_DEBUG, argv[0]);

	if(!did_explicit_auth)
		auth_register_all();
	if(tickets) {
		auth_ticket_load(tickets);
		free(tickets);
	} else if(getenv(CHIRP_CLIENT_TICKETS)) {
		auth_ticket_load(getenv(CHIRP_CLIENT_TICKETS));
	} else {
		auth_ticket_load(NULL);
	}

	if((argc - optind) < 4) {
		show_help(argv[0]);
		return 1;
	}

	if(!strcmp(argv[optind], "split")) {
		stream_mode = MODE_SPLIT;
		localmode = "r";
		remotemode = CHIRP_STREAM_WRITE;
	} else if(!strcmp(argv[optind], "copy")) {
		stream_mode = MODE_COPY;
		localmode = "r";
		remotemode = CHIRP_STREAM_WRITE;
	} else if(!strcmp(argv[optind], "join")) {
		stream_mode = MODE_JOIN;
		localmode = "w";
		remotemode = CHIRP_STREAM_READ;
	} else {
		fprintf(stderr, "unknown operation: %s\n", argv[0]);
		show_help(argv[0]);
		return 1;
	}

	char *buffer = malloc(buffer_size);

	srcindex = optind + 1;
	nstreams = (argc - optind - 2) / 2;
	stoptime = time(0) + timeout;

	localfile = fopen(argv[srcindex], localmode);
	if(!localfile) {
		fprintf(stderr, "couldn't open %s: %s\n", argv[srcindex], strerror(errno));
		return 1;
	}

	char **hostname = malloc(sizeof(*hostname) * nstreams);
	char **filename = malloc(sizeof(*filename) * nstreams);

	for(i = 0; i < nstreams; i++) {
		hostname[i] = argv[srcindex + (2 * i) + 1];
		filename[i] = argv[srcindex + (2 * i) + 2];

		stream[i] = chirp_stream_open(hostname[i], filename[i], remotemode, stoptime);
		if(!stream[i]) {
			fprintf(stderr, "couldn't open %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
			return 1;
		}
	}


	if(stream_mode == MODE_SPLIT) {
		i = 0;
		while(fgets(buffer, buffer_size, localfile)) {
			int length = strlen(buffer);
			int actual = chirp_stream_write(stream[i], buffer, length, stoptime);
			if(actual != length) {
				fprintf(stderr, "couldn't write to %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
				return 1;
			}
			i = (i + 1) % nstreams;
		}
	} else if(stream_mode == MODE_COPY) {

		while(fgets(buffer, buffer_size, localfile)) {
			int length = strlen(buffer);
			for(i = 0; i < nstreams; i++) {
				int actual = chirp_stream_write(stream[i], buffer, length, stoptime);
				if(actual != length) {
					fprintf(stderr, "couldn't write to %s:%s: %s\n", hostname[i], filename[i], strerror(errno));
					return 1;
				}
			}
		}

	} else {
		int streams_left = nstreams;
		while(streams_left > 0) {
			for(i = 0; i < nstreams; i++) {
				if(!stream[i])
					continue;
				int length = chirp_stream_readline(stream[i], buffer, buffer_size, stoptime);
				if(length > 0) {
					length = strlen(buffer);
					fprintf(localfile, "%s\n", buffer);
				} else {
					streams_left--;
				}
			}
		}
	}

	for(i = 0; i < nstreams; i++) {
		chirp_stream_flush(stream[i], stoptime);
		chirp_stream_close(stream[i], stoptime);
	}

	return 0;
}