/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *		Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/* This isn't as portable as, say, the stuff in GNU coreutils.
 * But I care not for OSF1. */
#ifdef HAVE_STATVFS
# ifdef HAVE_SYS_STATVFS_H
#  include <sys/statvfs.h>
# endif
#else
#ifdef HAVE_STATFS
# ifdef HAVE_SYS_PARAM_H
#  include <sys/param.h>	/* bsd interface */
# endif
# ifdef HAVE_SYS_MOUNT_H
#  include <sys/mount.h>
# endif
#endif
#endif

#include <errno.h>
#include <string.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>

#include "e-activity.h"
#include "e-file-utils.h"

typedef struct _AsyncContext AsyncContext;

struct _AsyncContext {
	EActivity *activity;
	gchar *new_etag;
};

static void
async_context_free (AsyncContext *context)
{
	if (context->activity != NULL)
		g_object_unref (context->activity);

	g_free (context->new_etag);

	g_slice_free (AsyncContext, context);
}

static void
file_replace_contents_cb (GFile *file,
                          GAsyncResult *result,
                          GSimpleAsyncResult *simple)
{
	AsyncContext *context;
	gchar *new_etag = NULL;
	GError *error = NULL;

	context = g_simple_async_result_get_op_res_gpointer (simple);

	g_file_replace_contents_finish (file, result, &new_etag, &error);

	if (!e_activity_handle_cancellation (context->activity, error))
		e_activity_set_state (context->activity, E_ACTIVITY_COMPLETED);

	if (error == NULL)
		context->new_etag = new_etag;
	else {
		g_warn_if_fail (new_etag == NULL);
		g_simple_async_result_set_from_error (simple, error);
		g_error_free (error);
	}

	g_simple_async_result_complete (simple);

	g_object_unref (simple);
}

/**
 * e_file_replace_contents_async:
 * @file: input #GFile
 * @contents: string of contents to replace the file with
 * @length: the length of @contents in bytes
 * @etag: a new entity tag for the @file, or %NULL
 * @make_backup: %TRUE if a backup should be created
 * @flags: a set of #GFileCreateFlags
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
 * @user_data: the data to pass to the callback function
 *
 * This is a wrapper for g_file_replace_contents_async() that also returns
 * an #EActivity to track the file operation.  Cancelling the activity will
 * cancel the file operation.  See g_file_replace_contents_async() for more
 * details.
 *
 * Returns: an #EActivity for the file operation
 **/
EActivity *
e_file_replace_contents_async (GFile *file,
                               const gchar *contents,
                               gsize length,
                               const gchar *etag,
                               gboolean make_backup,
                               GFileCreateFlags flags,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	GSimpleAsyncResult *simple;
	GCancellable *cancellable;
	AsyncContext *context;
	const gchar *format;
	gchar *description;
	gchar *basename;
	gchar *filename;
	gchar *hostname;
	gchar *uri;

	g_return_val_if_fail (G_IS_FILE (file), NULL);
	g_return_val_if_fail (contents != NULL, NULL);

	uri = g_file_get_uri (file);
	filename = g_filename_from_uri (uri, &hostname, NULL);
	if (filename != NULL)
		basename = g_filename_display_basename (filename);
	else
		basename = g_strdup (_("(Unknown Filename)"));

	if (hostname == NULL) {
		/* Translators: The string value is the basename of a file. */
		format = _("Writing \"%s\"");
		description = g_strdup_printf (format, basename);
	} else {
		/* Translators: The first string value is the basename of a
		 * remote file, the second string value is the hostname. */
		format = _("Writing \"%s\" to %s");
		description = g_strdup_printf (format, basename, hostname);
	}

	cancellable = g_cancellable_new ();

	context = g_slice_new0 (AsyncContext);
	context->activity = e_activity_new ();

	e_activity_set_text (context->activity, description);
	e_activity_set_cancellable (context->activity, cancellable);

	simple = g_simple_async_result_new (
		G_OBJECT (file), callback, user_data,
		e_file_replace_contents_async);

	g_simple_async_result_set_op_res_gpointer (
		simple, context, (GDestroyNotify) async_context_free);

	g_file_replace_contents_async (
		file, contents, length, etag,
		make_backup, flags, cancellable,
		(GAsyncReadyCallback) file_replace_contents_cb,
		simple);

	g_object_unref (cancellable);

	g_free (description);
	g_free (basename);
	g_free (filename);
	g_free (hostname);
	g_free (uri);

	return context->activity;
}

