Exemplo n.º 1
0
/**
 * asb_utils_ensure_exists_and_empty:
 * @directory: utf8 directory name
 * @error: A #GError or %NULL
 *
 * Ensures a directory exists and empty.
 *
 * Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
asb_utils_ensure_exists_and_empty (const gchar *directory, GError **error)
{
	const gchar *filename;
	g_autoptr(GDir) dir = NULL;

	/* does directory exist */
	if (!asb_utils_ensure_exists (directory, error))
		return FALSE;

	/* try to open */
	dir = g_dir_open (directory, 0, error);
	if (dir == NULL)
		return FALSE;

	/* find each */
	while ((filename = g_dir_read_name (dir))) {
		g_autofree gchar *src = NULL;
		src = g_build_filename (directory, filename, NULL);
		if (g_file_test (src, G_FILE_TEST_IS_DIR)) {
			if (!asb_utils_rmtree (src, error))
				return FALSE;
		} else {
			if (g_unlink (src) != 0) {
				g_set_error (error,
					     ASB_PLUGIN_ERROR,
					     ASB_PLUGIN_ERROR_FAILED,
					     "Failed to delete: %s", src);
				return FALSE;
			}
		}
	}
	return TRUE;
}
Exemplo n.º 2
0
/**
 * asb_package_log_flush:
 * @pkg: A #AsbPackage
 * @error: A #GError or %NULL
 *
 * Flushes the log queue.
 *
 * Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
asb_package_log_flush (AsbPackage *pkg, GError **error)
{
	AsbPackagePrivate *priv = GET_PRIVATE (pkg);
	g_autofree gchar *logfile = NULL;
	g_autofree gchar *logdir_char = NULL;

	/* needs no update */
	if (priv->log_written_len == priv->log->len)
		return TRUE;

	/* don't write if unset */
	if (asb_package_get_config (pkg, "LogDir") == NULL)
		return TRUE;

	/* overwrite old log */
	logdir_char = g_strdup_printf ("%s/%c",
				       asb_package_get_config (pkg, "LogDir"),
				       g_ascii_tolower (priv->name[0]));
	if (!asb_utils_ensure_exists (logdir_char, error))
		return FALSE;
	priv->log_written_len = priv->log->len;
	logfile = g_strdup_printf ("%s/%s.log", logdir_char, priv->name);
	return g_file_set_contents (logfile, priv->log->str, -1, error);
}
Exemplo n.º 3
0
/**
 * asb_context_setup:
 * @ctx: A #AsbContext
 * @error: A #GError or %NULL
 *
 * Sets up the context ready for use.
 *
 * Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
asb_context_setup (AsbContext *ctx, GError **error)
{
	AsbContextPrivate *priv = GET_PRIVATE (ctx);
	g_autofree gchar *icons_dir = NULL;
	g_autofree gchar *screenshot_dir1 = NULL;
	g_autofree gchar *screenshot_dir2 = NULL;

	/* required stuff set */
	if (priv->origin == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_FAILED,
				     "origin not set!");
		return FALSE;
	}
	if (priv->output_dir == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_FAILED,
				     "output_dir not set!");
		return FALSE;
	}
	if (priv->icons_dir == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_FAILED,
				     "icons_dir not set!");
		return FALSE;
	}
	if (priv->temp_dir == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_FAILED,
				     "temp_dir not set!");
		return FALSE;
	}
	if (priv->cache_dir == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_FAILED,
				     "cache_dir not set!");
		return FALSE;
	}

	/* create temp space */
	if (!asb_utils_ensure_exists (priv->output_dir, error))
		return FALSE;
	screenshot_dir1 = g_build_filename (priv->temp_dir, "screenshots", NULL);
	if (!asb_utils_ensure_exists_and_empty (screenshot_dir1, error))
		return FALSE;
	screenshot_dir2 = g_build_filename (priv->cache_dir, "screenshots", NULL);
	if (!asb_utils_ensure_exists (screenshot_dir2, error))
		return FALSE;
	if (priv->log_dir != NULL) {
		if (!asb_utils_ensure_exists (priv->log_dir, error))
			return FALSE;
	}

	/* icons is nuked; we can re-decompress from the -icons.tar.gz */
	if (!asb_utils_ensure_exists (priv->icons_dir, error))
		return FALSE;
	if (priv->flags & ASB_CONTEXT_FLAG_HIDPI_ICONS) {
		g_autofree gchar *icons_dir_hidpi = NULL;
		g_autofree gchar *icons_dir_lodpi = NULL;
		icons_dir_lodpi = g_build_filename (priv->icons_dir, "64x64", NULL);
		if (!asb_utils_ensure_exists (icons_dir_lodpi, error))
			return FALSE;
		icons_dir_hidpi = g_build_filename (priv->icons_dir, "128x128", NULL);
		if (!asb_utils_ensure_exists (icons_dir_hidpi, error))
			return FALSE;
	}

	/* decompress the icons */
	if (priv->old_metadata != NULL) {
		g_autofree gchar *icons_fn = NULL;
		icons_fn = g_strdup_printf ("%s/%s-icons.tar.gz",
					    priv->old_metadata,
					    priv->basename);
		if (g_file_test (icons_fn, G_FILE_TEST_EXISTS)) {
			if (!asb_utils_explode (icons_fn,
						priv->icons_dir,
						NULL,
						error))
				return FALSE;
		}
	}

	/* load plugins */
	if (!asb_plugin_loader_setup (priv->plugin_loader, error))
		return FALSE;

	/* get a cache of the file globs */
	priv->file_globs = asb_plugin_loader_get_globs (priv->plugin_loader);

	/* add old metadata */
	if (priv->old_metadata != NULL) {
		g_autofree gchar *builder_id = NULL;
		g_autofree gchar *fn_failed = NULL;
		g_autofree gchar *fn_ignore = NULL;
		g_autofree gchar *fn_old = NULL;
		g_autoptr(GFile) file_failed = NULL;
		g_autoptr(GFile) file_ignore = NULL;
		g_autoptr(GFile) file_old = NULL;

		builder_id = asb_utils_get_builder_id ();
		fn_old = g_strdup_printf ("%s/%s.xml.gz",
					  priv->old_metadata,
					  priv->basename);
		file_old = g_file_new_for_path (fn_old);
		if (g_file_query_exists (file_old, NULL)) {
			if (!as_store_from_file (priv->store_old, file_old,
						 NULL, NULL, error))
				return FALSE;
			/* check builder-id matches */
			if (g_strcmp0 (as_store_get_builder_id (priv->store_old),
				       builder_id) != 0) {
				g_debug ("builder ID does not match: %s:%s",
					 as_store_get_builder_id (priv->store_old),
					 builder_id);
				as_store_remove_all (priv->store_old);
			}
		}
		fn_ignore = g_strdup_printf ("%s/%s-ignore.xml.gz",
					     priv->old_metadata,
					     priv->basename);
		file_ignore = g_file_new_for_path (fn_ignore);
		if (g_file_query_exists (file_ignore, NULL)) {
			if (!as_store_from_file (priv->store_ignore, file_ignore,
						 NULL, NULL, error))
				return FALSE;
			/* check builder-id matches */
			if (g_strcmp0 (as_store_get_builder_id (priv->store_ignore),
				       builder_id) != 0) {
				g_debug ("builder ID does not match: %s:%s",
					 as_store_get_builder_id (priv->store_ignore),
					 builder_id);
				as_store_remove_all (priv->store_ignore);
			}
		}
		fn_failed = g_strdup_printf ("%s/%s-failed.xml.gz",
					     priv->old_metadata,
					     priv->basename);
		file_failed = g_file_new_for_path (fn_failed);
		if (g_file_query_exists (file_failed, NULL)) {
			if (!as_store_from_file (priv->store_failed, file_failed,
						 NULL, NULL, error))
				return FALSE;
			/* check builder-id matches */
			if (g_strcmp0 (as_store_get_builder_id (priv->store_failed),
				       builder_id) != 0) {
				g_debug ("builder ID does not match: %s:%s",
					 as_store_get_builder_id (priv->store_failed),
					 builder_id);
				as_store_remove_all (priv->store_failed);
			}
		}
	}

	return TRUE;
}