New upstream version 2017.12
This commit is contained in:
commit
d5964d6850
283 files changed
+4566
-2286
No files matched your search
@@ -0,0 +1,34 @@
|
||||
/* -*- 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef GError _GLnxTestAutoError;
|
||||
static inline void
|
||||
_glnx_test_auto_error_cleanup (_GLnxTestAutoError *autoerror)
|
||||
{
|
||||
g_assert_no_error (autoerror);
|
||||
/* We could add a clear call here, but no point...we'll have aborted */
|
||||
}
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(_GLnxTestAutoError, _glnx_test_auto_error_cleanup);
|
||||
|
||||
#define _GLNX_TEST_DECLARE_ERROR(local_error, error) \
|
||||
g_autoptr(_GLnxTestAutoError) local_error = NULL; \
|
||||
GError **error = &local_error
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <err.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libglnx-testlib.h"
|
||||
|
||||
static gboolean
|
||||
renameat_test_setup (int *out_srcfd, int *out_destfd,
|
||||
GError **error)
|
||||
@@ -59,14 +61,13 @@ renameat_test_setup (int *out_srcfd, int *out_destfd,
|
||||
static void
|
||||
test_renameat2_noreplace (void)
|
||||
{
|
||||
g_autoptr(GError) local_error = NULL;
|
||||
GError **error = &local_error;
|
||||
_GLNX_TEST_DECLARE_ERROR(local_error, 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;
|
||||
return;
|
||||
|
||||
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0)
|
||||
g_assert_not_reached ();
|
||||
@@ -76,117 +77,153 @@ test_renameat2_noreplace (void)
|
||||
}
|
||||
|
||||
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0)
|
||||
{
|
||||
glnx_set_error_from_errno (error);
|
||||
goto out;
|
||||
}
|
||||
return (void)glnx_throw_errno_prefix (error, "renameat");
|
||||
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
goto out;
|
||||
return;
|
||||
|
||||
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_TEST_DECLARE_ERROR(local_error, 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;
|
||||
return;
|
||||
|
||||
if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0)
|
||||
{
|
||||
glnx_set_error_from_errno (error);
|
||||
goto out;
|
||||
}
|
||||
return (void)glnx_throw_errno_prefix (error, "renameat");
|
||||
|
||||
/* 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;
|
||||
}
|
||||
struct stat stbuf;
|
||||
if (!glnx_fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
/* 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);
|
||||
if (!glnx_fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
if (!glnx_fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
test_tmpfile (void)
|
||||
{
|
||||
g_autoptr(GError) local_error = NULL;
|
||||
GError **error = &local_error;
|
||||
_GLNX_TEST_DECLARE_ERROR(local_error, error);
|
||||
|
||||
g_auto(GLnxTmpfile) tmpf = { 0, };
|
||||
|
||||
if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error))
|
||||
goto out;
|
||||
|
||||
return;
|
||||
if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0)
|
||||
{
|
||||
(void)glnx_throw_errno_prefix (error, "write");
|
||||
goto out;
|
||||
}
|
||||
|
||||
return (void)glnx_throw_errno_prefix (error, "write");
|
||||
if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error))
|
||||
goto out;
|
||||
|
||||
out:
|
||||
g_assert_no_error (local_error);
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
test_stdio_file (void)
|
||||
{
|
||||
g_autoptr(GError) local_error = NULL;
|
||||
GError **error = &local_error;
|
||||
_GLNX_TEST_DECLARE_ERROR(local_error, error);
|
||||
g_auto(GLnxTmpfile) tmpf = { 0, };
|
||||
g_autoptr(FILE) f = NULL;
|
||||
|
||||
if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error))
|
||||
goto out;
|
||||
return;
|
||||
f = fdopen (tmpf.fd, "w");
|
||||
tmpf.fd = -1; /* Ownership was transferred via fdopen() */
|
||||
if (!f)
|
||||
{
|
||||
(void)glnx_throw_errno_prefix (error, "fdopen");
|
||||
goto out;
|
||||
}
|
||||
|
||||
return (void)glnx_throw_errno_prefix (error, "fdopen");
|
||||
if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello"))
|
||||
{
|
||||
(void)glnx_throw_errno_prefix (error, "fwrite");
|
||||
goto out;
|
||||
}
|
||||
return (void)glnx_throw_errno_prefix (error, "fwrite");
|
||||
if (!glnx_stdio_file_flush (f, error))
|
||||
goto out;
|
||||
return;
|
||||
}
|
||||
|
||||
out:
|
||||
static void
|
||||
test_fstatat (void)
|
||||
{
|
||||
_GLNX_TEST_DECLARE_ERROR(local_error, error);
|
||||
struct stat stbuf = { 0, };
|
||||
|
||||
if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", &stbuf, 0, error))
|
||||
return;
|
||||
g_assert_cmpint (errno, ==, 0);
|
||||
g_assert_no_error (local_error);
|
||||
g_assert (S_ISDIR (stbuf.st_mode));
|
||||
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", &stbuf, 0, error))
|
||||
return;
|
||||
g_assert_cmpint (errno, ==, ENOENT);
|
||||
g_assert_no_error (local_error);
|
||||
}
|
||||
|
||||
static void
|
||||
test_filecopy (void)
|
||||
{
|
||||
_GLNX_TEST_DECLARE_ERROR(local_error, error);
|
||||
g_auto(GLnxTmpfile) tmpf = { 0, };
|
||||
const char foo[] = "foo";
|
||||
struct stat stbuf;
|
||||
|
||||
if (!glnx_ensure_dir (AT_FDCWD, "subdir", 0755, error))
|
||||
return;
|
||||
|
||||
if (!glnx_file_replace_contents_at (AT_FDCWD, foo, (guint8*)foo, sizeof (foo),
|
||||
GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
|
||||
return;
|
||||
|
||||
/* Copy it into both the same dir and a subdir */
|
||||
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
|
||||
GLNX_FILE_COPY_NOXATTRS, NULL, error))
|
||||
return;
|
||||
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "subdir/bar",
|
||||
GLNX_FILE_COPY_NOXATTRS, NULL, error))
|
||||
return;
|
||||
if (!glnx_fstatat (AT_FDCWD, "subdir/bar", &stbuf, 0, error))
|
||||
return;
|
||||
|
||||
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
|
||||
GLNX_FILE_COPY_NOXATTRS, NULL, error))
|
||||
g_assert_not_reached ();
|
||||
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
|
||||
g_clear_error (&local_error);
|
||||
|
||||
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
|
||||
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
|
||||
NULL, error))
|
||||
return;
|
||||
|
||||
if (symlinkat ("nosuchtarget", AT_FDCWD, "link") < 0)
|
||||
return (void) glnx_throw_errno_prefix (error, "symlinkat");
|
||||
|
||||
/* Shouldn't be able to overwrite a symlink without GLNX_FILE_COPY_OVERWRITE */
|
||||
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
|
||||
GLNX_FILE_COPY_NOXATTRS,
|
||||
NULL, error))
|
||||
g_assert_not_reached ();
|
||||
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
|
||||
g_clear_error (&local_error);
|
||||
|
||||
/* Test overwriting symlink */
|
||||
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
|
||||
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
|
||||
NULL, error))
|
||||
return;
|
||||
|
||||
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchtarget", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
g_assert_cmpint (errno, ==, ENOENT);
|
||||
g_assert_no_error (local_error);
|
||||
|
||||
if (!glnx_fstatat (AT_FDCWD, "link", &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
||||
return;
|
||||
g_assert (S_ISREG (stbuf.st_mode));
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
@@ -197,8 +234,10 @@ int main (int argc, char **argv)
|
||||
|
||||
g_test_add_func ("/tmpfile", test_tmpfile);
|
||||
g_test_add_func ("/stdio-file", test_stdio_file);
|
||||
g_test_add_func ("/filecopy", test_filecopy);
|
||||
g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace);
|
||||
g_test_add_func ("/renameat2-exchange", test_renameat2_exchange);
|
||||
g_test_add_func ("/fstat", test_fstatat);
|
||||
|
||||
ret = g_test_run();
|
||||
|
||||
|
||||
@@ -223,19 +223,17 @@ test_xattr_races (void)
|
||||
GThread *threads[nprocs];
|
||||
g_autoptr(GError) local_error = NULL;
|
||||
GError **error = &local_error;
|
||||
glnx_fd_close int dfd = -1;
|
||||
g_autofree char *tmpdir = g_strdup_printf ("%s/libglnx-xattrs-XXXXXX",
|
||||
getenv ("TMPDIR") ?: "/var/tmp");
|
||||
g_auto(GLnxTmpDir) tmpdir = { 0, };
|
||||
g_autofree char *tmpdir_path = g_strdup_printf ("%s/libglnx-xattrs-XXXXXX",
|
||||
getenv ("TMPDIR") ?: "/var/tmp");
|
||||
guint nread = 0;
|
||||
|
||||
if (!glnx_mkdtempat (AT_FDCWD, tmpdir, 0700, error))
|
||||
goto out;
|
||||
|
||||
if (!glnx_opendirat (AT_FDCWD, tmpdir, TRUE, &dfd, error))
|
||||
if (!glnx_mkdtempat (AT_FDCWD, tmpdir_path, 0700,
|
||||
&tmpdir, error))
|
||||
goto out;
|
||||
|
||||
/* Support people building/testing on tmpfs https://github.com/flatpak/flatpak/issues/686 */
|
||||
if (fsetxattr (dfd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
|
||||
if (fsetxattr (tmpdir.fd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
|
||||
{
|
||||
if (errno == EOPNOTSUPP)
|
||||
{
|
||||
@@ -252,7 +250,7 @@ test_xattr_races (void)
|
||||
for (guint i = 0; i < nprocs; i++)
|
||||
{
|
||||
struct XattrWorker *worker = &wdata[i];
|
||||
worker->dfd = dfd;
|
||||
worker->dfd = tmpdir.fd;
|
||||
worker->is_writer = i % 2 == 0;
|
||||
threads[i] = g_thread_new (NULL, xattr_thread, worker);
|
||||
}
|
||||
@@ -267,8 +265,6 @@ test_xattr_races (void)
|
||||
|
||||
g_print ("Read %u xattrs race free!\n", nread);
|
||||
|
||||
(void) glnx_shutil_rm_rf_at (AT_FDCWD, tmpdir, NULL, NULL);
|
||||
|
||||
out:
|
||||
g_assert_no_error (local_error);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user