Ejemplo n.º 1
0
Archivo: io.c Proyecto: chwress/salad
const BOOL fwrite_model_zip(FILE* const f, const salad_t* const s)
{
#ifndef USE_ARCHIVES
	return FALSE;
#else
	// Initialize output file
	struct archive* a = archive_write_easyopen(f, ZIP);
	if (a == NULL) return -1;

	// Generate temporary output filename
	char tmpconfig[16], tmpdata[16];
	gen_tmpname(tmpconfig, 16);
	gen_tmpname(tmpdata, 16);

	// Configuration header
	FILE* config = fopen(tmpconfig, "w+");
	if (config == NULL) return -1;

	const BOOL config_ok = fwrite_modelconfig_ex(config, s);

	BOOL data_ok = FALSE;
	CONTAINER_OUTPUTSTATE_T(state);
	do
	{
		// Container data
		FILE* data = fopen(tmpdata, "w+");
		if (data == NULL)
		{
			fclose(config);
			remove(tmpconfig);
			return FALSE;
		}

		container_outputspec_t spec = {config, data, CONTAINER_OUTPUTFMT_SEPARATED};
		data_ok = fwrite_modeldata(&spec, s, &state);

		fclose(data);
		if (!data_ok) break;

		archive_write_file(a, tmpdata, state.filename);
		remove(tmpdata);

	} while (!state.done);

	fclose(config);
	if (!config_ok || !config_ok)
	{
		remove(tmpconfig);
		remove(tmpdata);
		return FALSE;
	}

	archive_write_file(a, tmpconfig, "config");
	remove(tmpconfig);

	archive_write_easyclose(a);
	return TRUE;
#endif
}
Ejemplo n.º 2
0
int archive_save(tArchiveFile *files, int numFiles, char *filename)
{
	int fd, len;
	unsigned int i;

	if (numFiles < 0)
		numFiles = _fileNum;

	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
	if (fd < 0) {
		int errno_saved = errno;

		DPRINTF("Cannot create archive file %s, error %d (%s)\n", filename, errno_saved, strerror(errno_saved));
		return -errno_saved;
	}

	len = archive_write_header(fd, files, numFiles);
	DPRINTF("Number of files is %d\n", numFiles);
	DPRINTF("Archive header size: %d (0x%x) bytes\n", len, len);

	for (i = 0; i < numFiles; i++)
		archive_write_file(fd, basename(files[i].name), files[i].compressed_size);

	/* Now alter the header size */
	unsigned char dataWord[2] = { 0 };
	lseek(fd, HEADER_SIZE_OFFSET, SEEK_SET);
	WORDSTR(dataWord, len);
	write(fd, dataWord, sizeof(dataWord));

	close(fd);

	return 0;
}