New upstream version 2017.4

This commit is contained in:
Simon McVittie committed 2017-04-17 15:25:30 +01:00
commit 0b7290b235
168 files changed
+3573 -2254

No files matched your search

+11 -2
View File
@@ -52,9 +52,18 @@ libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic
libglnx_la_LIBADD = $(libglnx_libs)
TESTS += test-libglnx-xattrs
libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors
TESTS += $(libglnx_tests)
check_PROGRAMS += test-libglnx-xattrs
check_PROGRAMS += $(libglnx_tests)
test_libglnx_xattrs_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-xattrs.c
test_libglnx_xattrs_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_xattrs_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_fdio_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-fdio.c
test_libglnx_fdio_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_fdio_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_errors_SOURCES = $(libglnx_srcpath)/tests/test-libglnx-errors.c
test_libglnx_errors_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la
+11 -2
View File
@@ -52,9 +52,18 @@ libglnx_la_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
libglnx_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^glnx_" -no-undefined -export-dynamic
libglnx_la_LIBADD = $(libglnx_libs)
TESTS += test-libglnx-xattrs
libglnx_tests = test-libglnx-xattrs test-libglnx-fdio test-libglnx-errors
TESTS += $(libglnx_tests)
check_PROGRAMS += test-libglnx-xattrs
check_PROGRAMS += $(libglnx_tests)
test_libglnx_xattrs_SOURCES = libglnx/tests/test-libglnx-xattrs.c
test_libglnx_xattrs_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_xattrs_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_fdio_SOURCES = libglnx/tests/test-libglnx-fdio.c
test_libglnx_fdio_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_fdio_LDADD = $(libglnx_libs) libglnx.la
test_libglnx_errors_SOURCES = libglnx/tests/test-libglnx-errors.c
test_libglnx_errors_CFLAGS = $(AM_CFLAGS) $(libglnx_cflags)
test_libglnx_errors_LDADD = $(libglnx_libs) libglnx.la
+2 -4
View File
@@ -68,8 +68,7 @@ glnx_opendirat (int dfd,
int ret = glnx_opendirat_with_errno (dfd, path, follow);
if (ret == -1)
{
glnx_set_prefix_error_from_errno (error, "%s", "openat");
return FALSE;
return glnx_throw_errno_prefix (error, "openat");
}
*out_fd = ret;
return TRUE;
@@ -339,8 +338,7 @@ glnx_mkdtempat (int dfd,
/* Any other error will apply also to other names we might
* try, and there are 2^32 or so of them, so give up now.
*/
glnx_set_prefix_error_from_errno (error, "%s", "mkdirat");
return FALSE;
return glnx_throw_errno_prefix (error, "mkdirat");
}
return TRUE;
+13 -22
View File
@@ -24,30 +24,21 @@
#include <glnx-errors.h>
void
glnx_real_set_prefix_error_from_errno (GError **error,
gint errsv,
const char *format,
...)
glnx_real_set_prefix_error_from_errno_va (GError **error,
gint errsv,
const char *format,
va_list args)
{
if (!error)
return;
else
{
GString *buf = g_string_new ("");
va_list args;
va_start (args, format);
g_string_append_vprintf (buf, format, args);
va_end (args);
g_string_append (buf, ": ");
g_string_append (buf, g_strerror (errsv));
g_set_error_literal (error,
G_IO_ERROR,
g_io_error_from_errno (errsv),
buf->str);
g_string_free (buf, TRUE);
errno = errsv;
}
/* TODO - enhance GError to have a "set and take ownership" API */
g_autoptr(GString) buf = g_string_new ("");
g_string_append_vprintf (buf, format, args);
g_string_append (buf, ": ");
g_string_append (buf, g_strerror (errsv));
g_set_error_literal (error,
G_IO_ERROR,
g_io_error_from_errno (errsv),
buf->str);
}
+90 -14
View File
@@ -25,25 +25,101 @@
G_BEGIN_DECLS
/* Set @error with G_IO_ERROR/G_IO_ERROR_FAILED.
*
* This function returns %FALSE so it can be used conveniently in a single
* statement:
*
* ``
* if (strcmp (foo, "somevalue") != 0)
* return glnx_throw (error, "key must be somevalue, not '%s'", foo);
* ```
*/
static inline gboolean G_GNUC_PRINTF (2,3)
glnx_throw (GError **error, const char *fmt, ...)
{
if (error == NULL)
return FALSE;
va_list args;
va_start (args, fmt);
GError *new = g_error_new_valist (G_IO_ERROR, G_IO_ERROR_FAILED, fmt, args);
va_end (args);
g_propagate_error (error, g_steal_pointer (&new));
return FALSE;
}
/* Set @error using the value of `g_strerror (errno)`.
*
* This function returns %FALSE so it can be used conveniently in a single
* statement:
*
* ``
* if (unlinkat (fd, somepathname) < 0)
* return glnx_throw_errno (error);
* ```
*/
static inline gboolean
glnx_throw_errno (GError **error)
{
/* Save the value of errno, in case one of the
* intermediate function calls happens to set it.
*/
int errsv = errno;
g_set_error_literal (error, G_IO_ERROR,
g_io_error_from_errno (errsv),
g_strerror (errsv));
/* We also restore the value of errno, since that's
* what was done in a long-ago libgsystem commit
* https://git.gnome.org/browse/libgsystem/commit/?id=ed106741f7a0596dc8b960b31fdae671d31d666d
* but I certainly can't remember now why I did that.
*/
errno = errsv;
return FALSE;
}
/* Implementation detail of glnx_throw_errno_prefix() */
void glnx_real_set_prefix_error_from_errno_va (GError **error,
gint errsv,
const char *format,
va_list args) G_GNUC_PRINTF (3,0);
/* Set @error using the value of `$prefix: g_strerror (errno)` where `$prefix`
* is computed via printf @fmt.
*
* This function returns %FALSE so it can be used conveniently in a single
* statement:
*
* ```
* return glnx_throw_errno_prefix (error, "unlinking %s", pathname);
* ```
*/
static inline gboolean G_GNUC_PRINTF (2,3)
glnx_throw_errno_prefix (GError **error, const char *fmt, ...)
{
int errsv = errno;
va_list args;
va_start (args, fmt);
glnx_real_set_prefix_error_from_errno_va (error, errsv, fmt, args);
va_end (args);
/* See comment above about preserving errno */
errno = errsv;
return FALSE;
}
/* BEGIN LEGACY APIS */
#define glnx_set_error_from_errno(error) \
do { \
int errsv = errno; \
g_set_error_literal (error, G_IO_ERROR, \
g_io_error_from_errno (errsv), \
g_strerror (errsv)); \
errno = errsv; \
glnx_throw_errno (error); \
} while (0);
#define glnx_set_prefix_error_from_errno(error, format, args...) \
do { \
int errsv = errno; \
glnx_real_set_prefix_error_from_errno (error, errsv, format, args); \
errno = errsv; \
#define glnx_set_prefix_error_from_errno(error, format, args...) \
do { \
glnx_set_error_from_errno (error); \
g_prefix_error (error, format, args); \
} while (0);
void glnx_real_set_prefix_error_from_errno (GError **error,
gint errsv,
const char *format,
...) G_GNUC_PRINTF (3,4);
G_END_DECLS
+68 -23
View File
@@ -54,11 +54,13 @@
sizeof(type) <= 4 ? 10 : \
sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
static gboolean
rename_file_noreplace_at (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath,
gboolean ignore_eexist,
GError **error)
/* An implementation of renameat2(..., RENAME_NOREPLACE)
* with fallback to a non-atomic version.
*/
int
glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath)
{
#ifndef ENABLE_WRPSEUDO_COMPAT
if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE) < 0)
@@ -66,9 +68,35 @@ rename_file_noreplace_at (int olddirfd, const char *oldpath,
if (errno == EINVAL || errno == ENOSYS)
{
/* Fall through */
;
}
else if (errno == EEXIST && ignore_eexist)
else
{
return -1;
}
}
else
return TRUE;
#endif
if (linkat (olddirfd, oldpath, newdirfd, newpath, 0) < 0)
return -1;
if (unlinkat (olddirfd, oldpath, 0) < 0)
return -1;
return 0;
}
static gboolean
rename_file_noreplace_at (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath,
gboolean ignore_eexist,
GError **error)
{
if (glnx_renameat2_noreplace (olddirfd, oldpath,
newdirfd, newpath) < 0)
{
if (errno == EEXIST && ignore_eexist)
{
(void) unlinkat (olddirfd, oldpath, 0);
return TRUE;
@@ -79,29 +107,46 @@ rename_file_noreplace_at (int olddirfd, const char *oldpath,
return FALSE;
}
}
else
return TRUE;
#endif
return TRUE;
}
if (linkat (olddirfd, oldpath, newdirfd, newpath, 0) < 0)
/* An implementation of renameat2(..., RENAME_EXCHANGE)
* with fallback to a non-atomic version.
*/
int
glnx_renameat2_exchange (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath)
{
#ifndef ENABLE_WRPSEUDO_COMPAT
if (renameat2 (olddirfd, oldpath, newdirfd, newpath, RENAME_EXCHANGE) == 0)
return 0;
else
{
if (errno == EEXIST && ignore_eexist)
/* Fall through */
;
if (errno == ENOSYS || errno == EINVAL)
{
/* Fall through */
}
else
{
glnx_set_error_from_errno (error);
return FALSE;
return -1;
}
}
if (unlinkat (olddirfd, oldpath, 0) < 0)
{
glnx_set_error_from_errno (error);
return FALSE;
}
#endif
return TRUE;
/* Fallback */
{ const char *old_tmp_name = glnx_strjoina (oldpath, ".XXXXXX");
/* Move old out of the way */
if (renameat (olddirfd, oldpath, olddirfd, old_tmp_name) < 0)
return -1;
/* Now move new into its place */
if (renameat (newdirfd, newpath, olddirfd, oldpath) < 0)
return -1;
/* And finally old(tmp) into new */
if (renameat (olddirfd, old_tmp_name, newdirfd, newpath) < 0)
return -1;
}
return 0;
}
gboolean
+6
View File
@@ -150,4 +150,10 @@ glnx_stream_fstat (GFileDescriptorBased *stream,
struct stat *stbuf,
GError **error);
int glnx_renameat2_noreplace (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath);
int glnx_renameat2_exchange (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath);
G_END_DECLS
+3
View File
@@ -48,5 +48,8 @@
#ifndef RENAME_NOREPLACE
#define RENAME_NOREPLACE (1 << 0)
#endif
#ifndef RENAME_EXCHANGE
#define RENAME_EXCHANGE (1 << 1)
#endif
#include "glnx-missing-syscall.h"
+79
View File
@@ -0,0 +1,79 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2017 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 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "libglnx.h"
#include <glib.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <string.h>
static void
test_error_throw (void)
{
g_autoptr(GError) error = NULL;
g_assert (!glnx_throw (&error, "foo: %s %d", "hello", 42));
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
g_assert_cmpstr (error->message, ==, "foo: hello 42");
}
static void
test_error_errno (void)
{
g_autoptr(GError) error = NULL;
const char noent_path[] = "/enoent-this-should-not-exist";
int fd;
fd = open (noent_path, O_RDONLY);
if (fd < 0)
{
g_assert (!glnx_throw_errno (&error));
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
g_clear_error (&error);
}
else
g_assert_cmpint (fd, ==, -1);
fd = open (noent_path, O_RDONLY);
if (fd < 0)
{
g_assert (!glnx_throw_errno_prefix (&error, "Failed to open %s", noent_path));
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
g_assert (g_str_has_prefix (error->message, glnx_strjoina ("Failed to open ", noent_path)));
g_clear_error (&error);
}
else
g_assert_cmpint (fd, ==, -1);
}
int main (int argc, char **argv)
{
int ret;
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/error-throw", test_error_throw);
g_test_add_func ("/error-errno", test_error_errno);
ret = g_test_run();
return ret;
}
+155
View File
@@ -0,0 +1,155 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2017 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 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "libglnx.h"
#include <glib.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <err.h>
#include <string.h>
static gboolean
renameat_test_setup (int *out_srcfd, int *out_destfd,
GError **error)
{
glnx_fd_close int srcfd = -1;
glnx_fd_close int destfd = -1;
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL);
if (mkdir ("srcdir", 0755) < 0)
err (1, "mkdir");
if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error))
return FALSE;
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL);
if (mkdir ("destdir", 0755) < 0)
err (1, "mkdir");
if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error))
return FALSE;
if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"),
GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
return FALSE;
if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"),
GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
return FALSE;
*out_srcfd = srcfd; srcfd = -1;
*out_destfd = destfd; destfd = -1;
return TRUE;
}
static void
test_renameat2_noreplace (void)
{
g_autoptr(GError) local_error = NULL;
GError **error = &local_error;
glnx_fd_close int srcfd = -1;
glnx_fd_close int destfd = -1;
struct stat stbuf;
if (!renameat_test_setup (&srcfd, &destfd, error))
goto out;
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0)
g_assert_not_reached ();
else
{
g_assert_cmpint (errno, ==, EEXIST);
}
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
if (fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
g_assert_not_reached ();
else
g_assert_cmpint (errno, ==, ENOENT);
out:
g_assert_no_error (local_error);
}
static void
test_renameat2_exchange (void)
{
g_autoptr(GError) local_error = NULL;
GError **error = &local_error;
glnx_fd_close int srcfd = -1;
glnx_fd_close int destfd = -1;
struct stat stbuf;
if (!renameat_test_setup (&srcfd, &destfd, error))
goto out;
if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
/* Ensure the dir fds are the same */
if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
if (fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
/* But the dirs should be swapped */
if (fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
if (fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
out:
g_assert_no_error (local_error);
}
int main (int argc, char **argv)
{
int ret;
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace);
g_test_add_func ("/renameat2-exchange", test_renameat2_exchange);
ret = g_test_run();
return ret;
}