summaryrefslogtreecommitdiff
path: root/src/util/glibcompat.c
blob: fdc32af5e2b3464deb72b684383b58370abcf94b (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "glibcompat.h"

/*
 * Note that because of the GLIB_VERSION_MAX_ALLOWED constant in
 * config-post.h, allowing use of functions from newer GLib via
 * this compat impl needs a little trickery to prevent warnings
 * being emitted.
 *
 * Consider a function from newer glib-X.Y that we want to use
 *
 *    int g_foo(const char *wibble)
 *
 * We must define a function with the same signature that does
 * what we need, but with a "vir_" prefix e.g.
 *
 * void vir_g_foo(const char *wibble)
 * {
 *     #if GLIB_CHECK_VERSION(X, Y, 0)
 *        g_foo(wibble)
 *     #else
 *        g_something_equivalent_in_older_glib(wibble);
 *     #endif
 * }
 *
 * The #pragma at the top of this file turns off -Wdeprecated-declarations,
 * ensuring this wrapper function impl doesn't trigger the compiler
 * warning about using too new glib APIs. Finally in glibcompat.h we can
 * add
 *
 *   #define g_foo(a) vir_g_foo(a)
 *
 * Thus all the code elsewhere in libvirt, which *does* have the
 * -Wdeprecated-declarations warning active, can call g_foo(...) as
 * normal, without generating warnings. The cost is an extra function
 * call when using new glib, but this compat code will go away over
 * time as we update the supported platforms target.
 */

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#undef g_canonicalize_filename
#undef g_hash_table_steal_extended
#undef g_fsync
#undef g_strdup_printf
#undef g_strdup_vprintf


gchar *
vir_g_canonicalize_filename(const gchar *filename,
                            const gchar *relative_to)
{
#if GLIB_CHECK_VERSION(2, 58, 0)
    return g_canonicalize_filename(filename, relative_to);
#else /* ! GLIB_CHECK_VERSION(2, 58, 0) */
    gchar *canon, *start, *p, *q;
    guint i;

    g_return_val_if_fail(relative_to == NULL || g_path_is_absolute(relative_to), NULL);

    if (!g_path_is_absolute(filename)) {
        gchar *cwd_allocated = NULL;
        const gchar  *cwd;

        if (relative_to != NULL)
            cwd = relative_to;
        else
            cwd = cwd_allocated = g_get_current_dir();

        canon = g_build_filename(cwd, filename, NULL);
        g_free(cwd_allocated);
    } else {
        canon = g_strdup(filename);
    }

    start = (char *)g_path_skip_root(canon);

    if (start == NULL) {
        /* This shouldn't really happen, as g_get_current_dir() should
           return an absolute pathname, but bug 573843 shows this is
           not always happening */
        g_free(canon);
        return g_build_filename(G_DIR_SEPARATOR_S, filename, NULL);
    }

    /* POSIX allows double slashes at the start to
     * mean something special (as does windows too).
     * So, "//" != "/", but more than two slashes
     * is treated as "/".
     */
    i = 0;
    for (p = start - 1;
         (p >= canon) &&
             G_IS_DIR_SEPARATOR(*p);
         p--)
        i++;
    if (i > 2) {
        i -= 1;
        start -= i;
        memmove(start, start+i, strlen(start+i) + 1);
    }

    /* Make sure we're using the canonical dir separator */
    p++;
    while (p < start && G_IS_DIR_SEPARATOR(*p))
        *p++ = G_DIR_SEPARATOR;

    p = start;
    while (*p != 0) {
        if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR(p[1]))) {
            memmove(p, p+1, strlen(p+1)+1);
        } else if (p[0] == '.' && p[1] == '.' &&
                   (p[2] == 0 || G_IS_DIR_SEPARATOR(p[2]))) {
            q = p + 2;
            /* Skip previous separator */
            p = p - 2;
            if (p < start)
                p = start;
            while (p > start && !G_IS_DIR_SEPARATOR(*p))
                p--;
            if (G_IS_DIR_SEPARATOR(*p))
                *p++ = G_DIR_SEPARATOR;
            memmove(p, q, strlen(q)+1);
        } else {
            /* Skip until next separator */
            while (*p != 0 && !G_IS_DIR_SEPARATOR(*p))
                p++;

            if (*p != 0) {
                /* Canonicalize one separator */
                *p++ = G_DIR_SEPARATOR;
            }
        }

        /* Remove additional separators */
        q = p;
        while (*q && G_IS_DIR_SEPARATOR(*q))
            q++;

        if (p != q)
            memmove(p, q, strlen(q) + 1);
    }

    /* Remove trailing slashes */
    if (p > start && G_IS_DIR_SEPARATOR(*(p-1)))
        *(p-1) = 0;

    return canon;
#endif /* ! GLIB_CHECK_VERSION(2, 58, 0) */
}


gboolean
vir_g_hash_table_steal_extended(GHashTable *hash_table,
                                gconstpointer lookup_key,
                                gpointer *stolen_key,
                                gpointer *stolen_value)
{
#if GLIB_CHECK_VERSION(2, 58, 0)
    return g_hash_table_steal_extended(hash_table, lookup_key, stolen_key, stolen_value);
#else /* ! GLIB_CHECK_VERSION(2, 58, 0) */
    if (!(g_hash_table_lookup_extended(hash_table, lookup_key, stolen_key, stolen_value)))
        return FALSE;

    g_hash_table_steal(hash_table, lookup_key);

    return TRUE;
#endif /* ! GLIB_CHECK_VERSION(2, 58, 0) */
}


/* Drop when min glib >= 2.63.0 */
gint
vir_g_fsync(gint fd)
{
#ifdef G_OS_WIN32
    return _commit(fd);
#else
    return fsync(fd);
#endif
}


/* Due to a bug in glib, g_strdup_printf() nor g_strdup_vprintf()
 * abort on OOM.  It's fixed in glib's upstream. Provide our own
 * implementation until the fix gets distributed. */
char *
vir_g_strdup_printf(const char *msg, ...)
{
    va_list args;
    char *ret;
    va_start(args, msg);
    ret = g_strdup_vprintf(msg, args);
    if (!ret)
        abort();
    va_end(args);
    return ret;
}


char *
vir_g_strdup_vprintf(const char *msg, va_list args)
{
    char *ret;
    ret = g_strdup_vprintf(msg, args);
    if (!ret)
        abort();
    return ret;
}


/*
 * If the last reference to a GSource is released in a non-main
 * thread we're exposed to a race condition that causes a
 * crash:
 *
 *    https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1358
 *
 * Thus we're using an idle func to release our ref...
 *
 * ...but this imposes a significant performance penalty on
 * I/O intensive workloads which are sensitive to the iterations
 * of the event loop, so avoid the workaround if we know we have
 * new enough glib.
 *
 * The function below is used from a header file definition.
 *
 * Drop when min glib >= 2.64.0
 */
#if GLIB_CHECK_VERSION(2, 64, 0)
void vir_g_source_unref(GSource *src, GMainContext *ctx G_GNUC_UNUSED)
{
    g_source_unref(src);
}
#else

static gboolean
virEventGLibSourceUnrefIdle(gpointer data)
{
    GSource *src = data;

    g_source_unref(src);

    return FALSE;
}

void vir_g_source_unref(GSource *src, GMainContext *ctx)
{
    GSource *idle = g_idle_source_new();

    g_source_set_callback(idle, virEventGLibSourceUnrefIdle, src, NULL);

    g_source_attach(idle, ctx);

    g_source_unref(idle);
}

#endif