Esempio n. 1
0
int real_time_listen(
	serial_port_t const port,
	struct activity *activity,
	struct model const *const model
) {
	double elapsed;
	struct packet packet;
	struct timespec tic;
	char message[MSG_LEN];
	struct calibr calibr[CONF_BAN_NUM_NODES];
	if (calibration_table_read(FILE_CONF_CALIBRATION, calibr))
		return EXIT_FAILURE;
	file_write_header(activity->files[FILE_RAW]);
	file_write_header(activity->files[FILE_HISTORY]);
	clock_gettime(CLOCK_MONOTONIC, &tic);
	while (time_stopwatch(tic, CONF_TIME_LISTENING, &elapsed)) {
		if (serial_port_read(port, message, sizeof(message)))
			return EXIT_FAILURE;
		file_write_raw(activity->files[FILE_RAW], message);
		if (packet_validate(message, &packet))
			continue;
		packet.elapsed = elapsed;
		if (calibrate_packet(&packet, calibr, sizeof(calibr)))
			return EXIT_FAILURE;
		file_write_history(activity->files[FILE_HISTORY], packet);
		process_region(&packet, model);
		activity->window[packet.node_id - 1][packet.region - 1]++;
	}
	return EXIT_SUCCESS;
}
Esempio n. 2
0
void file_write_activity_code(
	FILE *file,
	struct activity const *const activity,
	struct model const *const model
) {
	file_write_header(file);
	fprintf(file, "# Activity: %s\n", activity->name);
	fprintf(file, "# User: %s\n\n", activity->user);
	for (int i = 0; i < CONF_BAN_NUM_NODES; i++) {
		for (int j = 0; j < model->num_region; j++)
			fprintf(file, "%3d ", activity->window[i][j]);
		fprintf(file, "\n");
	}
}
Esempio n. 3
0
int transform(const char *fpath, const struct stat *sb, int typeflag, struct FTW* ftwbuf)
{
	int compress_type;
	int fd_s, fd_t;
	struct stat stbuf_s;
	struct utimbuf utime_s;
	char *tmpname;
	off_t size;
	compressor_t *decomp;

	if (typeflag != FTW_F || !S_ISREG(sb->st_mode))
		return 0;

	/* internal file (attribute file, dedup DB) */
	if (strncmp(&fpath[ftwbuf->base], FUSECOMPRESS_PREFIX, sizeof(FUSECOMPRESS_PREFIX) - 1) == 0)
		return 0;

	if (verbose)
		fprintf(stderr, "%s: ", fpath);

	if (comp && !is_compressible(&fpath[ftwbuf->base])) {
		if (verbose)
			fprintf(stderr, "incompressible\n");
		return 0;
	}

	compress_type = is_compressed(fpath);
	if (compress_type < -1)
	{
		fprintf(stderr, "failed to check %s for compression status\n", fpath);
		errors++;
		return 0;
	}

	/* if this is the type we are supposed to recompress, decompress
	   it first */
	if (comp && recomp && compress_type == recomp->type) {
		compressor_t *temp_c = comp;
		int temp_v = verbose;
		comp = NULL;
		if (verbose)
			fprintf(stderr, "recompress ");
		verbose = 0;
		transform(fpath, sb, typeflag, ftwbuf);
		verbose = temp_v;
		comp = temp_c;
		compress_type = -1;
	}

	if (comp && compress_type >= 0)
	{
		if (verbose)
			fprintf(stderr, "compressed already\n");
		return 0;
	}
	if (!comp && compress_type < 0)
	{
		if (verbose)
			fprintf(stderr, "uncompressed already\n");
		return 0;
	}

	fd_s = file_open(fpath, O_RDONLY);
	if (fstat(fd_s, &stbuf_s) < 0)
	{
		perror("unable to fstat");
		return -1;
	}
	tmpname = file_create_temp(&fd_t);
	current_tmp_file = tmpname;

	if (!tmpname)
	{
		fprintf(stderr, "unable to create temporary file for compression\n");
		return -1;
	}

	if (comp)
	{
		if (lseek(fd_t, sizeof(header_t), SEEK_SET) < 0)
		{
			perror("failed to seek beyond header");
			goto out;
		}
		size = comp->compress(NULL, fd_s, fd_t);
		if (lseek(fd_t, 0, SEEK_SET) < 0)
		{
			perror("failed to seek to beginning");
			goto out;
		}
		if (file_write_header(fd_t, comp, stbuf_s.st_size) < 0)
		{
			perror("failed to write header");
			goto out;
		}
	}
	else
	{
		if (file_read_header_fd(fd_s, &decomp, &size) < 0)
		{
			perror("unable to read header");
			goto out;
		}
		size = decomp->decompress(fd_s, fd_t);
	}

	if (size == (off_t) - 1)
	{
		fprintf(stderr, "compression failed\n");
		goto out;
	}
	close(fd_s);
	close(fd_t);
	if (rename(tmpname, fpath) < 0)
	{
		perror("failed to rename tempfile");
		goto out;
	}

	current_tmp_file = NULL;

	if (lchown(fpath, stbuf_s.st_uid, stbuf_s.st_gid) < 0)
	{
		perror("unable to set owner/group");
		/* do not abort; the filesystem may not support this */
	}
	if (chmod(fpath, stbuf_s.st_mode) < 0)
	{
		perror("unable to set permissions");
		/* do not abort; the filesystem may not support this */
	}
	utime_s.actime = stbuf_s.st_atime;
	utime_s.modtime = stbuf_s.st_mtime;
	if (utime(fpath, &utime_s) < 0)
	{
		perror("failed to set timestamps");
		goto out;
	}

	if (verbose)
		fprintf(stderr, "ok\n");

	return 0;

out:
	close(fd_s);
	close(fd_t);
	unlink(tmpname);
	return -1;
}