Пример #1
0
void init_custom_update()
{
	char *str = NULL;
	size_t str_size = 256;
	Uint32 i;

	memset(update_thread_data, 0, sizeof(update_thread_data));

	update_thread_data[0].dir = datadir;
	update_thread_data[1].dir = get_path_config_base();

	if ((str = (char *)calloc(sizeof(char), str_size)) == NULL)
		return;

	for (i = 0; i < 2; i++)
	{
		safe_snprintf(str, str_size, "%s%s", get_path_config_base(),
			zip_names[i]);
		load_zip_archive(str);

		safe_snprintf(update_thread_data[i].str,
			sizeof(update_thread_data[i].str),
			"%s custom updates: %s", update_names[i], "waiting");
		update_thread_data[i].mutex = SDL_CreateMutex();
		update_thread_data[i].condition = SDL_CreateCond();
		update_thread_data[i].zip = zip_names[i];
		update_thread_data[i].name = update_names[i];
		update_thread_data[i].running = 1;
		update_thread_data[i].error = 0;
		update_thread_data[i].thread = SDL_CreateThread(
			custom_update_thread, "CustomupdateThread", &update_thread_data[i]);
	}
	free(str);
}
Пример #2
0
Uint32 update(const char* server, const char* file, const char* dir,
	const char* zip, progress_fnc update_progress_function, void* user_data)
{
	char *tmp[MAX_OLD_UPDATE_FILES];
	const size_t tmp_size = 1024;
	char *path = NULL;
	const size_t path_size = 1024;
	char *str = NULL;
	const size_t str_size = 1024;
	char *etag = NULL;
	const size_t etag_size = 1024;
	char md5[33];
	unzFile source_zips[MAX_OLD_UPDATE_FILES];
	zipFile dest_zip;
	update_info_t* infos;
	Uint32 count, result, i;

	path = (char*)calloc(sizeof(char), path_size);
	safe_snprintf(path, path_size, "http://%s/%s/", server, dir);

	str = (char *)calloc(sizeof(char), str_size);
	safe_snprintf(str, str_size, "Downloading from server %s", path);
	update_progress_function(str, 0, 0, user_data);

	for (i = 0; i < MAX_OLD_UPDATE_FILES; i++)
	{
		tmp[i] = (char *)calloc(sizeof(char), tmp_size);

		safe_snprintf(tmp[i], tmp_size, "%s%s%i", zip, ".t", i);
	}

	safe_snprintf(str, str_size, "Opening %s", zip);
	update_progress_function(str, 0, 0, user_data);

	memset(str, 0, str_size);

	source_zips[0] = unzOpen64(zip);
	unzGetGlobalComment(source_zips[0], str, str_size);
	unzClose(source_zips[0]);

	infos = 0;
	count = 0;

	etag = (char *)calloc(sizeof(char), etag_size);
	memset(md5, 0, sizeof(md5));
	sscanf(str, "ETag: %s MD5: %32s", etag, md5);

	result = build_update_list(server, file, path, &infos, &count, md5,
		etag_size, etag, update_progress_function, user_data);

	if (result != 0)
	{
		free(path);
		free(str);
		free(etag);
		for (i = 0; i < MAX_OLD_UPDATE_FILES; i++)
			free(tmp[i]);

		if (result == 1)
			return 0;

		free(infos);
		return 3;
	}

	source_zips[0] = unzOpen64(zip);

	remove(tmp[MAX_OLD_UPDATE_FILES - 1]);

	for (i = MAX_OLD_UPDATE_FILES - 1; i > 0; i--)
	{
		rename(tmp[i - 1], tmp[i]);
		source_zips[i] = unzOpen64(tmp[i]);
	}

	dest_zip = zipOpen64(tmp[0], APPEND_STATUS_CREATE);

	result = download_files(infos, count, server, path,
		MAX_OLD_UPDATE_FILES, source_zips, dest_zip,
		update_progress_function, user_data);

	if (result == 0)
	{
		memset(str, 0, str_size);
		safe_snprintf(str, str_size, "ETag: %s MD5: %s", etag, md5);
	}

	for (i = 0; i < MAX_OLD_UPDATE_FILES; i++)
	{
		unzClose(source_zips[i]);
	}

	zipClose(dest_zip, str);

	if (result == 2)
	{
		update_progress_function("Canceled updating", 0, 0,
			user_data);
	}

	free(infos);

	if (result != 0)
	{
		free(path);
		free(str);
		free(etag);
		for (i = 0; i < MAX_OLD_UPDATE_FILES; i++)
			free(tmp[i]);
		return result;
	}

	unload_zip_archive(zip);
	remove(zip);
	rename(tmp[0], zip);
	load_zip_archive(zip);

	for (i = 1; i < MAX_OLD_UPDATE_FILES; i++)
	{
		remove(tmp[i]);
	}

	update_progress_function("Update complete", 0, 0, user_data);

	free(path);
	free(str);
	free(etag);
	for (i = 0; i < MAX_OLD_UPDATE_FILES; i++)
		free(tmp[i]);

	return 0;
}