xed/xedit/xedit-gio-document-saver.c

776 lines
19 KiB
C
Raw Permalink Normal View History

2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* xedit-gio-document-saver.c
* This file is part of xedit
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2005-2006 - Paolo Borelli and Paolo Maggi
* Copyright (C) 2007 - Paolo Borelli, Paolo Maggi, Steve Frécinaux
* Copyright (C) 2008 - Jesse van den Kieboom
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
/*
2016-01-25 08:13:49 -06:00
* Modified by the xedit Team, 2005-2006. See the AUTHORS file for a
* list of people on the xedit Team.
2011-11-07 13:46:58 -06:00
* See the ChangeLog files for a list of changes.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib/gi18n.h>
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
2016-01-25 08:13:49 -06:00
#include "xedit-gio-document-saver.h"
#include "xedit-document-input-stream.h"
#include "xedit-debug.h"
2011-11-07 13:46:58 -06:00
#define WRITE_CHUNK_SIZE 8192
typedef struct
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *saver;
2011-11-07 13:46:58 -06:00
gchar buffer[WRITE_CHUNK_SIZE];
GCancellable *cancellable;
gboolean tried_mount;
gssize written;
gssize read;
GError *error;
} AsyncData;
#define REMOTE_QUERY_ATTRIBUTES G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," \
G_FILE_ATTRIBUTE_TIME_MODIFIED
2016-01-25 08:13:49 -06:00
#define XEDIT_GIO_DOCUMENT_SAVER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
XEDIT_TYPE_GIO_DOCUMENT_SAVER, \
XeditGioDocumentSaverPrivate))
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
static void xedit_gio_document_saver_save (XeditDocumentSaver *saver,
2011-11-07 13:46:58 -06:00
GTimeVal *old_mtime);
2016-01-25 08:13:49 -06:00
static goffset xedit_gio_document_saver_get_file_size (XeditDocumentSaver *saver);
static goffset xedit_gio_document_saver_get_bytes_written (XeditDocumentSaver *saver);
2011-11-07 13:46:58 -06:00
static void check_modified_async (AsyncData *async);
2016-01-25 08:13:49 -06:00
struct _XeditGioDocumentSaverPrivate
2011-11-07 13:46:58 -06:00
{
GTimeVal old_mtime;
goffset size;
goffset bytes_written;
GFile *gfile;
GCancellable *cancellable;
GOutputStream *stream;
GInputStream *input;
GError *error;
};
2016-01-25 08:13:49 -06:00
G_DEFINE_TYPE(XeditGioDocumentSaver, xedit_gio_document_saver, XEDIT_TYPE_DOCUMENT_SAVER)
2011-11-07 13:46:58 -06:00
static void
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_dispose (GObject *object)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaverPrivate *priv = XEDIT_GIO_DOCUMENT_SAVER (object)->priv;
2011-11-07 13:46:58 -06:00
if (priv->cancellable != NULL)
{
g_cancellable_cancel (priv->cancellable);
g_object_unref (priv->cancellable);
priv->cancellable = NULL;
}
if (priv->gfile != NULL)
{
g_object_unref (priv->gfile);
priv->gfile = NULL;
}
if (priv->error != NULL)
{
g_error_free (priv->error);
priv->error = NULL;
}
if (priv->stream != NULL)
{
g_object_unref (priv->stream);
priv->stream = NULL;
}
if (priv->input != NULL)
{
g_object_unref (priv->input);
priv->input = NULL;
}
2016-01-25 08:13:49 -06:00
G_OBJECT_CLASS (xedit_gio_document_saver_parent_class)->dispose (object);
2011-11-07 13:46:58 -06:00
}
static AsyncData *
2016-01-25 08:13:49 -06:00
async_data_new (XeditGioDocumentSaver *gvsaver)
2011-11-07 13:46:58 -06:00
{
AsyncData *async;
async = g_slice_new (AsyncData);
async->saver = gvsaver;
async->cancellable = g_object_ref (gvsaver->priv->cancellable);
async->tried_mount = FALSE;
async->written = 0;
async->read = 0;
async->error = NULL;
return async;
}
static void
async_data_free (AsyncData *async)
{
g_object_unref (async->cancellable);
if (async->error)
{
g_error_free (async->error);
}
g_slice_free (AsyncData, async);
}
static void
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_class_init (XeditGioDocumentSaverClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2016-01-25 08:13:49 -06:00
XeditDocumentSaverClass *saver_class = XEDIT_DOCUMENT_SAVER_CLASS (klass);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
object_class->dispose = xedit_gio_document_saver_dispose;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
saver_class->save = xedit_gio_document_saver_save;
saver_class->get_file_size = xedit_gio_document_saver_get_file_size;
saver_class->get_bytes_written = xedit_gio_document_saver_get_bytes_written;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
g_type_class_add_private (object_class, sizeof(XeditGioDocumentSaverPrivate));
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_init (XeditGioDocumentSaver *gvsaver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
gvsaver->priv = XEDIT_GIO_DOCUMENT_SAVER_GET_PRIVATE (gvsaver);
2011-11-07 13:46:58 -06:00
gvsaver->priv->cancellable = g_cancellable_new ();
gvsaver->priv->error = NULL;
}
static void
2016-01-25 08:13:49 -06:00
remote_save_completed_or_failed (XeditGioDocumentSaver *gvsaver,
2011-11-07 13:46:58 -06:00
AsyncData *async)
{
2016-01-25 08:13:49 -06:00
xedit_document_saver_saving (XEDIT_DOCUMENT_SAVER (gvsaver),
2011-11-07 13:46:58 -06:00
TRUE,
gvsaver->priv->error);
if (async)
async_data_free (async);
}
static void
async_failed (AsyncData *async,
GError *error)
{
g_propagate_error (&async->saver->priv->error, error);
remote_save_completed_or_failed (async->saver, async);
}
/* BEGIN NOTE:
*
* This fixes an issue in GOutputStream that applies the atomic replace
* save strategy. The stream moves the written file to the original file
* when the stream is closed. However, there is no way currently to tell
* the stream that the save should be aborted (there could be a
* conversion error). The patch explicitly closes the output stream
* in all these cases with a GCancellable in the cancelled state, causing
* the output stream to close, but not move the file. This makes use
* of an implementation detail in the local gio file stream and should be
* properly fixed by adding the appropriate API in gio. Until then, at least
* we prevent data corruption for now.
*
* Relevant bug reports:
*
2016-01-25 08:13:49 -06:00
* Bug 615110 - write file ignore encoding errors (xedit)
2011-11-07 16:52:18 -06:00
* https://bugzilla.gnome.org/show_bug.cgi?id=615110
2011-11-07 13:46:58 -06:00
*
* Bug 602412 - g_file_replace does not restore original file when there is
* errors while writing (glib/gio)
2011-11-07 16:52:18 -06:00
* https://bugzilla.gnome.org/show_bug.cgi?id=602412
2011-11-07 13:46:58 -06:00
*/
static void
cancel_output_stream_ready_cb (GOutputStream *stream,
GAsyncResult *result,
AsyncData *async)
{
GError *error;
g_output_stream_close_finish (stream, result, NULL);
/* check cancelled state manually */
if (g_cancellable_is_cancelled (async->cancellable) || async->error == NULL)
{
async_data_free (async);
return;
}
error = async->error;
async->error = NULL;
async_failed (async, error);
}
static void
cancel_output_stream (AsyncData *async)
{
GCancellable *cancellable;
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Cancel output stream");
2011-11-07 13:46:58 -06:00
cancellable = g_cancellable_new ();
g_cancellable_cancel (cancellable);
g_output_stream_close_async (async->saver->priv->stream,
G_PRIORITY_HIGH,
cancellable,
(GAsyncReadyCallback)cancel_output_stream_ready_cb,
async);
g_object_unref (cancellable);
}
static void
cancel_output_stream_and_fail (AsyncData *async,
GError *error)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Cancel output stream and fail");
2011-11-07 13:46:58 -06:00
g_propagate_error (&async->error, error);
cancel_output_stream (async);
}
/*
* END NOTE
*/
static void
remote_get_info_cb (GFile *source,
GAsyncResult *res,
AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *saver;
2011-11-07 13:46:58 -06:00
GFileInfo *info;
GError *error = NULL;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* check cancelled state manually */
if (g_cancellable_is_cancelled (async->cancellable))
{
async_data_free (async);
return;
}
saver = async->saver;
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Finished query info on file");
2011-11-07 13:46:58 -06:00
info = g_file_query_info_finish (source, res, &error);
if (info != NULL)
{
2016-01-25 08:13:49 -06:00
if (XEDIT_DOCUMENT_SAVER (saver)->info != NULL)
g_object_unref (XEDIT_DOCUMENT_SAVER (saver)->info);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_DOCUMENT_SAVER (saver)->info = info;
2011-11-07 13:46:58 -06:00
}
else
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Query info failed: %s", error->message);
2011-11-07 13:46:58 -06:00
g_propagate_error (&saver->priv->error, error);
}
remote_save_completed_or_failed (saver, async);
}
static void
close_async_ready_get_info_cb (GOutputStream *stream,
GAsyncResult *res,
AsyncData *async)
{
GError *error = NULL;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* check cancelled state manually */
if (g_cancellable_is_cancelled (async->cancellable))
{
async_data_free (async);
return;
}
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Finished closing stream");
2011-11-07 13:46:58 -06:00
if (!g_output_stream_close_finish (stream, res, &error))
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Closing stream error: %s", error->message);
2011-11-07 13:46:58 -06:00
async_failed (async, error);
return;
}
/* get the file info: note we cannot use
* g_file_output_stream_query_info_async since it is not able to get the
* content type etc, beside it is not supported by gvfs.
* I'm not sure this is actually necessary, can't we just use
* g_content_type_guess (since we have the file name and the data)
*/
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Query info on file");
2011-11-07 13:46:58 -06:00
g_file_query_info_async (async->saver->priv->gfile,
REMOTE_QUERY_ATTRIBUTES,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_HIGH,
async->cancellable,
(GAsyncReadyCallback) remote_get_info_cb,
async);
}
static void
write_complete (AsyncData *async)
{
GError *error = NULL;
/* first we close the input stream */
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Close input stream");
2011-11-07 13:46:58 -06:00
if (!g_input_stream_close (async->saver->priv->input,
async->cancellable, &error))
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Closing input stream error: %s", error->message);
2011-11-07 13:46:58 -06:00
cancel_output_stream_and_fail (async, error);
return;
}
/* now we close the output stream */
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Close output stream");
2011-11-07 13:46:58 -06:00
g_output_stream_close_async (async->saver->priv->stream,
G_PRIORITY_HIGH,
async->cancellable,
(GAsyncReadyCallback)close_async_ready_get_info_cb,
async);
}
/* prototype, because they call each other... isn't C lovely */
static void read_file_chunk (AsyncData *async);
static void write_file_chunk (AsyncData *async);
static void
async_write_cb (GOutputStream *stream,
GAsyncResult *res,
AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
2011-11-07 13:46:58 -06:00
gssize bytes_written;
GError *error = NULL;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* Check cancelled state manually */
if (g_cancellable_is_cancelled (async->cancellable))
{
cancel_output_stream (async);
return;
}
bytes_written = g_output_stream_write_finish (stream, res, &error);
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Written: %" G_GSSIZE_FORMAT, bytes_written);
2011-11-07 13:46:58 -06:00
if (bytes_written == -1)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Write error: %s", error->message);
2011-11-07 13:46:58 -06:00
cancel_output_stream_and_fail (async, error);
return;
}
gvsaver = async->saver;
async->written += bytes_written;
/* write again */
if (async->written != async->read)
{
write_file_chunk (async);
return;
}
/* note that this signal blocks the write... check if it isn't
* a performance problem
*/
2016-01-25 08:13:49 -06:00
xedit_document_saver_saving (XEDIT_DOCUMENT_SAVER (gvsaver),
2011-11-07 13:46:58 -06:00
FALSE,
NULL);
read_file_chunk (async);
}
static void
write_file_chunk (AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
gvsaver = async->saver;
g_output_stream_write_async (G_OUTPUT_STREAM (gvsaver->priv->stream),
async->buffer + async->written,
async->read - async->written,
G_PRIORITY_HIGH,
async->cancellable,
(GAsyncReadyCallback) async_write_cb,
async);
}
static void
read_file_chunk (AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
XeditDocumentInputStream *dstream;
2011-11-07 13:46:58 -06:00
GError *error = NULL;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
gvsaver = async->saver;
async->written = 0;
/* we use sync methods on doc stream since it is in memory. Using async
would be racy and we can endup with invalidated iters */
async->read = g_input_stream_read (gvsaver->priv->input,
async->buffer,
WRITE_CHUNK_SIZE,
async->cancellable,
&error);
if (error != NULL)
{
cancel_output_stream_and_fail (async, error);
return;
}
/* Check if we finished reading and writing */
if (async->read == 0)
{
write_complete (async);
return;
}
/* Get how many chars have been read */
2016-01-25 08:13:49 -06:00
dstream = XEDIT_DOCUMENT_INPUT_STREAM (gvsaver->priv->input);
gvsaver->priv->bytes_written = xedit_document_input_stream_tell (dstream);
2011-11-07 13:46:58 -06:00
write_file_chunk (async);
}
static void
async_replace_ready_callback (GFile *source,
GAsyncResult *res,
AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
XeditDocumentSaver *saver;
2011-11-07 13:46:58 -06:00
GCharsetConverter *converter;
GFileOutputStream *file_stream;
GError *error = NULL;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* Check cancelled state manually */
if (g_cancellable_is_cancelled (async->cancellable))
{
async_data_free (async);
return;
}
gvsaver = async->saver;
2016-01-25 08:13:49 -06:00
saver = XEDIT_DOCUMENT_SAVER (gvsaver);
2011-11-07 13:46:58 -06:00
file_stream = g_file_replace_finish (source, res, &error);
/* handle any error that might occur */
if (!file_stream)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Opening file failed: %s", error->message);
2011-11-07 13:46:58 -06:00
async_failed (async, error);
return;
}
/* FIXME: manage converter error? */
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Encoding charset: %s",
xedit_encoding_get_charset (saver->encoding));
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
if (saver->encoding != xedit_encoding_get_utf8 ())
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
converter = g_charset_converter_new (xedit_encoding_get_charset (saver->encoding),
2011-11-07 13:46:58 -06:00
"UTF-8",
NULL);
gvsaver->priv->stream = g_converter_output_stream_new (G_OUTPUT_STREAM (file_stream),
G_CONVERTER (converter));
g_object_unref (file_stream);
g_object_unref (converter);
}
else
{
gvsaver->priv->stream = G_OUTPUT_STREAM (file_stream);
}
2016-01-25 08:13:49 -06:00
gvsaver->priv->input = xedit_document_input_stream_new (GTK_TEXT_BUFFER (saver->document),
2011-11-07 13:46:58 -06:00
saver->newline_type);
2016-01-25 08:13:49 -06:00
gvsaver->priv->size = xedit_document_input_stream_get_total_size (XEDIT_DOCUMENT_INPUT_STREAM (gvsaver->priv->input));
2011-11-07 13:46:58 -06:00
read_file_chunk (async);
}
static void
begin_write (AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
XeditDocumentSaver *saver;
2011-11-07 13:46:58 -06:00
gboolean backup;
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Start replacing file contents");
2011-11-07 13:46:58 -06:00
/* For remote files we simply use g_file_replace_async. There is no
* backup as of yet
*/
gvsaver = async->saver;
2016-01-25 08:13:49 -06:00
saver = XEDIT_DOCUMENT_SAVER (gvsaver);
2011-11-07 13:46:58 -06:00
/* Do not make backups for remote files so they do not clutter remote systems */
2016-01-25 08:13:49 -06:00
backup = (saver->keep_backup && xedit_document_is_local (saver->document));
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "File contents size: %" G_GINT64_FORMAT, gvsaver->priv->size);
xedit_debug_message (DEBUG_SAVER, "Calling replace_async");
xedit_debug_message (DEBUG_SAVER, backup ? "Keep backup" : "Discard backup");
2011-11-07 13:46:58 -06:00
g_file_replace_async (gvsaver->priv->gfile,
NULL,
backup,
G_FILE_CREATE_NONE,
G_PRIORITY_HIGH,
async->cancellable,
(GAsyncReadyCallback) async_replace_ready_callback,
async);
}
static void
mount_ready_callback (GFile *file,
GAsyncResult *res,
AsyncData *async)
{
GError *error = NULL;
gboolean mounted;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* manual check for cancelled state */
if (g_cancellable_is_cancelled (async->cancellable))
{
async_data_free (async);
return;
}
mounted = g_file_mount_enclosing_volume_finish (file, res, &error);
if (!mounted)
{
async_failed (async, error);
}
else
{
/* try again to get the modified state */
check_modified_async (async);
}
}
static void
recover_not_mounted (AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditDocument *doc;
2011-11-07 13:46:58 -06:00
GMountOperation *mount_operation;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_LOADER);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
doc = xedit_document_saver_get_document (XEDIT_DOCUMENT_SAVER (async->saver));
mount_operation = _xedit_document_create_mount_operation (doc);
2011-11-07 13:46:58 -06:00
async->tried_mount = TRUE;
g_file_mount_enclosing_volume (async->saver->priv->gfile,
G_MOUNT_MOUNT_NONE,
mount_operation,
async->cancellable,
(GAsyncReadyCallback) mount_ready_callback,
async);
g_object_unref (mount_operation);
}
static void
check_modification_callback (GFile *source,
GAsyncResult *res,
AsyncData *async)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver;
2011-11-07 13:46:58 -06:00
GError *error = NULL;
GFileInfo *info;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_SAVER);
2011-11-07 13:46:58 -06:00
/* manually check cancelled state */
if (g_cancellable_is_cancelled (async->cancellable))
{
async_data_free (async);
return;
}
gvsaver = async->saver;
info = g_file_query_info_finish (source, res, &error);
if (info == NULL)
{
if (error->code == G_IO_ERROR_NOT_MOUNTED && !async->tried_mount)
{
recover_not_mounted (async);
g_error_free (error);
return;
}
/* it's perfectly fine if the file doesn't exist yet */
if (error->code != G_IO_ERROR_NOT_FOUND)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Error getting modification: %s", error->message);
2011-11-07 13:46:58 -06:00
async_failed (async, error);
return;
}
}
/* check if the mtime is > what we know about it (if we have it) */
if (info != NULL && g_file_info_has_attribute (info,
G_FILE_ATTRIBUTE_TIME_MODIFIED))
{
GTimeVal mtime;
GTimeVal old_mtime;
g_file_info_get_modification_time (info, &mtime);
old_mtime = gvsaver->priv->old_mtime;
if ((old_mtime.tv_sec > 0 || old_mtime.tv_usec > 0) &&
(mtime.tv_sec != old_mtime.tv_sec || mtime.tv_usec != old_mtime.tv_usec) &&
2016-01-25 08:13:49 -06:00
(XEDIT_DOCUMENT_SAVER (gvsaver)->flags & XEDIT_DOCUMENT_SAVE_IGNORE_MTIME) == 0)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "File is externally modified");
2011-11-07 13:46:58 -06:00
g_set_error (&gvsaver->priv->error,
2016-01-25 08:13:49 -06:00
XEDIT_DOCUMENT_ERROR,
XEDIT_DOCUMENT_ERROR_EXTERNALLY_MODIFIED,
2011-11-07 13:46:58 -06:00
"Externally modified");
remote_save_completed_or_failed (gvsaver, async);
g_object_unref (info);
return;
}
}
if (info != NULL)
g_object_unref (info);
/* modification check passed, start write */
begin_write (async);
}
static void
check_modified_async (AsyncData *async)
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Check externally modified");
2011-11-07 13:46:58 -06:00
g_file_query_info_async (async->saver->priv->gfile,
G_FILE_ATTRIBUTE_TIME_MODIFIED,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_HIGH,
async->cancellable,
(GAsyncReadyCallback) check_modification_callback,
async);
}
static gboolean
2016-01-25 08:13:49 -06:00
save_remote_file_real (XeditGioDocumentSaver *gvsaver)
2011-11-07 13:46:58 -06:00
{
AsyncData *async;
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_SAVER, "Starting gio save");
2011-11-07 13:46:58 -06:00
/* First find out if the file is modified externally. This requires
* a stat, but I don't think we can do this any other way
*/
async = async_data_new (gvsaver);
check_modified_async (async);
/* return false to stop timeout */
return FALSE;
}
static void
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_save (XeditDocumentSaver *saver,
2011-11-07 13:46:58 -06:00
GTimeVal *old_mtime)
{
2016-01-25 08:13:49 -06:00
XeditGioDocumentSaver *gvsaver = XEDIT_GIO_DOCUMENT_SAVER (saver);
2011-11-07 13:46:58 -06:00
gvsaver->priv->old_mtime = *old_mtime;
gvsaver->priv->gfile = g_file_new_for_uri (saver->uri);
/* saving start */
2016-01-25 08:13:49 -06:00
xedit_document_saver_saving (saver, FALSE, NULL);
2011-11-07 13:46:58 -06:00
g_timeout_add_full (G_PRIORITY_HIGH,
0,
(GSourceFunc) save_remote_file_real,
gvsaver,
NULL);
}
static goffset
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_get_file_size (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
return XEDIT_GIO_DOCUMENT_SAVER (saver)->priv->size;
2011-11-07 13:46:58 -06:00
}
static goffset
2016-01-25 08:13:49 -06:00
xedit_gio_document_saver_get_bytes_written (XeditDocumentSaver *saver)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
return XEDIT_GIO_DOCUMENT_SAVER (saver)->priv->bytes_written;
2011-11-07 13:46:58 -06:00
}