/**
 * e_file_replace_contents_finish:
 * @file: input #GFile
 * @result: a #GAsyncResult
 * @new_etag: return location for a new entity tag
 * @error: return location for a #GError, or %NULL
 *
 * Finishes an asynchronous replace of the given @file.  See
 * e_file_replace_contents_async().  Sets @new_etag to the new entity
 * tag for the document, if present.  Free it with g_free() when it is
 * no longer needed.
 *
 * Returns: %TRUE on success, %FALSE on failure
 **/
gboolean
e_file_replace_contents_finish (GFile *file,
                                GAsyncResult *result,
                                gchar **new_etag,
                                GError **error)
{
	GSimpleAsyncResult *simple;
	AsyncContext *context;

	g_return_val_if_fail (G_IS_FILE (file), FALSE);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	context = g_simple_async_result_get_op_res_gpointer (simple);

	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;

	if (new_etag != NULL)
		*new_etag = g_strdup (context->new_etag);

	return TRUE;
}

eeBSD Ports (https://github.com/freebsd/freebsd-ports)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/about/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/refs/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/stats/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/databases/linux-oracle-instantclient-basic'>
<input type='hidden' name='h' value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'/><input type='hidden' name='id' value='bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/databases?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>databases</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3'>linux-oracle-instantclient-basic</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/databases/linux-oracle-instantclient-basic?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=bc62eb56f7c34380e5ea8dbe6390ebb1f5f520b3&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
tion value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>bapt &lt;bapt@FreeBSD.org&gt;</td><td class='right'>2013-09-21 02:35:44 +0800</td></tr>
<tr><th>committer</th><td>bapt &lt;bapt@FreeBSD.org&gt;</td><td class='right'>2013-09-21 02:35:44 +0800</td></tr>
<tr><th>commit</th><td colspan='2' class='sha1'><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/graphics/zbar?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=5495ad1eb0426206406e428e711fb1ba9e3150ce'>5495ad1eb0426206406e428e711fb1ba9e3150ce</a> (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/patch/graphics/zbar?id=5495ad1eb0426206406e428e711fb1ba9e3150ce'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='sha1'><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=5495ad1eb0426206406e428e711fb1ba9e3150ce'>f4861f167b47e08ed6e9f5bbe5172252c8b51efc</a> /<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/graphics/zbar?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=5495ad1eb0426206406e428e711fb1ba9e3150ce'>graphics/zbar</a></td></tr>
<tr><th>parent</th><td colspan='2' class='sha1'><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/graphics/zbar?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=2012706cb95e488737909070df298912a9c68273'>2012706cb95e488737909070df298912a9c68273</a> (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/graphics/zbar?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=5495ad1eb0426206406e428e711fb1ba9e3150ce&amp;id2=2012706cb95e488737909070df298912a9c68273'>diff</a>)</td></tr><tr><th>download</th><td colspan='2' class='sha1'><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/snapshot/freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.tar.gz'>freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.tar.gz</a><br/><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/snapshot/freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.tar.zst'>freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.tar.zst</a><br/><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/snapshot/freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.zip'>freebsd-ports-5495ad1eb0426206406e428e711fb1ba9e3150ce.zip</a><br/></td></tr></table>
<div class='commit-subject'>Add NO_STAGE all over the place in preparation for the staging support (cat: graphics)</div><div class='commit-msg'></div><div class='diffstat-header'><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=5495ad1eb0426206406e428e711fb1ba9e3150ce'>Diffstat</a> (limited to 'graphics/zbar')</div><table summary='diffstat' class='diffstat'>