aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-file-utils.c
blob: 3f5f85488a803aee87247913f9e5021a4ede8459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *
 * 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)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#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/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;
}